1/*
2  +----------------------------------------------------------------------+
3  | PHP Version 7                                                        |
4  +----------------------------------------------------------------------+
5  | Copyright (c) 1997-2018 The PHP Group                                |
6  +----------------------------------------------------------------------+
7  | This source file is subject to version 3.01 of the PHP license,      |
8  | that is bundled with this package in the file LICENSE, and is        |
9  | available through the world-wide-web at the following url:           |
10  | http://www.php.net/license/3_01.txt                                  |
11  | If you did not receive a copy of the PHP license and are unable to   |
12  | obtain it through the world-wide-web, please send a note to          |
13  | license@php.net so we can mail you a copy immediately.               |
14  +----------------------------------------------------------------------+
15  | Authors: Georg Richter <georg@php.net>                               |
16  |          Andrey Hristov <andrey@php.net>                             |
17  |          Ulf Wendel <uw@php.net>                                     |
18  +----------------------------------------------------------------------+
19*/
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <signal.h>
26
27#include "php.h"
28#include "php_ini.h"
29#include "ext/standard/info.h"
30#include "ext/standard/php_string.h"
31#include "php_mysqli.h"
32#include "php_mysqli_structs.h"
33#include "mysqli_priv.h"
34#include "zend_exceptions.h"
35#include "ext/spl/spl_exceptions.h"
36#include "zend_interfaces.h"
37
38ZEND_DECLARE_MODULE_GLOBALS(mysqli)
39static PHP_GINIT_FUNCTION(mysqli);
40
41#define MYSQLI_ADD_PROPERTIES(a, b) \
42{ \
43	int i = 0; \
44	while (b[i].pname != NULL) { \
45		mysqli_add_property((a), (b)[i].pname, (b)[i].pname_length, \
46							(mysqli_read_t)(b)[i].r_func, (mysqli_write_t)(b)[i].w_func); \
47		i++; \
48	} \
49}
50
51static HashTable classes;
52static zend_object_handlers mysqli_object_handlers;
53static zend_object_handlers mysqli_object_driver_handlers;
54static zend_object_handlers mysqli_object_link_handlers;
55static zend_object_handlers mysqli_object_result_handlers;
56static zend_object_handlers mysqli_object_stmt_handlers;
57static zend_object_handlers mysqli_object_warning_handlers;
58static HashTable mysqli_driver_properties;
59static HashTable mysqli_link_properties;
60static HashTable mysqli_result_properties;
61static HashTable mysqli_stmt_properties;
62static HashTable mysqli_warning_properties;
63
64zend_class_entry *mysqli_link_class_entry;
65zend_class_entry *mysqli_stmt_class_entry;
66zend_class_entry *mysqli_result_class_entry;
67zend_class_entry *mysqli_driver_class_entry;
68zend_class_entry *mysqli_warning_class_entry;
69zend_class_entry *mysqli_exception_class_entry;
70
71
72typedef zval *(*mysqli_read_t)(mysqli_object *obj, zval *rv);
73typedef int (*mysqli_write_t)(mysqli_object *obj, zval *newval);
74
75typedef struct _mysqli_prop_handler {
76	zend_string *name;
77	mysqli_read_t read_func;
78	mysqli_write_t write_func;
79} mysqli_prop_handler;
80
81static int le_pmysqli;
82
83static void free_prop_handler(zval *el) {
84	pefree(Z_PTR_P(el), 1);
85}
86
87/* Destructor for mysqli entries in free_links/used_links */
88void php_mysqli_dtor_p_elements(void *data)
89{
90	MYSQL *mysql = (MYSQL *)data;
91	mysqli_close(mysql, MYSQLI_CLOSE_IMPLICIT);
92}
93
94
95ZEND_RSRC_DTOR_FUNC(php_mysqli_dtor)
96{
97	if (res->ptr) {
98		mysqli_plist_entry *plist = (mysqli_plist_entry *)res->ptr;
99		zend_ptr_stack_clean(&plist->free_links, php_mysqli_dtor_p_elements, 0);
100		zend_ptr_stack_destroy(&plist->free_links);
101		free(plist);
102	}
103}
104
105
106int php_le_pmysqli(void)
107{
108	return le_pmysqli;
109}
110
111#ifndef MYSQLI_USE_MYSQLND
112/* {{{ php_free_stmt_bind_buffer */
113void php_free_stmt_bind_buffer(BIND_BUFFER bbuf, int type)
114{
115	unsigned int i;
116
117	if (!bbuf.var_cnt) {
118		return;
119	}
120
121	for (i=0; i < bbuf.var_cnt; i++) {
122
123		/* free temporary bind buffer */
124		if (type == FETCH_RESULT && bbuf.buf[i].val) {
125			efree(bbuf.buf[i].val);
126		}
127
128		zval_ptr_dtor(&bbuf.vars[i]);
129	}
130
131	if (bbuf.vars) {
132		efree(bbuf.vars);
133	}
134
135	/*
136	  Don't free bbuf.is_null for FETCH_RESULT since we have allocated
137	  is_null and buf in one block so we free only buf, which is the beginning
138	  of the block. When FETCH_SIMPLE then buf wasn't allocated together with
139	  buf and we have to free it.
140	*/
141	if (type == FETCH_RESULT) {
142		efree(bbuf.buf);
143	} else if (type == FETCH_SIMPLE){
144		efree(bbuf.is_null);
145	}
146
147	bbuf.var_cnt = 0;
148}
149/* }}} */
150#endif
151
152/* {{{ php_clear_stmt_bind */
153void php_clear_stmt_bind(MY_STMT *stmt)
154{
155	if (stmt->stmt) {
156		if (mysqli_stmt_close(stmt->stmt, TRUE)) {
157			php_error_docref(NULL, E_WARNING, "Error occurred while closing statement");
158			return;
159		}
160	}
161
162	/*
163	  mysqlnd keeps track of the binding and has freed its
164	  structures in stmt_close() above
165	*/
166#ifndef MYSQLI_USE_MYSQLND
167	/* Clean param bind */
168	php_free_stmt_bind_buffer(stmt->param, FETCH_SIMPLE);
169	/* Clean output bind */
170	php_free_stmt_bind_buffer(stmt->result, FETCH_RESULT);
171
172	if (!Z_ISUNDEF(stmt->link_handle)) {
173		zval_ptr_dtor(&stmt->link_handle);
174	}
175#endif
176	if (stmt->query) {
177		efree(stmt->query);
178	}
179	efree(stmt);
180}
181/* }}} */
182
183/* {{{ php_clear_mysql */
184void php_clear_mysql(MY_MYSQL *mysql) {
185	if (mysql->hash_key) {
186		zend_string_release_ex(mysql->hash_key, 0);
187		mysql->hash_key = NULL;
188	}
189	if (!Z_ISUNDEF(mysql->li_read)) {
190		zval_ptr_dtor(&(mysql->li_read));
191		ZVAL_UNDEF(&mysql->li_read);
192	}
193}
194/* }}} */
195
196/* {{{ mysqli_objects_free_storage
197 */
198static void mysqli_objects_free_storage(zend_object	*object)
199{
200	mysqli_object 	*intern = php_mysqli_fetch_object(object);
201	MYSQLI_RESOURCE	*my_res = (MYSQLI_RESOURCE *)intern->ptr;
202
203	my_efree(my_res);
204	zend_object_std_dtor(&intern->zo);
205}
206/* }}} */
207
208/* mysqli_link_free_storage partly doubles the work of PHP_FUNCTION(mysqli_close) */
209
210/* {{{ mysqli_link_free_storage
211 */
212static void mysqli_link_free_storage(zend_object *object)
213{
214	mysqli_object 	*intern = php_mysqli_fetch_object(object);
215	MYSQLI_RESOURCE	*my_res = (MYSQLI_RESOURCE *)intern->ptr;
216
217	if (my_res && my_res->ptr) {
218		MY_MYSQL *mysql = (MY_MYSQL *)my_res->ptr;
219		if (mysql->mysql) {
220			php_mysqli_close(mysql, MYSQLI_CLOSE_EXPLICIT, my_res->status);
221		}
222		php_clear_mysql(mysql);
223		efree(mysql);
224		my_res->status = MYSQLI_STATUS_UNKNOWN;
225	}
226	mysqli_objects_free_storage(object);
227}
228/* }}} */
229
230/* {{{ mysql_driver_free_storage */
231static void mysqli_driver_free_storage(zend_object *object)
232{
233	mysqli_objects_free_storage(object);
234}
235/* }}} */
236
237/* {{{ mysqli_stmt_free_storage
238 */
239static void mysqli_stmt_free_storage(zend_object *object)
240{
241	mysqli_object 	*intern = php_mysqli_fetch_object(object);
242	MYSQLI_RESOURCE	*my_res = (MYSQLI_RESOURCE *)intern->ptr;
243
244	if (my_res && my_res->ptr) {
245		MY_STMT *stmt = (MY_STMT *)my_res->ptr;
246		php_clear_stmt_bind(stmt);
247	}
248	mysqli_objects_free_storage(object);
249}
250/* }}} */
251
252/* {{{ mysqli_result_free_storage
253 */
254static void mysqli_result_free_storage(zend_object *object)
255{
256	mysqli_object 	*intern = php_mysqli_fetch_object(object);
257	MYSQLI_RESOURCE	*my_res = (MYSQLI_RESOURCE *)intern->ptr;
258
259	if (my_res && my_res->ptr) {
260		mysql_free_result(my_res->ptr);
261	}
262	mysqli_objects_free_storage(object);
263}
264/* }}} */
265
266/* {{{ mysqli_warning_free_storage
267 */
268static void mysqli_warning_free_storage(zend_object *object)
269{
270	mysqli_object 	*intern = php_mysqli_fetch_object(object);
271	MYSQLI_RESOURCE	*my_res = (MYSQLI_RESOURCE *)intern->ptr;
272
273	if (my_res && my_res->ptr) {
274		php_clear_warnings((MYSQLI_WARNING *)my_res->info);
275		my_res->ptr = NULL;
276	}
277	mysqli_objects_free_storage(object);
278}
279/* }}} */
280
281/* {{{ mysqli_read_na */
282static zval *mysqli_read_na(mysqli_object *obj, zval *retval)
283{
284	zend_throw_error(NULL, "Cannot read property");
285	return NULL;
286}
287/* }}} */
288
289/* {{{ mysqli_write_na */
290static int mysqli_write_na(mysqli_object *obj, zval *newval)
291{
292	zend_throw_error(NULL, "Cannot write property");
293	return FAILURE;
294}
295/* }}} */
296
297/* {{{ mysqli_read_property */
298zval *mysqli_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv)
299{
300	zval tmp_member;
301	zval *retval;
302	mysqli_object *obj;
303	mysqli_prop_handler *hnd = NULL;
304
305	obj = Z_MYSQLI_P(object);
306
307	if (Z_TYPE_P(member) != IS_STRING) {
308		ZVAL_STR(&tmp_member, zval_get_string_func(member));
309		member = &tmp_member;
310	}
311
312	if (obj->prop_handler != NULL) {
313		hnd = zend_hash_find_ptr(obj->prop_handler, Z_STR_P(member));
314	}
315
316	if (hnd) {
317		retval = hnd->read_func(obj, rv);
318		if (retval == NULL) {
319			retval = &EG(uninitialized_zval);
320		}
321	} else {
322		retval = zend_std_read_property(object, member, type, cache_slot, rv);
323	}
324
325	if (member == &tmp_member) {
326		zval_ptr_dtor_str(&tmp_member);
327	}
328
329	return retval;
330}
331/* }}} */
332
333/* {{{ mysqli_write_property */
334void mysqli_write_property(zval *object, zval *member, zval *value, void **cache_slot)
335{
336	zval tmp_member;
337	mysqli_object *obj;
338	mysqli_prop_handler *hnd = NULL;
339
340	if (Z_TYPE_P(member) != IS_STRING) {
341		ZVAL_STR(&tmp_member, zval_get_string_func(member));
342		member = &tmp_member;
343	}
344
345	obj = Z_MYSQLI_P(object);
346
347	if (obj->prop_handler != NULL) {
348		hnd = zend_hash_find_ptr(obj->prop_handler, Z_STR_P(member));
349	}
350
351	if (hnd) {
352		hnd->write_func(obj, value);
353	} else {
354		zend_std_write_property(object, member, value, cache_slot);
355	}
356
357	if (member == &tmp_member) {
358		zval_ptr_dtor_str(&tmp_member);
359	}
360}
361/* }}} */
362
363/* {{{ void mysqli_add_property(HashTable *h, char *pname, mysqli_read_t r_func, mysqli_write_t w_func) */
364void mysqli_add_property(HashTable *h, const char *pname, size_t pname_len, mysqli_read_t r_func, mysqli_write_t w_func) {
365	mysqli_prop_handler	p;
366
367	p.name = zend_string_init_interned(pname, pname_len, 1);
368	p.read_func = (r_func) ? r_func : mysqli_read_na;
369	p.write_func = (w_func) ? w_func : mysqli_write_na;
370	zend_hash_add_mem(h, p.name, &p, sizeof(mysqli_prop_handler));
371	zend_string_release_ex(p.name, 1);
372}
373/* }}} */
374
375static int mysqli_object_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot) /* {{{ */
376{
377	mysqli_object *obj = Z_MYSQLI_P(object);
378	mysqli_prop_handler	*p;
379	int ret = 0;
380
381	if ((p = zend_hash_find_ptr(obj->prop_handler, Z_STR_P(member))) != NULL) {
382		switch (has_set_exists) {
383			case ZEND_PROPERTY_EXISTS:
384				ret = 1;
385				break;
386			case ZEND_PROPERTY_NOT_EMPTY: {
387				zval rv;
388				zval *value = mysqli_read_property(object, member, BP_VAR_IS, cache_slot, &rv);
389				if (value != &EG(uninitialized_zval)) {
390					convert_to_boolean(value);
391					ret = Z_TYPE_P(value) == IS_TRUE ? 1 : 0;
392				}
393				break;
394			}
395			case ZEND_PROPERTY_ISSET: {
396				zval rv;
397				zval *value = mysqli_read_property(object, member, BP_VAR_IS, cache_slot, &rv);
398				if (value != &EG(uninitialized_zval)) {
399					ret = Z_TYPE_P(value) != IS_NULL? 1 : 0;
400					zval_ptr_dtor(value);
401				}
402				break;
403			}
404			default:
405				php_error_docref(NULL, E_WARNING, "Invalid value for has_set_exists");
406		}
407	} else {
408		ret = zend_std_has_property(object, member, has_set_exists, cache_slot);
409	}
410
411	return ret;
412} /* }}} */
413
414HashTable *mysqli_object_get_debug_info(zval *object, int *is_temp)
415{
416	mysqli_object *obj = Z_MYSQLI_P(object);
417	HashTable *retval, *props = obj->prop_handler;
418	mysqli_prop_handler *entry;
419
420	retval = zend_new_array(zend_hash_num_elements(props) + 1);
421
422	ZEND_HASH_FOREACH_PTR(props, entry) {
423		zval rv, member;
424		zval *value;
425		ZVAL_STR(&member, entry->name);
426		value = mysqli_read_property(object, &member, BP_VAR_IS, 0, &rv);
427		if (value != &EG(uninitialized_zval)) {
428			zend_hash_add(retval, Z_STR(member), value);
429		}
430	} ZEND_HASH_FOREACH_END();
431
432	*is_temp = 1;
433	return retval;
434}
435
436/* {{{ mysqli_objects_new
437 */
438PHP_MYSQLI_EXPORT(zend_object *) mysqli_objects_new(zend_class_entry *class_type)
439{
440	mysqli_object *intern;
441	zend_class_entry *mysqli_base_class;
442	zend_object_handlers *handlers;
443
444	intern = zend_object_alloc(sizeof(mysqli_object), class_type);
445
446	mysqli_base_class = class_type;
447	while (mysqli_base_class->type != ZEND_INTERNAL_CLASS &&
448		   mysqli_base_class->parent != NULL) {
449		mysqli_base_class = mysqli_base_class->parent;
450	}
451	intern->prop_handler = zend_hash_find_ptr(&classes, mysqli_base_class->name);
452
453	zend_object_std_init(&intern->zo, class_type);
454	object_properties_init(&intern->zo, class_type);
455
456	/* link object */
457	if (instanceof_function(class_type, mysqli_link_class_entry)) {
458		handlers = &mysqli_object_link_handlers;
459	} else if (instanceof_function(class_type, mysqli_driver_class_entry)) { /* driver object */
460		handlers = &mysqli_object_driver_handlers;
461	} else if (instanceof_function(class_type, mysqli_stmt_class_entry)) { /* stmt object */
462		handlers = &mysqli_object_stmt_handlers;
463	} else if (instanceof_function(class_type, mysqli_result_class_entry)) { /* result object */
464		handlers = &mysqli_object_result_handlers;
465	} else if (instanceof_function(class_type, mysqli_warning_class_entry)) { /* warning object */
466		handlers = &mysqli_object_warning_handlers;
467	} else {
468		handlers = &mysqli_object_handlers;
469	}
470
471	intern->zo.handlers = handlers;
472
473	return &intern->zo;
474}
475/* }}} */
476
477#ifdef MYSQLI_USE_MYSQLND
478#include "ext/mysqlnd/mysqlnd_reverse_api.h"
479static MYSQLND *mysqli_convert_zv_to_mysqlnd(zval * zv)
480{
481	if (Z_TYPE_P(zv) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zv), mysqli_link_class_entry)) {
482		MY_MYSQL *mysql;
483		MYSQLI_RESOURCE  *my_res;
484		mysqli_object *intern = Z_MYSQLI_P(zv);
485		if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
486			/* We know that we have a mysqli object, so this failure should be emitted */
487			php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));
488			return NULL;
489		}
490		mysql = (MY_MYSQL *)(my_res->ptr);
491		return mysql ? mysql->mysql : NULL;
492	}
493	return NULL;
494}
495
496static const MYSQLND_REVERSE_API mysqli_reverse_api = {
497	&mysqli_module_entry,
498	mysqli_convert_zv_to_mysqlnd
499};
500#endif
501
502/* {{{ PHP_INI_BEGIN
503*/
504PHP_INI_BEGIN()
505	STD_PHP_INI_ENTRY_EX("mysqli.max_links",			"-1",	PHP_INI_SYSTEM,		OnUpdateLong,		max_links,			zend_mysqli_globals,		mysqli_globals, display_link_numbers)
506	STD_PHP_INI_ENTRY_EX("mysqli.max_persistent",		"-1",	PHP_INI_SYSTEM,		OnUpdateLong,		max_persistent,		zend_mysqli_globals,		mysqli_globals,	display_link_numbers)
507	STD_PHP_INI_BOOLEAN("mysqli.allow_persistent",		"1",	PHP_INI_SYSTEM,		OnUpdateLong,		allow_persistent,	zend_mysqli_globals,		mysqli_globals)
508	STD_PHP_INI_BOOLEAN("mysqli.rollback_on_cached_plink",	"0",PHP_INI_SYSTEM,		OnUpdateBool,		rollback_on_cached_plink,	zend_mysqli_globals,		mysqli_globals)
509	STD_PHP_INI_ENTRY("mysqli.default_host",			NULL,	PHP_INI_ALL,		OnUpdateString,		default_host,		zend_mysqli_globals,		mysqli_globals)
510	STD_PHP_INI_ENTRY("mysqli.default_user",			NULL,	PHP_INI_ALL,		OnUpdateString,		default_user,		zend_mysqli_globals,		mysqli_globals)
511	STD_PHP_INI_ENTRY("mysqli.default_pw",				NULL,	PHP_INI_ALL,		OnUpdateString,		default_pw,			zend_mysqli_globals,		mysqli_globals)
512	STD_PHP_INI_ENTRY("mysqli.default_port",			"3306",	PHP_INI_ALL,		OnUpdateLong,		default_port,		zend_mysqli_globals,		mysqli_globals)
513#ifdef PHP_MYSQL_UNIX_SOCK_ADDR
514	STD_PHP_INI_ENTRY("mysqli.default_socket",			MYSQL_UNIX_ADDR,PHP_INI_ALL,OnUpdateStringUnempty,	default_socket,	zend_mysqli_globals,		mysqli_globals)
515#else
516	STD_PHP_INI_ENTRY("mysqli.default_socket",			NULL,	PHP_INI_ALL,		OnUpdateStringUnempty,	default_socket,	zend_mysqli_globals,		mysqli_globals)
517#endif
518	STD_PHP_INI_BOOLEAN("mysqli.reconnect",				"0",	PHP_INI_SYSTEM,		OnUpdateLong,		reconnect,			zend_mysqli_globals,		mysqli_globals)
519	STD_PHP_INI_BOOLEAN("mysqli.allow_local_infile",	"0",	PHP_INI_SYSTEM,		OnUpdateLong,		allow_local_infile,	zend_mysqli_globals,		mysqli_globals)
520PHP_INI_END()
521/* }}} */
522
523/* {{{ PHP_GINIT_FUNCTION
524 */
525static PHP_GINIT_FUNCTION(mysqli)
526{
527#if defined(COMPILE_DL_MYSQLI) && defined(ZTS)
528	ZEND_TSRMLS_CACHE_UPDATE();
529#endif
530	mysqli_globals->num_links = 0;
531	mysqli_globals->num_active_persistent = 0;
532	mysqli_globals->num_inactive_persistent = 0;
533	mysqli_globals->max_links = -1;
534	mysqli_globals->max_persistent = -1;
535	mysqli_globals->allow_persistent = 1;
536	mysqli_globals->default_port = 0;
537	mysqli_globals->default_host = NULL;
538	mysqli_globals->default_user = NULL;
539	mysqli_globals->default_pw = NULL;
540	mysqli_globals->default_socket = NULL;
541	mysqli_globals->reconnect = 0;
542	mysqli_globals->report_mode = 0;
543	mysqli_globals->report_ht = 0;
544	mysqli_globals->allow_local_infile = 0;
545#ifdef HAVE_EMBEDDED_MYSQLI
546	mysqli_globals->embedded = 1;
547#else
548	mysqli_globals->embedded = 0;
549#endif
550	mysqli_globals->rollback_on_cached_plink = FALSE;
551}
552/* }}} */
553
554/* {{{ PHP_MINIT_FUNCTION
555 */
556PHP_MINIT_FUNCTION(mysqli)
557{
558	zend_class_entry *ce,cex;
559
560	REGISTER_INI_ENTRIES();
561#ifndef MYSQLI_USE_MYSQLND
562	if (mysql_server_init(0, NULL, NULL)) {
563		return FAILURE;
564	}
565#endif
566
567	memcpy(&mysqli_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
568	mysqli_object_handlers.offset = XtOffsetOf(mysqli_object, zo);
569	mysqli_object_handlers.free_obj = mysqli_objects_free_storage;
570	mysqli_object_handlers.clone_obj = NULL;
571	mysqli_object_handlers.read_property = mysqli_read_property;
572	mysqli_object_handlers.write_property = mysqli_write_property;
573	mysqli_object_handlers.has_property = mysqli_object_has_property;
574	mysqli_object_handlers.get_debug_info = mysqli_object_get_debug_info;
575	memcpy(&mysqli_object_driver_handlers, &mysqli_object_handlers, sizeof(zend_object_handlers));
576	mysqli_object_driver_handlers.free_obj = mysqli_driver_free_storage;
577	memcpy(&mysqli_object_link_handlers, &mysqli_object_handlers, sizeof(zend_object_handlers));
578	mysqli_object_link_handlers.free_obj = mysqli_link_free_storage;
579	memcpy(&mysqli_object_result_handlers, &mysqli_object_handlers, sizeof(zend_object_handlers));
580	mysqli_object_result_handlers.free_obj = mysqli_result_free_storage;
581	memcpy(&mysqli_object_stmt_handlers, &mysqli_object_handlers, sizeof(zend_object_handlers));
582	mysqli_object_stmt_handlers.free_obj = mysqli_stmt_free_storage;
583	memcpy(&mysqli_object_warning_handlers, &mysqli_object_handlers, sizeof(zend_object_handlers));
584	mysqli_object_warning_handlers.free_obj = mysqli_warning_free_storage;
585
586	zend_hash_init(&classes, 0, NULL, NULL, 1);
587
588	/* persistent connections */
589	le_pmysqli = zend_register_list_destructors_ex(NULL, php_mysqli_dtor,
590		"MySqli persistent connection", module_number);
591
592	INIT_CLASS_ENTRY(cex, "mysqli_sql_exception", mysqli_exception_methods);
593	mysqli_exception_class_entry = zend_register_internal_class_ex(&cex, spl_ce_RuntimeException);
594	mysqli_exception_class_entry->ce_flags |= ZEND_ACC_FINAL;
595	zend_declare_property_long(mysqli_exception_class_entry, "code", sizeof("code")-1, 0, ZEND_ACC_PROTECTED);
596	zend_declare_property_string(mysqli_exception_class_entry, "sqlstate", sizeof("sqlstate")-1, "00000", ZEND_ACC_PROTECTED);
597
598	REGISTER_MYSQLI_CLASS_ENTRY("mysqli_driver", mysqli_driver_class_entry, mysqli_driver_methods);
599	ce = mysqli_driver_class_entry;
600	zend_hash_init(&mysqli_driver_properties, 0, NULL, free_prop_handler, 1);
601	MYSQLI_ADD_PROPERTIES(&mysqli_driver_properties, mysqli_driver_property_entries);
602	zend_declare_property_null(ce, "client_info", 		sizeof("client_info") - 1, ZEND_ACC_PUBLIC);
603	zend_declare_property_null(ce, "client_version", 	sizeof("client_version") - 1, ZEND_ACC_PUBLIC);
604	zend_declare_property_null(ce, "driver_version", 	sizeof("driver_version") - 1, ZEND_ACC_PUBLIC);
605	zend_declare_property_null(ce, "embedded", 			sizeof("embedded") - 1, ZEND_ACC_PUBLIC);
606	zend_declare_property_null(ce, "reconnect",			sizeof("reconnect") - 1, ZEND_ACC_PUBLIC);
607	zend_declare_property_null(ce, "report_mode", 		sizeof("report_mode") - 1, ZEND_ACC_PUBLIC);
608	ce->ce_flags |= ZEND_ACC_FINAL;
609	zend_hash_add_ptr(&classes, ce->name, &mysqli_driver_properties);
610
611	REGISTER_MYSQLI_CLASS_ENTRY("mysqli", mysqli_link_class_entry, mysqli_link_methods);
612	ce = mysqli_link_class_entry;
613	zend_hash_init(&mysqli_link_properties, 0, NULL, free_prop_handler, 1);
614	MYSQLI_ADD_PROPERTIES(&mysqli_link_properties, mysqli_link_property_entries);
615	zend_declare_property_null(ce, "affected_rows",		sizeof("affected_rows") - 1, ZEND_ACC_PUBLIC);
616	zend_declare_property_null(ce, "client_info", 		sizeof("client_info") - 1, ZEND_ACC_PUBLIC);
617	zend_declare_property_null(ce, "client_version", 	sizeof("client_version") - 1, ZEND_ACC_PUBLIC);
618	zend_declare_property_null(ce, "connect_errno",		sizeof("connect_errno") - 1, ZEND_ACC_PUBLIC);
619	zend_declare_property_null(ce, "connect_error",		sizeof("connect_error") - 1, ZEND_ACC_PUBLIC);
620	zend_declare_property_null(ce, "errno",				sizeof("errno") - 1, ZEND_ACC_PUBLIC);
621	zend_declare_property_null(ce, "error",				sizeof("error") - 1, ZEND_ACC_PUBLIC);
622	zend_declare_property_null(ce, "error_list", 		sizeof("error_list") - 1, ZEND_ACC_PUBLIC);
623	zend_declare_property_null(ce, "field_count", 		sizeof("field_count") - 1, ZEND_ACC_PUBLIC);
624	zend_declare_property_null(ce, "host_info", 		sizeof("host_info") - 1, ZEND_ACC_PUBLIC);
625	zend_declare_property_null(ce, "info", 				sizeof("info") - 1, ZEND_ACC_PUBLIC);
626	zend_declare_property_null(ce, "insert_id",			sizeof("insert_id") - 1, ZEND_ACC_PUBLIC);
627	zend_declare_property_null(ce, "server_info", 		sizeof("server_info") - 1, ZEND_ACC_PUBLIC);
628	zend_declare_property_null(ce, "server_version", 	sizeof("server_version") - 1, ZEND_ACC_PUBLIC);
629	zend_declare_property_null(ce, "stat", 				sizeof("stat") - 1, ZEND_ACC_PUBLIC);
630	zend_declare_property_null(ce, "sqlstate", 			sizeof("sqlstate") - 1, ZEND_ACC_PUBLIC);
631	zend_declare_property_null(ce, "protocol_version", 	sizeof("protocol_version") - 1,  ZEND_ACC_PUBLIC);
632	zend_declare_property_null(ce, "thread_id",			sizeof("thread_id") - 1, ZEND_ACC_PUBLIC);
633	zend_declare_property_null(ce, "warning_count",		sizeof("warning_count") - 1, ZEND_ACC_PUBLIC);
634	zend_hash_add_ptr(&classes, ce->name, &mysqli_link_properties);
635
636	REGISTER_MYSQLI_CLASS_ENTRY("mysqli_warning", mysqli_warning_class_entry, mysqli_warning_methods);
637	ce = mysqli_warning_class_entry;
638	ce->ce_flags |= ZEND_ACC_FINAL;
639	zend_hash_init(&mysqli_warning_properties, 0, NULL, free_prop_handler, 1);
640	MYSQLI_ADD_PROPERTIES(&mysqli_warning_properties, mysqli_warning_property_entries);
641	zend_declare_property_null(ce, "message", 	sizeof("message") - 1, ZEND_ACC_PUBLIC);
642	zend_declare_property_null(ce, "sqlstate", 	sizeof("sqlstate") - 1, ZEND_ACC_PUBLIC);
643	zend_declare_property_null(ce, "errno",		sizeof("errno") - 1, ZEND_ACC_PUBLIC);
644	zend_hash_add_ptr(&classes, ce->name, &mysqli_warning_properties);
645
646	REGISTER_MYSQLI_CLASS_ENTRY("mysqli_result", mysqli_result_class_entry, mysqli_result_methods);
647	ce = mysqli_result_class_entry;
648	zend_hash_init(&mysqli_result_properties, 0, NULL, free_prop_handler, 1);
649	MYSQLI_ADD_PROPERTIES(&mysqli_result_properties, mysqli_result_property_entries);
650	zend_declare_property_null(ce, "current_field",	sizeof("current_field") - 1,ZEND_ACC_PUBLIC);
651	zend_declare_property_null(ce, "field_count",	sizeof("field_count") - 1, ZEND_ACC_PUBLIC);
652	zend_declare_property_null(ce, "lengths",		sizeof("lengths") - 1, ZEND_ACC_PUBLIC);
653	zend_declare_property_null(ce, "num_rows",		sizeof("num_rows") - 1, ZEND_ACC_PUBLIC);
654	zend_declare_property_null(ce, "type",			sizeof("type") - 1, ZEND_ACC_PUBLIC);
655	mysqli_result_class_entry->get_iterator = php_mysqli_result_get_iterator;
656	zend_class_implements(mysqli_result_class_entry, 1, zend_ce_traversable);
657	zend_hash_add_ptr(&classes, ce->name, &mysqli_result_properties);
658
659	REGISTER_MYSQLI_CLASS_ENTRY("mysqli_stmt", mysqli_stmt_class_entry, mysqli_stmt_methods);
660	ce = mysqli_stmt_class_entry;
661	zend_hash_init(&mysqli_stmt_properties, 0, NULL, free_prop_handler, 1);
662	MYSQLI_ADD_PROPERTIES(&mysqli_stmt_properties, mysqli_stmt_property_entries);
663	zend_declare_property_null(ce, "affected_rows", sizeof("affected_rows") - 1, ZEND_ACC_PUBLIC);
664	zend_declare_property_null(ce, "insert_id",		sizeof("insert_id") - 1, ZEND_ACC_PUBLIC);
665	zend_declare_property_null(ce, "num_rows",		sizeof("num_rows") - 1, ZEND_ACC_PUBLIC);
666	zend_declare_property_null(ce, "param_count",	sizeof("param_count") - 1, ZEND_ACC_PUBLIC);
667	zend_declare_property_null(ce, "field_count",	sizeof("field_count") - 1, ZEND_ACC_PUBLIC);
668	zend_declare_property_null(ce, "errno",			sizeof("errno") - 1, ZEND_ACC_PUBLIC);
669	zend_declare_property_null(ce, "error",			sizeof("error") - 1, ZEND_ACC_PUBLIC);
670	zend_declare_property_null(ce, "error_list",	sizeof("error_list") - 1, ZEND_ACC_PUBLIC);
671	zend_declare_property_null(ce, "sqlstate",		sizeof("sqlstate") - 1, ZEND_ACC_PUBLIC);
672	zend_declare_property_null(ce, "id",			sizeof("id") - 1, ZEND_ACC_PUBLIC);
673	zend_hash_add_ptr(&classes, ce->name, &mysqli_stmt_properties);
674
675	/* mysqli_options */
676	REGISTER_LONG_CONSTANT("MYSQLI_READ_DEFAULT_GROUP", MYSQL_READ_DEFAULT_GROUP, CONST_CS | CONST_PERSISTENT);
677	REGISTER_LONG_CONSTANT("MYSQLI_READ_DEFAULT_FILE", MYSQL_READ_DEFAULT_FILE, CONST_CS | CONST_PERSISTENT);
678	REGISTER_LONG_CONSTANT("MYSQLI_OPT_CONNECT_TIMEOUT", MYSQL_OPT_CONNECT_TIMEOUT, CONST_CS | CONST_PERSISTENT);
679	REGISTER_LONG_CONSTANT("MYSQLI_OPT_LOCAL_INFILE", MYSQL_OPT_LOCAL_INFILE, CONST_CS | CONST_PERSISTENT);
680	REGISTER_LONG_CONSTANT("MYSQLI_INIT_COMMAND", MYSQL_INIT_COMMAND, CONST_CS | CONST_PERSISTENT);
681	REGISTER_LONG_CONSTANT("MYSQLI_OPT_READ_TIMEOUT", MYSQL_OPT_READ_TIMEOUT, CONST_CS | CONST_PERSISTENT);
682#if defined(MYSQLI_USE_MYSQLND)
683	REGISTER_LONG_CONSTANT("MYSQLI_OPT_NET_CMD_BUFFER_SIZE", MYSQLND_OPT_NET_CMD_BUFFER_SIZE, CONST_CS | CONST_PERSISTENT);
684	REGISTER_LONG_CONSTANT("MYSQLI_OPT_NET_READ_BUFFER_SIZE", MYSQLND_OPT_NET_READ_BUFFER_SIZE, CONST_CS | CONST_PERSISTENT);
685#endif
686#ifdef MYSQLND_STRING_TO_INT_CONVERSION
687	REGISTER_LONG_CONSTANT("MYSQLI_OPT_INT_AND_FLOAT_NATIVE", MYSQLND_OPT_INT_AND_FLOAT_NATIVE, CONST_CS | CONST_PERSISTENT);
688#endif
689#if (MYSQL_VERSION_ID > 50110 && MYSQL_VERSION_ID < 80000) || (MYSQL_VERSION_ID >= 100000) || defined(MYSQLI_USE_MYSQLND)
690	REGISTER_LONG_CONSTANT("MYSQLI_OPT_SSL_VERIFY_SERVER_CERT", MYSQL_OPT_SSL_VERIFY_SERVER_CERT, CONST_CS | CONST_PERSISTENT);
691#endif
692
693#if MYSQL_VERSION_ID > 50605 || defined(MYSQLI_USE_MYSQLND)
694	REGISTER_LONG_CONSTANT("MYSQLI_SERVER_PUBLIC_KEY", MYSQL_SERVER_PUBLIC_KEY, CONST_CS | CONST_PERSISTENT);
695#endif
696
697	/* mysqli_real_connect flags */
698	REGISTER_LONG_CONSTANT("MYSQLI_CLIENT_SSL", CLIENT_SSL, CONST_CS | CONST_PERSISTENT);
699	REGISTER_LONG_CONSTANT("MYSQLI_CLIENT_COMPRESS",CLIENT_COMPRESS, CONST_CS | CONST_PERSISTENT);
700	REGISTER_LONG_CONSTANT("MYSQLI_CLIENT_INTERACTIVE", CLIENT_INTERACTIVE, CONST_CS | CONST_PERSISTENT);
701	REGISTER_LONG_CONSTANT("MYSQLI_CLIENT_IGNORE_SPACE", CLIENT_IGNORE_SPACE, CONST_CS | CONST_PERSISTENT);
702	REGISTER_LONG_CONSTANT("MYSQLI_CLIENT_NO_SCHEMA", CLIENT_NO_SCHEMA, CONST_CS | CONST_PERSISTENT);
703	REGISTER_LONG_CONSTANT("MYSQLI_CLIENT_FOUND_ROWS", CLIENT_FOUND_ROWS, CONST_CS | CONST_PERSISTENT);
704#ifdef CLIENT_SSL_VERIFY_SERVER_CERT
705	REGISTER_LONG_CONSTANT("MYSQLI_CLIENT_SSL_VERIFY_SERVER_CERT", CLIENT_SSL_VERIFY_SERVER_CERT, CONST_CS | CONST_PERSISTENT);
706#if defined(MYSQLI_USE_MYSQLND)
707	REGISTER_LONG_CONSTANT("MYSQLI_CLIENT_SSL_DONT_VERIFY_SERVER_CERT", CLIENT_SSL_DONT_VERIFY_SERVER_CERT, CONST_CS | CONST_PERSISTENT);
708#endif
709#endif
710#if (MYSQL_VERSION_ID >= 50611 && defined(CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS)) || defined(MYSQLI_USE_MYSQLND)
711	REGISTER_LONG_CONSTANT("MYSQLI_CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS", CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS, CONST_CS | CONST_PERSISTENT);
712	REGISTER_LONG_CONSTANT("MYSQLI_OPT_CAN_HANDLE_EXPIRED_PASSWORDS", MYSQL_OPT_CAN_HANDLE_EXPIRED_PASSWORDS, CONST_CS | CONST_PERSISTENT);
713#endif
714
715	/* for mysqli_query */
716	REGISTER_LONG_CONSTANT("MYSQLI_STORE_RESULT", MYSQLI_STORE_RESULT, CONST_CS | CONST_PERSISTENT);
717	REGISTER_LONG_CONSTANT("MYSQLI_USE_RESULT", MYSQLI_USE_RESULT, CONST_CS | CONST_PERSISTENT);
718#if defined (MYSQLI_USE_MYSQLND)
719	REGISTER_LONG_CONSTANT("MYSQLI_ASYNC", MYSQLI_ASYNC, CONST_CS | CONST_PERSISTENT);
720	REGISTER_LONG_CONSTANT("MYSQLI_STORE_RESULT_COPY_DATA", MYSQLI_STORE_RESULT_COPY_DATA, CONST_CS | CONST_PERSISTENT);
721#endif
722
723	/* for mysqli_fetch_assoc */
724	REGISTER_LONG_CONSTANT("MYSQLI_ASSOC", MYSQLI_ASSOC, CONST_CS | CONST_PERSISTENT);
725	REGISTER_LONG_CONSTANT("MYSQLI_NUM", MYSQLI_NUM, CONST_CS | CONST_PERSISTENT);
726	REGISTER_LONG_CONSTANT("MYSQLI_BOTH", MYSQLI_BOTH, CONST_CS | CONST_PERSISTENT);
727
728	/* for mysqli_stmt_set_attr */
729	REGISTER_LONG_CONSTANT("MYSQLI_STMT_ATTR_UPDATE_MAX_LENGTH", STMT_ATTR_UPDATE_MAX_LENGTH, CONST_CS | CONST_PERSISTENT);
730
731#if MYSQL_VERSION_ID > 50003 || defined(MYSQLI_USE_MYSQLND)
732	REGISTER_LONG_CONSTANT("MYSQLI_STMT_ATTR_CURSOR_TYPE", STMT_ATTR_CURSOR_TYPE, CONST_CS | CONST_PERSISTENT);
733	REGISTER_LONG_CONSTANT("MYSQLI_CURSOR_TYPE_NO_CURSOR", CURSOR_TYPE_NO_CURSOR, CONST_CS | CONST_PERSISTENT);
734	REGISTER_LONG_CONSTANT("MYSQLI_CURSOR_TYPE_READ_ONLY", CURSOR_TYPE_READ_ONLY, CONST_CS | CONST_PERSISTENT);
735	REGISTER_LONG_CONSTANT("MYSQLI_CURSOR_TYPE_FOR_UPDATE", CURSOR_TYPE_FOR_UPDATE, CONST_CS | CONST_PERSISTENT);
736	REGISTER_LONG_CONSTANT("MYSQLI_CURSOR_TYPE_SCROLLABLE", CURSOR_TYPE_SCROLLABLE, CONST_CS | CONST_PERSISTENT);
737#endif
738
739#if MYSQL_VERSION_ID > 50007 || defined(MYSQLI_USE_MYSQLND)
740	REGISTER_LONG_CONSTANT("MYSQLI_STMT_ATTR_PREFETCH_ROWS", STMT_ATTR_PREFETCH_ROWS, CONST_CS | CONST_PERSISTENT);
741#endif
742
743	/* column information */
744	REGISTER_LONG_CONSTANT("MYSQLI_NOT_NULL_FLAG", NOT_NULL_FLAG, CONST_CS | CONST_PERSISTENT);
745	REGISTER_LONG_CONSTANT("MYSQLI_PRI_KEY_FLAG", PRI_KEY_FLAG, CONST_CS | CONST_PERSISTENT);
746	REGISTER_LONG_CONSTANT("MYSQLI_UNIQUE_KEY_FLAG", UNIQUE_KEY_FLAG, CONST_CS | CONST_PERSISTENT);
747	REGISTER_LONG_CONSTANT("MYSQLI_MULTIPLE_KEY_FLAG", MULTIPLE_KEY_FLAG, CONST_CS | CONST_PERSISTENT);
748	REGISTER_LONG_CONSTANT("MYSQLI_BLOB_FLAG", BLOB_FLAG, CONST_CS | CONST_PERSISTENT);
749	REGISTER_LONG_CONSTANT("MYSQLI_UNSIGNED_FLAG", UNSIGNED_FLAG, CONST_CS | CONST_PERSISTENT);
750	REGISTER_LONG_CONSTANT("MYSQLI_ZEROFILL_FLAG", ZEROFILL_FLAG, CONST_CS | CONST_PERSISTENT);
751	REGISTER_LONG_CONSTANT("MYSQLI_AUTO_INCREMENT_FLAG", AUTO_INCREMENT_FLAG, CONST_CS | CONST_PERSISTENT);
752	REGISTER_LONG_CONSTANT("MYSQLI_TIMESTAMP_FLAG", TIMESTAMP_FLAG, CONST_CS | CONST_PERSISTENT);
753	REGISTER_LONG_CONSTANT("MYSQLI_SET_FLAG", SET_FLAG, CONST_CS | CONST_PERSISTENT);
754	REGISTER_LONG_CONSTANT("MYSQLI_NUM_FLAG", NUM_FLAG, CONST_CS | CONST_PERSISTENT);
755	REGISTER_LONG_CONSTANT("MYSQLI_PART_KEY_FLAG", PART_KEY_FLAG, CONST_CS | CONST_PERSISTENT);
756	REGISTER_LONG_CONSTANT("MYSQLI_GROUP_FLAG", GROUP_FLAG, CONST_CS | CONST_PERSISTENT);
757	REGISTER_LONG_CONSTANT("MYSQLI_ENUM_FLAG", ENUM_FLAG, CONST_CS | CONST_PERSISTENT);
758	REGISTER_LONG_CONSTANT("MYSQLI_BINARY_FLAG", BINARY_FLAG, CONST_CS | CONST_PERSISTENT);
759#if MYSQL_VERSION_ID > 50001 || defined(MYSQLI_USE_MYSQLND)
760	REGISTER_LONG_CONSTANT("MYSQLI_NO_DEFAULT_VALUE_FLAG", NO_DEFAULT_VALUE_FLAG, CONST_CS | CONST_PERSISTENT);
761#endif
762
763#if (MYSQL_VERSION_ID > 51122 && MYSQL_VERSION_ID < 60000) || (MYSQL_VERSION_ID > 60003) || defined(MYSQLI_USE_MYSQLND)
764	REGISTER_LONG_CONSTANT("MYSQLI_ON_UPDATE_NOW_FLAG", ON_UPDATE_NOW_FLAG, CONST_CS | CONST_PERSISTENT);
765#endif
766
767	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_DECIMAL", FIELD_TYPE_DECIMAL, CONST_CS | CONST_PERSISTENT);
768	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_TINY", FIELD_TYPE_TINY, CONST_CS | CONST_PERSISTENT);
769	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_SHORT", FIELD_TYPE_SHORT, CONST_CS | CONST_PERSISTENT);
770	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_LONG", FIELD_TYPE_LONG, CONST_CS | CONST_PERSISTENT);
771	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_FLOAT", FIELD_TYPE_FLOAT, CONST_CS | CONST_PERSISTENT);
772	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_DOUBLE", FIELD_TYPE_DOUBLE, CONST_CS | CONST_PERSISTENT);
773	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_NULL", FIELD_TYPE_NULL, CONST_CS | CONST_PERSISTENT);
774	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_TIMESTAMP", FIELD_TYPE_TIMESTAMP, CONST_CS | CONST_PERSISTENT);
775	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_LONGLONG", FIELD_TYPE_LONGLONG, CONST_CS | CONST_PERSISTENT);
776	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_INT24", FIELD_TYPE_INT24, CONST_CS | CONST_PERSISTENT);
777	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_DATE", FIELD_TYPE_DATE, CONST_CS | CONST_PERSISTENT);
778	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_TIME", FIELD_TYPE_TIME, CONST_CS | CONST_PERSISTENT);
779	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_DATETIME", FIELD_TYPE_DATETIME	, CONST_CS | CONST_PERSISTENT);
780	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_YEAR", FIELD_TYPE_YEAR, CONST_CS | CONST_PERSISTENT);
781	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_NEWDATE", FIELD_TYPE_NEWDATE, CONST_CS | CONST_PERSISTENT);
782	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_ENUM", FIELD_TYPE_ENUM, CONST_CS | CONST_PERSISTENT);
783	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_SET", FIELD_TYPE_SET, CONST_CS | CONST_PERSISTENT);
784	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_TINY_BLOB", FIELD_TYPE_TINY_BLOB, CONST_CS | CONST_PERSISTENT);
785	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_MEDIUM_BLOB", FIELD_TYPE_MEDIUM_BLOB, CONST_CS | CONST_PERSISTENT);
786	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_LONG_BLOB", FIELD_TYPE_LONG_BLOB, CONST_CS | CONST_PERSISTENT);
787	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_BLOB", FIELD_TYPE_BLOB, CONST_CS | CONST_PERSISTENT);
788	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_VAR_STRING", FIELD_TYPE_VAR_STRING, CONST_CS | CONST_PERSISTENT);
789	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_STRING", FIELD_TYPE_STRING, CONST_CS | CONST_PERSISTENT);
790	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_CHAR", FIELD_TYPE_CHAR, CONST_CS | CONST_PERSISTENT);
791	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_INTERVAL", FIELD_TYPE_INTERVAL, CONST_CS | CONST_PERSISTENT);
792	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_GEOMETRY", FIELD_TYPE_GEOMETRY, CONST_CS | CONST_PERSISTENT);
793#ifdef FIELD_TYPE_JSON
794	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_JSON", FIELD_TYPE_JSON, CONST_CS | CONST_PERSISTENT);
795#endif
796#if MYSQL_VERSION_ID > 50002 || defined(MYSQLI_USE_MYSQLND)
797	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_NEWDECIMAL", FIELD_TYPE_NEWDECIMAL, CONST_CS | CONST_PERSISTENT);
798	REGISTER_LONG_CONSTANT("MYSQLI_TYPE_BIT", FIELD_TYPE_BIT, CONST_CS | CONST_PERSISTENT);
799#endif
800
801	REGISTER_LONG_CONSTANT("MYSQLI_SET_CHARSET_NAME", MYSQL_SET_CHARSET_NAME, CONST_CS | CONST_PERSISTENT);
802	REGISTER_LONG_CONSTANT("MYSQLI_SET_CHARSET_DIR", MYSQL_SET_CHARSET_DIR, CONST_CS | CONST_PERSISTENT);
803
804	/* bind support */
805	REGISTER_LONG_CONSTANT("MYSQLI_NO_DATA", MYSQL_NO_DATA, CONST_CS | CONST_PERSISTENT);
806#ifdef MYSQL_DATA_TRUNCATED
807	REGISTER_LONG_CONSTANT("MYSQLI_DATA_TRUNCATED", MYSQL_DATA_TRUNCATED, CONST_CS | CONST_PERSISTENT);
808#endif
809
810	/* reporting */
811	REGISTER_LONG_CONSTANT("MYSQLI_REPORT_INDEX", MYSQLI_REPORT_INDEX, CONST_CS | CONST_PERSISTENT);
812	REGISTER_LONG_CONSTANT("MYSQLI_REPORT_ERROR", MYSQLI_REPORT_ERROR, CONST_CS | CONST_PERSISTENT);
813	REGISTER_LONG_CONSTANT("MYSQLI_REPORT_STRICT", MYSQLI_REPORT_STRICT, CONST_CS | CONST_PERSISTENT);
814	REGISTER_LONG_CONSTANT("MYSQLI_REPORT_ALL", MYSQLI_REPORT_ALL, CONST_CS | CONST_PERSISTENT);
815	REGISTER_LONG_CONSTANT("MYSQLI_REPORT_OFF", 0, CONST_CS | CONST_PERSISTENT);
816
817	/* We use non-nested macros with expansion, as VC has problems */
818#ifdef MYSQLI_USE_MYSQLND
819	REGISTER_LONG_CONSTANT("MYSQLI_DEBUG_TRACE_ENABLED", MYSQLND_DBG_ENABLED, CONST_CS | CONST_PERSISTENT);
820#else
821#ifdef DBUG_ON
822	REGISTER_LONG_CONSTANT("MYSQLI_DEBUG_TRACE_ENABLED", 1, CONST_CS | CONST_PERSISTENT);
823#else
824	REGISTER_LONG_CONSTANT("MYSQLI_DEBUG_TRACE_ENABLED", 0, CONST_CS | CONST_PERSISTENT);
825#endif
826#endif
827
828	REGISTER_LONG_CONSTANT("MYSQLI_SERVER_QUERY_NO_GOOD_INDEX_USED", SERVER_QUERY_NO_GOOD_INDEX_USED, CONST_CS | CONST_PERSISTENT);
829	REGISTER_LONG_CONSTANT("MYSQLI_SERVER_QUERY_NO_INDEX_USED", SERVER_QUERY_NO_INDEX_USED, CONST_CS | CONST_PERSISTENT);
830#ifdef SERVER_QUERY_WAS_SLOW
831	REGISTER_LONG_CONSTANT("MYSQLI_SERVER_QUERY_WAS_SLOW", SERVER_QUERY_WAS_SLOW, CONST_CS | CONST_PERSISTENT);
832#endif
833#ifdef SERVER_PS_OUT_PARAMS
834	REGISTER_LONG_CONSTANT("MYSQLI_SERVER_PS_OUT_PARAMS", SERVER_PS_OUT_PARAMS, CONST_CS | CONST_PERSISTENT);
835#endif
836
837	REGISTER_LONG_CONSTANT("MYSQLI_REFRESH_GRANT",      REFRESH_GRANT, CONST_CS | CONST_PERSISTENT);
838	REGISTER_LONG_CONSTANT("MYSQLI_REFRESH_LOG",        REFRESH_LOG, CONST_CS | CONST_PERSISTENT);
839	REGISTER_LONG_CONSTANT("MYSQLI_REFRESH_TABLES",     REFRESH_TABLES, CONST_CS | CONST_PERSISTENT);
840	REGISTER_LONG_CONSTANT("MYSQLI_REFRESH_HOSTS",      REFRESH_HOSTS, CONST_CS | CONST_PERSISTENT);
841	REGISTER_LONG_CONSTANT("MYSQLI_REFRESH_STATUS",     REFRESH_STATUS, CONST_CS | CONST_PERSISTENT);
842	REGISTER_LONG_CONSTANT("MYSQLI_REFRESH_THREADS",    REFRESH_THREADS, CONST_CS | CONST_PERSISTENT);
843	REGISTER_LONG_CONSTANT("MYSQLI_REFRESH_SLAVE",      REFRESH_SLAVE, CONST_CS | CONST_PERSISTENT);
844	REGISTER_LONG_CONSTANT("MYSQLI_REFRESH_MASTER",     REFRESH_MASTER, CONST_CS | CONST_PERSISTENT);
845#ifdef REFRESH_BACKUP_LOG
846	REGISTER_LONG_CONSTANT("MYSQLI_REFRESH_BACKUP_LOG", REFRESH_BACKUP_LOG, CONST_CS | CONST_PERSISTENT);
847#endif
848
849
850	REGISTER_LONG_CONSTANT("MYSQLI_TRANS_START_WITH_CONSISTENT_SNAPSHOT", TRANS_START_WITH_CONSISTENT_SNAPSHOT, CONST_CS | CONST_PERSISTENT);
851	REGISTER_LONG_CONSTANT("MYSQLI_TRANS_START_READ_WRITE", TRANS_START_READ_WRITE, CONST_CS | CONST_PERSISTENT);
852	REGISTER_LONG_CONSTANT("MYSQLI_TRANS_START_READ_ONLY", TRANS_START_READ_ONLY, CONST_CS | CONST_PERSISTENT);
853
854	REGISTER_LONG_CONSTANT("MYSQLI_TRANS_COR_AND_CHAIN", TRANS_COR_AND_CHAIN, CONST_CS | CONST_PERSISTENT);
855	REGISTER_LONG_CONSTANT("MYSQLI_TRANS_COR_AND_NO_CHAIN", TRANS_COR_AND_NO_CHAIN, CONST_CS | CONST_PERSISTENT);
856	REGISTER_LONG_CONSTANT("MYSQLI_TRANS_COR_RELEASE", TRANS_COR_RELEASE, CONST_CS | CONST_PERSISTENT);
857	REGISTER_LONG_CONSTANT("MYSQLI_TRANS_COR_NO_RELEASE", TRANS_COR_NO_RELEASE, CONST_CS | CONST_PERSISTENT);
858
859
860#ifdef MYSQLI_USE_MYSQLND
861	mysqlnd_reverse_api_register_api(&mysqli_reverse_api);
862#endif
863
864	return SUCCESS;
865}
866/* }}} */
867
868/* {{{ PHP_MSHUTDOWN_FUNCTION
869 */
870PHP_MSHUTDOWN_FUNCTION(mysqli)
871{
872#ifndef MYSQLI_USE_MYSQLND
873#ifdef PHP_WIN32
874	zend_ulong client_ver = mysql_get_client_version();
875	/*
876	  Can't call mysql_server_end() multiple times prior to 5.0.46 on Windows.
877	  PHP bug#41350 MySQL bug#25621
878	*/
879	if ((client_ver >= 50046 && client_ver < 50100) || client_ver > 50122) {
880		mysql_server_end();
881	}
882#else
883	mysql_server_end();
884#endif
885#endif
886
887	zend_hash_destroy(&mysqli_driver_properties);
888	zend_hash_destroy(&mysqli_result_properties);
889	zend_hash_destroy(&mysqli_stmt_properties);
890	zend_hash_destroy(&mysqli_warning_properties);
891	zend_hash_destroy(&mysqli_link_properties);
892	zend_hash_destroy(&classes);
893
894	UNREGISTER_INI_ENTRIES();
895	return SUCCESS;
896}
897/* }}} */
898
899/* {{{ PHP_RINIT_FUNCTION
900 */
901PHP_RINIT_FUNCTION(mysqli)
902{
903#if !defined(MYSQLI_USE_MYSQLND) && defined(ZTS)
904	if (mysql_thread_init()) {
905		return FAILURE;
906	}
907#endif
908	MyG(error_msg) = NULL;
909	MyG(error_no) = 0;
910	MyG(report_mode) = 0;
911
912	return SUCCESS;
913}
914/* }}} */
915
916#if defined(A0) && defined(MYSQLI_USE_MYSQLND)
917static void php_mysqli_persistent_helper_for_every(void *p)
918{
919	mysqlnd_end_psession((MYSQLND *) p);
920} /* }}} */
921
922
923static int php_mysqli_persistent_helper_once(zend_rsrc_list_entry *le)
924{
925	if (le->type == php_le_pmysqli()) {
926		mysqli_plist_entry *plist = (mysqli_plist_entry *) le->ptr;
927		zend_ptr_stack_apply(&plist->free_links, php_mysqli_persistent_helper_for_every);
928	}
929	return ZEND_HASH_APPLY_KEEP;
930} /* }}} */
931#endif
932
933
934/* {{{ PHP_RSHUTDOWN_FUNCTION
935 */
936PHP_RSHUTDOWN_FUNCTION(mysqli)
937{
938	/* check persistent connections, move used to free */
939
940#if !defined(MYSQLI_USE_MYSQLND) && defined(ZTS)
941	mysql_thread_end();
942#endif
943	if (MyG(error_msg)) {
944		efree(MyG(error_msg));
945	}
946#if defined(A0) && defined(MYSQLI_USE_MYSQLND)
947	/* psession is being called when the connection is freed - explicitly or implicitly */
948	zend_hash_apply(&EG(persistent_list), (apply_func_t) php_mysqli_persistent_helper_once);
949#endif
950	return SUCCESS;
951}
952/* }}} */
953
954
955/* {{{ PHP_MINFO_FUNCTION
956 */
957PHP_MINFO_FUNCTION(mysqli)
958{
959	char buf[32];
960
961	php_info_print_table_start();
962	php_info_print_table_header(2, "MysqlI Support", "enabled");
963	php_info_print_table_row(2, "Client API library version", mysql_get_client_info());
964	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MyG(num_active_persistent));
965	php_info_print_table_row(2, "Active Persistent Links", buf);
966	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MyG(num_inactive_persistent));
967	php_info_print_table_row(2, "Inactive Persistent Links", buf);
968	snprintf(buf, sizeof(buf), ZEND_LONG_FMT, MyG(num_links));
969	php_info_print_table_row(2, "Active Links", buf);
970#if !defined(MYSQLI_USE_MYSQLND)
971	php_info_print_table_row(2, "Client API header version", MYSQL_SERVER_VERSION);
972	php_info_print_table_row(2, "MYSQLI_SOCKET", MYSQL_UNIX_ADDR);
973#endif
974	php_info_print_table_end();
975
976	DISPLAY_INI_ENTRIES();
977}
978/* }}} */
979
980
981/* Dependencies */
982static const  zend_module_dep mysqli_deps[] = {
983	ZEND_MOD_REQUIRED("spl")
984#if defined(MYSQLI_USE_MYSQLND)
985	ZEND_MOD_REQUIRED("mysqlnd")
986#endif
987	ZEND_MOD_END
988};
989
990/* {{{ mysqli_module_entry
991 */
992zend_module_entry mysqli_module_entry = {
993	STANDARD_MODULE_HEADER_EX, NULL,
994	mysqli_deps,
995	"mysqli",
996	mysqli_functions,
997	PHP_MINIT(mysqli),
998	PHP_MSHUTDOWN(mysqli),
999	PHP_RINIT(mysqli),
1000	PHP_RSHUTDOWN(mysqli),
1001	PHP_MINFO(mysqli),
1002	PHP_MYSQLI_VERSION,
1003	PHP_MODULE_GLOBALS(mysqli),
1004	PHP_GINIT(mysqli),
1005	NULL,
1006	NULL,
1007	STANDARD_MODULE_PROPERTIES_EX
1008};
1009/* }}} */
1010
1011#ifdef COMPILE_DL_MYSQLI
1012#ifdef ZTS
1013ZEND_TSRMLS_CACHE_DEFINE()
1014#endif
1015ZEND_GET_MODULE(mysqli)
1016#endif
1017
1018
1019/* {{{ mixed mysqli_stmt_construct()
1020constructor for statement object.
1021Parameters:
1022  object -> mysqli_stmt_init
1023  object, query -> mysqli_prepare
1024*/
1025PHP_FUNCTION(mysqli_stmt_construct)
1026{
1027	MY_MYSQL			*mysql;
1028	zval				*mysql_link;
1029	MY_STMT				*stmt;
1030	MYSQLI_RESOURCE		*mysqli_resource;
1031	char				*statement;
1032	size_t					statement_len;
1033
1034	switch (ZEND_NUM_ARGS())
1035	{
1036		case 1:  /* mysql_stmt_init */
1037			if (zend_parse_parameters(1, "O", &mysql_link, mysqli_link_class_entry)==FAILURE) {
1038				return;
1039			}
1040			MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1041
1042			stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
1043
1044			stmt->stmt = mysql_stmt_init(mysql->mysql);
1045		break;
1046		case 2:
1047			if (zend_parse_parameters(2, "Os", &mysql_link, mysqli_link_class_entry, &statement, &statement_len)==FAILURE) {
1048				return;
1049			}
1050			MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1051
1052			stmt = (MY_STMT *)ecalloc(1,sizeof(MY_STMT));
1053
1054			if ((stmt->stmt = mysql_stmt_init(mysql->mysql))) {
1055				mysql_stmt_prepare(stmt->stmt, (char *)statement, statement_len);
1056			}
1057		break;
1058		default:
1059			WRONG_PARAM_COUNT;
1060		break;
1061	}
1062
1063	if (!stmt->stmt) {
1064		efree(stmt);
1065		RETURN_FALSE;
1066	}
1067#ifndef MYSQLI_USE_MYSQLND
1068	ZVAL_COPY(&stmt->link_handle, mysql_link);
1069#endif
1070
1071	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
1072	mysqli_resource->ptr = (void *)stmt;
1073	mysqli_resource->status = (ZEND_NUM_ARGS() == 1) ? MYSQLI_STATUS_INITIALIZED : MYSQLI_STATUS_VALID;
1074
1075	MYSQLI_REGISTER_RESOURCE_EX(mysqli_resource, getThis());
1076}
1077/* }}} */
1078
1079/* {{{ mixed mysqli_result_construct()
1080constructor for result object.
1081Parameters:
1082  object [, mode] -> mysqli_store/use_result
1083*/
1084PHP_FUNCTION(mysqli_result_construct)
1085{
1086	MY_MYSQL			*mysql;
1087	MYSQL_RES			*result = NULL;
1088	zval				*mysql_link;
1089	MYSQLI_RESOURCE		*mysqli_resource;
1090	zend_long				resmode = MYSQLI_STORE_RESULT;
1091
1092	switch (ZEND_NUM_ARGS()) {
1093		case 1:
1094			if (zend_parse_parameters(1, "O", &mysql_link, mysqli_link_class_entry)==FAILURE) {
1095				return;
1096			}
1097			break;
1098		case 2:
1099			if (zend_parse_parameters(2, "Ol", &mysql_link, mysqli_link_class_entry, &resmode)==FAILURE) {
1100				return;
1101			}
1102			break;
1103		default:
1104			WRONG_PARAM_COUNT;
1105	}
1106
1107	MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1108
1109	switch (resmode) {
1110		case MYSQLI_STORE_RESULT:
1111			result = mysql_store_result(mysql->mysql);
1112			break;
1113		case MYSQLI_USE_RESULT:
1114			result = mysql_use_result(mysql->mysql);
1115			break;
1116		default:
1117			php_error_docref(NULL, E_WARNING, "Invalid value for resultmode");
1118	}
1119
1120	if (!result) {
1121		RETURN_FALSE;
1122	}
1123
1124	mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
1125	mysqli_resource->ptr = (void *)result;
1126	mysqli_resource->status = MYSQLI_STATUS_VALID;
1127
1128	MYSQLI_REGISTER_RESOURCE_EX(mysqli_resource, getThis());
1129}
1130/* }}} */
1131
1132/* {{{ php_mysqli_fetch_into_hash_aux
1133 */
1134void php_mysqli_fetch_into_hash_aux(zval *return_value, MYSQL_RES * result, zend_long fetchtype)
1135{
1136#if !defined(MYSQLI_USE_MYSQLND)
1137	MYSQL_ROW row;
1138	unsigned int	i, num_fields;
1139	MYSQL_FIELD		*fields;
1140	zend_ulong	*field_len;
1141
1142	if (!(row = mysql_fetch_row(result))) {
1143		RETURN_NULL();
1144	}
1145
1146	if (fetchtype & MYSQLI_ASSOC) {
1147		fields = mysql_fetch_fields(result);
1148	}
1149
1150	array_init(return_value);
1151	field_len = mysql_fetch_lengths(result);
1152	num_fields = mysql_num_fields(result);
1153
1154	for (i = 0; i < num_fields; i++) {
1155		if (row[i]) {
1156			zval res;
1157
1158#if MYSQL_VERSION_ID > 50002
1159			if (mysql_fetch_field_direct(result, i)->type == MYSQL_TYPE_BIT) {
1160				my_ulonglong llval;
1161				char tmp[22];
1162				switch (field_len[i]) {
1163					case 8:llval = (my_ulonglong)  bit_uint8korr(row[i]);break;
1164					case 7:llval = (my_ulonglong)  bit_uint7korr(row[i]);break;
1165					case 6:llval = (my_ulonglong)  bit_uint6korr(row[i]);break;
1166					case 5:llval = (my_ulonglong)  bit_uint5korr(row[i]);break;
1167					case 4:llval = (my_ulonglong)  bit_uint4korr(row[i]);break;
1168					case 3:llval = (my_ulonglong)  bit_uint3korr(row[i]);break;
1169					case 2:llval = (my_ulonglong)  bit_uint2korr(row[i]);break;
1170					case 1:llval = (my_ulonglong)  uint1korr(row[i]);break;
1171				}
1172				/* even though lval is declared as unsigned, the value
1173				 * may be negative. Therefor we cannot use MYSQLI_LLU_SPEC and must
1174				 * use MYSQLI_LL_SPEC.
1175				 */
1176				snprintf(tmp, sizeof(tmp), (mysql_fetch_field_direct(result, i)->flags & UNSIGNED_FLAG)? MYSQLI_LLU_SPEC : MYSQLI_LL_SPEC, llval);
1177				ZVAL_STRING(&res, tmp);
1178			} else
1179#endif
1180			{
1181
1182				ZVAL_STRINGL(&res, row[i], field_len[i]);
1183			}
1184
1185			if (fetchtype & MYSQLI_NUM) {
1186				add_index_zval(return_value, i, &res);
1187			}
1188			if (fetchtype & MYSQLI_ASSOC) {
1189				if (fetchtype & MYSQLI_NUM && Z_REFCOUNTED(res)) {
1190					Z_ADDREF(res);
1191				}
1192				add_assoc_zval(return_value, fields[i].name, &res);
1193			}
1194		} else {
1195			if (fetchtype & MYSQLI_NUM) {
1196				add_index_null(return_value, i);
1197			}
1198			if (fetchtype & MYSQLI_ASSOC) {
1199				add_assoc_null(return_value, fields[i].name);
1200			}
1201		}
1202	}
1203#else
1204	mysqlnd_fetch_into(result, ((fetchtype & MYSQLI_NUM)? MYSQLND_FETCH_NUM:0) | ((fetchtype & MYSQLI_ASSOC)? MYSQLND_FETCH_ASSOC:0), return_value, MYSQLND_MYSQLI);
1205#endif
1206}
1207/* }}} */
1208
1209/* {{{ php_mysqli_fetch_into_hash
1210 */
1211void php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAMETERS, int override_flags, int into_object)
1212{
1213	MYSQL_RES		*result;
1214	zval			*mysql_result;
1215	zend_long			fetchtype;
1216	zval			*ctor_params = NULL;
1217	zend_class_entry *ce = NULL;
1218
1219	if (into_object) {
1220		zend_string *class_name = NULL;
1221
1222		if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|Sz", &mysql_result, mysqli_result_class_entry, &class_name, &ctor_params) == FAILURE) {
1223			return;
1224		}
1225		if (class_name == NULL) {
1226			ce = zend_standard_class_def;
1227		} else {
1228			ce = zend_fetch_class(class_name, ZEND_FETCH_CLASS_AUTO);
1229		}
1230		if (!ce) {
1231			php_error_docref(NULL, E_WARNING, "Could not find class '%s'", ZSTR_VAL(class_name));
1232			return;
1233		}
1234		if (UNEXPECTED(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) {
1235			zend_throw_error(NULL, "Class '%s' cannot be instantiated", ZSTR_VAL(ce->name));
1236			return;
1237		}
1238		fetchtype = MYSQLI_ASSOC;
1239	} else {
1240		if (override_flags) {
1241			if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_result, mysqli_result_class_entry) == FAILURE) {
1242				return;
1243			}
1244			fetchtype = override_flags;
1245		} else {
1246			fetchtype = MYSQLI_BOTH;
1247			if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &fetchtype) == FAILURE) {
1248				return;
1249			}
1250		}
1251	}
1252	MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
1253
1254	if (fetchtype < MYSQLI_ASSOC || fetchtype > MYSQLI_BOTH) {
1255		php_error_docref(NULL, E_WARNING, "The result type should be either MYSQLI_NUM, MYSQLI_ASSOC or MYSQLI_BOTH");
1256		RETURN_FALSE;
1257	}
1258
1259	php_mysqli_fetch_into_hash_aux(return_value, result, fetchtype);
1260
1261	if (into_object && Z_TYPE_P(return_value) == IS_ARRAY) {
1262		zval dataset, retval;
1263		zend_fcall_info fci;
1264		zend_fcall_info_cache fcc;
1265
1266		ZVAL_COPY_VALUE(&dataset, return_value);
1267
1268		object_and_properties_init(return_value, ce, NULL);
1269		if (!ce->default_properties_count && !ce->__set) {
1270			Z_OBJ_P(return_value)->properties = Z_ARR(dataset);
1271		} else {
1272			zend_merge_properties(return_value, Z_ARRVAL(dataset));
1273			zval_ptr_dtor(&dataset);
1274		}
1275
1276		if (ce->constructor) {
1277			fci.size = sizeof(fci);
1278			ZVAL_UNDEF(&fci.function_name);
1279			fci.object = Z_OBJ_P(return_value);
1280			fci.retval = &retval;
1281			fci.params = NULL;
1282			fci.param_count = 0;
1283			fci.no_separation = 1;
1284
1285			if (ctor_params && Z_TYPE_P(ctor_params) != IS_NULL) {
1286				if (zend_fcall_info_args(&fci, ctor_params) == FAILURE) {
1287					/* Two problems why we throw exceptions here: PHP is typeless
1288					 * and hence passing one argument that's not an array could be
1289					 * by mistake and the other way round is possible, too. The
1290					 * single value is an array. Also we'd have to make that one
1291					 * argument passed by reference.
1292					 */
1293					zend_throw_exception(zend_ce_exception, "Parameter ctor_params must be an array", 0);
1294					return;
1295				}
1296			}
1297
1298			fcc.function_handler = ce->constructor;
1299			fcc.called_scope = Z_OBJCE_P(return_value);
1300			fcc.object = Z_OBJ_P(return_value);
1301
1302			if (zend_call_function(&fci, &fcc) == FAILURE) {
1303				zend_throw_exception_ex(zend_ce_exception, 0, "Could not execute %s::%s()", ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
1304			} else {
1305				zval_ptr_dtor(&retval);
1306			}
1307			zend_fcall_info_args_clear(&fci, 1);
1308		} else if (ctor_params) {
1309			zend_throw_exception_ex(zend_ce_exception, 0, "Class %s does not have a constructor hence you cannot use ctor_params", ZSTR_VAL(ce->name));
1310		}
1311	}
1312}
1313/* }}} */
1314
1315/*
1316 * Local variables:
1317 * tab-width: 4
1318 * c-basic-offset: 4
1319 * End:
1320 * vim600: noet sw=4 ts=4 fdm=marker
1321 * vim<600: noet sw=4 ts=4
1322 */
1323