xref: /PHP-8.0/ext/pdo/pdo_dbh.c (revision 30d3062c)
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: Wez Furlong <wez@php.net>                                    |
14   |         Marcus Boerger <helly@php.net>                               |
15   |         Sterling Hughes <sterling@php.net>                           |
16   +----------------------------------------------------------------------+
17 */
18 
19 /* The PDO Database Handle Class */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "php.h"
26 #include "php_ini.h"
27 #include "ext/standard/info.h"
28 #include "php_pdo.h"
29 #include "php_pdo_driver.h"
30 #include "php_pdo_int.h"
31 #include "zend_exceptions.h"
32 #include "zend_object_handlers.h"
33 #include "zend_hash.h"
34 #include "zend_interfaces.h"
35 #include "pdo_dbh_arginfo.h"
36 
37 static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value);
38 
pdo_throw_exception(unsigned int driver_errcode,char * driver_errmsg,pdo_error_type * pdo_error)39 void pdo_throw_exception(unsigned int driver_errcode, char *driver_errmsg, pdo_error_type *pdo_error)
40 {
41 		zval error_info,pdo_exception;
42 		char *pdo_exception_message;
43 
44 		object_init_ex(&pdo_exception, php_pdo_get_exception());
45 		array_init(&error_info);
46 
47 		add_next_index_string(&error_info, *pdo_error);
48 		add_next_index_long(&error_info, driver_errcode);
49 		add_next_index_string(&error_info, driver_errmsg);
50 
51 		spprintf(&pdo_exception_message, 0,"SQLSTATE[%s] [%d] %s",*pdo_error, driver_errcode, driver_errmsg);
52 		zend_update_property(php_pdo_get_exception(), Z_OBJ(pdo_exception), "errorInfo", sizeof("errorInfo")-1, &error_info);
53 		zend_update_property_long(php_pdo_get_exception(), Z_OBJ(pdo_exception), "code", sizeof("code")-1, driver_errcode);
54 		zend_update_property_string(
55 			php_pdo_get_exception(),
56 			Z_OBJ(pdo_exception),
57 			"message",
58 			sizeof("message")-1,
59 			pdo_exception_message
60 		);
61 		efree(pdo_exception_message);
62 		zval_ptr_dtor(&error_info);
63 		zend_throw_exception_object(&pdo_exception);
64 }
65 
pdo_raise_impl_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt,const char * sqlstate,const char * supp)66 void pdo_raise_impl_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt, const char *sqlstate, const char *supp) /* {{{ */
67 {
68 	pdo_error_type *pdo_err = &dbh->error_code;
69 	char *message = NULL;
70 	const char *msg;
71 
72 	if (dbh && dbh->error_mode == PDO_ERRMODE_SILENT) {
73 #if 0
74 		/* BUG: if user is running in silent mode and hits an error at the driver level
75 		 * when they use the PDO methods to call up the error information, they may
76 		 * get bogus information */
77 		return;
78 #endif
79 	}
80 
81 	if (stmt) {
82 		pdo_err = &stmt->error_code;
83 	}
84 
85 	strncpy(*pdo_err, sqlstate, 6);
86 
87 	/* hash sqlstate to error messages */
88 	msg = pdo_sqlstate_state_to_description(*pdo_err);
89 	if (!msg) {
90 		msg = "<<Unknown error>>";
91 	}
92 
93 	if (supp) {
94 		spprintf(&message, 0, "SQLSTATE[%s]: %s: %s", *pdo_err, msg, supp);
95 	} else {
96 		spprintf(&message, 0, "SQLSTATE[%s]: %s", *pdo_err, msg);
97 	}
98 
99 	if (dbh && dbh->error_mode != PDO_ERRMODE_EXCEPTION) {
100 		php_error_docref(NULL, E_WARNING, "%s", message);
101 	} else {
102 		zval ex, info;
103 		zend_class_entry *pdo_ex = php_pdo_get_exception();
104 
105 		object_init_ex(&ex, pdo_ex);
106 
107 		zend_update_property_string(zend_ce_exception, Z_OBJ(ex), "message", sizeof("message")-1, message);
108 		zend_update_property_string(zend_ce_exception, Z_OBJ(ex), "code", sizeof("code")-1, *pdo_err);
109 
110 		array_init(&info);
111 
112 		add_next_index_string(&info, *pdo_err);
113 		add_next_index_long(&info, 0);
114 		zend_update_property(pdo_ex, Z_OBJ(ex), "errorInfo", sizeof("errorInfo")-1, &info);
115 		zval_ptr_dtor(&info);
116 
117 		zend_throw_exception_object(&ex);
118 	}
119 
120 	if (message) {
121 		efree(message);
122 	}
123 }
124 /* }}} */
125 
pdo_handle_error(pdo_dbh_t * dbh,pdo_stmt_t * stmt)126 PDO_API void pdo_handle_error(pdo_dbh_t *dbh, pdo_stmt_t *stmt) /* {{{ */
127 {
128 	pdo_error_type *pdo_err = &dbh->error_code;
129 	const char *msg = "<<Unknown>>";
130 	char *supp = NULL;
131 	zend_long native_code = 0;
132 	zend_string *message = NULL;
133 	zval info;
134 
135 	if (dbh == NULL || dbh->error_mode == PDO_ERRMODE_SILENT) {
136 		return;
137 	}
138 
139 	if (stmt) {
140 		pdo_err = &stmt->error_code;
141 	}
142 
143 	/* hash sqlstate to error messages */
144 	msg = pdo_sqlstate_state_to_description(*pdo_err);
145 	if (!msg) {
146 		msg = "<<Unknown error>>";
147 	}
148 
149 	ZVAL_UNDEF(&info);
150 	if (dbh->methods->fetch_err) {
151 		array_init(&info);
152 
153 		add_next_index_string(&info, *pdo_err);
154 
155 		if (dbh->methods->fetch_err(dbh, stmt, &info)) {
156 			zval *item;
157 
158 			if ((item = zend_hash_index_find(Z_ARRVAL(info), 1)) != NULL
159 					&& Z_TYPE_P(item) == IS_LONG) {
160 				native_code = Z_LVAL_P(item);
161 			}
162 
163 			if ((item = zend_hash_index_find(Z_ARRVAL(info), 2)) != NULL) {
164 				supp = estrndup(Z_STRVAL_P(item), Z_STRLEN_P(item));
165 			}
166 		}
167 	}
168 
169 	if (native_code && supp) {
170 		message = strpprintf(0, "SQLSTATE[%s]: %s: " ZEND_LONG_FMT " %s", *pdo_err, msg, native_code, supp);
171 	} else if (supp) {
172 		message = strpprintf(0, "SQLSTATE[%s]: %s: %s", *pdo_err, msg, supp);
173 	} else {
174 		message = strpprintf(0, "SQLSTATE[%s]: %s", *pdo_err, msg);
175 	}
176 
177 	if (dbh->error_mode == PDO_ERRMODE_WARNING) {
178 		php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(message));
179 	} else if (EG(exception) == NULL) {
180 		zval ex;
181 		zend_class_entry *pdo_ex = php_pdo_get_exception();
182 
183 		object_init_ex(&ex, pdo_ex);
184 
185 		zend_update_property_str(zend_ce_exception, Z_OBJ(ex), "message", sizeof("message") - 1, message);
186 		zend_update_property_string(zend_ce_exception, Z_OBJ(ex), "code", sizeof("code") - 1, *pdo_err);
187 
188 		if (!Z_ISUNDEF(info)) {
189 			zend_update_property(pdo_ex, Z_OBJ(ex), "errorInfo", sizeof("errorInfo") - 1, &info);
190 		}
191 
192 		zend_throw_exception_object(&ex);
193 	}
194 
195 	if (!Z_ISUNDEF(info)) {
196 		zval_ptr_dtor(&info);
197 	}
198 
199 	if (message) {
200 		zend_string_release_ex(message, 0);
201 	}
202 
203 	if (supp) {
204 		efree(supp);
205 	}
206 }
207 /* }}} */
208 
dsn_from_uri(char * uri,char * buf,size_t buflen)209 static char *dsn_from_uri(char *uri, char *buf, size_t buflen) /* {{{ */
210 {
211 	php_stream *stream;
212 	char *dsn = NULL;
213 
214 	stream = php_stream_open_wrapper(uri, "rb", REPORT_ERRORS, NULL);
215 	if (stream) {
216 		dsn = php_stream_get_line(stream, buf, buflen, NULL);
217 		php_stream_close(stream);
218 	}
219 	return dsn;
220 }
221 /* }}} */
222 
223 /* {{{ */
PHP_METHOD(PDO,__construct)224 PHP_METHOD(PDO, __construct)
225 {
226 	zval *object = ZEND_THIS;
227 	pdo_dbh_t *dbh = NULL;
228 	zend_bool is_persistent = 0;
229 	char *data_source;
230 	size_t data_source_len;
231 	char *colon;
232 	char *username=NULL, *password=NULL;
233 	size_t usernamelen, passwordlen;
234 	pdo_driver_t *driver = NULL;
235 	zval *options = NULL;
236 	char alt_dsn[512];
237 	int call_factory = 1;
238 	zend_error_handling zeh;
239 
240 	ZEND_PARSE_PARAMETERS_START(1, 4)
241 		Z_PARAM_STRING(data_source, data_source_len)
242 		Z_PARAM_OPTIONAL
243 		Z_PARAM_STRING_OR_NULL(username, usernamelen)
244 		Z_PARAM_STRING_OR_NULL(password, passwordlen)
245 		Z_PARAM_ARRAY_OR_NULL(options)
246 	ZEND_PARSE_PARAMETERS_END();
247 
248 	/* parse the data source name */
249 	colon = strchr(data_source, ':');
250 
251 	if (!colon) {
252 		/* let's see if this string has a matching dsn in the php.ini */
253 		char *ini_dsn = NULL;
254 
255 		snprintf(alt_dsn, sizeof(alt_dsn), "pdo.dsn.%s", data_source);
256 		if (FAILURE == cfg_get_string(alt_dsn, &ini_dsn)) {
257 			zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source name");
258 			RETURN_THROWS();
259 		}
260 
261 		data_source = ini_dsn;
262 		colon = strchr(data_source, ':');
263 
264 		if (!colon) {
265 			zend_throw_exception_ex(php_pdo_get_exception(), 0, "invalid data source name (via INI: %s)", alt_dsn);
266 			RETURN_THROWS();
267 		}
268 	}
269 
270 	if (!strncmp(data_source, "uri:", sizeof("uri:")-1)) {
271 		/* the specified URI holds connection details */
272 		data_source = dsn_from_uri(data_source + sizeof("uri:")-1, alt_dsn, sizeof(alt_dsn));
273 		if (!data_source) {
274 			zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source URI");
275 			RETURN_THROWS();
276 		}
277 		colon = strchr(data_source, ':');
278 		if (!colon) {
279 			zend_argument_error(php_pdo_get_exception(), 1, "must be a valid data source name (via URI)");
280 			RETURN_THROWS();
281 		}
282 	}
283 
284 	driver = pdo_find_driver(data_source, colon - data_source);
285 
286 	if (!driver) {
287 		/* NB: don't want to include the data_source in the error message as
288 		 * it might contain a password */
289 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "could not find driver");
290 		RETURN_THROWS();
291 	}
292 
293 	dbh = Z_PDO_DBH_P(object);
294 
295 	/* is this supposed to be a persistent connection ? */
296 	if (options) {
297 		int plen = 0;
298 		char *hashkey = NULL;
299 		zend_resource *le;
300 		pdo_dbh_t *pdbh = NULL;
301 		zval *v;
302 
303 		if ((v = zend_hash_index_find_deref(Z_ARRVAL_P(options), PDO_ATTR_PERSISTENT)) != NULL) {
304 			if (Z_TYPE_P(v) == IS_STRING &&
305 				!is_numeric_string(Z_STRVAL_P(v), Z_STRLEN_P(v), NULL, NULL, 0) && Z_STRLEN_P(v) > 0) {
306 				/* user specified key */
307 				plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s:%s", data_source,
308 						username ? username : "",
309 						password ? password : "",
310 						Z_STRVAL_P(v));
311 				is_persistent = 1;
312 			} else {
313 				is_persistent = zval_get_long(v) ? 1 : 0;
314 				plen = spprintf(&hashkey, 0, "PDO:DBH:DSN=%s:%s:%s", data_source,
315 						username ? username : "",
316 						password ? password : "");
317 			}
318 		}
319 
320 		if (is_persistent) {
321 			/* let's see if we have one cached.... */
322 			if ((le = zend_hash_str_find_ptr(&EG(persistent_list), hashkey, plen)) != NULL) {
323 				if (le->type == php_pdo_list_entry()) {
324 					pdbh = (pdo_dbh_t*)le->ptr;
325 
326 					/* is the connection still alive ? */
327 					if (pdbh->methods->check_liveness && FAILURE == (pdbh->methods->check_liveness)(pdbh)) {
328 						/* nope... need to kill it */
329 						pdbh->refcount--;
330 						zend_list_close(le);
331 						pdbh = NULL;
332 					}
333 				}
334 			}
335 
336 			if (pdbh) {
337 				call_factory = 0;
338 			} else {
339 				/* need a brand new pdbh */
340 				pdbh = pecalloc(1, sizeof(*pdbh), 1);
341 
342 				pdbh->refcount = 1;
343 				pdbh->is_persistent = 1;
344 				pdbh->persistent_id = pemalloc(plen + 1, 1);
345 				memcpy((char *)pdbh->persistent_id, hashkey, plen+1);
346 				pdbh->persistent_id_len = plen;
347 				pdbh->def_stmt_ce = dbh->def_stmt_ce;
348 			}
349 		}
350 
351 		if (pdbh) {
352 			efree(dbh);
353 			/* switch over to the persistent one */
354 			Z_PDO_OBJECT_P(object)->inner = pdbh;
355 			pdbh->refcount++;
356 			dbh = pdbh;
357 		}
358 
359 		if (hashkey) {
360 			efree(hashkey);
361 		}
362 	}
363 
364 	if (call_factory) {
365 		dbh->data_source_len = strlen(colon + 1);
366 		dbh->data_source = (const char*)pestrdup(colon + 1, is_persistent);
367 		dbh->username = username ? pestrdup(username, is_persistent) : NULL;
368 		dbh->password = password ? pestrdup(password, is_persistent) : NULL;
369 		dbh->default_fetch_type = PDO_FETCH_BOTH;
370 	}
371 
372 	dbh->auto_commit = pdo_attr_lval(options, PDO_ATTR_AUTOCOMMIT, 1);
373 	dbh->error_mode = pdo_attr_lval(options, PDO_ATTR_ERRMODE, PDO_ERRMODE_EXCEPTION);
374 
375 	if (!dbh->data_source || (username && !dbh->username) || (password && !dbh->password)) {
376 		php_error_docref(NULL, E_ERROR, "Out of memory");
377 	}
378 
379 	zend_replace_error_handling(EH_THROW, pdo_exception_ce, &zeh);
380 
381 	if (!call_factory) {
382 		/* we got a persistent guy from our cache */
383 		goto options;
384 	}
385 
386 	if (driver->db_handle_factory(dbh, options)) {
387 		/* all set */
388 
389 		if (is_persistent) {
390 			/* register in the persistent list etc. */
391 			/* we should also need to replace the object store entry,
392 			   since it was created with emalloc */
393 			if ((zend_register_persistent_resource(
394 						(char*)dbh->persistent_id, dbh->persistent_id_len, dbh, php_pdo_list_entry())) == NULL) {
395 				php_error_docref(NULL, E_ERROR, "Failed to register persistent entry");
396 			}
397 		}
398 
399 		dbh->driver = driver;
400 options:
401 		if (options) {
402 			zval *attr_value;
403 			zend_ulong long_key;
404 			zend_string *str_key = NULL;
405 
406 			ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(options), long_key, str_key, attr_value) {
407 				if (str_key) {
408 					continue;
409 				}
410 				ZVAL_DEREF(attr_value);
411 				/* TODO: Check that this doesn't fail? */
412 				pdo_dbh_attribute_set(dbh, long_key, attr_value);
413 			} ZEND_HASH_FOREACH_END();
414 		}
415 
416 		zend_restore_error_handling(&zeh);
417 		return;
418 	}
419 
420 	/* the connection failed; things will tidy up in free_storage */
421 	/* XXX raise exception */
422 	zend_restore_error_handling(&zeh);
423 	if (!EG(exception)) {
424 		zend_throw_exception(pdo_exception_ce, "Constructor failed", 0);
425 	}
426 }
427 /* }}} */
428 
pdo_stmt_instantiate(pdo_dbh_t * dbh,zval * object,zend_class_entry * dbstmt_ce,zval * ctor_args)429 static zval *pdo_stmt_instantiate(pdo_dbh_t *dbh, zval *object, zend_class_entry *dbstmt_ce, zval *ctor_args) /* {{{ */
430 {
431 	if (!Z_ISUNDEF_P(ctor_args)) {
432 		/* This implies an error within PDO if this does not hold */
433 		ZEND_ASSERT(Z_TYPE_P(ctor_args) == IS_ARRAY);
434 		if (!dbstmt_ce->constructor) {
435 			zend_throw_error(NULL, "User-supplied statement does not accept constructor arguments");
436 			return NULL;
437 		}
438 	}
439 
440 	if (UNEXPECTED(object_init_ex(object, dbstmt_ce) != SUCCESS)) {
441 		if (EXPECTED(!EG(exception))) {
442 			zend_throw_error(NULL, "Cannot instantiate user-supplied statement class");
443 		}
444 		return NULL;
445 	}
446 
447 	return object;
448 } /* }}} */
449 
pdo_stmt_construct(zend_execute_data * execute_data,pdo_stmt_t * stmt,zval * object,zend_class_entry * dbstmt_ce,zval * ctor_args)450 static void pdo_stmt_construct(zend_execute_data *execute_data, pdo_stmt_t *stmt, zval *object, zend_class_entry *dbstmt_ce, zval *ctor_args) /* {{{ */
451 {
452 	zval query_string;
453 	zend_string *key;
454 
455 	ZVAL_STRINGL(&query_string, stmt->query_string, stmt->query_stringlen);
456 	key = zend_string_init("queryString", sizeof("queryString") - 1, 0);
457 	zend_std_write_property(Z_OBJ_P(object), key, &query_string, NULL);
458 	zval_ptr_dtor(&query_string);
459 	zend_string_release_ex(key, 0);
460 
461 	if (dbstmt_ce->constructor) {
462 		zend_fcall_info fci;
463 		zend_fcall_info_cache fcc;
464 		zval retval;
465 
466 		fci.size = sizeof(zend_fcall_info);
467 		ZVAL_UNDEF(&fci.function_name);
468 		fci.object = Z_OBJ_P(object);
469 		fci.retval = &retval;
470 		fci.param_count = 0;
471 		fci.params = NULL;
472 		fci.named_params = NULL;
473 
474 		zend_fcall_info_args(&fci, ctor_args);
475 
476 		fcc.function_handler = dbstmt_ce->constructor;
477 		fcc.called_scope = Z_OBJCE_P(object);
478 		fcc.object = Z_OBJ_P(object);
479 
480 		if (zend_call_function(&fci, &fcc) != FAILURE) {
481 			zval_ptr_dtor(&retval);
482 		}
483 
484 		zend_fcall_info_args_clear(&fci, 1);
485 	}
486 }
487 /* }}} */
488 
489 /* {{{ Prepares a statement for execution and returns a statement object */
PHP_METHOD(PDO,prepare)490 PHP_METHOD(PDO, prepare)
491 {
492 	pdo_stmt_t *stmt;
493 	char *statement;
494 	size_t statement_len;
495 	zval *options = NULL, *value, *item, ctor_args;
496 	zend_class_entry *dbstmt_ce, *pce;
497 	pdo_dbh_object_t *dbh_obj = Z_PDO_OBJECT_P(ZEND_THIS);
498 	pdo_dbh_t *dbh = dbh_obj->inner;
499 
500 	ZEND_PARSE_PARAMETERS_START(1, 2)
501 		Z_PARAM_STRING(statement, statement_len)
502 		Z_PARAM_OPTIONAL
503 		Z_PARAM_ARRAY(options)
504 	ZEND_PARSE_PARAMETERS_END();
505 
506 	PDO_CONSTRUCT_CHECK;
507 
508 	if (statement_len == 0) {
509 		zend_argument_value_error(1, "cannot be empty");
510 		RETURN_THROWS();
511 	}
512 
513 	PDO_DBH_CLEAR_ERR();
514 
515 	if (options && (value = zend_hash_index_find(Z_ARRVAL_P(options), PDO_ATTR_STATEMENT_CLASS)) != NULL) {
516 		if (Z_TYPE_P(value) != IS_ARRAY) {
517 			zend_type_error("PDO::ATTR_STATEMENT_CLASS value must be of type array, %s given",
518 				zend_zval_type_name(value));
519 			RETURN_THROWS();
520 		}
521 		if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 0)) == NULL) {
522 			zend_value_error("PDO::ATTR_STATEMENT_CLASS value must be an array with the format "
523 				"array(classname, constructor_args)");
524 			RETURN_THROWS();
525 		}
526 		if (Z_TYPE_P(item) != IS_STRING || (pce = zend_lookup_class(Z_STR_P(item))) == NULL) {
527 			zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be a valid class");
528 			RETURN_THROWS();
529 		}
530 		dbstmt_ce = pce;
531 		if (!instanceof_function(dbstmt_ce, pdo_dbstmt_ce)) {
532 			zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be derived from PDOStatement");
533 			RETURN_THROWS();
534 		}
535 		if (dbstmt_ce->constructor && !(dbstmt_ce->constructor->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED))) {
536 			zend_type_error("User-supplied statement class cannot have a public constructor");
537 			RETURN_THROWS();
538 		}
539 		if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 1)) != NULL) {
540 			if (Z_TYPE_P(item) != IS_ARRAY) {
541 				zend_type_error("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given",
542 					zend_zval_type_name(value));
543 				RETURN_THROWS();
544 			}
545 			ZVAL_COPY_VALUE(&ctor_args, item);
546 		} else {
547 			ZVAL_UNDEF(&ctor_args);
548 		}
549 	} else {
550 		dbstmt_ce = dbh->def_stmt_ce;
551 		ZVAL_COPY_VALUE(&ctor_args, &dbh->def_stmt_ctor_args);
552 	}
553 
554 	if (!pdo_stmt_instantiate(dbh, return_value, dbstmt_ce, &ctor_args)) {
555 		RETURN_THROWS();
556 	}
557 	stmt = Z_PDO_STMT_P(return_value);
558 
559 	/* unconditionally keep this for later reference */
560 	stmt->query_string = estrndup(statement, statement_len);
561 	stmt->query_stringlen = statement_len;
562 	stmt->default_fetch_type = dbh->default_fetch_type;
563 	stmt->dbh = dbh;
564 	/* give it a reference to me */
565 	ZVAL_OBJ_COPY(&stmt->database_object_handle, &dbh_obj->std);
566 	/* we haven't created a lazy object yet */
567 	ZVAL_UNDEF(&stmt->lazy_object_ref);
568 
569 	if (dbh->methods->preparer(dbh, statement, statement_len, stmt, options)) {
570 		pdo_stmt_construct(execute_data, stmt, return_value, dbstmt_ce, &ctor_args);
571 		return;
572 	}
573 
574 	PDO_HANDLE_DBH_ERR();
575 
576 	/* kill the object handle for the stmt here */
577 	zval_ptr_dtor(return_value);
578 
579 	RETURN_FALSE;
580 }
581 /* }}} */
582 
583 
pdo_is_in_transaction(pdo_dbh_t * dbh)584 static zend_bool pdo_is_in_transaction(pdo_dbh_t *dbh) {
585 	if (dbh->methods->in_transaction) {
586 		return dbh->methods->in_transaction(dbh);
587 	}
588 	return dbh->in_txn;
589 }
590 
591 /* {{{ Initiates a transaction */
PHP_METHOD(PDO,beginTransaction)592 PHP_METHOD(PDO, beginTransaction)
593 {
594 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
595 
596 	ZEND_PARSE_PARAMETERS_NONE();
597 
598 	PDO_CONSTRUCT_CHECK;
599 
600 	if (pdo_is_in_transaction(dbh)) {
601 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "There is already an active transaction");
602 		RETURN_THROWS();
603 	}
604 
605 	if (!dbh->methods->begin) {
606 		/* TODO: this should be an exception; see the auto-commit mode
607 		 * comments below */
608 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "This driver doesn't support transactions");
609 		RETURN_THROWS();
610 	}
611 
612 	if (dbh->methods->begin(dbh)) {
613 		dbh->in_txn = 1;
614 		RETURN_TRUE;
615 	}
616 
617 	PDO_HANDLE_DBH_ERR();
618 	RETURN_FALSE;
619 }
620 /* }}} */
621 
622 /* {{{ Commit a transaction */
PHP_METHOD(PDO,commit)623 PHP_METHOD(PDO, commit)
624 {
625 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
626 
627 	ZEND_PARSE_PARAMETERS_NONE();
628 
629 	PDO_CONSTRUCT_CHECK;
630 
631 	if (!pdo_is_in_transaction(dbh)) {
632 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "There is no active transaction");
633 		RETURN_THROWS();
634 	}
635 
636 	if (dbh->methods->commit(dbh)) {
637 		dbh->in_txn = 0;
638 		RETURN_TRUE;
639 	}
640 
641 	PDO_HANDLE_DBH_ERR();
642 	RETURN_FALSE;
643 }
644 /* }}} */
645 
646 /* {{{ roll back a transaction */
PHP_METHOD(PDO,rollBack)647 PHP_METHOD(PDO, rollBack)
648 {
649 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
650 
651 	ZEND_PARSE_PARAMETERS_NONE();
652 
653 	PDO_CONSTRUCT_CHECK;
654 
655 	if (!pdo_is_in_transaction(dbh)) {
656 		zend_throw_exception_ex(php_pdo_get_exception(), 0, "There is no active transaction");
657 		RETURN_THROWS();
658 	}
659 
660 	if (dbh->methods->rollback(dbh)) {
661 		dbh->in_txn = 0;
662 		RETURN_TRUE;
663 	}
664 
665 	PDO_HANDLE_DBH_ERR();
666 	RETURN_FALSE;
667 }
668 /* }}} */
669 
670 /* {{{ determine if inside a transaction */
PHP_METHOD(PDO,inTransaction)671 PHP_METHOD(PDO, inTransaction)
672 {
673 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
674 
675 	ZEND_PARSE_PARAMETERS_NONE();
676 
677 	PDO_CONSTRUCT_CHECK;
678 
679 	RETURN_BOOL(pdo_is_in_transaction(dbh));
680 }
681 /* }}} */
682 
pdo_dbh_attribute_set(pdo_dbh_t * dbh,zend_long attr,zval * value)683 static zend_result pdo_dbh_attribute_set(pdo_dbh_t *dbh, zend_long attr, zval *value) /* {{{ */
684 {
685 	zend_long lval;
686 
687 /* TODO: Make distinction between numeric and non-numeric strings */
688 #define PDO_LONG_PARAM_CHECK \
689 	if (Z_TYPE_P(value) != IS_LONG && Z_TYPE_P(value) != IS_STRING && Z_TYPE_P(value) != IS_FALSE && Z_TYPE_P(value) != IS_TRUE) { \
690 		zend_type_error("Attribute value must be of type int for selected attribute, %s given", zend_zval_type_name(value)); \
691 		return FAILURE; \
692 	} \
693 
694 	switch (attr) {
695 		case PDO_ATTR_ERRMODE:
696 			PDO_LONG_PARAM_CHECK;
697 			lval = zval_get_long(value);
698 			switch (lval) {
699 				case PDO_ERRMODE_SILENT:
700 				case PDO_ERRMODE_WARNING:
701 				case PDO_ERRMODE_EXCEPTION:
702 					dbh->error_mode = lval;
703 					return SUCCESS;
704 				default:
705 					zend_value_error("Error mode must be one of the PDO::ERRMODE_* constants");
706 					return FAILURE;
707 			}
708 			return FAILURE;
709 
710 		case PDO_ATTR_CASE:
711 			PDO_LONG_PARAM_CHECK;
712 			lval = zval_get_long(value);
713 			switch (lval) {
714 				case PDO_CASE_NATURAL:
715 				case PDO_CASE_UPPER:
716 				case PDO_CASE_LOWER:
717 					dbh->desired_case = lval;
718 					return SUCCESS;
719 				default:
720 					zend_value_error("Case folding mode must be one of the PDO::CASE_* constants");
721 					return FAILURE;
722 			}
723 			return FAILURE;
724 
725 		case PDO_ATTR_ORACLE_NULLS:
726 			PDO_LONG_PARAM_CHECK;
727 			dbh->oracle_nulls = zval_get_long(value);
728 			return SUCCESS;
729 
730 		case PDO_ATTR_DEFAULT_FETCH_MODE:
731 			if (Z_TYPE_P(value) == IS_ARRAY) {
732 				zval *tmp;
733 				if ((tmp = zend_hash_index_find(Z_ARRVAL_P(value), 0)) != NULL && Z_TYPE_P(tmp) == IS_LONG) {
734 					if (Z_LVAL_P(tmp) == PDO_FETCH_INTO || Z_LVAL_P(tmp) == PDO_FETCH_CLASS) {
735 						zend_value_error("PDO::FETCH_INTO and PDO::FETCH_CLASS cannot be set as the default fetch mode");
736 						return FAILURE;
737 					}
738 				}
739 			} else {
740 				PDO_LONG_PARAM_CHECK;
741 			}
742 			lval = zval_get_long(value);
743 			if (lval == PDO_FETCH_USE_DEFAULT) {
744 				zend_value_error("Fetch mode must be a bitmask of PDO::FETCH_* constants");
745 				return FAILURE;
746 			}
747 			dbh->default_fetch_type = lval;
748 			return SUCCESS;
749 
750 		case PDO_ATTR_STRINGIFY_FETCHES:
751 			PDO_LONG_PARAM_CHECK;
752 			dbh->stringify = zval_get_long(value) ? 1 : 0;
753 			return SUCCESS;
754 
755 		case PDO_ATTR_STATEMENT_CLASS: {
756 			/* array(string classname, array(mixed ctor_args)) */
757 			zend_class_entry *pce;
758 			zval *item;
759 
760 			if (dbh->is_persistent) {
761 				/* TODO: ValueError/ PDOException? */
762 				pdo_raise_impl_error(dbh, NULL, "HY000",
763 					"PDO::ATTR_STATEMENT_CLASS cannot be used with persistent PDO instances"
764 					);
765 				PDO_HANDLE_DBH_ERR();
766 				return FAILURE;
767 			}
768 			if (Z_TYPE_P(value) != IS_ARRAY) {
769 				zend_type_error("PDO::ATTR_STATEMENT_CLASS value must be of type array, %s given",
770 					zend_zval_type_name(value));
771 				return FAILURE;
772 			}
773 			if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 0)) == NULL) {
774 				zend_value_error("PDO::ATTR_STATEMENT_CLASS value must be an array with the format "
775 					"array(classname, constructor_args)");
776 				return FAILURE;
777 			}
778 			if (Z_TYPE_P(item) != IS_STRING || (pce = zend_lookup_class(Z_STR_P(item))) == NULL) {
779 				zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be a valid class");
780 				return FAILURE;
781 			}
782 			if (!instanceof_function(pce, pdo_dbstmt_ce)) {
783 				zend_type_error("PDO::ATTR_STATEMENT_CLASS class must be derived from PDOStatement");
784 				return FAILURE;
785 			}
786 			if (pce->constructor && !(pce->constructor->common.fn_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_PROTECTED))) {
787 				zend_type_error("User-supplied statement class cannot have a public constructor");
788 				return FAILURE;
789 			}
790 			dbh->def_stmt_ce = pce;
791 			if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
792 				zval_ptr_dtor(&dbh->def_stmt_ctor_args);
793 				ZVAL_UNDEF(&dbh->def_stmt_ctor_args);
794 			}
795 			if ((item = zend_hash_index_find(Z_ARRVAL_P(value), 1)) != NULL) {
796 				if (Z_TYPE_P(item) != IS_ARRAY) {
797 					zend_type_error("PDO::ATTR_STATEMENT_CLASS constructor_args must be of type ?array, %s given",
798 						zend_zval_type_name(value));
799 					return FAILURE;
800 				}
801 				ZVAL_COPY(&dbh->def_stmt_ctor_args, item);
802 			}
803 			return SUCCESS;
804 		}
805 
806 		default:
807 			;
808 	}
809 
810 	if (!dbh->methods->set_attribute) {
811 		goto fail;
812 	}
813 
814 	PDO_DBH_CLEAR_ERR();
815 	if (dbh->methods->set_attribute(dbh, attr, value)) {
816 		return SUCCESS;
817 	}
818 
819 fail:
820 	if (!dbh->methods->set_attribute) {
821 		pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support setting attributes");
822 	} else {
823 		PDO_HANDLE_DBH_ERR();
824 	}
825 	return FAILURE;
826 }
827 /* }}} */
828 
829 /* {{{ Set an attribute */
PHP_METHOD(PDO,setAttribute)830 PHP_METHOD(PDO, setAttribute)
831 {
832 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
833 	zend_long attr;
834 	zval *value;
835 
836 	ZEND_PARSE_PARAMETERS_START(2, 2)
837 		Z_PARAM_LONG(attr)
838 		Z_PARAM_ZVAL(value)
839 	ZEND_PARSE_PARAMETERS_END();
840 
841 	PDO_DBH_CLEAR_ERR();
842 	PDO_CONSTRUCT_CHECK;
843 
844 	if (pdo_dbh_attribute_set(dbh, attr, value) != FAILURE) {
845  		RETURN_TRUE;
846  	}
847  	RETURN_FALSE;
848 }
849 /* }}} */
850 
851 /* {{{ Get an attribute */
PHP_METHOD(PDO,getAttribute)852 PHP_METHOD(PDO, getAttribute)
853 {
854 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
855 	zend_long attr;
856 
857 	ZEND_PARSE_PARAMETERS_START(1, 1)
858 		Z_PARAM_LONG(attr)
859 	ZEND_PARSE_PARAMETERS_END();
860 
861 	PDO_DBH_CLEAR_ERR();
862 	PDO_CONSTRUCT_CHECK;
863 
864 	/* handle generic PDO-level attributes */
865 	switch (attr) {
866 		case PDO_ATTR_PERSISTENT:
867 			RETURN_BOOL(dbh->is_persistent);
868 
869 		case PDO_ATTR_CASE:
870 			RETURN_LONG(dbh->desired_case);
871 
872 		case PDO_ATTR_ORACLE_NULLS:
873 			RETURN_LONG(dbh->oracle_nulls);
874 
875 		case PDO_ATTR_ERRMODE:
876 			RETURN_LONG(dbh->error_mode);
877 
878 		case PDO_ATTR_DRIVER_NAME:
879 			RETURN_STRINGL((char*)dbh->driver->driver_name, dbh->driver->driver_name_len);
880 
881 		case PDO_ATTR_STATEMENT_CLASS:
882 			array_init(return_value);
883 			add_next_index_str(return_value, zend_string_copy(dbh->def_stmt_ce->name));
884 			if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
885 				Z_TRY_ADDREF(dbh->def_stmt_ctor_args);
886 				add_next_index_zval(return_value, &dbh->def_stmt_ctor_args);
887 			}
888 			return;
889 		case PDO_ATTR_DEFAULT_FETCH_MODE:
890 			RETURN_LONG(dbh->default_fetch_type);
891 		default:
892 			break;
893 	}
894 
895 	if (!dbh->methods->get_attribute) {
896 		pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support getting attributes");
897 		RETURN_FALSE;
898 	}
899 
900 	switch (dbh->methods->get_attribute(dbh, attr, return_value)) {
901 		case -1:
902 			PDO_HANDLE_DBH_ERR();
903 			RETURN_FALSE;
904 
905 		case 0:
906 			pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support that attribute");
907 			RETURN_FALSE;
908 
909 		default:
910 			return;
911 	}
912 }
913 /* }}} */
914 
915 /* {{{ Execute a statement that does not return a row set, returning the number of affected rows */
PHP_METHOD(PDO,exec)916 PHP_METHOD(PDO, exec)
917 {
918 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
919 	char *statement;
920 	size_t statement_len;
921 	zend_long ret;
922 
923 	ZEND_PARSE_PARAMETERS_START(1, 1)
924 		Z_PARAM_STRING(statement, statement_len)
925 	ZEND_PARSE_PARAMETERS_END();
926 
927 	if (statement_len == 0) {
928 		zend_argument_value_error(1, "cannot be empty");
929 		RETURN_THROWS();
930 	}
931 
932 	PDO_DBH_CLEAR_ERR();
933 	PDO_CONSTRUCT_CHECK;
934 	ret = dbh->methods->doer(dbh, statement, statement_len);
935 	if(ret == -1) {
936 		PDO_HANDLE_DBH_ERR();
937 		RETURN_FALSE;
938 	} else {
939 		RETURN_LONG(ret);
940 	}
941 }
942 /* }}} */
943 
944 /* {{{ Returns the id of the last row that we affected on this connection. Some databases require a sequence or table name to be passed in. Not always meaningful. */
PHP_METHOD(PDO,lastInsertId)945 PHP_METHOD(PDO, lastInsertId)
946 {
947 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
948 	char *name = NULL;
949 	size_t namelen;
950 
951 	ZEND_PARSE_PARAMETERS_START(0, 1)
952 		Z_PARAM_OPTIONAL
953 		Z_PARAM_STRING_OR_NULL(name, namelen)
954 	ZEND_PARSE_PARAMETERS_END();
955 
956 	PDO_CONSTRUCT_CHECK;
957 
958 	PDO_DBH_CLEAR_ERR();
959 
960 	if (!dbh->methods->last_id) {
961 		pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support lastInsertId()");
962 		RETURN_FALSE;
963 	} else {
964 		size_t id_len;
965 		char *id;
966 		id = dbh->methods->last_id(dbh, name, &id_len);
967 		if (!id) {
968 			PDO_HANDLE_DBH_ERR();
969 			RETURN_FALSE;
970 		} else {
971 			//??? use zend_string ?
972 			RETVAL_STRINGL(id, id_len);
973 			efree(id);
974 		}
975 	}
976 }
977 /* }}} */
978 
979 /* {{{ Fetch the error code associated with the last operation on the database handle */
PHP_METHOD(PDO,errorCode)980 PHP_METHOD(PDO, errorCode)
981 {
982 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
983 
984 	ZEND_PARSE_PARAMETERS_NONE();
985 
986 	PDO_CONSTRUCT_CHECK;
987 
988 	if (dbh->query_stmt) {
989 		RETURN_STRING(dbh->query_stmt->error_code);
990 	}
991 
992 	if (dbh->error_code[0] == '\0') {
993 		RETURN_NULL();
994 	}
995 
996 	/**
997 	 * Making sure that we fallback to the default implementation
998 	 * if the dbh->error_code is not null.
999 	 */
1000 	RETURN_STRING(dbh->error_code);
1001 }
1002 /* }}} */
1003 
1004 /* {{{ Fetch extended error information associated with the last operation on the database handle */
PHP_METHOD(PDO,errorInfo)1005 PHP_METHOD(PDO, errorInfo)
1006 {
1007 	int error_count;
1008 	int error_count_diff 	 = 0;
1009 	int error_expected_count = 3;
1010 
1011 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
1012 
1013 	ZEND_PARSE_PARAMETERS_NONE();
1014 
1015 	PDO_CONSTRUCT_CHECK;
1016 
1017 	array_init(return_value);
1018 
1019 	if (dbh->query_stmt) {
1020 		add_next_index_string(return_value, dbh->query_stmt->error_code);
1021 		if(!strncmp(dbh->query_stmt->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE))) goto fill_array;
1022 	} else {
1023 		add_next_index_string(return_value, dbh->error_code);
1024 		if(!strncmp(dbh->error_code, PDO_ERR_NONE, sizeof(PDO_ERR_NONE))) goto fill_array;
1025 	}
1026 
1027 	if (dbh->methods->fetch_err) {
1028 		dbh->methods->fetch_err(dbh, dbh->query_stmt, return_value);
1029 	}
1030 
1031 fill_array:
1032 	/**
1033 	 * In order to be consistent, we have to make sure we add the good amount
1034 	 * of nulls depending on the current number of elements. We make a simple
1035 	 * difference and add the needed elements
1036 	 */
1037 	error_count = zend_hash_num_elements(Z_ARRVAL_P(return_value));
1038 
1039 	if (error_expected_count > error_count) {
1040 		int current_index;
1041 
1042 		error_count_diff = error_expected_count - error_count;
1043 		for (current_index = 0; current_index < error_count_diff; current_index++) {
1044 			add_next_index_null(return_value);
1045 		}
1046 	}
1047 }
1048 /* }}} */
1049 
1050 /* {{{ Prepare and execute $sql; returns the statement object for iteration */
PHP_METHOD(PDO,query)1051 PHP_METHOD(PDO, query)
1052 {
1053 	pdo_stmt_t *stmt;
1054 	char *statement;
1055 	size_t statement_len;
1056 	zend_long fetch_mode;
1057 	zend_bool fetch_mode_is_null = 1;
1058 	zval *args = NULL;
1059 	uint32_t num_args = 0;
1060 	pdo_dbh_object_t *dbh_obj = Z_PDO_OBJECT_P(ZEND_THIS);
1061 	pdo_dbh_t *dbh = dbh_obj->inner;
1062 
1063 	if (FAILURE == zend_parse_parameters(ZEND_NUM_ARGS(), "s|l!*", &statement, &statement_len,
1064 			&fetch_mode, &fetch_mode_is_null, &args, &num_args)) {
1065 		RETURN_THROWS();
1066 	}
1067 
1068 	PDO_CONSTRUCT_CHECK;
1069 
1070 	if (statement_len == 0) {
1071 		zend_argument_value_error(1, "cannot be empty");
1072 		RETURN_THROWS();
1073 	}
1074 
1075 	PDO_DBH_CLEAR_ERR();
1076 
1077 	if (!pdo_stmt_instantiate(dbh, return_value, dbh->def_stmt_ce, &dbh->def_stmt_ctor_args)) {
1078 		RETURN_THROWS();
1079 	}
1080 	stmt = Z_PDO_STMT_P(return_value);
1081 
1082 	/* unconditionally keep this for later reference */
1083 	stmt->query_string = estrndup(statement, statement_len);
1084 	stmt->query_stringlen = statement_len;
1085 
1086 	stmt->default_fetch_type = dbh->default_fetch_type;
1087 	stmt->active_query_string = stmt->query_string;
1088 	stmt->active_query_stringlen = statement_len;
1089 	stmt->dbh = dbh;
1090 	/* give it a reference to me */
1091 	ZVAL_OBJ_COPY(&stmt->database_object_handle, &dbh_obj->std);
1092 	/* we haven't created a lazy object yet */
1093 	ZVAL_UNDEF(&stmt->lazy_object_ref);
1094 
1095 	if (dbh->methods->preparer(dbh, statement, statement_len, stmt, NULL)) {
1096 		PDO_STMT_CLEAR_ERR();
1097 		if (fetch_mode_is_null || pdo_stmt_setup_fetch_mode(stmt, fetch_mode, 2, args, num_args)) {
1098 			/* now execute the statement */
1099 			PDO_STMT_CLEAR_ERR();
1100 			if (stmt->methods->executer(stmt)) {
1101 				int ret = 1;
1102 				if (!stmt->executed) {
1103 					if (stmt->dbh->alloc_own_columns) {
1104 						ret = pdo_stmt_describe_columns(stmt);
1105 					}
1106 					stmt->executed = 1;
1107 				}
1108 				if (ret) {
1109 					pdo_stmt_construct(execute_data, stmt, return_value, dbh->def_stmt_ce, &dbh->def_stmt_ctor_args);
1110 					return;
1111 				}
1112 			}
1113 		}
1114 		/* something broke */
1115 		dbh->query_stmt = stmt;
1116 		ZVAL_OBJ(&dbh->query_stmt_zval, Z_OBJ_P(return_value));
1117 		Z_DELREF(stmt->database_object_handle);
1118 		ZVAL_UNDEF(&stmt->database_object_handle);
1119 		PDO_HANDLE_STMT_ERR();
1120 	} else {
1121 		PDO_HANDLE_DBH_ERR();
1122 		zval_ptr_dtor(return_value);
1123 	}
1124 
1125 	RETURN_FALSE;
1126 }
1127 /* }}} */
1128 
1129 /* {{{ quotes string for use in a query. The optional paramtype acts as a hint for drivers that have alternate quoting styles. The default value is PDO_PARAM_STR */
PHP_METHOD(PDO,quote)1130 PHP_METHOD(PDO, quote)
1131 {
1132 	pdo_dbh_t *dbh = Z_PDO_DBH_P(ZEND_THIS);
1133 	char *str;
1134 	size_t str_len;
1135 	zend_long paramtype = PDO_PARAM_STR;
1136 	char *qstr;
1137 	size_t qlen;
1138 
1139 	ZEND_PARSE_PARAMETERS_START(1, 2)
1140 		Z_PARAM_STRING(str, str_len)
1141 		Z_PARAM_OPTIONAL
1142 		Z_PARAM_LONG(paramtype)
1143 	ZEND_PARSE_PARAMETERS_END();
1144 
1145 	PDO_CONSTRUCT_CHECK;
1146 
1147 	PDO_DBH_CLEAR_ERR();
1148 	if (!dbh->methods->quoter) {
1149 		pdo_raise_impl_error(dbh, NULL, "IM001", "driver does not support quoting");
1150 		RETURN_FALSE;
1151 	}
1152 
1153 	if (dbh->methods->quoter(dbh, str, str_len, &qstr, &qlen, paramtype)) {
1154 		RETVAL_STRINGL(qstr, qlen);
1155 		efree(qstr);
1156 		return;
1157 	}
1158 	PDO_HANDLE_DBH_ERR();
1159 	RETURN_FALSE;
1160 }
1161 /* }}} */
1162 
1163 /* {{{ Return array of available PDO drivers */
PHP_METHOD(PDO,getAvailableDrivers)1164 PHP_METHOD(PDO, getAvailableDrivers)
1165 {
1166 	pdo_driver_t *pdriver;
1167 
1168 	ZEND_PARSE_PARAMETERS_NONE();
1169 
1170 	array_init(return_value);
1171 
1172 	ZEND_HASH_FOREACH_PTR(&pdo_driver_hash, pdriver) {
1173 		add_next_index_stringl(return_value, (char*)pdriver->driver_name, pdriver->driver_name_len);
1174 	} ZEND_HASH_FOREACH_END();
1175 }
1176 /* }}} */
1177 
cls_method_dtor(zval * el)1178 static void cls_method_dtor(zval *el) /* {{{ */ {
1179 	zend_function *func = (zend_function*)Z_PTR_P(el);
1180 	if (func->common.function_name) {
1181 		zend_string_release_ex(func->common.function_name, 0);
1182 	}
1183 	efree(func);
1184 }
1185 /* }}} */
1186 
cls_method_pdtor(zval * el)1187 static void cls_method_pdtor(zval *el) /* {{{ */ {
1188 	zend_function *func = (zend_function*)Z_PTR_P(el);
1189 	if (func->common.function_name) {
1190 		zend_string_release_ex(func->common.function_name, 1);
1191 	}
1192 	pefree(func, 1);
1193 }
1194 /* }}} */
1195 
1196 /* {{{ overloaded object handlers for PDO class */
pdo_hash_methods(pdo_dbh_object_t * dbh_obj,int kind)1197 int pdo_hash_methods(pdo_dbh_object_t *dbh_obj, int kind)
1198 {
1199 	const zend_function_entry *funcs;
1200 	zend_internal_function func;
1201 	size_t namelen;
1202 	char *lc_name;
1203 	pdo_dbh_t *dbh = dbh_obj->inner;
1204 
1205 	if (!dbh || !dbh->methods || !dbh->methods->get_driver_methods) {
1206 		return 0;
1207 	}
1208 	funcs =	dbh->methods->get_driver_methods(dbh, kind);
1209 	if (!funcs) {
1210 		return 0;
1211 	}
1212 
1213 	dbh->cls_methods[kind] = pemalloc(sizeof(HashTable), dbh->is_persistent);
1214 	zend_hash_init(dbh->cls_methods[kind], 8, NULL,
1215 			dbh->is_persistent? cls_method_pdtor : cls_method_dtor, dbh->is_persistent);
1216 
1217 	memset(&func, 0, sizeof(func));
1218 
1219 	while (funcs->fname) {
1220 		func.type = ZEND_INTERNAL_FUNCTION;
1221 		func.handler = funcs->handler;
1222 		func.function_name = zend_string_init(funcs->fname, strlen(funcs->fname), dbh->is_persistent);
1223 		func.scope = dbh_obj->std.ce;
1224 		func.prototype = NULL;
1225 		if (funcs->flags) {
1226 			func.fn_flags = funcs->flags | ZEND_ACC_NEVER_CACHE;
1227 		} else {
1228 			func.fn_flags = ZEND_ACC_PUBLIC | ZEND_ACC_NEVER_CACHE;
1229 		}
1230 		if (funcs->arg_info) {
1231 			zend_internal_function_info *info = (zend_internal_function_info*)funcs->arg_info;
1232 
1233 			func.arg_info = (zend_internal_arg_info*)funcs->arg_info + 1;
1234 			func.num_args = funcs->num_args;
1235 			if (info->required_num_args == (uint32_t)-1) {
1236 				func.required_num_args = funcs->num_args;
1237 			} else {
1238 				func.required_num_args = info->required_num_args;
1239 			}
1240 			if (ZEND_ARG_SEND_MODE(info)) {
1241 				func.fn_flags |= ZEND_ACC_RETURN_REFERENCE;
1242 			}
1243 			if (ZEND_ARG_IS_VARIADIC(&funcs->arg_info[funcs->num_args])) {
1244 				func.fn_flags |= ZEND_ACC_VARIADIC;
1245 				/* Don't count the variadic argument */
1246 				func.num_args--;
1247 			}
1248 		} else {
1249 			func.arg_info = NULL;
1250 			func.num_args = 0;
1251 			func.required_num_args = 0;
1252 		}
1253 		zend_set_function_arg_flags((zend_function*)&func);
1254 		namelen = strlen(funcs->fname);
1255 		lc_name = emalloc(namelen+1);
1256 		zend_str_tolower_copy(lc_name, funcs->fname, namelen);
1257 		zend_hash_str_add_mem(dbh->cls_methods[kind], lc_name, namelen, &func, sizeof(func));
1258 		efree(lc_name);
1259 		funcs++;
1260 	}
1261 
1262 	return 1;
1263 }
1264 
dbh_method_get(zend_object ** object,zend_string * method_name,const zval * key)1265 static zend_function *dbh_method_get(zend_object **object, zend_string *method_name, const zval *key)
1266 {
1267 	zend_function *fbc = NULL;
1268 	pdo_dbh_object_t *dbh_obj = php_pdo_dbh_fetch_object(*object);
1269 	zend_string *lc_method_name;
1270 
1271 	if ((fbc = zend_std_get_method(object, method_name, key)) == NULL) {
1272 		/* not a pre-defined method, nor a user-defined method; check
1273 		 * the driver specific methods */
1274 		if (!dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH]) {
1275 			if (!pdo_hash_methods(dbh_obj,
1276 				PDO_DBH_DRIVER_METHOD_KIND_DBH)
1277 				|| !dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH]) {
1278 				goto out;
1279 			}
1280 		}
1281 
1282 		lc_method_name = zend_string_tolower(method_name);
1283 		fbc = zend_hash_find_ptr(dbh_obj->inner->cls_methods[PDO_DBH_DRIVER_METHOD_KIND_DBH], lc_method_name);
1284 		zend_string_release_ex(lc_method_name, 0);
1285 	}
1286 
1287 out:
1288 	return fbc;
1289 }
1290 
dbh_compare(zval * object1,zval * object2)1291 static int dbh_compare(zval *object1, zval *object2)
1292 {
1293 	return ZEND_UNCOMPARABLE;
1294 }
1295 
dbh_get_gc(zend_object * object,zval ** gc_data,int * gc_count)1296 static HashTable *dbh_get_gc(zend_object *object, zval **gc_data, int *gc_count)
1297 {
1298 	pdo_dbh_t *dbh = php_pdo_dbh_fetch_inner(object);
1299 	*gc_data = &dbh->def_stmt_ctor_args;
1300 	*gc_count = 1;
1301 	return zend_std_get_properties(object);
1302 }
1303 
1304 static zend_object_handlers pdo_dbh_object_handlers;
1305 static void pdo_dbh_free_storage(zend_object *std);
1306 
pdo_dbh_init(void)1307 void pdo_dbh_init(void)
1308 {
1309 	zend_class_entry ce;
1310 
1311 	INIT_CLASS_ENTRY(ce, "PDO", class_PDO_methods);
1312 	pdo_dbh_ce = zend_register_internal_class(&ce);
1313 	pdo_dbh_ce->create_object = pdo_dbh_new;
1314 	pdo_dbh_ce->serialize = zend_class_serialize_deny;
1315 	pdo_dbh_ce->unserialize = zend_class_unserialize_deny;
1316 
1317 	memcpy(&pdo_dbh_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
1318 	pdo_dbh_object_handlers.offset = XtOffsetOf(pdo_dbh_object_t, std);
1319 	pdo_dbh_object_handlers.dtor_obj = zend_objects_destroy_object;
1320 	pdo_dbh_object_handlers.free_obj = pdo_dbh_free_storage;
1321 	pdo_dbh_object_handlers.clone_obj = NULL;
1322 	pdo_dbh_object_handlers.get_method = dbh_method_get;
1323 	pdo_dbh_object_handlers.compare = dbh_compare;
1324 	pdo_dbh_object_handlers.get_gc = dbh_get_gc;
1325 
1326 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_BOOL", (zend_long)PDO_PARAM_BOOL);
1327 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_NULL", (zend_long)PDO_PARAM_NULL);
1328 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_INT",  (zend_long)PDO_PARAM_INT);
1329 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR",  (zend_long)PDO_PARAM_STR);
1330 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_LOB",  (zend_long)PDO_PARAM_LOB);
1331 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_STMT", (zend_long)PDO_PARAM_STMT);
1332 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_INPUT_OUTPUT", (zend_long)PDO_PARAM_INPUT_OUTPUT);
1333 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR_NATL", (zend_long)PDO_PARAM_STR_NATL);
1334 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_STR_CHAR", (zend_long)PDO_PARAM_STR_CHAR);
1335 
1336 
1337 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_ALLOC",		(zend_long)PDO_PARAM_EVT_ALLOC);
1338 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FREE",			(zend_long)PDO_PARAM_EVT_FREE);
1339 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_EXEC_PRE",		(zend_long)PDO_PARAM_EVT_EXEC_PRE);
1340 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_EXEC_POST",	(zend_long)PDO_PARAM_EVT_EXEC_POST);
1341 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FETCH_PRE",	(zend_long)PDO_PARAM_EVT_FETCH_PRE);
1342 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_FETCH_POST",	(zend_long)PDO_PARAM_EVT_FETCH_POST);
1343 	REGISTER_PDO_CLASS_CONST_LONG("PARAM_EVT_NORMALIZE",	(zend_long)PDO_PARAM_EVT_NORMALIZE);
1344 
1345 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_DEFAULT", (zend_long)PDO_FETCH_USE_DEFAULT);
1346 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_LAZY", (zend_long)PDO_FETCH_LAZY);
1347 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ASSOC", (zend_long)PDO_FETCH_ASSOC);
1348 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_NUM",  (zend_long)PDO_FETCH_NUM);
1349 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_BOTH", (zend_long)PDO_FETCH_BOTH);
1350 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_OBJ",  (zend_long)PDO_FETCH_OBJ);
1351 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_BOUND", (zend_long)PDO_FETCH_BOUND);
1352 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_COLUMN", (zend_long)PDO_FETCH_COLUMN);
1353 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_CLASS", (zend_long)PDO_FETCH_CLASS);
1354 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_INTO", (zend_long)PDO_FETCH_INTO);
1355 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_FUNC", (zend_long)PDO_FETCH_FUNC);
1356 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_GROUP", (zend_long)PDO_FETCH_GROUP);
1357 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_UNIQUE", (zend_long)PDO_FETCH_UNIQUE);
1358 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_KEY_PAIR", (zend_long)PDO_FETCH_KEY_PAIR);
1359 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_CLASSTYPE", (zend_long)PDO_FETCH_CLASSTYPE);
1360 
1361 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_SERIALIZE",(zend_long)PDO_FETCH_SERIALIZE);
1362 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_PROPS_LATE", (zend_long)PDO_FETCH_PROPS_LATE);
1363 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_NAMED", (zend_long)PDO_FETCH_NAMED);
1364 
1365 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_AUTOCOMMIT",	(zend_long)PDO_ATTR_AUTOCOMMIT);
1366 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_PREFETCH",		(zend_long)PDO_ATTR_PREFETCH);
1367 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_TIMEOUT", 		(zend_long)PDO_ATTR_TIMEOUT);
1368 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_ERRMODE", 		(zend_long)PDO_ATTR_ERRMODE);
1369 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_SERVER_VERSION",	(zend_long)PDO_ATTR_SERVER_VERSION);
1370 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CLIENT_VERSION", 	(zend_long)PDO_ATTR_CLIENT_VERSION);
1371 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_SERVER_INFO",		(zend_long)PDO_ATTR_SERVER_INFO);
1372 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CONNECTION_STATUS", 	(zend_long)PDO_ATTR_CONNECTION_STATUS);
1373 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CASE",		 	(zend_long)PDO_ATTR_CASE);
1374 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CURSOR_NAME", 	(zend_long)PDO_ATTR_CURSOR_NAME);
1375 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_CURSOR",	 	(zend_long)PDO_ATTR_CURSOR);
1376 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_ORACLE_NULLS",	(zend_long)PDO_ATTR_ORACLE_NULLS);
1377 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_PERSISTENT",	(zend_long)PDO_ATTR_PERSISTENT);
1378 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_STATEMENT_CLASS",		(zend_long)PDO_ATTR_STATEMENT_CLASS);
1379 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_FETCH_TABLE_NAMES",		(zend_long)PDO_ATTR_FETCH_TABLE_NAMES);
1380 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_FETCH_CATALOG_NAMES",		(zend_long)PDO_ATTR_FETCH_CATALOG_NAMES);
1381 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_DRIVER_NAME",		(zend_long)PDO_ATTR_DRIVER_NAME);
1382 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_STRINGIFY_FETCHES", (zend_long)PDO_ATTR_STRINGIFY_FETCHES);
1383 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_MAX_COLUMN_LEN", (zend_long)PDO_ATTR_MAX_COLUMN_LEN);
1384 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_EMULATE_PREPARES", (zend_long)PDO_ATTR_EMULATE_PREPARES);
1385 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_DEFAULT_FETCH_MODE", (zend_long)PDO_ATTR_DEFAULT_FETCH_MODE);
1386 	REGISTER_PDO_CLASS_CONST_LONG("ATTR_DEFAULT_STR_PARAM", (zend_long)PDO_ATTR_DEFAULT_STR_PARAM);
1387 
1388 	REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_SILENT",	(zend_long)PDO_ERRMODE_SILENT);
1389 	REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_WARNING",	(zend_long)PDO_ERRMODE_WARNING);
1390 	REGISTER_PDO_CLASS_CONST_LONG("ERRMODE_EXCEPTION",	(zend_long)PDO_ERRMODE_EXCEPTION);
1391 
1392 	REGISTER_PDO_CLASS_CONST_LONG("CASE_NATURAL",	(zend_long)PDO_CASE_NATURAL);
1393 	REGISTER_PDO_CLASS_CONST_LONG("CASE_LOWER",	(zend_long)PDO_CASE_LOWER);
1394 	REGISTER_PDO_CLASS_CONST_LONG("CASE_UPPER",	(zend_long)PDO_CASE_UPPER);
1395 
1396 	REGISTER_PDO_CLASS_CONST_LONG("NULL_NATURAL",	(zend_long)PDO_NULL_NATURAL);
1397 	REGISTER_PDO_CLASS_CONST_LONG("NULL_EMPTY_STRING",	(zend_long)PDO_NULL_EMPTY_STRING);
1398 	REGISTER_PDO_CLASS_CONST_LONG("NULL_TO_STRING",	(zend_long)PDO_NULL_TO_STRING);
1399 
1400 	REGISTER_PDO_CLASS_CONST_STRING("ERR_NONE",	PDO_ERR_NONE);
1401 
1402 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_NEXT", (zend_long)PDO_FETCH_ORI_NEXT);
1403 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_PRIOR", (zend_long)PDO_FETCH_ORI_PRIOR);
1404 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_FIRST", (zend_long)PDO_FETCH_ORI_FIRST);
1405 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_LAST", (zend_long)PDO_FETCH_ORI_LAST);
1406 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_ABS", (zend_long)PDO_FETCH_ORI_ABS);
1407 	REGISTER_PDO_CLASS_CONST_LONG("FETCH_ORI_REL", (zend_long)PDO_FETCH_ORI_REL);
1408 
1409 	REGISTER_PDO_CLASS_CONST_LONG("CURSOR_FWDONLY", (zend_long)PDO_CURSOR_FWDONLY);
1410 	REGISTER_PDO_CLASS_CONST_LONG("CURSOR_SCROLL", (zend_long)PDO_CURSOR_SCROLL);
1411 }
1412 
dbh_free(pdo_dbh_t * dbh,zend_bool free_persistent)1413 static void dbh_free(pdo_dbh_t *dbh, zend_bool free_persistent)
1414 {
1415 	int i;
1416 
1417 	if (dbh->query_stmt) {
1418 		zval_ptr_dtor(&dbh->query_stmt_zval);
1419 		dbh->query_stmt = NULL;
1420 	}
1421 
1422 	if (dbh->is_persistent) {
1423 #if ZEND_DEBUG
1424 		ZEND_ASSERT(!free_persistent || (dbh->refcount == 1));
1425 #endif
1426 		if (!free_persistent && (--dbh->refcount)) {
1427 			return;
1428 		}
1429 	}
1430 
1431 	if (dbh->methods) {
1432 		dbh->methods->closer(dbh);
1433 	}
1434 
1435 	if (dbh->data_source) {
1436 		pefree((char *)dbh->data_source, dbh->is_persistent);
1437 	}
1438 	if (dbh->username) {
1439 		pefree(dbh->username, dbh->is_persistent);
1440 	}
1441 	if (dbh->password) {
1442 		pefree(dbh->password, dbh->is_persistent);
1443 	}
1444 
1445 	if (dbh->persistent_id) {
1446 		pefree((char *)dbh->persistent_id, dbh->is_persistent);
1447 	}
1448 
1449 	if (!Z_ISUNDEF(dbh->def_stmt_ctor_args)) {
1450 		zval_ptr_dtor(&dbh->def_stmt_ctor_args);
1451 	}
1452 
1453 	for (i = 0; i < PDO_DBH_DRIVER_METHOD_KIND__MAX; i++) {
1454 		if (dbh->cls_methods[i]) {
1455 			zend_hash_destroy(dbh->cls_methods[i]);
1456 			pefree(dbh->cls_methods[i], dbh->is_persistent);
1457 		}
1458 	}
1459 
1460 	pefree(dbh, dbh->is_persistent);
1461 }
1462 
pdo_dbh_free_storage(zend_object * std)1463 static void pdo_dbh_free_storage(zend_object *std)
1464 {
1465 	pdo_dbh_t *dbh = php_pdo_dbh_fetch_inner(std);
1466 	if (dbh->driver_data && dbh->methods && dbh->methods->rollback && pdo_is_in_transaction(dbh)) {
1467 		dbh->methods->rollback(dbh);
1468 		dbh->in_txn = 0;
1469 	}
1470 
1471 	if (dbh->is_persistent && dbh->methods && dbh->methods->persistent_shutdown) {
1472 		dbh->methods->persistent_shutdown(dbh);
1473 	}
1474 	zend_object_std_dtor(std);
1475 	dbh_free(dbh, 0);
1476 }
1477 
pdo_dbh_new(zend_class_entry * ce)1478 zend_object *pdo_dbh_new(zend_class_entry *ce)
1479 {
1480 	pdo_dbh_object_t *dbh;
1481 
1482 	dbh = zend_object_alloc(sizeof(pdo_dbh_object_t), ce);
1483 	zend_object_std_init(&dbh->std, ce);
1484 	object_properties_init(&dbh->std, ce);
1485 	rebuild_object_properties(&dbh->std);
1486 	dbh->inner = ecalloc(1, sizeof(pdo_dbh_t));
1487 	dbh->inner->def_stmt_ce = pdo_dbstmt_ce;
1488 
1489 	dbh->std.handlers = &pdo_dbh_object_handlers;
1490 
1491 	return &dbh->std;
1492 }
1493 
1494 /* }}} */
1495 
ZEND_RSRC_DTOR_FUNC(php_pdo_pdbh_dtor)1496 ZEND_RSRC_DTOR_FUNC(php_pdo_pdbh_dtor) /* {{{ */
1497 {
1498 	if (res->ptr) {
1499 		pdo_dbh_t *dbh = (pdo_dbh_t*)res->ptr;
1500 		dbh_free(dbh, 1);
1501 		res->ptr = NULL;
1502 	}
1503 }
1504 /* }}} */
1505