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