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