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