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