1 /*
2 +----------------------------------------------------------------------+
3 | PHP Version 5 |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 1997-2016 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$ */
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 typedef struct {
31 HashTable *htab;
32 zval *current_section;
33 char *current_section_name;
34 char filename[MAXPATHLEN];
35 } browser_data;
36
37 /* browser data defined in startup phase, eagerly loaded in MINIT */
38 static browser_data global_bdata = {0};
39
40 /* browser data defined in activation phase, lazily loaded in get_browser.
41 * Per request and per thread, if applicable */
ZEND_BEGIN_MODULE_GLOBALS(browscap)42 ZEND_BEGIN_MODULE_GLOBALS(browscap)
43 browser_data activation_bdata;
44 ZEND_END_MODULE_GLOBALS(browscap)
45
46 ZEND_DECLARE_MODULE_GLOBALS(browscap)
47
48 #ifdef ZTS
49 #define BROWSCAP_G(v) TSRMG(browscap_globals_id, zend_browscap_globals *, v)
50 #else
51 #define BROWSCAP_G(v) (browscap_globals.v)
52 #endif
53
54 #define DEFAULT_SECTION_NAME "Default Browser Capability Settings"
55
56 /* OBJECTS_FIXME: This whole extension needs going through. The use of objects looks pretty broken here */
57
58 static void browscap_entry_dtor_request(zval **zvalue) /* {{{ */
59 {
60 if (Z_TYPE_PP(zvalue) == IS_ARRAY) {
61 zend_hash_destroy(Z_ARRVAL_PP(zvalue));
62 efree(Z_ARRVAL_PP(zvalue));
63 } else if (Z_TYPE_PP(zvalue) == IS_STRING) {
64 if (Z_STRVAL_PP(zvalue)) {
65 efree(Z_STRVAL_PP(zvalue));
66 }
67 }
68 efree(*zvalue);
69 }
70 /* }}} */
71
browscap_entry_dtor_persistent(zval ** zvalue)72 static void browscap_entry_dtor_persistent(zval **zvalue) /* {{{ */ {
73 if (Z_TYPE_PP(zvalue) == IS_ARRAY) {
74 zend_hash_destroy(Z_ARRVAL_PP(zvalue));
75 free(Z_ARRVAL_PP(zvalue));
76 } else if (Z_TYPE_PP(zvalue) == IS_STRING) {
77 if (Z_STRVAL_PP(zvalue)) {
78 free(Z_STRVAL_PP(zvalue));
79 }
80 }
81 free(*zvalue);
82 }
83 /* }}} */
84
convert_browscap_pattern(zval * pattern,int persistent)85 static void convert_browscap_pattern(zval *pattern, int persistent) /* {{{ */
86 {
87 int i, j=0;
88 char *t;
89
90 php_strtolower(Z_STRVAL_P(pattern), Z_STRLEN_P(pattern));
91
92 t = (char *) safe_pemalloc(Z_STRLEN_P(pattern), 2, 5, persistent);
93
94 t[j++] = '~';
95 t[j++] = '^';
96
97 for (i=0; i<Z_STRLEN_P(pattern); i++, j++) {
98 switch (Z_STRVAL_P(pattern)[i]) {
99 case '?':
100 t[j] = '.';
101 break;
102 case '*':
103 t[j++] = '.';
104 t[j] = '*';
105 break;
106 case '.':
107 t[j++] = '\\';
108 t[j] = '.';
109 break;
110 case '\\':
111 t[j++] = '\\';
112 t[j] = '\\';
113 break;
114 case '(':
115 t[j++] = '\\';
116 t[j] = '(';
117 break;
118 case ')':
119 t[j++] = '\\';
120 t[j] = ')';
121 break;
122 case '~':
123 t[j++] = '\\';
124 t[j] = '~';
125 break;
126 case '+':
127 t[j++] = '\\';
128 t[j] = '+';
129 break;
130 default:
131 t[j] = Z_STRVAL_P(pattern)[i];
132 break;
133 }
134 }
135
136 t[j++] = '$';
137 t[j++] = '~';
138
139 t[j]=0;
140 Z_STRVAL_P(pattern) = t;
141 Z_STRLEN_P(pattern) = j;
142 }
143 /* }}} */
144
php_browscap_parser_cb(zval * arg1,zval * arg2,zval * arg3,int callback_type,void * arg TSRMLS_DC)145 static void php_browscap_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg TSRMLS_DC) /* {{{ */
146 {
147 browser_data *bdata = arg;
148 int persistent = bdata->htab->persistent;
149
150 if (!arg1) {
151 return;
152 }
153
154 switch (callback_type) {
155 case ZEND_INI_PARSER_ENTRY:
156 if (bdata->current_section && arg2) {
157 zval *new_property;
158 char *new_key;
159
160 /* parent entry can not be same as current section -> causes infinite loop! */
161 if (!strcasecmp(Z_STRVAL_P(arg1), "parent") &&
162 bdata->current_section_name != NULL &&
163 !strcasecmp(bdata->current_section_name, Z_STRVAL_P(arg2))
164 ) {
165 zend_error(E_CORE_ERROR, "Invalid browscap ini file: "
166 "'Parent' value cannot be same as the section name: %s "
167 "(in file %s)", bdata->current_section_name, INI_STR("browscap"));
168 return;
169 }
170
171 new_property = (zval *) pemalloc(sizeof(zval), persistent);
172 INIT_PZVAL(new_property);
173 Z_TYPE_P(new_property) = IS_STRING;
174
175 /* Set proper value for true/false settings */
176 if ((Z_STRLEN_P(arg2) == 2 && !strncasecmp(Z_STRVAL_P(arg2), "on", sizeof("on") - 1)) ||
177 (Z_STRLEN_P(arg2) == 3 && !strncasecmp(Z_STRVAL_P(arg2), "yes", sizeof("yes") - 1)) ||
178 (Z_STRLEN_P(arg2) == 4 && !strncasecmp(Z_STRVAL_P(arg2), "true", sizeof("true") - 1))
179 ) {
180 Z_STRVAL_P(new_property) = pestrndup("1", 1, persistent);
181 Z_STRLEN_P(new_property) = 1;
182 } else if (
183 (Z_STRLEN_P(arg2) == 2 && !strncasecmp(Z_STRVAL_P(arg2), "no", sizeof("no") - 1)) ||
184 (Z_STRLEN_P(arg2) == 3 && !strncasecmp(Z_STRVAL_P(arg2), "off", sizeof("off") - 1)) ||
185 (Z_STRLEN_P(arg2) == 4 && !strncasecmp(Z_STRVAL_P(arg2), "none", sizeof("none") - 1)) ||
186 (Z_STRLEN_P(arg2) == 5 && !strncasecmp(Z_STRVAL_P(arg2), "false", sizeof("false") - 1))
187 ) {
188 Z_STRVAL_P(new_property) = pestrndup("", 0, persistent);
189 Z_STRLEN_P(new_property) = 0;
190 } else { /* Other than true/false setting */
191 Z_STRVAL_P(new_property) = pestrndup(Z_STRVAL_P(arg2),
192 Z_STRLEN_P(arg2), persistent);
193 Z_STRLEN_P(new_property) = Z_STRLEN_P(arg2);
194 }
195 new_key = pestrndup(Z_STRVAL_P(arg1), Z_STRLEN_P(arg1), persistent);
196 zend_str_tolower(new_key, Z_STRLEN_P(arg1));
197 zend_hash_update(Z_ARRVAL_P(bdata->current_section), new_key, Z_STRLEN_P(arg1) + 1, &new_property, sizeof(zval *), NULL);
198 pefree(new_key, persistent);
199 }
200 break;
201 case ZEND_INI_PARSER_SECTION: {
202 zval *processed;
203 zval *unprocessed;
204 HashTable *section_properties;
205
206 /*printf("'%s' (%d)\n",$1.value.str.val,$1.value.str.len + 1);*/
207 bdata->current_section = (zval *) pemalloc(sizeof(zval), persistent);
208 INIT_PZVAL(bdata->current_section);
209 processed = (zval *) pemalloc(sizeof(zval), persistent);
210 INIT_PZVAL(processed);
211 unprocessed = (zval *) pemalloc(sizeof(zval), persistent);
212 INIT_PZVAL(unprocessed);
213
214 section_properties = (HashTable *) pemalloc(sizeof(HashTable), persistent);
215 zend_hash_init(section_properties, 0, NULL,
216 (dtor_func_t) (persistent?browscap_entry_dtor_persistent
217 :browscap_entry_dtor_request),
218 persistent);
219 Z_ARRVAL_P(bdata->current_section) = section_properties;
220 Z_TYPE_P(bdata->current_section) = IS_ARRAY;
221 if (bdata->current_section_name) {
222 pefree(bdata->current_section_name, persistent);
223 }
224 bdata->current_section_name = pestrndup(Z_STRVAL_P(arg1),
225 Z_STRLEN_P(arg1), persistent);
226
227 zend_hash_update(bdata->htab, Z_STRVAL_P(arg1), Z_STRLEN_P(arg1) + 1, (void *) &bdata->current_section, sizeof(zval *), NULL);
228
229 Z_STRVAL_P(processed) = Z_STRVAL_P(arg1);
230 Z_STRLEN_P(processed) = Z_STRLEN_P(arg1);
231 Z_TYPE_P(processed) = IS_STRING;
232 Z_STRVAL_P(unprocessed) = Z_STRVAL_P(arg1);
233 Z_STRLEN_P(unprocessed) = Z_STRLEN_P(arg1);
234 Z_TYPE_P(unprocessed) = IS_STRING;
235 Z_STRVAL_P(unprocessed) = pestrndup(Z_STRVAL_P(unprocessed), Z_STRLEN_P(unprocessed), persistent);
236
237 convert_browscap_pattern(processed, persistent);
238 zend_hash_update(section_properties, "browser_name_regex", sizeof("browser_name_regex"), (void *) &processed, sizeof(zval *), NULL);
239 zend_hash_update(section_properties, "browser_name_pattern", sizeof("browser_name_pattern"), (void *) &unprocessed, sizeof(zval *), NULL);
240 }
241 break;
242 }
243 }
244 /* }}} */
245
browscap_read_file(char * filename,browser_data * browdata,int persistent TSRMLS_DC)246 static int browscap_read_file(char *filename, browser_data *browdata, int persistent TSRMLS_DC) /* {{{ */
247 {
248 zend_file_handle fh = {0};
249
250 if (filename == NULL || filename[0] == '\0') {
251 return FAILURE;
252 }
253
254 browdata->htab = pemalloc(sizeof *browdata->htab, persistent);
255 if (browdata->htab == NULL) {
256 return FAILURE;
257 }
258
259 if (zend_hash_init_ex(browdata->htab, 0, NULL,
260 (dtor_func_t) (persistent?browscap_entry_dtor_persistent
261 :browscap_entry_dtor_request),
262 persistent, 0) == FAILURE) {
263 pefree(browdata->htab, persistent);
264 browdata->htab = NULL;
265 return FAILURE;
266 }
267
268 fh.handle.fp = VCWD_FOPEN(filename, "r");
269 fh.opened_path = NULL;
270 fh.free_filename = 0;
271 if (!fh.handle.fp) {
272 zend_hash_destroy(browdata->htab);
273 pefree(browdata->htab, persistent);
274 browdata->htab = NULL;
275 zend_error(E_CORE_WARNING, "Cannot open '%s' for reading", filename);
276 return FAILURE;
277 }
278 fh.filename = filename;
279 Z_TYPE(fh) = ZEND_HANDLE_FP;
280 browdata->current_section_name = NULL;
281 zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_RAW,
282 (zend_ini_parser_cb_t) php_browscap_parser_cb, browdata TSRMLS_CC);
283 if (browdata->current_section_name != NULL) {
284 pefree(browdata->current_section_name, persistent);
285 browdata->current_section_name = NULL;
286 }
287
288 return SUCCESS;
289 }
290 /* }}} */
291
292 #ifdef ZTS
browscap_globals_ctor(zend_browscap_globals * browscap_globals TSRMLS_DC)293 static void browscap_globals_ctor(zend_browscap_globals *browscap_globals TSRMLS_DC) /* {{{ */
294 {
295 browscap_globals->activation_bdata.htab = NULL;
296 browscap_globals->activation_bdata.current_section = NULL;
297 browscap_globals->activation_bdata.current_section_name = NULL;
298 browscap_globals->activation_bdata.filename[0] = '\0';
299 }
300 /* }}} */
301 #endif
302
browscap_bdata_dtor(browser_data * bdata,int persistent TSRMLS_DC)303 static void browscap_bdata_dtor(browser_data *bdata, int persistent TSRMLS_DC) /* {{{ */
304 {
305 if (bdata->htab != NULL) {
306 zend_hash_destroy(bdata->htab);
307 pefree(bdata->htab, persistent);
308 bdata->htab = NULL;
309 }
310 bdata->filename[0] = '\0';
311 /* current_section_* are only used during parsing */
312 }
313 /* }}} */
314
315 /* {{{ PHP_INI_MH
316 */
PHP_INI_MH(OnChangeBrowscap)317 PHP_INI_MH(OnChangeBrowscap)
318 {
319 if (stage == PHP_INI_STAGE_STARTUP) {
320 /* value handled in browscap.c's MINIT */
321 return SUCCESS;
322 } else if (stage == PHP_INI_STAGE_ACTIVATE) {
323 browser_data *bdata = &BROWSCAP_G(activation_bdata);
324 if (bdata->filename[0] != '\0') {
325 browscap_bdata_dtor(bdata, 0 TSRMLS_CC);
326 }
327 if (VCWD_REALPATH(new_value, bdata->filename) == NULL) {
328 return FAILURE;
329 }
330 return SUCCESS;
331 }
332
333 return FAILURE;
334 }
335 /* }}} */
336
PHP_MINIT_FUNCTION(browscap)337 PHP_MINIT_FUNCTION(browscap) /* {{{ */
338 {
339 char *browscap = INI_STR("browscap");
340
341 #ifdef ZTS
342 ts_allocate_id(&browscap_globals_id, sizeof(browser_data),
343 browscap_globals_ctor, NULL);
344 #endif
345 /* ctor call not really needed for non-ZTS */
346
347 if (browscap && browscap[0]) {
348 if (browscap_read_file(browscap, &global_bdata, 1 TSRMLS_CC) == FAILURE) {
349 return FAILURE;
350 }
351 }
352
353 return SUCCESS;
354 }
355 /* }}} */
356
PHP_RSHUTDOWN_FUNCTION(browscap)357 PHP_RSHUTDOWN_FUNCTION(browscap) /* {{{ */
358 {
359 browser_data *bdata = &BROWSCAP_G(activation_bdata);
360 if (bdata->filename[0] != '\0') {
361 browscap_bdata_dtor(bdata, 0 TSRMLS_CC);
362 }
363
364 return SUCCESS;
365 }
366 /* }}} */
367
PHP_MSHUTDOWN_FUNCTION(browscap)368 PHP_MSHUTDOWN_FUNCTION(browscap) /* {{{ */
369 {
370 browscap_bdata_dtor(&global_bdata, 1 TSRMLS_CC);
371
372 return SUCCESS;
373 }
374 /* }}} */
375
browser_reg_compare(zval ** browser TSRMLS_DC,int num_args,va_list args,zend_hash_key * key)376 static int browser_reg_compare(zval **browser TSRMLS_DC, int num_args, va_list args, zend_hash_key *key) /* {{{ */
377 {
378 zval **browser_regex, **previous_match;
379 pcre *re;
380 int re_options;
381 pcre_extra *re_extra;
382 char *lookup_browser_name = va_arg(args, char *);
383 int lookup_browser_length = va_arg(args, int);
384 zval **found_browser_entry = va_arg(args, zval **);
385
386 /* See if we have an exact match, if so, we're done... */
387 if (*found_browser_entry) {
388 if (zend_hash_find(Z_ARRVAL_PP(found_browser_entry), "browser_name_pattern", sizeof("browser_name_pattern"), (void**) &previous_match) == FAILURE) {
389 return 0;
390 }
391 else if (!strcasecmp(Z_STRVAL_PP(previous_match), lookup_browser_name)) {
392 return 0;
393 }
394 }
395
396 if (zend_hash_find(Z_ARRVAL_PP(browser), "browser_name_regex", sizeof("browser_name_regex"), (void **) &browser_regex) == FAILURE) {
397 return 0;
398 }
399
400 re = pcre_get_compiled_regex(Z_STRVAL_PP(browser_regex), &re_extra, &re_options TSRMLS_CC);
401 if (re == NULL) {
402 return 0;
403 }
404
405 if (pcre_exec(re, re_extra, lookup_browser_name, lookup_browser_length, 0, re_options, NULL, 0) == 0) {
406 /* If we've found a possible browser, we need to do a comparison of the
407 number of characters changed in the user agent being checked versus
408 the previous match found and the current match. */
409 if (*found_browser_entry) {
410 int i, prev_len = 0, curr_len = 0, ua_len;
411 zval **current_match;
412
413 if (zend_hash_find(Z_ARRVAL_PP(browser), "browser_name_pattern", sizeof("browser_name_pattern"), (void**) ¤t_match) == FAILURE) {
414 return 0;
415 }
416
417 ua_len = lookup_browser_length;
418
419 for (i = 0; i < Z_STRLEN_PP(previous_match); i++) {
420 switch (Z_STRVAL_PP(previous_match)[i]) {
421 case '?':
422 case '*':
423 /* do nothing, ignore these characters in the count */
424 break;
425
426 default:
427 ++prev_len;
428 }
429 }
430
431 for (i = 0; i < Z_STRLEN_PP(current_match); i++) {
432 switch (Z_STRVAL_PP(current_match)[i]) {
433 case '?':
434 case '*':
435 /* do nothing, ignore these characters in the count */
436 break;
437
438 default:
439 ++curr_len;
440 }
441 }
442
443 /* Pick which browser pattern replaces the least amount of
444 characters when compared to the original user agent string... */
445 if (ua_len - prev_len > ua_len - curr_len) {
446 *found_browser_entry = *browser;
447 }
448 }
449 else {
450 *found_browser_entry = *browser;
451 }
452 }
453
454 return 0;
455 }
456 /* }}} */
457
browscap_zval_copy_ctor(zval ** p)458 static void browscap_zval_copy_ctor(zval **p) /* {{{ */
459 {
460 zval *new;
461
462 ALLOC_ZVAL(new);
463 *new = **p;
464
465 zval_copy_ctor(new);
466
467 INIT_PZVAL(new);
468 *p = new;
469 } /* }}} */
470
471 /* {{{ proto mixed get_browser([string browser_name [, bool return_array]])
472 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)473 PHP_FUNCTION(get_browser)
474 {
475 char *agent_name = NULL;
476 int agent_name_len = 0;
477 zend_bool return_array = 0;
478 zval **agent, **z_agent_name, **http_user_agent;
479 zval *found_browser_entry, *tmp_copy;
480 char *lookup_browser_name;
481 browser_data *bdata;
482
483 if (BROWSCAP_G(activation_bdata).filename[0] != '\0') {
484 bdata = &BROWSCAP_G(activation_bdata);
485 if (bdata->htab == NULL) { /* not initialized yet */
486 if (browscap_read_file(bdata->filename, bdata, 0 TSRMLS_CC) == FAILURE) {
487 RETURN_FALSE;
488 }
489 }
490 } else {
491 if (!global_bdata.htab) {
492 php_error_docref(NULL TSRMLS_CC, E_WARNING, "browscap ini directive not set");
493 RETURN_FALSE;
494 }
495 bdata = &global_bdata;
496 }
497
498 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!b", &agent_name, &agent_name_len, &return_array) == FAILURE) {
499 return;
500 }
501
502 if (agent_name == NULL) {
503 zend_is_auto_global("_SERVER", sizeof("_SERVER") - 1 TSRMLS_CC);
504 if (!PG(http_globals)[TRACK_VARS_SERVER] ||
505 zend_hash_find(HASH_OF(PG(http_globals)[TRACK_VARS_SERVER]), "HTTP_USER_AGENT", sizeof("HTTP_USER_AGENT"), (void **) &http_user_agent) == FAILURE
506 ) {
507 php_error_docref(NULL TSRMLS_CC, E_WARNING, "HTTP_USER_AGENT variable is not set, cannot determine user agent name");
508 RETURN_FALSE;
509 }
510 agent_name = Z_STRVAL_PP(http_user_agent);
511 agent_name_len = Z_STRLEN_PP(http_user_agent);
512 }
513
514 lookup_browser_name = estrndup(agent_name, agent_name_len);
515 php_strtolower(lookup_browser_name, agent_name_len);
516
517 if (zend_hash_find(bdata->htab, lookup_browser_name, agent_name_len + 1, (void **) &agent) == FAILURE) {
518 found_browser_entry = NULL;
519 zend_hash_apply_with_arguments(bdata->htab TSRMLS_CC, (apply_func_args_t) browser_reg_compare, 3, lookup_browser_name, agent_name_len, &found_browser_entry);
520
521 if (found_browser_entry) {
522 agent = &found_browser_entry;
523 } else if (zend_hash_find(bdata->htab, DEFAULT_SECTION_NAME, sizeof(DEFAULT_SECTION_NAME), (void **) &agent) == FAILURE) {
524 efree(lookup_browser_name);
525 RETURN_FALSE;
526 }
527 }
528
529 if (return_array) {
530 array_init(return_value);
531 zend_hash_copy(Z_ARRVAL_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) browscap_zval_copy_ctor, (void *) &tmp_copy, sizeof(zval *));
532 }
533 else {
534 object_init(return_value);
535 zend_hash_copy(Z_OBJPROP_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) browscap_zval_copy_ctor, (void *) &tmp_copy, sizeof(zval *));
536 }
537
538 while (zend_hash_find(Z_ARRVAL_PP(agent), "parent", sizeof("parent"), (void **) &z_agent_name) == SUCCESS) {
539 if (zend_hash_find(bdata->htab, Z_STRVAL_PP(z_agent_name), Z_STRLEN_PP(z_agent_name) + 1, (void **)&agent) == FAILURE) {
540 break;
541 }
542
543 if (return_array) {
544 zend_hash_merge(Z_ARRVAL_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) browscap_zval_copy_ctor, (void *) &tmp_copy, sizeof(zval *), 0);
545 }
546 else {
547 zend_hash_merge(Z_OBJPROP_P(return_value), Z_ARRVAL_PP(agent), (copy_ctor_func_t) browscap_zval_copy_ctor, (void *) &tmp_copy, sizeof(zval *), 0);
548 }
549 }
550
551 efree(lookup_browser_name);
552 }
553 /* }}} */
554
555 /*
556 * Local variables:
557 * tab-width: 4
558 * c-basic-offset: 4
559 * End:
560 * vim600: sw=4 ts=4 fdm=marker
561 * vim<600: sw=4 ts=4
562 */
563