2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
15 #define HTTP_WANT_SAPI
20 #include "php_variables.h"
21 #include "zend_interfaces.h"
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"
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)
33 HTTP_BEGIN_ARGS(__construct
, 0)
34 HTTP_ARG_VAL(global
, 0)
35 HTTP_ARG_VAL(params
, 0)
39 HTTP_BEGIN_ARGS(singleton
, 0)
40 HTTP_ARG_VAL(global
, 0)
44 HTTP_EMPTY_ARGS(toArray
);
45 HTTP_EMPTY_ARGS(toString
);
47 HTTP_BEGIN_ARGS(get
, 0)
50 HTTP_ARG_VAL(defval
, 0)
51 HTTP_ARG_VAL(delete, 0)
54 HTTP_BEGIN_ARGS(set
, 1)
55 HTTP_ARG_VAL(params
, 0)
58 HTTP_BEGIN_ARGS(mod
, 0)
59 HTTP_ARG_VAL(params
, 0)
62 HTTP_BEGIN_ARGS(__getter
, 1)
64 HTTP_ARG_VAL(defval
, 0)
65 HTTP_ARG_VAL(delete, 0)
68 #ifdef HTTP_HAVE_ICONV
69 HTTP_BEGIN_ARGS(xlate
, 2)
70 HTTP_ARG_VAL(from_encoding
, 0)
71 HTTP_ARG_VAL(to_encoding
, 0)
75 HTTP_EMPTY_ARGS(serialize
);
76 HTTP_BEGIN_ARGS(unserialize
, 1)
77 HTTP_ARG_VAL(serialized
, 0)
80 #define OBJ_PROP_CE http_querystring_object_ce
81 zend_class_entry
*http_querystring_object_ce
;
82 zend_function_entry http_querystring_object_fe
[] = {
83 HTTP_QUERYSTRING_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
|ZEND_ACC_FINAL
)
85 HTTP_QUERYSTRING_ME(toArray
, ZEND_ACC_PUBLIC
)
86 HTTP_QUERYSTRING_ME(toString
, ZEND_ACC_PUBLIC
)
87 ZEND_MALIAS(HttpQueryString
, __toString
, toString
, HTTP_ARGS(HttpQueryString
, toString
), ZEND_ACC_PUBLIC
)
89 HTTP_QUERYSTRING_ME(get
, ZEND_ACC_PUBLIC
)
90 HTTP_QUERYSTRING_ME(set
, ZEND_ACC_PUBLIC
)
91 HTTP_QUERYSTRING_ME(mod
, ZEND_ACC_PUBLIC
)
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
)
101 HTTP_QUERYSTRING_ME(singleton
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
103 #ifdef HTTP_HAVE_ICONV
104 HTTP_QUERYSTRING_ME(xlate
, ZEND_ACC_PUBLIC
)
107 /* Implements Serializable */
108 HTTP_QUERYSTRING_ME(serialize
, ZEND_ACC_PUBLIC
)
109 HTTP_QUERYSTRING_ME(unserialize
, ZEND_ACC_PUBLIC
)
113 static zend_object_handlers http_querystring_object_handlers
;
115 PHP_MINIT_FUNCTION(http_querystring_object
)
117 HTTP_REGISTER_CLASS_EX(HttpQueryString
, http_querystring_object
, NULL
, 0);
120 zend_class_implements(http_querystring_object_ce TSRMLS_CC
, 1, zend_ce_serializable
);
123 DCL_STATIC_PROP_N(PRIVATE
, instance
);
124 DCL_PROP_N(PRIVATE
, queryArray
);
125 DCL_PROP(PRIVATE
, string
, queryString
, "");
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
);
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
);
146 zend_object_value
_http_querystring_object_new(zend_class_entry
*ce TSRMLS_DC
)
148 return http_querystring_object_new_ex(ce
, NULL
);
151 zend_object_value
_http_querystring_object_new_ex(zend_class_entry
*ce
, http_querystring_object
**ptr TSRMLS_DC
)
153 zend_object_value ov
;
154 http_querystring_object
*o
;
156 o
= ecalloc(1, sizeof(http_querystring_object
));
163 ALLOC_HASHTABLE(OBJ_PROP(o
));
164 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), 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
*));
167 ov
.handle
= putObject(http_querystring_object
, o
);
168 ov
.handlers
= &http_querystring_object_handlers
;
173 void _http_querystring_object_free(zend_object
*object TSRMLS_DC
)
175 http_querystring_object
*o
= (http_querystring_object
*) object
;
178 zend_hash_destroy(OBJ_PROP(o
));
179 FREE_HASHTABLE(OBJ_PROP(o
));
184 /* {{{ querystring helpers */
185 #define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC)
186 static inline zval
*_http_querystring_instantiate(zend_bool global TSRMLS_DC
)
188 zval
*zobj
, *zglobal
;
190 MAKE_STD_ZVAL(zglobal
);
191 ZVAL_BOOL(zglobal
, global
);
194 Z_TYPE_P(zobj
) = IS_OBJECT
;
195 Z_OBJVAL_P(zobj
) = http_querystring_object_new(http_querystring_object_ce
);
196 zend_call_method_with_1_params(&zobj
, Z_OBJCE_P(zobj
), NULL
, "__construct", NULL
, zglobal
);
198 zval_ptr_dtor(&zglobal
);
203 #define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC)
204 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
)
206 zval
**arrval
, *qarray
= GET_PROP(queryArray
);
208 if ((Z_TYPE_P(qarray
) == IS_ARRAY
) && (SUCCESS
== zend_hash_find(Z_ARRVAL_P(qarray
), name
, name_len
+ 1, (void *) &arrval
))) {
209 RETVAL_ZVAL(*arrval
, 1, 0);
212 convert_to_type(type
, return_value
);
215 if (del
&& (SUCCESS
== zend_hash_del(Z_ARRVAL_P(qarray
), name
, name_len
+ 1))) {
216 http_querystring_update(qarray
, GET_PROP(queryString
));
219 RETURN_ZVAL(defval
, 1, 0);
224 /* {{{ proto final void HttpQueryString::__construct([bool global = true[, mixed add])
226 * Creates a new HttpQueryString object instance.
227 * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE.
229 PHP_METHOD(HttpQueryString
, __construct
)
231 zend_bool global
= 1;
232 zval
*params
= NULL
, *qarray
= NULL
, *qstring
= NULL
, **_GET
, **_SERVER
, **QUERY_STRING
;
235 if (!sapi_module
.treat_data
) {
236 http_error(HE_ERROR
, HTTP_E_QUERYSTRING
, "The SAPI does not have a treat_data function registered");
237 } else if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bz", &global
, ¶ms
)) {
240 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC
);
242 if ( (SUCCESS
== zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void *) &_SERVER
)) &&
243 (Z_TYPE_PP(_SERVER
) == IS_ARRAY
) &&
244 (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(_SERVER
), "QUERY_STRING", sizeof("QUERY_STRING"), (void *) &QUERY_STRING
))) {
246 qstring
= *QUERY_STRING
;
248 zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC
);
250 if ((SUCCESS
== zend_hash_find(&EG(symbol_table
), "_GET", sizeof("_GET"), (void *) &_GET
)) && (Z_TYPE_PP(_GET
) == IS_ARRAY
)) {
253 http_error(HE_WARNING
, HTTP_E_QUERYSTRING
, "Could not acquire reference to superglobal GET array");
256 http_error(HE_WARNING
, HTTP_E_QUERYSTRING
, "Could not acquire reference to QUERY_STRING");
259 if (qarray
&& qstring
) {
260 if (Z_TYPE_P(qstring
) != IS_STRING
) {
261 convert_to_string(qstring
);
264 SET_PROP(queryArray
, qarray
);
265 SET_PROP(queryString
, qstring
);
266 GET_PROP(queryArray
)->is_ref
= 1;
267 GET_PROP(queryString
)->is_ref
= 1;
270 http_querystring_modify(GET_PROP(queryArray
), params
);
272 http_querystring_update(GET_PROP(queryArray
), GET_PROP(queryString
));
275 qarray
= ecalloc(1, sizeof(zval
));
278 SET_PROP(queryArray
, qarray
);
279 UPD_STRL(queryString
, "", 0);
281 if (params
&& http_querystring_modify(qarray
, params
)) {
282 http_querystring_update(qarray
, GET_PROP(queryString
));
290 /* {{{ proto string HttpQueryString::toString()
292 * Returns the string representation.
294 PHP_METHOD(HttpQueryString
, toString
)
297 RETURN_PROP(queryString
);
301 /* {{{ proto array HttpQueryString::toArray()
303 * Returns the array representation.
305 PHP_METHOD(HttpQueryString
, toArray
)
308 RETURN_PROP(queryArray
);
312 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
314 * Get (part of) the query string.
316 * The type parameter is either one of the HttpQueryString::TYPE_* constants or a type abbreviation like
317 * "b" for bool, "i" for int, "f" for float, "s" for string, "a" for array and "o" for a stdClass object.
319 PHP_METHOD(HttpQueryString
, get
)
325 zval
*ztype
= NULL
, *defval
= NULL
;
327 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|szzb", &name
, &name_len
, &ztype
, &defval
, &del
)) {
328 if (name
&& name_len
) {
330 if (Z_TYPE_P(ztype
) == IS_LONG
) {
331 type
= Z_LVAL_P(ztype
);
332 } else if(Z_TYPE_P(ztype
) == IS_STRING
) {
333 switch (Z_STRVAL_P(ztype
)[0]) {
335 case 'b': type
= HTTP_QUERYSTRING_TYPE_BOOL
; break;
337 case 'i': type
= HTTP_QUERYSTRING_TYPE_INT
; break;
339 case 'f': type
= HTTP_QUERYSTRING_TYPE_FLOAT
; break;
341 case 's': type
= HTTP_QUERYSTRING_TYPE_STRING
; break;
343 case 'a': type
= HTTP_QUERYSTRING_TYPE_ARRAY
; break;
345 case 'o': type
= HTTP_QUERYSTRING_TYPE_OBJECT
; break;
349 http_querystring_get(getThis(), type
, name
, name_len
, defval
, del
, return_value
);
351 RETURN_PROP(queryString
);
357 /* {{{ proto string HttpQueryString::set(mixed params)
359 * Set query string entry/entries. NULL values will unset the variable.
361 PHP_METHOD(HttpQueryString
, set
)
365 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", ¶ms
)) {
366 zval
*qarray
= GET_PROP(queryArray
);
367 if (http_querystring_modify(qarray
, params
)) {
368 http_querystring_update(qarray
, GET_PROP(queryString
));
372 if (return_value_used
) {
373 RETURN_PROP(queryString
);
378 /* {{{ proto HttpQueryString HttpQueryString::mod(mixed params)
380 * Copies the query string object and sets provided params at the clone.
381 * This is basically shorthand for:
384 * $newQS = new HttpQueryString(false, $oldQS);
385 * $newQS->set($other_params);
389 PHP_METHOD(HttpQueryString
, mod
)
391 zval
*orig
, *zobj
, *qarr
, *qstr
, *params
;
393 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", ¶ms
)) {
394 zobj
= http_querystring_instantiate(0);
395 orig
= GET_PROP(queryArray
);
396 qarr
= GET_PROP_EX(zobj
, queryArray
);
397 qstr
= GET_PROP_EX(zobj
, queryString
);
399 array_copy(orig
, qarr
);
400 http_querystring_modify(qarr
, params
);
401 http_querystring_update(qarr
, qstr
);
403 RETURN_ZVAL(zobj
, 1, 1);
409 /* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true])
411 * Get a single instance (differentiates between the global setting).
413 PHP_METHOD(HttpQueryString
, singleton
)
415 zend_bool global
= 1;
416 zval
*instance
= GET_STATIC_PROP(instance
);
419 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &global
)) {
420 zval
**zobj_ptr
= NULL
, *zobj
= NULL
;
422 if (Z_TYPE_P(instance
) == IS_ARRAY
) {
423 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(instance
), global
, (void *) &zobj_ptr
)) {
424 RETVAL_ZVAL(*zobj_ptr
, 1, 0);
426 zobj
= http_querystring_instantiate(global
);
427 add_index_zval(instance
, global
, zobj
);
428 RETVAL_OBJECT(zobj
, 1);
431 MAKE_STD_ZVAL(instance
);
432 array_init(instance
);
434 zobj
= http_querystring_instantiate(global
);
435 add_index_zval(instance
, global
, zobj
);
436 RETVAL_OBJECT(zobj
, 1);
438 SET_STATIC_PROP(instance
, instance
);
439 zval_ptr_dtor(&instance
);
447 /* {{{ Getters by type */
448 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
449 PHP_METHOD(HttpQueryString, method) \
453 zval *defval = NULL; \
455 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
456 http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
459 HTTP_QUERYSTRING_GETTER(getBool
, IS_BOOL
);
460 HTTP_QUERYSTRING_GETTER(getInt
, IS_LONG
);
461 HTTP_QUERYSTRING_GETTER(getFloat
, IS_DOUBLE
);
462 HTTP_QUERYSTRING_GETTER(getString
, IS_STRING
);
463 HTTP_QUERYSTRING_GETTER(getArray
, IS_ARRAY
);
464 HTTP_QUERYSTRING_GETTER(getObject
, IS_OBJECT
);
467 #ifdef HTTP_HAVE_ICONV
468 /* {{{ proto bool HttpQueryString::xlate(string ie, string oe)
470 * Converts the query string from the source encoding ie to the target encoding oe.
471 * WARNING: Don't use any character set that can contain NUL bytes like UTF-16.
473 * Returns TRUE on success or FALSE on failure.
475 PHP_METHOD(HttpQueryString
, xlate
)
482 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &ie
, &ie_len
, &oe
, &oe_len
)) {
486 qa
= GET_PROP(queryArray
);
487 qs
= GET_PROP(queryString
);
491 if (SUCCESS
== (rs
= http_querystring_xlate(&xa
, qa
, ie
, oe
))) {
492 zend_hash_clean(Z_ARRVAL_P(qa
));
493 zend_hash_copy(Z_ARRVAL_P(qa
), Z_ARRVAL(xa
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
494 http_querystring_update(qa
, qs
);
501 #endif /* HAVE_ICONV */
503 /* {{{ proto string HttpQueryString::serialize()
505 * Implements Serializable.
507 PHP_METHOD(HttpQueryString
, serialize
)
510 RETURN_PROP(queryString
);
514 /* {{{ proto void HttpQueryString::unserialize(string serialized)
516 * Implements Serializable.
518 PHP_METHOD(HttpQueryString
, unserialize
)
523 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &serialized
)) {
524 if (Z_TYPE_P(serialized
) == IS_STRING
) {
525 zval
*qa
= GET_PROP(queryArray
);
527 zend_hash_clean(Z_ARRVAL_P(qa
));
528 http_querystring_modify(qa
, serialized
);
529 http_querystring_update(qa
, GET_PROP(queryString
));
531 http_error(HE_WARNING
, HTTP_E_QUERYSTRING
, "Expected a string as parameter");
538 #endif /* ZEND_ENGINE_2 */
545 * vim600: noet sw=4 ts=4 fdm=marker
546 * vim<600: noet sw=4 ts=4