Merge branch 'master' into phpng
[m6w6/ext-http] / php_http_querystring.c
index 77df3215dfaa7daaf6fc009944b050eda8ea12dd..5f4eff81a9a101414d4ffed647fcc9ae6c9a4f12 100644 (file)
@@ -16,6 +16,9 @@
 #include <ext/spl/spl_array.h>
 
 #ifdef PHP_HTTP_HAVE_ICONV
+#      ifndef HAVE_ICONV
+#              define HAVE_ICONV 1
+#      endif
 #      undef PHP_ATOM_INC
 #      include <ext/iconv/php_iconv.h>
 #endif
@@ -29,7 +32,7 @@ static inline void php_http_querystring_set(zval *instance, zval *params, int fl
        array_init(&qa);
 
        if (flags & QS_MERGE) {
-               zval *old = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0);
+               zval old_tmp, *old = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &old_tmp);
 
                ZVAL_DEREF(old);
                if (Z_TYPE_P(old) == IS_ARRAY) {
@@ -39,11 +42,12 @@ static inline void php_http_querystring_set(zval *instance, zval *params, int fl
 
        php_http_querystring_update(&qa, params, NULL);
        zend_update_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), &qa);
+       zval_ptr_dtor(&qa);
 }
 
 static inline void php_http_querystring_str(zval *instance, zval *return_value)
 {
-       zval *qa = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0);
+       zval qa_tmp, *qa = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &qa_tmp);
 
        ZVAL_DEREF(qa);
        if (Z_TYPE_P(qa) == IS_ARRAY) {
@@ -53,18 +57,20 @@ static inline void php_http_querystring_str(zval *instance, zval *return_value)
        }
 }
 
-static inline void php_http_querystring_get(zval *instance, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value TSRMLS_DC)
+static inline void php_http_querystring_get(zval *instance, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value)
 {
-       zval *arrval, *qarray = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0 TSRMLS_CC);
+       zval *arrval, qarray_tmp, *qarray = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &qarray_tmp);
 
        ZVAL_DEREF(qarray);
        if ((Z_TYPE_P(qarray) == IS_ARRAY) && (arrval = zend_symtable_str_find(Z_ARRVAL_P(qarray), name, name_len))) {
                if (type && type != Z_TYPE_P(arrval)) {
-                       SEPARATE_ZVAL(arrval);
-                       convert_to_explicit_type(arrval, type);
-                       RETVAL_ZVAL(arrval, 1, 1);
+                       zval tmp;
+
+                       ZVAL_DUP(&tmp, arrval);
+                       convert_to_explicit_type(&tmp, type);
+                       RETVAL_ZVAL(&tmp, 0, 0);
                } else {
-                       RETVAL_ZVAL_FAST(arrval);
+                       RETVAL_ZVAL(arrval, 1, 0);
                }
 
                if (del) {
@@ -76,7 +82,7 @@ static inline void php_http_querystring_get(zval *instance, int type, char *name
                        zval_ptr_dtor(&delarr);
                }
        } else if(defval) {
-               RETURN_ZVAL_FAST(defval);
+               RETURN_ZVAL(defval, 1, 0);
        }
 }
 
@@ -175,7 +181,7 @@ ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size
        opts.val = vsepp;
        opts.flags = PHP_HTTP_PARAMS_QUERY;
 
-       if (SUCCESS == php_http_ini_entry(ZEND_STRL("arg_separator.input"), &asi_str, &asi_len, 0 TSRMLS_CC) && asi_len) {
+       if (SUCCESS == php_http_ini_entry(ZEND_STRL("arg_separator.input"), &asi_str, &asi_len, 0) && asi_len) {
                zval arr;
 
                array_init_size(&arr, asi_len);
@@ -185,7 +191,6 @@ ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size
                } while (*asi_str);
 
                opts.param = php_http_params_separator_init(&arr);
-
                zval_ptr_dtor(&arr);
        }
 
