xref: /PHP-8.0/ext/pdo_mysql/pdo_mysql.c (revision 4fc70266)
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   | http://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 
128 #ifdef PDO_USE_MYSQLND
129 	mysqlnd_reverse_api_register_api(&pdo_mysql_reverse_api);
130 #endif
131 
132 	return php_pdo_register_driver(&pdo_mysql_driver);
133 }
134 /* }}} */
135 
136 /* {{{ PHP_MSHUTDOWN_FUNCTION */
PHP_MSHUTDOWN_FUNCTION(pdo_mysql)137 static PHP_MSHUTDOWN_FUNCTION(pdo_mysql)
138 {
139 	php_pdo_unregister_driver(&pdo_mysql_driver);
140 #ifdef PDO_USE_MYSQLND
141 	UNREGISTER_INI_ENTRIES();
142 #endif
143 
144 	return SUCCESS;
145 }
146 /* }}} */
147 
148 /* {{{ PHP_MINFO_FUNCTION */
PHP_MINFO_FUNCTION(pdo_mysql)149 static PHP_MINFO_FUNCTION(pdo_mysql)
150 {
151 	php_info_print_table_start();
152 
153 	php_info_print_table_header(2, "PDO Driver for MySQL", "enabled");
154 	php_info_print_table_row(2, "Client API version", mysql_get_client_info());
155 
156 	php_info_print_table_end();
157 
158 #ifndef PHP_WIN32
159 	DISPLAY_INI_ENTRIES();
160 #endif
161 }
162 /* }}} */
163 
164 
165 #if defined(PDO_USE_MYSQLND) && PDO_DBG_ENABLED
166 /* {{{ PHP_RINIT_FUNCTION */
PHP_RINIT_FUNCTION(pdo_mysql)167 static PHP_RINIT_FUNCTION(pdo_mysql)
168 {
169 	if (PDO_MYSQL_G(debug)) {
170 		MYSQLND_DEBUG *dbg = mysqlnd_debug_init(mysqlnd_debug_std_no_trace_funcs);
171 		if (!dbg) {
172 			return FAILURE;
173 		}
174 		dbg->m->set_mode(dbg, PDO_MYSQL_G(debug));
175 		PDO_MYSQL_G(dbg) = dbg;
176 	}
177 
178 	return SUCCESS;
179 }
180 /* }}} */
181 
182 /* {{{ PHP_RSHUTDOWN_FUNCTION */
PHP_RSHUTDOWN_FUNCTION(pdo_mysql)183 static PHP_RSHUTDOWN_FUNCTION(pdo_mysql)
184 {
185 	MYSQLND_DEBUG *dbg = PDO_MYSQL_G(dbg);
186 	PDO_DBG_ENTER("RSHUTDOWN");
187 	if (dbg) {
188 		dbg->m->close(dbg);
189 		dbg->m->free_handle(dbg);
190 		PDO_MYSQL_G(dbg) = NULL;
191 	}
192 
193 	return SUCCESS;
194 }
195 /* }}} */
196 #endif
197 
198 /* {{{ PHP_GINIT_FUNCTION */
PHP_GINIT_FUNCTION(pdo_mysql)199 static PHP_GINIT_FUNCTION(pdo_mysql)
200 {
201 #if defined(COMPILE_DL_PDO_MYSQL) && defined(ZTS)
202 ZEND_TSRMLS_CACHE_UPDATE();
203 #endif
204 #ifndef PHP_WIN32
205 	pdo_mysql_globals->default_socket = NULL;
206 #endif
207 #if PDO_DBG_ENABLED
208 	pdo_mysql_globals->debug = NULL;	/* The actual string */
209 	pdo_mysql_globals->dbg = NULL;	/* The DBG object*/
210 #endif
211 }
212 /* }}} */
213 
214 /* {{{ pdo_mysql_deps[] */
215 static const zend_module_dep pdo_mysql_deps[] = {
216 	ZEND_MOD_REQUIRED("pdo")
217 #ifdef PDO_USE_MYSQLND
218 	ZEND_MOD_REQUIRED("mysqlnd")
219 #endif
220 	ZEND_MOD_END
221 };
222 /* }}} */
223 
224 /* {{{ pdo_mysql_module_entry */
225 zend_module_entry pdo_mysql_module_entry = {
226 	STANDARD_MODULE_HEADER_EX, NULL,
227 	pdo_mysql_deps,
228 	"pdo_mysql",
229 	NULL,
230 	PHP_MINIT(pdo_mysql),
231 	PHP_MSHUTDOWN(pdo_mysql),
232 #if defined(PDO_USE_MYSQLND) && PDO_DBG_ENABLED
233 	PHP_RINIT(pdo_mysql),
234 	PHP_RSHUTDOWN(pdo_mysql),
235 #else
236 	NULL,
237 	NULL,
238 #endif
239 	PHP_MINFO(pdo_mysql),
240 	PHP_PDO_MYSQL_VERSION,
241 	PHP_MODULE_GLOBALS(pdo_mysql),
242 	PHP_GINIT(pdo_mysql),
243 	NULL,
244 	NULL,
245 	STANDARD_MODULE_PROPERTIES_EX
246 };
247 /* }}} */
248