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