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