- add HttpQueryString::mod()
[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_querystring_api.h"
25 #include "php_http_querystring_object.h"
26 #include "php_http_exception_object.h"
27
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)
32
33 HTTP_BEGIN_ARGS(__construct, 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(singleton, 0)
40 HTTP_ARG_VAL(global, 0)
41 HTTP_END_ARGS;
42 #endif
43
44 HTTP_EMPTY_ARGS(toArray);
45 HTTP_EMPTY_ARGS(toString);
46
47 HTTP_BEGIN_ARGS(get, 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, 1)
55 HTTP_ARG_VAL(params, 0)
56 HTTP_END_ARGS;
57
58 HTTP_BEGIN_ARGS(mod, 0)
59 HTTP_ARG_VAL(params, 0)
60 HTTP_END_ARGS;
61
62 HTTP_BEGIN_ARGS(__getter, 1)
63 HTTP_ARG_VAL(name, 0)
64 HTTP_ARG_VAL(defval, 0)
65 HTTP_ARG_VAL(delete, 0)
66 HTTP_END_ARGS;
67
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)
72 HTTP_END_ARGS;
73 #endif
74
75 HTTP_EMPTY_ARGS(serialize);
76 HTTP_BEGIN_ARGS(unserialize, 1)
77 HTTP_ARG_VAL(serialized, 0)
78 HTTP_END_ARGS;
79
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)
84
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)
88
89 HTTP_QUERYSTRING_ME(get, ZEND_ACC_PUBLIC)
90 HTTP_QUERYSTRING_ME(set, ZEND_ACC_PUBLIC)
91 HTTP_QUERYSTRING_ME(mod, ZEND_ACC_PUBLIC)
92
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)
99
100 #ifndef WONKY
101 HTTP_QUERYSTRING_ME(singleton, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
102 #endif
103 #ifdef HTTP_HAVE_ICONV
104 HTTP_QUERYSTRING_ME(xlate, ZEND_ACC_PUBLIC)
105 #endif
106
107 /* Implements Serializable */
108 HTTP_QUERYSTRING_ME(serialize, ZEND_ACC_PUBLIC)
109 HTTP_QUERYSTRING_ME(unserialize, ZEND_ACC_PUBLIC)
110
111 EMPTY_FUNCTION_ENTRY
112 };
113 static zend_object_handlers http_querystring_object_handlers;
114
115 PHP_MINIT_FUNCTION(http_querystring_object)
116 {
117 HTTP_REGISTER_CLASS_EX(HttpQueryString, http_querystring_object, NULL, 0);
118
119 #ifndef WONKY
120 zend_class_implements(http_querystring_object_ce TSRMLS_CC, 1, zend_ce_serializable);
121 #endif
122
123 DCL_STATIC_PROP_N(PRIVATE, instance);
124 DCL_PROP_N(PRIVATE, queryArray);
125 DCL_PROP(PRIVATE, string, queryString, "");
126
127 #ifndef WONKY
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);
134 #endif
135
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);
142
143 return SUCCESS;
144 }
145
146 zend_object_value _http_querystring_object_new(zend_class_entry *ce TSRMLS_DC)
147 {
148 return http_querystring_object_new_ex(ce, NULL);
149 }
150
151 zend_object_value _http_querystring_object_new_ex(zend_class_entry *ce, http_querystring_object **ptr TSRMLS_DC)
152 {
153 zend_object_value ov;
154 http_querystring_object *o;
155
156 o = ecalloc(1, sizeof(http_querystring_object));
157 o->zo.ce = ce;
158
159 if (ptr) {
160 *ptr = o;
161 }
162
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 *));
166
167 ov.handle = putObject(http_querystring_object, o);
168 ov.handlers = &http_querystring_object_handlers;
169
170 return ov;
171 }
172
173 void _http_querystring_object_free(zend_object *object TSRMLS_DC)
174 {
175 http_querystring_object *o = (http_querystring_object *) object;
176
177 if (OBJ_PROP(o)) {
178 zend_hash_destroy(OBJ_PROP(o));
179 FREE_HASHTABLE(OBJ_PROP(o));
180 }
181 efree(o);
182 }
183
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)
187 {
188 zval *zobj, *zglobal;
189
190 MAKE_STD_ZVAL(zglobal);
191 ZVAL_BOOL(zglobal, global);
192
193 MAKE_STD_ZVAL(zobj);
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);
197
198 zval_ptr_dtor(&zglobal);
199
200 return zobj;
201 }
202
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)
205 {
206 zval **arrval, *qarray = GET_PROP(queryArray);
207
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);
210
211 if (type) {
212 convert_to_type(type, return_value);
213 }
214
215 if (del && (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), name, name_len + 1))) {
216 http_querystring_update(qarray, GET_PROP(queryString));
217 }
218 } else if(defval) {
219 RETURN_ZVAL(defval, 1, 0);
220 }
221 }
222 /* }}} */
223
224 /* {{{ proto final void HttpQueryString::__construct([bool global = true[, mixed add])
225 *
226 * Creates a new HttpQueryString object instance.
227 * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE.
228 */
229 PHP_METHOD(HttpQueryString, __construct)
230 {
231 zend_bool global = 1;
232 zval *params = NULL, *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING;
233
234 SET_EH_THROW_HTTP();
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, &params)) {
238 if (global) {
239 #ifdef ZEND_ENGINE_2
240 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
241 #endif
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))) {
245
246 qstring = *QUERY_STRING;
247 #ifdef ZEND_ENGINE_2
248 zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC);
249 #endif
250 if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void *) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
251 qarray = *_GET;
252 } else {
253 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array");
254 }
255 } else {
256 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to QUERY_STRING");
257 }
258
259 if (qarray && qstring) {
260 if (Z_TYPE_P(qstring) != IS_STRING) {
261 convert_to_string(qstring);
262 }
263
264 SET_PROP(queryArray, qarray);
265 SET_PROP(queryString, qstring);
266 GET_PROP(queryArray)->is_ref = 1;
267 GET_PROP(queryString)->is_ref = 1;
268
269 if (params) {
270 http_querystring_modify(GET_PROP(queryArray), params);
271 }
272 http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString));
273 }
274 } else {
275 qarray = ecalloc(1, sizeof(zval));
276 array_init(qarray);
277
278 SET_PROP(queryArray, qarray);
279 UPD_STRL(queryString, "", 0);
280
281 if (params && http_querystring_modify(qarray, params)) {
282 http_querystring_update(qarray, GET_PROP(queryString));
283 }
284 }
285 }
286 SET_EH_NORMAL();
287 }
288 /* }}} */
289
290 /* {{{ proto string HttpQueryString::toString()
291 *
292 * Returns the string representation.
293 */
294 PHP_METHOD(HttpQueryString, toString)
295 {
296 NO_ARGS;
297 RETURN_PROP(queryString);
298 }
299 /* }}} */
300
301 /* {{{ proto array HttpQueryString::toArray()
302 *
303 * Returns the array representation.
304 */
305 PHP_METHOD(HttpQueryString, toArray)
306 {
307 NO_ARGS;
308 RETURN_PROP(queryArray);
309 }
310 /* }}} */
311
312 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
313 *
314 * Get (part of) the query string.
315 *
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.
318 */
319 PHP_METHOD(HttpQueryString, get)
320 {
321 char *name = NULL;
322 int name_len = 0;
323 long type = 0;
324 zend_bool del = 0;
325 zval *ztype = NULL, *defval = NULL;
326
327 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name, &name_len, &ztype, &defval, &del)) {
328 if (name && name_len) {
329 if (ztype) {
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]) {
334 case 'B':
335 case 'b': type = HTTP_QUERYSTRING_TYPE_BOOL; break;
336 case 'I':
337 case 'i': type = HTTP_QUERYSTRING_TYPE_INT; break;
338 case 'F':
339 case 'f': type = HTTP_QUERYSTRING_TYPE_FLOAT; break;
340 case 'S':
341 case 's': type = HTTP_QUERYSTRING_TYPE_STRING; break;
342 case 'A':
343 case 'a': type = HTTP_QUERYSTRING_TYPE_ARRAY; break;
344 case 'O':
345 case 'o': type = HTTP_QUERYSTRING_TYPE_OBJECT; break;
346 }
347 }
348 }
349 http_querystring_get(getThis(), type, name, name_len, defval, del, return_value);
350 } else {
351 RETURN_PROP(queryString);
352 }
353 }
354 }
355 /* }}} */
356
357 /* {{{ proto string HttpQueryString::set(mixed params)
358 *
359 * Set query string entry/entries. NULL values will unset the variable.
360 */
361 PHP_METHOD(HttpQueryString, set)
362 {
363 zval *params;
364
365 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params)) {
366 zval *qarray = GET_PROP(queryArray);
367 if (http_querystring_modify(qarray, params)) {
368 http_querystring_update(qarray, GET_PROP(queryString));
369 }
370 }
371
372 if (return_value_used) {
373 RETURN_PROP(queryString);
374 }
375 }
376 /* }}} */
377
378 /* {{{ proto HttpQueryString HttpQueryString::mod(mixed params)
379 *
380 * Copies the query string object and sets provided params at the clone.
381 * This is basically shorthand for:
382 * <pre>
383 * <?php
384 * $newQS = new HttpQueryString(false, $oldQS);
385 * $newQS->set($other_params);
386 * ?>
387 * </pre>
388 */
389 PHP_METHOD(HttpQueryString, mod)
390 {
391 zval *orig, *zobj, *qarr, *qstr, *params;
392
393 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params)) {
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);
398
399 array_copy(orig, qarr);
400 http_querystring_modify(qarr, params);
401 http_querystring_update(qarr, qstr);
402
403 RETURN_ZVAL(zobj, 1, 1);
404 }
405 }
406 /* }}} */
407
408 #ifndef WONKY
409 /* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true])
410 *
411 * Get a single instance (differentiates between the global setting).
412 */
413 PHP_METHOD(HttpQueryString, singleton)
414 {
415 zend_bool global = 1;
416 zval *instance = GET_STATIC_PROP(instance);
417
418 SET_EH_THROW_HTTP();
419 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
420 zval **zobj_ptr = NULL, *zobj = NULL;
421
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);
425 } else {
426 zobj = http_querystring_instantiate(global);
427 add_index_zval(instance, global, zobj);
428 RETVAL_OBJECT(zobj, 1);
429 }
430 } else {
431 MAKE_STD_ZVAL(instance);
432 array_init(instance);
433
434 zobj = http_querystring_instantiate(global);
435 add_index_zval(instance, global, zobj);
436 RETVAL_OBJECT(zobj, 1);
437
438 SET_STATIC_PROP(instance, instance);
439 zval_ptr_dtor(&instance);
440 }
441 }
442 SET_EH_NORMAL();
443 }
444 /* }}} */
445 #endif
446
447 /* {{{ Getters by type */
448 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
449 PHP_METHOD(HttpQueryString, method) \
450 { \
451 char *name; \
452 int name_len; \
453 zval *defval = NULL; \
454 zend_bool del = 0; \
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); \
457 } \
458 }
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);
465 /* }}} */
466
467 #ifdef HTTP_HAVE_ICONV
468 /* {{{ proto bool HttpQueryString::xlate(string ie, string oe)
469 *
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.
472 *
473 * Returns TRUE on success or FALSE on failure.
474 */
475 PHP_METHOD(HttpQueryString, xlate)
476 {
477 char *ie, *oe;
478 int ie_len, oe_len;
479 zval xa, *qa, *qs;
480 STATUS rs;
481
482 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len)) {
483 RETURN_FALSE;
484 }
485
486 qa = GET_PROP(queryArray);
487 qs = GET_PROP(queryString);
488 INIT_PZVAL(&xa);
489 array_init(&xa);
490
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);
495 }
496 zval_dtor(&xa);
497
498 RETURN_SUCCESS(rs);
499 }
500 /* }}} */
501 #endif /* HAVE_ICONV */
502
503 /* {{{ proto string HttpQueryString::serialize()
504 *
505 * Implements Serializable.
506 */
507 PHP_METHOD(HttpQueryString, serialize)
508 {
509 NO_ARGS;
510 RETURN_PROP(queryString);
511 }
512 /* }}} */
513
514 /* {{{ proto void HttpQueryString::unserialize(string serialized)
515 *
516 * Implements Serializable.
517 */
518 PHP_METHOD(HttpQueryString, unserialize)
519 {
520 zval *serialized;
521
522 SET_EH_THROW_HTTP();
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);
526
527 zend_hash_clean(Z_ARRVAL_P(qa));
528 http_querystring_modify(qa, serialized);
529 http_querystring_update(qa, GET_PROP(queryString));
530 } else {
531 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Expected a string as parameter");
532 }
533 }
534 SET_EH_NORMAL();
535 }
536 /* }}} */
537
538 #endif /* ZEND_ENGINE_2 */
539
540 /*
541 * Local variables:
542 * tab-width: 4
543 * c-basic-offset: 4
544 * End:
545 * vim600: noet sw=4 ts=4 fdm=marker
546 * vim<600: noet sw=4 ts=4
547 */
548