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