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