1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Author: Zeev Suraski <zeev@zend.com> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: browscap.c 281742 2009-06-06 02:40:49Z mattwil $ */
20 :
21 : #include "php.h"
22 : #include "php_browscap.h"
23 : #include "php_ini.h"
24 : #include "php_string.h"
25 : #include "ext/pcre/php_pcre.h"
26 :
27 : #include "zend_ini_scanner.h"
28 : #include "zend_globals.h"
29 :
30 : static HashTable browser_hash;
31 : static zval *current_section;
32 : static char *current_section_name;
33 :
34 : #define DEFAULT_SECTION_NAME "Default Browser Capability Settings"
35 :
36 : /* OBJECTS_FIXME: This whole extension needs going through. The use of objects looks pretty broken here */
37 :
38 : static void browscap_entry_dtor(zval **zvalue) /* {{{ */
39 118158 : {
40 118158 : if (Z_TYPE_PP(zvalue) == IS_ARRAY) {
41 19188 : zend_hash_destroy(Z_ARRVAL_PP(zvalue));
42 19188 : free(Z_ARRVAL_PP(zvalue));
43 98970 : } else if (Z_TYPE_PP(zvalue) == IS_STRING) {
44 98970 : if (Z_STRVAL_PP(zvalue)) {
45 98970 : free(Z_STRVAL_PP(zvalue));
46 : }
47 : }
48 118158 : free(*zvalue);
49 118158 : }
50 : /* }}} */
51 :
52 : static void convert_browscap_pattern(zval *pattern) /* {{{ */
53 19188 : {
54 19188 : int i, j=0;
55 : char *t;
56 :
57 19188 : php_strtolower(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern));
58 :
59 19188 : t = (char *) safe_pemalloc(Z_STRLEN_P(pattern), 2, 5, 1);
60 :
61 19188 : t[j++] = '§';
62 19188 : t[j++] = '^';
63 :
64 695178 : for (i=0; i<Z_STRLEN_P(pattern); i++, j++) {
65 675990 : switch (Z_STRVAL_P(pattern)[i]) {
66 : case '?':
67 5352 : t[j] = '.';
68 5352 : break;
69 : case '*':
70 41568 : t[j++] = '.';
71 41568 : t[j] = '*';
72 41568 : break;
73 : case '.':
74 29280 : t[j++] = '\\';
75 29280 : t[j] = '.';
76 29280 : break;
77 : case '\\':
78 78 : t[j++] = '\\';
79 78 : t[j] = '\\';
80 78 : break;
81 : case '(':
82 12852 : t[j++] = '\\';
83 12852 : t[j] = '(';
84 12852 : break;
85 : case ')':
86 12696 : t[j++] = '\\';
87 12696 : t[j] = ')';
88 12696 : break;
89 : case '§':
90 0 : t[j++] = '\\';
91 0 : t[j] = '§';
92 0 : break;
93 : default:
94 574164 : t[j] = Z_STRVAL_P(pattern)[i];
95 : break;
96 : }
97 : }
98 :
99 19188 : t[j++] = '$';
100 19188 : t[j++] = '§';
101 :
102 19188 : t[j]=0;
103 19188 : Z_STRVAL_P(pattern) = t;
104 19188 : Z_STRLEN_P(pattern) = j;
105 19188 : }
106 : /* }}} */
107 :
108 : static void php_browscap_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg TSRMLS_DC) /* {{{ */
109 79782 : {
110 79782 : if (!arg1) {
111 0 : return;
112 : }
113 :
114 79782 : switch (callback_type) {
115 : case ZEND_INI_PARSER_ENTRY:
116 60594 : if (current_section && arg2) {
117 : zval *new_property;
118 : char *new_key;
119 :
120 : /* parent entry can not be same as current section -> causes infinite loop! */
121 60594 : if (!strcasecmp(Z_STRVAL_P(arg1), "parent") &&
122 : current_section_name != NULL &&
123 : !strcasecmp(current_section_name, Z_STRVAL_P(arg2))
124 : ) {
125 0 : zend_error(E_CORE_ERROR, "Invalid browscap ini file: 'Parent' value cannot be same as the section name: %s (in file %s)", current_section_name, INI_STR("browscap"));
126 0 : return;
127 : }
128 :
129 60594 : new_property = (zval *) pemalloc(sizeof(zval), 1);
130 60594 : INIT_PZVAL(new_property);
131 60594 : Z_TYPE_P(new_property) = IS_STRING;
132 :
133 : /* Set proper value for true/false settings */
134 74952 : if ((Z_STRLEN_P(arg2) == 2 && !strncasecmp(Z_STRVAL_P(arg2), "on", sizeof("on") - 1)) ||
135 : (Z_STRLEN_P(arg2) == 3 && !strncasecmp(Z_STRVAL_P(arg2), "yes", sizeof("yes") - 1)) ||
136 : (Z_STRLEN_P(arg2) == 4 && !strncasecmp(Z_STRVAL_P(arg2), "true", sizeof("true") - 1))
137 : ) {
138 14358 : Z_STRVAL_P(new_property) = zend_strndup("1", 1);
139 14358 : Z_STRLEN_P(new_property) = 1;
140 46662 : } else if (
141 : (Z_STRLEN_P(arg2) == 2 && !strncasecmp(Z_STRVAL_P(arg2), "no", sizeof("no") - 1)) ||
142 : (Z_STRLEN_P(arg2) == 3 && !strncasecmp(Z_STRVAL_P(arg2), "off", sizeof("off") - 1)) ||
143 : (Z_STRLEN_P(arg2) == 4 && !strncasecmp(Z_STRVAL_P(arg2), "none", sizeof("none") - 1)) ||
144 : (Z_STRLEN_P(arg2) == 5 && !strncasecmp(Z_STRVAL_P(arg2), "false", sizeof("false") - 1))
145 : ) {
146 426 : Z_STRVAL_P(new_property) = zend_strndup("", 0);
147 426 : Z_STRLEN_P(new_property) = 0;
148 : } else { /* Other than true/false setting */
149 45810 : Z_STRVAL_P(new_property) = zend_strndup(Z_STRVAL_P(arg2), Z_STRLEN_P(arg2));
150 45810 : Z_STRLEN_P(new_property) = Z_STRLEN_P(arg2);
151 : }
152 60594 : new_key = zend_strndup(Z_STRVAL_P(arg1), Z_STRLEN_P(arg1));
153 60594 : zend_str_tolower(new_key, Z_STRLEN_P(arg1));
154 60594 : zend_hash_update(Z_ARRVAL_P(current_section), new_key, Z_STRLEN_P(arg1) + 1, &new_property, sizeof(zval *), NULL);
155 60594 : free(new_key);
156 : }
157 60594 : break;
158 : case ZEND_INI_PARSER_SECTION: {
159 : zval *processed;
160 : zval *unprocessed;
161 : HashTable *section_properties;
162 :
163 : /*printf("'%s' (%d)\n",$1.value.str.val,$1.value.str.len + 1);*/
164 19188 : current_section = (zval *) pemalloc(sizeof(zval), 1);
165 19188 : INIT_PZVAL(current_section);
166 19188 : processed = (zval *) pemalloc(sizeof(zval), 1);
167 19188 : INIT_PZVAL(processed);
168 19188 : unprocessed = (zval *) pemalloc(sizeof(zval), 1);
169 19188 : INIT_PZVAL(unprocessed);
170 :
171 19188 : section_properties = (HashTable *) pemalloc(sizeof(HashTable), 1);
172 19188 : zend_hash_init(section_properties, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1);
173 19188 : Z_ARRVAL_P(current_section) = section_properties;
174 19188 : Z_TYPE_P(current_section) = IS_ARRAY;
175 19188 : if (current_section_name) {
176 19182 : free(current_section_name);
177 : }
178 19188 : current_section_name = zend_strndup(Z_STRVAL_P(arg1), Z_STRLEN_P(arg1));
179 :
180 19188 : zend_hash_update(&browser_hash, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1) + 1, (void *) ¤t_section, sizeof(zval *), NULL);
181 :
182 19188 : Z_STRVAL_P(processed) = Z_STRVAL_P(arg1);
183 19188 : Z_STRLEN_P(processed) = Z_STRLEN_P(arg1);
184 19188 : Z_TYPE_P(processed) = IS_STRING;
185 19188 : Z_STRVAL_P(unprocessed) = Z_STRVAL_P(arg1);
186 19188 : Z_STRLEN_P(unprocessed) = Z_STRLEN_P(arg1);
187 19188 : Z_TYPE_P(unprocessed) = IS_STRING;
188 19188 : Z_STRVAL_P(unprocessed) = zend_strndup(Z_STRVAL_P(unprocessed), Z_STRLEN_P(unprocessed));
189 :
190 19188 : convert_browscap_pattern(processed);
191 19188 : zend_hash_update(section_properties, "browser_name_regex", sizeof("browser_name_regex"), (void *) &processed, sizeof(zval *), NULL);
192 19188 : zend_hash_update(section_properties, "browser_name_pattern", sizeof("browser_name_pattern"), (void *) &unprocessed, sizeof(zval *), NULL);
193 : }
194 : break;
195 : }
196 : }
197 : /* }}} */
198 :
199 : PHP_MINIT_FUNCTION(browscap) /* {{{ */
200 17633 : {
201 17633 : char *browscap = INI_STR("browscap");
202 :
203 17633 : if (browscap && browscap[0]) {
204 : zend_file_handle fh;
205 6 : memset(&fh, 0, sizeof(fh));
206 :
207 6 : if (zend_hash_init_ex(&browser_hash, 0, NULL, (dtor_func_t) browscap_entry_dtor, 1, 0) == FAILURE) {
208 0 : return FAILURE;
209 : }
210 :
211 6 : fh.handle.fp = VCWD_FOPEN(browscap, "r");
212 6 : fh.opened_path = NULL;
213 6 : fh.free_filename = 0;
214 6 : if (!fh.handle.fp) {
215 0 : zend_error(E_CORE_WARNING, "Cannot open '%s' for reading", browscap);
216 0 : return FAILURE;
217 : }
218 6 : fh.filename = browscap;
219 6 : Z_TYPE(fh) = ZEND_HANDLE_FP;
220 6 : current_section_name = NULL;
221 6 : zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_RAW, (zend_ini_parser_cb_t) php_browscap_parser_cb, &browser_hash TSRMLS_CC);
222 6 : if (current_section_name) {
223 6 : free(current_section_name);
224 6 : current_section_name = NULL;
225 : }
226 : }
227 :
228 17633 : return SUCCESS;
229 : }
230 : /* }}} */
231 :
232 : PHP_MSHUTDOWN_FUNCTION(browscap) /* {{{ */
233 17665 : {
234 17665 : char *browscap = INI_STR("browscap");
235 17665 : if (browscap && browscap[0]) {
236 6 : zend_hash_destroy(&browser_hash);
237 : }
238 17665 : return SUCCESS;
239 : }
240 : /* }}} */
241 :
242 : static int browser_reg_compare(zval **browser TSRMLS_DC, int num_args, va_list args, zend_hash_key *key) /* {{{ */
243 127800 : {
244 : zval **browser_regex, **previous_match;
245 : pcre *re;
246 : int re_options;
247 : pcre_extra *re_extra;
248 127800 : char *lookup_browser_name = va_arg(args, char *);
249 127800 : int lookup_browser_length = va_arg(args, int);
250 127800 : zval **found_browser_entry = va_arg(args, zval **);
251 :
252 : /* See if we have an exact match, if so, we're done... */
253 127800 : if (*found_browser_entry) {
254 24916 : if (zend_hash_find(Z_ARRVAL_PP(found_browser_entry), "browser_name_pattern", sizeof("browser_name_pattern"), (void**) &previous_match) == FAILURE) {
255 0 : return 0;
256 : }
257 24916 : else if (!strcasecmp(Z_STRVAL_PP(previous_match), lookup_browser_name)) {
258 0 : return 0;
259 : }
260 : }
261 :
262 127800 : if (zend_hash_find(Z_ARRVAL_PP(browser), "browser_name_regex", sizeof("browser_name_regex"), (void **) &browser_regex) == FAILURE) {
263 0 : return 0;
264 : }
265 :
266 127800 : re = pcre_get_compiled_regex(Z_STRVAL_PP(browser_regex), &re_extra, &re_options TSRMLS_CC);
267 127800 : if (re == NULL) {
268 0 : return 0;
269 : }
270 :
271 127800 : if (pcre_exec(re, re_extra, lookup_browser_name, lookup_browser_length, 0, re_options, NULL, 0) == 0) {
272 : /* If we've found a possible browser, we need to do a comparison of the
273 : number of characters changed in the user agent being checked versus
274 : the previous match found and the current match. */
275 90 : if (*found_browser_entry) {
276 50 : int i, prev_len = 0, curr_len = 0, ua_len;
277 : zval **current_match;
278 :
279 50 : if (zend_hash_find(Z_ARRVAL_PP(browser), "browser_name_pattern", sizeof("browser_name_pattern"), (void**) ¤t_match) == FAILURE) {
280 0 : return 0;
281 : }
282 :
283 50 : ua_len = lookup_browser_length;
284 :
285 2640 : for (i = 0; i < Z_STRLEN_PP(previous_match); i++) {
286 2590 : switch (Z_STRVAL_PP(previous_match)[i]) {
287 : case '?':
288 : case '*':
289 : /* do nothing, ignore these characters in the count */
290 201 : break;
291 :
292 : default:
293 2389 : ++prev_len;
294 : }
295 : }
296 :
297 948 : for (i = 0; i < Z_STRLEN_PP(current_match); i++) {
298 898 : switch (Z_STRVAL_PP(current_match)[i]) {
299 : case '?':
300 : case '*':
301 : /* do nothing, ignore these characters in the count */
302 110 : break;
303 :
304 : default:
305 788 : ++curr_len;
306 : }
307 : }
308 :
309 : /* Pick which browser pattern replaces the least amount of
310 : characters when compared to the original user agent string... */
311 50 : if (ua_len - prev_len > ua_len - curr_len) {
312 7 : *found_browser_entry = *browser;
313 : }
314 : }
315 : else {
316 40 : *found_browser_entry = *browser;
317 : }
318 : }
319 :
320 127800 : return 0;
321 : }
322 : /* }}} */
323 :
324 : /* {{{ proto mixed get_browser([string browser_name [, bool return_array]])
325 : 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. */
326 : PHP_FUNCTION(get_browser)
327 42 : {
328 42 : char *agent_name = NULL;
329 42 : int agent_name_len = 0;
330 42 : zend_bool return_array = 0;
331 : zval **agent, **z_agent_name;
332 : zval *found_browser_entry, *tmp_copy;
333 : char *lookup_browser_name;
334 42 : char *browscap = INI_STR("browscap");
335 :
336 42 : if (!browscap || !browscap[0]) {
337 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "browscap ini directive not set");
338 0 : RETURN_FALSE;
339 : }
340 :
341 42 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!b", &agent_name, &agent_name_len, &return_array) == FAILURE) {
342 1 : return;
343 : }
344 :
345 41 : if (agent_name == NULL) {
346 1 : zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC);
347 1 : if (!PG(http_globals)[TRACK_VARS_SERVER]
348 : || zend_hash_find(PG(http_globals)[TRACK_VARS_SERVER]->value.ht, "HTTP_USER_AGENT", sizeof("HTTP_USER_AGENT"), (void **) &agent_name) == FAILURE) {
349 1 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "HTTP_USER_AGENT variable is not set, cannot determine user agent name");
350 1 : RETURN_FALSE;
351 : }
352 : }
353 :
354 40 : lookup_browser_name = estrndup(agent_name, agent_name_len);
355 40 : php_strtolower(lookup_browser_name, agent_name_len);
356 :
357 40 : if (zend_hash_find(&browser_hash, lookup_browser_name, agent_name_len + 1, (void **) &agent) == FAILURE) {
358 40 : found_browser_entry = NULL;
359 40 : zend_hash_apply_with_arguments(&browser_hash TSRMLS_CC, (apply_func_args_t) browser_reg_compare, 3, lookup_browser_name, agent_name_len, &found_browser_entry);
360 :
361 40 : if (found_browser_entry) {
362 40 : agent = &found_browser_entry;
363 0 : } else if (zend_hash_find(&browser_hash, DEFAULT_SECTION_NAME, sizeof(DEFAULT_SECTION_NAME), (void **) &agent) == FAILURE) {
364 0 : efree(lookup_browser_name);
365 0 : RETURN_FALSE;
366 : }
367 : }
368 :
369 40 : if (return_array) {
370 40 : array_init(return_value);
371 40 : zend_hash_copy(Z_ARRVAL_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *));
372 : }
373 : else {
374 0 : object_init(return_value);
375 0 : zend_hash_copy(Z_OBJPROP_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *));
376 : }
377 :
378 140 : while (zend_hash_find(Z_ARRVAL_PP(agent), "parent", sizeof("parent"), (void **) &z_agent_name) == SUCCESS) {
379 60 : if (zend_hash_find(&browser_hash, Z_STRVAL_PP(z_agent_name), Z_STRLEN_PP(z_agent_name) + 1, (void **)&agent) == FAILURE) {
380 0 : break;
381 : }
382 :
383 60 : if (return_array) {
384 60 : 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);
385 : }
386 : else {
387 0 : 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);
388 : }
389 : }
390 :
391 40 : efree(lookup_browser_name);
392 : }
393 : /* }}} */
394 :
395 : /*
396 : * Local variables:
397 : * tab-width: 4
398 : * c-basic-offset: 4
399 : * End:
400 : * vim600: sw=4 ts=4 fdm=marker
401 : * vim<600: sw=4 ts=4
402 : */
|