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_url_api.h"
25 #include "php_http_querystring_object.h"
26 #include "php_http_exception_object.h"
28 #define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpQueryString, method, ret_ref, req_args)
29 #define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpQueryString, method, ret_ref)
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, 0)
34 HTTP_ARG_VAL(global
, 0)
35 HTTP_ARG_VAL(params
, 0)
38 HTTP_BEGIN_ARGS(getInstance
, 0, 0)
39 HTTP_ARG_VAL(global
, 0)
42 HTTP_EMPTY_ARGS(toArray
, 0);
43 HTTP_EMPTY_ARGS(toString
, 0);
45 HTTP_BEGIN_ARGS(get
, 0, 0)
48 HTTP_ARG_VAL(defval
, 0)
49 HTTP_ARG_VAL(delete, 0)
52 HTTP_BEGIN_ARGS(set
, 0, 2)
54 HTTP_ARG_VAL(value
, 0)
57 HTTP_BEGIN_ARGS(__getter
, 0, 1)
59 HTTP_ARG_VAL(defval
, 0)
60 HTTP_ARG_VAL(delete, 0)
63 #define http_querystring_object_declare_default_properties() _http_querystring_object_declare_default_properties(TSRMLS_C)
64 static inline void _http_querystring_object_declare_default_properties(TSRMLS_D
);
66 #define GET_STATIC_PROP(n) *GET_STATIC_PROP_EX(http_querystring_object_ce, n)
67 #define SET_STATIC_PROP(n, v) SET_STATIC_PROP_EX(http_querystring_object_ce, n, v)
68 #define OBJ_PROP_CE http_querystring_object_ce
69 zend_class_entry
*http_querystring_object_ce
;
70 zend_function_entry http_querystring_object_fe
[] = {
71 HTTP_QUERYSTRING_ME(__construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
|ZEND_ACC_FINAL
)
73 HTTP_QUERYSTRING_ME(toArray
, ZEND_ACC_PUBLIC
)
74 HTTP_QUERYSTRING_ME(toString
, ZEND_ACC_PUBLIC
)
75 ZEND_MALIAS(HttpQueryString
, __toString
, toString
, HTTP_ARGS(HttpQueryString
, toString
), ZEND_ACC_PUBLIC
)
77 HTTP_QUERYSTRING_ME(get
, ZEND_ACC_PUBLIC
)
78 HTTP_QUERYSTRING_ME(set
, ZEND_ACC_PUBLIC
)
80 HTTP_QUERYSTRING_ME(getInstance
, ZEND_ACC_PUBLIC
|ZEND_ACC_STATIC
)
82 HTTP_QUERYSTRING_GME(getBool
, ZEND_ACC_PUBLIC
)
83 HTTP_QUERYSTRING_GME(getInt
, ZEND_ACC_PUBLIC
)
84 HTTP_QUERYSTRING_GME(getFloat
, ZEND_ACC_PUBLIC
)
85 HTTP_QUERYSTRING_GME(getString
, ZEND_ACC_PUBLIC
)
86 HTTP_QUERYSTRING_GME(getArray
, ZEND_ACC_PUBLIC
)
87 HTTP_QUERYSTRING_GME(getObject
, ZEND_ACC_PUBLIC
)
91 static zend_object_handlers http_querystring_object_handlers
;
93 PHP_MINIT_FUNCTION(http_querystring_object
)
95 HTTP_REGISTER_CLASS_EX(HttpQueryString
, http_querystring_object
, NULL
, 0);
97 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL
);
98 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_INT", HTTP_QUERYSTRING_TYPE_INT
);
99 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT
);
100 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING
);
101 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY
);
102 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT
);
107 zend_object_value
_http_querystring_object_new(zend_class_entry
*ce TSRMLS_DC
)
109 return http_querystring_object_new_ex(ce
, NULL
);
112 zend_object_value
_http_querystring_object_new_ex(zend_class_entry
*ce
, http_querystring_object
**ptr TSRMLS_DC
)
114 zend_object_value ov
;
115 http_querystring_object
*o
;
117 o
= ecalloc(1, sizeof(http_querystring_object
));
124 ALLOC_HASHTABLE(OBJ_PROP(o
));
125 zend_hash_init(OBJ_PROP(o
), 0, NULL
, ZVAL_PTR_DTOR
, 0);
126 zend_hash_copy(OBJ_PROP(o
), &ce
->default_properties
, (copy_ctor_func_t
) zval_add_ref
, NULL
, sizeof(zval
*));
128 ov
.handle
= putObject(http_querystring_object
, o
);
129 ov
.handlers
= &http_querystring_object_handlers
;
134 static inline void _http_querystring_object_declare_default_properties(TSRMLS_D
)
136 zend_class_entry
*ce
= http_querystring_object_ce
;
138 DCL_STATIC_PROP_N(PRIVATE
, instance
);
140 DCL_PROP_N(PRIVATE
, queryArray
);
141 DCL_PROP(PRIVATE
, string
, queryString
, "");
144 DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL
);
145 DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT
);
146 DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT
);
147 DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING
);
148 DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY
);
149 DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT
);
153 void _http_querystring_object_free(zend_object
*object TSRMLS_DC
)
155 http_querystring_object
*o
= (http_querystring_object
*) object
;
158 zend_hash_destroy(OBJ_PROP(o
));
159 FREE_HASHTABLE(OBJ_PROP(o
));
164 #define http_querystring_update(qa, qs) _http_querystring_update((qa), (qs) TSRMLS_CC)
165 static inline void _http_querystring_update(zval
*qarray
, zval
*qstring TSRMLS_DC
)
170 if (Z_TYPE_P(qarray
) != IS_ARRAY
) {
171 convert_to_array(qarray
);
173 if (SUCCESS
== http_urlencode_hash_ex(Z_ARRVAL_P(qarray
), 0, NULL
, 0, &s
, &l
)) {
175 ZVAL_STRINGL(qstring
, s
, l
, 0);
177 http_error(HE_WARNING
, HTTP_E_QUERYSTRING
, "Failed to update query string");
181 #define http_querystring_modify_ex(a, k, l, v) _http_querystring_modify_ex((a), (k), (l), (v) TSRMLS_CC)
182 static inline int _http_querystring_modify_ex(zval
*qarray
, char *key
, uint keylen
, zval
*data TSRMLS_DC
)
184 if (Z_TYPE_P(data
) == IS_NULL
) {
185 if (SUCCESS
!= zend_hash_del(Z_ARRVAL_P(qarray
), key
, keylen
)) {
190 add_assoc_zval(qarray
, key
, data
);
195 #define http_querystring_modify_array(q, a) _http_querystring_modify_array((q), (a) TSRMLS_CC)
196 static inline int _http_querystring_modify_array(zval
*qarray
, zval
*array TSRMLS_DC
)
205 FOREACH_KEYLENVAL(pos
, array
, key
, keylen
, idx
, value
) {
207 if (http_querystring_modify_ex(qarray
, key
, keylen
, *value
)) {
211 keylen
= spprintf(&key
, 0, "%lu", idx
);
212 if (http_querystring_modify_ex(qarray
, key
, keylen
, *value
)) {
223 #define http_querystring_modify(q, p) _http_querystring_modify((q), (p) TSRMLS_CC)
224 static inline int _http_querystring_modify(zval
*qarray
, zval
*params TSRMLS_DC
)
226 if (Z_TYPE_P(params
) == IS_ARRAY
) {
227 return http_querystring_modify_array(qarray
, params
);
236 convert_to_string_ex(¶ms
);
237 sapi_module
.treat_data(PARSE_STRING
, estrdup(Z_STRVAL_P(params
)), &array TSRMLS_CC
);
238 zval_ptr_dtor(¶ms
);
239 rv
= http_querystring_modify_array(qarray
, &array
);
245 #define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC)
246 static inline zval
*_http_querystring_instantiate(zend_bool global TSRMLS_DC
)
248 zval
*zobj
, *zglobal
;
250 MAKE_STD_ZVAL(zglobal
);
251 ZVAL_BOOL(zglobal
, global
);
254 Z_TYPE_P(zobj
) = IS_OBJECT
;
255 Z_OBJVAL_P(zobj
) = http_querystring_object_new(http_querystring_object_ce
);
256 zend_call_method_with_1_params(&zobj
, Z_OBJCE_P(zobj
), NULL
, "__construct", NULL
, zglobal
);
258 zval_ptr_dtor(&zglobal
);
263 #define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC)
264 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
)
266 zval
**arrval
, *qarray
= GET_PROP(queryArray
);
268 if ((Z_TYPE_P(qarray
) == IS_ARRAY
) && (SUCCESS
== zend_hash_find(Z_ARRVAL_P(qarray
), name
, name_len
+ 1, (void **) &arrval
))) {
269 RETVAL_ZVAL(*arrval
, 1, 0);
272 convert_to_type(type
, return_value
);
275 if (del
&& (SUCCESS
== zend_hash_del(Z_ARRVAL_P(qarray
), name
, name_len
+ 1))) {
276 http_querystring_update(qarray
, GET_PROP(queryString
));
279 RETURN_ZVAL(defval
, 1, 0);
283 /* {{{ proto void HttpQueryString::__construct([bool global = true[, mixed add])
285 * Creates a new HttpQueryString object instance.
286 * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE.
288 PHP_METHOD(HttpQueryString
, __construct
)
290 zend_bool global
= 1;
291 zval
*params
= NULL
, *qarray
= NULL
, *qstring
= NULL
, **_GET
, **_SERVER
, **QUERY_STRING
;
294 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|bz", &global
, ¶ms
)) {
296 if ( (SUCCESS
== zend_hash_find(&EG(symbol_table
), "_SERVER", sizeof("_SERVER"), (void **) &_SERVER
)) &&
297 (Z_TYPE_PP(_SERVER
) == IS_ARRAY
) &&
298 (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(_SERVER
), "QUERY_STRING", sizeof("QUERY_STRING"), (void **) &QUERY_STRING
))) {
300 qstring
= *QUERY_STRING
;
302 if ((SUCCESS
== zend_hash_find(&EG(symbol_table
), "_GET", sizeof("_GET"), (void **) &_GET
)) && (Z_TYPE_PP(_GET
) == IS_ARRAY
)) {
305 http_error(HE_WARNING
, HTTP_E_QUERYSTRING
, "Could not acquire reference to superglobal GET array");
308 http_error(HE_WARNING
, HTTP_E_QUERYSTRING
, "Could not acquire reference to QUERY_STRING");
311 if (qarray
&& qstring
) {
312 if (Z_TYPE_P(qstring
) != IS_STRING
) {
313 convert_to_string(qstring
);
316 SET_PROP(queryArray
, qarray
);
317 SET_PROP(queryString
, qstring
);
318 GET_PROP(queryArray
)->is_ref
= 1;
319 GET_PROP(queryString
)->is_ref
= 1;
321 if (params
&& http_querystring_modify(GET_PROP(queryArray
), params
)) {
322 http_querystring_update(GET_PROP(queryArray
), GET_PROP(queryString
));
326 qarray
= ecalloc(1, sizeof(zval
));
329 SET_PROP(queryArray
, qarray
);
330 UPD_STRL(queryString
, "", 0);
332 if (params
&& http_querystring_modify(qarray
, params
)) {
333 http_querystring_update(qarray
, GET_PROP(queryString
));
341 /* {{{ proto string HttpQueryString::toString()
343 * Returns the string representation.
345 PHP_METHOD(HttpQueryString
, toString
)
348 RETURN_PROP(queryString
);
352 /* {{{ proto array HttpQueryString::toArray()
354 * Returns the array representation.
356 PHP_METHOD(HttpQueryString
, toArray
)
359 RETURN_PROP(queryArray
);
363 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
365 * Get (part of) the query string.
367 * The type parameter is either one of the HttpQueryString::TYPE_* constants or a type abbreviation like
368 * "b" for bool, "i" for int, "f" for float, "s" for string, "a" for array and "o" for a stdClass object.
370 PHP_METHOD(HttpQueryString
, get
)
376 zval
*ztype
= NULL
, *defval
= NULL
;
378 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|szzb", &name
, &name_len
, &ztype
, &defval
, &del
)) {
379 if (name
&& name_len
) {
381 if (Z_TYPE_P(ztype
) == IS_LONG
) {
382 type
= Z_LVAL_P(ztype
);
383 } else if(Z_TYPE_P(ztype
) == IS_STRING
) {
384 switch (tolower(Z_STRVAL_P(ztype
)[0]))
386 case 'b': type
= HTTP_QUERYSTRING_TYPE_BOOL
; break;
387 case 'i': type
= HTTP_QUERYSTRING_TYPE_INT
; break;
388 case 'f': type
= HTTP_QUERYSTRING_TYPE_FLOAT
; break;
389 case 's': type
= HTTP_QUERYSTRING_TYPE_STRING
; break;
390 case 'a': type
= HTTP_QUERYSTRING_TYPE_ARRAY
; break;
391 case 'o': type
= HTTP_QUERYSTRING_TYPE_OBJECT
; break;
395 http_querystring_get(getThis(), type
, name
, name_len
, defval
, del
, return_value
);
397 RETURN_PROP(queryString
);
403 /* {{{ proto string HttpQueryString::set(mixed params)
405 * Set query string entry/entries. NULL values will unset the variable.
407 PHP_METHOD(HttpQueryString
, set
)
411 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "z", ¶ms
)) {
412 zval
*qarray
= GET_PROP(queryArray
);
413 if (http_querystring_modify(qarray
, params
)) {
414 http_querystring_update(qarray
, GET_PROP(queryString
));
419 RETURN_PROP(queryString
);
424 /* {{{ proto HttpQueryString HttpQueryString::getInstance([bool global = true])
426 * Get a single instance (differentiates between the global setting).
428 PHP_METHOD(HttpQueryString
, getInstance
)
430 zend_bool global
= 1;
431 zval
*instance
= GET_STATIC_PROP(instance
);
434 if (SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|b", &global
)) {
435 zval
**zobj_ptr
= NULL
, *zobj
= NULL
;
437 if (Z_TYPE_P(instance
) == IS_ARRAY
) {
438 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_P(instance
), global
, (void **) &zobj_ptr
)) {
439 RETVAL_ZVAL(*zobj_ptr
, 1, 0);
441 zobj
= http_querystring_instantiate(global
);
442 add_index_zval(instance
, global
, zobj
);
443 RETVAL_OBJECT(zobj
, 1);
446 MAKE_STD_ZVAL(instance
);
447 array_init(instance
);
449 zobj
= http_querystring_instantiate(global
);
450 add_index_zval(instance
, global
, zobj
);
451 RETVAL_OBJECT(zobj
, 1);
453 SET_STATIC_PROP(instance
, instance
);
454 zval_ptr_dtor(&instance
);
461 /* {{{ Getters by type */
462 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
463 PHP_METHOD(HttpQueryString, method) \
467 zval *defval = NULL; \
469 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
470 http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
473 HTTP_QUERYSTRING_GETTER(getBool
, IS_BOOL
);
474 HTTP_QUERYSTRING_GETTER(getInt
, IS_LONG
);
475 HTTP_QUERYSTRING_GETTER(getFloat
, IS_DOUBLE
);
476 HTTP_QUERYSTRING_GETTER(getString
, IS_STRING
);
477 HTTP_QUERYSTRING_GETTER(getArray
, IS_ARRAY
);
478 HTTP_QUERYSTRING_GETTER(getObject
, IS_OBJECT
);
481 #endif /* ZEND_ENGINE_2 */
488 * vim600: noet sw=4 ts=4 fdm=marker
489 * vim<600: noet sw=4 ts=4