browscap.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 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@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #include "php.h"
  19. #include "php_browscap.h"
  20. #include "php_ini.h"
  21. #include "php_string.h"
  22. #include "ext/pcre/php_pcre.h"
  23. #include "zend_ini_scanner.h"
  24. #include "zend_globals.h"
  25. #define BROWSCAP_NUM_CONTAINS 5
  26. typedef struct {
  27. zend_string *key;
  28. zend_string *value;
  29. } browscap_kv;
  30. typedef struct {
  31. zend_string *pattern;
  32. zend_string *parent;
  33. uint32_t kv_start;
  34. uint32_t kv_end;
  35. /* We ensure that the length fits in 16 bits, so this is fine */
  36. uint16_t contains_start[BROWSCAP_NUM_CONTAINS];
  37. uint8_t contains_len[BROWSCAP_NUM_CONTAINS];
  38. uint8_t prefix_len;
  39. } browscap_entry;
  40. typedef struct {
  41. HashTable *htab;
  42. browscap_kv *kv;
  43. uint32_t kv_used;
  44. uint32_t kv_size;
  45. char filename[MAXPATHLEN];
  46. } browser_data;
  47. /* browser data defined in startup phase, eagerly loaded in MINIT */
  48. static browser_data global_bdata = {0};
  49. /* browser data defined in activation phase, lazily loaded in get_browser.
  50. * Per request and per thread, if applicable */
  51. ZEND_BEGIN_MODULE_GLOBALS(browscap)
  52. browser_data activation_bdata;
  53. ZEND_END_MODULE_GLOBALS(browscap)
  54. ZEND_DECLARE_MODULE_GLOBALS(browscap)
  55. #define BROWSCAP_G(v) ZEND_MODULE_GLOBALS_ACCESSOR(browscap, v)
  56. #define DEFAULT_SECTION_NAME "Default Browser Capability Settings"
  57. /* OBJECTS_FIXME: This whole extension needs going through. The use of objects looks pretty broken here */
  58. static void browscap_entry_dtor(zval *zvalue)
  59. {
  60. browscap_entry *entry = Z_PTR_P(zvalue);
  61. zend_string_release_ex(entry->pattern, 0);
  62. if (entry->parent) {
  63. zend_string_release_ex(entry->parent, 0);
  64. }
  65. efree(entry);
  66. }
  67. static void browscap_entry_dtor_persistent(zval *zvalue)
  68. {
  69. browscap_entry *entry = Z_PTR_P(zvalue);
  70. zend_string_release_ex(entry->pattern, 1);
  71. if (entry->parent) {
  72. zend_string_release_ex(entry->parent, 1);
  73. }
  74. pefree(entry, 1);
  75. }
  76. static inline zend_bool is_placeholder(char c) {
  77. return c == '?' || c == '*';
  78. }
  79. /* Length of prefix not containing any wildcards */
  80. static uint8_t browscap_compute_prefix_len(zend_string *pattern) {
  81. size_t i;
  82. for (i = 0; i < ZSTR_LEN(pattern); i++) {
  83. if (is_placeholder(ZSTR_VAL(pattern)[i])) {
  84. break;
  85. }
  86. }
  87. return (uint8_t)MIN(i, UINT8_MAX);
  88. }
  89. static size_t browscap_compute_contains(
  90. zend_string *pattern, size_t start_pos,
  91. uint16_t *contains_start, uint8_t *contains_len) {
  92. size_t i = start_pos;
  93. /* Find first non-placeholder character after prefix */
  94. for (; i < ZSTR_LEN(pattern); i++) {
  95. if (!is_placeholder(ZSTR_VAL(pattern)[i])) {
  96. /* Skip the case of a single non-placeholder character.
  97. * Let's try to find something longer instead. */
  98. if (i + 1 < ZSTR_LEN(pattern) &&
  99. !is_placeholder(ZSTR_VAL(pattern)[i + 1])) {
  100. break;
  101. }
  102. }
  103. }
  104. *contains_start = (uint16_t)i;
  105. /* Find first placeholder character after that */
  106. for (; i < ZSTR_LEN(pattern); i++) {
  107. if (is_placeholder(ZSTR_VAL(pattern)[i])) {
  108. break;
  109. }
  110. }
  111. *contains_len = (uint8_t)MIN(i - *contains_start, UINT8_MAX);
  112. return i;
  113. }
  114. /* Length of regex, including escapes, anchors, etc. */
  115. static size_t browscap_compute_regex_len(zend_string *pattern) {
  116. size_t i, len = ZSTR_LEN(pattern);
  117. for (i = 0; i < ZSTR_LEN(pattern); i++) {
  118. switch (ZSTR_VAL(pattern)[i]) {
  119. case '*':
  120. case '.':
  121. case '\\':
  122. case '(':
  123. case ')':
  124. case '~':
  125. case '+':
  126. len++;
  127. break;
  128. }
  129. }
  130. return len + sizeof("~^$~")-1;
  131. }
  132. static zend_string *browscap_convert_pattern(zend_string *pattern, int persistent) /* {{{ */
  133. {
  134. size_t i, j=0;
  135. char *t;
  136. zend_string *res;
  137. char *lc_pattern;
  138. ALLOCA_FLAG(use_heap);
  139. res = zend_string_alloc(browscap_compute_regex_len(pattern), persistent);
  140. t = ZSTR_VAL(res);
  141. lc_pattern = do_alloca(ZSTR_LEN(pattern) + 1, use_heap);
  142. zend_str_tolower_copy(lc_pattern, ZSTR_VAL(pattern), ZSTR_LEN(pattern));
  143. t[j++] = '~';
  144. t[j++] = '^';
  145. for (i = 0; i < ZSTR_LEN(pattern); i++, j++) {
  146. switch (lc_pattern[i]) {
  147. case '?':
  148. t[j] = '.';
  149. break;
  150. case '*':
  151. t[j++] = '.';
  152. t[j] = '*';
  153. break;
  154. case '.':
  155. t[j++] = '\\';
  156. t[j] = '.';
  157. break;
  158. case '\\':
  159. t[j++] = '\\';
  160. t[j] = '\\';
  161. break;
  162. case '(':
  163. t[j++] = '\\';
  164. t[j] = '(';
  165. break;
  166. case ')':
  167. t[j++] = '\\';
  168. t[j] = ')';
  169. break;
  170. case '~':
  171. t[j++] = '\\';
  172. t[j] = '~';
  173. break;
  174. case '+':
  175. t[j++] = '\\';
  176. t[j] = '+';
  177. break;
  178. default:
  179. t[j] = lc_pattern[i];
  180. break;
  181. }
  182. }
  183. t[j++] = '$';
  184. t[j++] = '~';
  185. t[j]=0;
  186. ZSTR_LEN(res) = j;
  187. free_alloca(lc_pattern, use_heap);
  188. return res;
  189. }
  190. /* }}} */
  191. typedef struct _browscap_parser_ctx {
  192. browser_data *bdata;
  193. browscap_entry *current_entry;
  194. zend_string *current_section_name;
  195. HashTable str_interned;
  196. } browscap_parser_ctx;
  197. static zend_string *browscap_intern_str(
  198. browscap_parser_ctx *ctx, zend_string *str) {
  199. zend_string *interned = zend_hash_find_ptr(&ctx->str_interned, str);
  200. if (interned) {
  201. zend_string_addref(interned);
  202. } else {
  203. interned = zend_string_copy(str);
  204. zend_hash_add_new_ptr(&ctx->str_interned, interned, interned);
  205. }
  206. return interned;
  207. }
  208. static zend_string *browscap_intern_str_ci(
  209. browscap_parser_ctx *ctx, zend_string *str, zend_bool persistent) {
  210. zend_string *lcname;
  211. zend_string *interned;
  212. ALLOCA_FLAG(use_heap);
  213. ZSTR_ALLOCA_ALLOC(lcname, ZSTR_LEN(str), use_heap);
  214. zend_str_tolower_copy(ZSTR_VAL(lcname), ZSTR_VAL(str), ZSTR_LEN(str));
  215. interned = zend_hash_find_ptr(&ctx->str_interned, lcname);
  216. if (interned) {
  217. zend_string_addref(interned);
  218. } else {
  219. interned = zend_string_dup(lcname, persistent);
  220. zend_hash_add_new_ptr(&ctx->str_interned, interned, interned);
  221. }
  222. ZSTR_ALLOCA_FREE(lcname, use_heap);
  223. return interned;
  224. }
  225. static void browscap_add_kv(
  226. browser_data *bdata, zend_string *key, zend_string *value, zend_bool persistent) {
  227. if (bdata->kv_used == bdata->kv_size) {
  228. bdata->kv_size *= 2;
  229. bdata->kv = safe_perealloc(bdata->kv, sizeof(browscap_kv), bdata->kv_size, 0, persistent);
  230. }
  231. bdata->kv[bdata->kv_used].key = key;
  232. bdata->kv[bdata->kv_used].value = value;
  233. bdata->kv_used++;
  234. }
  235. static HashTable *browscap_entry_to_array(browser_data *bdata, browscap_entry *entry) {
  236. zval tmp;
  237. uint32_t i;
  238. HashTable *ht = zend_new_array(8);
  239. ZVAL_STR(&tmp, browscap_convert_pattern(entry->pattern, 0));
  240. zend_hash_str_add(ht, "browser_name_regex", sizeof("browser_name_regex")-1, &tmp);
  241. ZVAL_STR_COPY(&tmp, entry->pattern);
  242. zend_hash_str_add(ht, "browser_name_pattern", sizeof("browser_name_pattern")-1, &tmp);
  243. if (entry->parent) {
  244. ZVAL_STR_COPY(&tmp, entry->parent);
  245. zend_hash_str_add(ht, "parent", sizeof("parent")-1, &tmp);
  246. }
  247. for (i = entry->kv_start; i < entry->kv_end; i++) {
  248. ZVAL_STR_COPY(&tmp, bdata->kv[i].value);
  249. zend_hash_add(ht, bdata->kv[i].key, &tmp);
  250. }
  251. return ht;
  252. }
  253. static void php_browscap_parser_cb(zval *arg1, zval *arg2, zval *arg3, int callback_type, void *arg) /* {{{ */
  254. {
  255. browscap_parser_ctx *ctx = arg;
  256. browser_data *bdata = ctx->bdata;
  257. int persistent = GC_FLAGS(bdata->htab) & IS_ARRAY_PERSISTENT;
  258. if (!arg1) {
  259. return;
  260. }
  261. switch (callback_type) {
  262. case ZEND_INI_PARSER_ENTRY:
  263. if (ctx->current_entry != NULL && arg2) {
  264. zend_string *new_key, *new_value;
  265. /* Set proper value for true/false settings */
  266. if ((Z_STRLEN_P(arg2) == 2 && !strncasecmp(Z_STRVAL_P(arg2), "on", sizeof("on") - 1)) ||
  267. (Z_STRLEN_P(arg2) == 3 && !strncasecmp(Z_STRVAL_P(arg2), "yes", sizeof("yes") - 1)) ||
  268. (Z_STRLEN_P(arg2) == 4 && !strncasecmp(Z_STRVAL_P(arg2), "true", sizeof("true") - 1))
  269. ) {
  270. new_value = ZSTR_CHAR('1');
  271. } else if (
  272. (Z_STRLEN_P(arg2) == 2 && !strncasecmp(Z_STRVAL_P(arg2), "no", sizeof("no") - 1)) ||
  273. (Z_STRLEN_P(arg2) == 3 && !strncasecmp(Z_STRVAL_P(arg2), "off", sizeof("off") - 1)) ||
  274. (Z_STRLEN_P(arg2) == 4 && !strncasecmp(Z_STRVAL_P(arg2), "none", sizeof("none") - 1)) ||
  275. (Z_STRLEN_P(arg2) == 5 && !strncasecmp(Z_STRVAL_P(arg2), "false", sizeof("false") - 1))
  276. ) {
  277. new_value = ZSTR_EMPTY_ALLOC();
  278. } else { /* Other than true/false setting */
  279. new_value = browscap_intern_str(ctx, Z_STR_P(arg2));
  280. if (persistent) {
  281. new_value = zend_new_interned_string(zend_string_copy(new_value));
  282. if (ZSTR_IS_INTERNED(new_value)) {
  283. if (new_value == Z_STR_P(arg2)) {
  284. Z_TYPE_FLAGS_P(arg2) = 0;
  285. }
  286. } else {
  287. zend_string_release(new_value);
  288. }
  289. }
  290. }
  291. if (!strcasecmp(Z_STRVAL_P(arg1), "parent")) {
  292. /* parent entry can not be same as current section -> causes infinite loop! */
  293. if (ctx->current_section_name != NULL &&
  294. !strcasecmp(ZSTR_VAL(ctx->current_section_name), Z_STRVAL_P(arg2))
  295. ) {
  296. zend_error(E_CORE_ERROR, "Invalid browscap ini file: "
  297. "'Parent' value cannot be same as the section name: %s "
  298. "(in file %s)", ZSTR_VAL(ctx->current_section_name), INI_STR("browscap"));
  299. return;
  300. }
  301. if (ctx->current_entry->parent) {
  302. zend_string_release(ctx->current_entry->parent);
  303. }
  304. ctx->current_entry->parent = new_value;
  305. } else {
  306. new_key = browscap_intern_str_ci(ctx, Z_STR_P(arg1), persistent);
  307. if (persistent) {
  308. new_key = zend_new_interned_string(zend_string_copy(new_key));
  309. if (ZSTR_IS_INTERNED(new_key)) {
  310. if (new_key == Z_STR_P(arg1)) {
  311. Z_TYPE_FLAGS_P(arg1) = 0;
  312. }
  313. } else {
  314. zend_string_release(new_key);
  315. }
  316. }
  317. browscap_add_kv(bdata, new_key, new_value, persistent);
  318. ctx->current_entry->kv_end = bdata->kv_used;
  319. }
  320. }
  321. break;
  322. case ZEND_INI_PARSER_SECTION:
  323. {
  324. browscap_entry *entry;
  325. zend_string *pattern = Z_STR_P(arg1);
  326. size_t pos;
  327. int i;
  328. if (ZSTR_LEN(pattern) > UINT16_MAX) {
  329. php_error_docref(NULL, E_WARNING,
  330. "Skipping excessively long pattern of length %zd", ZSTR_LEN(pattern));
  331. break;
  332. }
  333. if (persistent) {
  334. pattern = zend_new_interned_string(zend_string_copy(pattern));
  335. if (ZSTR_IS_INTERNED(pattern)) {
  336. Z_TYPE_FLAGS_P(arg1) = 0;
  337. } else {
  338. zend_string_release(pattern);
  339. }
  340. }
  341. entry = ctx->current_entry
  342. = pemalloc(sizeof(browscap_entry), persistent);
  343. zend_hash_update_ptr(bdata->htab, pattern, entry);
  344. if (ctx->current_section_name) {
  345. zend_string_release(ctx->current_section_name);
  346. }
  347. ctx->current_section_name = zend_string_copy(pattern);
  348. entry->pattern = zend_string_copy(pattern);
  349. entry->kv_end = entry->kv_start = bdata->kv_used;
  350. entry->parent = NULL;
  351. pos = entry->prefix_len = browscap_compute_prefix_len(pattern);
  352. for (i = 0; i < BROWSCAP_NUM_CONTAINS; i++) {
  353. pos = browscap_compute_contains(pattern, pos,
  354. &entry->contains_start[i], &entry->contains_len[i]);
  355. }
  356. break;
  357. }
  358. }
  359. }
  360. /* }}} */
  361. static int browscap_read_file(char *filename, browser_data *browdata, int persistent) /* {{{ */
  362. {
  363. zend_file_handle fh;
  364. browscap_parser_ctx ctx = {0};
  365. if (filename == NULL || filename[0] == '\0') {
  366. return FAILURE;
  367. }
  368. fh.handle.fp = VCWD_FOPEN(filename, "r");
  369. fh.opened_path = NULL;
  370. fh.free_filename = 0;
  371. if (!fh.handle.fp) {
  372. zend_error(E_CORE_WARNING, "Cannot open '%s' for reading", filename);
  373. return FAILURE;
  374. }
  375. fh.filename = filename;
  376. fh.type = ZEND_HANDLE_FP;
  377. browdata->htab = pemalloc(sizeof *browdata->htab, persistent);
  378. zend_hash_init_ex(browdata->htab, 0, NULL,
  379. persistent ? browscap_entry_dtor_persistent : browscap_entry_dtor, persistent, 0);
  380. browdata->kv_size = 16 * 1024;
  381. browdata->kv_used = 0;
  382. browdata->kv = pemalloc(sizeof(browscap_kv) * browdata->kv_size, persistent);
  383. /* Create parser context */
  384. ctx.bdata = browdata;
  385. ctx.current_entry = NULL;
  386. ctx.current_section_name = NULL;
  387. zend_hash_init(&ctx.str_interned, 8, NULL, NULL, persistent);
  388. zend_parse_ini_file(&fh, 1, ZEND_INI_SCANNER_RAW,
  389. (zend_ini_parser_cb_t) php_browscap_parser_cb, &ctx);
  390. /* Destroy parser context */
  391. if (ctx.current_section_name) {
  392. zend_string_release(ctx.current_section_name);
  393. }
  394. zend_hash_destroy(&ctx.str_interned);
  395. return SUCCESS;
  396. }
  397. /* }}} */
  398. #ifdef ZTS
  399. static void browscap_globals_ctor(zend_browscap_globals *browscap_globals) /* {{{ */
  400. {
  401. browscap_globals->activation_bdata.htab = NULL;
  402. browscap_globals->activation_bdata.kv = NULL;
  403. browscap_globals->activation_bdata.filename[0] = '\0';
  404. }
  405. /* }}} */
  406. #endif
  407. static void browscap_bdata_dtor(browser_data *bdata, int persistent) /* {{{ */
  408. {
  409. if (bdata->htab != NULL) {
  410. uint32_t i;
  411. zend_hash_destroy(bdata->htab);
  412. pefree(bdata->htab, persistent);
  413. bdata->htab = NULL;
  414. for (i = 0; i < bdata->kv_used; i++) {
  415. zend_string_release(bdata->kv[i].key);
  416. zend_string_release(bdata->kv[i].value);
  417. }
  418. pefree(bdata->kv, persistent);
  419. bdata->kv = NULL;
  420. }
  421. bdata->filename[0] = '\0';
  422. }
  423. /* }}} */
  424. /* {{{ PHP_INI_MH
  425. */
  426. PHP_INI_MH(OnChangeBrowscap)
  427. {
  428. if (stage == PHP_INI_STAGE_STARTUP) {
  429. /* value handled in browscap.c's MINIT */
  430. return SUCCESS;
  431. } else if (stage == PHP_INI_STAGE_ACTIVATE) {
  432. browser_data *bdata = &BROWSCAP_G(activation_bdata);
  433. if (bdata->filename[0] != '\0') {
  434. browscap_bdata_dtor(bdata, 0);
  435. }
  436. if (VCWD_REALPATH(ZSTR_VAL(new_value), bdata->filename) == NULL) {
  437. return FAILURE;
  438. }
  439. return SUCCESS;
  440. }
  441. return FAILURE;
  442. }
  443. /* }}} */
  444. PHP_MINIT_FUNCTION(browscap) /* {{{ */
  445. {
  446. char *browscap = INI_STR("browscap");
  447. #ifdef ZTS
  448. ts_allocate_id(&browscap_globals_id, sizeof(browser_data), (ts_allocate_ctor) browscap_globals_ctor, NULL);
  449. #endif
  450. /* ctor call not really needed for non-ZTS */
  451. if (browscap && browscap[0]) {
  452. if (browscap_read_file(browscap, &global_bdata, 1) == FAILURE) {
  453. return FAILURE;
  454. }
  455. }
  456. return SUCCESS;
  457. }
  458. /* }}} */
  459. PHP_RSHUTDOWN_FUNCTION(browscap) /* {{{ */
  460. {
  461. browser_data *bdata = &BROWSCAP_G(activation_bdata);
  462. if (bdata->filename[0] != '\0') {
  463. browscap_bdata_dtor(bdata, 0);
  464. }
  465. return SUCCESS;
  466. }
  467. /* }}} */
  468. PHP_MSHUTDOWN_FUNCTION(browscap) /* {{{ */
  469. {
  470. browscap_bdata_dtor(&global_bdata, 1);
  471. return SUCCESS;
  472. }
  473. /* }}} */
  474. static inline size_t browscap_get_minimum_length(browscap_entry *entry) {
  475. size_t len = entry->prefix_len;
  476. int i;
  477. for (i = 0; i < BROWSCAP_NUM_CONTAINS; i++) {
  478. len += entry->contains_len[i];
  479. }
  480. return len;
  481. }
  482. static int browser_reg_compare(
  483. zval *entry_zv, int num_args, va_list args, zend_hash_key *key) /* {{{ */
  484. {
  485. browscap_entry *entry = Z_PTR_P(entry_zv);
  486. zend_string *agent_name = va_arg(args, zend_string *);
  487. browscap_entry **found_entry_ptr = va_arg(args, browscap_entry **);
  488. browscap_entry *found_entry = *found_entry_ptr;
  489. ALLOCA_FLAG(use_heap)
  490. zend_string *pattern_lc, *regex;
  491. const char *cur;
  492. int i;
  493. pcre2_code *re;
  494. pcre2_match_data *match_data;
  495. uint32_t re_options, capture_count;
  496. int rc;
  497. /* Agent name too short */
  498. if (ZSTR_LEN(agent_name) < browscap_get_minimum_length(entry)) {
  499. return 0;
  500. }
  501. /* Quickly discard patterns where the prefix doesn't match. */
  502. if (zend_binary_strcasecmp(
  503. ZSTR_VAL(agent_name), entry->prefix_len,
  504. ZSTR_VAL(entry->pattern), entry->prefix_len) != 0) {
  505. return 0;
  506. }
  507. /* Lowercase the pattern, the agent name is already lowercase */
  508. ZSTR_ALLOCA_ALLOC(pattern_lc, ZSTR_LEN(entry->pattern), use_heap);
  509. zend_str_tolower_copy(ZSTR_VAL(pattern_lc), ZSTR_VAL(entry->pattern), ZSTR_LEN(entry->pattern));
  510. /* Check if the agent contains the "contains" portions */
  511. cur = ZSTR_VAL(agent_name) + entry->prefix_len;
  512. for (i = 0; i < BROWSCAP_NUM_CONTAINS; i++) {
  513. if (entry->contains_len[i] != 0) {
  514. cur = zend_memnstr(cur,
  515. ZSTR_VAL(pattern_lc) + entry->contains_start[i],
  516. entry->contains_len[i],
  517. ZSTR_VAL(agent_name) + ZSTR_LEN(agent_name));
  518. if (!cur) {
  519. ZSTR_ALLOCA_FREE(pattern_lc, use_heap);
  520. return 0;
  521. }
  522. cur += entry->contains_len[i];
  523. }
  524. }
  525. /* See if we have an exact match, if so, we're done... */
  526. if (zend_string_equals(agent_name, pattern_lc)) {
  527. *found_entry_ptr = entry;
  528. ZSTR_ALLOCA_FREE(pattern_lc, use_heap);
  529. return ZEND_HASH_APPLY_STOP;
  530. }
  531. regex = browscap_convert_pattern(entry->pattern, 0);
  532. re = pcre_get_compiled_regex(regex, &capture_count, &re_options);
  533. if (re == NULL) {
  534. ZSTR_ALLOCA_FREE(pattern_lc, use_heap);
  535. zend_string_release(regex);
  536. return 0;
  537. }
  538. match_data = php_pcre_create_match_data(capture_count, re);
  539. if (!match_data) {
  540. ZSTR_ALLOCA_FREE(pattern_lc, use_heap);
  541. zend_string_release(regex);
  542. return 0;
  543. }
  544. rc = pcre2_match(re, (PCRE2_SPTR)ZSTR_VAL(agent_name), ZSTR_LEN(agent_name), 0, re_options, match_data, php_pcre_mctx());
  545. php_pcre_free_match_data(match_data);
  546. if (PCRE2_ERROR_NOMATCH != rc) {
  547. /* If we've found a possible browser, we need to do a comparison of the
  548. number of characters changed in the user agent being checked versus
  549. the previous match found and the current match. */
  550. if (found_entry) {
  551. size_t i, prev_len = 0, curr_len = 0;
  552. zend_string *previous_match = found_entry->pattern;
  553. zend_string *current_match = entry->pattern;
  554. for (i = 0; i < ZSTR_LEN(previous_match); i++) {
  555. switch (ZSTR_VAL(previous_match)[i]) {
  556. case '?':
  557. case '*':
  558. /* do nothing, ignore these characters in the count */
  559. break;
  560. default:
  561. ++prev_len;
  562. }
  563. }
  564. for (i = 0; i < ZSTR_LEN(current_match); i++) {
  565. switch (ZSTR_VAL(current_match)[i]) {
  566. case '?':
  567. case '*':
  568. /* do nothing, ignore these characters in the count */
  569. break;
  570. default:
  571. ++curr_len;
  572. }
  573. }
  574. /* Pick which browser pattern replaces the least amount of
  575. characters when compared to the original user agent string... */
  576. if (prev_len < curr_len) {
  577. *found_entry_ptr = entry;
  578. }
  579. } else {
  580. *found_entry_ptr = entry;
  581. }
  582. }
  583. ZSTR_ALLOCA_FREE(pattern_lc, use_heap);
  584. zend_string_release(regex);
  585. return 0;
  586. }
  587. /* }}} */
  588. static void browscap_zval_copy_ctor(zval *p) /* {{{ */
  589. {
  590. if (Z_REFCOUNTED_P(p)) {
  591. zend_string *str;
  592. ZEND_ASSERT(Z_TYPE_P(p) == IS_STRING);
  593. str = Z_STR_P(p);
  594. if (!(GC_FLAGS(str) & GC_PERSISTENT)) {
  595. GC_ADDREF(str);
  596. } else {
  597. ZVAL_NEW_STR(p, zend_string_init(ZSTR_VAL(str), ZSTR_LEN(str), 0));
  598. }
  599. }
  600. }
  601. /* }}} */
  602. /* {{{ proto mixed get_browser([string browser_name [, bool return_array]])
  603. 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. */
  604. PHP_FUNCTION(get_browser)
  605. {
  606. zend_string *agent_name = NULL, *lookup_browser_name;
  607. zend_bool return_array = 0;
  608. browser_data *bdata;
  609. browscap_entry *found_entry = NULL;
  610. HashTable *agent_ht;
  611. if (BROWSCAP_G(activation_bdata).filename[0] != '\0') {
  612. bdata = &BROWSCAP_G(activation_bdata);
  613. if (bdata->htab == NULL) { /* not initialized yet */
  614. if (browscap_read_file(bdata->filename, bdata, 0) == FAILURE) {
  615. RETURN_FALSE;
  616. }
  617. }
  618. } else {
  619. if (!global_bdata.htab) {
  620. php_error_docref(NULL, E_WARNING, "browscap ini directive not set");
  621. RETURN_FALSE;
  622. }
  623. bdata = &global_bdata;
  624. }
  625. ZEND_PARSE_PARAMETERS_START(0, 2)
  626. Z_PARAM_OPTIONAL
  627. Z_PARAM_STR_EX(agent_name, 1, 0)
  628. Z_PARAM_BOOL(return_array)
  629. ZEND_PARSE_PARAMETERS_END();
  630. if (agent_name == NULL) {
  631. zval *http_user_agent = NULL;
  632. if (Z_TYPE(PG(http_globals)[TRACK_VARS_SERVER]) == IS_ARRAY
  633. || zend_is_auto_global_str(ZEND_STRL("_SERVER"))) {
  634. http_user_agent = zend_hash_str_find(
  635. Z_ARRVAL_P(&PG(http_globals)[TRACK_VARS_SERVER]),
  636. "HTTP_USER_AGENT", sizeof("HTTP_USER_AGENT")-1);
  637. }
  638. if (http_user_agent == NULL) {
  639. php_error_docref(NULL, E_WARNING, "HTTP_USER_AGENT variable is not set, cannot determine user agent name");
  640. RETURN_FALSE;
  641. }
  642. agent_name = Z_STR_P(http_user_agent);
  643. }
  644. lookup_browser_name = zend_string_tolower(agent_name);
  645. found_entry = zend_hash_find_ptr(bdata->htab, lookup_browser_name);
  646. if (found_entry == NULL) {
  647. zend_hash_apply_with_arguments(bdata->htab, browser_reg_compare, 2, lookup_browser_name, &found_entry);
  648. if (found_entry == NULL) {
  649. found_entry = zend_hash_str_find_ptr(bdata->htab,
  650. DEFAULT_SECTION_NAME, sizeof(DEFAULT_SECTION_NAME)-1);
  651. if (found_entry == NULL) {
  652. zend_string_release(lookup_browser_name);
  653. RETURN_FALSE;
  654. }
  655. }
  656. }
  657. agent_ht = browscap_entry_to_array(bdata, found_entry);
  658. if (return_array) {
  659. RETVAL_ARR(agent_ht);
  660. } else {
  661. object_and_properties_init(return_value, zend_standard_class_def, agent_ht);
  662. }
  663. while (found_entry->parent) {
  664. found_entry = zend_hash_find_ptr(bdata->htab, found_entry->parent);
  665. if (found_entry == NULL) {
  666. break;
  667. }
  668. agent_ht = browscap_entry_to_array(bdata, found_entry);
  669. if (return_array) {
  670. zend_hash_merge(Z_ARRVAL_P(return_value), agent_ht, (copy_ctor_func_t) browscap_zval_copy_ctor, 0);
  671. } else {
  672. zend_hash_merge(Z_OBJPROP_P(return_value), agent_ht, (copy_ctor_func_t) browscap_zval_copy_ctor, 0);
  673. }
  674. zend_hash_destroy(agent_ht);
  675. efree(agent_ht);
  676. }
  677. zend_string_release_ex(lookup_browser_name, 0);
  678. }
  679. /* }}} */
  680. /*
  681. * Local variables:
  682. * tab-width: 4
  683. * c-basic-offset: 4
  684. * End:
  685. * vim600: sw=4 ts=4 fdm=marker
  686. * vim<600: sw=4 ts=4
  687. */