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