2 +--------------------------------------------------------------------+
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-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 static php_http_params_token_t def_param_sep
= {",", 1}, *def_param_sep_ptr
[] = {&def_param_sep
, NULL
};
16 static php_http_params_token_t def_arg_sep
= {";", 1}, *def_arg_sep_ptr
[] = {&def_arg_sep
, NULL
};
17 static php_http_params_token_t def_val_sep
= {"=", 1}, *def_val_sep_ptr
[] = {&def_val_sep
, NULL
};
18 static php_http_params_opts_t def_opts
= {
24 PHP_HTTP_PARAMS_DEFAULT
27 php_http_params_opts_t
*php_http_params_opts_default_get(php_http_params_opts_t
*opts
)
30 opts
= emalloc(sizeof(*opts
));
33 memcpy(opts
, &def_opts
, sizeof(def_opts
));
38 typedef struct php_http_params_state
{
39 php_http_params_token_t input
;
40 php_http_params_token_t param
;
41 php_http_params_token_t arg
;
42 php_http_params_token_t val
;
51 } php_http_params_state_t
;
53 static inline void sanitize_default(zval
*zv TSRMLS_DC
)
55 if (Z_STRVAL_P(zv
)[0] == '"' && Z_STRVAL_P(zv
)[Z_STRLEN_P(zv
) - 1] == '"') {
56 size_t deq_len
= Z_STRLEN_P(zv
) - 2;
57 char *deq
= estrndup(Z_STRVAL_P(zv
) + 1, deq_len
);
60 ZVAL_STRINGL(zv
, deq
, deq_len
, 0);
63 php_stripslashes(Z_STRVAL_P(zv
), &Z_STRLEN_P(zv
) TSRMLS_CC
);
66 static inline void prepare_default(zval
*zv TSRMLS_DC
)
68 if (Z_TYPE_P(zv
) == IS_STRING
) {
69 int len
= Z_STRLEN_P(zv
);
71 Z_STRVAL_P(zv
) = php_addslashes(Z_STRVAL_P(zv
), Z_STRLEN_P(zv
), &Z_STRLEN_P(zv
), 1 TSRMLS_CC
);
73 if (len
!= Z_STRLEN_P(zv
)) {
75 int len
= Z_STRLEN_P(zv
) + 2;
76 char *str
= emalloc(len
+ 1);
79 memcpy(&str
[1], Z_STRVAL_P(zv
), Z_STRLEN_P(zv
));
84 ZVAL_STRINGL(zv
, str
, len
, 0);
88 ZVAL_EMPTY_STRING(zv
);
92 static inline void sanitize_urlencoded(zval
*zv TSRMLS_DC
)
94 Z_STRLEN_P(zv
) = php_raw_url_decode(Z_STRVAL_P(zv
), Z_STRLEN_P(zv
));
97 static inline void prepare_urlencoded(zval
*zv TSRMLS_DC
)
100 char *str
= php_url_encode(Z_STRVAL_P(zv
), Z_STRLEN_P(zv
), &len
);
103 ZVAL_STRINGL(zv
, str
, len
, 0);
106 static void sanitize_dimension(zval
*zv TSRMLS_DC
)
108 zval
*arr
= NULL
, *tmp
= NULL
, **cur
= NULL
;
109 char *var
= NULL
, *ptr
= Z_STRVAL_P(zv
), *end
= Z_STRVAL_P(zv
) + Z_STRLEN_P(zv
);
123 if (++level
> PG(max_input_nesting_level
)) {
125 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Max input nesting level of %ld exceeded", (long) PG(max_input_nesting_level
));
128 if (ptr
- var
== 0) {
138 convert_to_array(*cur
);
143 zend_symtable_update(Z_ARRVAL_PP(cur
), var
, ptr
- var
+ 1, (void *) &tmp
, sizeof(zval
*), (void *) &cur
);
146 zend_hash_next_index_insert(Z_ARRVAL_PP(cur
), (void *) &tmp
, sizeof(zval
*), (void *) &cur
);
156 if (zend_hash_num_elements(Z_ARRVAL_P(arr
))) {
158 #if PHP_VERSION_ID >= 50400
159 ZVAL_COPY_VALUE(zv
, arr
);
161 zv
->value
= arr
->value
;
162 Z_TYPE_P(zv
) = Z_TYPE_P(arr
);
170 static inline void shift_key(php_http_buffer_t
*buf
, char *key_str
, size_t key_len
, const char *ass
, size_t asl
, unsigned flags TSRMLS_DC
);
171 static inline void shift_val(php_http_buffer_t
*buf
, zval
*zvalue
, const char *vss
, size_t vsl
, unsigned flags TSRMLS_DC
);
173 static void prepare_dimension(php_http_buffer_t
*buf
, php_http_buffer_t
*keybuf
, zval
*zvalue
, const char *pss
, size_t psl
, const char *vss
, size_t vsl
, unsigned flags TSRMLS_DC
)
175 HashTable
*ht
= HASH_OF(zvalue
);
177 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
179 php_http_buffer_t prefix
;
181 if (!ht
->nApplyCount
++) {
182 php_http_buffer_init(&prefix
);
183 php_http_buffer_append(&prefix
, keybuf
->data
, keybuf
->used
);
185 FOREACH_HASH_KEYVAL(pos
, ht
, key
, val
) {
186 if (key
.type
== HASH_KEY_IS_STRING
&& !*key
.str
) {
187 /* only public properties */
191 php_http_buffer_appends(&prefix
, "[");
192 if (key
.type
== HASH_KEY_IS_STRING
) {
193 php_http_buffer_append(&prefix
, key
.str
, key
.len
- 1);
195 php_http_buffer_appendf(&prefix
, "%lu", key
.num
);
197 php_http_buffer_appends(&prefix
, "]");
199 if (Z_TYPE_PP(val
) == IS_ARRAY
|| Z_TYPE_PP(val
) == IS_OBJECT
) {
200 prepare_dimension(buf
, &prefix
, *val
, pss
, psl
, vss
, vsl
, flags TSRMLS_CC
);
202 zval
*cpy
= php_http_ztyp(IS_STRING
, *val
);
204 shift_key(buf
, prefix
.data
, prefix
.used
, pss
, psl
, flags TSRMLS_CC
);
205 shift_val(buf
, cpy
, vss
, vsl
, flags TSRMLS_CC
);
209 php_http_buffer_cut(&prefix
, keybuf
->used
, prefix
.used
- keybuf
->used
);
211 php_http_buffer_dtor(&prefix
);
216 static inline void sanitize_key(unsigned flags
, char *str
, size_t len
, zval
*zv
, zend_bool
*rfc5987 TSRMLS_DC
)
221 php_trim(str
, len
, NULL
, 0, zv
, 3 TSRMLS_CC
);
223 if (flags
& PHP_HTTP_PARAMS_DEFAULT
) {
224 sanitize_default(zv TSRMLS_CC
);
227 eos
= &Z_STRVAL(zv
)[Z_STRLEN(zv
)-1];
234 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
235 sanitize_urlencoded(zv TSRMLS_CC
);
238 if (flags
& PHP_HTTP_PARAMS_DIMENSION
) {
239 sanitize_dimension(zv TSRMLS_CC
);
243 static inline void sanitze_rfc5987(zval
*zv
, unsigned *flags TSRMLS_DC
)
248 static inline void sanitize_value(unsigned flags
, char *str
, size_t len
, zval
*zv
, zend_bool rfc5987 TSRMLS_DC
)
251 php_trim(str
, len
, NULL
, 0, zv
, 3 TSRMLS_CC
);
254 sanitize_rfc5987(zv
, &flags TSRMLS_CC
);
257 if (flags
& PHP_HTTP_PARAMS_DEFAULT
) {
258 sanitize_default(zv TSRMLS_CC
);
261 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
262 sanitize_urlencoded(zv TSRMLS_CC
);
266 static inline void prepare_key(unsigned flags
, char *old_key
, size_t old_len
, char **new_key
, size_t *new_len TSRMLS_DC
)
271 ZVAL_STRINGL(&zv
, old_key
, old_len
, 1);
273 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
274 prepare_urlencoded(&zv TSRMLS_CC
);
277 if (flags
& PHP_HTTP_PARAMS_DEFAULT
) {
278 prepare_default(&zv TSRMLS_CC
);
281 *new_key
= Z_STRVAL(zv
);
282 *new_len
= Z_STRLEN(zv
);
285 static inline void prepare_value(unsigned flags
, zval
*zv TSRMLS_DC
)
287 if (flags
& PHP_HTTP_PARAMS_URLENCODED
) {
288 prepare_urlencoded(zv TSRMLS_CC
);
291 if (flags
& PHP_HTTP_PARAMS_DEFAULT
) {
292 prepare_default(zv TSRMLS_CC
);
296 static void merge_param(HashTable
*params
, zval
*zdata
, zval
***current_param
, zval
***current_args TSRMLS_DC
)
298 zval
**ptr
, **zdata_ptr
;
299 php_http_array_hashkey_t hkey
= php_http_array_hashkey_init(0);
304 INIT_PZVAL_ARRAY(&tmp
, params
);
305 fprintf(stderr
, "params = ");
306 zend_print_zval_r(&tmp
, 1 TSRMLS_CC
);
307 fprintf(stderr
, "\n");
311 hkey
.type
= zend_hash_get_current_key_ex(Z_ARRVAL_P(zdata
), &hkey
.str
, &hkey
.len
, &hkey
.num
, hkey
.dup
, NULL
);
313 if ((hkey
.type
== HASH_KEY_IS_STRING
&& !zend_hash_exists(params
, hkey
.str
, hkey
.len
))
314 || (hkey
.type
== HASH_KEY_IS_LONG
&& !zend_hash_index_exists(params
, hkey
.num
))
316 zval
*tmp
, *arg
, **args
;
318 /* create the entry if it doesn't exist */
319 zend_hash_get_current_data(Z_ARRVAL_P(zdata
), (void *) &ptr
);
323 add_assoc_zval_ex(tmp
, ZEND_STRS("value"), *ptr
);
327 zend_hash_update(Z_ARRVAL_P(tmp
), "arguments", sizeof("arguments"), (void *) &arg
, sizeof(zval
*), (void *) &args
);
328 *current_args
= args
;
330 if (hkey
.type
== HASH_KEY_IS_STRING
) {
331 zend_hash_update(params
, hkey
.str
, hkey
.len
, (void *) &tmp
, sizeof(zval
*), (void *) &ptr
);
333 zend_hash_index_update(params
, hkey
.num
, (void *) &tmp
, sizeof(zval
*), (void *) &ptr
);
337 if (hkey
.type
== HASH_KEY_IS_STRING
) {
338 zend_hash_find(params
, hkey
.str
, hkey
.len
, (void *) &ptr
);
340 zend_hash_index_find(params
, hkey
.num
, (void *) &ptr
);
345 if (Z_TYPE_PP(ptr
) == IS_ARRAY
346 && SUCCESS
== zend_hash_find(Z_ARRVAL_PP(ptr
), "value", sizeof("value"), (void *) &ptr
)
347 && SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_PP(zdata_ptr
), (void *) &zdata_ptr
)
350 * params = [arr => [value => [0 => 1]]]
352 * zdata = [arr => [0 => NULL]]
357 while (Z_TYPE_PP(zdata_ptr
) == IS_ARRAY
358 && SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_PP(zdata_ptr
), (void *) &test_ptr
)
360 if (Z_TYPE_PP(test_ptr
) == IS_ARRAY
) {
362 /* now find key in ptr */
363 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key_ex(Z_ARRVAL_PP(zdata_ptr
), &hkey
.str
, &hkey
.len
, &hkey
.num
, hkey
.dup
, NULL
)) {
364 if (SUCCESS
== zend_hash_find(Z_ARRVAL_PP(ptr
), hkey
.str
, hkey
.len
, (void *) &ptr
)) {
365 zdata_ptr
= test_ptr
;
367 Z_ADDREF_PP(test_ptr
);
368 zend_hash_update(Z_ARRVAL_PP(ptr
), hkey
.str
, hkey
.len
, (void *) test_ptr
, sizeof(zval
*), (void *) &ptr
);
372 if (SUCCESS
== zend_hash_index_find(Z_ARRVAL_PP(ptr
), hkey
.num
, (void *) &ptr
)) {
373 zdata_ptr
= test_ptr
;
374 } else if (hkey
.num
) {
375 Z_ADDREF_PP(test_ptr
);
376 zend_hash_index_update(Z_ARRVAL_PP(ptr
), hkey
.num
, (void *) test_ptr
, sizeof(zval
*), (void *) &ptr
);
379 Z_ADDREF_PP(test_ptr
);
380 zend_hash_next_index_insert(Z_ARRVAL_PP(ptr
), (void *) test_ptr
, sizeof(zval
*), (void *) &ptr
);
385 /* this is the leaf */
386 Z_ADDREF_PP(test_ptr
);
387 if (Z_TYPE_PP(ptr
) != IS_ARRAY
) {
391 if (HASH_KEY_IS_STRING
== zend_hash_get_current_key_ex(Z_ARRVAL_PP(zdata_ptr
), &hkey
.str
, &hkey
.len
, &hkey
.num
, hkey
.dup
, NULL
)) {
392 zend_hash_update(Z_ARRVAL_PP(ptr
), hkey
.str
, hkey
.len
, (void *) test_ptr
, sizeof(zval
*), (void *) &ptr
);
393 } else if (hkey
.num
) {
394 zend_hash_index_update(Z_ARRVAL_PP(ptr
), hkey
.num
, (void *) test_ptr
, sizeof(zval
*), (void *) &ptr
);
396 zend_hash_next_index_insert(Z_ARRVAL_PP(ptr
), (void *) test_ptr
, sizeof(zval
*), (void *) &ptr
);
406 while (Z_TYPE_PP(ptr
) == IS_ARRAY
&& SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_PP(ptr
), (void *) &ptr
));
407 *current_param
= ptr
;
410 static void push_param(HashTable
*params
, php_http_params_state_t
*state
, const php_http_params_opts_t
*opts TSRMLS_DC
)
412 if (state
->val
.str
) {
413 if (0 < (state
->val
.len
= state
->input
.str
- state
->val
.str
)) {
414 sanitize_value(opts
->flags
, state
->val
.str
, state
->val
.len
, *(state
->current
.val
), state
->rfc5987 TSRMLS_CC
);
417 } else if (state
->arg
.str
) {
418 if (0 < (state
->arg
.len
= state
->input
.str
- state
->arg
.str
)) {
420 zend_bool rfc5987
= 0;
424 sanitize_key(opts
->flags
, state
->arg
.str
, state
->arg
.len
, &key
, &rfc5987 TSRMLS_CC
);
425 state
->rfc5987
= rfc5987
;
426 if (Z_TYPE(key
) == IS_STRING
&& Z_STRLEN(key
)) {
429 zend_symtable_update(Z_ARRVAL_PP(state
->current
.args
), Z_STRVAL(key
), Z_STRLEN(key
) + 1, (void *) &val
, sizeof(zval
*), (void *) &state
->current
.val
);
433 } else if (state
->param
.str
) {
434 if (0 < (state
->param
.len
= state
->input
.str
- state
->param
.str
)) {
435 zval
*prm
, *arg
, *val
, *key
;
436 zend_bool rfc5987
= 0;
440 sanitize_key(opts
->flags
, state
->param
.str
, state
->param
.len
, key
, &rfc5987 TSRMLS_CC
);
441 state
->rfc5987
= rfc5987
;
442 if (Z_TYPE_P(key
) != IS_STRING
) {
443 merge_param(params
, key
, &state
->current
.val
, &state
->current
.args TSRMLS_CC
);
444 } else if (Z_STRLEN_P(key
)) {
450 ZVAL_COPY_VALUE(val
, opts
->defval
);
455 zend_hash_update(Z_ARRVAL_P(prm
), "value", sizeof("value"), (void *) &val
, sizeof(zval
*), (void *) &state
->current
.val
);
459 zend_hash_update(Z_ARRVAL_P(prm
), "arguments", sizeof("arguments"), (void *) &arg
, sizeof(zval
*), (void *) &state
->current
.args
);
461 zend_symtable_update(params
, Z_STRVAL_P(key
), Z_STRLEN_P(key
) + 1, (void *) &prm
, sizeof(zval
*), (void *) &state
->current
.param
);
468 static inline zend_bool
check_str(const char *chk_str
, size_t chk_len
, const char *sep_str
, size_t sep_len
) {
469 return 0 < sep_len
&& chk_len
>= sep_len
&& !memcmp(chk_str
, sep_str
, sep_len
);
472 static size_t check_sep(php_http_params_state_t
*state
, php_http_params_token_t
**separators
)
474 php_http_params_token_t
**sep
= separators
;
476 if (state
->quotes
|| state
->escape
) {
480 if (sep
) while (*sep
) {
481 if (check_str(state
->input
.str
, state
->input
.len
, (*sep
)->str
, (*sep
)->len
)) {
489 static void skip_sep(size_t skip
, php_http_params_state_t
*state
, php_http_params_token_t
**param
, php_http_params_token_t
**arg
, php_http_params_token_t
**val TSRMLS_DC
)
493 state
->input
.str
+= skip
;
494 state
->input
.len
-= skip
;
496 while ( (param
&& (sep_len
= check_sep(state
, param
)))
497 || (arg
&& (sep_len
= check_sep(state
, arg
)))
498 || (val
&& (sep_len
= check_sep(state
, val
)))
500 state
->input
.str
+= sep_len
;
501 state
->input
.len
-= sep_len
;
505 HashTable
*php_http_params_parse(HashTable
*params
, const php_http_params_opts_t
*opts TSRMLS_DC
)
507 php_http_params_state_t state
= {{NULL
,0}, {NULL
,0}, {NULL
,0}, {NULL
,0}, {NULL
,NULL
,NULL
}, 0, 0};
509 state
.input
.str
= opts
->input
.str
;
510 state
.input
.len
= opts
->input
.len
;
513 ALLOC_HASHTABLE(params
);
514 ZEND_INIT_SYMTABLE(params
);
517 while (state
.input
.len
) {
518 if (*state
.input
.str
== '"' && !state
.escape
) {
519 state
.quotes
= !state
.quotes
;
521 state
.escape
= (*state
.input
.str
== '\\');
524 if (!state
.param
.str
) {
526 skip_sep(0, &state
, opts
->param
, opts
->arg
, opts
->val TSRMLS_CC
);
527 state
.param
.str
= state
.input
.str
;
530 /* are we at a param separator? */
531 if (0 < (sep_len
= check_sep(&state
, opts
->param
))) {
532 push_param(params
, &state
, opts TSRMLS_CC
);
534 skip_sep(sep_len
, &state
, opts
->param
, opts
->arg
, opts
->val TSRMLS_CC
);
536 /* start off with a new param */
537 state
.param
.str
= state
.input
.str
;
539 state
.arg
.str
= NULL
;
541 state
.val
.str
= NULL
;
547 /* are we at an arg separator? */
548 if (0 < (sep_len
= check_sep(&state
, opts
->arg
))) {
549 push_param(params
, &state
, opts TSRMLS_CC
);
551 skip_sep(sep_len
, &state
, NULL
, opts
->arg
, opts
->val TSRMLS_CC
);
553 /* continue with a new arg */
554 state
.arg
.str
= state
.input
.str
;
556 state
.val
.str
= NULL
;
562 /* are we at a val separator? */
563 if (0 < (sep_len
= check_sep(&state
, opts
->val
))) {
564 /* only handle separator if we're not already reading in a val */
565 if (!state
.val
.str
) {
566 push_param(params
, &state
, opts TSRMLS_CC
);
568 skip_sep(sep_len
, &state
, NULL
, NULL
, opts
->val TSRMLS_CC
);
570 state
.val
.str
= state
.input
.str
;
578 if (state
.input
.len
) {
584 push_param(params
, &state
, opts TSRMLS_CC
);
589 static inline void shift_key(php_http_buffer_t
*buf
, char *key_str
, size_t key_len
, const char *ass
, size_t asl
, unsigned flags TSRMLS_DC
)
595 php_http_buffer_append(buf
, ass
, asl
);
598 prepare_key(flags
, key_str
, key_len
, &str
, &len TSRMLS_CC
);
599 php_http_buffer_append(buf
, str
, len
);
603 static inline void shift_val(php_http_buffer_t
*buf
, zval
*zvalue
, const char *vss
, size_t vsl
, unsigned flags TSRMLS_DC
)
605 if (Z_TYPE_P(zvalue
) != IS_BOOL
) {
606 zval
*tmp
= php_http_zsep(1, IS_STRING
, zvalue
);
608 prepare_value(flags
, tmp TSRMLS_CC
);
609 php_http_buffer_append(buf
, vss
, vsl
);
610 php_http_buffer_append(buf
, Z_STRVAL_P(tmp
), Z_STRLEN_P(tmp
));
613 } else if (!Z_BVAL_P(zvalue
)) {
614 php_http_buffer_append(buf
, vss
, vsl
);
615 php_http_buffer_appends(buf
, "0");
619 static void shift_arg(php_http_buffer_t
*buf
, char *key_str
, size_t key_len
, zval
*zvalue
, const char *ass
, size_t asl
, const char *vss
, size_t vsl
, unsigned flags TSRMLS_DC
)
621 if (Z_TYPE_P(zvalue
) == IS_ARRAY
|| Z_TYPE_P(zvalue
) == IS_OBJECT
) {
623 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
626 shift_key(buf
, key_str
, key_len
, ass
, asl
, flags TSRMLS_CC
);
627 FOREACH_KEYVAL(pos
, zvalue
, key
, val
) {
628 /* did you mean recursion? */
629 php_http_array_hashkey_stringify(&key
);
630 shift_arg(buf
, key
.str
, key
.len
-1, *val
, ass
, asl
, vss
, vsl
, flags TSRMLS_CC
);
631 php_http_array_hashkey_stringfree(&key
);
634 shift_key(buf
, key_str
, key_len
, ass
, asl
, flags TSRMLS_CC
);
635 shift_val(buf
, zvalue
, vss
, vsl
, flags TSRMLS_CC
);
639 static void shift_param(php_http_buffer_t
*buf
, char *key_str
, size_t key_len
, zval
*zvalue
, const char *pss
, size_t psl
, const char *ass
, size_t asl
, const char *vss
, size_t vsl
, unsigned flags TSRMLS_DC
)
641 if (Z_TYPE_P(zvalue
) == IS_ARRAY
|| Z_TYPE_P(zvalue
) == IS_OBJECT
) {
642 /* treat as arguments, unless we care for dimensions */
643 if (flags
& PHP_HTTP_PARAMS_DIMENSION
) {
644 php_http_buffer_t
*keybuf
= php_http_buffer_from_string(key_str
, key_len
);
645 prepare_dimension(buf
, keybuf
, zvalue
, pss
, psl
, vss
, vsl
, flags TSRMLS_CC
);
646 php_http_buffer_free(&keybuf
);
648 shift_arg(buf
, key_str
, key_len
, zvalue
, ass
, asl
, vss
, vsl
, flags TSRMLS_CC
);
651 shift_key(buf
, key_str
, key_len
, pss
, psl
, flags TSRMLS_CC
);
652 shift_val(buf
, zvalue
, vss
, vsl
, flags TSRMLS_CC
);
656 php_http_buffer_t
*php_http_params_to_string(php_http_buffer_t
*buf
, HashTable
*params
, const char *pss
, size_t psl
, const char *ass
, size_t asl
, const char *vss
, size_t vsl
, unsigned flags TSRMLS_DC
)
659 HashPosition pos
, pos1
;
660 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0), key1
= php_http_array_hashkey_init(0);
663 buf
= php_http_buffer_init(NULL
);
666 FOREACH_HASH_KEYVAL(pos
, params
, key
, zparam
) {
667 zval
**zvalue
, **zargs
;
669 if (Z_TYPE_PP(zparam
) != IS_ARRAY
|| SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(zparam
), ZEND_STRS("value"), (void *) &zvalue
)) {
673 php_http_array_hashkey_stringify(&key
);
674 shift_param(buf
, key
.str
, key
.len
- 1, *zvalue
, pss
, psl
, ass
, asl
, vss
, vsl
, flags TSRMLS_CC
);
675 php_http_array_hashkey_stringfree(&key
);
677 if (Z_TYPE_PP(zparam
) == IS_ARRAY
&& SUCCESS
!= zend_hash_find(Z_ARRVAL_PP(zparam
), ZEND_STRS("arguments"), (void *) &zvalue
)) {
678 if (zvalue
== zparam
) {
684 if (Z_TYPE_PP(zvalue
) == IS_ARRAY
) {
685 FOREACH_KEYVAL(pos1
, *zvalue
, key1
, zargs
) {
686 if (zvalue
== zparam
&& key1
.type
== HASH_KEY_IS_STRING
&& !strcmp(key1
.str
, "value")) {
690 php_http_array_hashkey_stringify(&key1
);
691 shift_arg(buf
, key1
.str
, key1
.len
- 1, *zargs
, ass
, asl
, vss
, vsl
, flags TSRMLS_CC
);
692 php_http_array_hashkey_stringfree(&key1
);
697 php_http_buffer_shrink(buf
);
698 php_http_buffer_fix(buf
);
703 php_http_params_token_t
**php_http_params_separator_init(zval
*zv TSRMLS_DC
)
707 php_http_params_token_t
**ret
, **tmp
;
713 zv
= php_http_ztyp(IS_ARRAY
, zv
);
714 ret
= ecalloc(zend_hash_num_elements(Z_ARRVAL_P(zv
)) + 1, sizeof(*ret
));
717 FOREACH_VAL(pos
, zv
, sep
) {
718 zval
*zt
= php_http_ztyp(IS_STRING
, *sep
);
720 if (Z_STRLEN_P(zt
)) {
721 *tmp
= emalloc(sizeof(**tmp
));
722 (*tmp
)->str
= estrndup(Z_STRVAL_P(zt
), (*tmp
)->len
= Z_STRLEN_P(zt
));
733 void php_http_params_separator_free(php_http_params_token_t
**separator
)
735 php_http_params_token_t
**sep
= separator
;
738 STR_FREE((*sep
)->str
);
746 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams___construct
, 0, 0, 0)
747 ZEND_ARG_INFO(0, params
)
748 ZEND_ARG_INFO(0, param_sep
)
749 ZEND_ARG_INFO(0, arg_sep
)
750 ZEND_ARG_INFO(0, val_sep
)
751 ZEND_ARG_INFO(0, flags
)
753 PHP_METHOD(HttpParams
, __construct
)
755 zval
*zcopy
, *zparams
= NULL
, *param_sep
= NULL
, *arg_sep
= NULL
, *val_sep
= NULL
;
756 long flags
= PHP_HTTP_PARAMS_DEFAULT
;
757 zend_error_handling zeh
;
759 php_http_expect(SUCCESS
== zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "|z!/z/z/z/l", &zparams
, ¶m_sep
, &arg_sep
, &val_sep
, &flags
), invalid_arg
, return);
761 zend_replace_error_handling(EH_THROW
, php_http_exception_runtime_class_entry
, &zeh TSRMLS_CC
);
763 switch (ZEND_NUM_ARGS()) {
765 zend_update_property_long(php_http_params_class_entry
, getThis(), ZEND_STRL("flags"), flags TSRMLS_CC
);
768 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), val_sep TSRMLS_CC
);
771 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), arg_sep TSRMLS_CC
);
774 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), param_sep TSRMLS_CC
);
779 switch (Z_TYPE_P(zparams
)) {
782 zcopy
= php_http_zsep(1, IS_ARRAY
, zparams
);
783 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zcopy TSRMLS_CC
);
784 zval_ptr_dtor(&zcopy
);
787 zcopy
= php_http_ztyp(IS_STRING
, zparams
);
788 if (Z_STRLEN_P(zcopy
)) {
789 php_http_params_opts_t opts
= {
790 {Z_STRVAL_P(zcopy
), Z_STRLEN_P(zcopy
)},
791 php_http_params_separator_init(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC
) TSRMLS_CC
),
792 php_http_params_separator_init(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC
) TSRMLS_CC
),
793 php_http_params_separator_init(zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC
) TSRMLS_CC
),
797 MAKE_STD_ZVAL(zparams
);
799 php_http_params_parse(Z_ARRVAL_P(zparams
), &opts TSRMLS_CC
);
800 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC
);
801 zval_ptr_dtor(&zparams
);
803 php_http_params_separator_free(opts
.param
);
804 php_http_params_separator_free(opts
.arg
);
805 php_http_params_separator_free(opts
.val
);
807 zval_ptr_dtor(&zcopy
);
811 MAKE_STD_ZVAL(zparams
);
813 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC
);
814 zval_ptr_dtor(&zparams
);
817 zend_restore_error_handling(&zeh TSRMLS_CC
);
820 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_toArray
, 0, 0, 0)
822 PHP_METHOD(HttpParams
, toArray
)
826 if (SUCCESS
!= zend_parse_parameters_none()) {
829 zparams
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
);
830 RETURN_ZVAL(zparams
, 1, 0);
833 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_toString
, 0, 0, 0)
835 PHP_METHOD(HttpParams
, toString
)
837 zval
**tmp
, *zparams
, *zpsep
, *zasep
, *zvsep
, *zflags
;
838 php_http_buffer_t buf
;
840 zparams
= php_http_zsep(1, IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
841 zflags
= php_http_ztyp(IS_LONG
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("flags"), 0 TSRMLS_CC
));
843 zpsep
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC
);
844 if (Z_TYPE_P(zpsep
) == IS_ARRAY
&& SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zpsep
), (void *) &tmp
)) {
845 zpsep
= php_http_ztyp(IS_STRING
, *tmp
);
847 zpsep
= php_http_ztyp(IS_STRING
, zpsep
);
849 zasep
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC
);
850 if (Z_TYPE_P(zasep
) == IS_ARRAY
&& SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zasep
), (void *) &tmp
)) {
851 zasep
= php_http_ztyp(IS_STRING
, *tmp
);
853 zasep
= php_http_ztyp(IS_STRING
, zasep
);
855 zvsep
= zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC
);
856 if (Z_TYPE_P(zvsep
) == IS_ARRAY
&& SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zvsep
), (void *) &tmp
)) {
857 zvsep
= php_http_ztyp(IS_STRING
, *tmp
);
859 zvsep
= php_http_ztyp(IS_STRING
, zvsep
);
862 php_http_buffer_init(&buf
);
863 php_http_params_to_string(&buf
, Z_ARRVAL_P(zparams
), Z_STRVAL_P(zpsep
), Z_STRLEN_P(zpsep
), Z_STRVAL_P(zasep
), Z_STRLEN_P(zasep
), Z_STRVAL_P(zvsep
), Z_STRLEN_P(zvsep
), Z_LVAL_P(zflags
) TSRMLS_CC
);
865 zval_ptr_dtor(&zparams
);
866 zval_ptr_dtor(&zpsep
);
867 zval_ptr_dtor(&zasep
);
868 zval_ptr_dtor(&zvsep
);
869 zval_ptr_dtor(&zflags
);
871 RETVAL_PHP_HTTP_BUFFER_VAL(&buf
);
874 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetExists
, 0, 0, 1)
875 ZEND_ARG_INFO(0, name
)
877 PHP_METHOD(HttpParams
, offsetExists
)
881 zval
**zparam
, *zparams
;
883 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &name_str
, &name_len
)) {
887 zparams
= php_http_ztyp(IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
889 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1, (void *) &zparam
)) {
890 RETVAL_BOOL(Z_TYPE_PP(zparam
) != IS_NULL
);
894 zval_ptr_dtor(&zparams
);
897 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetGet
, 0, 0, 1)
898 ZEND_ARG_INFO(0, name
)
900 PHP_METHOD(HttpParams
, offsetGet
)
904 zval
**zparam
, *zparams
;
906 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &name_str
, &name_len
)) {
910 zparams
= php_http_ztyp(IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
912 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1, (void *) &zparam
)) {
913 RETVAL_ZVAL(*zparam
, 1, 0);
916 zval_ptr_dtor(&zparams
);
919 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetUnset
, 0, 0, 1)
920 ZEND_ARG_INFO(0, name
)
922 PHP_METHOD(HttpParams
, offsetUnset
)
928 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "s", &name_str
, &name_len
)) {
932 zparams
= php_http_zsep(1, IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
934 zend_symtable_del(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1);
935 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC
);
937 zval_ptr_dtor(&zparams
);
940 ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams_offsetSet
, 0, 0, 2)
941 ZEND_ARG_INFO(0, name
)
942 ZEND_ARG_INFO(0, value
)
944 PHP_METHOD(HttpParams
, offsetSet
)
949 zval
**zparam
, *zparams
;
951 if (SUCCESS
!= zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC
, "sz", &name_str
, &name_len
, &nvalue
)) {
955 zparams
= php_http_zsep(1, IS_ARRAY
, zend_read_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC
));
958 if (Z_TYPE_P(nvalue
) == IS_ARRAY
) {
961 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1, (void *) &zparam
)) {
962 new_zparam
= php_http_zsep(1, IS_ARRAY
, *zparam
);
963 array_join(Z_ARRVAL_P(nvalue
), Z_ARRVAL_P(new_zparam
), 0, 0);
966 Z_ADDREF_P(new_zparam
);
968 add_assoc_zval_ex(zparams
, name_str
, name_len
+ 1, new_zparam
);
972 if (SUCCESS
== zend_symtable_find(Z_ARRVAL_P(zparams
), name_str
, name_len
+ 1, (void *) &zparam
)) {
973 tmp
= php_http_zsep(1, IS_ARRAY
, *zparam
);
980 add_assoc_zval_ex(tmp
, ZEND_STRS("value"), nvalue
);
981 add_assoc_zval_ex(zparams
, name_str
, name_len
+ 1, tmp
);
984 zval
*tmp
= php_http_ztyp(IS_STRING
, nvalue
), *arr
;
988 add_assoc_bool_ex(arr
, ZEND_STRS("value"), 1);
989 add_assoc_zval_ex(zparams
, Z_STRVAL_P(tmp
), Z_STRLEN_P(tmp
) + 1, arr
);
993 zend_update_property(php_http_params_class_entry
, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC
);
994 zval_ptr_dtor(&zparams
);
997 static zend_function_entry php_http_params_methods
[] = {
998 PHP_ME(HttpParams
, __construct
, ai_HttpParams___construct
, ZEND_ACC_PUBLIC
|ZEND_ACC_CTOR
|ZEND_ACC_FINAL
)
1000 PHP_ME(HttpParams
, toArray
, ai_HttpParams_toArray
, ZEND_ACC_PUBLIC
)
1001 PHP_ME(HttpParams
, toString
, ai_HttpParams_toString
, ZEND_ACC_PUBLIC
)
1002 ZEND_MALIAS(HttpParams
, __toString
, toString
, ai_HttpParams_toString
, ZEND_ACC_PUBLIC
)
1004 PHP_ME(HttpParams
, offsetExists
, ai_HttpParams_offsetExists
, ZEND_ACC_PUBLIC
)
1005 PHP_ME(HttpParams
, offsetUnset
, ai_HttpParams_offsetUnset
, ZEND_ACC_PUBLIC
)
1006 PHP_ME(HttpParams
, offsetSet
, ai_HttpParams_offsetSet
, ZEND_ACC_PUBLIC
)
1007 PHP_ME(HttpParams
, offsetGet
, ai_HttpParams_offsetGet
, ZEND_ACC_PUBLIC
)
1009 EMPTY_FUNCTION_ENTRY
1012 zend_class_entry
*php_http_params_class_entry
;
1014 PHP_MINIT_FUNCTION(http_params
)
1016 zend_class_entry ce
= {0};
1018 INIT_NS_CLASS_ENTRY(ce
, "http", "Params", php_http_params_methods
);
1019 php_http_params_class_entry
= zend_register_internal_class(&ce TSRMLS_CC
);
1020 php_http_params_class_entry
->create_object
= php_http_params_object_new
;
1021 zend_class_implements(php_http_params_class_entry TSRMLS_CC
, 1, zend_ce_arrayaccess
);
1023 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_PARAM_SEP"), ZEND_STRL(",") TSRMLS_CC
);
1024 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_ARG_SEP"), ZEND_STRL(";") TSRMLS_CC
);
1025 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("DEF_VAL_SEP"), ZEND_STRL("=") TSRMLS_CC
);
1026 zend_declare_class_constant_stringl(php_http_params_class_entry
, ZEND_STRL("COOKIE_PARAM_SEP"), ZEND_STRL("") TSRMLS_CC
);
1028 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_RAW"), PHP_HTTP_PARAMS_RAW TSRMLS_CC
);
1029 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC
);
1030 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_URLENCODED"), PHP_HTTP_PARAMS_URLENCODED TSRMLS_CC
);
1031 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_DIMENSION"), PHP_HTTP_PARAMS_DIMENSION TSRMLS_CC
);
1032 zend_declare_class_constant_long(php_http_params_class_entry
, ZEND_STRL("PARSE_QUERY"), PHP_HTTP_PARAMS_QUERY TSRMLS_CC
);
1034 zend_declare_property_null(php_http_params_class_entry
, ZEND_STRL("params"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1035 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("param_sep"), ZEND_STRL(","), ZEND_ACC_PUBLIC TSRMLS_CC
);
1036 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("arg_sep"), ZEND_STRL(";"), ZEND_ACC_PUBLIC TSRMLS_CC
);
1037 zend_declare_property_stringl(php_http_params_class_entry
, ZEND_STRL("val_sep"), ZEND_STRL("="), ZEND_ACC_PUBLIC TSRMLS_CC
);
1038 zend_declare_property_long(php_http_params_class_entry
, ZEND_STRL("flags"), PHP_HTTP_PARAMS_DEFAULT
, ZEND_ACC_PUBLIC TSRMLS_CC
);
1048 * vim600: noet sw=4 ts=4 fdm=marker
1049 * vim<600: noet sw=4 ts=4