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   | http://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_ini.h"
27 #include "php_mysqli_structs.h"
28 #include "mysqli_priv.h"
29 #include "zend_interfaces.h"
30 
31 
32 extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
33 
34 typedef struct {
35 	zend_object_iterator  intern;
36 	mysqli_object *result;
37 	zval current_row;
38 	my_longlong row_num;
39 } php_mysqli_result_iterator;
40 
41 
42 /* {{{ */
php_mysqli_result_get_iterator(zend_class_entry * ce,zval * object,int by_ref)43 zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref)
44 {
45 	php_mysqli_result_iterator *iterator;
46 
47 	if (by_ref) {
48 		zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
49 		return NULL;
50 	}
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 #ifdef 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 	NULL, /* get_gc */
157 };
158 /* }}} */
159