1 | <?php |
||
18 | class CrawlerDetect |
||
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 | * Prefetch Headers object. |
||
64 | * |
||
65 | * @var \Jaybizzle\CrawlerDetect\Fixtures\Headers |
||
66 | */ |
||
67 | protected $prefetchHttpHeaders; |
||
68 | |||
69 | /** |
||
70 | * The compiled regex string. |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $compiledRegex; |
||
75 | |||
76 | /** |
||
77 | * The compiled exclusions regex string. |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $compiledExclusions; |
||
82 | |||
83 | /** |
||
84 | * Class constructor. |
||
85 | */ |
||
86 | public function __construct(array $headers = null, $userAgent = null) |
||
87 | { |
||
88 | $this->crawlers = new Crawlers(); |
||
89 | $this->exclusions = new Exclusions(); |
||
90 | $this->uaHttpHeaders = new Headers(); |
||
91 | $this->prefetchHttpHeaders = new Headers()->getPrefetchHeaders(); |
||
|
|||
92 | |||
93 | $this->compiledRegex = $this->compileRegex($this->crawlers->getAll()); |
||
94 | $this->compiledExclusions = $this->compileRegex($this->exclusions->getAll()); |
||
95 | |||
96 | $this->setHttpHeaders($headers); |
||
97 | $this->userAgent = $this->setUserAgent($userAgent); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Compile the regex patterns into one regex string. |
||
102 | * |
||
103 | * @param array |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | public function compileRegex($patterns) |
||
108 | { |
||
109 | return '('.implode('|', $patterns).')'; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Set HTTP headers. |
||
114 | * |
||
115 | * @param array|null $httpHeaders |
||
116 | */ |
||
117 | public function setHttpHeaders($httpHeaders) |
||
118 | { |
||
119 | // Use global _SERVER if $httpHeaders aren't defined. |
||
120 | if (! is_array($httpHeaders) || ! count($httpHeaders)) { |
||
121 | $httpHeaders = $_SERVER; |
||
122 | } |
||
123 | |||
124 | // Clear existing headers. |
||
125 | $this->httpHeaders = array(); |
||
126 | |||
127 | // Only save HTTP headers. In PHP land, that means |
||
128 | // only _SERVER vars that start with HTTP_. |
||
129 | foreach ($httpHeaders as $key => $value) { |
||
130 | if (strpos($key, 'HTTP_') === 0) { |
||
131 | $this->httpHeaders[$key] = $value; |
||
132 | } |
||
133 | } |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Return user agent headers. |
||
138 | * |
||
139 | * @return array |
||
140 | */ |
||
141 | public function getUaHttpHeaders() |
||
142 | { |
||
143 | return $this->uaHttpHeaders->getAll(); |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Set the user agent. |
||
148 | * |
||
149 | * @param string $userAgent |
||
150 | */ |
||
151 | public function setUserAgent($userAgent) |
||
152 | { |
||
153 | if (is_null($userAgent)) { |
||
154 | foreach ($this->getUaHttpHeaders() as $altHeader) { |
||
155 | if (isset($this->httpHeaders[$altHeader])) { |
||
156 | $userAgent .= $this->httpHeaders[$altHeader].' '; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | return $userAgent; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * Check user agent string against the regex. |
||
166 | * |
||
167 | * @param string|null $userAgent |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function isCrawler($userAgent = null) |
||
172 | { |
||
173 | $prefetchHeaders = $this->hasPrefetchHeaderSet; |
||
174 | if ($prefetchHeaders) { |
||
175 | $this->matches = $prefetchHeaders; |
||
176 | return true; |
||
177 | } |
||
178 | |||
179 | $agent = $userAgent ?: $this->userAgent; |
||
180 | |||
181 | $agent = preg_replace('/'.$this->compiledExclusions.'/i', '', $agent); |
||
182 | |||
183 | if (strlen(trim($agent)) == 0) { |
||
184 | return false; |
||
185 | } |
||
186 | |||
187 | $result = preg_match('/'.$this->compiledRegex.'/i', trim($agent), $matches); |
||
188 | |||
189 | if ($matches) { |
||
190 | $this->matches = $matches; |
||
191 | } |
||
192 | |||
222 |