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