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 #ifndef PHP_MYSQLI_STRUCTS_H
22 #define PHP_MYSQLI_STRUCTS_H
23
24 /* A little hack to prevent build break, when mysql is used together with
25 * c-client, which also defines LIST.
26 */
27 #ifdef LIST
28 #undef LIST
29 #endif
30
31 #ifndef TRUE
32 #define TRUE 1
33 #endif
34
35 #ifndef FALSE
36 #define FALSE 0
37 #endif
38
39 #ifdef MYSQLI_USE_MYSQLND
40 #include "ext/mysqlnd/mysqlnd.h"
41 #include "mysqli_mysqlnd.h"
42 #else
43
44 /*
45 The libmysql headers (a PITA) also define it and there will be an warning.
46 Undef it and later we might need to define it again.
47 */
48 #ifdef HAVE_MBRLEN
49 #undef HAVE_MBRLEN
50 #define WE_HAD_MBRLEN
51 #endif
52 #ifdef HAVE_MBSTATE_T
53 #undef HAVE_MBSTATE_T
54 #define WE_HAD_MBSTATE_T
55 #endif
56
57 #if !defined(HAVE_MBRLEN) && defined(WE_HAD_MBRLEN)
58 #define HAVE_MBRLEN 1
59 #endif
60
61 #if !defined(HAVE_MBSTATE_T) && defined(WE_HAD_MBSTATE_T)
62 #define HAVE_MBSTATE_T 1
63 #endif
64
65 #include <mysql.h>
66 #if MYSQL_VERSION_ID >= 80000 && MYSQL_VERSION_ID < 100000
67 typedef _Bool my_bool;
68 #endif
69 #include <errmsg.h>
70 #include <mysqld_error.h>
71 #include "mysqli_libmysql.h"
72 #endif /* MYSQLI_USE_MYSQLND */
73
74
75 #define MYSQLI_VERSION_ID 101009
76
77 enum mysqli_status {
78 MYSQLI_STATUS_UNKNOWN=0,
79 MYSQLI_STATUS_CLEARED,
80 MYSQLI_STATUS_INITIALIZED,
81 MYSQLI_STATUS_VALID
82 };
83
84 typedef struct {
85 char *val;
86 zend_ulong buflen;
87 zend_ulong output_len;
88 zend_ulong type;
89 } VAR_BUFFER;
90
91 typedef struct {
92 unsigned int var_cnt;
93 VAR_BUFFER *buf;
94 zval *vars;
95 char *is_null;
96 } BIND_BUFFER;
97
98 typedef struct {
99 MYSQL_STMT *stmt;
100 BIND_BUFFER param;
101 BIND_BUFFER result;
102 char *query;
103 #ifndef MYSQLI_USE_MYSQLND
104 /* used to manage refcount with libmysql (already implement in mysqlnd) */
105 zval link_handle;
106 #endif
107 } MY_STMT;
108
109 typedef struct {
110 MYSQL *mysql;
111 zend_string *hash_key;
112 zval li_read;
113 php_stream *li_stream;
114 unsigned int multi_query;
115 zend_bool persistent;
116 #if defined(MYSQLI_USE_MYSQLND)
117 int async_result_fetch_type;
118 #endif
119 } MY_MYSQL;
120
121 typedef struct {
122 void *ptr; /* resource: (mysql, result, stmt) */
123 void *info; /* additional buffer */
124 enum mysqli_status status; /* object status */
125 } MYSQLI_RESOURCE;
126
127 typedef struct _mysqli_object {
128 void *ptr;
129 HashTable *prop_handler;
130 zend_object zo;
131 } mysqli_object; /* extends zend_object */
132
php_mysqli_fetch_object(zend_object * obj)133 static inline mysqli_object *php_mysqli_fetch_object(zend_object *obj) {
134 return (mysqli_object *)((char*)(obj) - XtOffsetOf(mysqli_object, zo));
135 }
136
137 #define Z_MYSQLI_P(zv) php_mysqli_fetch_object(Z_OBJ_P((zv)))
138
139 typedef struct st_mysqli_warning MYSQLI_WARNING;
140
141 struct st_mysqli_warning {
142 zval reason;
143 zval sqlstate;
144 int errorno;
145 MYSQLI_WARNING *next;
146 };
147
148 typedef struct _mysqli_property_entry {
149 const char *pname;
150 size_t pname_length;
151 int (*r_func)(mysqli_object *obj, zval *retval, zend_bool quiet);
152 int (*w_func)(mysqli_object *obj, zval *value);
153 } mysqli_property_entry;
154
155 typedef struct {
156 zend_ptr_stack free_links;
157 } mysqli_plist_entry;
158
159 #ifdef PHP_WIN32
160 #define PHP_MYSQLI_API __declspec(dllexport)
161 #define MYSQLI_LLU_SPEC "%I64u"
162 #define MYSQLI_LL_SPEC "%I64d"
163 #ifndef L64
164 #define L64(x) x##i64
165 #endif
166 typedef __int64 my_longlong;
167 #else
168 # if defined(__GNUC__) && __GNUC__ >= 4
169 # define PHP_MYSQLI_API __attribute__ ((visibility("default")))
170 # else
171 # define PHP_MYSQLI_API
172 # endif
173 /* we need this for PRIu64 and PRId64 */
174 #include <inttypes.h>
175 #define MYSQLI_LLU_SPEC "%" PRIu64
176 #define MYSQLI_LL_SPEC "%" PRId64
177 #ifndef L64
178 #define L64(x) x##LL
179 #endif
180 typedef int64_t my_longlong;
181 #endif
182
183 #ifdef ZTS
184 #include "TSRM.h"
185 #endif
186
187 extern zend_class_entry *mysqli_link_class_entry;
188 extern zend_class_entry *mysqli_stmt_class_entry;
189 extern zend_class_entry *mysqli_result_class_entry;
190 extern zend_class_entry *mysqli_driver_class_entry;
191 extern zend_class_entry *mysqli_warning_class_entry;
192 extern zend_class_entry *mysqli_exception_class_entry;
193 extern int php_le_pmysqli(void);
194 extern void php_mysqli_dtor_p_elements(void *data);
195
196 extern void php_mysqli_close(MY_MYSQL * mysql, int close_type, int resource_status);
197
198 extern const zend_object_iterator_funcs php_mysqli_result_iterator_funcs;
199 extern zend_object_iterator *php_mysqli_result_get_iterator(zend_class_entry *ce, zval *object, int by_ref);
200
201 extern void php_mysqli_fetch_into_hash_aux(zval *return_value, MYSQL_RES * result, zend_long fetchtype);
202
203 #define MYSQLI_DISABLE_MQ if (mysql->multi_query) { \
204 mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_OFF); \
205 mysql->multi_query = 0; \
206 }
207
208 #define MYSQLI_ENABLE_MQ if (!mysql->multi_query) { \
209 mysql_set_server_option(mysql->mysql, MYSQL_OPTION_MULTI_STATEMENTS_ON); \
210 mysql->multi_query = 1; \
211 }
212
213 #define REGISTER_MYSQLI_CLASS_ENTRY(name, mysqli_entry, class_functions) { \
214 zend_class_entry ce; \
215 INIT_CLASS_ENTRY(ce, name,class_functions); \
216 ce.create_object = mysqli_objects_new; \
217 mysqli_entry = zend_register_internal_class(&ce); \
218 } \
219
220 #define MYSQLI_REGISTER_RESOURCE_EX(__ptr, __zval) \
221 (Z_MYSQLI_P(__zval))->ptr = __ptr;
222
223 #define MYSQLI_RETURN_RESOURCE(__ptr, __ce) \
224 RETVAL_OBJ(mysqli_objects_new(__ce)); \
225 MYSQLI_REGISTER_RESOURCE_EX(__ptr, return_value)
226
227 #define MYSQLI_REGISTER_RESOURCE(__ptr, __ce) \
228 {\
229 zval *object = getThis(); \
230 if (!object || !instanceof_function(Z_OBJCE_P(object), mysqli_link_class_entry)) { \
231 object = return_value; \
232 ZVAL_OBJ(object, mysqli_objects_new(__ce)); \
233 } \
234 MYSQLI_REGISTER_RESOURCE_EX(__ptr, object)\
235 }
236
237 #define MYSQLI_FETCH_RESOURCE(__ptr, __type, __id, __name, __check) \
238 { \
239 MYSQLI_RESOURCE *my_res; \
240 mysqli_object *intern = Z_MYSQLI_P(__id); \
241 if (!(my_res = (MYSQLI_RESOURCE *)intern->ptr)) {\
242 php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
243 RETURN_FALSE;\
244 }\
245 __ptr = (__type)my_res->ptr; \
246 if (__check && my_res->status < __check) { \
247 php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
248 RETURN_FALSE;\
249 }\
250 }
251
252 #define MYSQLI_FETCH_RESOURCE_BY_OBJ(__ptr, __type, __obj, __name, __check) \
253 { \
254 MYSQLI_RESOURCE *my_res; \
255 if (!(my_res = (MYSQLI_RESOURCE *)(__obj->ptr))) {\
256 php_error_docref(NULL, E_WARNING, "Couldn't fetch %s", ZSTR_VAL(intern->zo.ce->name));\
257 return;\
258 }\
259 __ptr = (__type)my_res->ptr; \
260 if (__check && my_res->status < __check) { \
261 php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
262 return;\
263 }\
264 }
265
266 #define MYSQLI_FETCH_RESOURCE_CONN(__ptr, __id, __check) \
267 { \
268 MYSQLI_FETCH_RESOURCE((__ptr), MY_MYSQL *, (__id), "mysqli_link", (__check)); \
269 if (!(__ptr)->mysql) { \
270 mysqli_object *intern = Z_MYSQLI_P(__id); \
271 php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
272 RETURN_NULL(); \
273 } \
274 }
275
276 #define MYSQLI_FETCH_RESOURCE_STMT(__ptr, __id, __check) \
277 { \
278 MYSQLI_FETCH_RESOURCE((__ptr), MY_STMT *, (__id), "mysqli_stmt", (__check)); \
279 if (!(__ptr)->stmt) { \
280 mysqli_object *intern = Z_MYSQLI_P(__id); \
281 php_error_docref(NULL, E_WARNING, "invalid object or resource %s\n", ZSTR_VAL(intern->zo.ce->name)); \
282 RETURN_NULL();\
283 } \
284 }
285
286 #define MYSQLI_SET_STATUS(__id, __value) \
287 { \
288 mysqli_object *intern = Z_MYSQLI_P(__id); \
289 ((MYSQLI_RESOURCE *)intern->ptr)->status = __value; \
290 } \
291
292 #define MYSQLI_CLEAR_RESOURCE(__id) \
293 { \
294 mysqli_object *intern = Z_MYSQLI_P(__id); \
295 efree(intern->ptr); \
296 intern->ptr = NULL; \
297 }
298
299
300 ZEND_BEGIN_MODULE_GLOBALS(mysqli)
301 zend_long default_link;
302 zend_long num_links;
303 zend_long max_links;
304 zend_long num_active_persistent;
305 zend_long num_inactive_persistent;
306 zend_long max_persistent;
307 zend_long allow_persistent;
308 zend_ulong default_port;
309 char *default_host;
310 char *default_user;
311 char *default_socket;
312 char *default_pw;
313 zend_long reconnect;
314 zend_long allow_local_infile;
315 zend_long strict;
316 zend_long error_no;
317 char *error_msg;
318 zend_long report_mode;
319 HashTable *report_ht;
320 zend_ulong multi_query;
321 zend_ulong embedded;
322 zend_bool rollback_on_cached_plink;
323 ZEND_END_MODULE_GLOBALS(mysqli)
324
325 #define MyG(v) ZEND_MODULE_GLOBALS_ACCESSOR(mysqli, v)
326
327 #if defined(ZTS) && defined(COMPILE_DL_MYSQLI)
328 ZEND_TSRMLS_CACHE_EXTERN()
329 #endif
330
331 ZEND_EXTERN_MODULE_GLOBALS(mysqli)
332
333 #endif /* PHP_MYSQLI_STRUCTS.H */
334