xref: /php-src/ext/pdo_mysql/mysql_driver.c (revision 37989666)
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   |         Wez Furlong <wez@php.net>                                    |
15   |         Johannes Schlueter <johannes@mysql.com>                      |
16   +----------------------------------------------------------------------+
17 */
18 
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "pdo/php_pdo.h"
27 #include "pdo/php_pdo_driver.h"
28 #include "php_pdo_mysql.h"
29 #include "php_pdo_mysql_int.h"
30 #ifndef PDO_USE_MYSQLND
31 #include <mysqld_error.h>
32 #endif
33 #include "zend_exceptions.h"
34 
35 #ifdef PDO_USE_MYSQLND
36 #	define pdo_mysql_init(persistent) mysqlnd_init(MYSQLND_CLIENT_NO_FLAG, persistent)
37 #else
38 #	define pdo_mysql_init(persistent) mysql_init(NULL)
39 #endif
40 
41 /* {{{ _pdo_mysql_error */
_pdo_mysql_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt,const char * file,int line)42 int _pdo_mysql_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *file, int line)
43 {
44 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
45 	pdo_error_type *pdo_err;
46 	pdo_mysql_error_info *einfo;
47 	pdo_mysql_stmt *S = NULL;
48 
49 	PDO_DBG_ENTER("_pdo_mysql_error");
50 	PDO_DBG_INF_FMT("file=%s line=%d", file, line);
51 	if (stmt) {
52 		S = (pdo_mysql_stmt*)stmt->driver_data;
53 		pdo_err = &stmt->error_code;
54 		einfo   = &S->einfo;
55 	} else {
56 		pdo_err = &dbh->error_code;
57 		einfo   = &H->einfo;
58 	}
59 
60 	if (S && S->stmt) {
61 		einfo->errcode = mysql_stmt_errno(S->stmt);
62 	} else {
63 		einfo->errcode = mysql_errno(H->server);
64 	}
65 
66 	einfo->file = file;
67 	einfo->line = line;
68 
69 	if (einfo->errmsg) {
70 		pefree(einfo->errmsg, dbh->is_persistent);
71 		einfo->errmsg = NULL;
72 	}
73 
74 	if (einfo->errcode) {
75 		if (einfo->errcode == 2014) {
76 			if (mysql_more_results(H->server)) {
77 				einfo->errmsg = pestrdup(
78 					"Cannot execute queries while there are pending result sets. "
79 					"Consider unsetting the previous PDOStatement or calling "
80 					"PDOStatement::closeCursor()",
81 					dbh->is_persistent);
82 			} else {
83 				einfo->errmsg = pestrdup(
84 					"Cannot execute queries while other unbuffered queries are active.  "
85 					"Consider using PDOStatement::fetchAll().  Alternatively, if your code "
86 					"is only ever going to run against mysql, you may enable query "
87 					"buffering by setting the PDO::MYSQL_ATTR_USE_BUFFERED_QUERY attribute.",
88 					dbh->is_persistent);
89 			}
90 		} else if (einfo->errcode == 2057) {
91 			einfo->errmsg = pestrdup(
92 				"A stored procedure returning result sets of different size was called. "
93 				"This is not supported by libmysql",
94 				dbh->is_persistent);
95 
96 		} else {
97 			if (S && S->stmt) {
98 				einfo->errmsg = pestrdup(mysql_stmt_error(S->stmt), dbh->is_persistent);
99 			} else {
100 				einfo->errmsg = pestrdup(mysql_error(H->server), dbh->is_persistent);
101 			}
102 		}
103 	} else { /* no error */
104 		strcpy(*pdo_err, PDO_ERR_NONE);
105 		PDO_DBG_RETURN(0);
106 	}
107 
108 	if (S && S->stmt) {
109 		strcpy(*pdo_err, mysql_stmt_sqlstate(S->stmt));
110 	} else {
111 		strcpy(*pdo_err, mysql_sqlstate(H->server));
112 	}
113 
114 	if (!dbh->methods) {
115 		PDO_DBG_INF("Throwing exception");
116 		pdo_throw_exception(einfo->errcode, einfo->errmsg, pdo_err);
117 	}
118 
119 	PDO_DBG_RETURN(einfo->errcode);
120 }
121 /* }}} */
122 
123 /* {{{ pdo_mysql_fetch_error_func */
pdo_mysql_fetch_error_func(pdo_dbh_t * dbh,pdo_stmt_t * stmt,zval * info)124 static void pdo_mysql_fetch_error_func(pdo_dbh_t *dbh, pdo_stmt_t *stmt, zval *info)
125 {
126 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
127 	pdo_mysql_error_info *einfo = &H->einfo;
128 
129 	PDO_DBG_ENTER("pdo_mysql_fetch_error_func");
130 	PDO_DBG_INF_FMT("dbh=%p stmt=%p", dbh, stmt);
131 	if (stmt) {
132 		pdo_mysql_stmt *S = (pdo_mysql_stmt*)stmt->driver_data;
133 		einfo = &S->einfo;
134 	} else {
135 		einfo = &H->einfo;
136 	}
137 
138 	if (einfo->errcode) {
139 		add_next_index_long(info, einfo->errcode);
140 		add_next_index_string(info, einfo->errmsg);
141 	}
142 
143 	PDO_DBG_VOID_RETURN;
144 }
145 /* }}} */
146 
147 /* {{{ mysql_handle_closer */
mysql_handle_closer(pdo_dbh_t * dbh)148 static void mysql_handle_closer(pdo_dbh_t *dbh)
149 {
150 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
151 
152 	PDO_DBG_ENTER("mysql_handle_closer");
153 	PDO_DBG_INF_FMT("dbh=%p", dbh);
154 	if (H) {
155 		if (H->server) {
156 			mysql_close(H->server);
157 			H->server = NULL;
158 		}
159 		if (H->einfo.errmsg) {
160 			pefree(H->einfo.errmsg, dbh->is_persistent);
161 			H->einfo.errmsg = NULL;
162 		}
163 		pefree(H, dbh->is_persistent);
164 		dbh->driver_data = NULL;
165 	}
166 }
167 /* }}} */
168 
169 /* {{{ mysql_handle_preparer */
mysql_handle_preparer(pdo_dbh_t * dbh,zend_string * sql,pdo_stmt_t * stmt,zval * driver_options)170 static bool mysql_handle_preparer(pdo_dbh_t *dbh, zend_string *sql, pdo_stmt_t *stmt, zval *driver_options)
171 {
172 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
173 	pdo_mysql_stmt *S = ecalloc(1, sizeof(pdo_mysql_stmt));
174 	zend_string *nsql = NULL;
175 	int ret;
176 	int server_version;
177 
178 	PDO_DBG_ENTER("mysql_handle_preparer");
179 	PDO_DBG_INF_FMT("dbh=%p", dbh);
180 	PDO_DBG_INF_FMT("sql=%.*s", (int) ZSTR_LEN(sql), ZSTR_VAL(sql));
181 
182 	S->H = H;
183 	stmt->driver_data = S;
184 	stmt->methods = &mysql_stmt_methods;
185 
186 	if (H->emulate_prepare) {
187 		goto end;
188 	}
189 
190 	server_version = mysql_get_server_version(H->server);
191 	if (server_version < 40100) {
192 		goto fallback;
193 	}
194 	stmt->supports_placeholders = PDO_PLACEHOLDER_POSITIONAL;
195 	ret = pdo_parse_params(stmt, sql, &nsql);
196 
197 	if (ret == 1) {
198 		/* query was rewritten */
199 		sql = nsql;
200 	} else if (ret == -1) {
201 		/* failed to parse */
202 		strcpy(dbh->error_code, stmt->error_code);
203 		PDO_DBG_RETURN(false);
204 	}
205 
206 	if (!(S->stmt = mysql_stmt_init(H->server))) {
207 		pdo_mysql_error(dbh);
208 		if (nsql) {
209 			zend_string_release(nsql);
210 		}
211 		PDO_DBG_RETURN(false);
212 	}
213 
214 	if (mysql_stmt_prepare(S->stmt, ZSTR_VAL(sql), ZSTR_LEN(sql))) {
215 		if (nsql) {
216 			zend_string_release(nsql);
217 		}
218 		/* TODO: might need to pull statement specific info here? */
219 		/* if the query isn't supported by the protocol, fallback to emulation */
220 		if (mysql_errno(H->server) == 1295) {
221 			mysql_stmt_close(S->stmt);
222 			S->stmt = NULL;
223 			goto fallback;
224 		}
225 		pdo_mysql_error(dbh);
226 		PDO_DBG_RETURN(false);
227 	}
228 	if (nsql) {
229 		zend_string_release(nsql);
230 	}
231 
232 	S->num_params = mysql_stmt_param_count(S->stmt);
233 
234 	if (S->num_params) {
235 #ifdef PDO_USE_MYSQLND
236 		S->params = NULL;
237 #else
238 		S->params = ecalloc(S->num_params, sizeof(MYSQL_BIND));
239 		S->in_null = ecalloc(S->num_params, sizeof(my_bool));
240 		S->in_length = ecalloc(S->num_params, sizeof(zend_ulong));
241 #endif
242 	}
243 	dbh->alloc_own_columns = 1;
244 
245 	S->max_length = pdo_attr_lval(driver_options, PDO_ATTR_MAX_COLUMN_LEN, 0);
246 
247 	PDO_DBG_RETURN(true);
248 
249 fallback:
250 end:
251 	stmt->supports_placeholders = PDO_PLACEHOLDER_NONE;
252 
253 	PDO_DBG_RETURN(true);
254 }
255 /* }}} */
256 
257 /* {{{ mysql_handle_doer */
mysql_handle_doer(pdo_dbh_t * dbh,const zend_string * sql)258 static zend_long mysql_handle_doer(pdo_dbh_t *dbh, const zend_string *sql)
259 {
260 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
261 	PDO_DBG_ENTER("mysql_handle_doer");
262 	PDO_DBG_INF_FMT("dbh=%p", dbh);
263 	PDO_DBG_INF_FMT("sql=%.*s", (int)ZSTR_LEN(sql), ZSTR_VAL(sql));
264 
265 	if (mysql_real_query(H->server, ZSTR_VAL(sql), ZSTR_LEN(sql))) {
266 		pdo_mysql_error(dbh);
267 		PDO_DBG_RETURN(-1);
268 	} else {
269 		my_ulonglong c = mysql_affected_rows(H->server);
270 		if (c == (my_ulonglong) -1) {
271 			pdo_mysql_error(dbh);
272 			PDO_DBG_RETURN(H->einfo.errcode ? -1 : 0);
273 		} else {
274 
275 			/* MULTI_QUERY support - eat up all unfetched result sets */
276 			MYSQL_RES* result;
277 			while (mysql_more_results(H->server)) {
278 				if (mysql_next_result(H->server)) {
279 					pdo_mysql_error(dbh);
280 					PDO_DBG_RETURN(-1);
281 				}
282 				result = mysql_store_result(H->server);
283 				if (result) {
284 					mysql_free_result(result);
285 				}
286 			}
287 			PDO_DBG_RETURN((int)c);
288 		}
289 	}
290 }
291 /* }}} */
292 
293 /* {{{ pdo_mysql_last_insert_id */
pdo_mysql_last_insert_id(pdo_dbh_t * dbh,const zend_string * name)294 static zend_string *pdo_mysql_last_insert_id(pdo_dbh_t *dbh, const zend_string *name)
295 {
296 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
297 	PDO_DBG_ENTER("pdo_mysql_last_insert_id");
298 	PDO_DBG_RETURN(zend_u64_to_str(mysql_insert_id(H->server)));
299 }
300 /* }}} */
301 
302 #if defined(PDO_USE_MYSQLND) || MYSQL_VERSION_ID < 50707 || defined(MARIADB_BASE_VERSION)
303 # define mysql_real_escape_string_quote(mysql, to, from, length, quote) \
304 	mysql_real_escape_string(mysql, to, from, length)
305 #endif
306 
307 /* {{{ mysql_handle_quoter */
mysql_handle_quoter(pdo_dbh_t * dbh,const zend_string * unquoted,enum pdo_param_type paramtype)308 static zend_string* mysql_handle_quoter(pdo_dbh_t *dbh, const zend_string *unquoted, enum pdo_param_type paramtype )
309 {
310 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
311 	bool use_national_character_set = 0;
312 	size_t quotedlen;
313 
314 	if (H->assume_national_character_set_strings) {
315 		use_national_character_set = 1;
316 	}
317 	if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
318 		use_national_character_set = 1;
319 	}
320 	if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
321 		use_national_character_set = 0;
322 	}
323 
324 	PDO_DBG_ENTER("mysql_handle_quoter");
325 	PDO_DBG_INF_FMT("dbh=%p", dbh);
326 	PDO_DBG_INF_FMT("unquoted=%.*s", (int)ZSTR_LEN(unquoted), ZSTR_VAL(unquoted));
327 
328 	zend_string *quoted_str = zend_string_safe_alloc(2, ZSTR_LEN(unquoted), 3 + (use_national_character_set ? 1 : 0), false);
329 	char *quoted = ZSTR_VAL(quoted_str);
330 
331 	if (use_national_character_set) {
332 		quotedlen = mysql_real_escape_string_quote(H->server, quoted + 2, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), '\'');
333 		quoted[0] = 'N';
334 		quoted[1] = '\'';
335 
336 		++quotedlen; /* N prefix */
337 	} else {
338 		quotedlen = mysql_real_escape_string_quote(H->server, quoted + 1, ZSTR_VAL(unquoted), ZSTR_LEN(unquoted), '\'');
339 		quoted[0] = '\'';
340 	}
341 
342 	quoted[++quotedlen] = '\'';
343 	quoted[++quotedlen] = '\0';
344 	PDO_DBG_INF_FMT("quoted=%.*s", (int)quotedlen, quoted);
345 
346 	quoted_str = zend_string_truncate(quoted_str, quotedlen, false);
347 
348 	PDO_DBG_RETURN(quoted_str);
349 }
350 /* }}} */
351 
352 /* {{{ mysql_handle_begin */
mysql_handle_begin(pdo_dbh_t * dbh)353 static bool mysql_handle_begin(pdo_dbh_t *dbh)
354 {
355 	zend_long return_value;
356 	zend_string *command;
357 
358 	PDO_DBG_ENTER("mysql_handle_begin");
359 	PDO_DBG_INF_FMT("dbh=%p", dbh);
360 
361 	command = ZSTR_INIT_LITERAL("START TRANSACTION", 0);
362 	return_value = mysql_handle_doer(dbh, command);
363 	zend_string_release_ex(command, 0);
364 	PDO_DBG_RETURN(0 <= return_value);
365 }
366 /* }}} */
367 
368 /* {{{ mysql_handle_commit */
mysql_handle_commit(pdo_dbh_t * dbh)369 static bool mysql_handle_commit(pdo_dbh_t *dbh)
370 {
371 	PDO_DBG_ENTER("mysql_handle_commit");
372 	PDO_DBG_INF_FMT("dbh=%p", dbh);
373 	if (mysql_commit(((pdo_mysql_db_handle *)dbh->driver_data)->server)) {
374 		pdo_mysql_error(dbh);
375 		PDO_DBG_RETURN(false);
376 	}
377 	PDO_DBG_RETURN(true);
378 }
379 /* }}} */
380 
381 /* {{{ mysql_handle_rollback */
mysql_handle_rollback(pdo_dbh_t * dbh)382 static bool mysql_handle_rollback(pdo_dbh_t *dbh)
383 {
384 	PDO_DBG_ENTER("mysql_handle_rollback");
385 	PDO_DBG_INF_FMT("dbh=%p", dbh);
386 	if (mysql_rollback(((pdo_mysql_db_handle *)dbh->driver_data)->server)) {
387 		pdo_mysql_error(dbh);
388 		PDO_DBG_RETURN(false);
389 	}
390 	PDO_DBG_RETURN(true);
391 }
392 /* }}} */
393 
394 /* {{{ mysql_handle_autocommit */
mysql_handle_autocommit(pdo_dbh_t * dbh)395 static inline int mysql_handle_autocommit(pdo_dbh_t *dbh)
396 {
397 	PDO_DBG_ENTER("mysql_handle_autocommit");
398 	PDO_DBG_INF_FMT("dbh=%p", dbh);
399 	PDO_DBG_INF_FMT("dbh->autocommit=%d", dbh->auto_commit);
400 	if (mysql_autocommit(((pdo_mysql_db_handle *)dbh->driver_data)->server, dbh->auto_commit)) {
401 		pdo_mysql_error(dbh);
402 		PDO_DBG_RETURN(0);
403 	}
404 	PDO_DBG_RETURN(1);
405 }
406 /* }}} */
407 
408 /* {{{ pdo_mysql_set_attribute */
pdo_mysql_set_attribute(pdo_dbh_t * dbh,zend_long attr,zval * val)409 static bool pdo_mysql_set_attribute(pdo_dbh_t *dbh, zend_long attr, zval *val)
410 {
411 	zend_long lval;
412 	bool bval;
413 	PDO_DBG_ENTER("pdo_mysql_set_attribute");
414 	PDO_DBG_INF_FMT("dbh=%p", dbh);
415 	PDO_DBG_INF_FMT("attr=" ZEND_LONG_FMT, attr);
416 
417 	switch (attr) {
418 		case PDO_ATTR_AUTOCOMMIT:
419 			if (!pdo_get_bool_param(&bval, val)) {
420 				PDO_DBG_RETURN(false);
421 			}
422 			/* ignore if the new value equals the old one */
423 			if (dbh->auto_commit ^ bval) {
424 				dbh->auto_commit = bval;
425 				if (!mysql_handle_autocommit(dbh)) {
426 					PDO_DBG_RETURN(false);
427 				}
428 			}
429 			PDO_DBG_RETURN(true);
430 
431 		case PDO_ATTR_DEFAULT_STR_PARAM:
432 			if (!pdo_get_long_param(&lval, val)) {
433 				PDO_DBG_RETURN(false);
434 			}
435 			((pdo_mysql_db_handle *)dbh->driver_data)->assume_national_character_set_strings = lval == PDO_PARAM_STR_NATL;
436 			PDO_DBG_RETURN(true);
437 
438 		case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
439 			if (!pdo_get_bool_param(&bval, val)) {
440 				PDO_DBG_RETURN(false);
441 			}
442 			/* ignore if the new value equals the old one */
443 			((pdo_mysql_db_handle *)dbh->driver_data)->buffered = bval;
444 			PDO_DBG_RETURN(true);
445 
446 		case PDO_MYSQL_ATTR_DIRECT_QUERY:
447 		case PDO_ATTR_EMULATE_PREPARES:
448 			if (!pdo_get_bool_param(&bval, val)) {
449 				PDO_DBG_RETURN(false);
450 			}
451 			/* ignore if the new value equals the old one */
452 			((pdo_mysql_db_handle *)dbh->driver_data)->emulate_prepare = bval;
453 			PDO_DBG_RETURN(true);
454 
455 		case PDO_ATTR_FETCH_TABLE_NAMES:
456 			if (!pdo_get_bool_param(&bval, val)) {
457 				PDO_DBG_RETURN(false);
458 			}
459 			((pdo_mysql_db_handle *)dbh->driver_data)->fetch_table_names = bval;
460 			PDO_DBG_RETURN(true);
461 
462 #ifdef PDO_USE_MYSQLND
463 		case PDO_ATTR_STRINGIFY_FETCHES:
464 			if (!pdo_get_bool_param(&bval, val)) {
465 				PDO_DBG_RETURN(false);
466 			}
467 			unsigned int int_and_float_native = !bval;
468 			pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
469 			if (mysql_options(H->server, MYSQLND_OPT_INT_AND_FLOAT_NATIVE, (const char *) &int_and_float_native)) {
470 				pdo_mysql_error(dbh);
471 				PDO_DBG_RETURN(false);
472 			}
473 			PDO_DBG_RETURN(true);
474 #else
475 		case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
476 			if (!pdo_get_long_param(&lval, val)) {
477 				PDO_DBG_RETURN(false);
478 			}
479 			if (lval < 0) {
480 				/* TODO: Johannes, can we throw a warning here? */
481 				((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = 1024*1024;
482 				PDO_DBG_INF_FMT("Adjusting invalid buffer size to =%l", ((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size);
483 			} else {
484 				((pdo_mysql_db_handle *)dbh->driver_data)->max_buffer_size = lval;
485 			}
486 			PDO_DBG_RETURN(true);
487 			break;
488 #endif
489 
490 		default:
491 			PDO_DBG_RETURN(false);
492 	}
493 }
494 /* }}} */
495 
496 /* {{{ pdo_mysql_get_attribute */
pdo_mysql_get_attribute(pdo_dbh_t * dbh,zend_long attr,zval * return_value)497 static int pdo_mysql_get_attribute(pdo_dbh_t *dbh, zend_long attr, zval *return_value)
498 {
499 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
500 
501 	PDO_DBG_ENTER("pdo_mysql_get_attribute");
502 	PDO_DBG_INF_FMT("dbh=%p", dbh);
503 	PDO_DBG_INF_FMT("attr=" ZEND_LONG_FMT, attr);
504 	switch (attr) {
505 		case PDO_ATTR_CLIENT_VERSION:
506 			ZVAL_STRING(return_value, (char *)mysql_get_client_info());
507 			break;
508 
509 		case PDO_ATTR_SERVER_VERSION:
510 			ZVAL_STRING(return_value, (char *)mysql_get_server_info(H->server));
511 			break;
512 
513 		case PDO_ATTR_CONNECTION_STATUS:
514 			ZVAL_STRING(return_value, (char *)mysql_get_host_info(H->server));
515 			break;
516 		case PDO_ATTR_SERVER_INFO: {
517 #ifdef PDO_USE_MYSQLND
518 			zend_string *tmp;
519 
520 			if (mysqlnd_stat(H->server, &tmp) == PASS) {
521 				ZVAL_STR(return_value, tmp);
522 #else
523 			char *tmp;
524 			if ((tmp = (char *)mysql_stat(H->server))) {
525 				ZVAL_STRING(return_value, tmp);
526 #endif
527 			} else {
528 				pdo_mysql_error(dbh);
529 				PDO_DBG_RETURN(-1);
530 			}
531 		}
532 			break;
533 
534 		case PDO_ATTR_AUTOCOMMIT:
535 			ZVAL_BOOL(return_value, dbh->auto_commit);
536 			break;
537 
538 		case PDO_ATTR_DEFAULT_STR_PARAM:
539 			ZVAL_LONG(return_value, H->assume_national_character_set_strings ? PDO_PARAM_STR_NATL : PDO_PARAM_STR_CHAR);
540 			break;
541 
542 		case PDO_MYSQL_ATTR_USE_BUFFERED_QUERY:
543 			ZVAL_BOOL(return_value, H->buffered);
544 			break;
545 
546 		case PDO_ATTR_EMULATE_PREPARES:
547 		case PDO_MYSQL_ATTR_DIRECT_QUERY:
548 			ZVAL_BOOL(return_value, H->emulate_prepare);
549 			break;
550 
551 #ifndef PDO_USE_MYSQLND
552 		case PDO_MYSQL_ATTR_MAX_BUFFER_SIZE:
553 			ZVAL_LONG(return_value, H->max_buffer_size);
554 			break;
555 #endif
556 
557 		case PDO_MYSQL_ATTR_LOCAL_INFILE:
558 			ZVAL_BOOL(return_value, H->local_infile);
559 			break;
560 
561 #if (MYSQL_VERSION_ID >= 80021 && !defined(MARIADB_BASE_VERSION)) || defined(PDO_USE_MYSQLND)
562 		case PDO_MYSQL_ATTR_LOCAL_INFILE_DIRECTORY:
563 		{
564 			const char* local_infile_directory = NULL;
565 #ifdef PDO_USE_MYSQLND
566 			local_infile_directory = H->server->data->options->local_infile_directory;
567 #else
568 			mysql_get_option(H->server, MYSQL_OPT_LOAD_DATA_LOCAL_DIR, &local_infile_directory);
569 #endif
570 			if (local_infile_directory) {
571 				ZVAL_STRING(return_value, local_infile_directory);
572 			} else {
573 				ZVAL_NULL(return_value);
574 			}
575 			break;
576 		}
577 #endif
578 
579 		case PDO_ATTR_FETCH_TABLE_NAMES:
580 			ZVAL_BOOL(return_value, H->fetch_table_names);
581 			break;
582 
583 		default:
584 			PDO_DBG_RETURN(0);
585 	}
586 
587 	PDO_DBG_RETURN(1);
588 }
589 /* }}} */
590 
591 /* {{{ pdo_mysql_check_liveness */
592 static zend_result pdo_mysql_check_liveness(pdo_dbh_t *dbh)
593 {
594 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
595 
596 	PDO_DBG_ENTER("pdo_mysql_check_liveness");
597 	PDO_DBG_INF_FMT("dbh=%p", dbh);
598 
599 	if (mysql_ping(H->server)) {
600 		PDO_DBG_RETURN(FAILURE);
601 	}
602 	PDO_DBG_RETURN(SUCCESS);
603 }
604 /* }}} */
605 
606 /* {{{ pdo_mysql_request_shutdown */
607 static void pdo_mysql_request_shutdown(pdo_dbh_t *dbh)
608 {
609 	PDO_DBG_ENTER("pdo_mysql_request_shutdown");
610 	PDO_DBG_INF_FMT("dbh=%p", dbh);
611 
612 #ifdef PDO_USE_MYSQLND
613 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
614 	if (H->server) {
615 		mysqlnd_end_psession(H->server);
616 	}
617 #endif
618 }
619 /* }}} */
620 
621 #ifdef PDO_USE_MYSQLND
622 # define pdo_mysql_get_server_status(m) mysqlnd_get_server_status(m)
623 #else
624 # define pdo_mysql_get_server_status(m) (m)->server_status
625 #endif
626 
627 /* {{{ pdo_mysql_in_transaction */
628 static bool pdo_mysql_in_transaction(pdo_dbh_t *dbh)
629 {
630 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
631 	PDO_DBG_ENTER("pdo_mysql_in_transaction");
632 	PDO_DBG_RETURN((pdo_mysql_get_server_status(H->server) & SERVER_STATUS_IN_TRANS) != 0);
633 }
634 /* }}} */
635 
636 /* {{{ mysql_methods */
637 static const struct pdo_dbh_methods mysql_methods = {
638 	mysql_handle_closer,
639 	mysql_handle_preparer,
640 	mysql_handle_doer,
641 	mysql_handle_quoter,
642 	mysql_handle_begin,
643 	mysql_handle_commit,
644 	mysql_handle_rollback,
645 	pdo_mysql_set_attribute,
646 	pdo_mysql_last_insert_id,
647 	pdo_mysql_fetch_error_func,
648 	pdo_mysql_get_attribute,
649 	pdo_mysql_check_liveness,
650 	NULL,
651 	pdo_mysql_request_shutdown,
652 	pdo_mysql_in_transaction,
653 	NULL /* get_gc */
654 };
655 /* }}} */
656 
657 #ifdef PHP_WIN32
658 # define PDO_DEFAULT_MYSQL_UNIX_ADDR	NULL
659 #else
660 # define PDO_DEFAULT_MYSQL_UNIX_ADDR	PDO_MYSQL_G(default_socket)
661 #endif
662 
663 /* {{{ pdo_mysql_handle_factory */
664 static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
665 {
666 	pdo_mysql_db_handle *H;
667 	size_t i;
668 	int ret = 0;
669 	char *host = NULL, *unix_socket = NULL;
670 	unsigned int port = 3306;
671 	char *dbname;
672 	struct pdo_data_src_parser vars[] = {
673 		{ "charset",  NULL,	0 },
674 		{ "dbname",   "",	0 },
675 		{ "host",     "localhost",	0 },
676 		{ "port",     "3306",	0 },
677 		{ "unix_socket",  PDO_DEFAULT_MYSQL_UNIX_ADDR,	0 },
678 		{ "user",     NULL,	0 },
679 		{ "password", NULL,	0 },
680 	};
681 	int connect_opts = 0
682 #ifdef CLIENT_MULTI_RESULTS
683 		|CLIENT_MULTI_RESULTS
684 #endif
685 		;
686 #ifdef PDO_USE_MYSQLND
687 	size_t dbname_len = 0;
688 	size_t password_len = 0;
689 #endif
690 
691 #ifdef CLIENT_MULTI_STATEMENTS
692 	if (!driver_options) {
693 		connect_opts |= CLIENT_MULTI_STATEMENTS;
694 	} else if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MULTI_STATEMENTS, 1)) {
695 		connect_opts |= CLIENT_MULTI_STATEMENTS;
696 	}
697 #endif
698 
699 	PDO_DBG_ENTER("pdo_mysql_handle_factory");
700 	PDO_DBG_INF_FMT("dbh=%p", dbh);
701 #ifdef CLIENT_MULTI_RESULTS
702 	PDO_DBG_INF("multi results");
703 #endif
704 
705 	php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 7);
706 
707 	H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent);
708 
709 	H->einfo.errcode = 0;
710 	H->einfo.errmsg = NULL;
711 
712 	/* allocate an environment */
713 
714 	/* handle for the server */
715 	if (!(H->server = pdo_mysql_init(dbh->is_persistent))) {
716 		pdo_mysql_error(dbh);
717 		goto cleanup;
718 	}
719 #ifdef PDO_USE_MYSQLND
720 	if (dbh->is_persistent) {
721 		mysqlnd_restart_psession(H->server);
722 	}
723 #endif
724 
725 	dbh->driver_data = H;
726 
727 	dbh->skip_param_evt =
728 		1 << PDO_PARAM_EVT_FREE |
729 		1 << PDO_PARAM_EVT_EXEC_POST |
730 		1 << PDO_PARAM_EVT_FETCH_PRE |
731 		1 << PDO_PARAM_EVT_FETCH_POST |
732 		1 << PDO_PARAM_EVT_NORMALIZE;
733 
734 #ifndef PDO_USE_MYSQLND
735 	H->max_buffer_size = 1024*1024;
736 #endif
737 
738 	H->assume_national_character_set_strings = 0;
739 	H->buffered = H->emulate_prepare = 1;
740 
741 	/* handle MySQL options */
742 	if (driver_options) {
743 		zend_long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
744 		zend_string *init_cmd = NULL;
745 #ifndef PDO_USE_MYSQLND
746 		zend_string *default_file = NULL, *default_group = NULL;
747 #endif
748 		zend_long compress = 0;
749 		zend_string *ssl_key = NULL, *ssl_cert = NULL, *ssl_ca = NULL, *ssl_capath = NULL, *ssl_cipher = NULL;
750 		H->buffered = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
751 
752 		H->emulate_prepare = pdo_attr_lval(driver_options,
753 			PDO_MYSQL_ATTR_DIRECT_QUERY, H->emulate_prepare);
754 		H->emulate_prepare = pdo_attr_lval(driver_options,
755 			PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare);
756 
757 		H->assume_national_character_set_strings = pdo_attr_lval(driver_options,
758 			PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL;
759 
760 #ifndef PDO_USE_MYSQLND
761 		H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size);
762 #endif
763 
764 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_FOUND_ROWS, 0)) {
765 			connect_opts |= CLIENT_FOUND_ROWS;
766 		}
767 
768 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_IGNORE_SPACE, 0)) {
769 			connect_opts |= CLIENT_IGNORE_SPACE;
770 		}
771 
772 		if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) {
773 			pdo_mysql_error(dbh);
774 			goto cleanup;
775 		}
776 
777 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE, 0)) {
778 			H->local_infile = 1;
779 #ifndef PDO_USE_MYSQLND
780 			if (PG(open_basedir) && PG(open_basedir)[0] != '\0') {
781 				H->local_infile = 0;
782 			}
783 #endif
784 		}
785 
786 #if (MYSQL_VERSION_ID >= 80021 && !defined(MARIADB_BASE_VERSION)) || defined(PDO_USE_MYSQLND)
787 		zend_string *local_infile_directory = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE_DIRECTORY, NULL);
788 		if (local_infile_directory && !php_check_open_basedir(ZSTR_VAL(local_infile_directory))) {
789 			if (mysql_options(H->server, MYSQL_OPT_LOAD_DATA_LOCAL_DIR, (const char *)ZSTR_VAL(local_infile_directory))) {
790 				zend_string_release(local_infile_directory);
791 				pdo_mysql_error(dbh);
792 				goto cleanup;
793 			}
794 			zend_string_release(local_infile_directory);
795 		}
796 #endif
797 #ifdef MYSQL_OPT_RECONNECT
798 		/* since 5.0.3, the default for this option is 0 if not specified.
799 		 * we want the old behaviour
800 		 * mysqlnd doesn't support reconnect, thus we don't have "|| defined(PDO_USE_MYSQLND)"
801 		*/
802 		{
803 			zend_long reconnect = 1;
804 			mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect);
805 		}
806 #endif
807 		init_cmd = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_INIT_COMMAND, NULL);
808 		if (init_cmd) {
809 			if (mysql_options(H->server, MYSQL_INIT_COMMAND, (const char *)ZSTR_VAL(init_cmd))) {
810 				zend_string_release_ex(init_cmd, 0);
811 				pdo_mysql_error(dbh);
812 				goto cleanup;
813 			}
814 			zend_string_release_ex(init_cmd, 0);
815 		}
816 #ifndef PDO_USE_MYSQLND
817 		default_file = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_FILE, NULL);
818 		if (default_file) {
819 			if (mysql_options(H->server, MYSQL_READ_DEFAULT_FILE, (const char *)ZSTR_VAL(default_file))) {
820 				zend_string_release_ex(default_file, 0);
821 				pdo_mysql_error(dbh);
822 				goto cleanup;
823 			}
824 			zend_string_release_ex(default_file, 0);
825 		}
826 
827 		default_group = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_GROUP, NULL);
828 		if (default_group) {
829 			if (mysql_options(H->server, MYSQL_READ_DEFAULT_GROUP, (const char *)ZSTR_VAL(default_group))) {
830 				zend_string_release_ex(default_group, 0);
831 				pdo_mysql_error(dbh);
832 				goto cleanup;
833 			}
834 			zend_string_release_ex(default_group, 0);
835 		}
836 #endif
837 		compress = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_COMPRESS, 0);
838 		if (compress) {
839 			if (mysql_options(H->server, MYSQL_OPT_COMPRESS, 0)) {
840 				pdo_mysql_error(dbh);
841 				goto cleanup;
842 			}
843 		}
844 
845 		ssl_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_KEY, NULL);
846 		ssl_cert = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CERT, NULL);
847 		ssl_ca = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CA, NULL);
848 		ssl_capath = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CAPATH, NULL);
849 		ssl_cipher = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CIPHER, NULL);
850 
851 		if (ssl_key || ssl_cert || ssl_ca || ssl_capath || ssl_cipher) {
852 			mysql_ssl_set(H->server,
853 					ssl_key? ZSTR_VAL(ssl_key) : NULL,
854 					ssl_cert? ZSTR_VAL(ssl_cert) : NULL,
855 					ssl_ca? ZSTR_VAL(ssl_ca) : NULL,
856 					ssl_capath? ZSTR_VAL(ssl_capath) : NULL,
857 					ssl_cipher? ZSTR_VAL(ssl_cipher) : NULL);
858 			if (ssl_key) {
859 				zend_string_release_ex(ssl_key, 0);
860 			}
861 			if (ssl_cert) {
862 				zend_string_release_ex(ssl_cert, 0);
863 			}
864 			if (ssl_ca) {
865 				zend_string_release_ex(ssl_ca, 0);
866 			}
867 			if (ssl_capath) {
868 				zend_string_release_ex(ssl_capath, 0);
869 			}
870 			if (ssl_cipher) {
871 				zend_string_release_ex(ssl_cipher, 0);
872 			}
873 		}
874 
875 #if MYSQL_VERSION_ID > 50605 || defined(PDO_USE_MYSQLND)
876 		{
877 			zend_string *public_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SERVER_PUBLIC_KEY, NULL);
878 			if (public_key) {
879 				if (mysql_options(H->server, MYSQL_SERVER_PUBLIC_KEY, ZSTR_VAL(public_key))) {
880 					pdo_mysql_error(dbh);
881 					zend_string_release_ex(public_key, 0);
882 					goto cleanup;
883 				}
884 				zend_string_release_ex(public_key, 0);
885 			}
886 		}
887 #endif
888 
889 #ifdef PDO_USE_MYSQLND
890 		{
891 			zend_long ssl_verify_cert = pdo_attr_lval(driver_options,
892 					PDO_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, -1);
893 			if (ssl_verify_cert != -1) {
894 				connect_opts |= ssl_verify_cert ?
895 					CLIENT_SSL_VERIFY_SERVER_CERT:
896 					CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
897 			}
898 		}
899 #endif
900 	}
901 
902 	/* Always explicitly set the LOCAL_INFILE option. */
903 	unsigned int local_infile = H->local_infile;
904 	if (mysql_options(H->server, MYSQL_OPT_LOCAL_INFILE, (const char *)&local_infile)) {
905 		pdo_mysql_error(dbh);
906 		goto cleanup;
907 	}
908 
909 #ifdef PDO_USE_MYSQLND
910 	unsigned int int_and_float_native = !pdo_attr_lval(driver_options, PDO_ATTR_STRINGIFY_FETCHES, dbh->stringify);
911 	if (mysql_options(H->server, MYSQLND_OPT_INT_AND_FLOAT_NATIVE, (const char *) &int_and_float_native)) {
912 		pdo_mysql_error(dbh);
913 		goto cleanup;
914 	}
915 #endif
916 
917 	if (vars[0].optval && mysql_options(H->server, MYSQL_SET_CHARSET_NAME, vars[0].optval)) {
918 		pdo_mysql_error(dbh);
919 		goto cleanup;
920 	}
921 
922 	dbname = vars[1].optval;
923 	host = vars[2].optval;
924 	if(vars[3].optval) {
925 		port = atoi(vars[3].optval);
926 	}
927 
928 #ifdef PHP_WIN32
929 	if (vars[2].optval && !strcmp(".", vars[2].optval)) {
930 #else
931 	if (vars[2].optval && !strcmp("localhost", vars[2].optval)) {
932 #endif
933 		unix_socket = vars[4].optval;
934 	}
935 
936 	if (!dbh->username && vars[5].optval) {
937 		dbh->username = pestrdup(vars[5].optval, dbh->is_persistent);
938 	}
939 
940 	if (!dbh->password && vars[6].optval) {
941 		dbh->password = pestrdup(vars[6].optval, dbh->is_persistent);
942 	}
943 
944 	/* TODO: - Check zval cache + ZTS */
945 #ifdef PDO_USE_MYSQLND
946 	if (dbname) {
947 		dbname_len = strlen(dbname);
948 	}
949 
950 	if (dbh->password) {
951 		password_len = strlen(dbh->password);
952 	}
953 
954 	if (mysqlnd_connect(H->server, host, dbh->username, dbh->password, password_len, dbname, dbname_len,
955 						port, unix_socket, connect_opts, MYSQLND_CLIENT_NO_FLAG) == NULL) {
956 #else
957 	if (mysql_real_connect(H->server, host, dbh->username, dbh->password, dbname, port, unix_socket, connect_opts) == NULL) {
958 #endif
959 		pdo_mysql_error(dbh);
960 		goto cleanup;
961 	}
962 
963 	if (!dbh->auto_commit) {
964 		mysql_handle_autocommit(dbh);
965 	}
966 
967 	H->attached = 1;
968 
969 	dbh->alloc_own_columns = 1;
970 	dbh->max_escaped_char_length = 2;
971 	dbh->methods = &mysql_methods;
972 
973 	ret = 1;
974 
975 cleanup:
976 	for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
977 		if (vars[i].freeme) {
978 			efree(vars[i].optval);
979 		}
980 	}
981 
982 	dbh->methods = &mysql_methods;
983 
984 	PDO_DBG_RETURN(ret);
985 }
986 /* }}} */
987 
988 const pdo_driver_t pdo_mysql_driver = {
989 	PDO_DRIVER_HEADER(mysql),
990 	pdo_mysql_handle_factory
991 };
992