forked from matomo-org/device-detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bot.php
70 lines (64 loc) · 1.8 KB
/
Bot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* Device Detector - The Universal Device Detection library for parsing User Agents
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/lgpl.html LGPL v3 or later
*/
namespace DeviceDetector\Parser;
/**
* Class Bot
*
* Parses a user agent for bot information
*
* Detected bots are defined in regexes/bots.yml
*
* @package DeviceDetector\Parser
*/
class Bot extends BotParserAbstract
{
protected $fixtureFile = 'regexes/bots.yml';
protected $parserName = 'bot';
protected $discardDetails = false;
/**
* Enables information discarding
*/
public function discardDetails()
{
$this->discardDetails = true;
}
/**
* Parses the current UA and checks whether it contains bot information
*
* @see bots.yml for list of detected bots
*
* Step 1: Build a big regex containing all regexes and match UA against it
* -> If no matches found: return
* -> Otherwise:
* Step 2: Walk through the list of regexes in bots.yml and try to match every one
* -> Return the matched data
*
* If $discardDetails is set to TRUE, the Step 2 will be skipped
* $bot will be set to TRUE instead
*
* NOTE: Doing the big match before matching every single regex speeds up the detection
*/
public function parse()
{
$result = null;
if ($this->preMatchOverall()) {
if ($this->discardDetails) {
return true;
}
foreach ($this->getRegexes() as $regex) {
$matches = $this->matchUserAgent($regex['regex']);
if ($matches) {
unset($regex['regex']);
$result = $regex;
break;
}
}
}
return $result;
}
}