1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Authors: Sara Golemon <pollita@php.net> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: http.c 276986 2009-03-10 23:40:06Z helly $ */
20 :
21 : #include "php_http.h"
22 : #include "php_ini.h"
23 : #include "url.h"
24 :
25 : #define URL_DEFAULT_ARG_SEP "&"
26 :
27 : /* {{{ php_url_encode_hash */
28 : PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
29 : const char *num_prefix, int num_prefix_len,
30 : const char *key_prefix, int key_prefix_len,
31 : const char *key_suffix, int key_suffix_len,
32 : zval *type, char *arg_sep TSRMLS_DC)
33 6 : {
34 : zstr key;
35 : char *ekey, *newprefix, *p;
36 : uint key_len;
37 : int arg_sep_len, ekey_len, key_type, newprefix_len;
38 : ulong idx;
39 6 : zval **zdata = NULL, *copyzval;
40 : zend_bool free_key;
41 :
42 6 : if (!ht) {
43 0 : return FAILURE;
44 : }
45 :
46 6 : if (ht->nApplyCount > 0) {
47 : /* Prevent recursion */
48 0 : return SUCCESS;
49 : }
50 :
51 6 : if (!arg_sep) {
52 5 : arg_sep = INI_STR("arg_separator.output");
53 5 : if (!arg_sep || !strlen(arg_sep)) {
54 0 : arg_sep = URL_DEFAULT_ARG_SEP;
55 : }
56 : }
57 6 : arg_sep_len = strlen(arg_sep);
58 :
59 6 : for (zend_hash_internal_pointer_reset(ht);
60 30 : (key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT;
61 18 : zend_hash_move_forward(ht))
62 : {
63 18 : free_key = 0;
64 :
65 : /* handling for private & protected object properties */
66 20 : if (((key_type == HASH_KEY_IS_STRING && key.s && key.s[0] == '\0') ||
67 : (key_type == HASH_KEY_IS_UNICODE && key.u && key.u[0] == 0))
68 : && type != NULL) {
69 : zstr tmp;
70 :
71 4 : zend_object *zobj = zend_objects_get_address(type TSRMLS_CC);
72 4 : if (zend_check_property_access(zobj, key_type, key, key_len-1 TSRMLS_CC) != SUCCESS) {
73 : /* private or protected property access outside of the class */
74 2 : continue;
75 : }
76 2 : zend_u_unmangle_property_name(key_type, key, key_len-1, &tmp, &key);
77 2 : key_len = (key_type == IS_UNICODE) ? u_strlen(key.u) : strlen(key.s);
78 : } else {
79 14 : key_len -= 1;
80 : }
81 :
82 16 : if (key_type == HASH_KEY_IS_UNICODE) {
83 : char *temp;
84 : int temp_len;
85 :
86 13 : zend_unicode_to_string(UG(utf8_conv), &temp, &temp_len, key.u, key_len TSRMLS_CC);
87 13 : key.s = temp;
88 13 : key_len = temp_len;
89 13 : key_type = HASH_KEY_IS_STRING;
90 13 : free_key = 1;
91 : }
92 :
93 16 : if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) {
94 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array");
95 0 : return FAILURE;
96 : }
97 :
98 16 : if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) {
99 0 : if (key_type == HASH_KEY_IS_STRING) {
100 0 : ekey = php_url_encode(key.s, key_len, &ekey_len);
101 0 : newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 3 /* %5B */;
102 0 : newprefix = emalloc(newprefix_len + 1);
103 0 : p = newprefix;
104 :
105 0 : if (key_prefix) {
106 0 : memcpy(p, key_prefix, key_prefix_len);
107 0 : p += key_prefix_len;
108 : }
109 :
110 0 : memcpy(p, ekey, ekey_len);
111 0 : p += ekey_len;
112 0 : efree(ekey);
113 :
114 0 : if (key_suffix) {
115 0 : memcpy(p, key_suffix, key_suffix_len);
116 0 : p += key_suffix_len;
117 : }
118 0 : *(p++) = '%';
119 0 : *(p++) = '5';
120 0 : *(p++) = 'B';
121 0 : *p = '\0';
122 : } else {
123 : /* Is an integer key */
124 0 : ekey_len = spprintf(&ekey, 0, "%ld", idx);
125 0 : newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 3 /* %5B */;
126 0 : newprefix = emalloc(newprefix_len + 1);
127 0 : p = newprefix;
128 :
129 0 : if (key_prefix) {
130 0 : memcpy(p, key_prefix, key_prefix_len);
131 0 : p += key_prefix_len;
132 : }
133 :
134 0 : memcpy(p, num_prefix, num_prefix_len);
135 0 : p += num_prefix_len;
136 :
137 0 : memcpy(p, ekey, ekey_len);
138 0 : p += ekey_len;
139 0 : efree(ekey);
140 :
141 0 : if (key_suffix) {
142 0 : memcpy(p, key_suffix, key_suffix_len);
143 0 : p += key_suffix_len;
144 : }
145 0 : *(p++) = '%';
146 0 : *(p++) = '5';
147 0 : *(p++) = 'B';
148 0 : *p = '\0';
149 : }
150 0 : ht->nApplyCount++;
151 0 : php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL, 0, newprefix, newprefix_len, "%5D", 3, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL), arg_sep TSRMLS_CC);
152 0 : ht->nApplyCount--;
153 0 : efree(newprefix);
154 16 : } else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
155 : /* Skip these types */
156 0 : if (free_key) {
157 0 : efree(key.s);
158 : }
159 0 : continue;
160 : } else {
161 16 : if (formstr->len) {
162 11 : smart_str_appendl(formstr, arg_sep, arg_sep_len);
163 : }
164 : /* Simple key=value */
165 16 : smart_str_appendl(formstr, key_prefix, key_prefix_len);
166 16 : if (key_type == HASH_KEY_IS_STRING) {
167 13 : ekey = php_url_encode(key.s, key_len, &ekey_len);
168 13 : smart_str_appendl(formstr, ekey, ekey_len);
169 13 : efree(ekey);
170 : } else {
171 : /* Numeric key */
172 3 : if (num_prefix) {
173 2 : smart_str_appendl(formstr, num_prefix, num_prefix_len);
174 : }
175 3 : ekey_len = spprintf(&ekey, 0, "%ld", idx);
176 3 : smart_str_appendl(formstr, ekey, ekey_len);
177 3 : efree(ekey);
178 : }
179 16 : smart_str_appendl(formstr, key_suffix, key_suffix_len);
180 16 : smart_str_appendl(formstr, "=", 1);
181 16 : switch (Z_TYPE_PP(zdata)) {
182 : case IS_UNICODE:
183 : {
184 : char *temp;
185 : int temp_len;
186 13 : zend_unicode_to_string(UG(utf8_conv), &temp, &temp_len, Z_USTRVAL_PP(zdata), Z_USTRLEN_PP(zdata) TSRMLS_CC);
187 13 : if (temp) {
188 13 : ekey = php_url_encode(temp, temp_len, &ekey_len);
189 13 : efree(temp);
190 : } else {
191 0 : smart_str_free(formstr);
192 0 : return FAILURE;
193 : }
194 13 : break;
195 : }
196 : case IS_STRING:
197 0 : ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
198 0 : break;
199 : case IS_LONG:
200 : case IS_BOOL:
201 3 : ekey_len = spprintf(&ekey, 0, "%ld", Z_LVAL_PP(zdata));
202 3 : break;
203 : case IS_DOUBLE:
204 0 : ekey_len = spprintf(&ekey, 0, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata));
205 0 : break;
206 : default:
207 : /* fall back on convert to string */
208 0 : MAKE_STD_ZVAL(copyzval);
209 0 : *copyzval = **zdata;
210 0 : zval_copy_ctor(copyzval);
211 0 : convert_to_string_ex(©zval);
212 0 : ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
213 0 : zval_ptr_dtor(©zval);
214 : }
215 16 : smart_str_appendl(formstr, ekey, ekey_len);
216 16 : efree(ekey);
217 : }
218 :
219 16 : if (free_key) {
220 13 : efree(key.s);
221 : }
222 : }
223 :
224 6 : return SUCCESS;
225 : }
226 : /* }}} */
227 :
228 : /* {{{ proto string http_build_query(mixed formdata [, string prefix [, string arg_separator]]) U
229 : Generates a form-encoded query string from an associative array or object. */
230 : PHP_FUNCTION(http_build_query)
231 6 : {
232 : zval *formdata;
233 6 : char *prefix = NULL, *arg_sep = NULL;
234 6 : int arg_sep_len = 0, prefix_len = 0;
235 6 : smart_str formstr = {0};
236 :
237 6 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s&s&", &formdata, &prefix, &prefix_len, UG(utf8_conv),
238 : &arg_sep, &arg_sep_len, UG(utf8_conv)) != SUCCESS) {
239 0 : RETURN_FALSE;
240 : }
241 :
242 6 : if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
243 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object. Incorrect value given");
244 0 : RETURN_FALSE;
245 : }
246 :
247 6 : if (php_url_encode_hash_ex(HASH_OF(formdata), &formstr, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL), arg_sep TSRMLS_CC) == FAILURE) {
248 0 : if (formstr.c) {
249 0 : efree(formstr.c);
250 : }
251 0 : RETURN_FALSE;
252 : }
253 :
254 6 : if (!formstr.c) {
255 1 : RETURN_EMPTY_STRING();
256 : }
257 :
258 5 : smart_str_0(&formstr);
259 :
260 5 : RETURN_ASCII_STRINGL(formstr.c, formstr.len, ZSTR_AUTOFREE);
261 : }
262 : /* }}} */
263 :
264 : /*
265 : * Local variables:
266 : * tab-width: 4
267 : * c-basic-offset: 4
268 : * End:
269 : * vim600: sw=4 ts=4 fdm=marker
270 : * vim<600: sw=4 ts=4
271 : */
272 :
|