- fix builds where include/php/ext/iconv does not have php_have_*.h files
[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), 0, 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 #ifndef WONKY
197 #define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC)
198 static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC)
199 {
200 zval *zobj, *zglobal;
201
202 MAKE_STD_ZVAL(zglobal);
203 ZVAL_BOOL(zglobal, global);
204
205 MAKE_STD_ZVAL(zobj);
206 Z_TYPE_P(zobj) = IS_OBJECT;
207 Z_OBJVAL_P(zobj) = http_querystring_object_new(http_querystring_object_ce);
208 zend_call_method_with_1_params(&zobj, Z_OBJCE_P(zobj), NULL, "__construct", NULL, zglobal);
209
210 zval_ptr_dtor(&zglobal);
211
212 return zobj;
213 }
214 #endif /* WONKY */
215 static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_DC)
216 {
217 char *s = NULL;
218 size_t l = 0;
219
220 if (Z_TYPE_P(qarray) != IS_ARRAY) {
221 convert_to_array(qarray);
222 }
223 if (SUCCESS == http_urlencode_hash_ex(Z_ARRVAL_P(qarray), 0, NULL, 0, &s, &l)) {
224 zval_dtor(qstring);
225 ZVAL_STRINGL(qstring, s, l, 0);
226 } else {
227 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to update query string");
228 }
229 }
230 static inline int _http_querystring_modify_array_ex(zval *qarray, char *key, int keylen, zval *params_entry TSRMLS_DC)
231 {
232 zval **qarray_entry;
233
234 /* delete */
235 if (Z_TYPE_P(params_entry) == IS_NULL) {
236 return (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), key, keylen));
237 }
238
239 /* update */
240 if (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), key, keylen, (void **) &qarray_entry)) {
241 zval equal;
242
243 /* recursive */
244 if (Z_TYPE_P(params_entry) == IS_ARRAY) {
245 return http_querystring_modify_array(*qarray_entry, params_entry);
246 }
247 /* equal */
248 if ((SUCCESS == is_equal_function(&equal, *qarray_entry, params_entry TSRMLS_CC)) && Z_BVAL(equal)) {
249 return 0;
250 }
251 }
252
253 /* add */
254 ZVAL_ADDREF(params_entry);
255 add_assoc_zval_ex(qarray, key, keylen, params_entry);
256 return 1;
257 }
258 static inline int _http_querystring_modify_array(zval *qarray, zval *params TSRMLS_DC)
259 {
260 int rv = 0;
261 char *key;
262 uint keylen;
263 ulong idx;
264 HashPosition pos;
265 zval **params_entry;
266
267 FOREACH_KEYLENVAL(pos, params, key, keylen, idx, params_entry) {
268 if (key) {
269 if (http_querystring_modify_array_ex(qarray, key, keylen, *params_entry)) {
270 rv = 1;
271 }
272 } else {
273 keylen = spprintf(&key, 0, "%lu", idx);
274 if (http_querystring_modify_array_ex(qarray, key, keylen, *params_entry)) {
275 rv = 1;
276 }
277 efree(key);
278 }
279 key = NULL;
280 }
281
282 return rv;
283 }
284 static inline int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC)
285 {
286 if (Z_TYPE_P(params) == IS_ARRAY) {
287 return http_querystring_modify_array(qarray, params);
288 } else if (Z_TYPE_P(params) == IS_OBJECT) {
289 if (!instanceof_function(Z_OBJCE_P(params), http_querystring_object_ce TSRMLS_CC)) {
290 zval temp_array;
291 INIT_ZARR(temp_array, HASH_OF(params));
292 return http_querystring_modify_array(qarray, &temp_array);
293 }
294 return http_querystring_modify_array(qarray, GET_PROP_EX(params, queryArray));
295 } else {
296 int rv;
297 zval array;
298
299 INIT_PZVAL(&array);
300 array_init(&array);
301
302 ZVAL_ADDREF(params);
303 convert_to_string_ex(&params);
304 sapi_module.treat_data(PARSE_STRING, estrdup(Z_STRVAL_P(params)), &array TSRMLS_CC);
305 zval_ptr_dtor(&params);
306 rv = http_querystring_modify_array(qarray, &array);
307 zval_dtor(&array);
308 return rv;
309 }
310 }
311 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)
312 {
313 zval **arrval, *qarray = GET_PROP(queryArray);
314
315 if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void **) &arrval))) {
316 RETVAL_ZVAL(*arrval, 1, 0);
317
318 if (type) {
319 convert_to_type(type, return_value);
320 }
321
322 if (del && (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), name, name_len + 1))) {
323 http_querystring_update(qarray, GET_PROP(queryString));
324 }
325 } else if(defval) {
326 RETURN_ZVAL(defval, 1, 0);
327 }
328 }
329 /* }}} */
330
331 /* {{{ proto final void HttpQueryString::__construct([bool global = true[, mixed add])
332 *
333 * Creates a new HttpQueryString object instance.
334 * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE.
335 */
336 PHP_METHOD(HttpQueryString, __construct)
337 {
338 zend_bool global = 1;
339 zval *params = NULL, *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING;
340
341 SET_EH_THROW_HTTP();
342 if (!sapi_module.treat_data) {
343 http_error(HE_ERROR, HTTP_E_QUERYSTRING, "The SAPI does not have a treat_data function registered");
344 } else if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, &params)) {
345 if (global) {
346 if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &_SERVER)) &&
347 (Z_TYPE_PP(_SERVER) == IS_ARRAY) &&
348 (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void **) &QUERY_STRING))) {
349
350 qstring = *QUERY_STRING;
351
352 if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
353 qarray = *_GET;
354 } else {
355 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array");
356 }
357 } else {
358 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to QUERY_STRING");
359 }
360
361 if (qarray && qstring) {
362 if (Z_TYPE_P(qstring) != IS_STRING) {
363 convert_to_string(qstring);
364 }
365
366 SET_PROP(queryArray, qarray);
367 SET_PROP(queryString, qstring);
368 GET_PROP(queryArray)->is_ref = 1;
369 GET_PROP(queryString)->is_ref = 1;
370
371 if (params && http_querystring_modify(GET_PROP(queryArray), params)) {
372 http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString));
373 }
374 }
375 } else {
376 qarray = ecalloc(1, sizeof(zval));
377 array_init(qarray);
378
379 SET_PROP(queryArray, qarray);
380 UPD_STRL(queryString, "", 0);
381
382 if (params && http_querystring_modify(qarray, params)) {
383 http_querystring_update(qarray, GET_PROP(queryString));
384 }
385 }
386 }
387 SET_EH_NORMAL();
388 }
389 /* }}} */
390
391 /* {{{ proto string HttpQueryString::toString()
392 *
393 * Returns the string representation.
394 */
395 PHP_METHOD(HttpQueryString, toString)
396 {
397 NO_ARGS;
398 RETURN_PROP(queryString);
399 }
400 /* }}} */
401
402 /* {{{ proto array HttpQueryString::toArray()
403 *
404 * Returns the array representation.
405 */
406 PHP_METHOD(HttpQueryString, toArray)
407 {
408 NO_ARGS;
409 RETURN_PROP(queryArray);
410 }
411 /* }}} */
412
413 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
414 *
415 * Get (part of) the query string.
416 *
417 * The type parameter is either one of the HttpQueryString::TYPE_* constants or a type abbreviation like
418 * "b" for bool, "i" for int, "f" for float, "s" for string, "a" for array and "o" for a stdClass object.
419 */
420 PHP_METHOD(HttpQueryString, get)
421 {
422 char *name = NULL;
423 int name_len = 0;
424 long type = 0;
425 zend_bool del = 0;
426 zval *ztype = NULL, *defval = NULL;
427
428 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name, &name_len, &ztype, &defval, &del)) {
429 if (name && name_len) {
430 if (ztype) {
431 if (Z_TYPE_P(ztype) == IS_LONG) {
432 type = Z_LVAL_P(ztype);
433 } else if(Z_TYPE_P(ztype) == IS_STRING) {
434 switch (Z_STRVAL_P(ztype)[0])
435 {
436 case 'B':
437 case 'b': type = HTTP_QUERYSTRING_TYPE_BOOL; break;
438 case 'I':
439 case 'i': type = HTTP_QUERYSTRING_TYPE_INT; break;
440 case 'F':
441 case 'f': type = HTTP_QUERYSTRING_TYPE_FLOAT; break;
442 case 'S':
443 case 's': type = HTTP_QUERYSTRING_TYPE_STRING; break;
444 case 'A':
445 case 'a': type = HTTP_QUERYSTRING_TYPE_ARRAY; break;
446 case 'O':
447 case 'o': type = HTTP_QUERYSTRING_TYPE_OBJECT; break;
448 }
449 }
450 }
451 http_querystring_get(getThis(), type, name, name_len, defval, del, return_value);
452 } else {
453 RETURN_PROP(queryString);
454 }
455 }
456 }
457 /* }}} */
458
459 /* {{{ proto string HttpQueryString::set(mixed params)
460 *
461 * Set query string entry/entries. NULL values will unset the variable.
462 */
463 PHP_METHOD(HttpQueryString, set)
464 {
465 zval *params;
466
467 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params)) {
468 zval *qarray = GET_PROP(queryArray);
469 if (http_querystring_modify(qarray, params)) {
470 http_querystring_update(qarray, GET_PROP(queryString));
471 }
472 }
473
474 IF_RETVAL_USED {
475 RETURN_PROP(queryString);
476 }
477 }
478 /* }}} */
479
480 #ifndef WONKY
481 /* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true])
482 *
483 * Get a single instance (differentiates between the global setting).
484 */
485 PHP_METHOD(HttpQueryString, singleton)
486 {
487 zend_bool global = 1;
488 zval *instance = GET_STATIC_PROP(instance);
489
490 SET_EH_THROW_HTTP();
491 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
492 zval **zobj_ptr = NULL, *zobj = NULL;
493
494 if (Z_TYPE_P(instance) == IS_ARRAY) {
495 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void **) &zobj_ptr)) {
496 RETVAL_ZVAL(*zobj_ptr, 1, 0);
497 } else {
498 zobj = http_querystring_instantiate(global);
499 add_index_zval(instance, global, zobj);
500 RETVAL_OBJECT(zobj, 1);
501 }
502 } else {
503 MAKE_STD_ZVAL(instance);
504 array_init(instance);
505
506 zobj = http_querystring_instantiate(global);
507 add_index_zval(instance, global, zobj);
508 RETVAL_OBJECT(zobj, 1);
509
510 SET_STATIC_PROP(instance, instance);
511 zval_ptr_dtor(&instance);
512 }
513 }
514 SET_EH_NORMAL();
515 }
516 /* }}} */
517 #endif
518
519 /* {{{ Getters by type */
520 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
521 PHP_METHOD(HttpQueryString, method) \
522 { \
523 char *name; \
524 int name_len; \
525 zval *defval = NULL; \
526 zend_bool del = 0; \
527 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
528 http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
529 } \
530 }
531 HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL);
532 HTTP_QUERYSTRING_GETTER(getInt, IS_LONG);
533 HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE);
534 HTTP_QUERYSTRING_GETTER(getString, IS_STRING);
535 HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY);
536 HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT);
537 /* }}} */
538
539 #ifdef HAVE_ICONV
540 /* {{{ proto bool HttpQueryString::xlate(string ie, string oe)
541 *
542 * Converts the query string from the source encoding ie to the target encoding oe.
543 * WARNING: Don't use any character set that can contain NUL bytes like UTF-16.
544 *
545 * Returns TRUE on success or FALSE on failure.
546 */
547 PHP_METHOD(HttpQueryString, xlate)
548 {
549 char *ie, *oe, *er = NULL;
550 int ie_len, oe_len;
551 size_t er_len = 0;
552 zval *qa, *qs;
553
554 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len)) {
555 RETURN_FALSE;
556 }
557
558 qa = GET_PROP(queryArray);
559 qs = GET_PROP(queryString);
560 Z_STRLEN_P(qs) = php_url_decode(Z_STRVAL_P(qs), Z_STRLEN_P(qs));
561 if (PHP_ICONV_ERR_SUCCESS == php_iconv_string(Z_STRVAL_P(qs), (size_t) Z_STRLEN_P(qs), &er, &er_len, oe, ie)) {
562 efree(Z_STRVAL_P(qs));
563 ZVAL_STRINGL(qs, er, er_len, 0);
564 http_querystring_modify(qa, qs);
565 RETVAL_TRUE;
566 } else {
567 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);
568 RETVAL_FALSE;
569 }
570 http_querystring_update(qa, qs);
571 }
572 /* }}} */
573 #endif /* HAVE_ICONV */
574
575 /* {{{ proto string HttpQueryString::serialize()
576 *
577 * Implements Serializable.
578 */
579 PHP_METHOD(HttpQueryString, serialize)
580 {
581 NO_ARGS;
582 RETURN_PROP(queryString);
583 }
584 /* }}} */
585
586 /* {{{ proto void HttpQueryString::unserialize(string serialized)
587 *
588 * Implements Serializable.
589 */
590 PHP_METHOD(HttpQueryString, unserialize)
591 {
592 zval *serialized;
593
594 SET_EH_THROW_HTTP();
595 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &serialized)) {
596 if (Z_TYPE_P(serialized) == IS_STRING) {
597 zval *qa = GET_PROP(queryArray);
598
599 zend_hash_clean(Z_ARRVAL_P(qa));
600 http_querystring_modify(qa, serialized);
601 http_querystring_update(qa, GET_PROP(queryString));
602 } else {
603 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Expected a string as parameter");
604 }
605 }
606 SET_EH_NORMAL();
607 }
608 /* }}} */
609
610 #endif /* ZEND_ENGINE_2 */
611
612 /*
613 * Local variables:
614 * tab-width: 4
615 * c-basic-offset: 4
616 * End:
617 * vim600: noet sw=4 ts=4 fdm=marker
618 * vim<600: noet sw=4 ts=4
619 */
620