@@ -222,7 +227,7 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o
 
                /* squeeze the hash out of the zval */
                if (Z_TYPE_P(params) == IS_OBJECT && instanceof_function(Z_OBJCE_P(params), php_http_querystring_class_entry)) {
-                       zval *qa = zend_read_property(php_http_querystring_class_entry, params, ZEND_STRL("queryArray"), 0);
+                       zval qa_tmp, *qa = zend_read_property(php_http_querystring_class_entry, params, ZEND_STRL("queryArray"), 0, &qa_tmp);
 
                        ZVAL_DEREF(qa);
                        convert_to_array(qa);
@@ -239,7 +244,7 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o
                        ht = Z_ARRVAL(zv);
                }
 
-               ZEND_HASH_FOREACH_KEY_VAL(ht, key.h, key.key, params_entry)
+               ZEND_HASH_FOREACH_KEY_VAL_IND(ht, key.h, key.key, params_entry)
                {
                        /* only public properties */
                        if (!key.key || *key.key->val) {
@@ -257,12 +262,12 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o
                                        /*
                                         * update
                                         */
-                                       zval equal, *entry = NULL;
+                                       zval equal, tmp, *entry = &tmp;
 
+                                       ZVAL_UNDEF(&tmp);
                                        /* recursive */
                                        if (Z_TYPE_P(params_entry) == IS_ARRAY || Z_TYPE_P(params_entry) == IS_OBJECT) {
-                                               entry = qarray_entry;
-                                               SEPARATE_ZVAL(entry);
+                                               ZVAL_DUP(entry, qarray_entry);
                                                convert_to_array(entry);
                                                php_http_querystring_update(entry, params_entry, NULL);
                                        } else if ((FAILURE == is_equal_function(&equal, qarray_entry, params_entry)) || Z_TYPE(equal) != IS_TRUE) {
@@ -327,7 +332,7 @@ PHP_METHOD(HttpQueryString, __construct)
        zval *params = NULL;
        zend_error_handling zeh;
        
-       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &params), invalid_arg, return);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &params), invalid_arg, return);
 
        zend_replace_error_handling(EH_THROW, php_http_exception_bad_querystring_class_entry, &zeh);
        php_http_querystring_set(getThis(), params, 0);
@@ -338,45 +343,39 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_getGlobalInstance, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, getGlobalInstance)
 {
-       zval *instance;
+       zval *instance, *_GET;
        zend_string *zs;
 
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 
        zs = zend_string_init(ZEND_STRL("instance"), 0);
-       instance = zend_std_get_static_property(php_http_querystring_class_entry, zs, 0, NULL);
+       instance = zend_std_get_static_property(php_http_querystring_class_entry, zs, 0);
        zend_string_release(zs);
 
-       if (Z_TYPE_P(instance) != IS_OBJECT) {
-               zval *_GET = NULL;
-
-               if ((_GET = php_http_env_get_superglobal(ZEND_STRL("_GET")))) {
-                       zval new_instance;
+       if (Z_TYPE_P(instance) == IS_OBJECT) {
+               RETVAL_ZVAL(instance, 1, 0);
+       } else if ((_GET = php_http_env_get_superglobal(ZEND_STRL("_GET")))) {
+               ZVAL_OBJ(return_value, php_http_querystring_object_new(php_http_querystring_class_entry));
 
-                       ZVAL_OBJ(&new_instance, php_http_querystring_object_new(php_http_querystring_class_entry));
+               ZVAL_MAKE_REF(_GET);
+               zend_update_property(php_http_querystring_class_entry, return_value, ZEND_STRL("queryArray"), _GET);
 
-                       ZVAL_MAKE_REF(_GET);
-                       zend_update_property(php_http_querystring_class_entry, &new_instance, ZEND_STRL("queryArray"), _GET);
-
-                       zend_update_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), instance);
-                       instance = &new_instance;
-               } else {
-                       php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array", NULL);
-               }
+               zend_update_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), return_value);
+       } else {
+               php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array", NULL);
        }
 
-       RETVAL_ZVAL_FAST(instance);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_getIterator, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, getIterator)
 {
-       zval *qa;
+       zval qa_tmp, *qa;
 
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
 
        object_init_ex(return_value, spl_ce_RecursiveArrayIterator);
        zend_call_method_with_1_params(return_value, spl_ce_RecursiveArrayIterator, NULL, "__construct", NULL, qa);
@@ -396,14 +395,14 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_toArray, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, toArray)
 {
-       zval *zqa;
+       zval zqa_tmp, *zqa;
 
        if (SUCCESS != zend_parse_parameters_none()) {
                return;
        }
 
-       zqa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
-       RETURN_ZVAL_FAST(zqa);
+       zqa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &zqa_tmp);
+       RETURN_ZVAL(zqa, 1, 0);
 }
 
 ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_get, 0, 0, 0)
