* php 4!
[m6w6/ext-http] / http_build_query.c
1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2004 The PHP Group |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.0 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_0.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$ */
20
21 #include "php_http.h"
22 #include "php_ini.h"
23 #include "ext/standard/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 TSRMLS_DC)
33 {
34 char *arg_sep = NULL, *key = NULL, *ekey, *newprefix, *p;
35 int arg_sep_len, key_len, ekey_len, key_type, newprefix_len;
36 ulong idx;
37 zval **zdata = NULL, *copyzval;
38
39 if (!ht) {
40 return FAILURE;
41 }
42
43 if (ht->nApplyCount > 0) {
44 /* Prevent recursion */
45 return SUCCESS;
46 }
47
48 arg_sep = INI_STR("arg_separator.output");
49 if (!arg_sep || !strlen(arg_sep)) {
50 arg_sep = URL_DEFAULT_ARG_SEP;
51 }
52 arg_sep_len = strlen(arg_sep);
53
54 for (zend_hash_internal_pointer_reset(ht);
55 (key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT;
56 zend_hash_move_forward(ht)
57 ) {
58 if (key_type == HASH_KEY_IS_STRING && key_len && key[key_len-1] == '\0') {
59 /* We don't want that trailing NULL */
60 key_len -= 1;
61 }
62
63 /* handling for private & protected object properties */
64 if (key && *key == '\0' && type != NULL) {
65 char *tmp;
66
67 zend_object *zobj = zend_objects_get_address(type TSRMLS_CC);
68 if (zend_check_property_access(zobj, key TSRMLS_CC) != SUCCESS) {
69 /* private or protected property access outside of the class */
70 continue;
71 }
72 zend_unmangle_property_name(key, &tmp, &key);
73 key_len = strlen(key);
74 }
75
76 if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) {
77 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array.");
78 return FAILURE;
79 }
80 if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) {
81 if (key_type == HASH_KEY_IS_STRING) {
82 ekey = php_url_encode(key, key_len, &ekey_len);
83 newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 1;
84 newprefix = emalloc(newprefix_len + 1);
85 p = newprefix;
86
87 if (key_prefix) {
88 memcpy(p, key_prefix, key_prefix_len);
89 p += key_prefix_len;
90 }
91
92 memcpy(p, ekey, ekey_len);
93 p += ekey_len;
94 efree(ekey);
95
96 if (key_suffix) {
97 memcpy(p, key_suffix, key_suffix_len);
98 p += key_suffix_len;
99 }
100
101 *(p++) = '[';
102 *p = '\0';
103 } else {
104 /* Is an integer key */
105 ekey_len = spprintf(&ekey, 12, "%ld", idx);
106 newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 1;
107 newprefix = emalloc(newprefix_len + 1);
108 p = newprefix;
109
110 if (key_prefix) {
111 memcpy(p, key_prefix, key_prefix_len);
112 p += key_prefix_len;
113 }
114
115 memcpy(p, num_prefix, num_prefix_len);
116 p += num_prefix_len;
117
118 memcpy(p, ekey, ekey_len);
119 p += ekey_len;
120 efree(ekey);
121
122 if (key_suffix) {
123 memcpy(p, key_suffix, key_suffix_len);
124 p += key_suffix_len;
125 }
126 *(p++) = '[';
127 *p = '\0';
128 }
129 ht->nApplyCount++;
130 php_url_encode_hash_ex(HASH_OF(*zdata), formstr, NULL, 0, newprefix, newprefix_len, "]", 1,
131 (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL) TSRMLS_CC);
132 ht->nApplyCount--;
133 efree(newprefix);
134 } else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
135 /* Skip these types */
136 continue;
137 } else {
138 if (formstr->len) {
139 smart_str_appendl(formstr, arg_sep, arg_sep_len);
140 }
141 /* Simple key=value */
142 smart_str_appendl(formstr, key_prefix, key_prefix_len);
143 if (key_type == HASH_KEY_IS_STRING) {
144 ekey = php_url_encode(key, key_len, &ekey_len);
145 smart_str_appendl(formstr, ekey, ekey_len);
146 efree(ekey);
147 } else {
148 /* Numeric key */
149 if (num_prefix) {
150 smart_str_appendl(formstr, num_prefix, num_prefix_len);
151 }
152 ekey_len = spprintf(&ekey, 12, "%ld", idx);
153 smart_str_appendl(formstr, ekey, ekey_len);
154 efree(ekey);
155 }
156 smart_str_appendl(formstr, key_suffix, key_suffix_len);
157 smart_str_appendl(formstr, "=", 1);
158 switch (Z_TYPE_PP(zdata)) {
159 case IS_STRING:
160 ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
161 break;
162 case IS_LONG:
163 case IS_BOOL:
164 ekey_len = spprintf(&ekey, 12, "%ld", Z_LVAL_PP(zdata));
165 break;
166 case IS_DOUBLE:
167 ekey_len = spprintf(&ekey, 48, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata));
168 break;
169 default:
170 /* fall back on convert to string */
171 MAKE_STD_ZVAL(copyzval);
172 *copyzval = **zdata;
173 zval_copy_ctor(copyzval);
174 convert_to_string_ex(&copyzval);
175 ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
176 zval_ptr_dtor(&copyzval);
177 }
178 smart_str_appendl(formstr, ekey, ekey_len);
179 efree(ekey);
180 }
181 }
182
183 return SUCCESS;
184 }
185 /* }}} */
186
187 /* {{{ proto string http_build_query(mixed formdata [, string prefix])
188 Generates a form-encoded query string from an associative array or object. */
189 PHP_FUNCTION(http_build_query)
190 {
191 zval *formdata;
192 char *prefix = NULL;
193 int prefix_len = 0;
194 smart_str formstr = {0};
195
196 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &formdata, &prefix, &prefix_len) != SUCCESS) {
197 RETURN_FALSE;
198 }
199
200 if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
201 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object. Incorrect value given.");
202 RETURN_FALSE;
203 }
204
205 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) TSRMLS_CC) == FAILURE) {
206 if (formstr.c) {
207 efree(formstr.c);
208 }
209 RETURN_FALSE;
210 }
211
212 if (!formstr.c) {
213 RETURN_NULL();
214 }
215
216 smart_str_0(&formstr);
217
218 RETURN_STRINGL(formstr.c, formstr.len, 0);
219 }
220 /* }}} */
221
222 /*
223 * Local variables:
224 * tab-width: 4
225 * c-basic-offset: 4
226 * End:
227 * vim600: sw=4 ts=4 fdm=marker
228 * vim<600: sw=4 ts=4
229 */
230