2004-01-07 04:07:07 +08:00
|
|
|
/*
|
2003-09-06 08:35:21 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| PHP Version 5 |
|
2003-09-06 08:35:21 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2004-01-08 16:18:22 +08:00
|
|
|
| Copyright (c) 1997-2004 The PHP Group |
|
2003-09-06 08:35:21 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
| This source file is subject to version 3.0 of the PHP license, |
|
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available through the world-wide-web at the following url: |
|
|
|
|
| http://www.php.net/license/3_0.txt. |
|
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
|
|
|
+----------------------------------------------------------------------+
|
2003-09-07 08:00:00 +08:00
|
|
|
| Authors: Sara Golemon <pollita@php.net> |
|
2003-09-06 08:35:21 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
|
|
|
|
2003-09-06 20:56:02 +08:00
|
|
|
#include "php_http.h"
|
2003-09-06 08:35:21 +08:00
|
|
|
#include "php_ini.h"
|
|
|
|
#include "url.h"
|
|
|
|
|
|
|
|
#define URL_DEFAULT_ARG_SEP "&"
|
|
|
|
|
|
|
|
/* {{{ php_url_encode_hash */
|
|
|
|
PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
|
|
|
|
const char *num_prefix, int num_prefix_len,
|
|
|
|
const char *key_prefix, int key_prefix_len,
|
2004-01-07 04:07:07 +08:00
|
|
|
const char *key_suffix, int key_suffix_len,
|
|
|
|
zval *type TSRMLS_DC)
|
2003-09-06 08:35:21 +08:00
|
|
|
{
|
|
|
|
char *arg_sep = NULL, *key = NULL, *ekey, *newprefix, *p;
|
|
|
|
int arg_sep_len, key_len, ekey_len, key_type, newprefix_len;
|
|
|
|
ulong idx;
|
|
|
|
zval **zdata = NULL, *copyzval;
|
|
|
|
|
|
|
|
if (!ht) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2003-09-06 13:41:36 +08:00
|
|
|
if (ht->nApplyCount > 0) {
|
2003-09-06 21:07:47 +08:00
|
|
|
/* Prevent recursion */
|
2003-09-06 13:41:36 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
2003-09-06 08:35:21 +08:00
|
|
|
arg_sep = INI_STR("arg_separator.output");
|
|
|
|
if (!arg_sep || !strlen(arg_sep)) {
|
|
|
|
arg_sep = URL_DEFAULT_ARG_SEP;
|
|
|
|
}
|
|
|
|
arg_sep_len = strlen(arg_sep);
|
|
|
|
|
2003-09-06 21:07:47 +08:00
|
|
|
for (zend_hash_internal_pointer_reset(ht);
|
2003-09-06 08:35:21 +08:00
|
|
|
(key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT;
|
2003-09-06 21:07:47 +08:00
|
|
|
zend_hash_move_forward(ht)
|
|
|
|
) {
|
2003-09-06 08:35:21 +08:00
|
|
|
if (key_len && key[key_len-1] == '\0') {
|
|
|
|
/* We don't want that trailing NULL */
|
|
|
|
key_len -= 1;
|
|
|
|
}
|
2004-02-21 04:38:14 +08:00
|
|
|
|
2004-01-07 04:07:07 +08:00
|
|
|
/* handling for private & protected object properties */
|
2004-02-21 04:38:14 +08:00
|
|
|
if (key && *key == '\0' && type != NULL) {
|
2004-01-07 04:38:00 +08:00
|
|
|
char *tmp;
|
|
|
|
|
2004-01-07 04:07:07 +08:00
|
|
|
zend_object *zobj = zend_objects_get_address(type TSRMLS_CC);
|
|
|
|
if (zend_check_property_access(zobj, key TSRMLS_CC) != SUCCESS) {
|
|
|
|
/* private or protected property access outside of the class */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
zend_unmangle_property_name(key, &tmp, &key);
|
|
|
|
key_len = strlen(key);
|
|
|
|
}
|
2003-09-06 08:35:21 +08:00
|
|
|
|
2003-09-06 15:34:58 +08:00
|
|
|
if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) {
|
2003-09-06 08:35:21 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array.");
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) {
|
|
|
|
if (key_type == HASH_KEY_IS_STRING) {
|
|
|
|
ekey = php_url_encode(key, key_len, &ekey_len);
|
|
|
|
newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 1;
|
|
|
|
newprefix = emalloc(newprefix_len + 1);
|
|
|
|
p = newprefix;
|
|
|
|
|
|
|
|
if (key_prefix) {
|
|
|
|
memcpy(p, key_prefix, key_prefix_len);
|
|
|
|
p += key_prefix_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(p, ekey, ekey_len);
|
|
|
|
p += ekey_len;
|
|
|
|
efree(ekey);
|
|
|
|
|
|
|
|
if (key_suffix) {
|
|
|
|
memcpy(p, key_suffix, key_suffix_len);
|
|
|
|
p += key_suffix_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
*(p++) = '[';
|
|
|
|
*p = '\0';
|
|
|
|
} else {
|
|
|
|
/* Is an integer key */
|
|
|
|
ekey_len = spprintf(&ekey, 12, "%ld", idx);
|
|
|
|
newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 1;
|
|
|
|
newprefix = emalloc(newprefix_len + 1);
|
|
|
|
p = newprefix;
|
|
|
|
|
|
|
|
if (key_prefix) {
|
|
|
|
memcpy(p, key_prefix, key_prefix_len);
|
|
|
|
p += key_prefix_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(p, num_prefix, num_prefix_len);
|
|
|
|
p += num_prefix_len;
|
|
|
|
|
|
|
|
memcpy(p, ekey, ekey_len);
|
|
|
|
p += ekey_len;
|
|
|
|
efree(ekey);
|
|
|
|
|
|
|
|
if (key_suffix) {
|
|
|
|
memcpy(p, key_suffix, key_suffix_len);
|
|
|
|
p += key_suffix_len;
|
|
|
|
}
|
|
|
|
*(p++) = '[';
|
|
|
|
*p = '\0';
|
|
|
|
}
|
2003-09-06 13:41:36 +08:00
|
|
|
ht->nApplyCount++;
|
2004-01-07 04:07:07 +08:00
|
|
|
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);
|
2003-09-06 13:41:36 +08:00
|
|
|
ht->nApplyCount--;
|
2003-09-06 08:35:21 +08:00
|
|
|
efree(newprefix);
|
2003-09-06 14:57:40 +08:00
|
|
|
} else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
|
|
|
|
/* Skip these types */
|
|
|
|
continue;
|
2003-09-06 08:35:21 +08:00
|
|
|
} else {
|
|
|
|
if (formstr->len) {
|
|
|
|
smart_str_appendl(formstr, arg_sep, arg_sep_len);
|
|
|
|
}
|
|
|
|
/* Simple key=value */
|
|
|
|
smart_str_appendl(formstr, key_prefix, key_prefix_len);
|
|
|
|
if (key_type == HASH_KEY_IS_STRING) {
|
|
|
|
ekey = php_url_encode(key, key_len, &ekey_len);
|
|
|
|
smart_str_appendl(formstr, ekey, ekey_len);
|
|
|
|
efree(ekey);
|
|
|
|
} else {
|
|
|
|
/* Numeric key */
|
|
|
|
if (num_prefix) {
|
|
|
|
smart_str_appendl(formstr, num_prefix, num_prefix_len);
|
|
|
|
}
|
2004-01-07 04:07:07 +08:00
|
|
|
ekey_len = spprintf(&ekey, 12, "%ld", idx);
|
2003-09-06 08:35:21 +08:00
|
|
|
smart_str_appendl(formstr, ekey, ekey_len);
|
|
|
|
efree(ekey);
|
|
|
|
}
|
|
|
|
smart_str_appendl(formstr, key_suffix, key_suffix_len);
|
|
|
|
smart_str_appendl(formstr, "=", 1);
|
|
|
|
switch (Z_TYPE_PP(zdata)) {
|
|
|
|
case IS_STRING:
|
|
|
|
ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
|
|
|
|
break;
|
|
|
|
case IS_LONG:
|
2003-09-06 14:57:40 +08:00
|
|
|
case IS_BOOL:
|
|
|
|
ekey_len = spprintf(&ekey, 12, "%ld", Z_LVAL_PP(zdata));
|
|
|
|
break;
|
|
|
|
case IS_DOUBLE:
|
|
|
|
ekey_len = spprintf(&ekey, 48, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata));
|
2003-09-06 08:35:21 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* fall back on convert to string */
|
2003-09-06 14:57:40 +08:00
|
|
|
MAKE_STD_ZVAL(copyzval);
|
2003-09-06 08:35:21 +08:00
|
|
|
*copyzval = **zdata;
|
|
|
|
zval_copy_ctor(copyzval);
|
|
|
|
convert_to_string_ex(©zval);
|
|
|
|
ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
|
|
|
|
zval_ptr_dtor(©zval);
|
|
|
|
}
|
|
|
|
smart_str_appendl(formstr, ekey, ekey_len);
|
|
|
|
efree(ekey);
|
|
|
|
}
|
2004-01-07 04:07:07 +08:00
|
|
|
}
|
2003-09-06 08:35:21 +08:00
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
/* }}} */
|
|
|
|
|
2003-09-06 21:07:47 +08:00
|
|
|
/* {{{ proto string http_build_query(mixed formdata [, string prefix])
|
2003-09-07 08:00:00 +08:00
|
|
|
Generates a form-encoded query string from an associative array or object. */
|
2003-09-06 08:35:21 +08:00
|
|
|
PHP_FUNCTION(http_build_query)
|
|
|
|
{
|
|
|
|
zval *formdata;
|
|
|
|
char *prefix = NULL;
|
|
|
|
int prefix_len = 0;
|
|
|
|
smart_str formstr = {0};
|
|
|
|
|
2003-09-06 15:34:58 +08:00
|
|
|
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|s", &formdata, &prefix, &prefix_len) != SUCCESS) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
|
|
|
|
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object. Incorrect value given.");
|
2003-09-06 08:35:21 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2004-01-07 04:07:07 +08:00
|
|
|
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) {
|
2003-09-06 08:35:21 +08:00
|
|
|
if (formstr.c) {
|
|
|
|
efree(formstr.c);
|
|
|
|
}
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
2004-01-07 04:07:07 +08:00
|
|
|
|
|
|
|
if (!formstr.c) {
|
|
|
|
RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
2003-09-06 08:35:21 +08:00
|
|
|
smart_str_0(&formstr);
|
2004-01-07 04:07:07 +08:00
|
|
|
|
2003-09-06 08:35:21 +08:00
|
|
|
RETURN_STRINGL(formstr.c, formstr.len, 0);
|
|
|
|
}
|
|
|
|
/* }}} */
|
2004-01-07 04:07:07 +08:00
|
|
|
|
2003-09-06 08:35:21 +08:00
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
|
|
|
*/
|
|
|
|
|