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