- fix ::set() arginfo
[m6w6/ext-http] / http_querystring_object.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #include "php_http.h"
17
18 #ifdef ZEND_ENGINE_2
19
20 #include "php_variables.h"
21 #include "zend_interfaces.h"
22
23 #include "php_http_api.h"
24 #include "php_http_url_api.h"
25 #include "php_http_querystring_object.h"
26 #include "php_http_exception_object.h"
27
28 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpQueryString, method, ret_ref, req_args)
29 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpQueryString, method, ret_ref)
30 #define HTTP_QUERYSTRING_ME(method, visibility) PHP_ME(HttpQueryString, method, HTTP_ARGS(HttpQueryString, method), visibility)
31 #define HTTP_QUERYSTRING_GME(method, visibility) PHP_ME(HttpQueryString, method, HTTP_ARGS(HttpQueryString, __getter), visibility)
32
33 HTTP_BEGIN_ARGS(__construct, 0, 0)
34 HTTP_ARG_VAL(global, 0)
35 HTTP_ARG_VAL(params, 0)
36 HTTP_END_ARGS;
37
38 HTTP_BEGIN_ARGS(getInstance, 0, 0)
39 HTTP_ARG_VAL(global, 0)
40 HTTP_END_ARGS;
41
42 HTTP_EMPTY_ARGS(toArray, 0);
43 HTTP_EMPTY_ARGS(toString, 0);
44
45 HTTP_BEGIN_ARGS(get, 0, 0)
46 HTTP_ARG_VAL(name, 0)
47 HTTP_ARG_VAL(type, 0)
48 HTTP_ARG_VAL(defval, 0)
49 HTTP_ARG_VAL(delete, 0)
50 HTTP_END_ARGS;
51
52 HTTP_BEGIN_ARGS(set, 0, 1)
53 HTTP_ARG_VAL(params, 0)
54 HTTP_END_ARGS;
55
56 HTTP_BEGIN_ARGS(__getter, 0, 1)
57 HTTP_ARG_VAL(name, 0)
58 HTTP_ARG_VAL(defval, 0)
59 HTTP_ARG_VAL(delete, 0)
60 HTTP_END_ARGS;
61
62 #define http_querystring_object_declare_default_properties() _http_querystring_object_declare_default_properties(TSRMLS_C)
63 static inline void _http_querystring_object_declare_default_properties(TSRMLS_D);
64
65 #define GET_STATIC_PROP(n) *GET_STATIC_PROP_EX(http_querystring_object_ce, n)
66 #define SET_STATIC_PROP(n, v) SET_STATIC_PROP_EX(http_querystring_object_ce, n, v)
67 #define OBJ_PROP_CE http_querystring_object_ce
68 zend_class_entry *http_querystring_object_ce;
69 zend_function_entry http_querystring_object_fe[] = {
70 HTTP_QUERYSTRING_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL)
71
72 HTTP_QUERYSTRING_ME(toArray, ZEND_ACC_PUBLIC)
73 HTTP_QUERYSTRING_ME(toString, ZEND_ACC_PUBLIC)
74 ZEND_MALIAS(HttpQueryString, __toString, toString, HTTP_ARGS(HttpQueryString, toString), ZEND_ACC_PUBLIC)
75
76 HTTP_QUERYSTRING_ME(get, ZEND_ACC_PUBLIC)
77 HTTP_QUERYSTRING_ME(set, ZEND_ACC_PUBLIC)
78
79 HTTP_QUERYSTRING_ME(getInstance, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
80
81 HTTP_QUERYSTRING_GME(getBool, ZEND_ACC_PUBLIC)
82 HTTP_QUERYSTRING_GME(getInt, ZEND_ACC_PUBLIC)
83 HTTP_QUERYSTRING_GME(getFloat, ZEND_ACC_PUBLIC)
84 HTTP_QUERYSTRING_GME(getString, ZEND_ACC_PUBLIC)
85 HTTP_QUERYSTRING_GME(getArray, ZEND_ACC_PUBLIC)
86 HTTP_QUERYSTRING_GME(getObject, ZEND_ACC_PUBLIC)
87
88 EMPTY_FUNCTION_ENTRY
89 };
90 static zend_object_handlers http_querystring_object_handlers;
91
92 PHP_MINIT_FUNCTION(http_querystring_object)
93 {
94 HTTP_REGISTER_CLASS_EX(HttpQueryString, http_querystring_object, NULL, 0);
95
96 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
97 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
98 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
99 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
100 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
101 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
102
103 return SUCCESS;
104 }
105
106 zend_object_value _http_querystring_object_new(zend_class_entry *ce TSRMLS_DC)
107 {
108 return http_querystring_object_new_ex(ce, NULL);
109 }
110
111 zend_object_value _http_querystring_object_new_ex(zend_class_entry *ce, http_querystring_object **ptr TSRMLS_DC)
112 {
113 zend_object_value ov;
114 http_querystring_object *o;
115
116 o = ecalloc(1, sizeof(http_querystring_object));
117 o->zo.ce = ce;
118
119 if (ptr) {
120 *ptr = o;
121 }
122
123 ALLOC_HASHTABLE(OBJ_PROP(o));
124 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
125 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
126
127 ov.handle = putObject(http_querystring_object, o);
128 ov.handlers = &http_querystring_object_handlers;
129
130 return ov;
131 }
132
133 static inline void _http_querystring_object_declare_default_properties(TSRMLS_D)
134 {
135 zend_class_entry *ce = http_querystring_object_ce;
136
137 DCL_STATIC_PROP_N(PRIVATE, instance);
138
139 DCL_PROP_N(PRIVATE, queryArray);
140 DCL_PROP(PRIVATE, string, queryString, "");
141
142 #ifndef WONKY
143 DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
144 DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
145 DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
146 DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
147 DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
148 DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
149 #endif
150 }
151
152 void _http_querystring_object_free(zend_object *object TSRMLS_DC)
153 {
154 http_querystring_object *o = (http_querystring_object *) object;
155
156 if (OBJ_PROP(o)) {
157 zend_hash_destroy(OBJ_PROP(o));
158 FREE_HASHTABLE(OBJ_PROP(o));
159 }
160 efree(o);
161 }
162
163 #define http_querystring_update(qa, qs) _http_querystring_update((qa), (qs) TSRMLS_CC)
164 static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_DC)
165 {
166 char *s = NULL;
167 size_t l = 0;
168
169 if (Z_TYPE_P(qarray) != IS_ARRAY) {
170 convert_to_array(qarray);
171 }
172 if (SUCCESS == http_urlencode_hash_ex(Z_ARRVAL_P(qarray), 0, NULL, 0, &s, &l)) {
173 zval_dtor(qstring);
174 ZVAL_STRINGL(qstring, s, l, 0);
175 } else {
176 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to update query string");
177 }
178 }
179
180 #define http_querystring_modify_ex(a, k, l, v) _http_querystring_modify_ex((a), (k), (l), (v) TSRMLS_CC)
181 static inline int _http_querystring_modify_ex(zval *qarray, char *key, uint keylen, zval *data TSRMLS_DC)
182 {
183 if (Z_TYPE_P(data) == IS_NULL) {
184 if (SUCCESS != zend_hash_del(Z_ARRVAL_P(qarray), key, keylen)) {
185 return 0;
186 }
187 } else {
188 ZVAL_ADDREF(data);
189 add_assoc_zval(qarray, key, data);
190 }
191 return 1;
192 }
193
194 #define http_querystring_modify_array(q, a) _http_querystring_modify_array((q), (a) TSRMLS_CC)
195 static inline int _http_querystring_modify_array(zval *qarray, zval *array TSRMLS_DC)
196 {
197 zval **value;
198 HashPosition pos;
199 char *key = NULL;
200 uint keylen = 0;
201 ulong idx = 0;
202 int rv = 0;
203
204 FOREACH_KEYLENVAL(pos, array, key, keylen, idx, value) {
205 if (key) {
206 if (http_querystring_modify_ex(qarray, key, keylen, *value)) {
207 rv = 1;
208 }
209 } else {
210 keylen = spprintf(&key, 0, "%lu", idx);
211 if (http_querystring_modify_ex(qarray, key, keylen, *value)) {
212 rv = 1;
213 }
214 efree(key);
215 }
216 key = NULL;
217 }
218
219 return rv;
220 }
221
222 #define http_querystring_modify(q, p) _http_querystring_modify((q), (p) TSRMLS_CC)
223 static inline int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC)
224 {
225 if (Z_TYPE_P(params) == IS_ARRAY) {
226 return http_querystring_modify_array(qarray, params);
227 } else {
228 int rv;
229 zval array;
230
231 INIT_PZVAL(&array);
232 array_init(&array);
233
234 ZVAL_ADDREF(params);
235 convert_to_string_ex(&params);
236 sapi_module.treat_data(PARSE_STRING, estrdup(Z_STRVAL_P(params)), &array TSRMLS_CC);
237 zval_ptr_dtor(&params);
238 rv = http_querystring_modify_array(qarray, &array);
239 zval_dtor(&array);
240 return rv;
241 }
242 }
243
244 #define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC)
245 static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC)
246 {
247 zval *zobj, *zglobal;
248
249 MAKE_STD_ZVAL(zglobal);
250 ZVAL_BOOL(zglobal, global);
251
252 MAKE_STD_ZVAL(zobj);
253 Z_TYPE_P(zobj) = IS_OBJECT;
254 Z_OBJVAL_P(zobj) = http_querystring_object_new(http_querystring_object_ce);
255 zend_call_method_with_1_params(&zobj, Z_OBJCE_P(zobj), NULL, "__construct", NULL, zglobal);
256
257 zval_ptr_dtor(&zglobal);
258
259 return zobj;
260 }
261
262 #define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC)
263 static inline void _http_querystring_get(zval *this_ptr, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value TSRMLS_DC)
264 {
265 zval **arrval, *qarray = GET_PROP(queryArray);
266
267 if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void **) &arrval))) {
268 RETVAL_ZVAL(*arrval, 1, 0);
269
270 if (type) {
271 convert_to_type(type, return_value);
272 }
273
274 if (del && (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), name, name_len + 1))) {
275 http_querystring_update(qarray, GET_PROP(queryString));
276 }
277 } else if(defval) {
278 RETURN_ZVAL(defval, 1, 0);
279 }
280 }
281
282 /* {{{ proto void HttpQueryString::__construct([bool global = true[, mixed add])
283 *
284 * Creates a new HttpQueryString object instance.
285 * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE.
286 */
287 PHP_METHOD(HttpQueryString, __construct)
288 {
289 zend_bool global = 1;
290 zval *params = NULL, *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING;
291
292 SET_EH_THROW_HTTP();
293 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, &params)) {
294 if (global) {
295 if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &_SERVER)) &&
296 (Z_TYPE_PP(_SERVER) == IS_ARRAY) &&
297 (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void **) &QUERY_STRING))) {
298
299 qstring = *QUERY_STRING;
300
301 if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
302 qarray = *_GET;
303 } else {
304 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array");
305 }
306 } else {
307 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to QUERY_STRING");
308 }
309
310 if (qarray && qstring) {
311 if (Z_TYPE_P(qstring) != IS_STRING) {
312 convert_to_string(qstring);
313 }
314
315 SET_PROP(queryArray, qarray);
316 SET_PROP(queryString, qstring);
317 GET_PROP(queryArray)->is_ref = 1;
318 GET_PROP(queryString)->is_ref = 1;
319
320 if (params && http_querystring_modify(GET_PROP(queryArray), params)) {
321 http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString));
322 }
323 }
324 } else {
325 qarray = ecalloc(1, sizeof(zval));
326 array_init(qarray);
327
328 SET_PROP(queryArray, qarray);
329 UPD_STRL(queryString, "", 0);
330
331 if (params && http_querystring_modify(qarray, params)) {
332 http_querystring_update(qarray, GET_PROP(queryString));
333 }
334 }
335 }
336 SET_EH_NORMAL();
337 }
338 /* }}} */
339
340 /* {{{ proto string HttpQueryString::toString()
341 *
342 * Returns the string representation.
343 */
344 PHP_METHOD(HttpQueryString, toString)
345 {
346 NO_ARGS;
347 RETURN_PROP(queryString);
348 }
349 /* }}} */
350
351 /* {{{ proto array HttpQueryString::toArray()
352 *
353 * Returns the array representation.
354 */
355 PHP_METHOD(HttpQueryString, toArray)
356 {
357 NO_ARGS;
358 RETURN_PROP(queryArray);
359 }
360 /* }}} */
361
362 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
363 *
364 * Get (part of) the query string.
365 *
366 * The type parameter is either one of the HttpQueryString::TYPE_* constants or a type abbreviation like
367 * "b" for bool, "i" for int, "f" for float, "s" for string, "a" for array and "o" for a stdClass object.
368 */
369 PHP_METHOD(HttpQueryString, get)
370 {
371 char *name = NULL;
372 int name_len = 0;
373 long type = 0;
374 zend_bool del = 0;
375 zval *ztype = NULL, *defval = NULL;
376
377 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name, &name_len, &ztype, &defval, &del)) {
378 if (name && name_len) {
379 if (ztype) {
380 if (Z_TYPE_P(ztype) == IS_LONG) {
381 type = Z_LVAL_P(ztype);
382 } else if(Z_TYPE_P(ztype) == IS_STRING) {
383 switch (tolower(Z_STRVAL_P(ztype)[0]))
384 {
385 case 'b': type = HTTP_QUERYSTRING_TYPE_BOOL; break;
386 case 'i': type = HTTP_QUERYSTRING_TYPE_INT; break;
387 case 'f': type = HTTP_QUERYSTRING_TYPE_FLOAT; break;
388 case 's': type = HTTP_QUERYSTRING_TYPE_STRING; break;
389 case 'a': type = HTTP_QUERYSTRING_TYPE_ARRAY; break;
390 case 'o': type = HTTP_QUERYSTRING_TYPE_OBJECT; break;
391 }
392 }
393 }
394 http_querystring_get(getThis(), type, name, name_len, defval, del, return_value);
395 } else {
396 RETURN_PROP(queryString);
397 }
398 }
399 }
400 /* }}} */
401
402 /* {{{ proto string HttpQueryString::set(mixed params)
403 *
404 * Set query string entry/entries. NULL values will unset the variable.
405 */
406 PHP_METHOD(HttpQueryString, set)
407 {
408 zval *params;
409
410 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params)) {
411 zval *qarray = GET_PROP(queryArray);
412 if (http_querystring_modify(qarray, params)) {
413 http_querystring_update(qarray, GET_PROP(queryString));
414 }
415 }
416
417 IF_RETVAL_USED {
418 RETURN_PROP(queryString);
419 }
420 }
421 /* }}} */
422
423 /* {{{ proto HttpQueryString HttpQueryString::getInstance([bool global = true])
424 *
425 * Get a single instance (differentiates between the global setting).
426 */
427 PHP_METHOD(HttpQueryString, getInstance)
428 {
429 zend_bool global = 1;
430 zval *instance = GET_STATIC_PROP(instance);
431
432 SET_EH_THROW_HTTP();
433 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
434 zval **zobj_ptr = NULL, *zobj = NULL;
435
436 if (Z_TYPE_P(instance) == IS_ARRAY) {
437 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void **) &zobj_ptr)) {
438 RETVAL_ZVAL(*zobj_ptr, 1, 0);
439 } else {
440 zobj = http_querystring_instantiate(global);
441 add_index_zval(instance, global, zobj);
442 RETVAL_OBJECT(zobj, 1);
443 }
444 } else {
445 MAKE_STD_ZVAL(instance);
446 array_init(instance);
447
448 zobj = http_querystring_instantiate(global);
449 add_index_zval(instance, global, zobj);
450 RETVAL_OBJECT(zobj, 1);
451
452 SET_STATIC_PROP(instance, instance);
453 zval_ptr_dtor(&instance);
454 }
455 }
456 SET_EH_NORMAL();
457 }
458 /* }}} */
459
460 /* {{{ Getters by type */
461 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
462 PHP_METHOD(HttpQueryString, method) \
463 { \
464 char *name; \
465 int name_len; \
466 zval *defval = NULL; \
467 zend_bool del = 0; \
468 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
469 http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
470 } \
471 }
472 HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL);
473 HTTP_QUERYSTRING_GETTER(getInt, IS_LONG);
474 HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE);
475 HTTP_QUERYSTRING_GETTER(getString, IS_STRING);
476 HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY);
477 HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT);
478 /* }}} */
479
480 #endif /* ZEND_ENGINE_2 */
481
482 /*
483 * Local variables:
484 * tab-width: 4
485 * c-basic-offset: 4
486 * End:
487 * vim600: noet sw=4 ts=4 fdm=marker
488 * vim<600: noet sw=4 ts=4
489 */
490