|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace DevCoding\Object\Internet\Header; |
| 4 | + |
| 5 | +/** |
| 6 | + * Object representing a User-Agent header. |
| 7 | + * |
| 8 | + * @author AMJones <am@jonesiscoding.com> |
| 9 | + * @license https://github.com/deviscoding/objection/blob/main/LICENSE |
| 10 | + * |
| 11 | + * @package DevCoding\Object\Internet\Header |
| 12 | + */ |
| 13 | +class UserAgentString extends AbstractHeader |
| 14 | +{ |
| 15 | + const BOTS = [ |
| 16 | + 'crawler', 'bot', 'spider', 'archiver', 'scraper', 'stripper', 'wget', 'curl', 'AppEngine-Google', |
| 17 | + 'AdsBot-Google', 'AdsBot-Google-Mobile-Apps', 'Mediapartners-Google', 'Slurp', 'facebookexternalhit', |
| 18 | + 'Zeus 32297 Webster Pro', '008', 'PagePeeker', 'Nutch', 'grub-client', 'NewsGator', 'Yandex', |
| 19 | + ]; |
| 20 | + |
| 21 | + const HEADERS = [ |
| 22 | + 'USER_AGENT', |
| 23 | + 'X_OPERAMINI_PHONE_UA', |
| 24 | + 'X_DEVICE_USER_AGENT', |
| 25 | + 'X_ORIGINAL_USER_AGENT', |
| 26 | + 'X_SKYFIRE_PHONE', |
| 27 | + 'X_BOLT_PHONE_UA', |
| 28 | + 'DEVICE_STOCK_UA', |
| 29 | + 'X_UCBROWSER_DEVICE_UA', |
| 30 | + ]; |
| 31 | + |
| 32 | + public function isBot() |
| 33 | + { |
| 34 | + return $this->isMatch(sprintf('#(%s)#i', implode('|', static::BOTS))); |
| 35 | + } |
| 36 | + |
| 37 | + public function isMatch($inc, $exc = null, &$matches = []) |
| 38 | + { |
| 39 | + if (!empty($this->_ua)) |
| 40 | + { |
| 41 | + if (preg_match($inc, (string) $this, $matches)) |
| 42 | + { |
| 43 | + if (empty($exc) || !preg_match($exc, (string) $this)) |
| 44 | + { |
| 45 | + return true; |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @param string $inc Regex Pattern to match this browser, including delimiters and options |
| 55 | + * @param string|null $exc Optional Regex Pattern of exclusions, including delimiters and options |
| 56 | + * @param \Closure|null $normalizer Normalizer for regex matches. Must return an array. |
| 57 | + * |
| 58 | + * @return array|bool |
| 59 | + */ |
| 60 | + public function getMatches($inc, $exc = null, $normalizer = null) |
| 61 | + { |
| 62 | + $matches = []; |
| 63 | + if ($this->isMatch($inc, $exc, $matches)) |
| 64 | + { |
| 65 | + if ($normalizer instanceof \Closure) |
| 66 | + { |
| 67 | + return $normalizer($matches); |
| 68 | + } |
| 69 | + else |
| 70 | + { |
| 71 | + return $matches; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @return string[] |
| 80 | + */ |
| 81 | + protected function getKeys() |
| 82 | + { |
| 83 | + return static::HEADERS; |
| 84 | + } |
| 85 | +} |
0 commit comments