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 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include <signal.h>
26 
27 #include "php.h"
28 #include "php_ini.h"
29 #include "php_mysqli_structs.h"
30 #include "mysqli_priv.h"
31 #include "zend_interfaces.h"
32 
33 
34 extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
35 
36 typedef struct {
37 	zend_object_iterator  intern;
38 	mysqli_object *result;
39 	zval current_row;
40 	my_longlong row_num;
41 } php_mysqli_result_iterator;
42 
43 
44 /* {{{ */
php_mysqli_result_get_iterator(zend_class_entry * ce,zval * object,int by_ref)45 zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
46 {
47 	php_mysqli_result_iterator *iterator;
48 
49 	if (by_ref) {
50 		zend_error(E_ERROR, "An iterator cannot be used with foreach by reference");
51 	}
52 	iterator = ecalloc(1, sizeof(php_mysqli_result_iterator));
53 	zend_iterator_init(&iterator->intern);
54 
55 	ZVAL_COPY(&iterator->intern.data, 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 int 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 MYSQLI_USE_MYSQLND
122 		if (result->unbuf->eof_reached) {
123 #else
124 		if (result->eof) {
125 #endif
126 			php_error_docref(NULL, E_WARNING, "Data fetched with MYSQLI_USE_RESULT can be iterated only once");
127 			return;
128 		}
129 	} else {
130 		mysql_data_seek(result, 0);
131 	}
132 	iterator->row_num = -1;
133 	php_mysqli_result_iterator_move_forward(iter);
134 }
135 /* }}} */
136 
137 /* {{{ php_mysqli_result_iterator_current_key */
138 static void php_mysqli_result_iterator_current_key(zend_object_iterator *iter, zval *key)
139 {
140 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
141 
142 	ZVAL_LONG(key, iterator->row_num);
143 }
144 /* }}} */
145 
146 /* {{{ php_mysqli_result_iterator_funcs */
147 const zend_object_iterator_funcs php_mysqli_result_iterator_funcs = {
148 	php_mysqli_result_iterator_dtor,
149 	php_mysqli_result_iterator_valid,
150 	php_mysqli_result_iterator_current_data,
151 	php_mysqli_result_iterator_current_key,
152 	php_mysqli_result_iterator_move_forward,
153 	php_mysqli_result_iterator_rewind,
154 	NULL
155 };
156 /* }}} */
157 
158 /*
159  * Local variables:
160  * tab-width: 4
161  * c-basic-offset: 4
162  * End:
163  * vim600: noet sw=4 ts=4 fdm=marker
164  * vim<600: noet sw=4 ts=4
165  */
166