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