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