- fix HttpQueryString::xlate()
[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 #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 if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &_SERVER)) &&
405 (Z_TYPE_PP(_SERVER) == IS_ARRAY) &&
406 (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void **) &QUERY_STRING))) {
407
408 qstring = *QUERY_STRING;
409
410 if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
411 qarray = *_GET;
412 } else {
413 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array");
414 }
415 } else {
416 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to QUERY_STRING");
417 }
418
419 if (qarray && qstring) {
420 if (Z_TYPE_P(qstring) != IS_STRING) {
421 convert_to_string(qstring);
422 }
423
424 SET_PROP(queryArray, qarray);
425 SET_PROP(queryString, qstring);
426 GET_PROP(queryArray)->is_ref = 1;
427 GET_PROP(queryString)->is_ref = 1;
428
429 if (params) {
430 http_querystring_modify(GET_PROP(queryArray), params);
431 }
432 http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString));
433 }
434 } else {
435 qarray = ecalloc(1, sizeof(zval));
436 array_init(qarray);
437
438 SET_PROP(queryArray, qarray);
439 UPD_STRL(queryString, "", 0);
440
441 if (params && http_querystring_modify(qarray, params)) {
442 http_querystring_update(qarray, GET_PROP(queryString));
443 }
444 }
445 }
446 SET_EH_NORMAL();
447 }
448 /* }}} */
449
450 /* {{{ proto string HttpQueryString::toString()
451 *
452 * Returns the string representation.
453 */
454 PHP_METHOD(HttpQueryString, toString)
455 {
456 NO_ARGS;
457 RETURN_PROP(queryString);
458 }
459 /* }}} */
460
461 /* {{{ proto array HttpQueryString::toArray()
462 *
463 * Returns the array representation.
464 */
465 PHP_METHOD(HttpQueryString, toArray)
466 {
467 NO_ARGS;
468 RETURN_PROP(queryArray);
469 }
470 /* }}} */
471
472 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
473 *
474 * Get (part of) the query string.
475 *
476 * The type parameter is either one of the HttpQueryString::TYPE_* constants or a type abbreviation like
477 * "b" for bool, "i" for int, "f" for float, "s" for string, "a" for array and "o" for a stdClass object.
478 */
479 PHP_METHOD(HttpQueryString, get)
480 {
481 char *name = NULL;
482 int name_len = 0;
483 long type = 0;
484 zend_bool del = 0;
485 zval *ztype = NULL, *defval = NULL;
486
487 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name, &name_len, &ztype, &defval, &del)) {
488 if (name && name_len) {
489 if (ztype) {
490 if (Z_TYPE_P(ztype) == IS_LONG) {
491 type = Z_LVAL_P(ztype);
492 } else if(Z_TYPE_P(ztype) == IS_STRING) {
493 switch (Z_STRVAL_P(ztype)[0])
494 {
495 case 'B':
496 case 'b': type = HTTP_QUERYSTRING_TYPE_BOOL; break;
497 case 'I':
498 case 'i': type = HTTP_QUERYSTRING_TYPE_INT; break;
499 case 'F':
500 case 'f': type = HTTP_QUERYSTRING_TYPE_FLOAT; break;
501 case 'S':
502 case 's': type = HTTP_QUERYSTRING_TYPE_STRING; break;
503 case 'A':
504 case 'a': type = HTTP_QUERYSTRING_TYPE_ARRAY; break;
505 case 'O':
506 case 'o': type = HTTP_QUERYSTRING_TYPE_OBJECT; break;
507 }
508 }
509 }
510 http_querystring_get(getThis(), type, name, name_len, defval, del, return_value);
511 } else {
512 RETURN_PROP(queryString);
513 }
514 }
515 }
516 /* }}} */
517
518 /* {{{ proto string HttpQueryString::set(mixed params)
519 *
520 * Set query string entry/entries. NULL values will unset the variable.
521 */
522 PHP_METHOD(HttpQueryString, set)
523 {
524 zval *params;
525
526 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params)) {
527 zval *qarray = GET_PROP(queryArray);
528 if (http_querystring_modify(qarray, params)) {
529 http_querystring_update(qarray, GET_PROP(queryString));
530 }
531 }
532
533 IF_RETVAL_USED {
534 RETURN_PROP(queryString);
535 }
536 }
537 /* }}} */
538
539 #ifndef WONKY
540 /* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true])
541 *
542 * Get a single instance (differentiates between the global setting).
543 */
544 PHP_METHOD(HttpQueryString, singleton)
545 {
546 zend_bool global = 1;
547 zval *instance = GET_STATIC_PROP(instance);
548
549 SET_EH_THROW_HTTP();
550 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
551 zval **zobj_ptr = NULL, *zobj = NULL;
552
553 if (Z_TYPE_P(instance) == IS_ARRAY) {
554 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void **) &zobj_ptr)) {
555 RETVAL_ZVAL(*zobj_ptr, 1, 0);
556 } else {
557 zobj = http_querystring_instantiate(global);
558 add_index_zval(instance, global, zobj);
559 RETVAL_OBJECT(zobj, 1);
560 }
561 } else {
562 MAKE_STD_ZVAL(instance);
563 array_init(instance);
564
565 zobj = http_querystring_instantiate(global);
566 add_index_zval(instance, global, zobj);
567 RETVAL_OBJECT(zobj, 1);
568
569 SET_STATIC_PROP(instance, instance);
570 zval_ptr_dtor(&instance);
571 }
572 }
573 SET_EH_NORMAL();
574 }
575 /* }}} */
576 #endif
577
578 /* {{{ Getters by type */
579 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
580 PHP_METHOD(HttpQueryString, method) \
581 { \
582 char *name; \
583 int name_len; \
584 zval *defval = NULL; \
585 zend_bool del = 0; \
586 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
587 http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
588 } \
589 }
590 HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL);
591 HTTP_QUERYSTRING_GETTER(getInt, IS_LONG);
592 HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE);
593 HTTP_QUERYSTRING_GETTER(getString, IS_STRING);
594 HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY);
595 HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT);
596 /* }}} */
597
598 #ifdef HAVE_ICONV
599 /* {{{ proto bool HttpQueryString::xlate(string ie, string oe)
600 *
601 * Converts the query string from the source encoding ie to the target encoding oe.
602 * WARNING: Don't use any character set that can contain NUL bytes like UTF-16.
603 *
604 * Returns TRUE on success or FALSE on failure.
605 */
606 PHP_METHOD(HttpQueryString, xlate)
607 {
608 char *ie, *oe;
609 int ie_len, oe_len;
610 zval xa, *qa, *qs;
611 STATUS rs;
612
613 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len)) {
614 RETURN_FALSE;
615 }
616
617 qa = GET_PROP(queryArray);
618 qs = GET_PROP(queryString);
619 INIT_PZVAL(&xa);
620 array_init(&xa);
621
622 if (SUCCESS == (rs = http_querystring_xlate(&xa, qa, ie, oe))) {
623 zend_hash_clean(Z_ARRVAL_P(qa));
624 zend_hash_copy(Z_ARRVAL_P(qa), Z_ARRVAL(xa), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
625 http_querystring_update(qa, qs);
626 }
627 zval_dtor(&xa);
628
629 RETURN_SUCCESS(rs);
630 }
631 /* }}} */
632 #endif /* HAVE_ICONV */
633
634 /* {{{ proto string HttpQueryString::serialize()
635 *
636 * Implements Serializable.
637 */
638 PHP_METHOD(HttpQueryString, serialize)
639 {
640 NO_ARGS;
641 RETURN_PROP(queryString);
642 }
643 /* }}} */
644
645 /* {{{ proto void HttpQueryString::unserialize(string serialized)
646 *
647 * Implements Serializable.
648 */
649 PHP_METHOD(HttpQueryString, unserialize)
650 {
651 zval *serialized;
652
653 SET_EH_THROW_HTTP();
654 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &serialized)) {
655 if (Z_TYPE_P(serialized) == IS_STRING) {
656 zval *qa = GET_PROP(queryArray);
657
658 zend_hash_clean(Z_ARRVAL_P(qa));
659 http_querystring_modify(qa, serialized);
660 http_querystring_update(qa, GET_PROP(queryString));
661 } else {
662 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Expected a string as parameter");
663 }
664 }
665 SET_EH_NORMAL();
666 }
667 /* }}} */
668
669 #endif /* ZEND_ENGINE_2 */
670
671 /*
672 * Local variables:
673 * tab-width: 4
674 * c-basic-offset: 4
675 * End:
676 * vim600: noet sw=4 ts=4 fdm=marker
677 * vim<600: noet sw=4 ts=4
678 */
679