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