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