X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=http_querystring_object.c;h=4da97b376007e00a18ae37dcb742afc5c4a8b76a;hp=e6daf67e1b303d6eef894127481cceebaf2cfde5;hb=d650022122969644c33bd03a552af1a631429e16;hpb=7489cd801f25fd08bcba22545fd4c89223709ebe diff --git a/http_querystring_object.c b/http_querystring_object.c index e6daf67..4da97b3 100644 --- a/http_querystring_object.c +++ b/http_querystring_object.c @@ -12,10 +12,12 @@ /* $Id$ */ +#define HTTP_WANT_SAPI #include "php_http.h" #ifdef ZEND_ENGINE_2 +#include "php_variables.h" #include "zend_interfaces.h" #include "php_http_api.h" @@ -23,37 +25,40 @@ #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(getInstance, 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_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) @@ -65,12 +70,24 @@ static inline void _http_querystring_object_declare_default_properties(TSRMLS_D) 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) + +#ifndef WONKY HTTP_QUERYSTRING_ME(getInstance, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) +#endif + + HTTP_QUERYSTRING_GME(getBool, ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_GME(getInt, ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_GME(getFloat, ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_GME(getString, ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_GME(getArray, ZEND_ACC_PUBLIC) + HTTP_QUERYSTRING_GME(getObject, ZEND_ACC_PUBLIC) EMPTY_FUNCTION_ENTRY }; @@ -164,11 +181,11 @@ static inline void _http_querystring_update(zval *qarray, zval *qstring TSRMLS_D } } -#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) +#define http_querystring_modify_ex(a, k, l, v) _http_querystring_modify_ex((a), (k), (l), (v) TSRMLS_CC) +static inline int _http_querystring_modify_ex(zval *qarray, char *key, uint keylen, zval *data TSRMLS_DC) { if (Z_TYPE_P(data) == IS_NULL) { - if (SUCCESS != zend_hash_del(Z_ARRVAL_P(qarray), key, keylen + 1)) { + if (SUCCESS != zend_hash_del(Z_ARRVAL_P(qarray), key, keylen)) { return 0; } } else { @@ -178,6 +195,59 @@ static inline int _http_querystring_modify_array(zval *qarray, char *key, uint k return 1; } +#define http_querystring_modify_array(q, a) _http_querystring_modify_array((q), (a) TSRMLS_CC) +static inline int _http_querystring_modify_array(zval *qarray, zval *array TSRMLS_DC) +{ + zval **value; + HashPosition pos; + char *key = NULL; + uint keylen = 0; + ulong idx = 0; + int rv = 0; + + FOREACH_KEYLENVAL(pos, array, key, keylen, idx, value) { + if (key) { + if (http_querystring_modify_ex(qarray, key, keylen, *value)) { + rv = 1; + } + } else { + keylen = spprintf(&key, 0, "%lu", idx); + if (http_querystring_modify_ex(qarray, key, keylen, *value)) { + rv = 1; + } + efree(key); + } + key = NULL; + } + + return rv; +} + +#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) +{ + if ((Z_TYPE_P(params) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(params), http_querystring_object_ce TSRMLS_CC)) { + return http_querystring_modify_array(qarray, GET_PROP_EX(params, queryArray)); + } else if (Z_TYPE_P(params) == IS_ARRAY) { + return http_querystring_modify_array(qarray, params); + } 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; + } +} + +#ifndef WONKY #define http_querystring_instantiate(g) _http_querystring_instantiate((g) TSRMLS_CC) static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC) { @@ -195,8 +265,29 @@ static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC) return zobj; } +#endif + +#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))) { + RETVAL_ZVAL(*arrval, 1, 0); + + if (type) { + convert_to_type(type, return_value); + } + + if (del && (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), name, name_len + 1))) { + http_querystring_update(qarray, GET_PROP(queryString)); + } + } else if(defval) { + RETURN_ZVAL(defval, 1, 0); + } +} -/* {{{ proto void HttpQueryString::__construct([bool global = true]) +/* {{{ proto 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. @@ -204,10 +295,10 @@ static inline zval *_http_querystring_instantiate(zend_bool global TSRMLS_DC) 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 (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)) && (Z_TYPE_PP(_SERVER) == IS_ARRAY) && @@ -233,29 +324,49 @@ 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. @@ -273,48 +384,22 @@ PHP_METHOD(HttpQueryString, get) if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|szzb", &name, &name_len, &ztype, &defval, &del)) { if (name && name_len) { - 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))) { - RETVAL_ZVAL(*arrval, 1, 0); - - if (ztype) { - 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])) - { - 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; - } + if (ztype) { + 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])) + { + 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; } - if (type) { - convert_to_type(type, return_value); - } - } - - if (del && (SUCCESS == zend_hash_del(Z_ARRVAL_P(qarray), name, name_len + 1))) { - http_querystring_update(qarray, GET_PROP(queryString)); } - } else if(defval) { - RETURN_ZVAL(defval, 1, 0); } + http_querystring_get(getThis(), type, name, name_len, defval, del, return_value); } else { RETURN_PROP(queryString); } @@ -322,97 +407,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 { @@ -421,6 +428,7 @@ PHP_METHOD(HttpQueryString, mod) } /* }}} */ +#ifndef WONKY /* {{{ proto HttpQueryString HttpQueryString::getInstance([bool global = true]) * * Get a single instance (differentiates between the global setting). @@ -457,6 +465,27 @@ PHP_METHOD(HttpQueryString, getInstance) SET_EH_NORMAL(); } /* }}} */ +#endif + +/* {{{ Getters by type */ +#define HTTP_QUERYSTRING_GETTER(method, TYPE) \ +PHP_METHOD(HttpQueryString, method) \ +{ \ + char *name; \ + int name_len; \ + zval *defval = NULL; \ + 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); +HTTP_QUERYSTRING_GETTER(getFloat, IS_DOUBLE); +HTTP_QUERYSTRING_GETTER(getString, IS_STRING); +HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY); +HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT); +/* }}} */ #endif /* ZEND_ENGINE_2 */