make user agent detection work better and for more browsers
[php-utility-classes.git] / include / classes / useragent.php-class
index 8250bc8c0e7695b7eacfb07390e1fd5f63e3187e..534dd716e2b65cef908dab41fd554a78edf79101 100755 (executable)
@@ -3,8 +3,8 @@ class userAgent {
   // userAgent PHP class
   // get user agent and tell us what Browser is accessing
   //
-  // function userAgent()
-  //   CONSTRUCTOR; reads UA string and gets info from that into our variables.
+  // function userAgent([$ua_string])
+  //   CONSTRUCTOR; reads UA string (or takes the optional given UA string) and gets info from that into our variables.
   //
   // var $uastring
   //   the plain User Agent string
@@ -23,52 +23,100 @@ class userAgent {
   //   returns true if User Agent seems to be a Gecko-based browser, false if not
   // function geckodate()
   //   returns the Gecko date when it's a Gecko-based browser, 0 if not
+  // function khtmlbased()
+  //   returns true if User Agent seems to be a KHTML-based browser, false if not
+
+  // collection of some known User Agent Strings:
+  // Mozilla/5.0 (compatible; Konqueror/3; Linux 2.4.18; X11; i686)
+  // Mozilla/5.0 (X11; U; Linux i686; de-AT; rv:1.3b) Gecko/20030114
+  // Mozilla/4.0 (compatible; MSIE 5.0; Windows 95; DigExt)
+  // Mozilla/4.0 (compatible; MSIE 6.0; Windows 98; Win 9x 4.90)
+  // Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
+  // Mozilla/4.75 [de] (Win98; U)
+  // Opera/5.12 (Windows 2000; U)  [de]
+  // Mozilla/5.0 (Windows; U; Win 9x 4.90; de-DE; m18) Gecko/20010131 Netscape6/6.01
+  // Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE; rv:1.0.1) Gecko/20020823 Netscape/7.0
+  // Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/51 (like Gecko) Safari/51
+  // Lynx/2.8.4rel.1 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/0.9.6g
+  // Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-US; rv:1.0.1) Gecko/20021109 Chimera/0.6+
+  // Mozilla/5.0 (Windows; U; Win 9x 4.90; en-US; rv:1.3a) Gecko/20021207 Phoenix/0.5
+  // Mozilla/5.0 Galeon/1.2.7 (X11; Linux i686; U;) Gecko/20021204
+  // Mozilla/4.0 (compatible; MSIE 5.0; Windows XP) Opera 6.05 [ja]
+  // Mozilla/4.0 (compatible; MSIE 5.12; Mac_PowerPC) OmniWeb/4.1.1-v424.6
 
-  // debug: var $uastring = "Mozilla/5.0 (compatible; Konqueror/3; Linux 2.4.18; X11; i686)";
   var $uastring;
   var $brand;
   var $version;
 
-  function userAgent() {
+  function userAgent($ua_string = "") {
     // *** constructor ***
-    // get raw UA string
-    $this->uastring = $_SERVER["HTTP_USER_AGENT"];
+    if (strlen($ua_string)) {
+      $this->uastring = $ua_string;
+    }
+    else {
+      // read raw UA string
+      $this->uastring = $_SERVER["HTTP_USER_AGENT"];
+    }
 
     // get UA brand and version
     $this->brand = "Unknown"; $this->version = 0;
-    if (ereg("([0-9a-zA-Z\.]+)/([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
-      $this->uabrand = $regs[1]; // this is a reasonable default :)
+    if (ereg("([0-9a-zA-Z\.]+)/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
+      $this->brand = $regs[1]; // this is a reasonable default :)
       $this->version = $regs[2]; // this is a reasonable default :)
     }
-    if (ereg("Mozilla/([0-9a-zA-Z\.]+)", $this->uastring, $regs) && !strstr($this->uastring, "compatible;") && !strstr($this->uastring, "Gecko/")) {
+    if (ereg("Netscape6/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
       $this->brand = "Netscape";
       $this->version = $regs[1];
-      if (intval($this->version) == 4) { $this->brand .= " Communicator"; }
     }
-    elseif (ereg("Netscape6/([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
+    elseif (ereg("Netscape/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
       $this->brand = "Netscape";
       $this->version = $regs[1];
     }
-    elseif (ereg("Netscape/([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
-      $this->brand = "Netscape";
+    elseif (ereg("Chimera/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
+      $this->brand = "Chimera";
+      $this->version = $regs[1];
+    }
+    elseif (ereg("Phoenix/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
+      $this->brand = "Phoenix";
+      $this->version = $regs[1];
+    }
+    elseif (ereg("Galeon/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
+      $this->brand = "Galeon";
       $this->version = $regs[1];
     }
-    elseif (ereg("rv:([0-9a-zA-Z\.]+)", $this->uastring, $regs) && strstr($this->uastring, "Mozilla/") && strstr($this->uastring, "Gecko/")) {
+    elseif (ereg("rv:([0-9a-zA-Z\.+]+)", $this->uastring, $regs) && strstr($this->uastring, "Mozilla/") && strstr($this->uastring, "Gecko/")) {
       $this->brand = "Mozilla";
       $this->version = $regs[1];
     }
-    elseif (ereg("Opera[ /]([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
+    elseif (ereg("Opera[ /]([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
       $this->brand = "Opera";
       $this->version = $regs[1];
     }
-    elseif (ereg("Konqueror/([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
+    elseif (ereg("OmniWeb/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
+      $this->brand = "OmniWeb";
+      $this->version = $regs[1];
+    }
+    elseif (ereg("Konqueror/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
       $this->brand = "Konqueror";
       $this->version = $regs[1];
     }
-    elseif (ereg("MSIE ([0-9a-zA-Z\.]+)", $this->uastring, $regs)) {
+    elseif (ereg("Safari/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
+      $this->brand = "Safari";
+      $this->version = $regs[1];
+    }
+    elseif (ereg("AppleWebKit/([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
+      $this->brand = "AppleWebKit";
+      $this->version = $regs[1];
+    }
+    elseif (ereg("MSIE ([0-9a-zA-Z\.+]+)", $this->uastring, $regs)) {
       $this->brand = "Microsoft Internet Explorer";
       $this->version = $regs[1];
     }
+    elseif (ereg("Mozilla/([0-9a-zA-Z\.+]+)", $this->uastring, $regs) && !strstr($this->uastring, "compatible;") && !strstr($this->uastring, "Gecko/")) {
+      $this->brand = "Netscape";
+      $this->version = $regs[1];
+      if (intval($this->version) == 4) { $this->brand .= " Communicator"; }
+    }
   }
 
   function isns() {
@@ -130,5 +178,17 @@ class userAgent {
     }
   return $gdate;
   }
+
+  function khtmlbased() {
+    // set it static so that we don't have to call it that often
+    static $is_khtml;
+    if (!isset($is_khtml)) {
+      $is_khtml = false;
+      if (strstr($this->brand, "Konqueror") || strstr($this->brand, "Safari") || strstr($this->brand, "AppleWebKit")) {
+        $is_khtml = true;
+      }
+    }
+  return $is_khtml;
+  }
 }
 ?>
\ No newline at end of file