X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_querystring_object.c;h=55d355d8e480bc6840de2d2c74aaf11c61717020;hp=a3177a376b0fba5da8ed25b8bc06cbccdcea63f4;hb=669d2e6a8bdc642b6b52693f4593f199ddd7e8d2;hpb=2dd36fa14a60e5bde382f0035810520db25a3ea2 diff --git a/http_querystring_object.c b/http_querystring_object.c index a3177a3..55d355d 100644 --- a/http_querystring_object.c +++ b/http_querystring_object.c @@ -12,73 +12,84 @@ /* $Id$ */ +#define HTTP_WANT_SAPI #include "php_http.h" #ifdef ZEND_ENGINE_2 +#include "php_variables.h" #include "zend_interfaces.h" +#ifdef HAVE_ICONV +# undef PHP_ATOM_INC +# include "ext/iconv/php_iconv.h" +# include "ext/standard/url.h" +#endif + #include "php_http_api.h" #include "php_http_url_api.h" #include "php_http_querystring_object.h" #include "php_http_exception_object.h" -#define HTTP_BEGIN_ARGS(method, ret_ref, req_args) HTTP_BEGIN_ARGS_EX(HttpQueryString, method, ret_ref, req_args) -#define HTTP_EMPTY_ARGS(method, ret_ref) HTTP_EMPTY_ARGS_EX(HttpQueryString, method, ret_ref) +#define HTTP_BEGIN_ARGS(method, req_args) HTTP_BEGIN_ARGS_EX(HttpQueryString, method, 0, req_args) +#define HTTP_EMPTY_ARGS(method) HTTP_EMPTY_ARGS_EX(HttpQueryString, method, 0) #define HTTP_QUERYSTRING_ME(method, visibility) PHP_ME(HttpQueryString, method, HTTP_ARGS(HttpQueryString, method), visibility) #define HTTP_QUERYSTRING_GME(method, visibility) PHP_ME(HttpQueryString, method, HTTP_ARGS(HttpQueryString, __getter), visibility) -HTTP_BEGIN_ARGS(__construct, 0, 0) +HTTP_BEGIN_ARGS(__construct, 0) HTTP_ARG_VAL(global, 0) + HTTP_ARG_VAL(params, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(getInstance, 0, 0) +#ifndef WONKY +HTTP_BEGIN_ARGS(singleton, 0) HTTP_ARG_VAL(global, 0) HTTP_END_ARGS; +#endif -HTTP_EMPTY_ARGS(__toString, 0); +HTTP_EMPTY_ARGS(toArray); +HTTP_EMPTY_ARGS(toString); -HTTP_BEGIN_ARGS(get, 0, 0) +HTTP_BEGIN_ARGS(get, 0) HTTP_ARG_VAL(name, 0) HTTP_ARG_VAL(type, 0) HTTP_ARG_VAL(defval, 0) HTTP_ARG_VAL(delete, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(set, 0, 2) - HTTP_ARG_VAL(name, 0) - HTTP_ARG_VAL(value, 0) -HTTP_END_ARGS; - -HTTP_BEGIN_ARGS(del, 0, 1) +HTTP_BEGIN_ARGS(set, 1) HTTP_ARG_VAL(params, 0) HTTP_END_ARGS; -HTTP_BEGIN_ARGS(mod, 0, 1) - HTTP_ARG_VAL(params, 0) -HTTP_END_ARGS; - -HTTP_BEGIN_ARGS(__getter, 0, 1) +HTTP_BEGIN_ARGS(__getter, 1) HTTP_ARG_VAL(name, 0) HTTP_ARG_VAL(defval, 0) HTTP_ARG_VAL(delete, 0) HTTP_END_ARGS; -#define http_querystring_object_declare_default_properties() _http_querystring_object_declare_default_properties(TSRMLS_C) -static inline void _http_querystring_object_declare_default_properties(TSRMLS_D); +#ifdef HAVE_ICONV +HTTP_BEGIN_ARGS(xlate, 2) + HTTP_ARG_VAL(from_encoding, 0) + HTTP_ARG_VAL(to_encoding, 0) +HTTP_END_ARGS; +#endif + +HTTP_EMPTY_ARGS(serialize); +HTTP_BEGIN_ARGS(unserialize, 1) + HTTP_ARG_VAL(serialized, 0) +HTTP_END_ARGS; -#define GET_STATIC_PROP(n) *GET_STATIC_PROP_EX(http_querystring_object_ce, n) -#define SET_STATIC_PROP(n, v) SET_STATIC_PROP_EX(http_querystring_object_ce, n, v) #define OBJ_PROP_CE http_querystring_object_ce zend_class_entry *http_querystring_object_ce; zend_function_entry http_querystring_object_fe[] = { HTTP_QUERYSTRING_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL) - HTTP_QUERYSTRING_ME(__toString, ZEND_ACC_PUBLIC) + + HTTP_QUERYSTRING_ME(toArray, ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_ME(toString, ZEND_ACC_PUBLIC) + ZEND_MALIAS(HttpQueryString, __toString, toString, HTTP_ARGS(HttpQueryString, toString), ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_ME(get, ZEND_ACC_PUBLIC) HTTP_QUERYSTRING_ME(set, ZEND_ACC_PUBLIC) - HTTP_QUERYSTRING_ME(del, ZEND_ACC_PUBLIC) - HTTP_QUERYSTRING_ME(mod, ZEND_ACC_PUBLIC) - HTTP_QUERYSTRING_ME(getInstance, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) HTTP_QUERYSTRING_GME(getBool, ZEND_ACC_PUBLIC) HTTP_QUERYSTRING_GME(getInt, ZEND_ACC_PUBLIC) @@ -87,6 +98,17 @@ zend_function_entry http_querystring_object_fe[] = { HTTP_QUERYSTRING_GME(getArray, ZEND_ACC_PUBLIC) HTTP_QUERYSTRING_GME(getObject, ZEND_ACC_PUBLIC) +#ifndef WONKY + HTTP_QUERYSTRING_ME(singleton, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) +#endif +#ifdef HAVE_ICONV + HTTP_QUERYSTRING_ME(xlate, ZEND_ACC_PUBLIC) +#endif + + /* Implements Serializable */ + HTTP_QUERYSTRING_ME(serialize, ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_ME(unserialize, ZEND_ACC_PUBLIC) + EMPTY_FUNCTION_ENTRY }; static zend_object_handlers http_querystring_object_handlers; @@ -95,6 +117,23 @@ PHP_MINIT_FUNCTION(http_querystring_object) { HTTP_REGISTER_CLASS_EX(HttpQueryString, http_querystring_object, NULL, 0); +#ifndef WONKY + zend_class_implements(http_querystring_object_ce TSRMLS_CC, 1, zend_ce_serializable); +#endif + + DCL_STATIC_PROP_N(PRIVATE, instance); + DCL_PROP_N(PRIVATE, queryArray); + DCL_PROP(PRIVATE, string, queryString, ""); + +#ifndef WONKY + DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL); + DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT); + DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT); + DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING); + DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY); + DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT); +#endif + HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL); HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_INT", HTTP_QUERYSTRING_TYPE_INT); HTTP_LONG_CONSTANT("HTTP_QUERYSTRING_TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT); @@ -132,25 +171,6 @@ zend_object_value _http_querystring_object_new_ex(zend_class_entry *ce, http_que return ov; } -static inline void _http_querystring_object_declare_default_properties(TSRMLS_D) -{ - zend_class_entry *ce = http_querystring_object_ce; - - DCL_STATIC_PROP_N(PRIVATE, instance); - - DCL_PROP_N(PRIVATE, queryArray); - DCL_PROP(PRIVATE, string, queryString, ""); - -#ifndef WONKY - DCL_CONST(long, "TYPE_BOOL", HTTP_QUERYSTRING_TYPE_BOOL); - DCL_CONST(long, "TYPE_INT", HTTP_QUERYSTRING_TYPE_INT); - DCL_CONST(long, "TYPE_FLOAT", HTTP_QUERYSTRING_TYPE_FLOAT); - DCL_CONST(long, "TYPE_STRING", HTTP_QUERYSTRING_TYPE_STRING); - DCL_CONST(long, "TYPE_ARRAY", HTTP_QUERYSTRING_TYPE_ARRAY); - DCL_CONST(long, "TYPE_OBJECT", HTTP_QUERYSTRING_TYPE_OBJECT); -#endif -} - void _http_querystring_object_free(zend_object *object TSRMLS_DC) { http_querystring_object *o = (http_querystring_object *) object; @@ -162,7 +182,94 @@ void _http_querystring_object_free(zend_object *object TSRMLS_DC) efree(o); } +/* {{{ querystring helpers */ #define http_querystring_update(qa, qs) _http_querystring_update((qa), (qs) TSRMLS_CC) +static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_DC); +#define http_querystring_modify_array_ex(q, k, kl, pe) _http_querystring_modify_array_ex((q), (k), (kl), (pe) TSRMLS_CC) +static inline int _http_querystring_modify_array_ex(zval *qarray, char *key, int keylen, zval *params_entry TSRMLS_DC); +#define http_querystring_modify_array(q, p) _http_querystring_modify_array((q), (p) TSRMLS_CC) +static inline int _http_querystring_modify_array(zval *qarray, zval *params TSRMLS_DC); +#define http_querystring_modify(q, p) _http_querystring_modify((q), (p) TSRMLS_CC) +static inline int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC); +#define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC) +static inline void _http_querystring_get(zval *this_ptr, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value TSRMLS_DC); +#ifdef HAVE_ICONV +#define http_querystring_xlate(a, p, ie, oe) _http_querystring_xlate((a), (p), (ie), (oe) TSRMLS_CC) +static inline int _http_querystring_xlate(zval *array, zval *param, const char *ie, const char *oe TSRMLS_DC) +{ + HashPosition pos; + zval **entry = NULL; + char *xlate_str = NULL, *xkey, *kstr = NULL; + size_t xlate_len = 0, xlen; + uint klen = 0; + ulong kidx = 0; + + FOREACH_KEYLENVAL(pos, param, kstr, klen, kidx, entry) { + if (kstr) { + if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(kstr, klen-1, &xkey, &xlen, oe, ie)) { + http_error_ex(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to convert '%.*s' from '%s' to '%s'", klen-1, kstr, ie, oe); + return FAILURE; + } + } + + if (Z_TYPE_PP(entry) == IS_STRING) { + if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry), &xlate_str, &xlate_len, oe, ie)) { + if (kstr) { + efree(xkey); + } + http_error_ex(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_PP(entry), Z_STRVAL_PP(entry), ie, oe); + return FAILURE; + } + if (kstr) { + add_assoc_stringl_ex(array, xkey, xlen+1, xlate_str, xlate_len, 0); + } else { + add_index_stringl(array, kidx, xlate_str, xlate_len, 0); + } + } else if (Z_TYPE_PP(entry) == IS_ARRAY) { + zval *subarray; + + MAKE_STD_ZVAL(subarray); + array_init(subarray); + if (kstr) { + add_assoc_zval_ex(array, xkey, xlen+1, subarray); + } else { + add_index_zval(array, kidx, subarray); + } + if (SUCCESS != http_querystring_xlate(subarray, *entry, ie, oe)) { + if (kstr) { + efree(xkey); + } + return FAILURE; + } + } + + if (kstr) { + kstr = NULL; + efree(xkey); + } + } + return SUCCESS; +} +#endif /* HAVE_ICONV */ +#ifndef WONKY +#define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC) +static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC) +{ + zval *zobj, *zglobal; + + MAKE_STD_ZVAL(zglobal); + ZVAL_BOOL(zglobal, global); + + MAKE_STD_ZVAL(zobj); + Z_TYPE_P(zobj) = IS_OBJECT; + Z_OBJVAL_P(zobj) = http_querystring_object_new(http_querystring_object_ce); + zend_call_method_with_1_params(&zobj, Z_OBJCE_P(zobj), NULL, "__construct", NULL, zglobal); + + zval_ptr_dtor(&zglobal); + + return zobj; +} +#endif /* WONKY */ static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_DC) { char *s = NULL; @@ -178,45 +285,92 @@ static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_D http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Failed to update query string"); } } - -#define http_querystring_modify_array(a, k, l, v) _http_querystring_modify_array((a), (k), (l), (v) TSRMLS_CC) -static inline int _http_querystring_modify_array(zval *qarray, char *key, uint keylen, zval *data TSRMLS_DC) +static inline int _http_querystring_modify_array_ex(zval *qarray, char *key, int keylen, zval *params_entry TSRMLS_DC) { - if (Z_TYPE_P(data) == IS_NULL) { - if (SUCCESS != zend_hash_del(Z_ARRVAL_P(qarray), key, keylen + 1)) { + zval **qarray_entry; + + /* delete */ + if (Z_TYPE_P(params_entry) == IS_NULL) { + return (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), key, keylen)); + } + + /* update */ + if (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), key, keylen, (void *) &qarray_entry)) { + zval equal; + + /* recursive */ + if (Z_TYPE_P(params_entry) == IS_ARRAY) { + return http_querystring_modify_array(*qarray_entry, params_entry); + } + /* equal */ + if ((SUCCESS == is_equal_function(&equal, *qarray_entry, params_entry TSRMLS_CC)) && Z_BVAL(equal)) { return 0; } - } else { - ZVAL_ADDREF(data); - add_assoc_zval(qarray, key, data); } + + /* add */ + ZVAL_ADDREF(params_entry); + add_assoc_zval_ex(qarray, key, keylen, params_entry); return 1; } - -#define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC) -static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC) +static inline int _http_querystring_modify_array(zval *qarray, zval *params TSRMLS_DC) { - zval *zobj, *zglobal; - - MAKE_STD_ZVAL(zglobal); - ZVAL_BOOL(zglobal, global); + int rv = 0; + char *key; + uint keylen; + ulong idx; + HashPosition pos; + zval **params_entry; - MAKE_STD_ZVAL(zobj); - Z_TYPE_P(zobj) = IS_OBJECT; - Z_OBJVAL_P(zobj) = http_querystring_object_new(http_querystring_object_ce); - zend_call_method_with_1_params(&zobj, Z_OBJCE_P(zobj), NULL, "__construct", NULL, zglobal); - - zval_ptr_dtor(&zglobal); + FOREACH_KEYLENVAL(pos, params, key, keylen, idx, params_entry) { + if (key) { + if (http_querystring_modify_array_ex(qarray, key, keylen, *params_entry)) { + rv = 1; + } + } else { + keylen = spprintf(&key, 0, "%lu", idx); + if (http_querystring_modify_array_ex(qarray, key, keylen, *params_entry)) { + rv = 1; + } + efree(key); + } + key = NULL; + } - return zobj; + return rv; +} +static inline int _http_querystring_modify(zval *qarray, zval *params TSRMLS_DC) +{ + if (Z_TYPE_P(params) == IS_ARRAY) { + return http_querystring_modify_array(qarray, params); + } else if (Z_TYPE_P(params) == IS_OBJECT) { + if (!instanceof_function(Z_OBJCE_P(params), http_querystring_object_ce TSRMLS_CC)) { + zval temp_array; + INIT_ZARR(temp_array, HASH_OF(params)); + return http_querystring_modify_array(qarray, &temp_array); + } + return http_querystring_modify_array(qarray, GET_PROP_EX(params, queryArray)); + } else { + int rv; + zval array; + + INIT_PZVAL(&array); + array_init(&array); + + ZVAL_ADDREF(params); + convert_to_string_ex(¶ms); + sapi_module.treat_data(PARSE_STRING, estrdup(Z_STRVAL_P(params)), &array TSRMLS_CC); + zval_ptr_dtor(¶ms); + rv = http_querystring_modify_array(qarray, &array); + zval_dtor(&array); + return rv; + } } - -#define http_querystring_get(o, t, n, l, def, del, r) _http_querystring_get((o), (t), (n), (l), (def), (del), (r) TSRMLS_CC) static inline void _http_querystring_get(zval *this_ptr, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value TSRMLS_DC) { zval **arrval, *qarray = GET_PROP(queryArray); - if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void **) &arrval))) { + if ((Z_TYPE_P(qarray) == IS_ARRAY) && (SUCCESS == zend_hash_find(Z_ARRVAL_P(qarray), name, name_len + 1, (void *) &arrval))) { RETVAL_ZVAL(*arrval, 1, 0); if (type) { @@ -230,8 +384,9 @@ static inline void _http_querystring_get(zval *this_ptr, int type, char *name, u RETURN_ZVAL(defval, 1, 0); } } +/* }}} */ -/* {{{ proto void HttpQueryString::__construct([bool global = true]) +/* {{{ proto final void HttpQueryString::__construct([bool global = true[, mixed add]) * * Creates a new HttpQueryString object instance. * Operates on and modifies $_GET and $_SERVER['QUERY_STRING'] if global is TRUE. @@ -239,18 +394,25 @@ static inline void _http_querystring_get(zval *this_ptr, int type, char *name, u PHP_METHOD(HttpQueryString, __construct) { zend_bool global = 1; - zval *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING; + zval *params = NULL, *qarray = NULL, *qstring = NULL, **_GET, **_SERVER, **QUERY_STRING; SET_EH_THROW_HTTP(); - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &global)) { + if (!sapi_module.treat_data) { + http_error(HE_ERROR, HTTP_E_QUERYSTRING, "The SAPI does not have a treat_data function registered"); + } else if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|bz", &global, ¶ms)) { if (global) { - if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void **) &_SERVER)) && +#ifdef ZEND_ENGINE_2 + zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC); +#endif + if ( (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &_SERVER)) && (Z_TYPE_PP(_SERVER) == IS_ARRAY) && - (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void **) &QUERY_STRING))) { + (SUCCESS == zend_hash_find(Z_ARRVAL_PP(_SERVER), "QUERY_STRING", sizeof("QUERY_STRING"), (void *) &QUERY_STRING))) { qstring = *QUERY_STRING; - - if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void **) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) { +#ifdef ZEND_ENGINE_2 + zend_is_auto_global("_GET", lenof("_GET") TSRMLS_CC); +#endif + if ((SUCCESS == zend_hash_find(&EG(symbol_table), "_GET", sizeof("_GET"), (void *) &_GET)) && (Z_TYPE_PP(_GET) == IS_ARRAY)) { qarray = *_GET; } else { http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Could not acquire reference to superglobal GET array"); @@ -268,29 +430,50 @@ PHP_METHOD(HttpQueryString, __construct) SET_PROP(queryString, qstring); GET_PROP(queryArray)->is_ref = 1; GET_PROP(queryString)->is_ref = 1; + + if (params) { + http_querystring_modify(GET_PROP(queryArray), params); + } + http_querystring_update(GET_PROP(queryArray), GET_PROP(queryString)); } } else { qarray = ecalloc(1, sizeof(zval)); array_init(qarray); + SET_PROP(queryArray, qarray); UPD_STRL(queryString, "", 0); + + if (params && http_querystring_modify(qarray, params)) { + http_querystring_update(qarray, GET_PROP(queryString)); + } } } SET_EH_NORMAL(); } /* }}} */ -/* {{{ proto string HttpQueryString::__toString() +/* {{{ proto string HttpQueryString::toString() * * Returns the string representation. */ -PHP_METHOD(HttpQueryString, __toString) +PHP_METHOD(HttpQueryString, toString) { NO_ARGS; RETURN_PROP(queryString); } /* }}} */ +/* {{{ proto array HttpQueryString::toArray() + * + * Returns the array representation. + */ +PHP_METHOD(HttpQueryString, toArray) +{ + NO_ARGS; + RETURN_PROP(queryArray); +} +/* }}} */ + /* {{{ proto mixed HttpQueryString::get([string key[, mixed type = 0[, mixed defval = NULL[, bool delete = false]]]]) * * Get (part of) the query string. @@ -312,26 +495,20 @@ PHP_METHOD(HttpQueryString, get) if (Z_TYPE_P(ztype) == IS_LONG) { type = Z_LVAL_P(ztype); } else if(Z_TYPE_P(ztype) == IS_STRING) { - switch (tolower(Z_STRVAL_P(ztype)[0])) + switch (Z_STRVAL_P(ztype)[0]) { - case 'b': - type = HTTP_QUERYSTRING_TYPE_BOOL; - break; - case 'i': - type = HTTP_QUERYSTRING_TYPE_INT; - break; - case 'f': - type = HTTP_QUERYSTRING_TYPE_FLOAT; - break; - case 's': - type = HTTP_QUERYSTRING_TYPE_STRING; - break; - case 'a': - type = HTTP_QUERYSTRING_TYPE_ARRAY; - break; - case 'o': - type = HTTP_QUERYSTRING_TYPE_OBJECT; - break; + case 'B': + case 'b': type = HTTP_QUERYSTRING_TYPE_BOOL; break; + case 'I': + case 'i': type = HTTP_QUERYSTRING_TYPE_INT; break; + case 'F': + case 'f': type = HTTP_QUERYSTRING_TYPE_FLOAT; break; + case 'S': + case 's': type = HTTP_QUERYSTRING_TYPE_STRING; break; + case 'A': + case 'a': type = HTTP_QUERYSTRING_TYPE_ARRAY; break; + case 'O': + case 'o': type = HTTP_QUERYSTRING_TYPE_OBJECT; break; } } } @@ -343,97 +520,19 @@ PHP_METHOD(HttpQueryString, get) } /* }}} */ -/* {{{ proto string HttpQueryString::set(string name, mixed value) +/* {{{ proto string HttpQueryString::set(mixed params) * - * Set a query string entry. + * Set query string entry/entries. NULL values will unset the variable. */ PHP_METHOD(HttpQueryString, set) -{ - char *name; - int name_len; - zval *value; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name, &name_len, &value)) { - zval *qarray = GET_PROP(queryArray); - - if (http_querystring_modify_array(qarray, name, name_len, value)) { - http_querystring_update(qarray, GET_PROP(queryString)); - } - } - - IF_RETVAL_USED { - RETURN_PROP(queryString); - } -} -/* }}} */ - -/* {{{ proto string HttpQueryString::del(mixed param) - * - * Deletes entry/entries from the query string. - */ -PHP_METHOD(HttpQueryString, del) { zval *params; - + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", ¶ms)) { zval *qarray = GET_PROP(queryArray); - - if (Z_TYPE_P(qarray) == IS_ARRAY) { - if (Z_TYPE_P(params) == IS_ARRAY) { - HashPosition pos; - zval **name; - - FOREACH_VAL(pos, params, name) { - ZVAL_ADDREF(*name); - convert_to_string_ex(name); - zend_hash_del(Z_ARRVAL_P(qarray), Z_STRVAL_PP(name), Z_STRLEN_PP(name) + 1); - zval_ptr_dtor(name); - } - - http_querystring_update(qarray, GET_PROP(queryString)); - } else { - ZVAL_ADDREF(params); - convert_to_string_ex(¶ms); - if (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), Z_STRVAL_P(params), Z_STRLEN_P(params) + 1)) { - http_querystring_update(qarray, GET_PROP(queryString)); - } - zval_ptr_dtor(¶ms); - } - } - } - IF_RETVAL_USED { - RETURN_PROP(queryString); - } -} -/* }}} */ - -/* {{{ proto string HttpQueryString::mod(array params) - * - * Modifies the query string according to params. NULL values will unset the variable. - */ -PHP_METHOD(HttpQueryString, mod) -{ - zval *params; - - if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", ¶ms)) { - zval **value, *qarray = GET_PROP(queryArray); - HashPosition pos; - char *key = NULL; - uint keylen = 0; - ulong idx = 0; - - FOREACH_KEYLENVAL(pos, params, key, keylen, idx, value) { - if (key) { - http_querystring_modify_array(qarray, key, keylen, *value); - } else { - keylen = spprintf(&key, 0, "%lu", idx); - http_querystring_modify_array(qarray, key, keylen, *value); - efree(key); - } - key = NULL; + if (http_querystring_modify(qarray, params)) { + http_querystring_update(qarray, GET_PROP(queryString)); } - - http_querystring_update(qarray, GET_PROP(queryString)); } IF_RETVAL_USED { @@ -442,11 +541,12 @@ PHP_METHOD(HttpQueryString, mod) } /* }}} */ -/* {{{ proto HttpQueryString HttpQueryString::getInstance([bool global = true]) +#ifndef WONKY +/* {{{ proto static HttpQueryString HttpQueryString::singleton([bool global = true]) * * Get a single instance (differentiates between the global setting). */ -PHP_METHOD(HttpQueryString, getInstance) +PHP_METHOD(HttpQueryString, singleton) { zend_bool global = 1; zval *instance = GET_STATIC_PROP(instance); @@ -456,7 +556,7 @@ PHP_METHOD(HttpQueryString, getInstance) zval **zobj_ptr = NULL, *zobj = NULL; if (Z_TYPE_P(instance) == IS_ARRAY) { - if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void **) &zobj_ptr)) { + if (SUCCESS == zend_hash_index_find(Z_ARRVAL_P(instance), global, (void *) &zobj_ptr)) { RETVAL_ZVAL(*zobj_ptr, 1, 0); } else { zobj = http_querystring_instantiate(global); @@ -478,6 +578,7 @@ PHP_METHOD(HttpQueryString, getInstance) SET_EH_NORMAL(); } /* }}} */ +#endif /* {{{ Getters by type */ #define HTTP_QUERYSTRING_GETTER(method, TYPE) \ @@ -489,7 +590,7 @@ PHP_METHOD(HttpQueryString, method) \ zend_bool del = 0; \ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|zb", &name, &name_len, &defval, &del)) { \ http_querystring_get(getThis(), TYPE, name, name_len, defval, del, return_value); \ -} \ + } \ } HTTP_QUERYSTRING_GETTER(getBool, IS_BOOL); HTTP_QUERYSTRING_GETTER(getInt, IS_LONG); @@ -499,6 +600,77 @@ HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY); HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT); /* }}} */ +#ifdef HAVE_ICONV +/* {{{ proto bool HttpQueryString::xlate(string ie, string oe) + * + * Converts the query string from the source encoding ie to the target encoding oe. + * WARNING: Don't use any character set that can contain NUL bytes like UTF-16. + * + * Returns TRUE on success or FALSE on failure. + */ +PHP_METHOD(HttpQueryString, xlate) +{ + char *ie, *oe; + int ie_len, oe_len; + zval xa, *qa, *qs; + STATUS rs; + + if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss", &ie, &ie_len, &oe, &oe_len)) { + RETURN_FALSE; + } + + qa = GET_PROP(queryArray); + qs = GET_PROP(queryString); + INIT_PZVAL(&xa); + array_init(&xa); + + if (SUCCESS == (rs = http_querystring_xlate(&xa, qa, ie, oe))) { + zend_hash_clean(Z_ARRVAL_P(qa)); + zend_hash_copy(Z_ARRVAL_P(qa), Z_ARRVAL(xa), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *)); + http_querystring_update(qa, qs); + } + zval_dtor(&xa); + + RETURN_SUCCESS(rs); +} +/* }}} */ +#endif /* HAVE_ICONV */ + +/* {{{ proto string HttpQueryString::serialize() + * + * Implements Serializable. + */ +PHP_METHOD(HttpQueryString, serialize) +{ + NO_ARGS; + RETURN_PROP(queryString); +} +/* }}} */ + +/* {{{ proto void HttpQueryString::unserialize(string serialized) + * + * Implements Serializable. + */ +PHP_METHOD(HttpQueryString, unserialize) +{ + zval *serialized; + + SET_EH_THROW_HTTP(); + if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &serialized)) { + if (Z_TYPE_P(serialized) == IS_STRING) { + zval *qa = GET_PROP(queryArray); + + zend_hash_clean(Z_ARRVAL_P(qa)); + http_querystring_modify(qa, serialized); + http_querystring_update(qa, GET_PROP(queryString)); + } else { + http_error(HE_WARNING, HTTP_E_QUERYSTRING, "Expected a string as parameter"); + } + } + SET_EH_NORMAL(); +} +/* }}} */ + #endif /* ZEND_ENGINE_2 */ /*