- use a more iterative approach in inflate code (instead of a retry-style)
[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 #ifdef HAVE_ICONV
24 # undef PHP_ATOM_INC
25 # include "ext/iconv/php_iconv.h"
26 # include "ext/standard/url.h"
27 #endif
28
29 #include "php_http_api.h"
30 #include "php_http_url_api.h"
31 #include "php_http_querystring_object.h"
32 #include "php_http_exception_object.h"
33
34 #define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpQueryString, method, 0, req_args)
35 #define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpQueryString, method, 0)
36 #define HTTP_QUERYSTRING_ME(method, visibility) PHP_ME(HttpQueryString, method, HTTP_ARGS(HttpQueryString, method), visibility)
37 #define HTTP_QUERYSTRING_GME(method, visibility) PHP_ME(HttpQueryString, method, HTTP_ARGS(HttpQueryString, __getter), visibility)
38
39 HTTP_BEGIN_ARGS(__construct, 0)
40 HTTP_ARG_VAL(global, 0)
41 HTTP_ARG_VAL(params, 0)
42 HTTP_END_ARGS;
43
44 #ifndef WONKY
45 HTTP_BEGIN_ARGS(singleton, 0)
46 HTTP_ARG_VAL(global, 0)
47 HTTP_END_ARGS;
48 #endif
49
50 HTTP_EMPTY_ARGS(toArray);
51 HTTP_EMPTY_ARGS(toString);
52
53 HTTP_BEGIN_ARGS(get, 0)
54 HTTP_ARG_VAL(name, 0)
55 HTTP_ARG_VAL(type, 0)
56 HTTP_ARG_VAL(defval, 0)
57 HTTP_ARG_VAL(delete, 0)
58 HTTP_END_ARGS;
59
60 HTTP_BEGIN_ARGS(set, 1)
61 HTTP_ARG_VAL(params, 0)
62 HTTP_END_ARGS;
63
64 HTTP_BEGIN_ARGS(__getter, 1)
65 HTTP_ARG_VAL(name, 0)
66 HTTP_ARG_VAL(defval, 0)
67 HTTP_ARG_VAL(delete, 0)
68 HTTP_END_ARGS;
69
70 #ifdef HAVE_ICONV
71 HTTP_BEGIN_ARGS(xlate, 2)
72 HTTP_ARG_VAL(from_encoding, 0)
73 HTTP_ARG_VAL(to_encoding, 0)
74 HTTP_END_ARGS;
75 #endif
76
77 HTTP_EMPTY_ARGS(serialize);
78 HTTP_BEGIN_ARGS(unserialize, 1)
79 HTTP_ARG_VAL(serialized, 0)
80 HTTP_END_ARGS;
81
82 #define OBJ_PROP_CE http_querystring_object_ce
83 zend_class_entry *http_querystring_object_ce;
84 zend_function_entry http_querystring_object_fe[] = {
85 HTTP_QUERYSTRING_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL)
86
87 HTTP_QUERYSTRING_ME(toArray, ZEND_ACC_PUBLIC)
88 HTTP_QUERYSTRING_ME(toString, ZEND_ACC_PUBLIC)
89 ZEND_MALIAS(HttpQueryString, __toString, toString, HTTP_ARGS(HttpQueryString, toString), ZEND_ACC_PUBLIC)
90
91 HTTP_QUERYSTRING_ME(get, ZEND_ACC_PUBLIC)
92 HTTP_QUERYSTRING_ME(set, ZEND_ACC_PUBLIC)
93
94 HTTP_QUERYSTRING_GME(getBool, ZEND_ACC_PUBLIC)
95 HTTP_QUERYSTRING_GME(getInt, ZEND_ACC_PUBLIC)
96 HTTP_QUERYSTRING_GME(getFloat, ZEND_ACC_PUBLIC)
97 HTTP_QUERYSTRING_GME(getString, ZEND_ACC_PUBLIC)
98 HTTP_QUERYSTRING_GME(getArray, ZEND_ACC_PUBLIC)
99 HTTP_QUERYSTRING_GME(getObject, ZEND_ACC_PUBLIC)
100
101 #ifndef WONKY
102 HTTP_QUERYSTRING_ME(singleton, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
103 #endif
104 #ifdef HAVE_ICONV
105 HTTP_QUERYSTRING_ME(xlate, ZEND_ACC_PUBLIC)
106 #endif
107
108 /* Implements Serializable */
109 HTTP_QUERYSTRING_ME(serialize, ZEND_ACC_PUBLIC)
110 HTTP_QUERYSTRING_ME(unserialize, ZEND_ACC_PUBLIC)
111
112 EMPTY_FUNCTION_ENTRY
113 };
114 static zend_object_handlers http_querystring_object_handlers;
115
116 PHP_MINIT_FUNCTION(http_querystring_object)
117 {
118 HTTP_REGISTER_CLASS_EX(HttpQueryString, http_querystring_object, NULL, 0);
119
120 #ifndef WONKY
121 zend_class_implements(http_querystring_object_ce TSRMLS_CC, 1, zend_ce_serializable);
122 #endif
123
124 DCL_STATIC_PROP_N(PRIVATE, instance);
125 DCL_PROP_N(PRIVATE, queryArray);
126 DCL_PROP(PRIVATE, string, queryString, "");
127
128 #ifndef WONKY
129 DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
130 DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
131 DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
132 DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
133 DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
134 DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
135 #endif
136
137 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
138 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
139 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
140 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
141 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
142 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
143
144 return SUCCESS;
145 }
146
147 zend_object_value _http_querystring_object_new(zend_class_entry *ce TSRMLS_DC)
148 {
149 return http_querystring_object_new_ex(ce, NULL);
150 }
151
152 zend_object_value _http_querystring_object_new_ex(zend_class_entry *ce, http_querystring_object **ptr TSRMLS_DC)
153 {
154 zend_object_value ov;
155 http_querystring_object *o;
156
157 o = ecalloc(1, sizeof(http_querystring_object));
158 o->zo.ce = ce;
159
160 if (ptr) {
161 *ptr = o;
162 }
163
164 ALLOC_HASHTABLE(OBJ_PROP(o));
165 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
166 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
167
168 ov.handle = putObject(http_querystring_object, o);
169 ov.handlers = &http_querystring_object_handlers;
170
171 return ov;
172 }
173
174 void _http_querystring_object_free(zend_object *object TSRMLS_DC)
175 {
176 http_querystring_object *o = (http_querystring_object *) object;
177
178 if (OBJ_PROP(o)) {
179 zend_hash_destroy(OBJ_PROP(o));
180 FREE_HASHTABLE(OBJ_PROP(o));
181 }
182 efree(o);
183 }
184
185 /* {{{ querystring helpers */
186 #define http_querystring_update(qa, qs) _http_querystring_update((qa), (qs) TSRMLS_CC)
187 static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_DC);
188 #define http_querystring_modify_array_ex(q, k, kl, pe) _http_querystring_modify_array_ex((q), (k), (kl), (pe) TSRMLS_CC)
189 static inline int _http_querystring_modify_array_ex(zval *qarray, char *key, int keylen, zval *params_entry TSRMLS_DC);
190 #define http_querystring_modify_array(q, p) _http_querystring_modify_array((q), (p) TSRMLS_CC)
191 static inline int _http_querystring_modify_array(zval *qarray, zval *params TSRMLS_DC);
192 #define http_querystring_modify(q, p) _http_querystring_modify((q), (p) TSRMLS_CC)
193 static inline int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC);
194 #define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC)
195 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);
196 #ifdef HAVE_ICONV
197 #define http_querystring_xlate(a, p, ie, oe) _http_querystring_xlate((a), (p), (ie), (oe) TSRMLS_CC)
198 static inline int _http_querystring_xlate(zval *array, zval *param, const char *ie, const char *oe TSRMLS_DC)
199 {
200 HashPosition pos;
201 zval **entry = NULL;
202 char *xlate_str = NULL, *xkey, *kstr = NULL;
203 size_t xlate_len = 0, xlen;
204 uint klen = 0;
205 ulong kidx = 0;
206
207 FOREACH_KEYLENVAL(pos, param, kstr, klen, kidx, entry) {
208 if (kstr) {
209 if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(kstr, klen-1, &xkey, &xlen, oe, ie)) {
210 http_error_ex(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to convert '%.*s' from '%s' to '%s'", klen-1, kstr, ie, oe);
211 return FAILURE;
212 }
213 }
214
215 if (Z_TYPE_PP(entry) == IS_STRING) {
216 if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry), &xlate_str, &xlate_len, oe, ie)) {
217 if (kstr) {
218 efree(xkey);
219 }
220 http_error_ex(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_PP(entry), Z_STRVAL_PP(entry), ie, oe);
221 return FAILURE;
222 }
223 if (kstr) {
224 add_assoc_stringl_ex(array, xkey, xlen+1, xlate_str, xlate_len, 0);
225 } else {
226 add_index_stringl(array, kidx, xlate_str, xlate_len, 0);
227 }
228 } else if (Z_TYPE_PP(entry) == IS_ARRAY) {
229 zval *subarray;
230
231 MAKE_STD_ZVAL(subarray);
232 array_init(subarray);
233 if (kstr) {
234 add_assoc_zval_ex(array, xkey, xlen+1, subarray);
235 } else {
236 add_index_zval(array, kidx, subarray);
237 }
238 if (SUCCESS != http_querystring_xlate(subarray, *entry, ie, oe)) {
239 if (kstr) {
240 efree(xkey);
241 }
242 return FAILURE;
243 }
244 }
245
246 if (kstr) {
247 kstr = NULL;
248 efree(xkey);
249 }
250 }
251 return SUCCESS;
252 }
253 #endif /* HAVE_ICONV */
254 #ifndef WONKY
255 #define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC)
256 static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC)
257 {
258 zval *zobj, *zglobal;
259
260 MAKE_STD_ZVAL(zglobal);
261 ZVAL_BOOL(zglobal, global);
262
263 MAKE_STD_ZVAL(zobj);
264 Z_TYPE_P(zobj) = IS_OBJECT;
265 Z_OBJVAL_P(zobj) = http_querystring_object_new(http_querystring_object_ce);
266 zend_call_method_with_1_params(&zobj, Z_OBJCE_P(zobj), NULL, "__construct", NULL, zglobal);
267
268 zval_ptr_dtor(&zglobal);
269
270 return zobj;
271 }
272 #endif /* WONKY */
273 static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_DC)
274 {
275 char *s = NULL;
276 size_t l = 0;
277
278 if (Z_TYPE_P(qarray) != IS_ARRAY) {
279 convert_to_array(qarray);
280 }
281 if (SUCCESS == http_urlencode_hash_ex(Z_ARRVAL_P(qarray), 0, NULL, 0, &s, &l)) {
282 zval_dtor(qstring);
283 ZVAL_STRINGL(qstring, s, l, 0);
284 } else {
285 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to update query string");
286 }
287 }
288 static inline int _http_querystring_modify_array_ex(zval *qarray, char *key, int keylen, zval *params_entry TSRMLS_DC)
289 {
290 zval **qarray_entry;
291
292 /* delete */
293 if (Z_TYPE_P(params_entry) == IS_NULL) {
294 return (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), key, keylen));
295 }
296
297 /* update */
298 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), key, keylen, (void *) &qarray_entry)) {
299 zval equal;
300
301 /* recursive */
302 if (Z_TYPE_P(params_entry) == IS_ARRAY) {
303 return http_querystring_modify_array(*qarray_entry, params_entry);
304 }
305 /* equal */
306 if ((SUCCESS == is_equal_function(&equal, *qarray_entry, params_entry TSRMLS_CC)) && Z_BVAL(equal)) {
307 return 0;
308 }
309 }
310
311 /* add */
312 ZVAL_ADDREF(params_entry);
313 add_assoc_zval_ex(qarray, key, keylen, params_entry);
314 return 1;
315 }
316 static inline int _http_querystring_modify_array(zval *qarray, zval *params TSRMLS_DC)
317 {
318 int rv = 0;
319 char *key;
320 uint keylen;
321 ulong idx;
322 HashPosition pos;
323 zval **params_entry;
324
325 FOREACH_KEYLENVAL(pos, params, key, keylen, idx, params_entry) {
326 if (key) {
327 if (http_querystring_modify_array_ex(qarray, key, keylen, *params_entry)) {
328 rv = 1;
329 }
330 } else {
331 keylen = spprintf(&key, 0, "%lu", idx);
332 if (http_querystring_modify_array_ex(qarray, key, keylen, *params_entry)) {
333 rv = 1;
334 }
335 efree(key);
336 }
337 key = NULL;
338 }
339
340 return rv;
341 }
342 static inline int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC)
343 {
344 if (Z_TYPE_P(params) == IS_ARRAY) {
345 return http_querystring_modify_array(qarray, params);
346 } else if (Z_TYPE_P(params) == IS_OBJECT) {
347 if (!instanceof_function(Z_OBJCE_P(params), http_querystring_object_ce TSRMLS_CC)) {
348 zval temp_array;
349 INIT_ZARR(temp_array, HASH_OF(params));
350 return http_querystring_modify_array(qarray, &temp_array);
351 }
352 return http_querystring_modify_array(qarray, GET_PROP_EX(params, queryArray));
353 } else {
354 int rv;
355 zval array;
356
357 INIT_PZVAL(&array);
358 array_init(&array);
359
360 ZVAL_ADDREF(params);
361 convert_to_string_ex(&params);
362 sapi_module.treat_data(PARSE_STRING, estrdup(Z_STRVAL_P(params)), &array TSRMLS_CC);
363 zval_ptr_dtor(&params);
364 rv = http_querystring_modify_array(qarray, &array);
365 zval_dtor(&array);
366 return rv;
367 }
368 }
369 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)
370 {
371 zval **arrval, *qarray = GET_PROP(queryArray);
372
373 if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void *) &arrval))) {
374 RETVAL_ZVAL(*arrval, 1, 0);
375
376 if (type) {
377 convert_to_type(type, return_value);
378 }
379
380 if (del && (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), name, name_len + 1))) {
381 http_querystring_update(qarray, GET_PROP(queryString));
382 }
383 } else if(defval) {
384 RETURN_ZVAL(defval, 1, 0);
385 }
386 }
387 /* }}} */
388
389 /* {{{ proto final void HttpQueryString::__construct([bool global = true[, mixed add])
390 *
391 * Creates a new HttpQueryString object instance.
392 * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE.
393 */
394 PHP_METHOD(HttpQueryString, __construct)
395 {
396 zend_bool global = 1;
397 zval *params = NULL, *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING;
398
399 SET_EH_THROW_HTTP();
400 if (!sapi_module.treat_data) {
401 http_error(HE_ERROR, HTTP_E_QUERYSTRING, "The SAPI does not have a treat_data function registered");
402 } else if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, &params)) {
403 if (global) {
404 #ifdef ZEND_ENGINE_2
405 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
406 #endif
407 if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &_SERVER)) &&
408 (Z_TYPE_PP(_SERVER) == IS_ARRAY) &&
409 (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void *) &QUERY_STRING))) {
410
411 qstring = *QUERY_STRING;
412 #ifdef ZEND_ENGINE_2
413 zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC);
414 #endif
415 if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void *) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
416 qarray = *_GET;
417 } else {
418 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array");
419 }
420 } else {
421 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to QUERY_STRING");
422 }
423
424 if (qarray && qstring) {
425 if (Z_TYPE_P(qstring) != IS_STRING) {
426 convert_to_string(qstring);
427 }
428
429 SET_PROP(queryArray, qarray);
430 SET_PROP(queryString, qstring);
431 GET_PROP(queryArray)->is_ref = 1;
432 GET_PROP(queryString)->is_ref = 1;
433
434 if (params) {
435 http_querystring_modify(GET_PROP(queryArray), params);
436 }
437 http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString));
438 }
439 } else {
440 qarray = ecalloc(1, sizeof(zval));
441 array_init(qarray);
442
443 SET_PROP(queryArray, qarray);
444 UPD_STRL(queryString, "", 0);
445
446 if (params && http_querystring_modify(qarray, params)) {
447 http_querystring_update(qarray, GET_PROP(queryString));
448 }
449 }
450 }
451 SET_EH_NORMAL();
452 }
453 /* }}} */
454
455 /* {{{ proto string HttpQueryString::toString()
456 *
457 * Returns the string representation.
458 */
459 PHP_METHOD(HttpQueryString, toString)
460 {
461 NO_ARGS;
462 RETURN_PROP(queryString);
463 }
464 /* }}} */
465
466 /* {{{ proto array HttpQueryString::toArray()
467 *
468 * Returns the array representation.
469 */
470 PHP_METHOD(HttpQueryString, toArray)
471 {
472 NO_ARGS;
473 RETURN_PROP(queryArray);
474 }
475 /* }}} */
476
477 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
478 *
479 * Get (part of) the query string.
480 *
481 * The type parameter is either one of the HttpQueryString::TYPE_* constants or a type abbreviation like
482 * "b" for bool, "i" for int, "f" for float, "s" for string, "a" for array and "o" for a stdClass object.
483 */
484 PHP_METHOD(HttpQueryString, get)
485 {
486 char *name = NULL;
487 int name_len = 0;
488 long type = 0;
489 zend_bool del = 0;
490 zval *ztype = NULL, *defval = NULL;
491
492 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name, &name_len, &ztype, &defval, &del)) {
493 if (name && name_len) {
494 if (ztype) {
495 if (Z_TYPE_P(ztype) == IS_LONG) {
496 type = Z_LVAL_P(ztype);
497 } else if(Z_TYPE_P(ztype) == IS_STRING) {
498 switch (Z_STRVAL_P(ztype)[0])
499 {
500 case 'B':
501 case 'b': type = HTTP_QUERYSTRING_TYPE_BOOL; break;
502 case 'I':
503 case 'i': type = HTTP_QUERYSTRING_TYPE_INT; break;
504 case 'F':
505 case 'f': type = HTTP_QUERYSTRING_TYPE_FLOAT; break;
506 case 'S':
507 case 's': type = HTTP_QUERYSTRING_TYPE_STRING; break;
508 case 'A':
509 case 'a': type = HTTP_QUERYSTRING_TYPE_ARRAY; break;
510 case 'O':
511 case 'o': type = HTTP_QUERYSTRING_TYPE_OBJECT; break;
512 }
513 }
514 }
515 http_querystring_get(getThis(), type, name, name_len, defval, del, return_value);
516 } else {
517 RETURN_PROP(queryString);
518 }
519 }
520 }
521 /* }}} */
522
523 /* {{{ proto string HttpQueryString::set(mixed params)
524 *
525 * Set query string entry/entries. NULL values will unset the variable.
526 */
527 PHP_METHOD(HttpQueryString, set)
528 {
529 zval *params;
530
531 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params)) {
532 zval *qarray = GET_PROP(queryArray);
533 if (http_querystring_modify(qarray, params)) {
534 http_querystring_update(qarray, GET_PROP(queryString));
535 }
536 }
537
538 IF_RETVAL_USED {
539 RETURN_PROP(queryString);
540 }
541 }
542 /* }}} */
543
544 #ifndef WONKY
545 /* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true])
546 *
547 * Get a single instance (differentiates between the global setting).
548 */
549 PHP_METHOD(HttpQueryString, singleton)
550 {
551 zend_bool global = 1;
552 zval *instance = GET_STATIC_PROP(instance);
553
554 SET_EH_THROW_HTTP();
555 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
556 zval **zobj_ptr = NULL, *zobj = NULL;
557
558 if (Z_TYPE_P(instance) == IS_ARRAY) {
559 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void *) &zobj_ptr)) {
560 RETVAL_ZVAL(*zobj_ptr, 1, 0);
561 } else {
562 zobj = http_querystring_instantiate(global);
563 add_index_zval(instance, global, zobj);
564 RETVAL_OBJECT(zobj, 1);
565 }
566 } else {
567 MAKE_STD_ZVAL(instance);
568 array_init(instance);
569
570 zobj = http_querystring_instantiate(global);
571 add_index_zval(instance, global, zobj);
572 RETVAL_OBJECT(zobj, 1);
573
574 SET_STATIC_PROP(instance, instance);
575 zval_ptr_dtor(&instance);
576 }
577 }
578 SET_EH_NORMAL();
579 }
580 /* }}} */
581 #endif
582
583 /* {{{ Getters by type */
584 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
585 PHP_METHOD(HttpQueryString, method) \
586 { \
587 char *name; \
588 int name_len; \
589 zval *defval = NULL; \
590 zend_bool del = 0; \
591 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
592 http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
593 } \
594 }
595 HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL);
596 HTTP_QUERYSTRING_GETTER(getInt, IS_LONG);
597 HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE);
598 HTTP_QUERYSTRING_GETTER(getString, IS_STRING);
599 HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY);
600 HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT);
601 /* }}} */
602
603 #ifdef HAVE_ICONV
604 /* {{{ proto bool HttpQueryString::xlate(string ie, string oe)
605 *
606 * Converts the query string from the source encoding ie to the target encoding oe.
607 * WARNING: Don't use any character set that can contain NUL bytes like UTF-16.
608 *
609 * Returns TRUE on success or FALSE on failure.
610 */
611 PHP_METHOD(HttpQueryString, xlate)
612 {
613 char *ie, *oe;
614 int ie_len, oe_len;
615 zval xa, *qa, *qs;
616 STATUS rs;
617
618 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len)) {
619 RETURN_FALSE;
620 }
621
622 qa = GET_PROP(queryArray);
623 qs = GET_PROP(queryString);
624 INIT_PZVAL(&xa);
625 array_init(&xa);
626
627 if (SUCCESS == (rs = http_querystring_xlate(&xa, qa, ie, oe))) {
628 zend_hash_clean(Z_ARRVAL_P(qa));
629 zend_hash_copy(Z_ARRVAL_P(qa), Z_ARRVAL(xa), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
630 http_querystring_update(qa, qs);
631 }
632 zval_dtor(&xa);
633
634 RETURN_SUCCESS(rs);
635 }
636 /* }}} */
637 #endif /* HAVE_ICONV */
638
639 /* {{{ proto string HttpQueryString::serialize()
640 *
641 * Implements Serializable.
642 */
643 PHP_METHOD(HttpQueryString, serialize)
644 {
645 NO_ARGS;
646 RETURN_PROP(queryString);
647 }
648 /* }}} */
649
650 /* {{{ proto void HttpQueryString::unserialize(string serialized)
651 *
652 * Implements Serializable.
653 */
654 PHP_METHOD(HttpQueryString, unserialize)
655 {
656 zval *serialized;
657
658 SET_EH_THROW_HTTP();
659 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &serialized)) {
660 if (Z_TYPE_P(serialized) == IS_STRING) {
661 zval *qa = GET_PROP(queryArray);
662
663 zend_hash_clean(Z_ARRVAL_P(qa));
664 http_querystring_modify(qa, serialized);
665 http_querystring_update(qa, GET_PROP(queryString));
666 } else {
667 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Expected a string as parameter");
668 }
669 }
670 SET_EH_NORMAL();
671 }
672 /* }}} */
673
674 #endif /* ZEND_ENGINE_2 */
675
676 /*
677 * Local variables:
678 * tab-width: 4
679 * c-basic-offset: 4
680 * End:
681 * vim600: noet sw=4 ts=4 fdm=marker
682 * vim<600: noet sw=4 ts=4
683 */
684