release 1.6.5
[m6w6/ext-http] / http_querystring_api.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-2007, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #include "php_http.h"
17
18 #include "php_variables.h"
19 #ifdef HTTP_HAVE_ICONV
20 # undef PHP_ATOM_INC
21 # include "ext/iconv/php_iconv.h"
22 # include "ext/standard/url.h"
23 #endif
24
25 #include "php_http_api.h"
26 #include "php_http_url_api.h"
27 #include "php_http_querystring_api.h"
28
29 #ifdef ZEND_ENGINE_2
30 #define THIS_CE http_querystring_object_ce
31 extern zend_class_entry *http_querystring_object_ce;
32 #endif
33
34
35 #define http_querystring_modify_array_ex(q, t, k, kl, i, pe) _http_querystring_modify_array_ex((q), (t), (k), (kl), (i), (pe) TSRMLS_CC)
36 static inline int _http_querystring_modify_array_ex(zval *qarray, int key_type, char *key, int keylen, ulong idx, zval *params_entry TSRMLS_DC);
37 #define http_querystring_modify_array(q, p) _http_querystring_modify_array((q), (p) TSRMLS_CC)
38 static inline int _http_querystring_modify_array(zval *qarray, zval *params TSRMLS_DC);
39
40
41 #ifdef HTTP_HAVE_ICONV
42 PHP_HTTP_API int _http_querystring_xlate(zval *array, zval *param, const char *ie, const char *oe TSRMLS_DC)
43 {
44 HashPosition pos;
45 zval **entry = NULL;
46 char *xlate_str = NULL, *xkey;
47 size_t xlate_len = 0, xlen;
48 HashKey key = initHashKey(0);
49
50 FOREACH_KEYVAL(pos, param, key, entry) {
51 if (key.type == HASH_KEY_IS_STRING) {
52 if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(key.str, key.len-1, &xkey, &xlen, oe, ie)) {
53 http_error_ex(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to convert '%.*s' from '%s' to '%s'", key.len-1, key.str, ie, oe);
54 return FAILURE;
55 }
56 }
57
58 if (Z_TYPE_PP(entry) == IS_STRING) {
59 if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry), &xlate_str, &xlate_len, oe, ie)) {
60 if (key.type == HASH_KEY_IS_STRING) {
61 efree(xkey);
62 }
63 http_error_ex(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_PP(entry), Z_STRVAL_PP(entry), ie, oe);
64 return FAILURE;
65 }
66 if (key.type == HASH_KEY_IS_STRING) {
67 add_assoc_stringl_ex(array, xkey, xlen+1, xlate_str, xlate_len, 0);
68 } else {
69 add_index_stringl(array, key.num, xlate_str, xlate_len, 0);
70 }
71 } else if (Z_TYPE_PP(entry) == IS_ARRAY) {
72 zval *subarray;
73
74 MAKE_STD_ZVAL(subarray);
75 array_init(subarray);
76 if (key.type == HASH_KEY_IS_STRING) {
77 add_assoc_zval_ex(array, xkey, xlen+1, subarray);
78 } else {
79 add_index_zval(array, key.num, subarray);
80 }
81 if (SUCCESS != http_querystring_xlate(subarray, *entry, ie, oe)) {
82 if (key.type == HASH_KEY_IS_STRING) {
83 efree(xkey);
84 }
85 return FAILURE;
86 }
87 }
88
89 if (key.type == HASH_KEY_IS_STRING) {
90 efree(xkey);
91 }
92 }
93 return SUCCESS;
94 }
95 #endif /* HAVE_ICONV */
96
97 PHP_HTTP_API void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_DC)
98 {
99 char *s = NULL;
100 size_t l = 0;
101
102 if (Z_TYPE_P(qarray) != IS_ARRAY) {
103 convert_to_array(qarray);
104 }
105 if (SUCCESS == http_urlencode_hash_ex(Z_ARRVAL_P(qarray), 0, NULL, 0, &s, &l)) {
106 zval_dtor(qstring);
107 ZVAL_STRINGL(qstring, s, l, 0);
108 } else {
109 http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to update query string");
110 }
111 }
112
113 PHP_HTTP_API int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC)
114 {
115 if (Z_TYPE_P(params) == IS_ARRAY) {
116 return http_querystring_modify_array(qarray, params);
117 } else if (Z_TYPE_P(params) == IS_OBJECT) {
118 #ifdef ZEND_ENGINE_2
119 if (instanceof_function(Z_OBJCE_P(params), http_querystring_object_ce TSRMLS_CC)) {
120 return http_querystring_modify_array(qarray, zend_read_property(THIS_CE, params, ZEND_STRS("queryArray")-1, 0 TSRMLS_CC));
121 } else {
122 #endif
123 return http_querystring_modify_array(qarray, params);
124 #ifdef ZEND_ENGINE_2
125 }
126 #endif
127 } else {
128 int rv;
129 zval array;
130 zval *qstring = http_zsep(IS_STRING, params);
131
132 INIT_PZVAL(&array);
133 array_init(&array);
134
135 sapi_module.treat_data(PARSE_STRING, estrdup(Z_STRVAL_P(qstring)), &array TSRMLS_CC);
136 zval_ptr_dtor(&qstring);
137
138 rv = http_querystring_modify_array(qarray, &array);
139 zval_dtor(&array);
140 return rv;
141 }
142 }
143
144 static inline int _http_querystring_modify_array(zval *qarray, zval *params TSRMLS_DC)
145 {
146 int rv = 0;
147 HashKey key = initHashKey(0);
148 HashPosition pos;
149 zval **params_entry = NULL;
150
151 FOREACH_HASH_KEYVAL(pos, HASH_OF(params), key, params_entry) {
152 /* only public properties */
153 if ((key.type != HASH_KEY_IS_STRING || *key.str) && http_querystring_modify_array_ex(qarray, key.type, key.str, key.len, key.num, *params_entry)) {
154 rv = 1;
155 }
156 }
157
158 return rv;
159 }
160
161 static inline int _http_querystring_modify_array_ex(zval *qarray, int key_type, char *key, int keylen, ulong idx, zval *params_entry TSRMLS_DC)
162 {
163 zval **qarray_entry;
164
165 /* delete */
166 if (Z_TYPE_P(params_entry) == IS_NULL) {
167 if (key_type == HASH_KEY_IS_STRING) {
168 return (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), key, keylen));
169 } else {
170 return (SUCCESS == zend_hash_index_del(Z_ARRVAL_P(qarray), idx));
171 }
172 }
173
174 /* update */
175 if ( ((key_type == HASH_KEY_IS_STRING) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), key, keylen, (void *) &qarray_entry))) ||
176 ((key_type == HASH_KEY_IS_LONG) && (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(qarray), idx, (void *) &qarray_entry)))) {
177 zval equal;
178
179 /* recursive */
180 if (Z_TYPE_P(params_entry) == IS_ARRAY || Z_TYPE_P(params_entry) == IS_OBJECT) {
181 return http_querystring_modify(*qarray_entry, params_entry);
182 }
183 /* equal */
184 if ((SUCCESS == is_equal_function(&equal, *qarray_entry, params_entry TSRMLS_CC)) && Z_BVAL(equal)) {
185 return 0;
186 }
187 }
188
189 /* add */
190 if (Z_TYPE_P(params_entry) == IS_OBJECT) {
191 zval *new_array;
192
193 MAKE_STD_ZVAL(new_array);
194 array_init(new_array);
195 http_querystring_modify_array(new_array, params_entry);
196 params_entry = new_array;
197 } else {
198 ZVAL_ADDREF(params_entry);
199 }
200 if (key_type == HASH_KEY_IS_STRING) {
201 add_assoc_zval_ex(qarray, key, keylen, params_entry);
202 } else {
203 add_index_zval(qarray, idx, params_entry);
204 }
205 return 1;
206 }
207
208 /*
209 * Local variables:
210 * tab-width: 4
211 * c-basic-offset: 4
212 * End:
213 * vim600: noet sw=4 ts=4 fdm=marker
214 * vim<600: noet sw=4 ts=4
215 */