xref: /PHP-8.1/ext/pdo_mysql/mysql_driver.c (revision e0aadc1c)
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 	char *quoted;
313 	size_t quotedlen;
314 	zend_string *quoted_str;
315 
316 	if (H->assume_national_character_set_strings) {
317 		use_national_character_set = 1;
318 	}
319 	if ((paramtype & PDO_PARAM_STR_NATL) == PDO_PARAM_STR_NATL) {
320 		use_national_character_set = 1;
321 	}
322 	if ((paramtype & PDO_PARAM_STR_CHAR) == PDO_PARAM_STR_CHAR) {
323 		use_national_character_set = 0;
324 	}
325 
326 	PDO_DBG_ENTER("mysql_handle_quoter");
327 	PDO_DBG_INF_FMT("dbh=%p", dbh);
328 	PDO_DBG_INF_FMT("unquoted=%.*s", (int)ZSTR_LEN(unquoted), ZSTR_VAL(unquoted));
329 	quoted = safe_emalloc(2, ZSTR_LEN(unquoted), 3 + (use_national_character_set ? 1 : 0));
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_init(quoted, quotedlen, 0);
347 	efree(quoted);
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_quoter");
359 	PDO_DBG_INF_FMT("dbh=%p", dbh);
360 
361 	command = zend_string_init("START TRANSACTION", strlen("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_LONG(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_LONG(return_value, H->buffered);
544 			break;
545 
546 		case PDO_ATTR_EMULATE_PREPARES:
547 		case PDO_MYSQL_ATTR_DIRECT_QUERY:
548 			ZVAL_LONG(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 		default:
580 			PDO_DBG_RETURN(0);
581 	}
582 
583 	PDO_DBG_RETURN(1);
584 }
585 /* }}} */
586 
587 /* {{{ pdo_mysql_check_liveness */
588 static zend_result pdo_mysql_check_liveness(pdo_dbh_t *dbh)
589 {
590 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
591 
592 	PDO_DBG_ENTER("pdo_mysql_check_liveness");
593 	PDO_DBG_INF_FMT("dbh=%p", dbh);
594 
595 	if (mysql_ping(H->server)) {
596 		PDO_DBG_RETURN(FAILURE);
597 	}
598 	PDO_DBG_RETURN(SUCCESS);
599 }
600 /* }}} */
601 
602 /* {{{ pdo_mysql_request_shutdown */
603 static void pdo_mysql_request_shutdown(pdo_dbh_t *dbh)
604 {
605 	PDO_DBG_ENTER("pdo_mysql_request_shutdown");
606 	PDO_DBG_INF_FMT("dbh=%p", dbh);
607 
608 #ifdef PDO_USE_MYSQLND
609 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
610 	if (H->server) {
611 		mysqlnd_end_psession(H->server);
612 	}
613 #endif
614 }
615 /* }}} */
616 
617 #ifdef PDO_USE_MYSQLND
618 # define pdo_mysql_get_server_status(m) mysqlnd_get_server_status(m)
619 #else
620 # define pdo_mysql_get_server_status(m) (m)->server_status
621 #endif
622 
623 /* {{{ pdo_mysql_in_transaction */
624 static bool pdo_mysql_in_transaction(pdo_dbh_t *dbh)
625 {
626 	pdo_mysql_db_handle *H = (pdo_mysql_db_handle *)dbh->driver_data;
627 	PDO_DBG_ENTER("pdo_mysql_in_transaction");
628 	PDO_DBG_RETURN((pdo_mysql_get_server_status(H->server) & SERVER_STATUS_IN_TRANS) != 0);
629 }
630 /* }}} */
631 
632 /* {{{ mysql_methods */
633 static const struct pdo_dbh_methods mysql_methods = {
634 	mysql_handle_closer,
635 	mysql_handle_preparer,
636 	mysql_handle_doer,
637 	mysql_handle_quoter,
638 	mysql_handle_begin,
639 	mysql_handle_commit,
640 	mysql_handle_rollback,
641 	pdo_mysql_set_attribute,
642 	pdo_mysql_last_insert_id,
643 	pdo_mysql_fetch_error_func,
644 	pdo_mysql_get_attribute,
645 	pdo_mysql_check_liveness,
646 	NULL,
647 	pdo_mysql_request_shutdown,
648 	pdo_mysql_in_transaction,
649 	NULL /* get_gc */
650 };
651 /* }}} */
652 
653 #ifdef PHP_WIN32
654 # define PDO_DEFAULT_MYSQL_UNIX_ADDR	NULL
655 #else
656 # define PDO_DEFAULT_MYSQL_UNIX_ADDR	PDO_MYSQL_G(default_socket)
657 #endif
658 
659 /* {{{ pdo_mysql_handle_factory */
660 static int pdo_mysql_handle_factory(pdo_dbh_t *dbh, zval *driver_options)
661 {
662 	pdo_mysql_db_handle *H;
663 	size_t i;
664 	int ret = 0;
665 	char *host = NULL, *unix_socket = NULL;
666 	unsigned int port = 3306;
667 	char *dbname;
668 	struct pdo_data_src_parser vars[] = {
669 		{ "charset",  NULL,	0 },
670 		{ "dbname",   "",	0 },
671 		{ "host",     "localhost",	0 },
672 		{ "port",     "3306",	0 },
673 		{ "unix_socket",  PDO_DEFAULT_MYSQL_UNIX_ADDR,	0 },
674 		{ "user",     NULL,	0 },
675 		{ "password", NULL,	0 },
676 	};
677 	int connect_opts = 0
678 #ifdef CLIENT_MULTI_RESULTS
679 		|CLIENT_MULTI_RESULTS
680 #endif
681 		;
682 #ifdef PDO_USE_MYSQLND
683 	size_t dbname_len = 0;
684 	size_t password_len = 0;
685 #endif
686 
687 #ifdef CLIENT_MULTI_STATEMENTS
688 	if (!driver_options) {
689 		connect_opts |= CLIENT_MULTI_STATEMENTS;
690 	} else if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MULTI_STATEMENTS, 1)) {
691 		connect_opts |= CLIENT_MULTI_STATEMENTS;
692 	}
693 #endif
694 
695 	PDO_DBG_ENTER("pdo_mysql_handle_factory");
696 	PDO_DBG_INF_FMT("dbh=%p", dbh);
697 #ifdef CLIENT_MULTI_RESULTS
698 	PDO_DBG_INF("multi results");
699 #endif
700 
701 	php_pdo_parse_data_source(dbh->data_source, dbh->data_source_len, vars, 7);
702 
703 	H = pecalloc(1, sizeof(pdo_mysql_db_handle), dbh->is_persistent);
704 
705 	H->einfo.errcode = 0;
706 	H->einfo.errmsg = NULL;
707 
708 	/* allocate an environment */
709 
710 	/* handle for the server */
711 	if (!(H->server = pdo_mysql_init(dbh->is_persistent))) {
712 		pdo_mysql_error(dbh);
713 		goto cleanup;
714 	}
715 #ifdef PDO_USE_MYSQLND
716 	if (dbh->is_persistent) {
717 		mysqlnd_restart_psession(H->server);
718 	}
719 #endif
720 
721 	dbh->driver_data = H;
722 
723 	dbh->skip_param_evt =
724 		1 << PDO_PARAM_EVT_FREE |
725 		1 << PDO_PARAM_EVT_EXEC_POST |
726 		1 << PDO_PARAM_EVT_FETCH_PRE |
727 		1 << PDO_PARAM_EVT_FETCH_POST |
728 		1 << PDO_PARAM_EVT_NORMALIZE;
729 
730 #ifndef PDO_USE_MYSQLND
731 	H->max_buffer_size = 1024*1024;
732 #endif
733 
734 	H->assume_national_character_set_strings = 0;
735 	H->buffered = H->emulate_prepare = 1;
736 
737 	/* handle MySQL options */
738 	if (driver_options) {
739 		zend_long connect_timeout = pdo_attr_lval(driver_options, PDO_ATTR_TIMEOUT, 30);
740 		zend_string *init_cmd = NULL;
741 #ifndef PDO_USE_MYSQLND
742 		zend_string *default_file = NULL, *default_group = NULL;
743 #endif
744 		zend_long compress = 0;
745 		zend_string *ssl_key = NULL, *ssl_cert = NULL, *ssl_ca = NULL, *ssl_capath = NULL, *ssl_cipher = NULL;
746 		H->buffered = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_USE_BUFFERED_QUERY, 1);
747 
748 		H->emulate_prepare = pdo_attr_lval(driver_options,
749 			PDO_MYSQL_ATTR_DIRECT_QUERY, H->emulate_prepare);
750 		H->emulate_prepare = pdo_attr_lval(driver_options,
751 			PDO_ATTR_EMULATE_PREPARES, H->emulate_prepare);
752 
753 		H->assume_national_character_set_strings = pdo_attr_lval(driver_options,
754 			PDO_ATTR_DEFAULT_STR_PARAM, 0) == PDO_PARAM_STR_NATL;
755 
756 #ifndef PDO_USE_MYSQLND
757 		H->max_buffer_size = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_MAX_BUFFER_SIZE, H->max_buffer_size);
758 #endif
759 
760 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_FOUND_ROWS, 0)) {
761 			connect_opts |= CLIENT_FOUND_ROWS;
762 		}
763 
764 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_IGNORE_SPACE, 0)) {
765 			connect_opts |= CLIENT_IGNORE_SPACE;
766 		}
767 
768 		if (mysql_options(H->server, MYSQL_OPT_CONNECT_TIMEOUT, (const char *)&connect_timeout)) {
769 			pdo_mysql_error(dbh);
770 			goto cleanup;
771 		}
772 
773 		if (pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE, 0)) {
774 			H->local_infile = 1;
775 #ifndef PDO_USE_MYSQLND
776 			if (PG(open_basedir) && PG(open_basedir)[0] != '\0') {
777 				H->local_infile = 0;
778 			}
779 #endif
780 		}
781 
782 #if (MYSQL_VERSION_ID >= 80021 && !defined(MARIADB_BASE_VERSION)) || defined(PDO_USE_MYSQLND)
783 		zend_string *local_infile_directory = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_LOCAL_INFILE_DIRECTORY, NULL);
784 		if (local_infile_directory && !php_check_open_basedir(ZSTR_VAL(local_infile_directory))) {
785 			if (mysql_options(H->server, MYSQL_OPT_LOAD_DATA_LOCAL_DIR, (const char *)ZSTR_VAL(local_infile_directory))) {
786 				zend_string_release(local_infile_directory);
787 				pdo_mysql_error(dbh);
788 				goto cleanup;
789 			}
790 			zend_string_release(local_infile_directory);
791 		}
792 #endif
793 #ifdef MYSQL_OPT_RECONNECT
794 		/* since 5.0.3, the default for this option is 0 if not specified.
795 		 * we want the old behaviour
796 		 * mysqlnd doesn't support reconnect, thus we don't have "|| defined(PDO_USE_MYSQLND)"
797 		*/
798 		{
799 			zend_long reconnect = 1;
800 			mysql_options(H->server, MYSQL_OPT_RECONNECT, (const char*)&reconnect);
801 		}
802 #endif
803 		init_cmd = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_INIT_COMMAND, NULL);
804 		if (init_cmd) {
805 			if (mysql_options(H->server, MYSQL_INIT_COMMAND, (const char *)ZSTR_VAL(init_cmd))) {
806 				zend_string_release_ex(init_cmd, 0);
807 				pdo_mysql_error(dbh);
808 				goto cleanup;
809 			}
810 			zend_string_release_ex(init_cmd, 0);
811 		}
812 #ifndef PDO_USE_MYSQLND
813 		default_file = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_FILE, NULL);
814 		if (default_file) {
815 			if (mysql_options(H->server, MYSQL_READ_DEFAULT_FILE, (const char *)ZSTR_VAL(default_file))) {
816 				zend_string_release_ex(default_file, 0);
817 				pdo_mysql_error(dbh);
818 				goto cleanup;
819 			}
820 			zend_string_release_ex(default_file, 0);
821 		}
822 
823 		default_group = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_READ_DEFAULT_GROUP, NULL);
824 		if (default_group) {
825 			if (mysql_options(H->server, MYSQL_READ_DEFAULT_GROUP, (const char *)ZSTR_VAL(default_group))) {
826 				zend_string_release_ex(default_group, 0);
827 				pdo_mysql_error(dbh);
828 				goto cleanup;
829 			}
830 			zend_string_release_ex(default_group, 0);
831 		}
832 #endif
833 		compress = pdo_attr_lval(driver_options, PDO_MYSQL_ATTR_COMPRESS, 0);
834 		if (compress) {
835 			if (mysql_options(H->server, MYSQL_OPT_COMPRESS, 0)) {
836 				pdo_mysql_error(dbh);
837 				goto cleanup;
838 			}
839 		}
840 
841 		ssl_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_KEY, NULL);
842 		ssl_cert = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CERT, NULL);
843 		ssl_ca = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CA, NULL);
844 		ssl_capath = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CAPATH, NULL);
845 		ssl_cipher = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SSL_CIPHER, NULL);
846 
847 		if (ssl_key || ssl_cert || ssl_ca || ssl_capath || ssl_cipher) {
848 			mysql_ssl_set(H->server,
849 					ssl_key? ZSTR_VAL(ssl_key) : NULL,
850 					ssl_cert? ZSTR_VAL(ssl_cert) : NULL,
851 					ssl_ca? ZSTR_VAL(ssl_ca) : NULL,
852 					ssl_capath? ZSTR_VAL(ssl_capath) : NULL,
853 					ssl_cipher? ZSTR_VAL(ssl_cipher) : NULL);
854 			if (ssl_key) {
855 				zend_string_release_ex(ssl_key, 0);
856 			}
857 			if (ssl_cert) {
858 				zend_string_release_ex(ssl_cert, 0);
859 			}
860 			if (ssl_ca) {
861 				zend_string_release_ex(ssl_ca, 0);
862 			}
863 			if (ssl_capath) {
864 				zend_string_release_ex(ssl_capath, 0);
865 			}
866 			if (ssl_cipher) {
867 				zend_string_release_ex(ssl_cipher, 0);
868 			}
869 		}
870 
871 #if MYSQL_VERSION_ID > 50605 || defined(PDO_USE_MYSQLND)
872 		{
873 			zend_string *public_key = pdo_attr_strval(driver_options, PDO_MYSQL_ATTR_SERVER_PUBLIC_KEY, NULL);
874 			if (public_key) {
875 				if (mysql_options(H->server, MYSQL_SERVER_PUBLIC_KEY, ZSTR_VAL(public_key))) {
876 					pdo_mysql_error(dbh);
877 					zend_string_release_ex(public_key, 0);
878 					goto cleanup;
879 				}
880 				zend_string_release_ex(public_key, 0);
881 			}
882 		}
883 #endif
884 
885 #ifdef PDO_USE_MYSQLND
886 		{
887 			zend_long ssl_verify_cert = pdo_attr_lval(driver_options,
888 					PDO_MYSQL_ATTR_SSL_VERIFY_SERVER_CERT, -1);
889 			if (ssl_verify_cert != -1) {
890 				connect_opts |= ssl_verify_cert ?
891 					CLIENT_SSL_VERIFY_SERVER_CERT:
892 					CLIENT_SSL_DONT_VERIFY_SERVER_CERT;
893 			}
894 		}
895 #endif
896 	}
897 
898 	/* Always explicitly set the LOCAL_INFILE option. */
899 	unsigned int local_infile = H->local_infile;
900 	if (mysql_options(H->server, MYSQL_OPT_LOCAL_INFILE, (const char *)&local_infile)) {
901 		pdo_mysql_error(dbh);
902 		goto cleanup;
903 	}
904 
905 #ifdef PDO_USE_MYSQLND
906 	unsigned int int_and_float_native = !pdo_attr_lval(driver_options, PDO_ATTR_STRINGIFY_FETCHES, dbh->stringify);
907 	if (mysql_options(H->server, MYSQLND_OPT_INT_AND_FLOAT_NATIVE, (const char *) &int_and_float_native)) {
908 		pdo_mysql_error(dbh);
909 		goto cleanup;
910 	}
911 #endif
912 
913 	if (vars[0].optval && mysql_options(H->server, MYSQL_SET_CHARSET_NAME, vars[0].optval)) {
914 		pdo_mysql_error(dbh);
915 		goto cleanup;
916 	}
917 
918 	dbname = vars[1].optval;
919 	host = vars[2].optval;
920 	if(vars[3].optval) {
921 		port = atoi(vars[3].optval);
922 	}
923 
924 #ifdef PHP_WIN32
925 	if (vars[2].optval && !strcmp(".", vars[2].optval)) {
926 #else
927 	if (vars[2].optval && !strcmp("localhost", vars[2].optval)) {
928 #endif
929 		unix_socket = vars[4].optval;
930 	}
931 
932 	if (!dbh->username && vars[5].optval) {
933 		dbh->username = pestrdup(vars[5].optval, dbh->is_persistent);
934 	}
935 
936 	if (!dbh->password && vars[6].optval) {
937 		dbh->password = pestrdup(vars[6].optval, dbh->is_persistent);
938 	}
939 
940 	/* TODO: - Check zval cache + ZTS */
941 #ifdef PDO_USE_MYSQLND
942 	if (dbname) {
943 		dbname_len = strlen(dbname);
944 	}
945 
946 	if (dbh->password) {
947 		password_len = strlen(dbh->password);
948 	}
949 
950 	if (mysqlnd_connect(H->server, host, dbh->username, dbh->password, password_len, dbname, dbname_len,
951 						port, unix_socket, connect_opts, MYSQLND_CLIENT_NO_FLAG) == NULL) {
952 #else
953 	if (mysql_real_connect(H->server, host, dbh->username, dbh->password, dbname, port, unix_socket, connect_opts) == NULL) {
954 #endif
955 		pdo_mysql_error(dbh);
956 		goto cleanup;
957 	}
958 
959 	if (!dbh->auto_commit) {
960 		mysql_handle_autocommit(dbh);
961 	}
962 
963 	H->attached = 1;
964 
965 	dbh->alloc_own_columns = 1;
966 	dbh->max_escaped_char_length = 2;
967 	dbh->methods = &mysql_methods;
968 
969 	ret = 1;
970 
971 cleanup:
972 	for (i = 0; i < sizeof(vars)/sizeof(vars[0]); i++) {
973 		if (vars[i].freeme) {
974 			efree(vars[i].optval);
975 		}
976 	}
977 
978 	dbh->methods = &mysql_methods;
979 
980 	PDO_DBG_RETURN(ret);
981 }
982 /* }}} */
983 
984 const pdo_driver_t pdo_mysql_driver = {
985 	PDO_DRIVER_HEADER(mysql),
986 	pdo_mysql_handle_factory
987 };
988