83f1dc6f09da9e550940069b7e65c62a74c1831c
[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-2006, 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_url_api.h"
25 #include "php_http_querystring_object.h"
26 #include "php_http_exception_object.h"
27
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)
32
33 HTTP_BEGIN_ARGS(__construct, 0, 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(getInstance, 0, 0)
40 HTTP_ARG_VAL(global, 0)
41 HTTP_END_ARGS;
42 #endif
43
44 HTTP_EMPTY_ARGS(toArray, 0);
45 HTTP_EMPTY_ARGS(toString, 0);
46
47 HTTP_BEGIN_ARGS(get, 0, 0)
48 HTTP_ARG_VAL(name, 0)
49 HTTP_ARG_VAL(type, 0)
50 HTTP_ARG_VAL(defval, 0)
51 HTTP_ARG_VAL(delete, 0)
52 HTTP_END_ARGS;
53
54 HTTP_BEGIN_ARGS(set, 0, 1)
55 HTTP_ARG_VAL(params, 0)
56 HTTP_END_ARGS;
57
58 HTTP_BEGIN_ARGS(__getter, 0, 1)
59 HTTP_ARG_VAL(name, 0)
60 HTTP_ARG_VAL(defval, 0)
61 HTTP_ARG_VAL(delete, 0)
62 HTTP_END_ARGS;
63
64 #define http_querystring_object_declare_default_properties() _http_querystring_object_declare_default_properties(TSRMLS_C)
65 static inline void _http_querystring_object_declare_default_properties(TSRMLS_D);
66
67 #define GET_STATIC_PROP(n) *GET_STATIC_PROP_EX(http_querystring_object_ce, n)
68 #define SET_STATIC_PROP(n, v) SET_STATIC_PROP_EX(http_querystring_object_ce, n, v)
69 #define OBJ_PROP_CE http_querystring_object_ce
70 zend_class_entry *http_querystring_object_ce;
71 zend_function_entry http_querystring_object_fe[] = {
72 HTTP_QUERYSTRING_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL)
73
74 HTTP_QUERYSTRING_ME(toArray, ZEND_ACC_PUBLIC)
75 HTTP_QUERYSTRING_ME(toString, ZEND_ACC_PUBLIC)
76 ZEND_MALIAS(HttpQueryString, __toString, toString, HTTP_ARGS(HttpQueryString, toString), ZEND_ACC_PUBLIC)
77
78 HTTP_QUERYSTRING_ME(get, ZEND_ACC_PUBLIC)
79 HTTP_QUERYSTRING_ME(set, ZEND_ACC_PUBLIC)
80
81 #ifndef WONKY
82 HTTP_QUERYSTRING_ME(getInstance, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
83 #endif
84
85 HTTP_QUERYSTRING_GME(getBool, ZEND_ACC_PUBLIC)
86 HTTP_QUERYSTRING_GME(getInt, ZEND_ACC_PUBLIC)
87 HTTP_QUERYSTRING_GME(getFloat, ZEND_ACC_PUBLIC)
88 HTTP_QUERYSTRING_GME(getString, ZEND_ACC_PUBLIC)
89 HTTP_QUERYSTRING_GME(getArray, ZEND_ACC_PUBLIC)
90 HTTP_QUERYSTRING_GME(getObject, ZEND_ACC_PUBLIC)
91
92 EMPTY_FUNCTION_ENTRY
93 };
94 static zend_object_handlers http_querystring_object_handlers;
95
96 PHP_MINIT_FUNCTION(http_querystring_object)
97 {
98 HTTP_REGISTER_CLASS_EX(HttpQueryString, http_querystring_object, NULL, 0);
99
100 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
101 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
102 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
103 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
104 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
105 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
106
107 return SUCCESS;
108 }
109
110 zend_object_value _http_querystring_object_new(zend_class_entry *ce TSRMLS_DC)
111 {
112 return http_querystring_object_new_ex(ce, NULL);
113 }
114
115 zend_object_value _http_querystring_object_new_ex(zend_class_entry *ce, http_querystring_object **ptr TSRMLS_DC)
116 {
117 zend_object_value ov;
118 http_querystring_object *o;
119
120 o = ecalloc(1, sizeof(http_querystring_object));
121 o->zo.ce = ce;
122
123 if (ptr) {
124 *ptr = o;
125 }
126
127 ALLOC_HASHTABLE(OBJ_PROP(o));
128 zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
129 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
130
131 ov.handle = putObject(http_querystring_object, o);
132 ov.handlers = &http_querystring_object_handlers;
133
134 return ov;
135 }
136
137 static inline void _http_querystring_object_declare_default_properties(TSRMLS_D)
138 {
139 zend_class_entry *ce = http_querystring_object_ce;
140
141 DCL_STATIC_PROP_N(PRIVATE, instance);
142
143 DCL_PROP_N(PRIVATE, queryArray);
144 DCL_PROP(PRIVATE, string, queryString, "");
145
146 #ifndef WONKY
147 DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
148 DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
149 DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
150 DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
151 DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
152 DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
153 #endif
154 }
155
156 void _http_querystring_object_free(zend_object *object TSRMLS_DC)
157 {
158 http_querystring_object *o = (http_querystring_object *) object;
159
160 if (OBJ_PROP(o)) {
161 zend_hash_destroy(OBJ_PROP(o));
162 FREE_HASHTABLE(OBJ_PROP(o));
163 }
164 efree(o);
165 }
166
167 #define http_querystring_update(qa, qs) _http_querystring_update((qa), (qs) TSRMLS_CC)
168 static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_DC)
169 {
170 char *s = NULL;
171 size_t l = 0;
172
173 if (Z_TYPE_P(qarray) != IS_ARRAY) {
174 convert_to_array(qarray);
175 }
176 if (SUCCESS == http_urlencode_hash_ex(Z_ARRVAL_P(qarray), 0, NULL, 0, &s, &l)) {
177 zval_dtor(qstring);
178 ZVAL_STRINGL(qstring, s, l, 0);
179 } else {
180 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to update query string");
181 }
182 }
183
184 #define http_querystring_modify_ex(a, k, l, v) _http_querystring_modify_ex((a), (k), (l), (v) TSRMLS_CC)
185 static inline int _http_querystring_modify_ex(zval *qarray, char *key, uint keylen, zval *data TSRMLS_DC)
186 {
187 if (Z_TYPE_P(data) == IS_NULL) {
188 if (SUCCESS != zend_hash_del(Z_ARRVAL_P(qarray), key, keylen)) {
189 return 0;
190 }
191 } else {
192 ZVAL_ADDREF(data);
193 add_assoc_zval(qarray, key, data);
194 }
195 return 1;
196 }
197
198 #define http_querystring_modify_array(q, a) _http_querystring_modify_array((q), (a) TSRMLS_CC)
199 static inline int _http_querystring_modify_array(zval *qarray, zval *array TSRMLS_DC)
200 {
201 zval **value;
202 HashPosition pos;
203 char *key = NULL;
204 uint keylen = 0;
205 ulong idx = 0;
206 int rv = 0;
207
208 FOREACH_KEYLENVAL(pos, array, key, keylen, idx, value) {
209 if (key) {
210 if (http_querystring_modify_ex(qarray, key, keylen, *value)) {
211 rv = 1;
212 }
213 } else {
214 keylen = spprintf(&key, 0, "%lu", idx);
215 if (http_querystring_modify_ex(qarray, key, keylen, *value)) {
216 rv = 1;
217 }
218 efree(key);
219 }
220 key = NULL;
221 }
222
223 return rv;
224 }
225
226 #define http_querystring_modify(q, p) _http_querystring_modify((q), (p) TSRMLS_CC)
227 static inline int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC)
228 {
229 if ((Z_TYPE_P(params) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(params), http_querystring_object_ce TSRMLS_CC)) {
230 return http_querystring_modify_array(qarray, GET_PROP_EX(params, queryArray));
231 } else if (Z_TYPE_P(params) == IS_ARRAY) {
232 return http_querystring_modify_array(qarray, params);
233 } else {
234 int rv;
235 zval array;
236
237 INIT_PZVAL(&array);
238 array_init(&array);
239
240 ZVAL_ADDREF(params);
241 convert_to_string_ex(&params);
242 sapi_module.treat_data(PARSE_STRING, estrdup(Z_STRVAL_P(params)), &array TSRMLS_CC);
243 zval_ptr_dtor(&params);
244 rv = http_querystring_modify_array(qarray, &array);
245 zval_dtor(&array);
246 return rv;
247 }
248 }
249
250 #ifndef WONKY
251 #define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC)
252 static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC)
253 {
254 zval *zobj, *zglobal;
255
256 MAKE_STD_ZVAL(zglobal);
257 ZVAL_BOOL(zglobal, global);
258
259 MAKE_STD_ZVAL(zobj);
260 Z_TYPE_P(zobj) = IS_OBJECT;
261 Z_OBJVAL_P(zobj) = http_querystring_object_new(http_querystring_object_ce);
262 zend_call_method_with_1_params(&zobj, Z_OBJCE_P(zobj), NULL, "__construct", NULL, zglobal);
263
264 zval_ptr_dtor(&zglobal);
265
266 return zobj;
267 }
268 #endif
269
270 #define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC)
271 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)
272 {
273 zval **arrval, *qarray = GET_PROP(queryArray);
274
275 if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void **) &arrval))) {
276 RETVAL_ZVAL(*arrval, 1, 0);
277
278 if (type) {
279 convert_to_type(type, return_value);
280 }
281
282 if (del && (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), name, name_len + 1))) {
283 http_querystring_update(qarray, GET_PROP(queryString));
284 }
285 } else if(defval) {
286 RETURN_ZVAL(defval, 1, 0);
287 }
288 }
289
290 /* {{{ proto void HttpQueryString::__construct([bool global = true[, mixed add])
291 *
292 * Creates a new HttpQueryString object instance.
293 * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE.
294 */
295 PHP_METHOD(HttpQueryString, __construct)
296 {
297 zend_bool global = 1;
298 zval *params = NULL, *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING;
299
300 SET_EH_THROW_HTTP();
301 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, &params)) {
302 if (global) {
303 if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &_SERVER)) &&
304 (Z_TYPE_PP(_SERVER) == IS_ARRAY) &&
305 (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void **) &QUERY_STRING))) {
306
307 qstring = *QUERY_STRING;
308
309 if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
310 qarray = *_GET;
311 } else {
312 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array");
313 }
314 } else {
315 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to QUERY_STRING");
316 }
317
318 if (qarray && qstring) {
319 if (Z_TYPE_P(qstring) != IS_STRING) {
320 convert_to_string(qstring);
321 }
322
323 SET_PROP(queryArray, qarray);
324 SET_PROP(queryString, qstring);
325 GET_PROP(queryArray)->is_ref = 1;
326 GET_PROP(queryString)->is_ref = 1;
327
328 if (params && http_querystring_modify(GET_PROP(queryArray), params)) {
329 http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString));
330 }
331 }
332 } else {
333 qarray = ecalloc(1, sizeof(zval));
334 array_init(qarray);
335
336 SET_PROP(queryArray, qarray);
337 UPD_STRL(queryString, "", 0);
338
339 if (params && http_querystring_modify(qarray, params)) {
340 http_querystring_update(qarray, GET_PROP(queryString));
341 }
342 }
343 }
344 SET_EH_NORMAL();
345 }
346 /* }}} */
347
348 /* {{{ proto string HttpQueryString::toString()
349 *
350 * Returns the string representation.
351 */
352 PHP_METHOD(HttpQueryString, toString)
353 {
354 NO_ARGS;
355 RETURN_PROP(queryString);
356 }
357 /* }}} */
358
359 /* {{{ proto array HttpQueryString::toArray()
360 *
361 * Returns the array representation.
362 */
363 PHP_METHOD(HttpQueryString, toArray)
364 {
365 NO_ARGS;
366 RETURN_PROP(queryArray);
367 }
368 /* }}} */
369
370 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
371 *
372 * Get (part of) the query string.
373 *
374 * The type parameter is either one of the HttpQueryString::TYPE_* constants or a type abbreviation like
375 * "b" for bool, "i" for int, "f" for float, "s" for string, "a" for array and "o" for a stdClass object.
376 */
377 PHP_METHOD(HttpQueryString, get)
378 {
379 char *name = NULL;
380 int name_len = 0;
381 long type = 0;
382 zend_bool del = 0;
383 zval *ztype = NULL, *defval = NULL;
384
385 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name, &name_len, &ztype, &defval, &del)) {
386 if (name && name_len) {
387 if (ztype) {
388 if (Z_TYPE_P(ztype) == IS_LONG) {
389 type = Z_LVAL_P(ztype);
390 } else if(Z_TYPE_P(ztype) == IS_STRING) {
391 switch (tolower(Z_STRVAL_P(ztype)[0]))
392 {
393 case 'b': type = HTTP_QUERYSTRING_TYPE_BOOL; break;
394 case 'i': type = HTTP_QUERYSTRING_TYPE_INT; break;
395 case 'f': type = HTTP_QUERYSTRING_TYPE_FLOAT; break;
396 case 's': type = HTTP_QUERYSTRING_TYPE_STRING; break;
397 case 'a': type = HTTP_QUERYSTRING_TYPE_ARRAY; break;
398 case 'o': type = HTTP_QUERYSTRING_TYPE_OBJECT; break;
399 }
400 }
401 }
402 http_querystring_get(getThis(), type, name, name_len, defval, del, return_value);
403 } else {
404 RETURN_PROP(queryString);
405 }
406 }
407 }
408 /* }}} */
409
410 /* {{{ proto string HttpQueryString::set(mixed params)
411 *
412 * Set query string entry/entries. NULL values will unset the variable.
413 */
414 PHP_METHOD(HttpQueryString, set)
415 {
416 zval *params;
417
418 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params)) {
419 zval *qarray = GET_PROP(queryArray);
420 if (http_querystring_modify(qarray, params)) {
421 http_querystring_update(qarray, GET_PROP(queryString));
422 }
423 }
424
425 IF_RETVAL_USED {
426 RETURN_PROP(queryString);
427 }
428 }
429 /* }}} */
430
431 #ifndef WONKY
432 /* {{{ proto HttpQueryString HttpQueryString::getInstance([bool global = true])
433 *
434 * Get a single instance (differentiates between the global setting).
435 */
436 PHP_METHOD(HttpQueryString, getInstance)
437 {
438 zend_bool global = 1;
439 zval *instance = GET_STATIC_PROP(instance);
440
441 SET_EH_THROW_HTTP();
442 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
443 zval **zobj_ptr = NULL, *zobj = NULL;
444
445 if (Z_TYPE_P(instance) == IS_ARRAY) {
446 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void **) &zobj_ptr)) {
447 RETVAL_ZVAL(*zobj_ptr, 1, 0);
448 } else {
449 zobj = http_querystring_instantiate(global);
450 add_index_zval(instance, global, zobj);
451 RETVAL_OBJECT(zobj, 1);
452 }
453 } else {
454 MAKE_STD_ZVAL(instance);
455 array_init(instance);
456
457 zobj = http_querystring_instantiate(global);
458 add_index_zval(instance, global, zobj);
459 RETVAL_OBJECT(zobj, 1);
460
461 SET_STATIC_PROP(instance, instance);
462 zval_ptr_dtor(&instance);
463 }
464 }
465 SET_EH_NORMAL();
466 }
467 /* }}} */
468 #endif
469
470 /* {{{ Getters by type */
471 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
472 PHP_METHOD(HttpQueryString, method) \
473 { \
474 char *name; \
475 int name_len; \
476 zval *defval = NULL; \
477 zend_bool del = 0; \
478 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
479 http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
480 } \
481 }
482 HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL);
483 HTTP_QUERYSTRING_GETTER(getInt, IS_LONG);
484 HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE);
485 HTTP_QUERYSTRING_GETTER(getString, IS_STRING);
486 HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY);
487 HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT);
488 /* }}} */
489
490 #endif /* ZEND_ENGINE_2 */
491
492 /*
493 * Local variables:
494 * tab-width: 4
495 * c-basic-offset: 4
496 * End:
497 * vim600: noet sw=4 ts=4 fdm=marker
498 * vim<600: noet sw=4 ts=4
499 */
500