8250bc8c0e7695b7eacfb07390e1fd5f63e3187e
[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()
7   //   CONSTRUCTOR; reads 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
27   // debug: var $uastring = "Mozilla/5.0 (compatible; Konqueror/3; Linux 2.4.18; X11; i686)";
28   var $uastring;
29   var $brand;
30   var $version;
31
32   function userAgent() {
33     // *** constructor ***
34     // get raw UA string
35     $this->uastring = $_SERVER["HTTP_USER_AGENT"];
36
37     // get UA brand and version
38     $this->brand = "Unknown"; $this->version = 0;
39     if (ereg("([0-9a-zA-Z\.]+)/([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
40       $this->uabrand = $regs[1]; // this is a reasonable default :)
41       $this->version = $regs[2]; // this is a reasonable default :)
42     }
43     if (ereg("Mozilla/([0-9a-zA-Z\.]+)", $this->uastring, $regs) && !strstr($this->uastring, "compatible;") && !strstr($this->uastring, "Gecko/")) {
44       $this->brand = "Netscape";
45       $this->version = $regs[1];
46       if (intval($this->version) == 4) { $this->brand .= " Communicator"; }
47     }
48     elseif (ereg("Netscape6/([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
49       $this->brand = "Netscape";
50       $this->version = $regs[1];
51     }
52     elseif (ereg("Netscape/([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
53       $this->brand = "Netscape";
54       $this->version = $regs[1];
55     }
56     elseif (ereg("rv:([0-9a-zA-Z\.]+)", $this->uastring, $regs) && strstr($this->uastring, "Mozilla/") && strstr($this->uastring, "Gecko/")) {
57       $this->brand = "Mozilla";
58       $this->version = $regs[1];
59     }
60     elseif (ereg("Opera[ /]([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
61       $this->brand = "Opera";
62       $this->version = $regs[1];
63     }
64     elseif (ereg("Konqueror/([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
65       $this->brand = "Konqueror";
66       $this->version = $regs[1];
67     }
68     elseif (ereg("MSIE ([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
69       $this->brand = "Microsoft Internet Explorer";
70       $this->version = $regs[1];
71     }
72   }
73
74   function isns() {
75     // set it static so that we don't have to call it that often
76     static $is_ns;
77     if (!isset($is_ns)) {
78       $is_ns = false;
79       if (strstr($this->brand, "Netscape")) {
80         $is_ns = true;
81       }
82     }
83   return $is_ns;
84   }
85
86   function isns4() {
87     // set it static so that we don't have to call it that often
88     static $is_ns4;
89     if (!isset($is_ns4)) {
90       $is_ns4 = false;
91       if (strstr($this->brand, "Netscape") && (intval($this->version) == 4)) {
92         $is_ns4 = true;
93       }
94     }
95   return $is_ns4;
96   }
97
98   function isie() {
99     // set it static so that we don't have to call it that often
100     static $is_ie;
101     if (!isset($is_ie)) {
102       $is_ie = false;
103       if (strstr($this->brand, "Internet Explorer")) {
104         $is_ie = true;
105       }
106     }
107   return $is_ie;
108   }
109
110   function geckobased() {
111     // set it static so that we don't have to call it that often
112     static $is_gecko;
113     if (!isset($is_gecko)) {
114       $is_gecko = false;
115       if (strstr($this->uastring, "Gecko/")) {
116         $is_gecko = true;
117       }
118     }
119   return $is_gecko;
120   }
121
122   function geckodate() {
123     // set it static so that we don't have to call it that often
124     static $gdate;
125     if (!isset($gdate)) {
126       $gdate = 0;
127       if (ereg("Gecko/([0-9]+)", $this->uastring, $regs)) {
128         $gdate = $regs[1];
129       }
130     }
131   return $gdate;
132   }
133 }
134 ?>