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-2007, Michael Wallner <mike@php.net> |
10 : +--------------------------------------------------------------------+
11 : */
12 :
13 : /* $Id: http_querystring_object.c,v 1.37 2007/02/22 11:04:37 mike Exp $ */
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 220 : {
148 220 : HTTP_REGISTER_CLASS_EX(HttpQueryString, http_querystring_object, NULL, 0);
149 :
150 : #ifndef WONKY
151 220 : zend_class_implements(http_querystring_object_ce TSRMLS_CC, 2, zend_ce_serializable, zend_ce_arrayaccess);
152 : #endif
153 :
154 220 : zend_declare_property_null(THIS_CE, ZEND_STRS("instance")-1, (ZEND_ACC_STATIC|ZEND_ACC_PRIVATE) TSRMLS_CC);
155 220 : zend_declare_property_null(THIS_CE, ZEND_STRS("queryArray")-1, ZEND_ACC_PRIVATE TSRMLS_CC);
156 220 : zend_declare_property_string(THIS_CE, ZEND_STRS("queryString")-1, "", ZEND_ACC_PRIVATE TSRMLS_CC);
157 :
158 : #ifndef WONKY
159 220 : zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_BOOL")-1, HTTP_QUERYSTRING_TYPE_BOOL TSRMLS_CC);
160 220 : zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_INT")-1, HTTP_QUERYSTRING_TYPE_INT TSRMLS_CC);
161 220 : zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_FLOAT")-1, HTTP_QUERYSTRING_TYPE_FLOAT TSRMLS_CC);
162 220 : zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_STRING")-1, HTTP_QUERYSTRING_TYPE_STRING TSRMLS_CC);
163 220 : zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_ARRAY")-1, HTTP_QUERYSTRING_TYPE_ARRAY TSRMLS_CC);
164 220 : zend_declare_class_constant_long(THIS_CE, ZEND_STRS("TYPE_OBJECT")-1, HTTP_QUERYSTRING_TYPE_OBJECT TSRMLS_CC);
165 : #endif
166 :
167 220 : HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
168 220 : HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
169 220 : HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
170 220 : HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
171 220 : HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
172 220 : HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
173 :
174 220 : return SUCCESS;
175 : }
176 :
177 : zend_object_value _http_querystring_object_new(zend_class_entry *ce TSRMLS_DC)
178 6 : {
179 6 : 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 6 : {
184 : zend_object_value ov;
185 : http_querystring_object *o;
186 :
187 6 : o = ecalloc(1, sizeof(http_querystring_object));
188 6 : o->zo.ce = ce;
189 :
190 6 : if (ptr) {
191 0 : *ptr = o;
192 : }
193 :
194 6 : ALLOC_HASHTABLE(OBJ_PROP(o));
195 6 : zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
196 6 : zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
197 :
198 6 : ov.handle = putObject(http_querystring_object, o);
199 6 : ov.handlers = &http_querystring_object_handlers;
200 :
201 6 : return ov;
202 : }
203 :
204 : void _http_querystring_object_free(zend_object *object TSRMLS_DC)
205 6 : {
206 6 : http_querystring_object *o = (http_querystring_object *) object;
207 :
208 6 : freeObject(o);
209 6 : }
210 :
211 : /* {{{ querystring helpers */
212 : #define http_querystring_instantiate(t, g, p, u) _http_querystring_instantiate((t), (g), (p), (u) TSRMLS_CC)
213 : static inline zval *_http_querystring_instantiate(zval *this_ptr, zend_bool global, zval *params, zend_bool defer_update TSRMLS_DC)
214 6 : {
215 6 : zval *qarray = NULL, *qstring = NULL, **_SERVER = NULL, **_GET = NULL, **QUERY_STRING = NULL;;
216 :
217 6 : if (!this_ptr) {
218 2 : MAKE_STD_ZVAL(this_ptr);
219 2 : Z_TYPE_P(this_ptr) = IS_OBJECT;
220 2 : this_ptr->value.obj = http_querystring_object_new(http_querystring_object_ce);
221 : }
222 6 : if (global) {
223 : #ifdef ZEND_ENGINE_2
224 1 : zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
225 : #endif
226 2 : if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &_SERVER)) &&
227 : (Z_TYPE_PP(_SERVER) == IS_ARRAY) &&
228 : (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void *) &QUERY_STRING))) {
229 :
230 1 : qstring = *QUERY_STRING;
231 : #ifdef ZEND_ENGINE_2
232 1 : zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC);
233 : #endif
234 2 : if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void *) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
235 1 : qarray = *_GET;
236 : } else {
237 0 : http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array");
238 : }
239 : } else {
240 0 : http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to QUERY_STRING");
241 : }
242 :
243 1 : if (qarray && qstring) {
244 1 : if (Z_TYPE_P(qstring) != IS_STRING) {
245 0 : convert_to_string(qstring);
246 : }
247 :
248 1 : zend_update_property(THIS_CE, getThis(), ZEND_STRS("queryArray")-1, qarray TSRMLS_CC);
249 1 : zend_update_property(THIS_CE, getThis(), ZEND_STRS("queryString")-1, qstring TSRMLS_CC);
250 1 : zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryArray")-1, 0 TSRMLS_CC)->is_ref = 1;
251 1 : zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryString")-1, 0 TSRMLS_CC)->is_ref = 1;
252 :
253 1 : if (params) {
254 0 : http_querystring_modify(zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryArray")-1, 0 TSRMLS_CC), params);
255 : }
256 1 : if (!defer_update) {
257 1 : 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));
258 : }
259 : }
260 : } else {
261 5 : qarray = ecalloc(1, sizeof(zval));
262 5 : array_init(qarray);
263 :
264 5 : zend_update_property(THIS_CE, getThis(), ZEND_STRS("queryArray")-1, qarray TSRMLS_CC);
265 5 : zend_update_property_stringl(THIS_CE, getThis(), ZEND_STRS("queryString")-1, "", 0 TSRMLS_CC);
266 :
267 5 : if (params && http_querystring_modify(qarray, params) && !defer_update) {
268 4 : http_querystring_update(qarray, zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryString")-1, 0 TSRMLS_CC));
269 : }
270 : }
271 :
272 6 : return this_ptr;
273 : }
274 :
275 : #define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC)
276 : 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)
277 22 : {
278 22 : zval **arrval, *qarray = zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryArray")-1, 0 TSRMLS_CC);
279 :
280 40 : if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void *) &arrval))) {
281 18 : RETVAL_ZVAL(*arrval, 1, 0);
282 :
283 18 : if (type) {
284 12 : convert_to_type(type, return_value);
285 : }
286 :
287 18 : if (del && (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), name, name_len + 1))) {
288 2 : http_querystring_update(qarray, zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryString")-1, 0 TSRMLS_CC));
289 : }
290 4 : } else if(defval) {
291 2 : RETURN_ZVAL(defval, 1, 0);
292 : }
293 : }
294 : /* }}} */
295 :
296 : /* {{{ proto final void HttpQueryString::__construct([bool global = true[, mixed add])
297 : Creates a new HttpQueryString object instance. Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE. */
298 : PHP_METHOD(HttpQueryString, __construct)
299 4 : {
300 4 : zend_bool global = 1;
301 4 : zval *params = NULL;
302 :
303 4 : SET_EH_THROW_HTTP();
304 4 : if (!sapi_module.treat_data) {
305 0 : http_error(HE_ERROR, HTTP_E_QUERYSTRING, "The SAPI does not have a treat_data function registered");
306 4 : } else if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, ¶ms)) {
307 4 : http_querystring_instantiate(getThis(), global, params, 0);
308 : }
309 4 : SET_EH_NORMAL();
310 4 : }
311 : /* }}} */
312 :
313 : /* {{{ proto HttpQueryString HttpQueryString::factory([bool global = TRUE[, mixed params[, string class_name = "HttpQueryString"])
314 : Creates a new HttpQueryString object instance. */
315 : PHP_METHOD(HttpQueryString, factory)
316 0 : {
317 0 : zend_bool global = 1;
318 0 : zval *params = NULL;
319 0 : char *cn = NULL;
320 0 : int cl = 0;
321 : zend_object_value ov;
322 :
323 0 : SET_EH_THROW_HTTP();
324 0 : if (!sapi_module.treat_data) {
325 0 : http_error(HE_ERROR, HTTP_E_QUERYSTRING, "The SAPI does not have a treat_data function registered");
326 0 : } else if ( SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bzs", &global, ¶ms, &cn, &cl) &&
327 : SUCCESS == http_object_new(&ov, cn, cl, _http_querystring_object_new_ex, http_querystring_object_ce, NULL, NULL)) {
328 0 : RETVAL_OBJVAL(ov, 0);
329 0 : http_querystring_instantiate(return_value, global, params, 0);
330 : }
331 0 : SET_EH_NORMAL();
332 0 : }
333 : /* }}} */
334 :
335 : /* {{{ proto string HttpQueryString::toString()
336 : Returns the string representation. */
337 : PHP_METHOD(HttpQueryString, toString)
338 0 : {
339 0 : NO_ARGS;
340 0 : RETURN_PROP(queryString);
341 : }
342 : /* }}} */
343 :
344 : /* {{{ proto array HttpQueryString::toArray()
345 : Returns the array representation. */
346 : PHP_METHOD(HttpQueryString, toArray)
347 0 : {
348 0 : NO_ARGS;
349 0 : RETURN_PROP(queryArray);
350 : }
351 : /* }}} */
352 :
353 : /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
354 : 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. */
355 : PHP_METHOD(HttpQueryString, get)
356 27 : {
357 27 : char *name = NULL;
358 27 : int name_len = 0;
359 27 : long type = 0;
360 27 : zend_bool del = 0;
361 27 : zval *ztype = NULL, *defval = NULL;
362 :
363 27 : if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name, &name_len, &ztype, &defval, &del)) {
364 49 : if (name && name_len) {
365 22 : if (ztype) {
366 14 : if (Z_TYPE_P(ztype) == IS_LONG) {
367 10 : type = Z_LVAL_P(ztype);
368 4 : } else if(Z_TYPE_P(ztype) == IS_STRING) {
369 4 : switch (Z_STRVAL_P(ztype)[0]) {
370 : case 'B':
371 0 : case 'b': type = HTTP_QUERYSTRING_TYPE_BOOL; break;
372 : case 'I':
373 2 : case 'i': type = HTTP_QUERYSTRING_TYPE_INT; break;
374 : case 'F':
375 0 : case 'f': type = HTTP_QUERYSTRING_TYPE_FLOAT; break;
376 : case 'S':
377 2 : case 's': type = HTTP_QUERYSTRING_TYPE_STRING; break;
378 : case 'A':
379 0 : case 'a': type = HTTP_QUERYSTRING_TYPE_ARRAY; break;
380 : case 'O':
381 0 : case 'o': type = HTTP_QUERYSTRING_TYPE_OBJECT; break;
382 : }
383 : }
384 : }
385 22 : http_querystring_get(getThis(), type, name, name_len, defval, del, return_value);
386 : } else {
387 5 : RETURN_PROP(queryString);
388 : }
389 : }
390 : }
391 : /* }}} */
392 :
393 : /* {{{ proto string HttpQueryString::set(mixed params)
394 : Set query string entry/entries. NULL values will unset the variable. */
395 : PHP_METHOD(HttpQueryString, set)
396 6 : {
397 : zval *params;
398 :
399 6 : if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", ¶ms)) {
400 6 : zval *qarray = zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryArray")-1, 0 TSRMLS_CC);
401 6 : if (http_querystring_modify(qarray, params)) {
402 6 : http_querystring_update(qarray, zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryString")-1, 0 TSRMLS_CC));
403 : }
404 : }
405 :
406 6 : if (return_value_used) {
407 0 : RETURN_PROP(queryString);
408 : }
409 : }
410 : /* }}} */
411 :
412 : /* {{{ proto HttpQueryString HttpQueryString::mod(mixed params)
413 : Copies the query string object and sets provided params at the clone. */
414 : PHP_METHOD(HttpQueryString, mod)
415 1 : {
416 : zval *zobj, *qarr, *qstr, *params;
417 :
418 1 : if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", ¶ms)) {
419 1 : zobj = http_querystring_instantiate(NULL, 0, zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryArray")-1, 0 TSRMLS_CC), 1);
420 1 : qarr = zend_read_property(THIS_CE, zobj, ZEND_STRS("queryArray")-1, 0 TSRMLS_CC);
421 1 : qstr = zend_read_property(THIS_CE, zobj, ZEND_STRS("queryString")-1, 0 TSRMLS_CC);
422 :
423 1 : http_querystring_modify(qarr, params);
424 1 : http_querystring_update(qarr, qstr);
425 :
426 1 : RETURN_ZVAL(zobj, 1, 1);
427 : }
428 : }
429 : /* }}} */
430 :
431 : #ifndef WONKY
432 : /* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true])
433 : Get a single instance (differentiates between the global setting). */
434 : PHP_METHOD(HttpQueryString, singleton)
435 16 : {
436 16 : zend_bool global = 1;
437 16 : zval *instance = *zend_std_get_static_property(THIS_CE, ZEND_STRS("instance")-1, 0 TSRMLS_CC);
438 :
439 16 : SET_EH_THROW_HTTP();
440 16 : if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
441 16 : zval **zobj_ptr = NULL, *zobj = NULL;
442 :
443 16 : if (Z_TYPE_P(instance) == IS_ARRAY) {
444 15 : if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void *) &zobj_ptr)) {
445 15 : RETVAL_ZVAL(*zobj_ptr, 1, 0);
446 : } else {
447 0 : zobj = http_querystring_instantiate(NULL, global, NULL, (zend_bool) !global);
448 0 : add_index_zval(instance, global, zobj);
449 0 : RETVAL_OBJECT(zobj, 1);
450 : }
451 : } else {
452 1 : MAKE_STD_ZVAL(instance);
453 1 : array_init(instance);
454 :
455 1 : zobj = http_querystring_instantiate(NULL, global, NULL, (zend_bool) !global);
456 1 : add_index_zval(instance, global, zobj);
457 1 : RETVAL_OBJECT(zobj, 1);
458 :
459 1 : zend_update_static_property(THIS_CE, ZEND_STRS("instance")-1, instance TSRMLS_CC);
460 1 : zval_ptr_dtor(&instance);
461 : }
462 : }
463 16 : SET_EH_NORMAL();
464 16 : }
465 : /* }}} */
466 : #endif
467 :
468 : /* {{{ Getters by type */
469 : #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
470 : PHP_METHOD(HttpQueryString, method) \
471 : { \
472 : char *name; \
473 : int name_len; \
474 : zval *defval = NULL; \
475 : zend_bool del = 0; \
476 : if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
477 : http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
478 : } \
479 : }
480 0 : HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL);
481 0 : HTTP_QUERYSTRING_GETTER(getInt, IS_LONG);
482 0 : HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE);
483 0 : HTTP_QUERYSTRING_GETTER(getString, IS_STRING);
484 0 : HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY);
485 0 : HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT);
486 : /* }}} */
487 :
488 : #ifdef HTTP_HAVE_ICONV
489 : /* {{{ proto bool HttpQueryString::xlate(string ie, string oe)
490 : 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. */
491 : PHP_METHOD(HttpQueryString, xlate)
492 2 : {
493 : char *ie, *oe;
494 : int ie_len, oe_len;
495 : zval xa, *qa, *qs;
496 : STATUS rs;
497 :
498 2 : if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len)) {
499 0 : RETURN_FALSE;
500 : }
501 :
502 2 : qa = zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryArray")-1, 0 TSRMLS_CC);
503 2 : qs = zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryString")-1, 0 TSRMLS_CC);
504 2 : INIT_PZVAL(&xa);
505 2 : array_init(&xa);
506 :
507 2 : if (SUCCESS == (rs = http_querystring_xlate(&xa, qa, ie, oe))) {
508 2 : zend_hash_clean(Z_ARRVAL_P(qa));
509 2 : zend_hash_copy(Z_ARRVAL_P(qa), Z_ARRVAL(xa), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
510 2 : http_querystring_update(qa, qs);
511 : }
512 2 : zval_dtor(&xa);
513 :
514 2 : RETURN_SUCCESS(rs);
515 : }
516 : /* }}} */
517 : #endif /* HAVE_ICONV */
518 :
519 : /* {{{ proto string HttpQueryString::serialize()
520 : Implements Serializable::serialize(). */
521 : PHP_METHOD(HttpQueryString, serialize)
522 0 : {
523 0 : NO_ARGS;
524 0 : RETURN_PROP(queryString);
525 : }
526 : /* }}} */
527 :
528 : /* {{{ proto void HttpQueryString::unserialize(string serialized)
529 : Implements Serializable::unserialize(). */
530 : PHP_METHOD(HttpQueryString, unserialize)
531 0 : {
532 : zval *serialized;
533 :
534 0 : SET_EH_THROW_HTTP();
535 0 : if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &serialized)) {
536 0 : if (Z_TYPE_P(serialized) == IS_STRING) {
537 0 : http_querystring_instantiate(getThis(), 0, serialized, 0);
538 : } else {
539 0 : http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Expected a string as parameter");
540 : }
541 : }
542 0 : SET_EH_NORMAL();
543 0 : }
544 : /* }}} */
545 :
546 : /* {{{ proto mixed HttpQueryString::offsetGet(string offset)
547 : Implements ArrayAccess::offsetGet(). */
548 : PHP_METHOD(HttpQueryString, offsetGet)
549 0 : {
550 : char *offset_str;
551 : int offset_len;
552 : zval **value;
553 :
554 0 : if ( (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &offset_str, &offset_len)) &&
555 : (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))) {
556 0 : RETVAL_ZVAL(*value, 1, 0);
557 : }
558 0 : }
559 : /* }}} */
560 :
561 : /* {{{ proto void HttpQueryString::offsetSet(string offset, mixed value)
562 : Implements ArrayAccess::offsetGet(). */
563 : PHP_METHOD(HttpQueryString, offsetSet)
564 0 : {
565 : char *offset_str;
566 : int offset_len;
567 : zval *value;
568 :
569 0 : if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &offset_str, &offset_len, &value)) {
570 0 : 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);
571 :
572 0 : ZVAL_ADDREF(value);
573 0 : add_assoc_zval_ex(qarr, offset_str, offset_len + 1, value);
574 0 : http_querystring_update(qarr, qstr);
575 : }
576 0 : }
577 : /* }}} */
578 :
579 : /* {{{ proto bool HttpQueryString::offsetExists(string offset)
580 : Implements ArrayAccess::offsetExists(). */
581 : PHP_METHOD(HttpQueryString, offsetExists)
582 0 : {
583 : char *offset_str;
584 : int offset_len;
585 : zval **value;
586 :
587 0 : if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &offset_str, &offset_len)) {
588 0 : 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));
589 : }
590 : }
591 : /* }}} */
592 :
593 : /* {{{ proto void HttpQueryString::offsetUnset(string offset)
594 : Implements ArrayAccess::offsetUnset(). */
595 : PHP_METHOD(HttpQueryString, offsetUnset)
596 0 : {
597 : char *offset_str;
598 : int offset_len;
599 :
600 0 : if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &offset_str, &offset_len)) {
601 0 : zval *qarr = zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryArray")-1, 0 TSRMLS_CC);
602 :
603 0 : if (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarr), offset_str, offset_len + 1)) {
604 0 : http_querystring_update(qarr, zend_read_property(THIS_CE, getThis(), ZEND_STRS("queryString")-1, 0 TSRMLS_CC));
605 : }
606 : }
607 0 : }
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 :
|