1185c502ddbf62c69f87f458248308f092e02cba
[php-utility-classes.git] / include / classes / useragent.php-class
1 <?php
2 class userAgent {
3   // userAgent PHP class
4   // get user agent and tell us what Browser is accessing
5   //
6   // function userAgent([$ua_string])
7   //   CONSTRUCTOR; reads UA string (or takes the optional given UA string) and gets info from that into our variables.
8   //
9   // var $uastring
10   //   the plain User Agent string
11   // var $brand
12   //   returns the User Agent brand name
13   // var $version
14   //   the User Agent version
15   // var $bot
16   //   bool: true if this agent is a bot
17   //
18   // function getBrand()
19   //   returns the User Agent Brand Name
20   // function getVersion()
21   //   returns the User Agent version
22   //
23   // function isbot()
24   //   returns true if User Agent seems to be a bot
25   // function isns()
26   //   returns true if User Agent seems to be Netscape brand, false if not
27   // function isns4()
28   //   returns true if User Agent seems to be Netscape Communicator 4.x, false if not
29   // function isie()
30   //   returns true if User Agent seems to be a version of Internet Exploder, false if not
31   // function geckobased()
32   //   returns true if User Agent seems to be a Gecko-based browser, false if not
33   // function geckodate()
34   //   returns the Gecko date when it's a Gecko-based browser, 0 if not
35   // function khtmlbased()
36   //   returns true if User Agent seems to be a KHTML-based browser, false if not
37
38   // collection of some known User Agent Strings:
39   // Mozilla/5.0 (compatible; Konqueror/3; Linux 2.4.18; X11; i686)
40   // Mozilla/5.0 (X11; U; Linux i686; de-AT; rv:1.3b) Gecko/20030114
41   // Mozilla/4.0 (compatible; MSIE 5.0; Windows 95; DigExt)
42   // Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90)
43   // Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
44   // Mozilla/4.75 [de] (Win98; U)
45   // Opera/5.12 (Windows 2000; U)  [de]
46   // Mozilla/5.0 (Windows; U; Win 9x 4.90; de-DE; m18) Gecko/20010131 Netscape6/6.01
47   // Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE; rv:1.0.1) Gecko/20020823 Netscape/7.0
48   // Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/51 (like Gecko) Safari/51
49   // Lynx/2.8.4rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6g
50   // Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20021109 Chimera/0.6+
51   // Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.3a) Gecko/20021207 Phoenix/0.5
52   // Mozilla/5.0 Galeon/1.2.7 (X11; Linux i686; U;) Gecko/20021204
53   // Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.05 [ja]
54   // Mozilla/4.0 (compatible; MSIE 5.12; Mac_PowerPC) OmniWeb/4.1.1-v424.6
55   // Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20030516 Mozilla Firebird/0.6
56   // Mozilla/5.0 (Windows; U; WinNT4.0; en-US; rv:1.5a) Gecko/20030728 Mozilla Firebird/0.6.1
57   // Python-urllib/1.15
58   // *** search bots: ***
59   // Scooter/3.3
60   // Spinne/2.0 med_AH
61   // Vagabondo/2.0 MT (webagent at wise-guys dot nl)
62   // TurnitinBot/1.5 ( ">http://www.turnitin.com/robot/crawlerinfo.html)
63   // FAST-WebCrawler/3.x Multimedia (mm dash crawler at fast dot no)
64   // Firefly/1.0 (compatible; Mozilla 4.0; MSIE 5.5)
65   // Googlebot/2.1 (+ ">http://www.googlebot.com/bot.html)
66   // Scrubby/2.2 ( ">http://www.scrubtheweb.com/)
67   // psbot/0.1 (+ ">http://www.picsearch.com/bot.html)
68   // NG/1.0
69   // URL_Spider_Pro/3.0 ( ">http://www.innerprise.net/usp-spider.asp)"
70   // Pompos/1.3 ">http://dir.com/pompos.html
71   // Szukacz/1.5 (robot; www.szukacz.pl/jakdzialarobot.html; info@szukacz.pl)
72   // ASPseek/1.2.10
73   // NPBot-1/2.0
74   // NetResearchServer/2.7(loopimprovements.com/robot.html)
75   // dloader(NaverRobot)/1.0
76   // Mozilla/4.0 compatible ZyBorg/1.0 Daily Refresh Beta-d03 (wn.zyborg@looksmart.net;
77   // Mozilla/2.0 (compatible; Ask Jeeves/Teoma)
78   // Mozilla/5.0 (Slurp/si; slurp@inktomi.com; ">http://www.inktomi.com/slurp.html)
79   // Mozilla/5.0 [en] (compatible; Gulper Web Bot 0.2.4 www.ecsl.cs.sunysb.edu/~maxim/cgi-bin/Link/GulperBot)
80   // Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; Girafabot; girafabot at girafa dot com;
81   // Mozilla/4.0 (efp@gmx.net)
82   // Mozilla/4.5 (compatible; HTTrack 3.0x; Windows 98)
83   // PingALink Monitoring Services 1.0 (http://www.pingalink.com)
84   // IlTrovatore-Setaccio (+ ">http://www.iltrovatore.it)
85   // Mercator-2.0
86   // appie 1.1 (www.walhello.com)
87   // larbin_2.6.2 (larbin2.6.2@unspecified.mail)
88   // OWR_Crawler 0.1
89   // search.ch V1.4.2 (spiderman@search.ch;
90   // WebFilter Robot 1.0
91   // Openfind data gatherer, Openbot/3.0+(robot-response@openfind.com.tw;+
92   // LinkWalker
93   // Internet Explorer 5.5
94   // BaiDuSpider
95   // Mozilla/4.0 (compatible; B-l-i-t-z-B-O-T)
96   // B l i t z B O T @ t r i c u s . n e t (Mozilla compatible)
97   // sitecheck.internetseer.com (For more info see: ">http://sitecheck.internetseer.com)
98   // http://www.almaden.ibm.com/cs/crawler   [c01]
99   // ia_archiver
100   // Nutch
101   // Mozilla
102   // HeinrichderMiragoRobot
103   // dumbBot
104
105   var $uastring;
106   var $brand;
107   var $version;
108   var $bot = false;
109
110   function userAgent($ua_string = "") {
111     // *** constructor ***
112     if (strlen($ua_string)) {
113       $this->uastring = $ua_string;
114     }
115     else {
116       // read raw UA string
117       $this->uastring = $_SERVER["HTTP_USER_AGENT"];
118     }
119
120     // get UA brand and version
121     $this->brand = "Unknown"; $this->version = 0;
122     if (ereg("([0-9a-zA-Z\.()-]+)/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
123       $this->brand = $regs[1]; // this is a reasonable default :)
124       $this->version = $regs[2]; // this is a reasonable default :)
125     }
126     if (ereg("Netscape6/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
127       $this->brand = "Netscape";
128       $this->version = $regs[1];
129     }
130     elseif (ereg("Netscape/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
131       $this->brand = "Netscape";
132       $this->version = $regs[1];
133     }
134     elseif (ereg("Chimera/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
135       $this->brand = "Chimera";
136       $this->version = $regs[1];
137     }
138     elseif (ereg("Phoenix/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
139       $this->brand = "Phoenix";
140       $this->version = $regs[1];
141     }
142     elseif (ereg("Mozilla Firebird/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
143       $this->brand = "Mozilla Firebird";
144       $this->version = $regs[1];
145     }
146     elseif (ereg("Galeon/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
147       $this->brand = "Galeon";
148       $this->version = $regs[1];
149     }
150     elseif (ereg("rv:([0-9a-zA-Z\.+]+)", $this->uastring, $regs) && strstr($this->uastring, "Mozilla/") && strstr($this->uastring, "Gecko/")) {
151       $this->brand = "Mozilla";
152       $this->version = $regs[1];
153     }
154     elseif (ereg("Opera[ /]([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
155       $this->brand = "Opera";
156       $this->version = $regs[1];
157     }
158     elseif (ereg("OmniWeb/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
159       $this->brand = "OmniWeb";
160       $this->version = $regs[1];
161     }
162     elseif (ereg("Konqueror/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
163       $this->brand = "Konqueror";
164       $this->version = $regs[1];
165     }
166     elseif (ereg("Safari/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
167       $this->brand = "Safari";
168       $this->version = $regs[1];
169     }
170     elseif (ereg("AppleWebKit/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
171       $this->brand = "AppleWebKit";
172       $this->version = $regs[1];
173     }
174     elseif (ereg("ZyBorg/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
175       $this->brand = "ZyBorg";
176       $this->version = $regs[1];
177       $this->bot = true;
178     }
179     elseif (ereg("Ask Jeeves/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
180       $this->brand = "Ask Jeeves";
181       $this->version = $regs[1];
182       $this->bot = true;
183     }
184     elseif (ereg("Slurp/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
185       $this->brand = "Slurp";
186       $this->version = $regs[1];
187       $this->bot = true;
188     }
189     elseif (ereg("Openbot/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
190       $this->brand = "Openbot";
191       $this->version = $regs[1];
192       $this->bot = true;
193     }
194     elseif (ereg("Mercator-([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
195       $this->brand = "Mercator";
196       $this->version = $regs[1];
197       $this->bot = true;
198     }
199     elseif (ereg("appie ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
200       $this->brand = "appie";
201       $this->version = $regs[1];
202       $this->bot = true;
203     }
204     elseif (ereg("larbin_([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
205       $this->brand = "larbin";
206       $this->version = $regs[1];
207       $this->bot = true;
208     }
209     elseif (ereg("Gulper Web Bot ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
210       $this->brand = "Gulper Web Bot";
211       $this->version = $regs[1];
212       $this->bot = true;
213     }
214     elseif (ereg("HTTrack ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
215       $this->brand = "HTTrack";
216       $this->version = $regs[1];
217       $this->bot = true;
218     }
219     elseif (ereg("OWR_Crawler ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
220       $this->brand = "OWR_Crawler";
221       $this->version = $regs[1];
222       $this->bot = true;
223     }
224     elseif (ereg("search.ch ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
225       $this->brand = "search.ch";
226       $this->version = $regs[1];
227       $this->bot = true;
228     }
229     elseif (ereg("WebFilter Robot ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
230       $this->brand = "WebFilter Robot";
231       $this->version = $regs[1];
232       $this->bot = true;
233     }
234     elseif (ereg("^LinkWalker", $this->uastring)) {
235       $this->brand = "LinkWalker";
236       $this->version = "";
237       $this->bot = true;
238     }
239     elseif (ereg("^Internet Explorer 5.5", $this->uastring)) {
240       $this->brand = "Unknown bot (IE5.5)";
241       $this->version = "";
242       $this->bot = true;
243     }
244     elseif (ereg("^Mozilla[\s ]*$", $this->uastring)) {
245       $this->brand = "Unknown bot (Mozilla)";
246       $this->version = "";
247       $this->bot = true;
248     }
249     elseif (ereg("^BaiDuSpider", $this->uastring)) {
250       $this->brand = "BaiDuSpider";
251       $this->version = "";
252       $this->bot = true;
253     }
254     elseif (ereg("^ia_archiver", $this->uastring)) {
255       $this->brand = "ia_archiver";
256       $this->version = "";
257       $this->bot = true;
258     }
259     elseif (ereg("^PingALink", $this->uastring)) {
260       $this->brand = "PingALink";
261       $this->version = "";
262       $this->bot = true;
263     }
264     elseif (ereg("^IlTrovatore-Setaccio", $this->uastring)) {
265       $this->brand = "IlTrovatore-Setaccio";
266       $this->version = "";
267       $this->bot = true;
268     }
269     elseif (ereg("^Nutch", $this->uastring)) {
270       $this->brand = "Nutch";
271       $this->version = "";
272       $this->bot = true;
273     }
274     elseif (ereg("^dumbBot", $this->uastring)) {
275       $this->brand = "dumbBot";
276       $this->version = "";
277       $this->bot = true;
278     }
279     elseif (ereg("http://www.almaden.ibm.com/cs/crawler", $this->uastring)) {
280       $this->brand = "almaden crawler";
281       $this->version = "";
282       $this->bot = true;
283     }
284     elseif (ereg("B-l-i-t-z-B-O-T", $this->uastring) || ereg("B l i t z B O T @ t r i c u s . n e t", $this->uastring)) {
285       $this->brand = "BlitzBOT";
286       $this->version = "";
287       $this->bot = true;
288     }
289     elseif (ereg("sitecheck.internetseer.com", $this->uastring)) {
290       $this->brand = "internetseer";
291       $this->version = "";
292       $this->bot = true;
293     }
294     elseif (ereg("Girafabot", $this->uastring)) {
295       $this->brand = "Girafabot";
296       $this->version = "";
297       $this->bot = true;
298     }
299     elseif (ereg("efp@gmx.net", $this->uastring)) {
300       $this->brand = "efp";
301       $this->version = "";
302       $this->bot = true;
303     }
304     elseif (ereg("HeinrichderMiragoRobot", $this->uastring)) {
305       $this->brand = "HeinrichderMiragoRobot";
306       $this->version = "";
307       $this->bot = true;
308     }
309     elseif (ereg("MSIE ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
310       $this->brand = "Microsoft Internet Explorer";
311       $this->version = $regs[1];
312     }
313     elseif (ereg("Mozilla/([0-9a-zA-Z\.+]+)", $this->uastring, $regs) && !strstr($this->uastring, "compatible;") && !strstr($this->uastring, "Gecko/")) {
314       $this->brand = "Netscape";
315       $this->version = $regs[1];
316       if (intval($this->version) == 4) { $this->brand .= " Communicator"; }
317     }
318
319     $botArray = array("Scooter","Spinne","Vagabondo","TurnitinBot","FAST-WebCrawler","Firefly","Googlebot",
320                       "Scrubby","psbot","NG","URL_Spider_Pro","Pompos","Szukacz","ASPseek","NPBot-1",
321                       "dloader(NaverRobot)","NetResearchServer","","","","","","","");
322
323     if (in_array($this->brand, $botArray)) {
324       $this->bot = true;
325     }
326   }
327
328   function getBrand() { return $this->brand; }
329   function getVersion() { return $this->version; }
330   function isbot() { return $this->bot; }
331
332   function isns() {
333     // set it static so that we don't have to call it that often
334     static $is_ns;
335     if (!isset($is_ns)) {
336       $is_ns = false;
337       if (strstr($this->brand, "Netscape")) {
338         $is_ns = true;
339       }
340     }
341   return $is_ns;
342   }
343
344   function isns4() {
345     // set it static so that we don't have to call it that often
346     static $is_ns4;
347     if (!isset($is_ns4)) {
348       $is_ns4 = false;
349       if (strstr($this->brand, "Netscape") && (intval($this->version) == 4)) {
350         $is_ns4 = true;
351       }
352     }
353   return $is_ns4;
354   }
355
356   function isie() {
357     // set it static so that we don't have to call it that often
358     static $is_ie;
359     if (!isset($is_ie)) {
360       $is_ie = false;
361       if (strstr($this->brand, "Internet Explorer")) {
362         $is_ie = true;
363       }
364     }
365   return $is_ie;
366   }
367
368   function geckobased() {
369     // set it static so that we don't have to call it that often
370     static $is_gecko;
371     if (!isset($is_gecko)) {
372       $is_gecko = false;
373       if (strstr($this->uastring, "Gecko/")) {
374         $is_gecko = true;
375       }
376     }
377   return $is_gecko;
378   }
379
380   function geckodate() {
381     // set it static so that we don't have to call it that often
382     static $gdate;
383     if (!isset($gdate)) {
384       $gdate = 0;
385       if (ereg("Gecko/([0-9]+)", $this->uastring, $regs)) {
386         $gdate = $regs[1];
387       }
388     }
389   return $gdate;
390   }
391
392   function khtmlbased() {
393     // set it static so that we don't have to call it that often
394     static $is_khtml;
395     if (!isset($is_khtml)) {
396       $is_khtml = false;
397       if (strstr($this->brand, "Konqueror") || strstr($this->brand, "Safari") || strstr($this->brand, "AppleWebKit")) {
398         $is_khtml = true;
399       }
400     }
401   return $is_khtml;
402   }
403 }
404 ?>