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