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