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; |
13
|
|
|
|
14
|
|
|
use Jaybizzle\CrawlerDetect\Fixtures\Crawlers; |
15
|
|
|
use Jaybizzle\CrawlerDetect\Fixtures\Exclusions; |
16
|
|
|
|
17
|
|
|
class CrawlerDetect |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* The user agent. |
21
|
|
|
* |
22
|
|
|
* @var null |
23
|
|
|
*/ |
24
|
|
|
protected $userAgent = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Headers that contain a user agent. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $httpHeaders = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Store regex matches. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $matches = array(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Crawlers object. |
42
|
|
|
* |
43
|
|
|
* @var \Jaybizzle\CrawlerDetect\Fixtures\Crawlers |
44
|
|
|
*/ |
45
|
|
|
protected $crawlers; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Exclusions object. |
49
|
|
|
* |
50
|
|
|
* @var \Jaybizzle\CrawlerDetect\Fixtures\Exclusions |
51
|
|
|
*/ |
52
|
|
|
protected $exclusions; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* All possible HTTP headers that represent the |
56
|
|
|
* User-Agent string. |
57
|
|
|
* |
58
|
|
|
* @var array |
59
|
|
|
*/ |
60
|
|
|
protected static $uaHttpHeaders = array( |
61
|
|
|
// The default User-Agent string. |
62
|
|
|
'HTTP_USER_AGENT', |
63
|
|
|
// Header can occur on devices using Opera Mini. |
64
|
|
|
'HTTP_X_OPERAMINI_PHONE_UA', |
65
|
|
|
// Vodafone specific header: http://www.seoprinciple.com/mobile-web-community-still-angry-at-vodafone/24/ |
66
|
|
|
'HTTP_X_DEVICE_USER_AGENT', |
67
|
|
|
'HTTP_X_ORIGINAL_USER_AGENT', |
68
|
|
|
'HTTP_X_SKYFIRE_PHONE', |
69
|
|
|
'HTTP_X_BOLT_PHONE_UA', |
70
|
|
|
'HTTP_DEVICE_STOCK_UA', |
71
|
|
|
'HTTP_X_UCBROWSER_DEVICE_UA', |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Class constructor. |
76
|
|
|
*/ |
77
|
|
|
public function __construct(array $headers = null, $userAgent = null) |
78
|
|
|
{ |
79
|
|
|
$this->setHttpHeaders($headers); |
80
|
|
|
$this->setUserAgent($userAgent); |
81
|
|
|
$this->crawlers = new Crawlers(); |
82
|
|
|
$this->exclusions = new Exclusions(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Set HTTP headers. |
87
|
|
|
* |
88
|
|
|
* @param array $httpHeaders |
89
|
|
|
*/ |
90
|
|
|
public function setHttpHeaders($httpHeaders = null) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
// use global _SERVER if $httpHeaders aren't defined |
93
|
|
|
if (!is_array($httpHeaders) || !count($httpHeaders)) { |
94
|
|
|
$httpHeaders = $_SERVER; |
95
|
|
|
} |
96
|
|
|
// clear existing headers |
97
|
|
|
$this->httpHeaders = array(); |
98
|
|
|
// Only save HTTP headers. In PHP land, that means only _SERVER vars that |
99
|
|
|
// start with HTTP_. |
100
|
|
|
foreach ($httpHeaders as $key => $value) { |
101
|
|
|
if (substr($key, 0, 5) === 'HTTP_') { |
102
|
|
|
$this->httpHeaders[$key] = $value; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return user agent headers. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
|
|
public function getUaHttpHeaders() |
113
|
|
|
{ |
114
|
|
|
return self::$uaHttpHeaders; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set the user agent. |
119
|
|
|
* |
120
|
|
|
* @param string $userAgent |
121
|
|
|
*/ |
122
|
|
|
public function setUserAgent($userAgent = null) |
123
|
|
|
{ |
124
|
|
|
if (false === empty($userAgent)) { |
125
|
|
|
return $this->userAgent = $userAgent; |
|
|
|
|
126
|
|
|
} else { |
127
|
|
|
$this->userAgent = null; |
128
|
|
|
foreach ($this->getUaHttpHeaders() as $altHeader) { |
129
|
|
|
if (false === empty($this->httpHeaders[$altHeader])) { // @todo: should use getHttpHeader(), but it would be slow. |
130
|
|
|
$this->userAgent .= $this->httpHeaders[$altHeader].' '; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $this->userAgent = (!empty($this->userAgent) ? trim($this->userAgent) : null); |
|
|
|
|
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Build the user agent regex. |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function getRegex() |
144
|
|
|
{ |
145
|
|
|
return '('.implode('|', $this->crawlers->getAll()).')'; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Build the replacement regex. |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
public function getExclusions() |
154
|
|
|
{ |
155
|
|
|
return '('.implode('|', $this->exclusions->getAll()).')'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Check user agent string against the regex. |
160
|
|
|
* |
161
|
|
|
* @param string $userAgent |
162
|
|
|
* |
163
|
|
|
* @return bool |
164
|
|
|
*/ |
165
|
|
|
public function isCrawler($userAgent = null) |
166
|
|
|
{ |
167
|
|
|
$agent = is_null($userAgent) ? $this->userAgent : $userAgent; |
168
|
|
|
|
169
|
|
|
$agent = preg_replace('/'.$this->getExclusions().'/i', '', $agent); |
170
|
|
|
|
171
|
|
|
if (trim($agent) === false) { |
172
|
|
|
return false; |
173
|
|
|
} else { |
174
|
|
|
$result = preg_match('/'.$this->getRegex().'/i', trim($agent), $matches); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if ($matches) { |
|
|
|
|
178
|
|
|
$this->matches = $matches; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
return (bool) $result; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Return the matches. |
186
|
|
|
* |
187
|
|
|
* @return string |
188
|
|
|
*/ |
189
|
|
|
public function getMatches() |
190
|
|
|
{ |
191
|
|
|
return $this->matches[0]; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: