|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of Crawler Detect - the web crawler detection library. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mark Beech <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Jaybizzle\CrawlerDetect\Detectors; |
|
13
|
|
|
|
|
14
|
|
|
use Jaybizzle\CrawlerDetect\Fixtures\Headers; |
|
15
|
|
|
use Jaybizzle\CrawlerDetect\Fixtures\Crawlers; |
|
16
|
|
|
use Jaybizzle\CrawlerDetect\Fixtures\Exclusions; |
|
17
|
|
|
|
|
18
|
|
|
class AgentDetector |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* The user agent. |
|
22
|
|
|
* |
|
23
|
|
|
* @var null |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $userAgent = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Headers that contain a user agent. |
|
29
|
|
|
* |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $httpHeaders = array(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Store regex matches. |
|
36
|
|
|
* |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $matches = array(); |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Crawlers object. |
|
43
|
|
|
* |
|
44
|
|
|
* @var \Jaybizzle\CrawlerDetect\Fixtures\Crawlers |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $crawlers; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Exclusions object. |
|
50
|
|
|
* |
|
51
|
|
|
* @var \Jaybizzle\CrawlerDetect\Fixtures\Exclusions |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $exclusions; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Headers object. |
|
57
|
|
|
* |
|
58
|
|
|
* @var \Jaybizzle\CrawlerDetect\Fixtures\Headers |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $uaHttpHeaders; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* The compiled regex string. |
|
64
|
|
|
* |
|
65
|
|
|
* @var string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected $compiledRegex; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* The compiled exclusions regex string. |
|
71
|
|
|
* |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected $compiledExclusions; |
|
75
|
|
|
|
|
76
|
|
|
public function __construct($userAgent) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->userAgent = $userAgent; |
|
79
|
|
|
|
|
80
|
|
|
$this->crawlers = new Crawlers(); |
|
81
|
|
|
$this->exclusions = new Exclusions(); |
|
82
|
|
|
$this->uaHttpHeaders = new Headers(); |
|
83
|
|
|
|
|
84
|
|
|
$this->compiledRegex = $this->compileRegex($this->crawlers->getAll()); |
|
85
|
|
|
$this->compiledExclusions = $this->compileRegex($this->exclusions->getAll()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Compile the regex patterns into one regex string. |
|
90
|
|
|
* |
|
91
|
|
|
* @param array |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
|
|
public function compileRegex($patterns) |
|
96
|
|
|
{ |
|
97
|
|
|
return '('.implode('|', $patterns).')'; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set HTTP headers. |
|
102
|
|
|
* |
|
103
|
|
|
* @param array|null $httpHeaders |
|
|
|
|
|
|
104
|
|
|
*/ |
|
105
|
|
|
public function setHttpHeaders() |
|
|
|
|
|
|
106
|
|
|
{ |
|
107
|
|
|
$httpHeaders = $_SERVER; |
|
108
|
|
|
|
|
109
|
|
|
// Only save HTTP headers. In PHP land, that means |
|
110
|
|
|
// only _SERVER vars that start with HTTP_. |
|
111
|
|
|
foreach ($httpHeaders as $key => $value) { |
|
112
|
|
|
if (strpos($key, 'HTTP_') === 0) { |
|
113
|
|
|
$this->httpHeaders[$key] = $value; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Return user agent headers. |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getUaHttpHeaders() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->uaHttpHeaders->getAll(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Set the user agent. |
|
130
|
|
|
* |
|
131
|
|
|
* @return void |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setUserAgent() |
|
134
|
|
|
{ |
|
135
|
|
|
foreach ($this->getUaHttpHeaders() as $altHeader) { |
|
136
|
|
|
if (false === empty($this->httpHeaders[$altHeader])) { // @todo: should use getHttpHeader(), but it would be slow. |
|
137
|
|
|
$this->userAgent .= $this->httpHeaders[$altHeader].' '; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$this->userAgent = (! empty($this->userAgent) ? trim($this->userAgent) : null); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Perform the check. |
|
146
|
|
|
* |
|
147
|
|
|
* @param string|null $userAgent |
|
148
|
|
|
* @return bool |
|
149
|
|
|
*/ |
|
150
|
|
|
public function check($userAgent = null) |
|
151
|
|
|
{ |
|
152
|
|
|
if (is_null($this->userAgent) && ! is_null($userAgent)) { |
|
153
|
|
|
$this->userAgent = $userAgent; |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
if (is_null($this->userAgent)) { |
|
157
|
|
|
$this->setHttpHeaders(); |
|
158
|
|
|
$this->setUserAgent(); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
$agent = preg_replace('/'.$this->compiledExclusions.'/i', '', $this->userAgent); |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
if (strlen(trim($this->userAgent)) == 0) { |
|
164
|
|
|
return false; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
$result = preg_match('/'.$this->compiledRegex.'/i', trim($this->userAgent), $matches); |
|
168
|
|
|
|
|
169
|
|
|
if ($matches) { |
|
|
|
|
|
|
170
|
|
|
$this->matches = $matches; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
return (bool) $result; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.