- attempt to fix gcc-2.95 build by using less HTTP_HAVE_$EXT magic
[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(__getter, 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 #ifdef HTTP_HAVE_ICONV
65 HTTP_BEGIN_ARGS(xlate, 2)
66 HTTP_ARG_VAL(from_encoding, 0)
67 HTTP_ARG_VAL(to_encoding, 0)
68 HTTP_END_ARGS;
69 #endif
70
71 HTTP_EMPTY_ARGS(serialize);
72 HTTP_BEGIN_ARGS(unserialize, 1)
73 HTTP_ARG_VAL(serialized, 0)
74 HTTP_END_ARGS;
75
76 #define OBJ_PROP_CE http_querystring_object_ce
77 zend_class_entry *http_querystring_object_ce;
78 zend_function_entry http_querystring_object_fe[] = {
79 HTTP_QUERYSTRING_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL)
80
81 HTTP_QUERYSTRING_ME(toArray, ZEND_ACC_PUBLIC)
82 HTTP_QUERYSTRING_ME(toString, ZEND_ACC_PUBLIC)
83 ZEND_MALIAS(HttpQueryString, __toString, toString, HTTP_ARGS(HttpQueryString, toString), ZEND_ACC_PUBLIC)
84
85 HTTP_QUERYSTRING_ME(get, ZEND_ACC_PUBLIC)
86 HTTP_QUERYSTRING_ME(set, ZEND_ACC_PUBLIC)
87
88 HTTP_QUERYSTRING_GME(getBool, ZEND_ACC_PUBLIC)
89 HTTP_QUERYSTRING_GME(getInt, ZEND_ACC_PUBLIC)
90 HTTP_QUERYSTRING_GME(getFloat, ZEND_ACC_PUBLIC)
91 HTTP_QUERYSTRING_GME(getString, ZEND_ACC_PUBLIC)
92 HTTP_QUERYSTRING_GME(getArray, ZEND_ACC_PUBLIC)
93 HTTP_QUERYSTRING_GME(getObject, ZEND_ACC_PUBLIC)
94
95 #ifndef WONKY
96 HTTP_QUERYSTRING_ME(singleton, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
97 #endif
98 #ifdef HTTP_HAVE_ICONV
99 HTTP_QUERYSTRING_ME(xlate, ZEND_ACC_PUBLIC)
100 #endif
101
102 /* Implements Serializable */
103 HTTP_QUERYSTRING_ME(serialize, ZEND_ACC_PUBLIC)
104 HTTP_QUERYSTRING_ME(unserialize, ZEND_ACC_PUBLIC)
105
106 EMPTY_FUNCTION_ENTRY
107 };
108 static zend_object_handlers http_querystring_object_handlers;
109
110 PHP_MINIT_FUNCTION(http_querystring_object)
111 {
112 HTTP_REGISTER_CLASS_EX(HttpQueryString, http_querystring_object, NULL, 0);
113
114 #ifndef WONKY
115 zend_class_implements(http_querystring_object_ce TSRMLS_CC, 1, zend_ce_serializable);
116 #endif
117
118 DCL_STATIC_PROP_N(PRIVATE, instance);
119 DCL_PROP_N(PRIVATE, queryArray);
120 DCL_PROP(PRIVATE, string, queryString, "");
121
122 #ifndef WONKY
123 DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
124 DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
125 DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
126 DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
127 DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
128 DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
129 #endif
130
131 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL);
132 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_INT", HTTP_QUERYSTRING_TYPE_INT);
133 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT);
134 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING);
135 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY);
136 HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT);
137
138 return SUCCESS;
139 }
140
141 zend_object_value _http_querystring_object_new(zend_class_entry *ce TSRMLS_DC)
142 {
143 return http_querystring_object_new_ex(ce, NULL);
144 }
145
146 zend_object_value _http_querystring_object_new_ex(zend_class_entry *ce, http_querystring_object **ptr TSRMLS_DC)
147 {
148 zend_object_value ov;
149 http_querystring_object *o;
150
151 o = ecalloc(1, sizeof(http_querystring_object));
152 o->zo.ce = ce;
153
154 if (ptr) {
155 *ptr = o;
156 }
157
158 ALLOC_HASHTABLE(OBJ_PROP(o));
159 zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
160 zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
161
162 ov.handle = putObject(http_querystring_object, o);
163 ov.handlers = &http_querystring_object_handlers;
164
165 return ov;
166 }
167
168 void _http_querystring_object_free(zend_object *object TSRMLS_DC)
169 {
170 http_querystring_object *o = (http_querystring_object *) object;
171
172 if (OBJ_PROP(o)) {
173 zend_hash_destroy(OBJ_PROP(o));
174 FREE_HASHTABLE(OBJ_PROP(o));
175 }
176 efree(o);
177 }
178
179 /* {{{ querystring helpers */
180 #ifndef WONKY
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 #endif /* WONKY */
199
200 #define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC)
201 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)
202 {
203 zval **arrval, *qarray = GET_PROP(queryArray);
204
205 if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void *) &arrval))) {
206 RETVAL_ZVAL(*arrval, 1, 0);
207
208 if (type) {
209 convert_to_type(type, return_value);
210 }
211
212 if (del && (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), name, name_len + 1))) {
213 http_querystring_update(qarray, GET_PROP(queryString));
214 }
215 } else if(defval) {
216 RETURN_ZVAL(defval, 1, 0);
217 }
218 }
219 /* }}} */
220
221 /* {{{ proto final void HttpQueryString::__construct([bool global = true[, mixed add])
222 *
223 * Creates a new HttpQueryString object instance.
224 * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE.
225 */
226 PHP_METHOD(HttpQueryString, __construct)
227 {
228 zend_bool global = 1;
229 zval *params = NULL, *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING;
230
231 SET_EH_THROW_HTTP();
232 if (!sapi_module.treat_data) {
233 http_error(HE_ERROR, HTTP_E_QUERYSTRING, "The SAPI does not have a treat_data function registered");
234 } else if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, &params)) {
235 if (global) {
236 #ifdef ZEND_ENGINE_2
237 zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
238 #endif
239 if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &_SERVER)) &&
240 (Z_TYPE_PP(_SERVER) == IS_ARRAY) &&
241 (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void *) &QUERY_STRING))) {
242
243 qstring = *QUERY_STRING;
244 #ifdef ZEND_ENGINE_2
245 zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC);
246 #endif
247 if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void *) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) {
248 qarray = *_GET;
249 } else {
250 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array");
251 }
252 } else {
253 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to QUERY_STRING");
254 }
255
256 if (qarray && qstring) {
257 if (Z_TYPE_P(qstring) != IS_STRING) {
258 convert_to_string(qstring);
259 }
260
261 SET_PROP(queryArray, qarray);
262 SET_PROP(queryString, qstring);
263 GET_PROP(queryArray)->is_ref = 1;
264 GET_PROP(queryString)->is_ref = 1;
265
266 if (params) {
267 http_querystring_modify(GET_PROP(queryArray), params);
268 }
269 http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString));
270 }
271 } else {
272 qarray = ecalloc(1, sizeof(zval));
273 array_init(qarray);
274
275 SET_PROP(queryArray, qarray);
276 UPD_STRL(queryString, "", 0);
277
278 if (params && http_querystring_modify(qarray, params)) {
279 http_querystring_update(qarray, GET_PROP(queryString));
280 }
281 }
282 }
283 SET_EH_NORMAL();
284 }
285 /* }}} */
286
287 /* {{{ proto string HttpQueryString::toString()
288 *
289 * Returns the string representation.
290 */
291 PHP_METHOD(HttpQueryString, toString)
292 {
293 NO_ARGS;
294 RETURN_PROP(queryString);
295 }
296 /* }}} */
297
298 /* {{{ proto array HttpQueryString::toArray()
299 *
300 * Returns the array representation.
301 */
302 PHP_METHOD(HttpQueryString, toArray)
303 {
304 NO_ARGS;
305 RETURN_PROP(queryArray);
306 }
307 /* }}} */
308
309 /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]])
310 *
311 * Get (part of) the query string.
312 *
313 * The type parameter is either one of the HttpQueryString::TYPE_* constants or a type abbreviation like
314 * "b" for bool, "i" for int, "f" for float, "s" for string, "a" for array and "o" for a stdClass object.
315 */
316 PHP_METHOD(HttpQueryString, get)
317 {
318 char *name = NULL;
319 int name_len = 0;
320 long type = 0;
321 zend_bool del = 0;
322 zval *ztype = NULL, *defval = NULL;
323
324 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name, &name_len, &ztype, &defval, &del)) {
325 if (name && name_len) {
326 if (ztype) {
327 if (Z_TYPE_P(ztype) == IS_LONG) {
328 type = Z_LVAL_P(ztype);
329 } else if(Z_TYPE_P(ztype) == IS_STRING) {
330 switch (Z_STRVAL_P(ztype)[0]) {
331 case 'B':
332 case 'b': type = HTTP_QUERYSTRING_TYPE_BOOL; break;
333 case 'I':
334 case 'i': type = HTTP_QUERYSTRING_TYPE_INT; break;
335 case 'F':
336 case 'f': type = HTTP_QUERYSTRING_TYPE_FLOAT; break;
337 case 'S':
338 case 's': type = HTTP_QUERYSTRING_TYPE_STRING; break;
339 case 'A':
340 case 'a': type = HTTP_QUERYSTRING_TYPE_ARRAY; break;
341 case 'O':
342 case 'o': type = HTTP_QUERYSTRING_TYPE_OBJECT; break;
343 }
344 }
345 }
346 http_querystring_get(getThis(), type, name, name_len, defval, del, return_value);
347 } else {
348 RETURN_PROP(queryString);
349 }
350 }
351 }
352 /* }}} */
353
354 /* {{{ proto string HttpQueryString::set(mixed params)
355 *
356 * Set query string entry/entries. NULL values will unset the variable.
357 */
358 PHP_METHOD(HttpQueryString, set)
359 {
360 zval *params;
361
362 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &params)) {
363 zval *qarray = GET_PROP(queryArray);
364 if (http_querystring_modify(qarray, params)) {
365 http_querystring_update(qarray, GET_PROP(queryString));
366 }
367 }
368
369 if (return_value_used) {
370 RETURN_PROP(queryString);
371 }
372 }
373 /* }}} */
374
375 #ifndef WONKY
376 /* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true])
377 *
378 * Get a single instance (differentiates between the global setting).
379 */
380 PHP_METHOD(HttpQueryString, singleton)
381 {
382 zend_bool global = 1;
383 zval *instance = GET_STATIC_PROP(instance);
384
385 SET_EH_THROW_HTTP();
386 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) {
387 zval **zobj_ptr = NULL, *zobj = NULL;
388
389 if (Z_TYPE_P(instance) == IS_ARRAY) {
390 if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void *) &zobj_ptr)) {
391 RETVAL_ZVAL(*zobj_ptr, 1, 0);
392 } else {
393 zobj = http_querystring_instantiate(global);
394 add_index_zval(instance, global, zobj);
395 RETVAL_OBJECT(zobj, 1);
396 }
397 } else {
398 MAKE_STD_ZVAL(instance);
399 array_init(instance);
400
401 zobj = http_querystring_instantiate(global);
402 add_index_zval(instance, global, zobj);
403 RETVAL_OBJECT(zobj, 1);
404
405 SET_STATIC_PROP(instance, instance);
406 zval_ptr_dtor(&instance);
407 }
408 }
409 SET_EH_NORMAL();
410 }
411 /* }}} */
412 #endif
413
414 /* {{{ Getters by type */
415 #define HTTP_QUERYSTRING_GETTER(method, TYPE) \
416 PHP_METHOD(HttpQueryString, method) \
417 { \
418 char *name; \
419 int name_len; \
420 zval *defval = NULL; \
421 zend_bool del = 0; \
422 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \
423 http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \
424 } \
425 }
426 HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL);
427 HTTP_QUERYSTRING_GETTER(getInt, IS_LONG);
428 HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE);
429 HTTP_QUERYSTRING_GETTER(getString, IS_STRING);
430 HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY);
431 HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT);
432 /* }}} */
433
434 #ifdef HTTP_HAVE_ICONV
435 /* {{{ proto bool HttpQueryString::xlate(string ie, string oe)
436 *
437 * Converts the query string from the source encoding ie to the target encoding oe.
438 * WARNING: Don't use any character set that can contain NUL bytes like UTF-16.
439 *
440 * Returns TRUE on success or FALSE on failure.
441 */
442 PHP_METHOD(HttpQueryString, xlate)
443 {
444 char *ie, *oe;
445 int ie_len, oe_len;
446 zval xa, *qa, *qs;
447 STATUS rs;
448
449 if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len)) {
450 RETURN_FALSE;
451 }
452
453 qa = GET_PROP(queryArray);
454 qs = GET_PROP(queryString);
455 INIT_PZVAL(&xa);
456 array_init(&xa);
457
458 if (SUCCESS == (rs = http_querystring_xlate(&xa, qa, ie, oe))) {
459 zend_hash_clean(Z_ARRVAL_P(qa));
460 zend_hash_copy(Z_ARRVAL_P(qa), Z_ARRVAL(xa), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
461 http_querystring_update(qa, qs);
462 }
463 zval_dtor(&xa);
464
465 RETURN_SUCCESS(rs);
466 }
467 /* }}} */
468 #endif /* HAVE_ICONV */
469
470 /* {{{ proto string HttpQueryString::serialize()
471 *
472 * Implements Serializable.
473 */
474 PHP_METHOD(HttpQueryString, serialize)
475 {
476 NO_ARGS;
477 RETURN_PROP(queryString);
478 }
479 /* }}} */
480
481 /* {{{ proto void HttpQueryString::unserialize(string serialized)
482 *
483 * Implements Serializable.
484 */
485 PHP_METHOD(HttpQueryString, unserialize)
486 {
487 zval *serialized;
488
489 SET_EH_THROW_HTTP();
490 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &serialized)) {
491 if (Z_TYPE_P(serialized) == IS_STRING) {
492 zval *qa = GET_PROP(queryArray);
493
494 zend_hash_clean(Z_ARRVAL_P(qa));
495 http_querystring_modify(qa, serialized);
496 http_querystring_update(qa, GET_PROP(queryString));
497 } else {
498 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Expected a string as parameter");
499 }
500 }
501 SET_EH_NORMAL();
502 }
503 /* }}} */
504
505 #endif /* ZEND_ENGINE_2 */
506
507 /*
508 * Local variables:
509 * tab-width: 4
510 * c-basic-offset: 4
511 * End:
512 * vim600: noet sw=4 ts=4 fdm=marker
513 * vim<600: noet sw=4 ts=4
514 */
515