1 /*
2 +----------------------------------------------------------------------+
3 | Copyright (c) The PHP Group |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.01 of the PHP license, |
6 | that is bundled with this package in the file LICENSE, and is |
7 | available through the world-wide-web at the following url: |
8 | https://www.php.net/license/3_01.txt |
9 | If you did not receive a copy of the PHP license and are unable to |
10 | obtain it through the world-wide-web, please send a note to |
11 | license@php.net so we can mail you a copy immediately. |
12 +----------------------------------------------------------------------+
13 | Authors: Georg Richter <georg@php.net> |
14 | Andrey Hristov <andrey@php.net> |
15 | Ulf Wendel <uw@php.net> |
16 +----------------------------------------------------------------------+
17 */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <signal.h>
24
25 #include "php.h"
26 #include "zend_smart_str.h"
27 #include "php_mysqli_structs.h"
28 #include "mysqli_priv.h"
29 #define ERROR_ARG_POS(arg_num) (hasThis() ? (arg_num-1) : (arg_num))
30
31 #define SAFE_STR(a) ((a)?a:"")
32
33 /* {{{ php_mysqli_set_error */
php_mysqli_set_error(zend_long mysql_errno,char * mysql_err)34 static void php_mysqli_set_error(zend_long mysql_errno, char *mysql_err)
35 {
36 MyG(error_no) = mysql_errno;
37 if (MyG(error_msg)) {
38 efree(MyG(error_msg));
39 }
40 if(mysql_err && *mysql_err) {
41 MyG(error_msg) = estrdup(mysql_err);
42 } else {
43 MyG(error_msg) = NULL;
44 }
45 }
46 /* }}} */
47
mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS,bool is_real_connect,bool in_ctor)48 void mysqli_common_connect(INTERNAL_FUNCTION_PARAMETERS, bool is_real_connect, bool in_ctor) /* {{{ */
49 {
50 MY_MYSQL *mysql = NULL;
51 MYSQLI_RESOURCE *mysqli_resource = NULL;
52 zval *object = getThis();
53 char *hostname = NULL, *username=NULL, *passwd=NULL, *dbname=NULL, *socket=NULL,
54 *ssl_key = NULL, *ssl_cert = NULL, *ssl_ca = NULL, *ssl_capath = NULL,
55 *ssl_cipher = NULL;
56 size_t hostname_len = 0, username_len = 0, passwd_len = 0, dbname_len = 0, socket_len = 0;
57 bool persistent = false, ssl = false;
58 zend_long port = 0, flags = 0;
59 bool port_is_null = 1;
60 zend_string *hash_key = NULL;
61 bool new_connection = false;
62 zend_resource *le;
63 mysqli_plist_entry *plist = NULL;
64 bool self_alloced = 0;
65
66
67 #if !defined(MYSQL_USE_MYSQLND)
68 if ((MYSQL_VERSION_ID / 100) != (mysql_get_client_version() / 100)) {
69 php_error_docref(NULL, E_WARNING,
70 "Headers and client library minor version mismatch. Headers:%d Library:%ld",
71 MYSQL_VERSION_ID, mysql_get_client_version());
72 }
73 #endif
74
75 if (getThis() && !ZEND_NUM_ARGS() && in_ctor) {
76 php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU, in_ctor);
77 return;
78 }
79 hostname = username = dbname = passwd = socket = NULL;
80
81 if (!is_real_connect) {
82 if (zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!s!s!l!s!", &hostname, &hostname_len, &username, &username_len,
83 &passwd, &passwd_len, &dbname, &dbname_len, &port, &port_is_null, &socket, &socket_len) == FAILURE) {
84 RETURN_THROWS();
85 }
86
87 if (object) {
88 ZEND_ASSERT(instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry));
89 mysqli_resource = (Z_MYSQLI_P(object))->ptr;
90 if (mysqli_resource && mysqli_resource->ptr) {
91 mysql = (MY_MYSQL*) mysqli_resource->ptr;
92 }
93 }
94 if (!mysql) {
95 mysql = (MY_MYSQL *) ecalloc(1, sizeof(MY_MYSQL));
96 self_alloced = 1;
97 }
98 flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
99 } else {
100 /* We have flags too */
101 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|s!s!s!s!l!s!l", &object, mysqli_link_class_entry,
102 &hostname, &hostname_len, &username, &username_len, &passwd, &passwd_len, &dbname, &dbname_len, &port, &port_is_null, &socket, &socket_len, &flags) == FAILURE) {
103 RETURN_THROWS();
104 }
105
106 mysqli_resource = (Z_MYSQLI_P(object))->ptr;
107 MYSQLI_FETCH_RESOURCE_CONN(mysql, object, MYSQLI_STATUS_INITIALIZED);
108
109 /* set some required options */
110 flags |= CLIENT_MULTI_RESULTS; /* needed for mysql_multi_query() */
111 /* remove some insecure options */
112 flags &= ~CLIENT_MULTI_STATEMENTS; /* don't allow multi_queries via connect parameter */
113 }
114
115 if (!socket_len || !socket) {
116 socket = MyG(default_socket);
117 }
118 if (port_is_null || !port) {
119 port = MyG(default_port);
120 }
121 if (!passwd) {
122 passwd = MyG(default_pw);
123 passwd_len = strlen(SAFE_STR(passwd));
124 }
125 if (!username){
126 username = MyG(default_user);
127 }
128 if (!hostname || !hostname_len) {
129 hostname = MyG(default_host);
130 }
131
132 if (mysql->mysql && mysqli_resource &&
133 (mysqli_resource->status > MYSQLI_STATUS_INITIALIZED))
134 {
135 /* already connected, we should close the connection */
136 php_mysqli_close(mysql, MYSQLI_CLOSE_IMPLICIT, mysqli_resource->status);
137 }
138
139 if (strlen(SAFE_STR(hostname)) > 2 && !strncasecmp(hostname, "p:", 2)) {
140 hostname += 2;
141 if (!MyG(allow_persistent)) {
142 php_error_docref(NULL, E_WARNING, "Persistent connections are disabled. Downgrading to normal");
143 } else {
144 mysql->persistent = persistent = true;
145
146 hash_key = strpprintf(0, "mysqli_%s_%s" ZEND_LONG_FMT "%s%s%s", SAFE_STR(hostname), SAFE_STR(socket),
147 port, SAFE_STR(username), SAFE_STR(dbname),
148 SAFE_STR(passwd));
149
150 mysql->hash_key = hash_key;
151
152 /* check if we can reuse existing connection ... */
153 if ((le = zend_hash_find_ptr(&EG(persistent_list), hash_key)) != NULL) {
154 if (le->type == php_le_pmysqli()) {
155 plist = (mysqli_plist_entry *) le->ptr;
156
157 do {
158 if (zend_ptr_stack_num_elements(&plist->free_links)) {
159 /* If we have an initialized (but unconnected) mysql resource,
160 * close it before we reuse an existing persistent resource. */
161 if (mysql->mysql) {
162 mysqli_close(mysql->mysql, MYSQLI_CLOSE_IMPLICIT);
163 }
164
165 mysql->mysql = zend_ptr_stack_pop(&plist->free_links);
166
167 MyG(num_inactive_persistent)--;
168 /* reset variables */
169
170 #ifndef MYSQLI_NO_CHANGE_USER_ON_PCONNECT
171 if (!mysqli_change_user_silent(mysql->mysql, username, passwd, dbname, passwd_len)) {
172 #else
173 if (!mysql_ping(mysql->mysql)) {
174 #endif
175 mysqlnd_restart_psession(mysql->mysql);
176 MyG(num_active_persistent)++;
177
178 /* clear error */
179 php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql));
180
181 goto end;
182 } else {
183 if (mysql->mysql->data->vio->data->ssl) {
184 /* copy over pre-existing ssl settings so we can reuse them when reconnecting */
185 ssl = true;
186
187 ssl_key = mysql->mysql->data->vio->data->options.ssl_key ? estrdup(mysql->mysql->data->vio->data->options.ssl_key) : NULL;
188 ssl_cert = mysql->mysql->data->vio->data->options.ssl_cert ? estrdup(mysql->mysql->data->vio->data->options.ssl_cert) : NULL;
189 ssl_ca = mysql->mysql->data->vio->data->options.ssl_ca ? estrdup(mysql->mysql->data->vio->data->options.ssl_ca) : NULL;
190 ssl_capath = mysql->mysql->data->vio->data->options.ssl_capath ? estrdup(mysql->mysql->data->vio->data->options.ssl_capath) : NULL;
191 ssl_cipher = mysql->mysql->data->vio->data->options.ssl_cipher ? estrdup(mysql->mysql->data->vio->data->options.ssl_cipher) : NULL;
192 }
193
194 mysqli_close(mysql->mysql, MYSQLI_CLOSE_IMPLICIT);
195 mysql->mysql = NULL;
196 }
197 }
198 } while (0);
199 }
200 } else {
201 plist = calloc(1, sizeof(mysqli_plist_entry));
202
203 zend_ptr_stack_init_ex(&plist->free_links, 1);
204 zend_register_persistent_resource(ZSTR_VAL(hash_key), ZSTR_LEN(hash_key), plist, php_le_pmysqli());
205 }
206 }
207 }
208 if (MyG(max_links) != -1 && MyG(num_links) >= MyG(max_links)) {
209 php_error_docref(NULL, E_WARNING, "Too many open links (" ZEND_LONG_FMT ")", MyG(num_links));
210 goto err;
211 }
212
213 if (persistent && MyG(max_persistent) != -1 &&
214 (MyG(num_active_persistent) + MyG(num_inactive_persistent))>= MyG(max_persistent))
215 {
216 php_error_docref(NULL, E_WARNING, "Too many open persistent links (" ZEND_LONG_FMT ")",
217 MyG(num_active_persistent) + MyG(num_inactive_persistent));
218 goto err;
219 }
220 if (!mysql->mysql) {
221 if (!(mysql->mysql = mysqlnd_init(MYSQLND_CLIENT_NO_FLAG, persistent))) {
222 goto err;
223 }
224 new_connection = true;
225 }
226
227 if (ssl) {
228 /* if we're here, this means previous conn was ssl, repopulate settings */
229 mysql_ssl_set(mysql->mysql, ssl_key, ssl_cert, ssl_ca, ssl_capath, ssl_cipher);
230
231 if (ssl_key) {
232 efree(ssl_key);
233 }
234
235 if (ssl_cert) {
236 efree(ssl_cert);
237 }
238
239 if (ssl_ca) {
240 efree(ssl_ca);
241 }
242
243 if (ssl_capath) {
244 efree(ssl_capath);
245 }
246
247 if (ssl_cipher) {
248 efree(ssl_cipher);
249 }
250 }
251 if (mysqlnd_connect(mysql->mysql, hostname, username, passwd, passwd_len, dbname, dbname_len,
252 port, socket, flags, MYSQLND_CLIENT_NO_FLAG) == NULL)
253 {
254 /* Save error messages - for mysqli_connect_error() & mysqli_connect_errno() */
255 php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql));
256 php_mysqli_throw_sql_exception((char *)mysql_sqlstate(mysql->mysql), mysql_errno(mysql->mysql),
257 "%s", mysql_error(mysql->mysql));
258 if (!is_real_connect) {
259 /* free mysql structure */
260 mysqli_close(mysql->mysql, MYSQLI_CLOSE_DISCONNECTED);
261 mysql->mysql = NULL;
262 }
263 goto err;
264 }
265
266 /* clear error */
267 php_mysqli_set_error(mysql_errno(mysql->mysql), (char *) mysql_error(mysql->mysql));
268
269 unsigned int allow_local_infile = MyG(allow_local_infile);
270 mysql_options(mysql->mysql, MYSQL_OPT_LOCAL_INFILE, (char *)&allow_local_infile);
271
272 if (MyG(local_infile_directory) && !php_check_open_basedir(MyG(local_infile_directory))) {
273 mysql_options(mysql->mysql, MYSQL_OPT_LOAD_DATA_LOCAL_DIR, MyG(local_infile_directory));
274 }
275
276 end:
277 if (!mysqli_resource) {
278 mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
279 mysqli_resource->ptr = (void *)mysql;
280 }
281 mysqli_resource->status = MYSQLI_STATUS_VALID;
282
283 /* store persistent connection */
284 if (persistent && (new_connection || is_real_connect)) {
285 MyG(num_active_persistent)++;
286 }
287
288 MyG(num_links)++;
289
290 mysql->multi_query = 0;
291
292 if (object) {
293 ZEND_ASSERT(instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry));
294 (Z_MYSQLI_P(object))->ptr = mysqli_resource;
295 RETURN_TRUE;
296 } else {
297 MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_link_class_entry);
298 return;
299 }
300
301 err:
302 if (mysql->hash_key) {
303 zend_string_release_ex(mysql->hash_key, 0);
304 mysql->hash_key = NULL;
305 mysql->persistent = false;
306 }
307 if (!is_real_connect && self_alloced) {
308 efree(mysql);
309 }
310 RETVAL_FALSE;
311 } /* }}} */
312
313 /* {{{ Open a connection to a mysql server */
314 PHP_FUNCTION(mysqli_connect)
315 {
316 mysqli_common_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, false, false);
317 }
318 /* }}} */
319
320 PHP_METHOD(mysqli, __construct)
321 {
322 mysqli_common_connect(INTERNAL_FUNCTION_PARAM_PASSTHRU, false, true);
323 }
324
325 /* {{{ Initialize mysqli and return a resource for use with mysql_real_connect */
326 PHP_METHOD(mysqli, init)
327 {
328 php_mysqli_init(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
329 }
330 /* }}} */
331
332 /* {{{ Returns the numerical value of the error message from last connect command */
333 PHP_FUNCTION(mysqli_connect_errno)
334 {
335 if (zend_parse_parameters_none() == FAILURE) {
336 RETURN_THROWS();
337 }
338
339 RETURN_LONG(MyG(error_no));
340 }
341 /* }}} */
342
343 /* {{{ Returns the text of the error message from previous MySQL operation */
344 PHP_FUNCTION(mysqli_connect_error)
345 {
346 if (zend_parse_parameters_none() == FAILURE) {
347 RETURN_THROWS();
348 }
349
350 if (MyG(error_msg)) {
351 RETURN_STRING(MyG(error_msg));
352 } else {
353 RETURN_NULL();
354 }
355 }
356 /* }}} */
357
358 /* {{{ Fetch a result row as an associative array, a numeric array, or both */
359 PHP_FUNCTION(mysqli_fetch_array)
360 {
361 php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0, 0);
362 }
363 /* }}} */
364
365 /* {{{ Fetch a result row as an associative array */
366 PHP_FUNCTION(mysqli_fetch_assoc)
367 {
368 php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 0);
369 }
370 /* }}} */
371
372 /* {{{ Fetch a column from the result row */
373 PHP_FUNCTION(mysqli_fetch_column)
374 {
375 MYSQL_RES *result;
376 zval *mysql_result;
377 zval row_array;
378 zend_long col_no = 0;
379
380 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &col_no) == FAILURE) {
381 RETURN_THROWS();
382 }
383 MYSQLI_FETCH_RESOURCE(result, MYSQL_RES*, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
384
385 if (col_no < 0) {
386 zend_argument_value_error(ERROR_ARG_POS(2), "must be greater than or equal to 0");
387 RETURN_THROWS();
388 }
389 if (col_no >= mysql_num_fields(result)) {
390 zend_argument_value_error(ERROR_ARG_POS(2), "must be less than the number of fields for this result set");
391 RETURN_THROWS();
392 }
393
394 php_mysqli_fetch_into_hash_aux(&row_array, result, MYSQLI_NUM);
395 if (Z_TYPE(row_array) != IS_ARRAY) {
396 zval_ptr_dtor_nogc(&row_array);
397 RETURN_FALSE;
398 }
399
400 ZVAL_COPY(return_value, zend_hash_index_find(Z_ARR(row_array), col_no));
401 zval_ptr_dtor_nogc(&row_array);
402 }
403 /* }}} */
404
405 /* {{{ Fetches all result rows as an associative array, a numeric array, or both */
406 PHP_FUNCTION(mysqli_fetch_all)
407 {
408 MYSQL_RES *result;
409 zval *mysql_result;
410 zend_long mode = MYSQLI_NUM;
411
412 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|l", &mysql_result, mysqli_result_class_entry, &mode) == FAILURE) {
413 RETURN_THROWS();
414 }
415 MYSQLI_FETCH_RESOURCE(result, MYSQL_RES *, mysql_result, "mysqli_result", MYSQLI_STATUS_VALID);
416
417 if (!mode || (mode & ~MYSQLI_BOTH)) {
418 zend_argument_value_error(ERROR_ARG_POS(2), "must be one of MYSQLI_NUM, MYSQLI_ASSOC, or MYSQLI_BOTH");
419 RETURN_THROWS();
420 }
421
422 array_init_size(return_value, mysql_num_rows(result));
423
424 zend_ulong i = 0;
425 do {
426 zval row;
427 php_mysqli_fetch_into_hash_aux(&row, result, mode);
428 if (Z_TYPE(row) != IS_ARRAY) {
429 zval_ptr_dtor_nogc(&row);
430 break;
431 }
432 add_index_zval(return_value, i++, &row);
433 } while (1);
434 }
435 /* }}} */
436
437 /* {{{ Returns statistics about the zval cache */
438 PHP_FUNCTION(mysqli_get_client_stats)
439 {
440 if (zend_parse_parameters_none() == FAILURE) {
441 RETURN_THROWS();
442 }
443 mysqlnd_get_client_stats(return_value);
444 }
445 /* }}} */
446
447 /* {{{ Returns statistics about the zval cache */
448 PHP_FUNCTION(mysqli_get_connection_stats)
449 {
450 MY_MYSQL *mysql;
451 zval *mysql_link;
452
453 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O",
454 &mysql_link, mysqli_link_class_entry) == FAILURE) {
455 RETURN_THROWS();
456 }
457 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
458
459 mysqlnd_get_connection_stats(mysql->mysql, return_value);
460 }
461 /* }}} */
462
463 /* {{{ Fetches all client errors */
464 PHP_FUNCTION(mysqli_error_list)
465 {
466 MY_MYSQL *mysql;
467 zval *mysql_link;
468
469 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
470 RETURN_THROWS();
471 }
472 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
473
474 MYSQLND_ERROR_LIST_ELEMENT * message;
475 zend_llist_position pos;
476 array_init(return_value);
477 for (message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_first_ex(&mysql->mysql->data->error_info->error_list, &pos);
478 message;
479 message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_next_ex(&mysql->mysql->data->error_info->error_list, &pos))
480 {
481 zval single_error;
482 array_init(&single_error);
483 add_assoc_long_ex(&single_error, "errno", sizeof("errno") - 1, message->error_no);
484 add_assoc_string_ex(&single_error, "sqlstate", sizeof("sqlstate") - 1, message->sqlstate);
485 add_assoc_string_ex(&single_error, "error", sizeof("error") - 1, message->error);
486 add_next_index_zval(return_value, &single_error);
487 }
488 }
489 /* }}} */
490
491 /* {{{ */
492 PHP_FUNCTION(mysqli_stmt_error_list)
493 {
494 MY_STMT *stmt;
495 zval *mysql_stmt;
496
497 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
498 RETURN_THROWS();
499 }
500 MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_INITIALIZED);
501 if (stmt->stmt && stmt->stmt->data && stmt->stmt->data->error_info) {
502 MYSQLND_ERROR_LIST_ELEMENT * message;
503 zend_llist_position pos;
504 array_init(return_value);
505 for (message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_first_ex(&stmt->stmt->data->error_info->error_list, &pos);
506 message;
507 message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_next_ex(&stmt->stmt->data->error_info->error_list, &pos))
508 {
509 zval single_error;
510 array_init(&single_error);
511 add_assoc_long_ex(&single_error, "errno", sizeof("errno") - 1, message->error_no);
512 add_assoc_string_ex(&single_error, "sqlstate", sizeof("sqlstate") - 1, message->sqlstate);
513 add_assoc_string_ex(&single_error, "error", sizeof("error") - 1, message->error);
514 add_next_index_zval(return_value, &single_error);
515 }
516 } else {
517 RETURN_EMPTY_ARRAY();
518 }
519 }
520 /* }}} */
521
522 /* {{{ Fetch a result row as an object */
523 PHP_FUNCTION(mysqli_fetch_object)
524 {
525 php_mysqli_fetch_into_hash(INTERNAL_FUNCTION_PARAM_PASSTHRU, MYSQLI_ASSOC, 1);
526 }
527 /* }}} */
528
529 /* {{{ allows to execute multiple queries */
530 PHP_FUNCTION(mysqli_multi_query)
531 {
532 MY_MYSQL *mysql;
533 zval *mysql_link;
534 char *query = NULL;
535 size_t query_len;
536
537 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &query, &query_len) == FAILURE) {
538 RETURN_THROWS();
539 }
540 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
541
542 MYSQLI_ENABLE_MQ;
543 if (mysql_real_query(mysql->mysql, query, query_len)) {
544 MYSQLND_ERROR_INFO error_info = *mysql->mysql->data->error_info;
545 mysql->mysql->data->error_info->error_list.head = NULL;
546 mysql->mysql->data->error_info->error_list.tail = NULL;
547 mysql->mysql->data->error_info->error_list.count = 0;
548 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
549 MYSQLI_DISABLE_MQ;
550
551 zend_llist_clean(&mysql->mysql->data->error_info->error_list);
552 *mysql->mysql->data->error_info = error_info;
553 RETURN_FALSE;
554 }
555 RETURN_TRUE;
556 }
557 /* }}} */
558
559 /* {{{ */
560 PHP_FUNCTION(mysqli_query)
561 {
562 MY_MYSQL *mysql;
563 zval *mysql_link;
564 MYSQLI_RESOURCE *mysqli_resource;
565 MYSQL_RES *result = NULL;
566 char *query = NULL;
567 size_t query_len;
568 zend_long resultmode = MYSQLI_STORE_RESULT;
569
570 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os|l", &mysql_link, mysqli_link_class_entry, &query, &query_len, &resultmode) == FAILURE) {
571 RETURN_THROWS();
572 }
573
574 if (!query_len) {
575 zend_argument_must_not_be_empty_error(ERROR_ARG_POS(2));
576 RETURN_THROWS();
577 }
578 if ((resultmode & ~MYSQLI_ASYNC) != MYSQLI_USE_RESULT &&
579 MYSQLI_STORE_RESULT != (resultmode & ~(MYSQLI_ASYNC | MYSQLI_STORE_RESULT_COPY_DATA))
580 ) {
581 zend_argument_value_error(ERROR_ARG_POS(3), "must be either MYSQLI_USE_RESULT or MYSQLI_STORE_RESULT with MYSQLI_ASYNC as an optional bitmask flag");
582 RETURN_THROWS();
583 }
584
585 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
586
587 MYSQLI_DISABLE_MQ;
588
589
590 if (resultmode & MYSQLI_ASYNC) {
591 if (mysqli_async_query(mysql->mysql, query, query_len)) {
592 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
593 RETURN_FALSE;
594 }
595 mysql->async_result_fetch_type = resultmode & ~MYSQLI_ASYNC;
596 RETURN_TRUE;
597 }
598
599 if (mysql_real_query(mysql->mysql, query, query_len)) {
600 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
601 RETURN_FALSE;
602 }
603
604 if (!mysql_field_count(mysql->mysql)) {
605 /* no result set - not a SELECT */
606 if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
607 php_mysqli_report_index(query, mysqli_server_status(mysql->mysql));
608 }
609 RETURN_TRUE;
610 }
611
612 switch (resultmode & ~(MYSQLI_ASYNC | MYSQLI_STORE_RESULT_COPY_DATA)) {
613 case MYSQLI_STORE_RESULT:
614 result = mysql_store_result(mysql->mysql);
615 break;
616 case MYSQLI_USE_RESULT:
617 result = mysql_use_result(mysql->mysql);
618 break;
619 }
620 if (!result) {
621 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
622 RETURN_FALSE;
623 }
624
625 if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
626 php_mysqli_report_index(query, mysqli_server_status(mysql->mysql));
627 }
628
629 mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
630 mysqli_resource->ptr = (void *)result;
631 mysqli_resource->status = MYSQLI_STATUS_VALID;
632 MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_result_class_entry);
633 }
634 /* }}} */
635
636 #include "php_network.h"
637 /* {{{ mysqlnd_zval_array_to_mysqlnd_array functions */
638 static zend_result mysqlnd_zval_array_to_mysqlnd_array(zval *in_array, MYSQLND ***out_array)
639 {
640 zval *elem;
641 int i = 0, current = 0;
642
643 ZEND_ASSERT(Z_TYPE_P(in_array) == IS_ARRAY);
644 *out_array = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(in_array)) + 1, sizeof(MYSQLND *));
645 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(in_array), elem) {
646 i++;
647 if (Z_TYPE_P(elem) != IS_OBJECT ||
648 !instanceof_function(Z_OBJCE_P(elem), mysqli_link_class_entry)) {
649 zend_argument_type_error(i, "must be an instance of mysqli, %s given", zend_zval_value_name(elem));
650 return FAILURE;
651 } else {
652 MY_MYSQL *mysql;
653 MYSQLI_RESOURCE *my_res;
654 mysqli_object *intern = Z_MYSQLI_P(elem);
655 if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
656 zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));
657 return FAILURE;
658 }
659 mysql = (MY_MYSQL*) my_res->ptr;
660 if (my_res->status < MYSQLI_STATUS_VALID) {
661 zend_throw_error(NULL, "%s object is not fully initialized", ZSTR_VAL(intern->zo.ce->name));
662 return FAILURE;
663 }
664 (*out_array)[current++] = mysql->mysql;
665 }
666 } ZEND_HASH_FOREACH_END();
667 return SUCCESS;
668 }
669 /* }}} */
670
671 /* {{{ mysqlnd_zval_array_from_mysqlnd_array */
672 static zend_result mysqlnd_zval_array_from_mysqlnd_array(MYSQLND **in_array, zval *out_array)
673 {
674 MYSQLND **p = in_array;
675 zval dest_array;
676 zval *elem, *dest_elem;
677
678 array_init_size(&dest_array, zend_hash_num_elements(Z_ARRVAL_P(out_array)));
679
680 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(out_array), elem) {
681 if (Z_TYPE_P(elem) != IS_OBJECT ||
682 !instanceof_function(Z_OBJCE_P(elem), mysqli_link_class_entry)) {
683 continue;
684 }
685 {
686 MY_MYSQL *mysql;
687 MYSQLI_RESOURCE *my_res;
688 mysqli_object *intern = Z_MYSQLI_P(elem);
689 if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {
690 zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(intern->zo.ce->name));
691 return FAILURE;
692 }
693 mysql = (MY_MYSQL *) my_res->ptr;
694 if (mysql->mysql == *p) {
695 dest_elem = zend_hash_next_index_insert(Z_ARRVAL(dest_array), elem);
696 if (dest_elem) {
697 zval_add_ref(dest_elem);
698 }
699 p++;
700 }
701 }
702 } ZEND_HASH_FOREACH_END();
703
704 /* destroy old array and add new one */
705 zval_ptr_dtor(out_array);
706 ZVAL_COPY_VALUE(out_array, &dest_array);
707
708 return SUCCESS;
709 }
710 /* }}} */
711
712 /* {{{ mysqlnd_dont_poll_zval_array_from_mysqlnd_array */
713 static void mysqlnd_dont_poll_zval_array_from_mysqlnd_array(MYSQLND **in_array, zval *in_zval_array, zval *out_array)
714 {
715 MYSQLND **p = in_array;
716 zval proxy, *elem, *dest_elem;
717
718 array_init(&proxy);
719 if (in_array) {
720 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(in_zval_array), elem) {
721 MY_MYSQL *mysql;
722 mysqli_object *intern = Z_MYSQLI_P(elem);
723 mysql = (MY_MYSQL *)((MYSQLI_RESOURCE *)intern->ptr)->ptr;
724 if (mysql->mysql == *p) {
725 dest_elem = zend_hash_next_index_insert(Z_ARRVAL(proxy), elem);
726 if (dest_elem) {
727 zval_add_ref(dest_elem);
728 }
729 p++;
730 }
731 } ZEND_HASH_FOREACH_END();
732 }
733
734 /* destroy old array and add new one */
735 zval_ptr_dtor(out_array);
736 ZVAL_COPY_VALUE(out_array, &proxy);
737 }
738 /* }}} */
739
740 /* {{{ Poll connections */
741 PHP_FUNCTION(mysqli_poll)
742 {
743 zval *r_array, *e_array, *dont_poll_array;
744 MYSQLND **new_r_array = NULL, **new_e_array = NULL, **new_dont_poll_array = NULL;
745 zend_long sec = 0, usec = 0;
746 enum_func_status ret;
747 int desc_num;
748
749 if (zend_parse_parameters(ZEND_NUM_ARGS(), "a!a!al|l", &r_array, &e_array, &dont_poll_array, &sec, &usec) == FAILURE) {
750 RETURN_THROWS();
751 }
752 if (sec < 0) {
753 zend_argument_value_error(4, "must be greater than or equal to 0");
754 RETURN_THROWS();
755 }
756 if (usec < 0) {
757 zend_argument_value_error(5, "must be greater than or equal to 0");
758 RETURN_THROWS();
759 }
760
761 if (!r_array && !e_array) {
762 zend_value_error("No stream arrays were passed");
763 RETURN_THROWS();
764 }
765
766 if (r_array != NULL) {
767 if (mysqlnd_zval_array_to_mysqlnd_array(r_array, &new_r_array) == FAILURE) {
768 efree(new_r_array);
769 RETURN_THROWS();
770 }
771 }
772 if (e_array != NULL) {
773 if (mysqlnd_zval_array_to_mysqlnd_array(e_array, &new_e_array) == FAILURE) {
774 efree(new_e_array);
775 efree(new_r_array);
776 RETURN_THROWS();
777 }
778 }
779
780 ret = mysqlnd_poll(new_r_array, new_e_array, &new_dont_poll_array, sec, usec, &desc_num);
781
782 mysqlnd_dont_poll_zval_array_from_mysqlnd_array(r_array != NULL ? new_dont_poll_array:NULL, r_array, dont_poll_array);
783
784 if (r_array != NULL) {
785 mysqlnd_zval_array_from_mysqlnd_array(new_r_array, r_array);
786 }
787 if (e_array != NULL) {
788 mysqlnd_zval_array_from_mysqlnd_array(new_e_array, e_array);
789 }
790
791 if (new_dont_poll_array) {
792 efree(new_dont_poll_array);
793 }
794 if (new_r_array) {
795 efree(new_r_array);
796 }
797 if (new_e_array) {
798 efree(new_e_array);
799 }
800 if (ret == PASS) {
801 RETURN_LONG(desc_num);
802 } else {
803 RETURN_FALSE;
804 }
805 }
806 /* }}} */
807
808 /* {{{ Poll connections */
809 PHP_FUNCTION(mysqli_reap_async_query)
810 {
811 MY_MYSQL *mysql;
812 zval *mysql_link;
813 MYSQLI_RESOURCE *mysqli_resource;
814 MYSQL_RES *result = NULL;
815
816 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
817 RETURN_THROWS();
818 }
819
820 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
821
822 if (FAIL == mysqlnd_reap_async_query(mysql->mysql)) {
823 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
824 RETURN_FALSE;
825 }
826
827 if (!mysql_field_count(mysql->mysql)) {
828 /* no result set - not a SELECT */
829 if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
830 /* php_mysqli_report_index("n/a", mysqli_server_status(mysql->mysql)); */
831 }
832 RETURN_TRUE;
833 }
834
835 switch (mysql->async_result_fetch_type) {
836 case MYSQLI_STORE_RESULT:
837 result = mysql_store_result(mysql->mysql);
838 break;
839 case MYSQLI_USE_RESULT:
840 result = mysql_use_result(mysql->mysql);
841 break;
842 }
843
844 if (!result) {
845 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
846 RETURN_FALSE;
847 }
848
849 if (MyG(report_mode) & MYSQLI_REPORT_INDEX) {
850 /* php_mysqli_report_index("n/a", mysqli_server_status(mysql->mysql)); */
851 }
852
853 mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
854 mysqli_resource->ptr = (void *)result;
855 mysqli_resource->status = MYSQLI_STATUS_VALID;
856 MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_result_class_entry);
857 }
858 /* }}} */
859
860 /* {{{ Buffer result set on client */
861 PHP_FUNCTION(mysqli_stmt_get_result)
862 {
863 MYSQL_RES *result;
864 MYSQLI_RESOURCE *mysqli_resource;
865 MY_STMT *stmt;
866 zval *mysql_stmt;
867
868 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_stmt, mysqli_stmt_class_entry) == FAILURE) {
869 RETURN_THROWS();
870 }
871 MYSQLI_FETCH_RESOURCE_STMT(stmt, mysql_stmt, MYSQLI_STATUS_VALID);
872
873 if (!(result = mysqlnd_stmt_get_result(stmt->stmt))) {
874 MYSQLI_REPORT_STMT_ERROR(stmt->stmt);
875 RETURN_FALSE;
876 }
877
878 mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
879 mysqli_resource->ptr = (void *)result;
880 mysqli_resource->status = MYSQLI_STATUS_VALID;
881 MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_result_class_entry);
882 }
883 /* }}} */
884
885 /* {{{ */
886 PHP_FUNCTION(mysqli_get_warnings)
887 {
888 MY_MYSQL *mysql;
889 zval *mysql_link;
890 MYSQLI_RESOURCE *mysqli_resource;
891 MYSQLI_WARNING *w = NULL;
892
893 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
894 RETURN_THROWS();
895 }
896 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
897
898 if (mysql_warning_count(mysql->mysql)) {
899 w = php_get_warnings(mysql->mysql->data);
900 }
901 if (!w) {
902 RETURN_FALSE;
903 }
904 mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
905 mysqli_resource->ptr = mysqli_resource->info = (void *)w;
906 mysqli_resource->status = MYSQLI_STATUS_VALID;
907 MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
908 }
909 /* }}} */
910
911 /* {{{ */
912 PHP_FUNCTION(mysqli_stmt_get_warnings)
913 {
914 MY_STMT *stmt;
915 zval *stmt_link;
916 MYSQLI_RESOURCE *mysqli_resource;
917 MYSQLI_WARNING *w = NULL;
918
919 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &stmt_link, mysqli_stmt_class_entry) == FAILURE) {
920 RETURN_THROWS();
921 }
922 MYSQLI_FETCH_RESOURCE_STMT(stmt, stmt_link, MYSQLI_STATUS_VALID);
923
924 if (mysqli_stmt_warning_count(stmt->stmt)) {
925 w = php_get_warnings(mysqli_stmt_get_connection(stmt->stmt));
926 }
927 if (!w) {
928 RETURN_FALSE;
929 }
930 mysqli_resource = (MYSQLI_RESOURCE *)ecalloc (1, sizeof(MYSQLI_RESOURCE));
931 mysqli_resource->ptr = mysqli_resource->info = (void *)w;
932 mysqli_resource->status = MYSQLI_STATUS_VALID;
933 MYSQLI_RETVAL_RESOURCE(mysqli_resource, mysqli_warning_class_entry);
934 }
935 /* }}} */
936
937 /* {{{ sets client character set */
938 PHP_FUNCTION(mysqli_set_charset)
939 {
940 MY_MYSQL *mysql;
941 zval *mysql_link;
942 char *cs_name;
943 size_t csname_len;
944
945 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &cs_name, &csname_len) == FAILURE) {
946 RETURN_THROWS();
947 }
948 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
949
950 if (mysql_set_character_set(mysql->mysql, cs_name)) {
951 MYSQLI_REPORT_MYSQL_ERROR(mysql->mysql);
952 RETURN_FALSE;
953 }
954 RETURN_TRUE;
955 }
956 /* }}} */
957
958 /* {{{ returns a character set object */
959 PHP_FUNCTION(mysqli_get_charset)
960 {
961 MY_MYSQL *mysql;
962 zval *mysql_link;
963 const char *name = NULL, *collation = NULL, *dir = NULL, *comment = NULL;
964 uint32_t minlength, maxlength, number, state;
965 const MYSQLND_CHARSET *cs;
966
967 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O", &mysql_link, mysqli_link_class_entry) == FAILURE) {
968 RETURN_THROWS();
969 }
970 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
971
972
973 cs = mysql->mysql->data->charset;
974 if (!cs) {
975 php_error_docref(NULL, E_WARNING, "The connection has no charset associated");
976 RETURN_NULL();
977 }
978 name = cs->name;
979 collation = cs->collation;
980 minlength = cs->char_minlen;
981 maxlength = cs->char_maxlen;
982 number = cs->nr;
983 comment = cs->comment;
984 state = 1; /* all charsets are compiled in */
985 object_init(return_value);
986
987 add_property_string(return_value, "charset", (name) ? (char *)name : "");
988 add_property_string(return_value, "collation",(collation) ? (char *)collation : "");
989 add_property_string(return_value, "dir", (dir) ? (char *)dir : "");
990 add_property_long(return_value, "min_length", minlength);
991 add_property_long(return_value, "max_length", maxlength);
992 add_property_long(return_value, "number", number);
993 add_property_long(return_value, "state", state);
994 add_property_string(return_value, "comment", (comment) ? (char *)comment : "");
995 }
996 /* }}} */
997
998 /* {{{ Starts a transaction */
999 PHP_FUNCTION(mysqli_begin_transaction)
1000 {
1001 MY_MYSQL *mysql;
1002 zval *mysql_link;
1003 zend_long flags = TRANS_START_NO_OPT;
1004 char * name = NULL;
1005 size_t name_len = 0;
1006
1007 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "O|ls!", &mysql_link, mysqli_link_class_entry, &flags, &name, &name_len) == FAILURE) {
1008 RETURN_THROWS();
1009 }
1010 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1011 if (flags < 0) {
1012 zend_argument_value_error(ERROR_ARG_POS(2), "must be one of the MYSQLI_TRANS_* constants");
1013 RETURN_THROWS();
1014 }
1015 if (name && !name_len) {
1016 zend_argument_must_not_be_empty_error(ERROR_ARG_POS(3));
1017 RETURN_THROWS();
1018 }
1019
1020 if (FAIL == mysqlnd_begin_transaction(mysql->mysql, flags, name)) {
1021 RETURN_FALSE;
1022 }
1023 RETURN_TRUE;
1024 }
1025 /* }}} */
1026
1027 /* {{{ Starts a transaction */
1028 PHP_FUNCTION(mysqli_savepoint)
1029 {
1030 MY_MYSQL *mysql;
1031 zval *mysql_link;
1032 char * name = NULL;
1033 size_t name_len;
1034
1035 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &name, &name_len) == FAILURE) {
1036 RETURN_THROWS();
1037 }
1038 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1039 if (name_len == 0) {
1040 zend_argument_must_not_be_empty_error(ERROR_ARG_POS(2));
1041 RETURN_THROWS();
1042 }
1043
1044 if (FAIL == mysqlnd_savepoint(mysql->mysql, name)) {
1045 RETURN_FALSE;
1046 }
1047 RETURN_TRUE;
1048 }
1049 /* }}} */
1050
1051 /* {{{ Starts a transaction */
1052 PHP_FUNCTION(mysqli_release_savepoint)
1053 {
1054 MY_MYSQL *mysql;
1055 zval *mysql_link;
1056 char * name = NULL;
1057 size_t name_len;
1058
1059 if (zend_parse_method_parameters(ZEND_NUM_ARGS(), getThis(), "Os", &mysql_link, mysqli_link_class_entry, &name, &name_len) == FAILURE) {
1060 RETURN_THROWS();
1061 }
1062 MYSQLI_FETCH_RESOURCE_CONN(mysql, mysql_link, MYSQLI_STATUS_VALID);
1063 if (name_len == 0) {
1064 zend_argument_must_not_be_empty_error(ERROR_ARG_POS(2));
1065 RETURN_THROWS();
1066 }
1067 if (FAIL == mysqlnd_release_savepoint(mysql->mysql, name)) {
1068 RETURN_FALSE;
1069 }
1070 RETURN_TRUE;
1071 }
1072 /* }}} */
1073
1074 /* {{{ Returns information about open and cached links */
1075 PHP_FUNCTION(mysqli_get_links_stats)
1076 {
1077 if (zend_parse_parameters_none() == FAILURE) {
1078 RETURN_THROWS();
1079 }
1080
1081 array_init(return_value);
1082 add_assoc_long_ex(return_value, "total", sizeof("total") - 1, MyG(num_links));
1083 add_assoc_long_ex(return_value, "active_plinks", sizeof("active_plinks") - 1, MyG(num_active_persistent));
1084 add_assoc_long_ex(return_value, "cached_plinks", sizeof("cached_plinks") - 1, MyG(num_inactive_persistent));
1085 }
1086 /* }}} */
1087