76313a0f7346ecbfdffe67b8f43e1efab945c94b
[m6w6/ext-http] / php_http_params.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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14
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 = {
19 .param = def_param_sep_ptr,
20 .arg = def_arg_sep_ptr,
21 .val = def_val_sep_ptr,
22 .defval = NULL,
23 .flags = PHP_HTTP_PARAMS_DEFAULT
24 };
25
26 PHP_HTTP_API php_http_params_opts_t *php_http_params_opts_default_get(php_http_params_opts_t *opts)
27 {
28 if (!opts) {
29 opts = emalloc(sizeof(*opts));
30 }
31
32 memcpy(opts, &def_opts, sizeof(def_opts));
33
34 return opts;
35 }
36
37 typedef struct php_http_params_state {
38 php_http_params_token_t input;
39 php_http_params_token_t param;
40 php_http_params_token_t arg;
41 php_http_params_token_t val;
42 struct {
43 zval **param;
44 zval **args;
45 zval **val;
46 } current;
47 } php_http_params_state_t;
48
49 static void sanitize_default(zval *zv TSRMLS_DC)
50 {
51 if (Z_STRVAL_P(zv)[0] == '"' && Z_STRVAL_P(zv)[Z_STRLEN_P(zv) - 1] == '"') {
52 size_t deq_len = Z_STRLEN_P(zv) - 2;
53 char *deq = estrndup(Z_STRVAL_P(zv) + 1, deq_len);
54
55 zval_dtor(zv);
56 ZVAL_STRINGL(zv, deq, deq_len, 0);
57 }
58
59 php_stripslashes(Z_STRVAL_P(zv), &Z_STRLEN_P(zv) TSRMLS_CC);
60 }
61
62 static void prepare_default(zval *zv TSRMLS_DC)
63 {
64 int len = Z_STRLEN_P(zv);
65
66 Z_STRVAL_P(zv) = php_addslashes(Z_STRVAL_P(zv), Z_STRLEN_P(zv), &Z_STRLEN_P(zv), 1 TSRMLS_CC);
67
68 if (len != Z_STRLEN_P(zv)) {
69 zval tmp = *zv;
70 int len = Z_STRLEN_P(zv) + 2;
71 char *str = emalloc(len + 1);
72
73 str[0] = '"';
74 memcpy(&str[1], Z_STRVAL_P(zv), Z_STRLEN_P(zv));
75 str[len-1] = '"';
76 str[len] = '\0';
77
78 zval_dtor(&tmp);
79 ZVAL_STRINGL(zv, str, len, 0);
80 }
81 }
82
83 static void sanitize_urlencoded(zval *zv TSRMLS_DC)
84 {
85 Z_STRLEN_P(zv) = php_raw_url_decode(Z_STRVAL_P(zv), Z_STRLEN_P(zv));
86 }
87
88 static void prepare_urlencoded(zval *zv TSRMLS_DC)
89 {
90 int len;
91 char *str = php_raw_url_encode(Z_STRVAL_P(zv), Z_STRLEN_P(zv), &len);
92
93 zval_dtor(zv);
94 ZVAL_STRINGL(zv, str, len, 0);
95 }
96
97 static void sanitize_dimension(zval *zv TSRMLS_DC)
98 {
99 zval *arr = NULL, *tmp = NULL, **cur = NULL;
100 char *var = NULL, *ptr = Z_STRVAL_P(zv), *end = Z_STRVAL_P(zv) + Z_STRLEN_P(zv);
101 long level = 0;
102
103 MAKE_STD_ZVAL(arr);
104 array_init(arr);
105 cur = &arr;
106
107 while (ptr < end) {
108 if (!var) {
109 var = ptr;
110 }
111
112 switch (*ptr) {
113 case '[':
114 if (++level > PG(max_input_nesting_level)) {
115 zval_ptr_dtor(&arr);
116 php_http_error(HE_WARNING, PHP_HTTP_E_QUERYSTRING, "Max input nesting level of %ld exceeded", PG(max_input_nesting_level));
117 return;
118 }
119 if (ptr - var == 0) {
120 ++var;
121 break;
122 }
123 /* no break */
124
125 case ']':
126
127 MAKE_STD_ZVAL(tmp);
128 ZVAL_NULL(tmp);
129 convert_to_array(*cur);
130
131 if (ptr - var) {
132 char chr = *ptr;
133 *ptr = '\0';
134 zend_symtable_update(Z_ARRVAL_PP(cur), var, ptr - var + 1, (void *) &tmp, sizeof(zval *), (void *) &cur);
135 *ptr = chr;
136 } else {
137 zend_hash_next_index_insert(Z_ARRVAL_PP(cur), (void *) &tmp, sizeof(zval *), (void *) &cur);
138 }
139
140 var = NULL;
141 break;
142 }
143
144 ++ptr;
145 }
146
147 if (zend_hash_num_elements(Z_ARRVAL_P(arr))) {
148 zval_dtor(zv);
149 ZVAL_COPY_VALUE(zv, arr);
150 FREE_ZVAL(arr);
151 } else {
152 zval_ptr_dtor(&arr);
153 }
154 }
155
156 static void prepare_dimension(zval *zv TSRMLS_DC)
157 {
158 if (Z_TYPE_P(zv) == IS_ARRAY) {
159 zval **zdata = &zv;
160 php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
161 php_http_buffer_t buf;
162
163 php_http_buffer_init(&buf);
164
165 do {
166 if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(Z_ARRVAL_PP(zdata), &key.str, &key.len, &key.num, key.dup, NULL)) {
167 php_http_buffer_appendf(&buf, "[%s]", key.str);
168 } else {
169 php_http_buffer_appendf(&buf, "[%lu]", key.num);
170 }
171 } while (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(zdata), (void *) &zdata) && Z_TYPE_PP(zdata) == IS_ARRAY);
172
173 php_http_buffer_fix(&buf);
174 zval_dtor(zv);
175 ZVAL_STRINGL(zv, buf.data, buf.used, 0);
176 }
177 }
178
179 static void sanitize_key(unsigned flags, char *str, size_t len, zval *zv TSRMLS_DC)
180 {
181 php_trim(str, len, NULL, 0, zv, 3 TSRMLS_CC);
182
183 if (flags & PHP_HTTP_PARAMS_DEFAULT) {
184 sanitize_default(zv TSRMLS_CC);
185 }
186
187 if (flags & PHP_HTTP_PARAMS_URLENCODED) {
188 sanitize_urlencoded(zv TSRMLS_CC);
189 }
190
191 if (flags & PHP_HTTP_PARAMS_DIMENSION) {
192 sanitize_dimension(zv TSRMLS_CC);
193 }
194 }
195
196 static void sanitize_value(unsigned flags, char *str, size_t len, zval *zv TSRMLS_DC)
197 {
198 php_trim(str, len, NULL, 0, zv, 3 TSRMLS_CC);
199
200 if (flags & PHP_HTTP_PARAMS_DEFAULT) {
201 sanitize_default(zv TSRMLS_CC);
202 }
203
204 if (flags & PHP_HTTP_PARAMS_URLENCODED) {
205 sanitize_urlencoded(zv TSRMLS_CC);
206 }
207 }
208
209 static void prepare_key(unsigned flags, char *old_key, size_t old_len, char **new_key, size_t *new_len TSRMLS_DC)
210 {
211 zval zv;
212
213 INIT_PZVAL(&zv);
214 ZVAL_STRINGL(&zv, old_key, old_len, 1);
215
216 if (flags & PHP_HTTP_PARAMS_DIMENSION) {
217 prepare_dimension(&zv TSRMLS_CC);
218 }
219
220 if (flags & PHP_HTTP_PARAMS_URLENCODED) {
221 prepare_urlencoded(&zv TSRMLS_CC);
222 }
223
224 if (flags & PHP_HTTP_PARAMS_DEFAULT) {
225 prepare_default(&zv TSRMLS_CC);
226 }
227
228 *new_key = Z_STRVAL(zv);
229 *new_len = Z_STRLEN(zv);
230 }
231
232 static void prepare_value(unsigned flags, zval *zv TSRMLS_DC)
233 {
234 if (flags & PHP_HTTP_PARAMS_DIMENSION) {
235 prepare_dimension(zv TSRMLS_CC);
236 }
237
238 if (flags & PHP_HTTP_PARAMS_URLENCODED) {
239 prepare_urlencoded(zv TSRMLS_CC);
240 }
241
242 if (flags & PHP_HTTP_PARAMS_DEFAULT) {
243 prepare_default(zv TSRMLS_CC);
244 }
245 }
246
247 static void merge_param(HashTable *params, zval *zdata, zval ***current_param, zval ***current_args TSRMLS_DC)
248 {
249 zval **ptr, **zdata_ptr;
250 php_http_array_hashkey_t hkey = php_http_array_hashkey_init(0);
251
252 #if 0
253 {
254 zval tmp;
255 INIT_PZVAL_ARRAY(&tmp, params);
256 fprintf(stderr, "params = ");
257 zend_print_zval_r(&tmp, 1 TSRMLS_CC);
258 fprintf(stderr, "\n");
259 }
260 #endif
261
262 hkey.type = zend_hash_get_current_key_ex(Z_ARRVAL_P(zdata), &hkey.str, &hkey.len, &hkey.num, hkey.dup, NULL);
263
264 if ((hkey.type == HASH_KEY_IS_STRING && !zend_hash_exists(params, hkey.str, hkey.len))
265 || (hkey.type == HASH_KEY_IS_LONG && !zend_hash_index_exists(params, hkey.num))
266 ) {
267 zval *tmp, *arg, **args;
268
269 /* create the entry if it doesn't exist */
270 zend_hash_get_current_data(Z_ARRVAL_P(zdata), (void *) &ptr);
271 Z_ADDREF_PP(ptr);
272 MAKE_STD_ZVAL(tmp);
273 array_init(tmp);
274 add_assoc_zval_ex(tmp, ZEND_STRS("value"), *ptr);
275
276 MAKE_STD_ZVAL(arg);
277 array_init(arg);
278 zend_hash_update(Z_ARRVAL_P(tmp), "arguments", sizeof("arguments"), (void *) &arg, sizeof(zval *), (void *) &args);
279 *current_args = args;
280
281 if (hkey.type == HASH_KEY_IS_STRING) {
282 zend_hash_update(params, hkey.str, hkey.len, (void *) &tmp, sizeof(zval *), (void *) &ptr);
283 } else {
284 zend_hash_index_update(params, hkey.num, (void *) &tmp, sizeof(zval *), (void *) &ptr);
285 }
286 } else {
287 /* merge */
288 if (hkey.type == HASH_KEY_IS_STRING) {
289 zend_hash_find(params, hkey.str, hkey.len, (void *) &ptr);
290 } else {
291 zend_hash_index_find(params, hkey.num, (void *) &ptr);
292 }
293
294 zdata_ptr = &zdata;
295
296 if (Z_TYPE_PP(ptr) == IS_ARRAY
297 && SUCCESS == zend_hash_find(Z_ARRVAL_PP(ptr), "value", sizeof("value"), (void *) &ptr)
298 && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(zdata_ptr), (void *) &zdata_ptr)
299 ) {
300 /*
301 * params = [arr => [value => [0 => 1]]]
302 * ^- ptr
303 * zdata = [arr => [0 => NULL]]
304 * ^- zdata_ptr
305 */
306 zval **test_ptr;
307
308 while (Z_TYPE_PP(zdata_ptr) == IS_ARRAY
309 && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(zdata_ptr), (void *) &test_ptr)
310 ) {
311 if (Z_TYPE_PP(test_ptr) == IS_ARRAY) {
312
313 /* now find key in ptr */
314 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)) {
315 if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(ptr), hkey.str, hkey.len, (void *) &ptr)) {
316 zdata_ptr = test_ptr;
317 } else {
318 Z_ADDREF_PP(test_ptr);
319 zend_hash_update(Z_ARRVAL_PP(ptr), hkey.str, hkey.len, (void *) test_ptr, sizeof(zval *), (void *) &ptr);
320 break;
321 }
322 } else {
323 if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(ptr), hkey.str, hkey.len, (void *) &ptr)) {
324 zdata_ptr = test_ptr;
325 } else if (hkey.num) {
326 Z_ADDREF_PP(test_ptr);
327 zend_hash_index_update(Z_ARRVAL_PP(ptr), hkey.num, (void *) test_ptr, sizeof(zval *), (void *) &ptr);
328 break;
329 } else {
330 Z_ADDREF_PP(test_ptr);
331 zend_hash_next_index_insert(Z_ARRVAL_PP(ptr), (void *) test_ptr, sizeof(zval *), (void *) &ptr);
332 break;
333 }
334 }
335 } else {
336 /* this is the leaf */
337 Z_ADDREF_PP(test_ptr);
338 if (Z_TYPE_PP(ptr) != IS_ARRAY) {
339 zval_dtor(*ptr);
340 array_init(*ptr);
341 }
342 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)) {
343 zend_hash_update(Z_ARRVAL_PP(ptr), hkey.str, hkey.len, (void *) test_ptr, sizeof(zval *), (void *) &ptr);
344 } else if (hkey.num) {
345 zend_hash_index_update(Z_ARRVAL_PP(ptr), hkey.num, (void *) test_ptr, sizeof(zval *), (void *) &ptr);
346 } else {
347 zend_hash_next_index_insert(Z_ARRVAL_PP(ptr), (void *) test_ptr, sizeof(zval *), (void *) &ptr);
348 }
349 break;
350 }
351 }
352
353 }
354 }
355
356 /* bubble up */
357 while (Z_TYPE_PP(ptr) == IS_ARRAY && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(ptr), (void *) &ptr));
358 *current_param = ptr;
359 }
360
361 static void push_param(HashTable *params, php_http_params_state_t *state, const php_http_params_opts_t *opts TSRMLS_DC)
362 {
363 if (state->val.str) {
364 if (0 < (state->val.len = state->input.str - state->val.str)) {
365 sanitize_value(opts->flags, state->val.str, state->val.len, *(state->current.val) TSRMLS_CC);
366 }
367 } else if (state->arg.str) {
368 if (0 < (state->arg.len = state->input.str - state->arg.str)) {
369 zval *val, key;
370
371 INIT_PZVAL(&key);
372 sanitize_key(opts->flags, state->arg.str, state->arg.len, &key TSRMLS_CC);
373 if (Z_STRLEN(key)) {
374 MAKE_STD_ZVAL(val);
375 ZVAL_TRUE(val);
376 zend_symtable_update(Z_ARRVAL_PP(state->current.args), Z_STRVAL(key), Z_STRLEN(key) + 1, (void *) &val, sizeof(zval *), (void *) &state->current.val);
377 }
378 zval_dtor(&key);
379 }
380 } else if (state->param.str) {
381 if (0 < (state->param.len = state->input.str - state->param.str)) {
382 zval *prm, *arg, *val, *key;
383
384 MAKE_STD_ZVAL(key);
385 ZVAL_NULL(key);
386 sanitize_key(opts->flags, state->param.str, state->param.len, key TSRMLS_CC);
387 if (Z_TYPE_P(key) != IS_STRING) {
388 merge_param(params, key, &state->current.val, &state->current.args TSRMLS_CC);
389 } else if (Z_STRLEN_P(key)) {
390 MAKE_STD_ZVAL(prm);
391 array_init(prm);
392
393 MAKE_STD_ZVAL(val);
394 if (opts->defval) {
395 ZVAL_COPY_VALUE(val, opts->defval);
396 zval_copy_ctor(val);
397 } else {
398 ZVAL_TRUE(val);
399 }
400 zend_hash_update(Z_ARRVAL_P(prm), "value", sizeof("value"), (void *) &val, sizeof(zval *), (void *) &state->current.val);
401
402 MAKE_STD_ZVAL(arg);
403 array_init(arg);
404 zend_hash_update(Z_ARRVAL_P(prm), "arguments", sizeof("arguments"), (void *) &arg, sizeof(zval *), (void *) &state->current.args);
405
406 zend_symtable_update(params, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1, (void *) &prm, sizeof(zval *), (void *) &state->current.param);
407 }
408 zval_ptr_dtor(&key);
409 }
410 }
411 }
412
413 static inline zend_bool check_str(const char *chk_str, size_t chk_len, const char *sep_str, size_t sep_len) {
414 return 0 < sep_len && chk_len >= sep_len && !memcmp(chk_str, sep_str, sep_len);
415 }
416
417 static size_t check_sep(php_http_params_state_t *state, php_http_params_token_t **separators)
418 {
419 php_http_params_token_t **sep = separators;
420
421 if (sep) while (*sep) {
422 if (check_str(state->input.str, state->input.len, (*sep)->str, (*sep)->len)) {
423 return (*sep)->len;
424 }
425 ++sep;
426 }
427 return 0;
428 }
429
430 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)
431 {
432 size_t sep_len;
433
434 state->input.str += skip;
435 state->input.len -= skip;
436
437 while ( (param && (sep_len = check_sep(state, param)))
438 || (arg && (sep_len = check_sep(state, arg)))
439 || (val && (sep_len = check_sep(state, val)))
440 ) {
441 state->input.str += sep_len;
442 state->input.len -= sep_len;
443 }
444 }
445
446 PHP_HTTP_API HashTable *php_http_params_parse(HashTable *params, const php_http_params_opts_t *opts TSRMLS_DC)
447 {
448 php_http_params_state_t state = {{NULL,0}, {NULL,0}, {NULL,0}, {NULL,0}, {NULL,NULL,NULL}};
449
450 state.input.str = opts->input.str;
451 state.input.len = opts->input.len;
452
453 if (!params) {
454 ALLOC_HASHTABLE(params);
455 ZEND_INIT_SYMTABLE(params);
456 }
457
458 while (state.input.len) {
459 if (*state.input.str == '\\') {
460 ++state.input.str;
461 --state.input.len;
462 } else if (!state.param.str) {
463 /* initialize */
464 skip_sep(0, &state, opts->param, opts->arg, opts->val TSRMLS_CC);
465 state.param.str = state.input.str;
466 } else {
467 size_t sep_len;
468 /* are we at a param separator? */
469 if (0 < (sep_len = check_sep(&state, opts->param))) {
470 push_param(params, &state, opts TSRMLS_CC);
471
472 skip_sep(sep_len, &state, opts->param, opts->arg, opts->val TSRMLS_CC);
473
474 /* start off with a new param */
475 state.param.str = state.input.str;
476 state.param.len = 0;
477 state.arg.str = NULL;
478 state.arg.len = 0;
479 state.val.str = NULL;
480 state.val.len = 0;
481
482 continue;
483
484 } else
485 /* are we at an arg separator? */
486 if (0 < (sep_len = check_sep(&state, opts->arg))) {
487 push_param(params, &state, opts TSRMLS_CC);
488
489 skip_sep(sep_len, &state, NULL, opts->arg, opts->val TSRMLS_CC);
490
491 /* continue with a new arg */
492 state.arg.str = state.input.str;
493 state.arg.len = 0;
494 state.val.str = NULL;
495 state.val.len = 0;
496
497 continue;
498
499 } else
500 /* are we at a val separator? */
501 if (0 < (sep_len = check_sep(&state, opts->val))) {
502 /* only handle separator if we're not already reading in a val */
503 if (!state.val.str) {
504 push_param(params, &state, opts TSRMLS_CC);
505
506 skip_sep(sep_len, &state, NULL, NULL, opts->val TSRMLS_CC);
507
508 state.val.str = state.input.str;
509 state.val.len = 0;
510
511 continue;
512 }
513 }
514 }
515
516 if (state.input.len) {
517 ++state.input.str;
518 --state.input.len;
519 }
520 }
521 /* finalize */
522 push_param(params, &state, opts TSRMLS_CC);
523
524 return params;
525 }
526
527 static void shift_param(php_http_buffer_t *buf, char *key_str, size_t key_len, zval **zvalue, const char *css, size_t csl, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
528 {
529 if (Z_TYPE_PP(zvalue) == IS_ARRAY) {
530 zval *tmp = php_http_zsep(1, IS_ARRAY, *zvalue);
531
532 do {
533 char *str;
534 size_t len;
535 zval *tmp2;
536
537 if (PHP_HTTP_BUFFER_LEN(buf)) {
538 php_http_buffer_append(buf, css, csl);
539 }
540
541 prepare_key(flags, key_str, key_len, &str, &len TSRMLS_CC);
542 php_http_buffer_append(buf, str, len);
543 efree(str);
544
545 tmp2 = php_http_zsep(1, IS_ARRAY, tmp);
546 prepare_value(flags, tmp2 TSRMLS_CC);
547 php_http_buffer_append(buf, Z_STRVAL_P(tmp2), Z_STRLEN_P(tmp2));
548 zval_ptr_dtor(&tmp2);
549
550 zvalue = &tmp;
551 while (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(zvalue), (void *) &zvalue) && Z_TYPE_PP(zvalue) == IS_ARRAY);
552
553 if (Z_TYPE_PP(zvalue) != IS_BOOL) {
554 php_http_buffer_append(buf, vss, vsl);
555
556 tmp2 = php_http_ztyp(IS_STRING, *zvalue);
557 prepare_value(flags, tmp2 TSRMLS_CC);
558 php_http_buffer_append(buf, Z_STRVAL_P(tmp2), Z_STRLEN_P(tmp2));
559 zval_ptr_dtor(&tmp2);
560 } else if (!Z_BVAL_PP(zvalue)) {
561 php_http_buffer_append(buf, vss, vsl);
562 php_http_buffer_appends(buf, "0");
563 }
564
565 } while (SUCCESS == zend_hash_move_forward(Z_ARRVAL_P(tmp)) && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(tmp), (void *) &zvalue));
566
567 zval_ptr_dtor(&tmp);
568
569 } else {
570 zval *tmp;
571 char *str;
572 size_t len;
573
574 if (PHP_HTTP_BUFFER_LEN(buf)) {
575 php_http_buffer_append(buf, css, csl);
576 }
577
578 prepare_key(flags, key_str, key_len, &str, &len TSRMLS_CC);
579 php_http_buffer_append(buf, str, len);
580 efree(str);
581
582 if (Z_TYPE_PP(zvalue) != IS_BOOL) {
583 tmp = php_http_ztyp(IS_STRING, *zvalue);
584 prepare_value(flags, tmp TSRMLS_CC);
585 php_http_buffer_append(buf, vss, vsl);
586 php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
587 zval_ptr_dtor(&tmp);
588 } else if (!Z_BVAL_PP(zvalue)) {
589 php_http_buffer_append(buf, vss, vsl);
590 php_http_buffer_appends(buf, "0");
591 }
592 }
593 }
594
595 PHP_HTTP_API 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)
596 {
597 zval **zparam;
598 HashPosition pos, pos1;
599 php_http_array_hashkey_t key = php_http_array_hashkey_init(0), key1 = php_http_array_hashkey_init(0);
600
601 if (!buf) {
602 buf = php_http_buffer_init(NULL);
603 }
604
605 FOREACH_HASH_KEYVAL(pos, params, key, zparam) {
606 zval **zvalue, **zargs;
607
608 if (Z_TYPE_PP(zparam) != IS_ARRAY || SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("value"), (void *) &zvalue)) {
609 zvalue = zparam;
610 }
611
612 php_http_array_hashkey_stringify(&key);
613 shift_param(buf, key.str, key.len - 1, zvalue, pss, psl, vss, vsl, flags TSRMLS_CC);
614 php_http_array_hashkey_stringfree(&key);
615
616 if (Z_TYPE_PP(zparam) == IS_ARRAY && SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("arguments"), (void *) &zvalue)) {
617 if (zvalue == zparam) {
618 continue;
619 }
620 zvalue = zparam;
621 }
622
623 if (Z_TYPE_PP(zvalue) == IS_ARRAY) {
624 FOREACH_KEYVAL(pos1, *zvalue, key1, zargs) {
625 if (zvalue == zparam && key1.type == HASH_KEY_IS_STRING && !strcmp(key1.str, "value")) {
626 continue;
627 }
628
629 php_http_array_hashkey_stringify(&key1);
630 shift_param(buf, key1.str, key1.len - 1, zargs, ass, asl, vss, vsl, flags TSRMLS_CC);
631 php_http_array_hashkey_stringfree(&key1);
632 }
633 }
634 }
635
636 php_http_buffer_shrink(buf);
637 php_http_buffer_fix(buf);
638
639 return buf;
640 }
641
642 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpParams, method, 0, req_args)
643 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpParams, method, 0)
644 #define PHP_HTTP_PARAMS_ME(method, visibility) PHP_ME(HttpParams, method, PHP_HTTP_ARGS(HttpParams, method), visibility)
645 #define PHP_HTTP_PARAMS_GME(method, visibility) PHP_ME(HttpParams, method, PHP_HTTP_ARGS(HttpParams, __getter), visibility)
646
647 PHP_HTTP_BEGIN_ARGS(__construct, 0)
648 PHP_HTTP_ARG_VAL(params, 0)
649 PHP_HTTP_ARG_VAL(param_sep, 0)
650 PHP_HTTP_ARG_VAL(arg_sep, 0)
651 PHP_HTTP_ARG_VAL(val_sep, 0)
652 PHP_HTTP_ARG_VAL(flags, 0)
653 PHP_HTTP_END_ARGS;
654
655 PHP_HTTP_EMPTY_ARGS(toArray);
656 PHP_HTTP_EMPTY_ARGS(toString);
657
658 PHP_HTTP_BEGIN_ARGS(offsetExists, 1)
659 PHP_HTTP_ARG_VAL(name, 0)
660 PHP_HTTP_END_ARGS;
661
662 PHP_HTTP_BEGIN_ARGS(offsetUnset, 1)
663 PHP_HTTP_ARG_VAL(name, 0)
664 PHP_HTTP_END_ARGS;
665
666 PHP_HTTP_BEGIN_ARGS(offsetGet, 1)
667 PHP_HTTP_ARG_VAL(name, 0)
668 PHP_HTTP_END_ARGS;
669
670 PHP_HTTP_BEGIN_ARGS(offsetSet, 2)
671 PHP_HTTP_ARG_VAL(name, 0)
672 PHP_HTTP_ARG_VAL(value, 0)
673 PHP_HTTP_END_ARGS;
674
675 static zend_class_entry *php_http_params_class_entry;
676
677 zend_class_entry *php_http_params_get_class_entry(void)
678 {
679 return php_http_params_class_entry;
680 }
681
682 static zend_function_entry php_http_params_method_entry[] = {
683 PHP_HTTP_PARAMS_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL)
684
685 PHP_HTTP_PARAMS_ME(toArray, ZEND_ACC_PUBLIC)
686 PHP_HTTP_PARAMS_ME(toString, ZEND_ACC_PUBLIC)
687 ZEND_MALIAS(HttpParams, __toString, toString, PHP_HTTP_ARGS(HttpParams, toString), ZEND_ACC_PUBLIC)
688
689 PHP_HTTP_PARAMS_ME(offsetExists, ZEND_ACC_PUBLIC)
690 PHP_HTTP_PARAMS_ME(offsetUnset, ZEND_ACC_PUBLIC)
691 PHP_HTTP_PARAMS_ME(offsetSet, ZEND_ACC_PUBLIC)
692 PHP_HTTP_PARAMS_ME(offsetGet, ZEND_ACC_PUBLIC)
693
694 EMPTY_FUNCTION_ENTRY
695 };
696
697 PHP_MINIT_FUNCTION(http_params)
698 {
699 PHP_HTTP_REGISTER_CLASS(http, Params, http_params, php_http_object_get_class_entry(), 0);
700
701 zend_class_implements(php_http_params_class_entry TSRMLS_CC, 1, zend_ce_arrayaccess);
702
703 zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_PARAM_SEP"), ZEND_STRL(",") TSRMLS_CC);
704 zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_ARG_SEP"), ZEND_STRL(";") TSRMLS_CC);
705 zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("DEF_VAL_SEP"), ZEND_STRL("=") TSRMLS_CC);
706 zend_declare_class_constant_stringl(php_http_params_class_entry, ZEND_STRL("COOKIE_PARAM_SEP"), ZEND_STRL("") TSRMLS_CC);
707
708 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_RAW"), PHP_HTTP_PARAMS_RAW TSRMLS_CC);
709 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_DEFAULT"), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC);
710 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_URLENCODED"), PHP_HTTP_PARAMS_URLENCODED TSRMLS_CC);
711 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_DIMENSION"), PHP_HTTP_PARAMS_DIMENSION TSRMLS_CC);
712 zend_declare_class_constant_long(php_http_params_class_entry, ZEND_STRL("PARSE_QUERY"), PHP_HTTP_PARAMS_QUERY TSRMLS_CC);
713
714 zend_declare_property_null(php_http_params_class_entry, ZEND_STRL("params"), ZEND_ACC_PUBLIC TSRMLS_CC);
715 zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("param_sep"), ZEND_STRL(","), ZEND_ACC_PUBLIC TSRMLS_CC);
716 zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("arg_sep"), ZEND_STRL(";"), ZEND_ACC_PUBLIC TSRMLS_CC);
717 zend_declare_property_stringl(php_http_params_class_entry, ZEND_STRL("val_sep"), ZEND_STRL("="), ZEND_ACC_PUBLIC TSRMLS_CC);
718 zend_declare_property_long(php_http_params_class_entry, ZEND_STRL("flags"), PHP_HTTP_PARAMS_DEFAULT, ZEND_ACC_PUBLIC TSRMLS_CC);
719
720 return SUCCESS;
721 }
722
723 PHP_HTTP_API php_http_params_token_t **php_http_params_separator_init(zval *zv TSRMLS_DC)
724 {
725 zval **sep;
726 HashPosition pos;
727 php_http_params_token_t **ret, **tmp;
728
729 if (!zv) {
730 return NULL;
731 }
732
733 zv = php_http_ztyp(IS_ARRAY, zv);
734 ret = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(zv)) + 1, sizeof(*ret));
735
736 tmp = ret;
737 FOREACH_VAL(pos, zv, sep) {
738 zval *zt = php_http_ztyp(IS_STRING, *sep);
739
740 if (Z_STRLEN_P(zt)) {
741 *tmp = emalloc(sizeof(**tmp));
742 (*tmp)->str = estrndup(Z_STRVAL_P(zt), (*tmp)->len = Z_STRLEN_P(zt));
743 ++tmp;
744 }
745 zval_ptr_dtor(&zt);
746 }
747 zval_ptr_dtor(&zv);
748
749 *tmp = NULL;
750 return ret;
751 }
752
753 PHP_HTTP_API void php_http_params_separator_free(php_http_params_token_t **separator)
754 {
755 php_http_params_token_t **sep = separator;
756 if (sep) {
757 while (*sep) {
758 STR_FREE((*sep)->str);
759 efree(*sep);
760 ++sep;
761 }
762 efree(separator);
763 }
764 }
765
766 PHP_METHOD(HttpParams, __construct)
767 {
768 with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
769 zval *zcopy, *zparams = NULL, *param_sep = NULL, *arg_sep = NULL, *val_sep = NULL;
770 long flags = PHP_HTTP_PARAMS_DEFAULT;
771
772 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!/z/z/z/l", &zparams, &param_sep, &arg_sep, &val_sep, &flags)) {
773 switch (ZEND_NUM_ARGS()) {
774 case 5:
775 zend_update_property_long(php_http_params_class_entry, getThis(), ZEND_STRL("flags"), flags TSRMLS_CC);
776 /* no break */
777 case 4:
778 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), val_sep TSRMLS_CC);
779 /* no break */
780 case 3:
781 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), arg_sep TSRMLS_CC);
782 /* no break */
783 case 2:
784 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), param_sep TSRMLS_CC);
785 /* no break */
786 }
787
788 if (zparams) {
789 switch (Z_TYPE_P(zparams)) {
790 case IS_OBJECT:
791 case IS_ARRAY:
792 zcopy = php_http_zsep(1, IS_ARRAY, zparams);
793 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zcopy TSRMLS_CC);
794 zval_ptr_dtor(&zcopy);
795 break;
796 default:
797 zcopy = php_http_ztyp(IS_STRING, zparams);
798 if (Z_STRLEN_P(zcopy)) {
799 php_http_params_opts_t opts = {
800 .input = {
801 .str = Z_STRVAL_P(zcopy),
802 .len = Z_STRLEN_P(zcopy)
803 },
804 .param = php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC) TSRMLS_CC),
805 .arg = php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC) TSRMLS_CC),
806 .val = php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC) TSRMLS_CC),
807 .flags = flags
808 };
809
810 MAKE_STD_ZVAL(zparams);
811 array_init(zparams);
812 php_http_params_parse(Z_ARRVAL_P(zparams), &opts TSRMLS_CC);
813 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC);
814 zval_ptr_dtor(&zparams);
815
816 php_http_params_separator_free(opts.param);
817 php_http_params_separator_free(opts.arg);
818 php_http_params_separator_free(opts.val);
819 }
820 zval_ptr_dtor(&zcopy);
821 break;
822 }
823 } else {
824 MAKE_STD_ZVAL(zparams);
825 array_init(zparams);
826 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC);
827 zval_ptr_dtor(&zparams);
828 }
829 }
830 } end_error_handling();
831 }
832
833 PHP_METHOD(HttpParams, toArray)
834 {
835 if (SUCCESS != zend_parse_parameters_none()) {
836 RETURN_FALSE;
837 }
838 RETURN_PROP(php_http_params_class_entry, "params");
839 }
840
841 PHP_METHOD(HttpParams, toString)
842 {
843 zval **tmp, *zparams, *zpsep, *zasep, *zvsep, *zflags;
844 php_http_buffer_t buf;
845
846 zparams = php_http_ztyp(IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC));
847 zflags = php_http_ztyp(IS_LONG, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("flags"), 0 TSRMLS_CC));
848 zpsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC);
849 zasep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC);
850 zvsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC);
851
852 zpsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC);
853 if (Z_TYPE_P(zpsep) == IS_ARRAY && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zpsep), (void *) &tmp)) {
854 zpsep = php_http_ztyp(IS_STRING, *tmp);
855 } else {
856 zpsep = php_http_ztyp(IS_STRING, zpsep);
857 }
858 zasep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC);
859 if (Z_TYPE_P(zasep) == IS_ARRAY && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zasep), (void *) &tmp)) {
860 zasep = php_http_ztyp(IS_STRING, *tmp);
861 } else {
862 zasep = php_http_ztyp(IS_STRING, zasep);
863 }
864 zvsep = zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC);
865 if (Z_TYPE_P(zvsep) == IS_ARRAY && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zvsep), (void *) &tmp)) {
866 zvsep = php_http_ztyp(IS_STRING, *tmp);
867 } else {
868 zvsep = php_http_ztyp(IS_STRING, zvsep);
869 }
870
871 php_http_buffer_init(&buf);
872 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);
873
874 zval_ptr_dtor(&zparams);
875 zval_ptr_dtor(&zpsep);
876 zval_ptr_dtor(&zasep);
877 zval_ptr_dtor(&zvsep);
878 zval_ptr_dtor(&zflags);
879
880 RETVAL_PHP_HTTP_BUFFER_VAL(&buf);
881 }
882
883 PHP_METHOD(HttpParams, offsetExists)
884 {
885 char *name_str;
886 int name_len;
887
888 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name_str, &name_len)) {
889 zval **zparam, *zparams = php_http_ztyp(IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC));
890
891 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(zparams), name_str, name_len + 1, (void *) &zparam)) {
892 RETVAL_BOOL(Z_TYPE_PP(zparam) != IS_NULL);
893 } else {
894 RETVAL_FALSE;
895 }
896 zval_ptr_dtor(&zparams);
897 }
898 }
899
900 PHP_METHOD(HttpParams, offsetGet)
901 {
902 char *name_str;
903 int name_len;
904
905 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name_str, &name_len)) {
906 zval **zparam, *zparams = php_http_ztyp(IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC));
907
908 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(zparams), name_str, name_len + 1, (void *) &zparam)) {
909 RETVAL_ZVAL(*zparam, 1, 0);
910 }
911
912 zval_ptr_dtor(&zparams);
913 }
914 }
915
916
917 PHP_METHOD(HttpParams, offsetUnset)
918 {
919 char *name_str;
920 int name_len;
921
922 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name_str, &name_len)) {
923 zval *zparams = php_http_zsep(1, IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC));
924
925 zend_symtable_del(Z_ARRVAL_P(zparams), name_str, name_len + 1);
926 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC);
927
928 zval_ptr_dtor(&zparams);
929 }
930 }
931
932 PHP_METHOD(HttpParams, offsetSet)
933 {
934 zval *nvalue;
935 char *name_str;
936 int name_len;
937
938 if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name_str, &name_len, &nvalue)) {
939 zval **zparam, *zparams = php_http_zsep(1, IS_ARRAY, zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), 0 TSRMLS_CC));
940
941 if (name_len) {
942 if (Z_TYPE_P(nvalue) == IS_ARRAY) {
943 zval *new_zparam;
944
945 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(zparams), name_str, name_len + 1, (void *) &zparam)) {
946 new_zparam = php_http_zsep(1, IS_ARRAY, *zparam);
947 array_join(Z_ARRVAL_P(nvalue), Z_ARRVAL_P(new_zparam), 0, 0);
948 } else {
949 new_zparam = nvalue;
950 Z_ADDREF_P(new_zparam);
951 }
952 add_assoc_zval_ex(zparams, name_str, name_len + 1, new_zparam);
953 } else {
954 zval *tmp;
955
956 if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(zparams), name_str, name_len + 1, (void *) &zparam)) {
957 tmp = php_http_zsep(1, IS_ARRAY, *zparam);
958 } else {
959 MAKE_STD_ZVAL(tmp);
960 array_init(tmp);
961 }
962
963 Z_ADDREF_P(nvalue);
964 add_assoc_zval_ex(tmp, ZEND_STRS("value"), nvalue);
965 add_assoc_zval_ex(zparams, name_str, name_len + 1, tmp);
966 }
967 } else {
968 zval *tmp = php_http_ztyp(IS_STRING, nvalue), *arr;
969
970 MAKE_STD_ZVAL(arr);
971 array_init(arr);
972 add_assoc_bool_ex(arr, ZEND_STRS("value"), 1);
973 add_assoc_zval_ex(zparams, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp) + 1, arr);
974 zval_ptr_dtor(&tmp);
975 }
976
977 zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zparams TSRMLS_CC);
978 zval_ptr_dtor(&zparams);
979 }
980 }
981
982 /*
983 * Local variables:
984 * tab-width: 4
985 * c-basic-offset: 4
986 * End:
987 * vim600: noet sw=4 ts=4 fdm=marker
988 * vim<600: noet sw=4 ts=4
989 */
990