xref: /PHP-7.2/ext/pdo_mysql/pdo_mysql.c (revision 7a7ec01a)
1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 1997-2018 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: George Schlossnagle <george@omniti.com>                      |
16   |         Johannes Schlueter <johannes@mysql.com>                      |
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_mysql.h"
32 #include "php_pdo_mysql_int.h"
33 
34 #ifdef COMPILE_DL_PDO_MYSQL
35 #ifdef ZTS
36 ZEND_TSRMLS_CACHE_DEFINE()
37 #endif
ZEND_GET_MODULE(pdo_mysql)38 ZEND_GET_MODULE(pdo_mysql)
39 #endif
40 
41 ZEND_DECLARE_MODULE_GLOBALS(pdo_mysql)
42 
43 /*
44  The default socket location is sometimes defined by configure.
45  With libmysql `mysql_config --socket` will fill PDO_MYSQL_UNIX_ADDR
46  and the user can use --with-mysql-sock=SOCKET which will fill
47  PDO_MYSQL_UNIX_ADDR. If both aren't set we're using mysqlnd and use
48  /tmp/mysql.sock as default on *nix and NULL for Windows (default
49  named pipe name is set in mysqlnd).
50 */
51 #ifndef PDO_MYSQL_UNIX_ADDR
52 # ifdef PHP_MYSQL_UNIX_SOCK_ADDR
53 #  define PDO_MYSQL_UNIX_ADDR PHP_MYSQL_UNIX_SOCK_ADDR
54 # else
55 #  if !PHP_WIN32
56 #   define PDO_MYSQL_UNIX_ADDR "/tmp/mysql.sock"
57 #  else
58 #   define PDO_MYSQL_UNIX_ADDR NULL
59 #  endif
60 # endif
61 #endif
62 
63 #ifdef PDO_USE_MYSQLND
64 #include "ext/mysqlnd/mysqlnd_reverse_api.h"
65 static MYSQLND * pdo_mysql_convert_zv_to_mysqlnd(zval * zv)
66 {
67 	if (Z_TYPE_P(zv) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zv), php_pdo_get_dbh_ce())) {
68 		pdo_dbh_t * dbh = Z_PDO_DBH_P(zv);
69 
70 		if (!dbh) {
71 			php_error_docref(NULL, E_WARNING, "Failed to retrieve handle from object store");
72 			return NULL;
73 		}
74 
75 		if (dbh->driver != &pdo_mysql_driver) {
76 			php_error_docref(NULL, E_WARNING, "Provided PDO instance is not using MySQL but %s", dbh->driver->driver_name);
77 			return NULL;
78 		}
79 
80 		return ((pdo_mysql_db_handle *)dbh->driver_data)->server;
81 	}
82 	return NULL;
83 }
84 
85 static MYSQLND_REVERSE_API pdo_mysql_reverse_api = {
86 	&pdo_mysql_module_entry,
87 	pdo_mysql_convert_zv_to_mysqlnd
88 };
89 #endif
90 
91 
92 /* {{{ PHP_INI_BEGIN
93 */
94 PHP_INI_BEGIN()
95 #ifndef PHP_WIN32
96 	STD_PHP_INI_ENTRY("pdo_mysql.default_socket", PDO_MYSQL_UNIX_ADDR, PHP_INI_SYSTEM, OnUpdateStringUnempty, default_socket, zend_pdo_mysql_globals, pdo_mysql_globals)
97 #endif
98 #if PDO_DBG_ENABLED
99 	STD_PHP_INI_ENTRY("pdo_mysql.debug",	NULL, PHP_INI_SYSTEM, OnUpdateString, debug, zend_pdo_mysql_globals, pdo_mysql_globals)
100 #endif
PHP_INI_END()101 PHP_INI_END()
102 /* }}} */
103 
104 /* true global environment */
105 
106 /* {{{ PHP_MINIT_FUNCTION
107  */
108 static PHP_MINIT_FUNCTION(pdo_mysql)
109 {
110 	REGISTER_INI_ENTRIES();
111 
112 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_USE_BUFFERED_QUERY", (zend_long)PDO_MYSQL_ATTR_USE_BUFFERED_QUERY);
113 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_LOCAL_INFILE", (zend_long)PDO_MYSQL_ATTR_LOCAL_INFILE);
114 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_INIT_COMMAND", (zend_long)PDO_MYSQL_ATTR_INIT_COMMAND);
115 #ifndef PDO_USE_MYSQLND
116 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_MAX_BUFFER_SIZE", (zend_long)PDO_MYSQL_ATTR_MAX_BUFFER_SIZE);
117 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_READ_DEFAULT_FILE", (zend_long)PDO_MYSQL_ATTR_READ_DEFAULT_FILE);
118 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_READ_DEFAULT_GROUP", (zend_long)PDO_MYSQL_ATTR_READ_DEFAULT_GROUP);
119 #endif
120 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_COMPRESS", (zend_long)PDO_MYSQL_ATTR_COMPRESS);
121 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_DIRECT_QUERY", (zend_long)PDO_MYSQL_ATTR_DIRECT_QUERY);
122 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_FOUND_ROWS", (zend_long)PDO_MYSQL_ATTR_FOUND_ROWS);
123 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_IGNORE_SPACE", (zend_long)PDO_MYSQL_ATTR_IGNORE_SPACE);
124 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_KEY", (zend_long)PDO_MYSQL_ATTR_SSL_KEY);
125 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_CERT", (zend_long)PDO_MYSQL_ATTR_SSL_CERT);
126 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_CA", (zend_long)PDO_MYSQL_ATTR_SSL_CA);
127 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_CAPATH", (zend_long)PDO_MYSQL_ATTR_SSL_CAPATH);
128 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_CIPHER", (zend_long)PDO_MYSQL_ATTR_SSL_CIPHER);
129 #if MYSQL_VERSION_ID > 50605 || defined(PDO_USE_MYSQLND)
130 	 REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SERVER_PUBLIC_KEY", (zend_long)PDO_MYSQL_ATTR_SERVER_PUBLIC_KEY);
131 #endif
132 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_MULTI_STATEMENTS", (zend_long)PDO_MYSQL_ATTR_MULTI_STATEMENTS);
133 #ifdef PDO_USE_MYSQLND
134 	REGISTER_PDO_CLASS_CONST_LONG("MYSQL_ATTR_SSL_VERIFY_SERVER_CERT", (zend_long)PDO_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT);
135 #endif
136 
137 #ifdef PDO_USE_MYSQLND
138 	mysqlnd_reverse_api_register_api(&pdo_mysql_reverse_api);
139 #endif
140 
141 	return php_pdo_register_driver(&pdo_mysql_driver);
142 }
143 /* }}} */
144 
145 /* {{{ PHP_MSHUTDOWN_FUNCTION
146  */
PHP_MSHUTDOWN_FUNCTION(pdo_mysql)147 static PHP_MSHUTDOWN_FUNCTION(pdo_mysql)
148 {
149 	php_pdo_unregister_driver(&pdo_mysql_driver);
150 #if PDO_USE_MYSQLND
151 	UNREGISTER_INI_ENTRIES();
152 #endif
153 
154 	return SUCCESS;
155 }
156 /* }}} */
157 
158 /* {{{ PHP_MINFO_FUNCTION
159  */
PHP_MINFO_FUNCTION(pdo_mysql)160 static PHP_MINFO_FUNCTION(pdo_mysql)
161 {
162 	php_info_print_table_start();
163 
164 	php_info_print_table_header(2, "PDO Driver for MySQL", "enabled");
165 	php_info_print_table_row(2, "Client API version", mysql_get_client_info());
166 
167 	php_info_print_table_end();
168 
169 #ifndef PHP_WIN32
170 	DISPLAY_INI_ENTRIES();
171 #endif
172 }
173 /* }}} */
174 
175 
176 #if PDO_USE_MYSQLND && PDO_DBG_ENABLED
177 /* {{{ PHP_RINIT_FUNCTION
178  */
PHP_RINIT_FUNCTION(pdo_mysql)179 static PHP_RINIT_FUNCTION(pdo_mysql)
180 {
181 	if (PDO_MYSQL_G(debug)) {
182 		MYSQLND_DEBUG *dbg = mysqlnd_debug_init(mysqlnd_debug_std_no_trace_funcs);
183 		if (!dbg) {
184 			return FAILURE;
185 		}
186 		dbg->m->set_mode(dbg, PDO_MYSQL_G(debug));
187 		PDO_MYSQL_G(dbg) = dbg;
188 	}
189 
190 	return SUCCESS;
191 }
192 /* }}} */
193 
194 /* {{{ PHP_RSHUTDOWN_FUNCTION
195  */
PHP_RSHUTDOWN_FUNCTION(pdo_mysql)196 static PHP_RSHUTDOWN_FUNCTION(pdo_mysql)
197 {
198 	MYSQLND_DEBUG *dbg = PDO_MYSQL_G(dbg);
199 	PDO_DBG_ENTER("RSHUTDOWN");
200 	if (dbg) {
201 		dbg->m->close(dbg);
202 		dbg->m->free_handle(dbg);
203 		PDO_MYSQL_G(dbg) = NULL;
204 	}
205 
206 	return SUCCESS;
207 }
208 /* }}} */
209 #endif
210 
211 /* {{{ PHP_GINIT_FUNCTION
212  */
PHP_GINIT_FUNCTION(pdo_mysql)213 static PHP_GINIT_FUNCTION(pdo_mysql)
214 {
215 #if defined(COMPILE_DL_PDO_MYSQL) && defined(ZTS)
216 ZEND_TSRMLS_CACHE_UPDATE();
217 #endif
218 #ifndef PHP_WIN32
219 	pdo_mysql_globals->default_socket = NULL;
220 #endif
221 #if PDO_DBG_ENABLED
222 	pdo_mysql_globals->debug = NULL;	/* The actual string */
223 	pdo_mysql_globals->dbg = NULL;	/* The DBG object*/
224 #endif
225 }
226 /* }}} */
227 
228 /* {{{ pdo_mysql_functions[] */
229 const zend_function_entry pdo_mysql_functions[] = {
230 	PHP_FE_END
231 };
232 /* }}} */
233 
234 /* {{{ pdo_mysql_deps[] */
235 static const zend_module_dep pdo_mysql_deps[] = {
236 	ZEND_MOD_REQUIRED("pdo")
237 #ifdef PDO_USE_MYSQLND
238 	ZEND_MOD_REQUIRED("mysqlnd")
239 #endif
240 	ZEND_MOD_END
241 };
242 /* }}} */
243 
244 /* {{{ pdo_mysql_module_entry */
245 zend_module_entry pdo_mysql_module_entry = {
246 	STANDARD_MODULE_HEADER_EX, NULL,
247 	pdo_mysql_deps,
248 	"pdo_mysql",
249 	pdo_mysql_functions,
250 	PHP_MINIT(pdo_mysql),
251 	PHP_MSHUTDOWN(pdo_mysql),
252 #if PDO_USE_MYSQLND && PDO_DBG_ENABLED
253 	PHP_RINIT(pdo_mysql),
254 	PHP_RSHUTDOWN(pdo_mysql),
255 #else
256 	NULL,
257 	NULL,
258 #endif
259 	PHP_MINFO(pdo_mysql),
260 	PHP_PDO_MYSQL_VERSION,
261 	PHP_MODULE_GLOBALS(pdo_mysql),
262 	PHP_GINIT(pdo_mysql),
263 	NULL,
264 	NULL,
265 	STANDARD_MODULE_PROPERTIES_EX
266 };
267 /* }}} */
268 
269 
270 /*
271  * Local variables:
272  * tab-width: 4
273  * c-basic-offset: 4
274  * End:
275  * vim600: noet sw=4 ts=4 fdm=marker
276  * vim<600: noet sw=4 ts=4
277  */
278