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 | Author: Georg Richter <georg@php.net> |
14 | Andrey Hristov <andrey@php.net> |
15 +----------------------------------------------------------------------+
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include <signal.h>
23
24 #include "php.h"
25 #include "php_ini.h"
26 #include "ext/standard/info.h"
27 #include "php_mysqli_structs.h"
28 #include "mysqli_priv.h"
29
30 #define CHECK_STATUS(value, quiet) \
31 if (!obj->ptr || ((MYSQLI_RESOURCE *)obj->ptr)->status < value ) { \
32 if (!quiet) { \
33 zend_throw_error(NULL, "Property access is not allowed yet"); \
34 } \
35 return FAILURE; \
36 } \
37
38 #define MYSQLI_GET_MYSQL(statusval) \
39 MYSQL *p; \
40 if (!obj->ptr || !(MY_MYSQL *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr) { \
41 if (!quiet) { \
42 zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name)); \
43 } \
44 return FAILURE; \
45 } else { \
46 CHECK_STATUS(statusval, quiet);\
47 p = (MYSQL *)((MY_MYSQL *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr)->mysql;\
48 }
49
50 #define MYSQLI_GET_RESULT(statusval) \
51 MYSQL_RES *p; \
52 if (!obj->ptr) { \
53 if (!quiet) { \
54 zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name)); \
55 } \
56 return FAILURE; \
57 } else { \
58 CHECK_STATUS(statusval, quiet);\
59 p = (MYSQL_RES *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr; \
60 }
61
62 #define MYSQLI_GET_STMT(statusval) \
63 MYSQL_STMT *p; \
64 if (!obj->ptr) { \
65 if (!quiet) { \
66 zend_throw_error(NULL, "%s object is already closed", ZSTR_VAL(obj->zo.ce->name)); \
67 } \
68 return FAILURE; \
69 } else { \
70 CHECK_STATUS(statusval, quiet); \
71 p = (MYSQL_STMT *)((MY_STMT *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr)->stmt; \
72 }
73
74 #define MYSQLI_MAP_PROPERTY_FUNC_LONG_OR_STR( __func, __int_func, __get_type, __ret_type, __ret_type_sprint_mod)\
75 static int __func(mysqli_object *obj, zval *retval, bool quiet) \
76 {\
77 __ret_type l;\
78 __get_type;\
79 ZEND_ASSERT(p);\
80 l = (__ret_type)__int_func(p);\
81 if (l < ZEND_LONG_MAX) {\
82 ZVAL_LONG(retval, (zend_long) l);\
83 } else { \
84 ZVAL_NEW_STR(retval, strpprintf(0, __ret_type_sprint_mod, l)); \
85 } \
86 return SUCCESS; \
87 }
88
89 #define MYSQLI_MAP_PROPERTY_FUNC_LONG( __func, __int_func, __get_type, __ret_type, __ret_type_sprint_mod)\
90 static int __func(mysqli_object *obj, zval *retval, bool quiet) \
91 {\
92 __ret_type l;\
93 __get_type;\
94 ZEND_ASSERT(p);\
95 l = (__ret_type)__int_func(p);\
96 ZEND_ASSERT(l < ZEND_LONG_MAX);\
97 ZVAL_LONG(retval, (zend_long) l);\
98 return SUCCESS; \
99 }
100
101 #define MYSQLI_MAP_PROPERTY_FUNC_STR_OR_NULL(__func, __int_func, __get_type)\
102 static int __func(mysqli_object *obj, zval *retval, bool quiet)\
103 {\
104 char *c;\
105 __get_type;\
106 ZEND_ASSERT(p);\
107 c = (char *)__int_func(p);\
108 if (c) {\
109 ZVAL_STRING(retval, c);\
110 } else {\
111 ZVAL_NULL(retval);\
112 }\
113 return SUCCESS; \
114 }
115
116 #define MYSQLI_MAP_PROPERTY_FUNC_STR(__func, __int_func, __get_type)\
117 static int __func(mysqli_object *obj, zval *retval, bool quiet)\
118 {\
119 char *c;\
120 __get_type;\
121 ZEND_ASSERT(p);\
122 c = (char *)__int_func(p);\
123 ZEND_ASSERT(c);\
124 ZVAL_STRING(retval, c);\
125 return SUCCESS; \
126 }
127
128 /* {{{ property link_client_version_read */
link_client_version_read(mysqli_object * obj,zval * retval,bool quiet)129 static int link_client_version_read(mysqli_object *obj, zval *retval, bool quiet)
130 {
131 ZVAL_LONG(retval, MYSQL_VERSION_ID);
132
133 return SUCCESS;
134 }
135 /* }}} */
136
137 /* {{{ property link_client_info_read */
link_client_info_read(mysqli_object * obj,zval * retval,bool quiet)138 static int link_client_info_read(mysqli_object *obj, zval *retval, bool quiet)
139 {
140 ZVAL_STRING(retval, MYSQL_SERVER_VERSION);
141
142 return SUCCESS;
143 }
144 /* }}} */
145
146 /* {{{ property link_connect_errno_read */
link_connect_errno_read(mysqli_object * obj,zval * retval,bool quiet)147 static int link_connect_errno_read(mysqli_object *obj, zval *retval, bool quiet)
148 {
149 ZVAL_LONG(retval, (zend_long)MyG(error_no));
150
151 return SUCCESS;
152 }
153 /* }}} */
154
155 /* {{{ property link_connect_error_read */
link_connect_error_read(mysqli_object * obj,zval * retval,bool quiet)156 static int link_connect_error_read(mysqli_object *obj, zval *retval, bool quiet)
157 {
158 if (MyG(error_msg)) {
159 ZVAL_STRING(retval, MyG(error_msg));
160 } else {
161 ZVAL_NULL(retval);
162 }
163
164 return SUCCESS;
165 }
166 /* }}} */
167
168 /* {{{ property link_affected_rows_read */
link_affected_rows_read(mysqli_object * obj,zval * retval,bool quiet)169 static int link_affected_rows_read(mysqli_object *obj, zval *retval, bool quiet)
170 {
171 MY_MYSQL *mysql;
172 my_ulonglong rc;
173
174 CHECK_STATUS(MYSQLI_STATUS_VALID, quiet);
175
176 mysql = (MY_MYSQL *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
177 ZEND_ASSERT(mysql);
178
179 rc = mysql_affected_rows(mysql->mysql);
180
181 if (rc == (my_ulonglong) -1) {
182 ZVAL_LONG(retval, -1);
183 return SUCCESS;
184 }
185
186 if (rc < ZEND_LONG_MAX) {
187 ZVAL_LONG(retval, (zend_long) rc);
188 } else {
189 ZVAL_NEW_STR(retval, strpprintf(0, MYSQLI_LLU_SPEC, rc));
190 }
191
192 return SUCCESS;
193 }
194 /* }}} */
195
196 /* {{{ property link_error_list_read */
link_error_list_read(mysqli_object * obj,zval * retval,bool quiet)197 static int link_error_list_read(mysqli_object *obj, zval *retval, bool quiet)
198 {
199 MY_MYSQL *mysql;
200
201 CHECK_STATUS(MYSQLI_STATUS_VALID, quiet);
202
203 mysql = (MY_MYSQL *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
204
205 if (mysql) {
206 array_init(retval);
207 MYSQLND_ERROR_LIST_ELEMENT * message;
208 zend_llist_position pos;
209 for (message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_first_ex(&mysql->mysql->data->error_info->error_list, &pos);
210 message;
211 message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_next_ex(&mysql->mysql->data->error_info->error_list, &pos))
212 {
213 zval single_error;
214 array_init(&single_error);
215 add_assoc_long_ex(&single_error, "errno", sizeof("errno") - 1, message->error_no);
216 add_assoc_string_ex(&single_error, "sqlstate", sizeof("sqlstate") - 1, message->sqlstate);
217 add_assoc_string_ex(&single_error, "error", sizeof("error") - 1, message->error);
218 add_next_index_zval(retval, &single_error);
219 }
220 } else {
221 ZVAL_EMPTY_ARRAY(retval);
222 }
223
224 return SUCCESS;
225 }
226 /* }}} */
227
228 /* link properties */
MYSQLI_MAP_PROPERTY_FUNC_LONG(link_errno_read,mysql_errno,MYSQLI_GET_MYSQL (MYSQLI_STATUS_INITIALIZED),zend_ulong,ZEND_ULONG_FMT)229 MYSQLI_MAP_PROPERTY_FUNC_LONG(link_errno_read, mysql_errno, MYSQLI_GET_MYSQL(MYSQLI_STATUS_INITIALIZED), zend_ulong, ZEND_ULONG_FMT)
230 MYSQLI_MAP_PROPERTY_FUNC_STR(link_error_read, mysql_error, MYSQLI_GET_MYSQL(MYSQLI_STATUS_INITIALIZED))
231 MYSQLI_MAP_PROPERTY_FUNC_LONG(link_field_count_read, mysql_field_count, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID), zend_ulong, ZEND_ULONG_FMT)
232 MYSQLI_MAP_PROPERTY_FUNC_STR(link_host_info_read, mysql_get_host_info, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID))
233 MYSQLI_MAP_PROPERTY_FUNC_STR_OR_NULL(link_info_read, mysql_info, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID))
234 MYSQLI_MAP_PROPERTY_FUNC_LONG_OR_STR(link_insert_id_read, mysql_insert_id, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID), my_ulonglong, MYSQLI_LLU_SPEC)
235 MYSQLI_MAP_PROPERTY_FUNC_LONG(link_protocol_version_read, mysql_get_proto_info, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID), zend_ulong, ZEND_ULONG_FMT)
236 MYSQLI_MAP_PROPERTY_FUNC_STR(link_server_info_read, mysql_get_server_info, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID))
237 MYSQLI_MAP_PROPERTY_FUNC_LONG(link_server_version_read, mysql_get_server_version, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID), zend_ulong, ZEND_ULONG_FMT)
238 MYSQLI_MAP_PROPERTY_FUNC_STR(link_sqlstate_read, mysql_sqlstate, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID))
239 MYSQLI_MAP_PROPERTY_FUNC_LONG(link_thread_id_read, mysql_thread_id, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID), zend_ulong, ZEND_ULONG_FMT)
240 MYSQLI_MAP_PROPERTY_FUNC_LONG(link_warning_count_read, mysql_warning_count, MYSQLI_GET_MYSQL(MYSQLI_STATUS_VALID), zend_ulong, ZEND_ULONG_FMT)
241
242 /* result properties */
243
244 /* {{{ property result_type_read */
245 static int result_type_read(mysqli_object *obj, zval *retval, bool quiet)
246 {
247 MYSQL_RES *p;
248
249 CHECK_STATUS(MYSQLI_STATUS_VALID, quiet);
250 p = (MYSQL_RES *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
251
252 ZEND_ASSERT(p);
253 ZVAL_LONG(retval, mysqli_result_is_unbuffered(p) ? MYSQLI_USE_RESULT:MYSQLI_STORE_RESULT);
254
255 return SUCCESS;
256 }
257 /* }}} */
258
259 /* {{{ property result_lengths_read */
result_lengths_read(mysqli_object * obj,zval * retval,bool quiet)260 static int result_lengths_read(mysqli_object *obj, zval *retval, bool quiet)
261 {
262 MYSQL_RES *p;
263 const size_t *ret;
264 uint32_t field_count;
265
266 CHECK_STATUS(MYSQLI_STATUS_VALID, quiet);
267 p = (MYSQL_RES *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
268 field_count = mysql_num_fields(p);
269 if (!p || !field_count || !(ret = mysql_fetch_lengths(p))) {
270 ZVAL_NULL(retval);
271 } else {
272 zend_ulong i;
273
274 array_init(retval);
275
276 for (i = 0; i < field_count; i++) {
277 add_index_long(retval, i, ret[i]);
278 }
279 }
280
281 return SUCCESS;
282 }
283 /* }}} */
284
MYSQLI_MAP_PROPERTY_FUNC_LONG(result_current_field_read,mysql_field_tell,MYSQLI_GET_RESULT (MYSQLI_STATUS_VALID),zend_ulong,ZEND_ULONG_FMT)285 MYSQLI_MAP_PROPERTY_FUNC_LONG(result_current_field_read, mysql_field_tell, MYSQLI_GET_RESULT(MYSQLI_STATUS_VALID), zend_ulong, ZEND_ULONG_FMT)
286 MYSQLI_MAP_PROPERTY_FUNC_LONG(result_field_count_read, mysql_num_fields, MYSQLI_GET_RESULT(MYSQLI_STATUS_VALID), zend_ulong, ZEND_ULONG_FMT)
287 MYSQLI_MAP_PROPERTY_FUNC_LONG_OR_STR(result_num_rows_read, mysql_num_rows, MYSQLI_GET_RESULT(MYSQLI_STATUS_VALID), my_ulonglong, MYSQLI_LLU_SPEC)
288
289 /* statement properties */
290
291 /* {{{ property stmt_id_read */
292 static int stmt_id_read(mysqli_object *obj, zval *retval, bool quiet)
293 {
294 MY_STMT *p;
295
296 CHECK_STATUS(MYSQLI_STATUS_VALID, quiet);
297
298 p = (MY_STMT*)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
299
300 ZEND_ASSERT(p);
301 ZVAL_LONG(retval, mysqli_stmt_get_id(p->stmt));
302
303 return SUCCESS;
304 }
305 /* }}} */
306
307 /* {{{ property stmt_affected_rows_read */
stmt_affected_rows_read(mysqli_object * obj,zval * retval,bool quiet)308 static int stmt_affected_rows_read(mysqli_object *obj, zval *retval, bool quiet)
309 {
310 MY_STMT *p;
311 my_ulonglong rc;
312
313 CHECK_STATUS(MYSQLI_STATUS_VALID, quiet);
314
315 p = (MY_STMT *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
316
317 ZEND_ASSERT(p);
318 rc = mysql_stmt_affected_rows(p->stmt);
319
320 if (rc == (my_ulonglong) -1) {
321 ZVAL_LONG(retval, -1);
322 return SUCCESS;
323 }
324
325 if (rc < ZEND_LONG_MAX) {
326 ZVAL_LONG(retval, (zend_long) rc);
327 } else {
328 ZVAL_NEW_STR(retval, strpprintf(0, MYSQLI_LLU_SPEC, rc));
329 }
330
331 return SUCCESS;
332 }
333 /* }}} */
334
335 /* {{{ property stmt_error_list_read */
stmt_error_list_read(mysqli_object * obj,zval * retval,bool quiet)336 static int stmt_error_list_read(mysqli_object *obj, zval *retval, bool quiet)
337 {
338 MY_STMT * stmt;
339
340 CHECK_STATUS(MYSQLI_STATUS_INITIALIZED, quiet);
341
342 stmt = (MY_STMT *)((MYSQLI_RESOURCE *)(obj->ptr))->ptr;
343 if (stmt && stmt->stmt) {
344 array_init(retval);
345 if (stmt->stmt->data && stmt->stmt->data->error_info) {
346 MYSQLND_ERROR_LIST_ELEMENT * message;
347 zend_llist_position pos;
348 for (message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_first_ex(&stmt->stmt->data->error_info->error_list, &pos);
349 message;
350 message = (MYSQLND_ERROR_LIST_ELEMENT *) zend_llist_get_next_ex(&stmt->stmt->data->error_info->error_list, &pos))
351 {
352 zval single_error;
353 array_init(&single_error);
354 add_assoc_long_ex(&single_error, "errno", sizeof("errno") - 1, message->error_no);
355 add_assoc_string_ex(&single_error, "sqlstate", sizeof("sqlstate") - 1, message->sqlstate);
356 add_assoc_string_ex(&single_error, "error", sizeof("error") - 1, message->error);
357 add_next_index_zval(retval, &single_error);
358 }
359 }
360 } else {
361 ZVAL_EMPTY_ARRAY(retval);
362 }
363
364 return SUCCESS;
365 }
366 /* }}} */
367
368 MYSQLI_MAP_PROPERTY_FUNC_LONG_OR_STR(stmt_insert_id_read, mysql_stmt_insert_id, MYSQLI_GET_STMT(MYSQLI_STATUS_VALID), my_ulonglong, MYSQLI_LLU_SPEC)
369 MYSQLI_MAP_PROPERTY_FUNC_LONG_OR_STR(stmt_num_rows_read, mysql_stmt_num_rows, MYSQLI_GET_STMT(MYSQLI_STATUS_VALID), my_ulonglong, MYSQLI_LLU_SPEC)
370 MYSQLI_MAP_PROPERTY_FUNC_LONG(stmt_param_count_read, mysql_stmt_param_count, MYSQLI_GET_STMT(MYSQLI_STATUS_VALID), zend_ulong, ZEND_ULONG_FMT)
371 MYSQLI_MAP_PROPERTY_FUNC_LONG(stmt_field_count_read, mysql_stmt_field_count, MYSQLI_GET_STMT(MYSQLI_STATUS_VALID), zend_ulong, ZEND_ULONG_FMT)
372 MYSQLI_MAP_PROPERTY_FUNC_LONG(stmt_errno_read, mysql_stmt_errno, MYSQLI_GET_STMT(MYSQLI_STATUS_INITIALIZED), zend_ulong, ZEND_ULONG_FMT)
373 MYSQLI_MAP_PROPERTY_FUNC_STR(stmt_error_read, mysql_stmt_error, MYSQLI_GET_STMT(MYSQLI_STATUS_INITIALIZED))
374 MYSQLI_MAP_PROPERTY_FUNC_STR(stmt_sqlstate_read, mysql_stmt_sqlstate, MYSQLI_GET_STMT(MYSQLI_STATUS_INITIALIZED))
375
376 /* }}} */
377 const mysqli_property_entry mysqli_link_property_entries[] = {
378 {"affected_rows", sizeof("affected_rows") - 1, link_affected_rows_read, NULL},
379 {"client_info", sizeof("client_info") - 1, link_client_info_read, NULL},
380 {"client_version", sizeof("client_version") - 1, link_client_version_read, NULL},
381 {"connect_errno", sizeof("connect_errno") - 1, link_connect_errno_read, NULL},
382 {"connect_error", sizeof("connect_error") - 1, link_connect_error_read, NULL},
383 {"errno", sizeof("errno") - 1, link_errno_read, NULL},
384 {"error", sizeof("error") - 1, link_error_read, NULL},
385 {"error_list", sizeof("error_list") - 1, link_error_list_read, NULL},
386 {"field_count", sizeof("field_count") - 1, link_field_count_read, NULL},
387 {"host_info", sizeof("host_info") - 1, link_host_info_read, NULL},
388 {"info", sizeof("info") - 1, link_info_read, NULL},
389 {"insert_id", sizeof("insert_id") - 1, link_insert_id_read, NULL},
390 {"server_info", sizeof("server_info") - 1, link_server_info_read, NULL},
391 {"server_version", sizeof("server_version") - 1, link_server_version_read, NULL},
392 {"sqlstate", sizeof("sqlstate") - 1, link_sqlstate_read, NULL},
393 {"protocol_version",sizeof("protocol_version") - 1, link_protocol_version_read, NULL},
394 {"thread_id", sizeof("thread_id") - 1, link_thread_id_read, NULL},
395 {"warning_count", sizeof("warning_count") - 1, link_warning_count_read, NULL},
396 {NULL, 0, NULL, NULL}
397 };
398
399
400 const mysqli_property_entry mysqli_result_property_entries[] = {
401 {"current_field",sizeof("current_field")-1, result_current_field_read, NULL},
402 {"field_count", sizeof("field_count") - 1, result_field_count_read, NULL},
403 {"lengths", sizeof("lengths") - 1, result_lengths_read, NULL},
404 {"num_rows", sizeof("num_rows") - 1, result_num_rows_read, NULL},
405 {"type", sizeof("type") - 1, result_type_read, NULL},
406 {NULL, 0, NULL, NULL}
407 };
408
409 const mysqli_property_entry mysqli_stmt_property_entries[] = {
410 {"affected_rows", sizeof("affected_rows")-1,stmt_affected_rows_read, NULL},
411 {"insert_id", sizeof("insert_id") - 1, stmt_insert_id_read, NULL},
412 {"num_rows", sizeof("num_rows") - 1, stmt_num_rows_read, NULL},
413 {"param_count", sizeof("param_count") - 1, stmt_param_count_read, NULL},
414 {"field_count", sizeof("field_count") - 1, stmt_field_count_read, NULL},
415 {"errno", sizeof("errno") - 1, stmt_errno_read, NULL},
416 {"error", sizeof("error") - 1, stmt_error_read, NULL},
417 {"error_list", sizeof("error_list") - 1, stmt_error_list_read, NULL},
418 {"sqlstate", sizeof("sqlstate") - 1, stmt_sqlstate_read, NULL},
419 {"id", sizeof("id") - 1, stmt_id_read, NULL},
420 {NULL, 0, NULL, NULL}
421 };
422