1 /*
2   +----------------------------------------------------------------------+
3   | PHP Version 7                                                        |
4   +----------------------------------------------------------------------+
5   | Copyright (c) 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 	Z_ADDREF_P(object);
56 	ZVAL_OBJ(&iterator->intern.data, Z_OBJ_P(object));
57 	iterator->intern.funcs = &php_mysqli_result_iterator_funcs;
58 	iterator->result = Z_MYSQLI_P(object);
59 	iterator->row_num = -1;
60 
61 	return &iterator->intern;
62 }
63 /* }}} */
64 
65 /* {{{ */
php_mysqli_result_iterator_dtor(zend_object_iterator * iter)66 static void php_mysqli_result_iterator_dtor(zend_object_iterator *iter)
67 {
68 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*)iter;
69 
70 	/* cleanup handled in sxe_object_dtor as we don't always have an iterator wrapper */
71 	zval_ptr_dtor(&iterator->intern.data);
72 	zval_ptr_dtor(&iterator->current_row);
73 }
74 /* }}} */
75 
76 /* {{{ */
php_mysqli_result_iterator_valid(zend_object_iterator * iter)77 static int php_mysqli_result_iterator_valid(zend_object_iterator *iter)
78 {
79 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
80 
81 	return Z_TYPE(iterator->current_row) == IS_ARRAY ? SUCCESS : FAILURE;
82 }
83 /* }}} */
84 
85 /* {{{ */
php_mysqli_result_iterator_current_data(zend_object_iterator * iter)86 static zval *php_mysqli_result_iterator_current_data(zend_object_iterator *iter)
87 {
88 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
89 
90 	return &iterator->current_row;
91 }
92 /* }}} */
93 
94 /* {{{ */
php_mysqli_result_iterator_move_forward(zend_object_iterator * iter)95 static void php_mysqli_result_iterator_move_forward(zend_object_iterator *iter)
96 {
97 
98 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
99 	mysqli_object *intern = iterator->result;
100 	MYSQL_RES	*result;
101 
102 	MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
103 
104 	zval_ptr_dtor(&iterator->current_row);
105 	php_mysqli_fetch_into_hash_aux(&iterator->current_row, result, MYSQLI_ASSOC);
106 	if (Z_TYPE(iterator->current_row) == IS_ARRAY) {
107 		iterator->row_num++;
108 	}
109 }
110 /* }}} */
111 
112 /* {{{ */
php_mysqli_result_iterator_rewind(zend_object_iterator * iter)113 static void php_mysqli_result_iterator_rewind(zend_object_iterator *iter)
114 {
115 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
116 	mysqli_object *intern = iterator->result;
117 	MYSQL_RES	*result;
118 
119 	MYSQLI_FETCH_RESOURCE_BY_OBJ(result, MYSQL_RES *, intern, "mysqli_result", MYSQLI_STATUS_VALID);
120 
121 	if (mysqli_result_is_unbuffered(result)) {
122 #if MYSQLI_USE_MYSQLND
123 		if (result->unbuf->eof_reached) {
124 #else
125 		if (result->eof) {
126 #endif
127 			php_error_docref(NULL, E_WARNING, "Data fetched with MYSQLI_USE_RESULT can be iterated only once");
128 			return;
129 		}
130 	} else {
131 		mysql_data_seek(result, 0);
132 	}
133 	iterator->row_num = -1;
134 	php_mysqli_result_iterator_move_forward(iter);
135 }
136 /* }}} */
137 
138 /* {{{ php_mysqli_result_iterator_current_key */
139 static void php_mysqli_result_iterator_current_key(zend_object_iterator *iter, zval *key)
140 {
141 	php_mysqli_result_iterator *iterator = (php_mysqli_result_iterator*) iter;
142 
143 	ZVAL_LONG(key, iterator->row_num);
144 }
145 /* }}} */
146 
147 /* {{{ php_mysqli_result_iterator_funcs */
148 const zend_object_iterator_funcs php_mysqli_result_iterator_funcs = {
149 	php_mysqli_result_iterator_dtor,
150 	php_mysqli_result_iterator_valid,
151 	php_mysqli_result_iterator_current_data,
152 	php_mysqli_result_iterator_current_key,
153 	php_mysqli_result_iterator_move_forward,
154 	php_mysqli_result_iterator_rewind,
155 	NULL
156 };
157 /* }}} */
158