Client
1 month ago
Device
1 month ago
AbstractBotParser.php
2 years ago
AbstractParser.php
1 month ago
Bot.php
3 months ago
OperatingSystem.php
1 month ago
VendorFragment.php
2 years ago
Bot.php
86 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Device Detector - The Universal Device Detection library for parsing User Agents |
| 5 | * |
| 6 | * @link https://matomo.org |
| 7 | * |
| 8 | * @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later |
| 9 | */ |
| 10 | declare (strict_types=1); |
| 11 | namespace IAWPSCOPED\DeviceDetector\Parser; |
| 12 | |
| 13 | /** |
| 14 | * Class Bot |
| 15 | * |
| 16 | * Parses a user agent for bot information |
| 17 | * |
| 18 | * Detected bots are defined in regexes/bots.yml |
| 19 | * @internal |
| 20 | */ |
| 21 | class Bot extends AbstractBotParser |
| 22 | { |
| 23 | /** |
| 24 | * @var string |
| 25 | */ |
| 26 | protected $fixtureFile = 'regexes/bots.yml'; |
| 27 | /** |
| 28 | * @var string |
| 29 | */ |
| 30 | protected $parserName = 'bot'; |
| 31 | /** |
| 32 | * @var bool |
| 33 | */ |
| 34 | protected $discardDetails = \false; |
| 35 | /** |
| 36 | * Enables information discarding |
| 37 | */ |
| 38 | public function discardDetails() : void |
| 39 | { |
| 40 | $this->discardDetails = \true; |
| 41 | } |
| 42 | /** |
| 43 | * Parses the current UA and checks whether it contains bot information |
| 44 | * |
| 45 | * @return array|null |
| 46 | * |
| 47 | * @throws \Exception |
| 48 | * |
| 49 | * @see bots.yml for list of detected bots |
| 50 | * |
| 51 | * Step 1: Build a big regex containing all regexes and match UA against it |
| 52 | * -> If no matches found: return |
| 53 | * -> Otherwise: |
| 54 | * Step 2: Walk through the list of regexes in bots.yml and try to match every one |
| 55 | * -> Return the matched data |
| 56 | * |
| 57 | * If $discardDetails is set to TRUE, the Step 2 will be skipped |
| 58 | * $bot will be set to TRUE instead |
| 59 | * |
| 60 | * NOTE: Doing the big match before matching every single regex speeds up the detection |
| 61 | * |
| 62 | */ |
| 63 | public function parse() : ?array |
| 64 | { |
| 65 | $result = null; |
| 66 | if ($this->preMatchOverall()) { |
| 67 | if ($this->discardDetails) { |
| 68 | return [\true]; |
| 69 | } |
| 70 | foreach ($this->getRegexes() as $regex) { |
| 71 | $matches = $this->matchUserAgent($regex['regex']); |
| 72 | if (!$matches) { |
| 73 | continue; |
| 74 | } |
| 75 | unset($regex['regex']); |
| 76 | $result = $regex; |
| 77 | if (\array_key_exists('name', $result)) { |
| 78 | $result['name'] = $this->buildByMatch($result['name'], $matches); |
| 79 | } |
| 80 | break; |
| 81 | } |
| 82 | } |
| 83 | return $result; |
| 84 | } |
| 85 | } |
| 86 |