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