1999-04-17 08:37:12 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| PHP Version 4 |
|
1999-04-17 08:37:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2001-12-11 23:32:16 +08:00
|
|
|
| Copyright (c) 1997-2002 The PHP Group |
|
1999-04-17 08:37:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2000-05-18 23:34:45 +08:00
|
|
|
| This source file is subject to version 2.02 of the PHP license, |
|
1999-07-16 21:13:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
|
|
|
| available at through the world-wide-web at |
|
2000-05-18 23:34:45 +08:00
|
|
|
| http://www.php.net/license/2_02.txt. |
|
1999-07-16 21:13:16 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
1999-04-17 08:37:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2002-02-28 16:29:35 +08:00
|
|
|
| Author: Zeev Suraski <zeev@zend.com> |
|
1999-04-17 08:37:12 +08:00
|
|
|
+----------------------------------------------------------------------+
|
|
|
|
*/
|
|
|
|
|
2000-07-24 09:40:02 +08:00
|
|
|
/* $Id$ */
|
|
|
|
|
1999-04-17 08:37:12 +08:00
|
|
|
#include "php.h"
|
2000-02-11 03:41:21 +08:00
|
|
|
#include "php_regex.h"
|
1999-12-05 03:19:57 +08:00
|
|
|
#include "php_browscap.h"
|
1999-04-17 08:37:12 +08:00
|
|
|
#include "php_ini.h"
|
|
|
|
|
|
|
|
#include "zend_globals.h"
|
|
|
|
|
2000-10-30 07:10:22 +08:00
|
|
|
static HashTable browser_hash;
|
|
|
|
static zval *current_section;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
2000-07-25 16:09:00 +08:00
|
|
|
#define DEFAULT_SECTION_NAME "Default Browser Capability Settings"
|
2000-07-23 19:11:35 +08:00
|
|
|
|
2001-07-29 16:40:41 +08:00
|
|
|
/* OBJECTS_FIXME: This whole extension needs going through. The use of objects looks pretty broken here */
|
2000-10-30 07:10:22 +08:00
|
|
|
|
|
|
|
static void browscap_entry_dtor(zval *pvalue)
|
|
|
|
{
|
2001-09-26 05:58:48 +08:00
|
|
|
if (Z_TYPE_P(pvalue) == IS_OBJECT) {
|
2001-07-29 16:40:41 +08:00
|
|
|
zend_hash_destroy(Z_OBJPROP_P(pvalue));
|
|
|
|
free(Z_OBJPROP_P(pvalue));
|
2000-10-30 07:10:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ convert_browscap_pattern
|
|
|
|
*/
|
2000-10-30 07:10:22 +08:00
|
|
|
static void convert_browscap_pattern(zval *pattern)
|
|
|
|
{
|
2001-08-12 01:03:37 +08:00
|
|
|
register int i, j;
|
2000-10-30 07:10:22 +08:00
|
|
|
char *t;
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
for (i=0; i<Z_STRLEN_P(pattern); i++) {
|
|
|
|
if (Z_STRVAL_P(pattern)[i]=='*' || Z_STRVAL_P(pattern)[i]=='?' || Z_STRVAL_P(pattern)[i]=='.') {
|
2000-10-30 07:10:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
if (i==Z_STRLEN_P(pattern)) { /* no wildcards */
|
|
|
|
Z_STRVAL_P(pattern) = zend_strndup(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern));
|
2000-10-30 07:10:22 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
t = (char *) malloc(Z_STRLEN_P(pattern)*2);
|
2000-10-30 07:10:22 +08:00
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
for (i=0, j=0; i<Z_STRLEN_P(pattern); i++, j++) {
|
|
|
|
switch (Z_STRVAL_P(pattern)[i]) {
|
2000-10-30 07:10:22 +08:00
|
|
|
case '?':
|
|
|
|
t[j] = '.';
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
t[j++] = '.';
|
|
|
|
t[j] = '*';
|
|
|
|
break;
|
|
|
|
case '.':
|
|
|
|
t[j++] = '\\';
|
|
|
|
t[j] = '.';
|
|
|
|
break;
|
|
|
|
default:
|
2001-09-26 05:58:48 +08:00
|
|
|
t[j] = Z_STRVAL_P(pattern)[i];
|
2000-10-30 07:10:22 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t[j]=0;
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(pattern) = t;
|
|
|
|
Z_STRLEN_P(pattern) = j;
|
2000-10-30 07:10:22 +08:00
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
2000-10-30 07:10:22 +08:00
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ php_browscap_parser_cb
|
|
|
|
*/
|
2000-10-30 07:10:22 +08:00
|
|
|
static void php_browscap_parser_cb(zval *arg1, zval *arg2, int callback_type, void *arg)
|
|
|
|
{
|
|
|
|
switch (callback_type) {
|
|
|
|
case ZEND_INI_PARSER_ENTRY:
|
|
|
|
if (current_section) {
|
|
|
|
zval *new_property;
|
|
|
|
char *new_key;
|
|
|
|
|
|
|
|
new_property = (zval *) malloc(sizeof(zval));
|
|
|
|
INIT_PZVAL(new_property);
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(new_property) = Z_STRLEN_P(arg2)?zend_strndup(Z_STRVAL_P(arg2), Z_STRLEN_P(arg2)):"";
|
|
|
|
Z_STRLEN_P(new_property) = Z_STRLEN_P(arg2);
|
|
|
|
Z_TYPE_P(new_property) = IS_STRING;
|
2000-10-30 07:10:22 +08:00
|
|
|
|
|
|
|
new_key = zend_strndup(Z_STRVAL_P(arg1), Z_STRLEN_P(arg1));
|
|
|
|
zend_str_tolower(new_key, Z_STRLEN_P(arg1));
|
2001-07-29 16:40:41 +08:00
|
|
|
zend_hash_update(Z_OBJPROP_P(current_section), new_key, Z_STRLEN_P(arg1)+1, &new_property, sizeof(zval *), NULL);
|
2000-10-30 07:10:22 +08:00
|
|
|
free(new_key);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ZEND_INI_PARSER_SECTION: {
|
|
|
|
zval *processed;
|
2002-04-30 19:30:07 +08:00
|
|
|
HashTable *section_properties;
|
|
|
|
TSRMLS_FETCH();
|
2000-10-30 07:10:22 +08:00
|
|
|
|
|
|
|
/*printf("'%s' (%d)\n",$1.value.str.val,$1.value.str.len+1);*/
|
|
|
|
current_section = (zval *) malloc(sizeof(zval));
|
|
|
|
INIT_PZVAL(current_section);
|
|
|
|
processed = (zval *) malloc(sizeof(zval));
|
|
|
|
INIT_PZVAL(processed);
|
|
|
|
|
2002-04-30 19:30:07 +08:00
|
|
|
section_properties = (HashTable *) malloc(sizeof(HashTable));
|
|
|
|
_object_and_properties_init(current_section, ZEND_STANDARD_CLASS_DEF_PTR, section_properties ZEND_FILE_LINE_CC TSRMLS_CC);
|
|
|
|
|
|
|
|
zend_hash_init(section_properties, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1);
|
2000-10-30 07:10:22 +08:00
|
|
|
zend_hash_update(&browser_hash, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1)+1, (void *) ¤t_section, sizeof(zval *), NULL);
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_STRVAL_P(processed) = Z_STRVAL_P(arg1);
|
|
|
|
Z_STRLEN_P(processed) = Z_STRLEN_P(arg1);
|
|
|
|
Z_TYPE_P(processed) = IS_STRING;
|
2000-10-30 07:10:22 +08:00
|
|
|
convert_browscap_pattern(processed);
|
2002-04-30 19:30:07 +08:00
|
|
|
zend_hash_update(section_properties, "browser_name_pattern", sizeof("browser_name_pattern"), (void *) &processed, sizeof(zval *), NULL);
|
2000-10-30 07:10:22 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
2000-10-30 07:10:22 +08:00
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ PHP_MINIT_FUNCTION
|
|
|
|
*/
|
2000-10-30 07:10:22 +08:00
|
|
|
PHP_MINIT_FUNCTION(browscap)
|
|
|
|
{
|
|
|
|
char *browscap = INI_STR("browscap");
|
|
|
|
|
|
|
|
if (browscap) {
|
|
|
|
zend_file_handle fh;
|
|
|
|
|
|
|
|
if (zend_hash_init(&browser_hash, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1)==FAILURE) {
|
|
|
|
return FAILURE;
|
|
|
|
}
|
|
|
|
|
2001-04-30 20:45:02 +08:00
|
|
|
fh.handle.fp = VCWD_FOPEN(browscap, "r");
|
2000-10-30 07:10:22 +08:00
|
|
|
if (!fh.handle.fp) {
|
2002-08-24 09:19:28 +08:00
|
|
|
php_error_docref(NULL TSRMLS_CC, E_CORE_WARNING, "Cannot open '%s' for reading", browscap);
|
2000-10-30 07:10:22 +08:00
|
|
|
return FAILURE;
|
|
|
|
}
|
2000-10-31 07:39:14 +08:00
|
|
|
fh.filename = browscap;
|
2001-09-26 05:58:48 +08:00
|
|
|
Z_TYPE(fh) = ZEND_HANDLE_FP;
|
2000-10-31 07:39:14 +08:00
|
|
|
zend_parse_ini_file(&fh, 1, (zend_ini_parser_cb_t) php_browscap_parser_cb, &browser_hash);
|
2000-10-30 07:10:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
2000-10-30 07:10:22 +08:00
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ PHP_MSHUTDOWN_FUNCTION
|
|
|
|
*/
|
2000-10-30 07:10:22 +08:00
|
|
|
PHP_MSHUTDOWN_FUNCTION(browscap)
|
|
|
|
{
|
|
|
|
if (INI_STR("browscap")) {
|
|
|
|
zend_hash_destroy(&browser_hash);
|
|
|
|
}
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
2000-10-30 07:10:22 +08:00
|
|
|
|
2001-06-06 21:06:12 +08:00
|
|
|
/* {{{ browser_reg_compare
|
|
|
|
*/
|
2001-08-12 01:03:37 +08:00
|
|
|
static int browser_reg_compare(zval **browser, int num_args, va_list args, zend_hash_key *key)
|
1999-04-17 08:37:12 +08:00
|
|
|
{
|
2000-07-26 02:50:02 +08:00
|
|
|
zval **browser_name;
|
1999-04-17 08:37:12 +08:00
|
|
|
regex_t r;
|
2001-08-12 01:03:37 +08:00
|
|
|
char *lookup_browser_name = va_arg(args, char *);
|
|
|
|
zval **found_browser_entry = va_arg(args, zval **);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
2000-07-23 19:11:35 +08:00
|
|
|
if (*found_browser_entry) { /* already found */
|
1999-04-17 08:37:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2001-08-12 01:03:37 +08:00
|
|
|
if(zend_hash_find(Z_OBJPROP_PP(browser), "browser_name_pattern", sizeof("browser_name_pattern"), (void **) &browser_name) == FAILURE) {
|
2000-07-23 19:32:18 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
if (!strchr(Z_STRVAL_PP(browser_name),'*')) {
|
1999-04-17 08:37:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2001-09-26 05:58:48 +08:00
|
|
|
if (regcomp(&r, Z_STRVAL_PP(browser_name), REG_NOSUB)!=0) {
|
1999-04-17 08:37:12 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2001-08-12 01:03:37 +08:00
|
|
|
if (regexec(&r, lookup_browser_name, 0, NULL, 0)==0) {
|
2000-07-23 19:11:35 +08:00
|
|
|
*found_browser_entry = *browser;
|
1999-04-17 08:37:12 +08:00
|
|
|
}
|
|
|
|
regfree(&r);
|
|
|
|
return 0;
|
|
|
|
}
|
2001-06-06 21:06:12 +08:00
|
|
|
/* }}} */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
2000-06-24 03:55:49 +08:00
|
|
|
/* {{{ proto object get_browser(string browser_name)
|
|
|
|
Get information about the capabilities of a browser */
|
1999-05-16 19:19:26 +08:00
|
|
|
PHP_FUNCTION(get_browser)
|
1999-04-17 08:37:12 +08:00
|
|
|
{
|
2001-08-12 01:03:37 +08:00
|
|
|
zval **agent_name, **agent;
|
|
|
|
zval *found_browser_entry, *tmp_copy;
|
2000-07-23 19:11:35 +08:00
|
|
|
char *lookup_browser_name;
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
if (!INI_STR("browscap")) {
|
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
|
2000-06-06 03:47:54 +08:00
|
|
|
switch(ZEND_NUM_ARGS()) {
|
1999-04-17 08:37:12 +08:00
|
|
|
case 0:
|
2001-03-14 07:20:23 +08:00
|
|
|
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) {
|
2001-08-12 01:03:37 +08:00
|
|
|
zend_error(E_WARNING, "HTTP_USER_AGENT variable is not set, cannot determine user agent name");
|
2000-07-23 19:11:35 +08:00
|
|
|
RETURN_FALSE;
|
1999-04-17 08:37:12 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
2001-08-12 01:03:37 +08:00
|
|
|
if (zend_get_parameters_ex(1, &agent_name)==FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
WRONG_PARAM_COUNT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-12-20 10:09:58 +08:00
|
|
|
convert_to_string_ex(agent_name);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
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);
|
1999-04-24 08:12:00 +08:00
|
|
|
found_browser_entry = NULL;
|
2001-08-03 15:15:14 +08:00
|
|
|
zend_hash_apply_with_arguments(&browser_hash, (apply_func_args_t) browser_reg_compare, 2, lookup_browser_name, &found_browser_entry);
|
1999-04-17 08:37:12 +08:00
|
|
|
|
1999-04-24 08:12:00 +08:00
|
|
|
if (found_browser_entry) {
|
2000-07-26 02:50:02 +08:00
|
|
|
agent = &found_browser_entry;
|
2000-07-23 19:11:35 +08:00
|
|
|
} else if (zend_hash_find(&browser_hash, DEFAULT_SECTION_NAME, sizeof(DEFAULT_SECTION_NAME), (void **) &agent)==FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
RETURN_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-07-26 19:21:03 +08:00
|
|
|
object_init(return_value);
|
2001-07-29 16:40:41 +08:00
|
|
|
zend_hash_copy(Z_OBJPROP_P(return_value), Z_OBJPROP_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *));
|
2000-07-26 19:21:03 +08:00
|
|
|
|
2001-08-12 01:03:37 +08:00
|
|
|
while (zend_hash_find(Z_OBJPROP_PP(agent), "parent", sizeof("parent"), (void **) &agent_name)==SUCCESS) {
|
1999-04-17 08:37:12 +08:00
|
|
|
|
2001-09-26 05:58:48 +08:00
|
|
|
if (zend_hash_find(&browser_hash, Z_STRVAL_PP(agent_name), Z_STRLEN_PP(agent_name)+1, (void **)&agent)==FAILURE) {
|
1999-04-17 08:37:12 +08:00
|
|
|
break;
|
|
|
|
}
|
2000-07-26 19:21:03 +08:00
|
|
|
|
2001-07-29 16:40:41 +08:00
|
|
|
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);
|
1999-04-17 08:37:12 +08:00
|
|
|
}
|
|
|
|
}
|
2000-03-29 19:38:47 +08:00
|
|
|
/* }}} */
|
1999-04-17 08:37:12 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
2001-09-09 21:29:31 +08:00
|
|
|
* vim600: sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: sw=4 ts=4
|
1999-04-17 08:37:12 +08:00
|
|
|
*/
|