Back to index

php5  5.3.10
http.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Authors: Sara Golemon <pollita@php.net>                              |
00016    +----------------------------------------------------------------------+
00017 */
00018 
00019 /* $Id: http.c 321634 2012-01-01 13:15:04Z felipe $ */
00020 
00021 #include "php_http.h"
00022 #include "php_ini.h"
00023 #include "url.h"
00024 
00025 #define URL_DEFAULT_ARG_SEP "&"
00026 
00027 /* {{{ php_url_encode_hash */
00028 PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
00029                             const char *num_prefix, int num_prefix_len,
00030                             const char *key_prefix, int key_prefix_len,
00031                             const char *key_suffix, int key_suffix_len,
00032                             zval *type, char *arg_sep TSRMLS_DC)
00033 {
00034        char *key = NULL, *ekey, *newprefix, *p;
00035        int arg_sep_len, key_len, ekey_len, key_type, newprefix_len;
00036        ulong idx;
00037        zval **zdata = NULL, *copyzval;
00038 
00039        if (!ht) {
00040               return FAILURE;
00041        }
00042 
00043        if (ht->nApplyCount > 0) {
00044               /* Prevent recursion */
00045               return SUCCESS;
00046        }
00047 
00048        if (!arg_sep) {
00049               arg_sep = INI_STR("arg_separator.output");
00050               if (!arg_sep || !strlen(arg_sep)) {
00051                      arg_sep = URL_DEFAULT_ARG_SEP;
00052               }
00053        }
00054        arg_sep_len = strlen(arg_sep);
00055 
00056        for (zend_hash_internal_pointer_reset(ht);
00057               (key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT;
00058               zend_hash_move_forward(ht)
00059        ) {
00060               if (key_type == HASH_KEY_IS_STRING && key_len && key[key_len-1] == '\0') {
00061                      /* We don't want that trailing NULL */
00062                      key_len -= 1;
00063               }
00064 
00065               /* handling for private & protected object properties */
00066               if (key && *key == '\0' && type != NULL) {
00067                      char *tmp;
00068 
00069                      zend_object *zobj = zend_objects_get_address(type TSRMLS_CC);
00070                      if (zend_check_property_access(zobj, key, key_len-1 TSRMLS_CC) != SUCCESS) {
00071                             /* private or protected property access outside of the class */
00072                             continue;
00073                      }
00074                      zend_unmangle_property_name(key, key_len-1, &tmp, &key);
00075                      key_len = strlen(key);             
00076               }
00077 
00078               if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) {
00079                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array");
00080                      return FAILURE;
00081               }
00082               if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) {
00083                      if (key_type == HASH_KEY_IS_STRING) {
00084                             ekey = php_url_encode(key, key_len, &ekey_len);
00085                             newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 3 /* %5B */;
00086                             newprefix = emalloc(newprefix_len + 1);
00087                             p = newprefix;
00088 
00089                             if (key_prefix) {
00090                                    memcpy(p, key_prefix, key_prefix_len);
00091                                    p += key_prefix_len;
00092                             }
00093 
00094                             memcpy(p, ekey, ekey_len);
00095                             p += ekey_len;
00096                             efree(ekey);
00097 
00098                             if (key_suffix) {
00099                                    memcpy(p, key_suffix, key_suffix_len);
00100                                    p += key_suffix_len;
00101                             }
00102                             *(p++) = '%';
00103                             *(p++) = '5';
00104                             *(p++) = 'B';
00105                             *p = '\0';
00106                      } else {
00107                             /* Is an integer key */
00108                             ekey_len = spprintf(&ekey, 0, "%ld", idx);
00109                             newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 3 /* %5B */;
00110                             newprefix = emalloc(newprefix_len + 1);
00111                             p = newprefix;
00112 
00113                             if (key_prefix) {
00114                                    memcpy(p, key_prefix, key_prefix_len);
00115                                    p += key_prefix_len;
00116                             }
00117 
00118                             memcpy(p, num_prefix, num_prefix_len);
00119                             p += num_prefix_len;
00120 
00121                             memcpy(p, ekey, ekey_len);
00122                             p += ekey_len;
00123                             efree(ekey);
00124 
00125                             if (key_suffix) {
00126                                    memcpy(p, key_suffix, key_suffix_len);
00127                                    p += key_suffix_len;
00128                             }
00129                             *(p++) = '%';
00130                             *(p++) = '5';
00131                             *(p++) = 'B';
00132                             *p = '\0';
00133                      }
00134                      ht->nApplyCount++;
00135                      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);
00136                      ht->nApplyCount--;
00137                      efree(newprefix);
00138               } else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
00139                      /* Skip these types */
00140                      continue;
00141               } else {
00142                      if (formstr->len) {
00143                             smart_str_appendl(formstr, arg_sep, arg_sep_len);
00144                      }
00145                      /* Simple key=value */
00146                      smart_str_appendl(formstr, key_prefix, key_prefix_len);
00147                      if (key_type == HASH_KEY_IS_STRING) {
00148                             ekey = php_url_encode(key, key_len, &ekey_len);
00149                             smart_str_appendl(formstr, ekey, ekey_len);
00150                             efree(ekey);
00151                      } else {
00152                             /* Numeric key */
00153                             if (num_prefix) {
00154                                    smart_str_appendl(formstr, num_prefix, num_prefix_len);
00155                             }
00156                             ekey_len = spprintf(&ekey, 0, "%ld", idx);
00157                             smart_str_appendl(formstr, ekey, ekey_len);
00158                             efree(ekey);
00159                      }
00160                      smart_str_appendl(formstr, key_suffix, key_suffix_len);
00161                      smart_str_appendl(formstr, "=", 1);
00162                      switch (Z_TYPE_PP(zdata)) {
00163                             case IS_STRING:
00164                                    ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
00165                                    break;
00166                             case IS_LONG:
00167                             case IS_BOOL:
00168                                    ekey_len = spprintf(&ekey, 0, "%ld", Z_LVAL_PP(zdata));
00169                                    break;
00170                             case IS_DOUBLE:
00171                                    ekey_len = spprintf(&ekey, 0, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata));
00172                                    break;
00173                             default:
00174                                    /* fall back on convert to string */
00175                                    MAKE_STD_ZVAL(copyzval);
00176                                    *copyzval = **zdata;
00177                                    zval_copy_ctor(copyzval);
00178                                    convert_to_string_ex(&copyzval);
00179                                    ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
00180                                    zval_ptr_dtor(&copyzval);
00181                      }
00182                      smart_str_appendl(formstr, ekey, ekey_len);
00183                      efree(ekey);
00184               }
00185        }
00186 
00187        return SUCCESS;
00188 }
00189 /* }}} */
00190 
00191 /* {{{ proto string http_build_query(mixed formdata [, string prefix [, string arg_separator]])
00192    Generates a form-encoded query string from an associative array or object. */
00193 PHP_FUNCTION(http_build_query)
00194 {
00195        zval *formdata;
00196        char *prefix = NULL, *arg_sep=NULL;
00197        int arg_sep_len = 0, prefix_len = 0;
00198        smart_str formstr = {0};
00199        
00200 
00201        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|ss", &formdata, &prefix, &prefix_len, &arg_sep, &arg_sep_len) != SUCCESS) {
00202               RETURN_FALSE;
00203        }
00204 
00205        if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
00206               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Parameter 1 expected to be Array or Object.  Incorrect value given");
00207               RETURN_FALSE;
00208        }
00209 
00210        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) {
00211               if (formstr.c) {
00212                      efree(formstr.c);
00213               }
00214               RETURN_FALSE;
00215        }
00216 
00217        if (!formstr.c) {
00218               RETURN_EMPTY_STRING();
00219        }
00220 
00221        smart_str_0(&formstr);
00222        
00223        RETURN_STRINGL(formstr.c, formstr.len, 0);
00224 }
00225 /* }}} */
00226 
00227 /*
00228  * Local variables:
00229  * tab-width: 4
00230  * c-basic-offset: 4
00231  * End:
00232  * vim600: sw=4 ts=4 fdm=marker
00233  * vim<600: sw=4 ts=4
00234  */
00235