@@ -473,13 +472,15 @@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_mod, 0, 0, 0)
 ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, mod)
 {
-       zval *params, *instance = getThis();
+       zval qa_tmp, *params, *instance = getThis();
        zend_error_handling zeh;
 
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", &params), invalid_arg, return);
        
        zend_replace_error_handling(EH_THROW, php_http_exception_bad_querystring_class_entry, &zeh);
        ZVAL_OBJ(return_value, Z_OBJ_HT_P(instance)->clone_obj(instance));
+       /* make sure we do not inherit the reference to _GET */
+       SEPARATE_ZVAL(zend_read_property(Z_OBJCE_P(return_value), return_value, ZEND_STRL("queryArray"), 0, &qa_tmp));
        php_http_querystring_set(return_value, params, QS_MERGE);
        zend_restore_error_handling(&zeh);
 }
@@ -516,12 +517,12 @@ PHP_METHOD(HttpQueryString, xlate)
 {
        char *ie, *oe;
        size_t ie_len, oe_len;
-       zval na, *qa;
+       zval na, qa_tmp, *qa;
 
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "ss", &ie, &ie_len, &oe, &oe_len), invalid_arg, return);
 
        array_init(&na);
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
        ZVAL_DEREF(qa);
        convert_to_array(qa);
 
@@ -531,7 +532,7 @@ PHP_METHOD(HttpQueryString, xlate)
        );
 
        php_http_querystring_set(getThis(), &na, 0);
-       RETVAL_ZVAL_FAST(getThis());
+       RETVAL_ZVAL(getThis(), 1, 0);
        
        zval_ptr_dtor(&na);
 }
@@ -571,18 +572,18 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetGet)
 {
        zend_string *offset;
-       zval *value, *qa;
+       zval *value, qa_tmp, *qa;
 
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) {
                return;
        }
        
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
        ZVAL_DEREF(qa);
 
        if (Z_TYPE_P(qa) == IS_ARRAY) {
                if ((value = zend_symtable_find(Z_ARRVAL_P(qa), offset))) {
-                       RETVAL_ZVAL_FAST(value);
+                       RETVAL_ZVAL(value, 1, 0);
                }
        }
 }
@@ -594,16 +595,21 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetSet)
 {
        zend_string *offset;
-       zval *value, param;
+       zval *value, param, znull;
        
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Sz", &offset, &value)) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "Sz", &offset, &value)) {
                return;
        }
 
-       array_init(&param);
+       array_init_size(&param, 1);
+       /* unset first */
+       ZVAL_NULL(&znull);
+       zend_symtable_update(Z_ARRVAL(param), offset, &znull);
+       php_http_querystring_set(getThis(), &param, QS_MERGE);
+       /* then update, else QS_MERGE would merge sub-arrrays */
        Z_TRY_ADDREF_P(value);
        zend_symtable_update(Z_ARRVAL(param), offset, value);
-       php_http_querystring_set(getThis(), &param, 0);
+       php_http_querystring_set(getThis(), &param, QS_MERGE);
        zval_ptr_dtor(&param);
 }
 
@@ -613,13 +619,13 @@ ZEND_END_ARG_INFO();
 PHP_METHOD(HttpQueryString, offsetExists)
 {
        zend_string *offset;
-       zval *value, *qa;
+       zval *value, qa_tmp, *qa;
 
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) {
                return;
        }
        
-       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0);
+       qa = zend_read_property(php_http_querystring_class_entry, getThis(), ZEND_STRL("queryArray"), 0, &qa_tmp);
        ZVAL_DEREF(qa);
 
        if (Z_TYPE_P(qa) == IS_ARRAY) {
@@ -638,7 +644,7 @@ PHP_METHOD(HttpQueryString, offsetUnset)
        zend_string *offset;
        zval param, znull;
        
-       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "S", &offset)) {
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "S", &offset)) {
                return;
        }