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