1 | <?php |
||
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) |
||
84 | |||
85 | /** |
||
86 | * Set HTTP headers. |
||
87 | * |
||
88 | * @param array $httpHeaders |
||
89 | */ |
||
90 | public function setHttpHeaders($httpHeaders = null) |
||
106 | |||
107 | /** |
||
108 | * Return user agent headers. |
||
109 | * |
||
110 | * @return array |
||
111 | */ |
||
112 | public function getUaHttpHeaders() |
||
116 | |||
117 | /** |
||
118 | * Set the user agent. |
||
119 | * |
||
120 | * @param string $userAgent |
||
121 | */ |
||
122 | public function setUserAgent($userAgent = null) |
||
137 | |||
138 | /** |
||
139 | * Build the user agent regex. |
||
140 | * |
||
141 | * @return string |
||
142 | */ |
||
143 | public function getRegex() |
||
147 | |||
148 | /** |
||
149 | * Build the replacement regex. |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function getExclusions() |
||
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) |
||
183 | |||
184 | /** |
||
185 | * Return the matches. |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getMatches() |
||
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: