1 | <?php |
---|
2 | /* ----------------------------------------------------------------------------- |
---|
3 | class name: GPCUserAgent |
---|
4 | class version : 1.0.0 |
---|
5 | plugin version : 3.5.2 |
---|
6 | date : 2012-05-25 |
---|
7 | ------------------------------------------------------------------------------ |
---|
8 | author: grum at piwog.org |
---|
9 | << May the Little SpaceFrog be with you >> |
---|
10 | ------------------------------------------------------------------------------ |
---|
11 | |
---|
12 | this classes provides base functions to parse User Agent string and retrieve |
---|
13 | properties: Browser, OS, Engine |
---|
14 | |
---|
15 | |
---|
16 | (static public) function parse($userAgent) |
---|
17 | |
---|
18 | | release | date | |
---|
19 | | 1.0.0 | 2012-05-25 | * implementation of the class |
---|
20 | | | | |
---|
21 | | | | |
---|
22 | | | | |
---|
23 | | | | |
---|
24 | | | | |
---|
25 | | | | |
---|
26 | |
---|
27 | ----------------------------------------------------------------------------- */ |
---|
28 | |
---|
29 | require_once('GPCUserAgent.const.inc.php'); |
---|
30 | |
---|
31 | |
---|
32 | class GPCUserAgent |
---|
33 | { |
---|
34 | /** |
---|
35 | * returned result is ana array with the following properties: |
---|
36 | * 'os' => Operating System id on which the browser run; integer => UA_OS_* |
---|
37 | * 'os_version' => Version of the OS; string |
---|
38 | * |
---|
39 | * 'browser' => browser id; integer => UA_BROWSER_* |
---|
40 | * 'brower_name' => browser name; string |
---|
41 | * 'browser_type' => browser type id (crawler, computer, mobile, ...); integer => UA_PROP_TYPE_* |
---|
42 | * 'browser_version' => browser version; string |
---|
43 | * |
---|
44 | * 'engine' => engine used by the browser (gecko, khtml, ...); integer => UA_ENGINE_* |
---|
45 | * |
---|
46 | * @param String $userAgent: a user agent string |
---|
47 | * @return Array: array of properties |
---|
48 | */ |
---|
49 | static public function parse($userAgent) |
---|
50 | { |
---|
51 | global $UA_BrowserInfo; |
---|
52 | |
---|
53 | $returned=array( |
---|
54 | 'os' => '', |
---|
55 | 'os_version' => '', |
---|
56 | |
---|
57 | 'browser' => '', |
---|
58 | 'browser_name' => '', |
---|
59 | 'browser_type' => '', |
---|
60 | 'browser_version' => '', |
---|
61 | |
---|
62 | 'engine' => '', |
---|
63 | 'engine_version' => '' |
---|
64 | ); |
---|
65 | |
---|
66 | $bot=self::getBot($userAgent); |
---|
67 | if($bot['crawler']!=='') |
---|
68 | { |
---|
69 | $returned['browser']=$bot['crawler']; |
---|
70 | $returned['browser_name']=$bot['name']; |
---|
71 | $returned['browser_type']=UA_PROP_TYPE_CRAWLER; |
---|
72 | $returned['browser_version']=str_replace(array(';', '(', ')'), array('', '', ''), $bot['version']); |
---|
73 | return($returned); |
---|
74 | } |
---|
75 | |
---|
76 | $tmp=self::getBrowser($userAgent); |
---|
77 | $returned['browser']=$tmp['browser']; |
---|
78 | $returned['browser_name']=$tmp['name']; |
---|
79 | $returned['browser_type']=$UA_BrowserInfo[UA_DATA_BROWSER][$tmp['browser']][UA_PROP_TYPE]; |
---|
80 | $returned['browser_version']=str_replace(array(';', '(', ')'), array('', '', ''), $tmp['version']); |
---|
81 | |
---|
82 | $tmp=self::getOs($userAgent); |
---|
83 | $returned['os']=$tmp['os']; |
---|
84 | $returned['os_version']=str_replace(array(';', '(', ')'), array('', '', ''), $tmp['version']); |
---|
85 | |
---|
86 | $tmp=self::getEngine($userAgent); |
---|
87 | $returned['engine']=$tmp['engine']; |
---|
88 | $returned['engine_version']=str_replace(array(';', '(', ')'), array('', '', ''), $tmp['version']); |
---|
89 | |
---|
90 | return($returned); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | /** |
---|
95 | * return bot properties |
---|
96 | * if user agent is not a bot, properties are filled with string |
---|
97 | * |
---|
98 | * @param String $userAgent : user agent string to parse |
---|
99 | * @return Array |
---|
100 | */ |
---|
101 | static private function getBot($userAgent) |
---|
102 | { |
---|
103 | $botList=array( |
---|
104 | '(008)\/(\S*)[\;]' => UA_BOT_008, |
---|
105 | '(abachobot)' => UA_BOT_ABACHOBOT, |
---|
106 | '(accoona-ai-agent)(?:\/(\S*))*' => UA_BOT_ACCOONA_AI_AGENT, |
---|
107 | '(arachmo)' => UA_BOT_ARACHMO, |
---|
108 | '(b-l-i-t-z-b-o-t)' => UA_BOT_BLITZBOT, |
---|
109 | '(blitzbot)' => UA_BOT_BLITZBOT, |
---|
110 | '(bingbot)\/(\S*)[\;]' => UA_BOT_MICROSOFT_BING, |
---|
111 | '(bingbot)(?:\/(\S*))*' => UA_BOT_MICROSOFT_BING, |
---|
112 | '(charlotte)\/(\S*)[\;]' => UA_BOT_CHARLOTTE, |
---|
113 | '(charlotte)(?:\/(\S*))*' => UA_BOT_CHARLOTTE, |
---|
114 | '(cerberian drtrs)\sversion-(\S*)[\)]' => UA_BOT_CERBERIAN_DRTRS, |
---|
115 | '(cosmos)\/(\S*)[\_]' => UA_BOT_COSMOS, |
---|
116 | '(covario-ids)(?:\/(\S*))*' => UA_BOT_COVARIO_IDS, |
---|
117 | '(dataparksearch)\/(\S*)[\-]' => UA_BOT_DATAPARKSEARCH, |
---|
118 | '(dataparksearch)(?:\/(\S*))*' => UA_BOT_DATAPARKSEARCH, |
---|
119 | '(gaisbot)\/(\S*)[\(\s]' => UA_BOT_GAISBOT, |
---|
120 | '(google)(?:\/(\S*))*' => UA_BOT_GOOGLE, |
---|
121 | '(googlebot)\/(\S*);' => UA_BOT_GOOGLE, |
---|
122 | '(googlebot)(?:\/(\S*))*' => UA_BOT_GOOGLE, |
---|
123 | '(googlebot-image)(?:\/(\S*))*' => UA_BOT_GOOGLE, |
---|
124 | '(gurujibot)\/(\S*);' => UA_BOT_GURUJIBOT, |
---|
125 | '(gurujibot)(?:\/(\S*))*' => UA_BOT_GURUJIBOT, |
---|
126 | '(holmes)(?:\/(\S*))*' => UA_BOT_HOLMES, |
---|
127 | '(htdig)(?:\/(\S*))*' => UA_BOT_HTDIG, |
---|
128 | '(ia_archiver)\/(\S*)' => UA_BOT_IA_ARCHIVER, |
---|
129 | '(ia_archiver)' => UA_BOT_IA_ARCHIVER, |
---|
130 | '(iccrawler)' => UA_BOT_ICCRAWLER, |
---|
131 | '(ichiro)\/(\S*)\(' => UA_BOT_ICHIRO, |
---|
132 | '(ichiro)(?:\/(\S*))*' => UA_BOT_ICHIRO, |
---|
133 | '(baiduspider)' => UA_BOT_BAIDU, |
---|
134 | '(msnbot)\/(\d+(?:\.\d+)*)' => UA_BOT_MSN_SEARCH, |
---|
135 | '(exabot)\/(\d+(?:\.\d+)*)' => UA_BOT_EXALEAD, |
---|
136 | '(gamespyhttp)\/(\d+(?:\.\d+)*)' => UA_BOT_GAMESPY, |
---|
137 | '(gigabot)\/(\d+(?:\.\d+)*)' => UA_BOT_GIGABLAST, |
---|
138 | '(inktomi)' => UA_BOT_INKTOMI, |
---|
139 | '(Yahoo! Slurp)' => UA_BOT_YAHOO_SEARCH, |
---|
140 | '\s*([a-z]*(?:bot|spyder|crawl|crawler|spider)[a-z]*)' => UA_BOT_UNKNOWN |
---|
141 | ); |
---|
142 | |
---|
143 | $returned=array( |
---|
144 | 'crawler' => '', |
---|
145 | 'name' => '', |
---|
146 | 'version' => '' |
---|
147 | ); |
---|
148 | |
---|
149 | $result=array(); |
---|
150 | foreach($botList as $regExp => $crawler) |
---|
151 | { |
---|
152 | if(preg_match('/'.$regExp.'/i',$userAgent,$result)) |
---|
153 | { |
---|
154 | $returned=array( |
---|
155 | 'crawler' => $crawler, |
---|
156 | 'name' => $result[1], |
---|
157 | 'version' => isset($result[2])?$result[2]:'' |
---|
158 | ); |
---|
159 | return($returned); |
---|
160 | } |
---|
161 | } |
---|
162 | |
---|
163 | return($returned); |
---|
164 | } |
---|
165 | |
---|
166 | |
---|
167 | /** |
---|
168 | * return browser properties |
---|
169 | * |
---|
170 | * @param String $userAgent : user agent string to parse |
---|
171 | * @return Array |
---|
172 | */ |
---|
173 | static private function getBrowser($userAgent) |
---|
174 | { |
---|
175 | $browserList=array( |
---|
176 | '(android).*applewebkit.*(?:version\/(\S*))(?:safari)?' => UA_BROWSER_ANDROID_WEBKIT, |
---|
177 | '(opera mini)\/(\S*)\/' => UA_BROWSER_OPERA_MINI, |
---|
178 | '(opera mini)\/(\S*)' => UA_BROWSER_OPERA_MINI, |
---|
179 | '(opera mobi).*(?:version\/(\S*))' => UA_BROWSER_OPERA_MOBILE, |
---|
180 | '(opera mobi)' => UA_BROWSER_OPERA_MOBILE, |
---|
181 | '(blackberry).*version\/(\S*)' => UA_BROWSER_BLACKBERRY, |
---|
182 | '(blackberry\d*)\/(\S*)' => UA_BROWSER_BLACKBERRY, |
---|
183 | '(blackberry)' => UA_BROWSER_BLACKBERRY, |
---|
184 | '(bunjalloo)\/(\S*)[\;\(]' => UA_BROWSER_BUNJALLOO, |
---|
185 | '(Fennec)\/(\S*)' => UA_BROWSER_FENNEC, |
---|
186 | '(gobrowser)\/(\S*)' => UA_BROWSER_GOBROWSER, |
---|
187 | '(gobrowser)' => UA_BROWSER_GOBROWSER, |
---|
188 | '(iemobile)\/(\S*)[\;\)]' => UA_BROWSER_IEMOBILE, |
---|
189 | '(iemobile)\s(\S*)[\;\)]' => UA_BROWSER_IEMOBILE, |
---|
190 | '(iris)\/(\S*)' => UA_BROWSER_IRIS, |
---|
191 | '(maemo browser)\s(\S*)' => UA_BROWSER_MAEMO_BROWSER, |
---|
192 | '(mib)\/(\S*)' => UA_BROWSER_MIB, |
---|
193 | '(minimo)\/(\S*)' => UA_BROWSER_MINIMO, |
---|
194 | '(netfront)\/(\S*)' => UA_BROWSER_NETFRONT, |
---|
195 | '(SEMC-Browser)\/(\S*)' => UA_BROWSER_SEMC, |
---|
196 | '(teashark)\/(\S*)' => UA_BROWSER_TEASHARK, |
---|
197 | |
---|
198 | '(playstation 3);\s*(\S*)[\;\)]' => UA_BROWSER_PS3, |
---|
199 | '(playstation 3)' => UA_BROWSER_PS3, |
---|
200 | '(PSP \(PlayStation Portable\));\s*(\S*)[\;\)]' => UA_BROWSER_PSP, |
---|
201 | '(PSP \(PlayStation Portable\));\s*(\S*)' => UA_BROWSER_PSP, |
---|
202 | |
---|
203 | '(opera)\/(\S*)\s.*(?:nintendo)' => UA_BROWSER_OPERA_WII, |
---|
204 | '(opera).*(?:version\/(\S*))' => UA_BROWSER_OPERA, |
---|
205 | '(opera)\/(\S*)' => UA_BROWSER_OPERA, |
---|
206 | '(opera)\s(\S*)' => UA_BROWSER_OPERA, |
---|
207 | '(shiira)\/(\S*)' => UA_BROWSER_SHIIRA, |
---|
208 | '(shiira)' => UA_BROWSER_SHIIRA, |
---|
209 | '(skyfire)\/(\S*)' => UA_BROWSER_SKYFIRE, |
---|
210 | '(thunderbird)\/(\S*)' => UA_BROWSER_THUNDERBIRD, |
---|
211 | '(namoroka)\/(\S*)' => UA_BROWSER_NAMOROKA, |
---|
212 | '(minefield)\/(\S*)' => UA_BROWSER_MINEFIELD, |
---|
213 | '(lunascape)\/(\S*)' => UA_BROWSER_LUNASCAPE, |
---|
214 | '(lunascape)\s(\S*)[\;\)]' => UA_BROWSER_LUNASCAPE, |
---|
215 | '(lunascape)\s(.*)[\;\)]' => UA_BROWSER_LUNASCAPE, |
---|
216 | '(lunascape)' => UA_BROWSER_LUNASCAPE, |
---|
217 | '(arora)\/(\S*)' => UA_BROWSER_ARORA, |
---|
218 | '(camino)\/(\S*)' => UA_BROWSER_CAMINO, |
---|
219 | '(kapiko)\/(\S*)' => UA_BROWSER_KAPIKO, |
---|
220 | '(kazehakase)\/(\S*)' => UA_BROWSER_KAZEHAKAZE, |
---|
221 | '(chromeplus)\/(\S*)' => UA_BROWSER_CHROMEPLUS, |
---|
222 | '(chrome)\/(\S*)' => UA_BROWSER_CHROME, |
---|
223 | '(epiphany)\/(\S*)' => UA_BROWSER_EPIPHANY, |
---|
224 | '(galeon)\/(\S*)' => UA_BROWSER_GALEON, |
---|
225 | '(orca)\/(\S*)' => UA_BROWSER_ORCA, |
---|
226 | '(lobo)\/(\S*)' => UA_BROWSER_LOBO, |
---|
227 | '(seamonkey)\/(\S*)' => UA_BROWSER_SEAMONKEY, |
---|
228 | '(midori)\/(\S*)' => UA_BROWSER_MIDORI, |
---|
229 | '(midori)' => UA_BROWSER_MIDORI, |
---|
230 | '(maxthon)\/(\S*)' => UA_BROWSER_MAXTHON, |
---|
231 | '(maxthon)\s?(\S*)[\);]' => UA_BROWSER_MAXTHON, |
---|
232 | '(iceape)\/(\S*)' => UA_BROWSER_ICEAPE, |
---|
233 | '(iceweasel)\/(\S*)' => UA_BROWSER_ICEWEASEL, |
---|
234 | '(netscape\d?)\/(\S*)' => UA_BROWSER_NETSCAPE, |
---|
235 | '(navigator)\/(\S*)' => UA_BROWSER_NETSCAPE, |
---|
236 | '(firebird)\/(\S*)' => UA_BROWSER_FIREBIRD, |
---|
237 | '(firefox)\/(\S*)' => UA_BROWSER_FIREFOX, |
---|
238 | '(msie)\s?(\S*)[\);]' => UA_BROWSER_INTERNET_EXPLORER, |
---|
239 | '(konqueror)\/(\S*)[\);]' => UA_BROWSER_KONQUEROR, |
---|
240 | '(links)\s?\((\S*)[\);]' => UA_BROWSER_LINKS, |
---|
241 | '(lynx)\/(\S*)' => UA_BROWSER_LYNX, |
---|
242 | '(safari)|version\/(\S*)' => UA_BROWSER_SAFARI, |
---|
243 | '(mozilla)\/(\S*)' => UA_BROWSER_NETSCAPE |
---|
244 | ); |
---|
245 | |
---|
246 | $returned=array( |
---|
247 | 'browser' => UA_BROWSER_UNKNOWN, |
---|
248 | 'name' => '', |
---|
249 | 'version' => '' |
---|
250 | ); |
---|
251 | |
---|
252 | $result=array(); |
---|
253 | foreach($browserList as $regExp => $browser) |
---|
254 | { |
---|
255 | if(preg_match('/'.$regExp.'/i',$userAgent,$result)) |
---|
256 | { |
---|
257 | //echo print_r($userAgent, true).print_r($result, true); |
---|
258 | if(is_array($result) and count($result)>2) |
---|
259 | { |
---|
260 | $returned=array( |
---|
261 | 'browser' => $browser, |
---|
262 | 'name' => $result[1], |
---|
263 | 'version' => isset($result[2])?$result[2]:'' |
---|
264 | ); |
---|
265 | } |
---|
266 | else |
---|
267 | { |
---|
268 | $returned=array( |
---|
269 | 'browser' => UA_BROWSER_UNKNOWN, |
---|
270 | 'name' => '', |
---|
271 | 'version' => '' |
---|
272 | ); |
---|
273 | } |
---|
274 | return($returned); |
---|
275 | } |
---|
276 | } |
---|
277 | return($returned); |
---|
278 | } |
---|
279 | |
---|
280 | |
---|
281 | /** |
---|
282 | * return OS properties |
---|
283 | * |
---|
284 | * @param String $userAgent : user agent string to parse |
---|
285 | * @return Array |
---|
286 | */ |
---|
287 | static private function getOs($userAgent) |
---|
288 | { |
---|
289 | $osList=array( |
---|
290 | '(android)\s?(\S*)[\;\)]' => UA_OS_ANDROID, |
---|
291 | '(android)\s?(\S*)' => UA_OS_ANDROID, |
---|
292 | '(linux)' => UA_OS_LINUX, |
---|
293 | '(freebsd)' => UA_OS_FREEBSD, |
---|
294 | '(openbsd)' => UA_OS_OPENBSD, |
---|
295 | '(netbsd)' => UA_OS_NETBSD, |
---|
296 | '(sunos)' => UA_OS_SUNOS, |
---|
297 | '(blackberry)\s?([^;.]*);' => UA_OS_BLACKBERRY, |
---|
298 | '(symbianos)' => UA_OS_SYMBIAN_OS, |
---|
299 | '(symbos)' => UA_OS_SYMBOS, |
---|
300 | '(windows)\s?(phone\sos\s\S*)[\;\)]' => UA_OS_WINDOWS, |
---|
301 | '(windows)\s?(phone\sos\s\S*)' => UA_OS_WINDOWS, |
---|
302 | '(windows)\s?(ce\s?\S*)[\;\)]' => UA_OS_WINDOWS, |
---|
303 | '(windows)\s?(ce\s?\S*)' => UA_OS_WINDOWS, |
---|
304 | '(windows)\s?(NT\s?\S*)[\;\)]' => UA_OS_WINDOWS, |
---|
305 | '(windows)\s?(NT\s?\S*)' => UA_OS_WINDOWS, |
---|
306 | '(windows)\s?(\S*)[\;\)]' => UA_OS_WINDOWS, |
---|
307 | '(windows)\s?(\S*)' => UA_OS_WINDOWS, |
---|
308 | '(mac_powerpc)' => UA_OS_MACINTOSH, |
---|
309 | '(cpu\siphone\sos)\s?(\S*)' => UA_OS_IOS, |
---|
310 | '(iphone|ipad|ipod);.*(?:cpu\sos\s(\S*))' => UA_OS_IOS, |
---|
311 | '(mac\sos\sx)\s(\S*)[\;\)]' => UA_OS_MAC_OS_X, |
---|
312 | '(mac\sos\sx)\s(\S*)' => UA_OS_MAC_OS_X, |
---|
313 | '(mac\sos\sx)' => UA_OS_MAC_OS_X, |
---|
314 | '(nintendo\swii).*;\s([0-9\-]*);' => UA_OS_NINTENDO_WII, |
---|
315 | '(playstation\s3)' => UA_OS_SONY_PS3, |
---|
316 | '(playstation\sportable)' => UA_OS_SONY_PSP, |
---|
317 | '(nintendo\sds)' => UA_OS_NINTENDO_DS |
---|
318 | |
---|
319 | ); |
---|
320 | |
---|
321 | $returned=array( |
---|
322 | 'os' => UA_OS_UNKNOWN, |
---|
323 | 'name' => '', |
---|
324 | 'version' => '' |
---|
325 | ); |
---|
326 | |
---|
327 | $result=array(); |
---|
328 | foreach($osList as $regExp => $os) |
---|
329 | { |
---|
330 | if(preg_match('/'.$regExp.'/i',$userAgent,$result)) |
---|
331 | { |
---|
332 | $returned=array( |
---|
333 | 'os' => $os, |
---|
334 | 'name' => $result[1], |
---|
335 | 'version' => isset($result[2])?$result[2]:'' |
---|
336 | ); |
---|
337 | switch($os) |
---|
338 | { |
---|
339 | case UA_OS_WINDOWS: |
---|
340 | switch($returned['version']) |
---|
341 | { |
---|
342 | case 'NT 5.0': |
---|
343 | $returned['version']='2000'; |
---|
344 | break; |
---|
345 | case 'NT 5.1': |
---|
346 | $returned['version']='XP'; |
---|
347 | break; |
---|
348 | case 'NT 5.2': |
---|
349 | $returned['version']='Server 2003'; |
---|
350 | break; |
---|
351 | case 'NT 6.0': |
---|
352 | $returned['version']='Vista'; |
---|
353 | break; |
---|
354 | case 'NT 6.1': |
---|
355 | $returned['version']='Seven'; |
---|
356 | break; |
---|
357 | default; |
---|
358 | break; |
---|
359 | } |
---|
360 | break; |
---|
361 | } |
---|
362 | |
---|
363 | return($returned); |
---|
364 | } |
---|
365 | } |
---|
366 | |
---|
367 | return($returned); |
---|
368 | } |
---|
369 | |
---|
370 | /** |
---|
371 | * return engine properties |
---|
372 | * |
---|
373 | * @param String $userAgent : user agent string to parse |
---|
374 | * @return Array |
---|
375 | */ |
---|
376 | static private function getEngine($userAgent) |
---|
377 | { |
---|
378 | $engineList=array( |
---|
379 | '(presto)\/(\S*)' => UA_ENGINE_PRESTO, |
---|
380 | '(gecko)\/(\S*)' => UA_ENGINE_GECKO, |
---|
381 | '(applewebkit)\/(\S*)' => UA_ENGINE_APPLEWEBKIT, |
---|
382 | '(webkit)\/(\S*)' => UA_ENGINE_WEBKIT, |
---|
383 | '(khtml)\/(\S*)' => UA_ENGINE_KHTML, |
---|
384 | '(trident)\/(\S*)' => UA_ENGINE_TRIDENT |
---|
385 | ); |
---|
386 | |
---|
387 | $returned=array( |
---|
388 | 'engine' => UA_ENGINE_UNKNOWN, |
---|
389 | 'name' => '', |
---|
390 | 'version' => '' |
---|
391 | ); |
---|
392 | |
---|
393 | $result=array(); |
---|
394 | foreach($engineList as $regExp => $engine) |
---|
395 | { |
---|
396 | if(preg_match('/'.$regExp.'/i',$userAgent,$result)) |
---|
397 | { |
---|
398 | $returned=array( |
---|
399 | 'engine' => $engine, |
---|
400 | 'name' => $result[1], |
---|
401 | 'version' => isset($result[2])?$result[2]:'' |
---|
402 | ); |
---|
403 | return($returned); |
---|
404 | } |
---|
405 | } |
---|
406 | |
---|
407 | return($returned); |
---|
408 | } |
---|
409 | |
---|
410 | |
---|
411 | } |
---|
412 | |
---|
413 | |
---|
414 | ?> |
---|