xref: /PHP-8.2/ext/pdo_dblib/pdo_dblib.c (revision 01b3fc03)
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 "pdo/php_pdo.h"
26 #include "pdo/php_pdo_driver.h"
27 #include "php_pdo_dblib.h"
28 #include "php_pdo_dblib_int.h"
29 #include "zend_exceptions.h"
30 
31 ZEND_DECLARE_MODULE_GLOBALS(dblib)
32 static PHP_GINIT_FUNCTION(dblib);
33 
34 static const zend_module_dep pdo_dblib_deps[] = {
35 	ZEND_MOD_REQUIRED("pdo")
36 	ZEND_MOD_END
37 };
38 
39 #ifdef PDO_DBLIB_IS_MSSQL
40 zend_module_entry pdo_mssql_module_entry = {
41 #else
42 zend_module_entry pdo_dblib_module_entry = {
43 #endif
44 	STANDARD_MODULE_HEADER_EX, NULL,
45 	pdo_dblib_deps,
46 #ifdef PDO_DBLIB_IS_MSSQL
47 	"pdo_mssql",
48 #elif defined(PHP_WIN32)
49 	"pdo_sybase",
50 #else
51 	"pdo_dblib",
52 #endif
53 	NULL,
54 	PHP_MINIT(pdo_dblib),
55 	PHP_MSHUTDOWN(pdo_dblib),
56 	NULL,
57 	PHP_RSHUTDOWN(pdo_dblib),
58 	PHP_MINFO(pdo_dblib),
59 	PHP_PDO_DBLIB_VERSION,
60 	PHP_MODULE_GLOBALS(dblib),
61 	PHP_GINIT(dblib),
62 	NULL,
63 	NULL,
64 	STANDARD_MODULE_PROPERTIES_EX
65 };
66 
67 #if defined(COMPILE_DL_PDO_DBLIB) || defined(COMPILE_DL_PDO_MSSQL)
68 #ifdef ZTS
69 ZEND_TSRMLS_CACHE_DEFINE()
70 #endif
71 #ifdef PDO_DBLIB_IS_MSSQL
ZEND_GET_MODULE(pdo_mssql)72 ZEND_GET_MODULE(pdo_mssql)
73 #else
74 ZEND_GET_MODULE(pdo_dblib)
75 #endif
76 #endif
77 
78 int pdo_dblib_error_handler(DBPROCESS *dbproc, int severity, int dberr,
79 	int oserr, char *dberrstr, char *oserrstr)
80 {
81 	pdo_dblib_err *einfo;
82 	char *state = "HY000";
83 
84 	if(dbproc) {
85 		einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
86 		if (!einfo) einfo = &DBLIB_G(err);
87 	} else {
88 		einfo = &DBLIB_G(err);
89 	}
90 
91 	einfo->severity = severity;
92 	einfo->oserr = oserr;
93 	einfo->dberr = dberr;
94 
95 	if (einfo->oserrstr) {
96 		efree(einfo->oserrstr);
97 	}
98 	if (einfo->dberrstr) {
99 		efree(einfo->dberrstr);
100 	}
101 	if (oserrstr) {
102 		einfo->oserrstr = estrdup(oserrstr);
103 	} else {
104 		einfo->oserrstr = NULL;
105 	}
106 	if (dberrstr) {
107 		einfo->dberrstr = estrdup(dberrstr);
108 	} else {
109 		einfo->dberrstr = NULL;
110 	}
111 
112 	switch (dberr) {
113 		case SYBESEOF:
114 		case SYBEFCON:	state = "01002"; break;
115 		case SYBEMEM:	state = "HY001"; break;
116 		case SYBEPWD:	state = "28000"; break;
117 	}
118 	strcpy(einfo->sqlstate, state);
119 
120 	return INT_CANCEL;
121 }
122 
pdo_dblib_msg_handler(DBPROCESS * dbproc,DBINT msgno,int msgstate,int severity,char * msgtext,char * srvname,char * procname,int line)123 int pdo_dblib_msg_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate,
124 	int severity, char *msgtext, char *srvname, char *procname, int line)
125 {
126 	pdo_dblib_err *einfo;
127 
128 	if (severity) {
129 		einfo = (pdo_dblib_err*)dbgetuserdata(dbproc);
130 		if (!einfo) {
131 			einfo = &DBLIB_G(err);
132 		}
133 
134 		if (einfo->lastmsg) {
135 			efree(einfo->lastmsg);
136 		}
137 
138 		einfo->lastmsg = estrdup(msgtext);
139 	}
140 
141 	return 0;
142 }
143 
pdo_dblib_err_dtor(pdo_dblib_err * err)144 void pdo_dblib_err_dtor(pdo_dblib_err *err)
145 {
146 	if (!err) {
147 		return;
148 	}
149 
150 	if (err->dberrstr) {
151 		efree(err->dberrstr);
152 		err->dberrstr = NULL;
153 	}
154 	if (err->lastmsg) {
155 		efree(err->lastmsg);
156 		err->lastmsg = NULL;
157 	}
158 	if (err->oserrstr) {
159 		efree(err->oserrstr);
160 		err->oserrstr = NULL;
161 	}
162 }
163 
PHP_GINIT_FUNCTION(dblib)164 static PHP_GINIT_FUNCTION(dblib)
165 {
166 #if defined(ZTS) && (defined(COMPILE_DL_PDO_DBLIB) || defined(COMPILE_DL_PDO_MSSQL))
167 	ZEND_TSRMLS_CACHE_UPDATE();
168 #endif
169 	memset(dblib_globals, 0, sizeof(*dblib_globals));
170 	dblib_globals->err.sqlstate = dblib_globals->sqlstate;
171 }
172 
PHP_RSHUTDOWN_FUNCTION(pdo_dblib)173 PHP_RSHUTDOWN_FUNCTION(pdo_dblib)
174 {
175 	if (DBLIB_G(err).oserrstr) {
176 		efree(DBLIB_G(err).oserrstr);
177 		DBLIB_G(err).oserrstr = NULL;
178 	}
179 	if (DBLIB_G(err).dberrstr) {
180 		efree(DBLIB_G(err).dberrstr);
181 		DBLIB_G(err).dberrstr = NULL;
182 	}
183 	if (DBLIB_G(err).lastmsg) {
184 		efree(DBLIB_G(err).lastmsg);
185 		DBLIB_G(err).lastmsg = NULL;
186 	}
187 	return SUCCESS;
188 }
189 
PHP_MINIT_FUNCTION(pdo_dblib)190 PHP_MINIT_FUNCTION(pdo_dblib)
191 {
192 	REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_CONNECTION_TIMEOUT", (long) PDO_DBLIB_ATTR_CONNECTION_TIMEOUT);
193 	REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_QUERY_TIMEOUT", (long) PDO_DBLIB_ATTR_QUERY_TIMEOUT);
194 	REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER", (long) PDO_DBLIB_ATTR_STRINGIFY_UNIQUEIDENTIFIER);
195 	REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_VERSION", (long) PDO_DBLIB_ATTR_VERSION);
196 	REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_TDS_VERSION", (long) PDO_DBLIB_ATTR_TDS_VERSION);
197 	REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_SKIP_EMPTY_ROWSETS", (long) PDO_DBLIB_ATTR_SKIP_EMPTY_ROWSETS);
198 	REGISTER_PDO_CLASS_CONST_LONG("DBLIB_ATTR_DATETIME_CONVERT", (long) PDO_DBLIB_ATTR_DATETIME_CONVERT);
199 
200 	if (FAIL == dbinit()) {
201 		return FAILURE;
202 	}
203 
204 	if (FAILURE == php_pdo_register_driver(&pdo_dblib_driver)) {
205 		return FAILURE;
206 	}
207 
208 #ifndef PHP_DBLIB_IS_MSSQL
209 	dberrhandle((EHANDLEFUNC) pdo_dblib_error_handler);
210 	dbmsghandle((MHANDLEFUNC) pdo_dblib_msg_handler);
211 #endif
212 
213 	return SUCCESS;
214 }
215 
PHP_MSHUTDOWN_FUNCTION(pdo_dblib)216 PHP_MSHUTDOWN_FUNCTION(pdo_dblib)
217 {
218 	php_pdo_unregister_driver(&pdo_dblib_driver);
219 	dbexit();
220 	return SUCCESS;
221 }
222 
PHP_MINFO_FUNCTION(pdo_dblib)223 PHP_MINFO_FUNCTION(pdo_dblib)
224 {
225 	php_info_print_table_start();
226 	php_info_print_table_header(2, "PDO Driver for "
227 #ifdef PDO_DBLIB_IS_MSSQL
228 		"MSSQL"
229 #elif defined(PHP_WIN32)
230 		"FreeTDS/Sybase/MSSQL"
231 #else
232 		"FreeTDS/Sybase"
233 #endif
234 		" DB-lib", "enabled");
235 	php_info_print_table_row(2, "Flavour", PDO_DBLIB_FLAVOUR);
236 	php_info_print_table_end();
237 }
238