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