make user agent detection work better and for more browsers
[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   //
16   // function isns()
17   //   returns true if User Agent seems to be Netscape brand, false if not
18   // function isns4()
19   //   returns true if User Agent seems to be Netscape Communicator 4.x, false if not
20   // function isie()
21   //   returns true if User Agent seems to be a version of Internet Exploder, false if not
22   // function geckobased()
23   //   returns true if User Agent seems to be a Gecko-based browser, false if not
24   // function geckodate()
25   //   returns the Gecko date when it's a Gecko-based browser, 0 if not
26   // function khtmlbased()
27   //   returns true if User Agent seems to be a KHTML-based browser, false if not
28
29   // collection of some known User Agent Strings:
30   // Mozilla/5.0 (compatible; Konqueror/3; Linux 2.4.18; X11; i686)
31   // Mozilla/5.0 (X11; U; Linux i686; de-AT; rv:1.3b) Gecko/20030114
32   // Mozilla/4.0 (compatible; MSIE 5.0; Windows 95; DigExt)
33   // Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90)
34   // Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
35   // Mozilla/4.75 [de] (Win98; U)
36   // Opera/5.12 (Windows 2000; U)  [de]
37   // Mozilla/5.0 (Windows; U; Win 9x 4.90; de-DE; m18) Gecko/20010131 Netscape6/6.01
38   // Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE; rv:1.0.1) Gecko/20020823 Netscape/7.0
39   // Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/51 (like Gecko) Safari/51
40   // Lynx/2.8.4rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6g
41   // Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20021109 Chimera/0.6+
42   // Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.3a) Gecko/20021207 Phoenix/0.5
43   // Mozilla/5.0 Galeon/1.2.7 (X11; Linux i686; U;) Gecko/20021204
44   // Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.05 [ja]
45   // Mozilla/4.0 (compatible; MSIE 5.12; Mac_PowerPC) OmniWeb/4.1.1-v424.6
46
47   var $uastring;
48   var $brand;
49   var $version;
50
51   function userAgent($ua_string = "") {
52     // *** constructor ***
53     if (strlen($ua_string)) {
54       $this->uastring = $ua_string;
55     }
56     else {
57       // read raw UA string
58       $this->uastring = $_SERVER["HTTP_USER_AGENT"];
59     }
60
61     // get UA brand and version
62     $this->brand = "Unknown"; $this->version = 0;
63     if (ereg("([0-9a-zA-Z\.]+)/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
64       $this->brand = $regs[1]; // this is a reasonable default :)
65       $this->version = $regs[2]; // this is a reasonable default :)
66     }
67     if (ereg("Netscape6/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
68       $this->brand = "Netscape";
69       $this->version = $regs[1];
70     }
71     elseif (ereg("Netscape/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
72       $this->brand = "Netscape";
73       $this->version = $regs[1];
74     }
75     elseif (ereg("Chimera/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
76       $this->brand = "Chimera";
77       $this->version = $regs[1];
78     }
79     elseif (ereg("Phoenix/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
80       $this->brand = "Phoenix";
81       $this->version = $regs[1];
82     }
83     elseif (ereg("Galeon/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
84       $this->brand = "Galeon";
85       $this->version = $regs[1];
86     }
87     elseif (ereg("rv:([0-9a-zA-Z\.+]+)", $this->uastring, $regs) && strstr($this->uastring, "Mozilla/") && strstr($this->uastring, "Gecko/")) {
88       $this->brand = "Mozilla";
89       $this->version = $regs[1];
90     }
91     elseif (ereg("Opera[ /]([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
92       $this->brand = "Opera";
93       $this->version = $regs[1];
94     }
95     elseif (ereg("OmniWeb/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
96       $this->brand = "OmniWeb";
97       $this->version = $regs[1];
98     }
99     elseif (ereg("Konqueror/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
100       $this->brand = "Konqueror";
101       $this->version = $regs[1];
102     }
103     elseif (ereg("Safari/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
104       $this->brand = "Safari";
105       $this->version = $regs[1];
106     }
107     elseif (ereg("AppleWebKit/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
108       $this->brand = "AppleWebKit";
109       $this->version = $regs[1];
110     }
111     elseif (ereg("MSIE ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
112       $this->brand = "Microsoft Internet Explorer";
113       $this->version = $regs[1];
114     }
115     elseif (ereg("Mozilla/([0-9a-zA-Z\.+]+)", $this->uastring, $regs) && !strstr($this->uastring, "compatible;") && !strstr($this->uastring, "Gecko/")) {
116       $this->brand = "Netscape";
117       $this->version = $regs[1];
118       if (intval($this->version) == 4) { $this->brand .= " Communicator"; }
119     }
120   }
121
122   function isns() {
123     // set it static so that we don't have to call it that often
124     static $is_ns;
125     if (!isset($is_ns)) {
126       $is_ns = false;
127       if (strstr($this->brand, "Netscape")) {
128         $is_ns = true;
129       }
130     }
131   return $is_ns;
132   }
133
134   function isns4() {
135     // set it static so that we don't have to call it that often
136     static $is_ns4;
137     if (!isset($is_ns4)) {
138       $is_ns4 = false;
139       if (strstr($this->brand, "Netscape") && (intval($this->version) == 4)) {
140         $is_ns4 = true;
141       }
142     }
143   return $is_ns4;
144   }
145
146   function isie() {
147     // set it static so that we don't have to call it that often
148     static $is_ie;
149     if (!isset($is_ie)) {
150       $is_ie = false;
151       if (strstr($this->brand, "Internet Explorer")) {
152         $is_ie = true;
153       }
154     }
155   return $is_ie;
156   }
157
158   function geckobased() {
159     // set it static so that we don't have to call it that often
160     static $is_gecko;
161     if (!isset($is_gecko)) {
162       $is_gecko = false;
163       if (strstr($this->uastring, "Gecko/")) {
164         $is_gecko = true;
165       }
166     }
167   return $is_gecko;
168   }
169
170   function geckodate() {
171     // set it static so that we don't have to call it that often
172     static $gdate;
173     if (!isset($gdate)) {
174       $gdate = 0;
175       if (ereg("Gecko/([0-9]+)", $this->uastring, $regs)) {
176         $gdate = $regs[1];
177       }
178     }
179   return $gdate;
180   }
181
182   function khtmlbased() {
183     // set it static so that we don't have to call it that often
184     static $is_khtml;
185     if (!isset($is_khtml)) {
186       $is_khtml = false;
187       if (strstr($this->brand, "Konqueror") || strstr($this->brand, "Safari") || strstr($this->brand, "AppleWebKit")) {
188         $is_khtml = true;
189       }
190     }
191   return $is_khtml;
192   }
193 }
194 ?>