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 HTTP_BEGIN_ARGS(offsetGet
, 1)
81 HTTP_ARG_VAL(offset
, 0)
84 HTTP_BEGIN_ARGS(offsetSet
, 2)
85 HTTP_ARG_VAL(offset
, 0)
86 HTTP_ARG_VAL(value
, 0)
89 HTTP_BEGIN_ARGS(offsetExists
, 1)
90 HTTP_ARG_VAL(offset
, 0)
93 HTTP_BEGIN_ARGS(offsetUnset
, 1)
94 HTTP_ARG_VAL(offset
, 0)
98 #define OBJ_PROP_CE http_querystring_object_ce
99 zend_class_entry
*http_querystring_object_ce
;
100 zend_function_entry http_querystring_object_fe
[] = {
101 HTTP_QUERYSTRING_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
|ZEND_ACC_FINAL
)
103 HTTP_QUERYSTRING_ME(toArray
, ZEND_ACC_PUBLIC
)
104 HTTP_QUERYSTRING_ME(toString
, ZEND_ACC_PUBLIC
)
105 ZEND_MALIAS(HttpQueryString
, __toString
, toString
, HTTP_ARGS(HttpQueryString
, toString
), ZEND_ACC_PUBLIC
)
107 HTTP_QUERYSTRING_ME(get
, ZEND_ACC_PUBLIC
)
108 HTTP_QUERYSTRING_ME(set
, ZEND_ACC_PUBLIC
)
109 HTTP_QUERYSTRING_ME(mod
, ZEND_ACC_PUBLIC
)
111 HTTP_QUERYSTRING_GME(getBool
, ZEND_ACC_PUBLIC
)
112 HTTP_QUERYSTRING_GME(getInt
, ZEND_ACC_PUBLIC
)
113 HTTP_QUERYSTRING_GME(getFloat
, ZEND_ACC_PUBLIC
)
114 HTTP_QUERYSTRING_GME(getString
, ZEND_ACC_PUBLIC
)
115 HTTP_QUERYSTRING_GME(getArray
, ZEND_ACC_PUBLIC
)
116 HTTP_QUERYSTRING_GME(getObject
, ZEND_ACC_PUBLIC
)
119 HTTP_QUERYSTRING_ME(singleton
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
121 #ifdef HTTP_HAVE_ICONV
122 HTTP_QUERYSTRING_ME(xlate
, ZEND_ACC_PUBLIC
)
125 /* Implements Serializable */
126 HTTP_QUERYSTRING_ME(serialize
, ZEND_ACC_PUBLIC
)
127 HTTP_QUERYSTRING_ME(unserialize
, ZEND_ACC_PUBLIC
)
129 /* Implements ArrayAccess */
130 HTTP_QUERYSTRING_ME(offsetGet
, ZEND_ACC_PUBLIC
)
131 HTTP_QUERYSTRING_ME(offsetSet
, ZEND_ACC_PUBLIC
)
132 HTTP_QUERYSTRING_ME(offsetExists
, ZEND_ACC_PUBLIC
)
133 HTTP_QUERYSTRING_ME(offsetUnset
, ZEND_ACC_PUBLIC
)
137 static zend_object_handlers http_querystring_object_handlers
;
139 PHP_MINIT_FUNCTION(http_querystring_object
)
141 HTTP_REGISTER_CLASS_EX(HttpQueryString
, http_querystring_object
, NULL
, 0);
144 zend_class_implements(http_querystring_object_ce TSRMLS_CC
, 2, zend_ce_serializable
, zend_ce_arrayaccess
);
147 DCL_STATIC_PROP_N(PRIVATE
, instance
);
148 DCL_PROP_N(PRIVATE
, queryArray
);
149 DCL_PROP(PRIVATE
, string
, queryString
, "");
152 DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL
);
153 DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT
);
154 DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT
);
155 DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING
);
156 DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY
);
157 DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT
);
160 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL
);
161 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_INT", HTTP_QUERYSTRING_TYPE_INT
);
162 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT
);
163 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING
);
164 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY
);
165 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT
);
170 zend_object_value
_http_querystring_object_new(zend_class_entry
*ce TSRMLS_DC
)
172 return http_querystring_object_new_ex(ce
, NULL
);
175 zend_object_value
_http_querystring_object_new_ex(zend_class_entry
*ce
, http_querystring_object
**ptr TSRMLS_DC
)
177 zend_object_value ov
;
178 http_querystring_object
*o
;
180 o
= ecalloc(1, sizeof(http_querystring_object
));
187 ALLOC_HASHTABLE(OBJ_PROP(o
));
188 zend_hash_init(OBJ_PROP(o
), zend_hash_num_elements(&ce
->default_properties
), NULL
, ZVAL_PTR_DTOR
, 0);
189 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
191 ov
.handle
= putObject(http_querystring_object
, o
);
192 ov
.handlers
= &http_querystring_object_handlers
;
197 void _http_querystring_object_free(zend_object
*object TSRMLS_DC
)
199 http_querystring_object
*o
= (http_querystring_object
*) object
;
204 /* {{{ querystring helpers */
205 #define http_querystring_instantiate(t, g, p, u) _http_querystring_instantiate((t), (g), (p), (u) TSRMLS_CC)
206 static inline zval
*_http_querystring_instantiate(zval
*this_ptr
, zend_bool global
, zval
*params
, zend_bool defer_update TSRMLS_DC
)
208 zval
*qarray
, *qstring
, **_SERVER
= NULL
, **_GET
= NULL
, **QUERY_STRING
= NULL
;;
211 MAKE_STD_ZVAL(this_ptr
);
212 Z_TYPE_P(this_ptr
) = IS_OBJECT
;
213 this_ptr
->value
.obj
= http_querystring_object_new(http_querystring_object_ce
);
217 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC
);
219 if ( (SUCCESS
== zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void *) &_SERVER
)) &&
220 (Z_TYPE_PP(_SERVER
) == IS_ARRAY
) &&
221 (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(_SERVER
), "QUERY_STRING", sizeof("QUERY_STRING"), (void *) &QUERY_STRING
))) {
223 qstring
= *QUERY_STRING
;
225 zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC
);
227 if ((SUCCESS
== zend_hash_find(&EG(symbol_table
), "_GET", sizeof("_GET"), (void *) &_GET
)) && (Z_TYPE_PP(_GET
) == IS_ARRAY
)) {
230 http_error(HE_WARNING
, HTTP_E_QUERYSTRING
, "Could not acquire reference to superglobal GET array");
233 http_error(HE_WARNING
, HTTP_E_QUERYSTRING
, "Could not acquire reference to QUERY_STRING");
236 if (qarray
&& qstring
) {
237 if (Z_TYPE_P(qstring
) != IS_STRING
) {
238 convert_to_string(qstring
);
241 SET_PROP(queryArray
, qarray
);
242 SET_PROP(queryString
, qstring
);
243 GET_PROP(queryArray
)->is_ref
= 1;
244 GET_PROP(queryString
)->is_ref
= 1;
247 http_querystring_modify(GET_PROP(queryArray
), params
);
250 http_querystring_update(GET_PROP(queryArray
), GET_PROP(queryString
));
254 qarray
= ecalloc(1, sizeof(zval
));
257 SET_PROP(queryArray
, qarray
);
258 UPD_STRL(queryString
, "", 0);
260 if (params
&& http_querystring_modify(qarray
, params
) && !defer_update
) {
261 http_querystring_update(qarray
, GET_PROP(queryString
));
268 #define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC)
269 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
)
271 zval
**arrval
, *qarray
= GET_PROP(queryArray
);
273 if ((Z_TYPE_P(qarray
) == IS_ARRAY
) && (SUCCESS
== zend_hash_find(Z_ARRVAL_P(qarray
), name
, name_len
+ 1, (void *) &arrval
))) {
274 RETVAL_ZVAL(*arrval
, 1, 0);
277 convert_to_type(type
, return_value
);
280 if (del
&& (SUCCESS
== zend_hash_del(Z_ARRVAL_P(qarray
), name
, name_len
+ 1))) {
281 http_querystring_update(qarray
, GET_PROP(queryString
));
284 RETURN_ZVAL(defval
, 1, 0);
289 /* {{{ proto final void HttpQueryString::__construct([bool global = true[, mixed add])
291 * Creates a new HttpQueryString object instance.
292 * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE.
294 PHP_METHOD(HttpQueryString
, __construct
)
296 zend_bool global
= 1;
300 if (!sapi_module
.treat_data
) {
301 http_error(HE_ERROR
, HTTP_E_QUERYSTRING
, "The SAPI does not have a treat_data function registered");
302 } else if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bz", &global
, ¶ms
)) {
303 http_querystring_instantiate(getThis(), global
, params
, 0);
309 /* {{{ proto string HttpQueryString::toString()
311 * Returns the string representation.
313 PHP_METHOD(HttpQueryString
, toString
)
316 RETURN_PROP(queryString
);
320 /* {{{ proto array HttpQueryString::toArray()
322 * Returns the array representation.
324 PHP_METHOD(HttpQueryString
, toArray
)
327 RETURN_PROP(queryArray
);
331 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
333 * Get (part of) the query string.
335 * The type parameter is either one of the HttpQueryString::TYPE_* constants or a type abbreviation like
336 * "b" for bool, "i" for int, "f" for float, "s" for string, "a" for array and "o" for a stdClass object.
338 PHP_METHOD(HttpQueryString
, get
)
344 zval
*ztype
= NULL
, *defval
= NULL
;
346 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|szzb", &name
, &name_len
, &ztype
, &defval
, &del
)) {
347 if (name
&& name_len
) {
349 if (Z_TYPE_P(ztype
) == IS_LONG
) {
350 type
= Z_LVAL_P(ztype
);
351 } else if(Z_TYPE_P(ztype
) == IS_STRING
) {
352 switch (Z_STRVAL_P(ztype
)[0]) {
354 case 'b': type
= HTTP_QUERYSTRING_TYPE_BOOL
; break;
356 case 'i': type
= HTTP_QUERYSTRING_TYPE_INT
; break;
358 case 'f': type
= HTTP_QUERYSTRING_TYPE_FLOAT
; break;
360 case 's': type
= HTTP_QUERYSTRING_TYPE_STRING
; break;
362 case 'a': type
= HTTP_QUERYSTRING_TYPE_ARRAY
; break;
364 case 'o': type
= HTTP_QUERYSTRING_TYPE_OBJECT
; break;
368 http_querystring_get(getThis(), type
, name
, name_len
, defval
, del
, return_value
);
370 RETURN_PROP(queryString
);
376 /* {{{ proto string HttpQueryString::set(mixed params)
378 * Set query string entry/entries. NULL values will unset the variable.
380 PHP_METHOD(HttpQueryString
, set
)
384 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", ¶ms
)) {
385 zval
*qarray
= GET_PROP(queryArray
);
386 if (http_querystring_modify(qarray
, params
)) {
387 http_querystring_update(qarray
, GET_PROP(queryString
));
391 if (return_value_used
) {
392 RETURN_PROP(queryString
);
397 /* {{{ proto HttpQueryString HttpQueryString::mod(mixed params)
399 * Copies the query string object and sets provided params at the clone.
400 * This is basically shorthand for:
403 * $newQS = new HttpQueryString(false, $oldQS);
404 * $newQS->set($other_params);
408 PHP_METHOD(HttpQueryString
, mod
)
410 zval
*zobj
, *qarr
, *qstr
, *params
;
412 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", ¶ms
)) {
413 zobj
= http_querystring_instantiate(NULL
, 0, GET_PROP(queryArray
), 1);
414 qarr
= GET_PROP_EX(zobj
, queryArray
);
415 qstr
= GET_PROP_EX(zobj
, queryString
);
417 http_querystring_modify(qarr
, params
);
418 http_querystring_update(qarr
, qstr
);
420 RETURN_ZVAL(zobj
, 1, 1);
426 /* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true])
428 * Get a single instance (differentiates between the global setting).
430 PHP_METHOD(HttpQueryString
, singleton
)
432 zend_bool global
= 1;
433 zval
*instance
= GET_STATIC_PROP(instance
);
436 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &global
)) {
437 zval
**zobj_ptr
= NULL
, *zobj
= NULL
;
439 if (Z_TYPE_P(instance
) == IS_ARRAY
) {
440 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(instance
), global
, (void *) &zobj_ptr
)) {
441 RETVAL_ZVAL(*zobj_ptr
, 1, 0);
443 zobj
= http_querystring_instantiate(NULL
, global
, NULL
, (zend_bool
) !global
);
444 add_index_zval(instance
, global
, zobj
);
445 RETVAL_OBJECT(zobj
, 1);
448 MAKE_STD_ZVAL(instance
);
449 array_init(instance
);
451 zobj
= http_querystring_instantiate(NULL
, global
, NULL
, (zend_bool
) !global
);
452 add_index_zval(instance
, global
, zobj
);
453 RETVAL_OBJECT(zobj
, 1);
455 SET_STATIC_PROP(instance
, instance
);
456 zval_ptr_dtor(&instance
);
464 /* {{{ Getters by type */
465 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
466 PHP_METHOD(HttpQueryString, method) \
470 zval *defval = NULL; \
472 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
473 http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
476 HTTP_QUERYSTRING_GETTER(getBool
, IS_BOOL
);
477 HTTP_QUERYSTRING_GETTER(getInt
, IS_LONG
);
478 HTTP_QUERYSTRING_GETTER(getFloat
, IS_DOUBLE
);
479 HTTP_QUERYSTRING_GETTER(getString
, IS_STRING
);
480 HTTP_QUERYSTRING_GETTER(getArray
, IS_ARRAY
);
481 HTTP_QUERYSTRING_GETTER(getObject
, IS_OBJECT
);
484 #ifdef HTTP_HAVE_ICONV
485 /* {{{ proto bool HttpQueryString::xlate(string ie, string oe)
487 * Converts the query string from the source encoding ie to the target encoding oe.
488 * WARNING: Don't use any character set that can contain NUL bytes like UTF-16.
490 * Returns TRUE on success or FALSE on failure.
492 PHP_METHOD(HttpQueryString
, xlate
)
499 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "ss", &ie
, &ie_len
, &oe
, &oe_len
)) {
503 qa
= GET_PROP(queryArray
);
504 qs
= GET_PROP(queryString
);
508 if (SUCCESS
== (rs
= http_querystring_xlate(&xa
, qa
, ie
, oe
))) {
509 zend_hash_clean(Z_ARRVAL_P(qa
));
510 zend_hash_copy(Z_ARRVAL_P(qa
), Z_ARRVAL(xa
), (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
511 http_querystring_update(qa
, qs
);
518 #endif /* HAVE_ICONV */
520 /* {{{ proto string HttpQueryString::serialize()
522 * Implements Serializable.
524 PHP_METHOD(HttpQueryString
, serialize
)
527 RETURN_PROP(queryString
);
531 /* {{{ proto void HttpQueryString::unserialize(string serialized)
533 * Implements Serializable.
535 PHP_METHOD(HttpQueryString
, unserialize
)
540 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", &serialized
)) {
541 if (Z_TYPE_P(serialized
) == IS_STRING
) {
542 http_querystring_instantiate(getThis(), 0, serialized
, 0);
544 http_error(HE_WARNING
, HTTP_E_QUERYSTRING
, "Expected a string as parameter");
551 /* {{{ proto mixed HttpQueryString::offsetGet(string offset)
553 * Implements ArrayAccess.
555 PHP_METHOD(HttpQueryString
, offsetGet
)
561 if ( (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &offset_str
, &offset_len
)) &&
562 (SUCCESS
== zend_hash_find(Z_ARRVAL_P(GET_PROP(queryArray
)), offset_str
, offset_len
+ 1, (void *) &value
))) {
563 RETVAL_ZVAL(*value
, 1, 0);
568 /* {{{ proto void HttpQueryString::offsetSet(string offset, mixed value)
570 * Implements ArrayAccess.
572 PHP_METHOD(HttpQueryString
, offsetSet
)
578 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &offset_str
, &offset_len
, &value
)) {
579 zval
*qarr
= GET_PROP(queryArray
), *qstr
= GET_PROP(queryString
);
582 add_assoc_zval_ex(qarr
, offset_str
, offset_len
+ 1, value
);
583 http_querystring_update(qarr
, qstr
);
588 /* {{{ proto bool HttpQueryString::offsetExists(string offset)
590 * Implements ArrayAccess.
592 PHP_METHOD(HttpQueryString
, offsetExists
)
598 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &offset_str
, &offset_len
)) {
599 RETURN_BOOL((SUCCESS
== zend_hash_find(Z_ARRVAL_P(GET_PROP(queryArray
)), offset_str
, offset_len
+ 1, (void *) &value
)) && (Z_TYPE_PP(value
) != IS_NULL
));
604 /* {{{ proto void HttpQueryString::offsetUnset(string offset)
606 * Implements ArrayAccess.
608 PHP_METHOD(HttpQueryString
, offsetUnset
)
613 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &offset_str
, &offset_len
)) {
614 zval
*qarr
= GET_PROP(queryArray
);
616 if (SUCCESS
== zend_hash_del(Z_ARRVAL_P(qarr
), offset_str
, offset_len
+ 1)) {
617 http_querystring_update(qarr
, GET_PROP(queryString
));
623 #endif /* ZEND_ENGINE_2 */
630 * vim600: noet sw=4 ts=4 fdm=marker
631 * vim<600: noet sw=4 ts=4