From fb79cfb853a437240978357d1c1a221ac9a04b33 Mon Sep 17 00:00:00 2001 From: Jay Smith Date: Wed, 9 Jul 2003 18:51:18 +0000 Subject: [PATCH] MFH: - fixed bug #24548 (get_browser() does not return platform) --- ext/standard/browscap.c | 132 ++++++++++++++++++++++------------------ 1 file changed, 74 insertions(+), 58 deletions(-) diff --git a/ext/standard/browscap.c b/ext/standard/browscap.c index c74fc13469..ed4da3e05e 100644 --- a/ext/standard/browscap.c +++ b/ext/standard/browscap.c @@ -34,9 +34,9 @@ static zval *current_section; static void browscap_entry_dtor(zval *pvalue) { - if (Z_TYPE_P(pvalue) == IS_OBJECT) { - zend_hash_destroy(Z_OBJPROP_P(pvalue)); - free(Z_OBJPROP_P(pvalue)); + if (Z_TYPE_P(pvalue) == IS_ARRAY) { + zend_hash_destroy(Z_ARRVAL_P(pvalue)); + free(Z_ARRVAL_P(pvalue)); } } @@ -47,19 +47,8 @@ static void convert_browscap_pattern(zval *pattern) register int i, j; char *t; - for (i=0; ivalue.ht = section_properties; + zend_hash_update(&browser_hash, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1)+1, (void *) ¤t_section, sizeof(zval *), NULL); Z_STRVAL_P(processed) = Z_STRVAL_P(arg1); Z_STRLEN_P(processed) = Z_STRLEN_P(arg1); Z_TYPE_P(processed) = IS_STRING; + Z_STRVAL_P(unprocessed) = Z_STRVAL_P(arg1); + Z_STRLEN_P(unprocessed) = Z_STRLEN_P(arg1); + Z_TYPE_P(unprocessed) = IS_STRING; + Z_STRVAL_P(unprocessed) = zend_strndup(Z_STRVAL_P(unprocessed), Z_STRLEN_P(unprocessed)); + convert_browscap_pattern(processed); - zend_hash_update(section_properties, "browser_name_pattern", sizeof("browser_name_pattern"), (void *) &processed, sizeof(zval *), NULL); + zend_hash_update(section_properties, "browser_name_regex", sizeof("browser_name_regex"), (void *) &processed, sizeof(zval *), NULL); + zend_hash_update(section_properties, "browser_name_pattern", sizeof("browser_name_pattern"), (void *) &unprocessed, sizeof(zval *), NULL); } break; } @@ -150,7 +146,8 @@ PHP_MINIT_FUNCTION(browscap) char *browscap = INI_STR("browscap"); if (browscap && browscap[0]) { - zend_file_handle fh = {0}; + zend_file_handle fh; + memset(&fh, 0, sizeof(fh)); if (zend_hash_init(&browser_hash, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1)==FAILURE) { return FAILURE; @@ -177,7 +174,6 @@ PHP_MINIT_FUNCTION(browscap) PHP_MSHUTDOWN_FUNCTION(browscap) { char *browscap = INI_STR("browscap"); - if (browscap && browscap[0]) { zend_hash_destroy(&browser_hash); } @@ -189,16 +185,24 @@ PHP_MSHUTDOWN_FUNCTION(browscap) */ static int browser_reg_compare(zval **browser, int num_args, va_list args, zend_hash_key *key) { - zval **browser_name; + zval **browser_name, **current; regex_t r; char *lookup_browser_name = va_arg(args, char *); zval **found_browser_entry = va_arg(args, zval **); - if (*found_browser_entry) { /* already found */ + if (zend_hash_find(Z_ARRVAL_PP(browser), "browser_name_regex", sizeof("browser_name_regex"), (void **) &browser_name) == FAILURE) { return 0; } - if(zend_hash_find(Z_OBJPROP_PP(browser), "browser_name_pattern", sizeof("browser_name_pattern"), (void **) &browser_name) == FAILURE) { - return 0; + + if (*found_browser_entry) { + /* If we've found a possible browser, check it's length. Longer user + agent strings are assumed to be more precise, so use them. */ + if (zend_hash_find(Z_ARRVAL_PP(found_browser_entry), "browser_name_regex", sizeof("browser_name_regex"), (void**) ¤t) == FAILURE) { + return 0; + } + else if (Z_STRLEN_PP(current) > Z_STRLEN_PP(browser_name)) { + return 0; + } } if (regcomp(&r, Z_STRVAL_PP(browser_name), REG_NOSUB)!=0) { return 0; @@ -211,62 +215,74 @@ static int browser_reg_compare(zval **browser, int num_args, va_list args, zend_ } /* }}} */ -/* {{{ proto object get_browser(string browser_name) - Get information about the capabilities of a browser */ +/* {{{ proto mixed get_browser([string browser_name [, bool return_array]]) + Get information about the capabilities of a browser. If browser_name is omitted + or null, HTTP_USER_AGENT is used. Returns an object by default; if return_array + is true, returns an array. */ PHP_FUNCTION(get_browser) { - zval **agent_name, **agent; + zval **agent_name = NULL, **agent, **retarr; zval *found_browser_entry, *tmp_copy; char *lookup_browser_name; + zend_bool return_array = 0; char *browscap = INI_STR("browscap"); if (!browscap || !browscap[0]) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "browscap ini directive not set."); RETURN_FALSE; } - - switch(ZEND_NUM_ARGS()) { - case 0: - if (!PG(http_globals)[TRACK_VARS_SERVER] - || zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_USER_AGENT", sizeof("HTTP_USER_AGENT"), (void **) &agent_name)==FAILURE) { - zend_error(E_WARNING, "HTTP_USER_AGENT variable is not set, cannot determine user agent name"); - RETURN_FALSE; - } - break; - case 1: - if (zend_get_parameters_ex(1, &agent_name)==FAILURE) { - RETURN_FALSE; - } - break; - default: - WRONG_PARAM_COUNT; - break; + + if (ZEND_NUM_ARGS() > 2 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &agent_name, &retarr) == FAILURE) { + ZEND_WRONG_PARAM_COUNT(); } + if (agent_name == NULL || Z_TYPE_PP(agent_name) == IS_NULL) { + if (!PG(http_globals)[TRACK_VARS_SERVER] + || zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_USER_AGENT", sizeof("HTTP_USER_AGENT"), (void **) &agent_name)==FAILURE) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "HTTP_USER_AGENT variable is not set, cannot determine user agent name"); + RETURN_FALSE; + } + } + convert_to_string_ex(agent_name); + if (ZEND_NUM_ARGS() == 2) { + convert_to_boolean_ex(retarr); + return_array = Z_LVAL_PP(retarr); + } + if (zend_hash_find(&browser_hash, Z_STRVAL_PP(agent_name), Z_STRLEN_PP(agent_name)+1, (void **) &agent)==FAILURE) { lookup_browser_name = Z_STRVAL_PP(agent_name); found_browser_entry = NULL; zend_hash_apply_with_arguments(&browser_hash, (apply_func_args_t) browser_reg_compare, 2, lookup_browser_name, &found_browser_entry); - + if (found_browser_entry) { agent = &found_browser_entry; } else if (zend_hash_find(&browser_hash, DEFAULT_SECTION_NAME, sizeof(DEFAULT_SECTION_NAME), (void **) &agent)==FAILURE) { RETURN_FALSE; } } - - object_init(return_value); - zend_hash_copy(Z_OBJPROP_P(return_value), Z_OBJPROP_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *)); - - while (zend_hash_find(Z_OBJPROP_PP(agent), "parent", sizeof("parent"), (void **) &agent_name)==SUCCESS) { + if (return_array) { + array_init(return_value); + zend_hash_copy(Z_ARRVAL_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *)); + } + else { + object_init(return_value); + zend_hash_copy(Z_OBJPROP_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *)); + } + + while (zend_hash_find(Z_ARRVAL_PP(agent), "parent", sizeof("parent"), (void **) &agent_name)==SUCCESS) { if (zend_hash_find(&browser_hash, Z_STRVAL_PP(agent_name), Z_STRLEN_PP(agent_name)+1, (void **)&agent)==FAILURE) { break; } - - zend_hash_merge(Z_OBJPROP_P(return_value), Z_OBJPROP_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0); + + if (return_array) { + zend_hash_merge(Z_ARRVAL_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0); + } + else { + zend_hash_merge(Z_OBJPROP_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *), 0); + } } } /* }}} */ -- 2.40.0