1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Author: Wez Furlong <wez@php.net> |
14 | Frank M. Kromann <frank@kromann.info> |
15 +----------------------------------------------------------------------+
16 */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #include "php.h"
23 #include "php_ini.h"
24 #include "ext/standard/info.h"
25 #include "ext/pdo/php_pdo.h"
26 #include "ext/pdo/php_pdo_driver.h"
27 #include "php_pdo_dblib.h"
28 #include "php_pdo_dblib_int.h"
29 #include "zend_exceptions.h"
30 #include "pdo_dblib_arginfo.h"
31
32 ZEND_DECLARE_MODULE_GLOBALS(dblib)
33 static PHP_GINIT_FUNCTION(dblib);
34
35 static zend_class_entry *PdoDblib_ce;
36
37 static const zend_module_dep pdo_dblib_deps[] = {
38 ZEND_MOD_REQUIRED("pdo")
39 ZEND_MOD_END
40 };
41
42 #ifdef PDO_DBLIB_IS_MSSQL
43 zend_module_entry pdo_mssql_module_entry = {
44 #else
45 zend_module_entry pdo_dblib_module_entry = {
46 #endif
47 STANDARD_MODULE_HEADER_EX, NULL,
48 pdo_dblib_deps,
49 #ifdef PDO_DBLIB_IS_MSSQL
50 "pdo_mssql",
51 #elif defined(PHP_WIN32)
52 "pdo_sybase",
53 #else
54 "pdo_dblib",
55 #endif
56 NULL,
57 PHP_MINIT(pdo_dblib),
58 PHP_MSHUTDOWN(pdo_dblib),
59 NULL,
60 PHP_RSHUTDOWN(pdo_dblib),
61 PHP_MINFO(pdo_dblib),
62 PHP_PDO_DBLIB_VERSION,
63 PHP_MODULE_GLOBALS(dblib),
64 PHP_GINIT(dblib),
65 NULL,
66 NULL,
67 STANDARD_MODULE_PROPERTIES_EX
68 };
69
70 #if defined(COMPILE_DL_PDO_DBLIB) || defined(COMPILE_DL_PDO_MSSQL)
71 #ifdef ZTS
72 ZEND_TSRMLS_CACHE_DEFINE()
73 #endif
74 #ifdef PDO_DBLIB_IS_MSSQL
ZEND_GET_MODULE(pdo_mssql)75 ZEND_GET_MODULE(pdo_mssql)
76 #else
77 ZEND_GET_MODULE(pdo_dblib)
78 #endif
79 #endif
80
81 int pdo_dblib_error_handler(DBPROCESS *dbproc, int severity, int dberr,
82 int oserr, char *dberrstr, char *oserrstr)
83 {
84 pdo_dblib_err *einfo;
85 char *state = "HY000";
86
87 if(dbproc) {
88 einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
89 if (!einfo) einfo = &DBLIB_G(err);
90 } else {
91 einfo = &DBLIB_G(err);
92 }
93
94 einfo->severity = severity;
95 einfo->oserr = oserr;
96 einfo->dberr = dberr;
97
98 if (einfo->oserrstr) {
99 efree(einfo->oserrstr);
100 }
101 if (einfo->dberrstr) {
102 efree(einfo->dberrstr);
103 }
104 if (oserrstr) {
105 einfo->oserrstr = estrdup(oserrstr);
106 } else {
107 einfo->oserrstr = NULL;
108 }
109 if (dberrstr) {
110 einfo->dberrstr = estrdup(dberrstr);
111 } else {
112 einfo->dberrstr = NULL;
113 }
114
115 switch (dberr) {
116 case SYBESEOF:
117 case SYBEFCON: state = "01002"; break;
118 case SYBEMEM: state = "HY001"; break;
119 case SYBEPWD: state = "28000"; break;
120 }
121 strcpy(einfo->sqlstate, state);
122
123 return INT_CANCEL;
124 }
125
pdo_dblib_msg_handler(DBPROCESS * dbproc,DBINT msgno,int msgstate,int severity,char * msgtext,char * srvname,char * procname,int line)126 int pdo_dblib_msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate,
127 int severity, char *msgtext, char *srvname, char *procname, int line)
128 {
129 pdo_dblib_err *einfo;
130
131 if (severity) {
132 einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
133 if (!einfo) {
134 einfo = &DBLIB_G(err);
135 }
136
137 if (einfo->lastmsg) {
138 efree(einfo->lastmsg);
139 }
140
141 einfo->lastmsg = estrdup(msgtext);
142 }
143
144 return 0;
145 }
146
pdo_dblib_err_dtor(pdo_dblib_err * err)147 void pdo_dblib_err_dtor(pdo_dblib_err *err)
148 {
149 if (!err) {
150 return;
151 }
152
153 if (err->dberrstr) {
154 efree(err->dberrstr);
155 err->dberrstr = NULL;
156 }
157 if (err->lastmsg) {
158 efree(err->lastmsg);
159 err->lastmsg = NULL;
160 }
161 if (err->oserrstr) {
162 efree(err->oserrstr);
163 err->oserrstr = NULL;
164 }
165 }
166
PHP_GINIT_FUNCTION(dblib)167 static PHP_GINIT_FUNCTION(dblib)
168 {
169 #if defined(ZTS) && (defined(COMPILE_DL_PDO_DBLIB) || defined(COMPILE_DL_PDO_MSSQL))
170 ZEND_TSRMLS_CACHE_UPDATE();
171 #endif
172 memset(dblib_globals, 0, sizeof(*dblib_globals));
173 dblib_globals->err.sqlstate = dblib_globals->sqlstate;
174 }
175
PHP_RSHUTDOWN_FUNCTION(pdo_dblib)176 PHP_RSHUTDOWN_FUNCTION(pdo_dblib)
177 {
178 if (DBLIB_G(err).oserrstr) {
179 efree(DBLIB_G(err).oserrstr);
180 DBLIB_G(err).oserrstr = NULL;
181 }
182 if (DBLIB_G(err).dberrstr) {
183 efree(DBLIB_G(err).dberrstr);
184 DBLIB_G(err).dberrstr = NULL;
185 }
186 if (DBLIB_G(err).lastmsg) {
187 efree(DBLIB_G(err).lastmsg);
188 DBLIB_G(err).lastmsg = NULL;
189 }
190 return SUCCESS;
191 }
192
PHP_MINIT_FUNCTION(pdo_dblib)193 PHP_MINIT_FUNCTION(pdo_dblib)
194 {
195 REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_CONNECTION_TIMEOUT", (long) PDO_DBLIB_ATTR_CONNECTION_TIMEOUT);
196 REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_QUERY_TIMEOUT", (long) PDO_DBLIB_ATTR_QUERY_TIMEOUT);
197 REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER", (long) PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER);
198 REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_VERSION", (long) PDO_DBLIB_ATTR_VERSION);
199 REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_TDS_VERSION", (long) PDO_DBLIB_ATTR_TDS_VERSION);
200 REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_SKIP_EMPTY_ROWSETS", (long) PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS);
201 REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_DATETIME_CONVERT", (long) PDO_DBLIB_ATTR_DATETIME_CONVERT);
202
203 if (FAIL == dbinit()) {
204 return FAILURE;
205 }
206
207 PdoDblib_ce = register_class_Pdo_Dblib(pdo_dbh_ce);
208 PdoDblib_ce->create_object = pdo_dbh_new;
209
210 if (FAILURE == php_pdo_register_driver(&pdo_dblib_driver)) {
211 return FAILURE;
212 }
213
214 #ifndef PHP_DBLIB_IS_MSSQL
215 dberrhandle((EHANDLEFUNC) pdo_dblib_error_handler);
216 dbmsghandle((MHANDLEFUNC) pdo_dblib_msg_handler);
217 #endif
218
219 return php_pdo_register_driver_specific_ce(&pdo_dblib_driver, PdoDblib_ce);
220 }
221
PHP_MSHUTDOWN_FUNCTION(pdo_dblib)222 PHP_MSHUTDOWN_FUNCTION(pdo_dblib)
223 {
224 php_pdo_unregister_driver(&pdo_dblib_driver);
225 dbexit();
226 return SUCCESS;
227 }
228
PHP_MINFO_FUNCTION(pdo_dblib)229 PHP_MINFO_FUNCTION(pdo_dblib)
230 {
231 php_info_print_table_start();
232 php_info_print_table_row(2, "PDO Driver for "
233 #ifdef PDO_DBLIB_IS_MSSQL
234 "MSSQL"
235 #elif defined(PHP_WIN32)
236 "FreeTDS/Sybase/MSSQL"
237 #else
238 "FreeTDS/Sybase"
239 #endif
240 " DB-lib", "enabled");
241 php_info_print_table_row(2, "Flavour", PDO_DBLIB_FLAVOUR);
242 php_info_print_table_end();
243 }
244