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 "php_mysqli_structs.h"
27 #include "mysqli_priv.h"
28 #include "zend_interfaces.h"
29 
30 
31 extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
32 
33 typedef struct {
34 	zend_object_iterator  intern;
35 	mysqli_object *result;
36 	zval current_row;
37 	my_longlong row_num;
38 } php_mysqli_result_iterator;
39 
40 
41 /* {{{ */
php_mysqli_result_get_iterator(zend_class_entry * ce,zval * object,int by_ref)42 zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
43 {
44 	php_mysqli_result_iterator *iterator;
45 
46 	if (by_ref) {
47 		zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
48 		return NULL;
49 	}
50 
51 	iterator = ecalloc(1, sizeof(php_mysqli_result_iterator));
52 	zend_iterator_init(&iterator->intern);
53 
54 	Z_ADDREF_P(object);
55 	ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
56 	iterator->intern.funcs = &php_mysqli_result_iterator_funcs;
57 	iterator->result = Z_MYSQLI_P(object);
58 	iterator->row_num = -1;
59 
60 	return &iterator->intern;
61 }
62 /* }}} */
63 
64 /* {{{ */
php_mysqli_result_iterator_dtor(zend_object_iterator * iter)65 static void php_mysqli_result_iterator_dtor(zend_object_iterator *iter)
66 {
67 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*)iter;
68 
69 	/* cleanup handled in sxe_object_dtor as we don't always have an iterator wrapper */
70 	zval_ptr_dtor(&iterator->intern.data);
71 	zval_ptr_dtor(&iterator->current_row);
72 }
73 /* }}} */
74 
75 /* {{{ */
php_mysqli_result_iterator_valid(zend_object_iterator * iter)76 static zend_result php_mysqli_result_iterator_valid(zend_object_iterator *iter)
77 {
78 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
79 
80 	return Z_TYPE(iterator->current_row) == IS_ARRAY ? SUCCESS : FAILURE;
81 }
82 /* }}} */
83 
84 /* {{{ */
php_mysqli_result_iterator_current_data(zend_object_iterator * iter)85 static zval *php_mysqli_result_iterator_current_data(zend_object_iterator *iter)
86 {
87 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
88 
89 	return &iterator->current_row;
90 }
91 /* }}} */
92 
93 /* {{{ */
php_mysqli_result_iterator_move_forward(zend_object_iterator * iter)94 static void php_mysqli_result_iterator_move_forward(zend_object_iterator *iter)
95 {
96 
97 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
98 	mysqli_object *intern = iterator->result;
99 	MYSQL_RES	*result;
100 
101 	MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
102 
103 	zval_ptr_dtor(&iterator->current_row);
104 	php_mysqli_fetch_into_hash_aux(&iterator->current_row, result, MYSQLI_ASSOC);
105 	if (Z_TYPE(iterator->current_row) == IS_ARRAY) {
106 		iterator->row_num++;
107 	}
108 }
109 /* }}} */
110 
111 /* {{{ */
php_mysqli_result_iterator_rewind(zend_object_iterator * iter)112 static void php_mysqli_result_iterator_rewind(zend_object_iterator *iter)
113 {
114 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
115 	mysqli_object *intern = iterator->result;
116 	MYSQL_RES	*result;
117 
118 	MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
119 
120 	if (mysqli_result_is_unbuffered(result)) {
121 		if (result->unbuf->eof_reached) {
122 			zend_error(E_WARNING, "Data fetched with MYSQLI_USE_RESULT can be iterated only once");
123 			return;
124 		}
125 	} else {
126 		mysql_data_seek(result, 0);
127 	}
128 	iterator->row_num = -1;
129 	php_mysqli_result_iterator_move_forward(iter);
130 }
131 /* }}} */
132 
133 /* {{{ php_mysqli_result_iterator_current_key */
php_mysqli_result_iterator_current_key(zend_object_iterator * iter,zval * key)134 static void php_mysqli_result_iterator_current_key(zend_object_iterator *iter, zval *key)
135 {
136 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
137 
138 	ZVAL_LONG(key, iterator->row_num);
139 }
140 /* }}} */
141 
142 /* {{{ php_mysqli_result_iterator_funcs */
143 const zend_object_iterator_funcs php_mysqli_result_iterator_funcs = {
144 	php_mysqli_result_iterator_dtor,
145 	php_mysqli_result_iterator_valid,
146 	php_mysqli_result_iterator_current_data,
147 	php_mysqli_result_iterator_current_key,
148 	php_mysqli_result_iterator_move_forward,
149 	php_mysqli_result_iterator_rewind,
150 	NULL,
151 	NULL, /* get_gc */
152 };
153 /* }}} */
154