Source for file OCSP_BROWSER.phpclass

Documentation is available at OCSP_BROWSER.phpclass

  1. <?php
  2. /*****************************************************************
  3.  
  4.     File name: browser.php
  5.     Author: Gary White
  6.     Last modified: November 10, 2003
  7.  
  8.     **************************************************************
  9.  
  10.     Copyright (C) 2003  Gary White
  11.  
  12.     This program is free software; you can redistribute it and/or
  13.     modify it under the terms of the GNU General Public License
  14.     as published by the Free Software Foundation; either version 2
  15.     of the License, or (at your option) any later version.
  16.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details at:
  21.     http://www.gnu.org/copyleft/gpl.html
  22.  
  23.     **************************************************************
  24.  
  25.     Browser class
  26.  
  27.     Identifies the user's Operating system, browser and version
  28.     by parsing the HTTP_USER_AGENT string sent to the server
  29.  
  30.     Typical Usage:
  31.  
  32.         require_once($_SERVER['DOCUMENT_ROOT'].'/include/browser.php');
  33.         $br = new Browser;
  34.         echo "$br->Platform, $br->Name version $br->Version";
  35.  
  36.     For operating systems, it will correctly identify:
  37.         Microsoft Windows
  38.         MacIntosh
  39.         Linux
  40.  
  41.     Anything not determined to be one of the above is considered to by Unix
  42.     because most Unix based browsers seem to not report the operating system.
  43.     The only known problem here is that, if a HTTP_USER_AGENT string does not
  44.     contain the operating system, it will be identified as Unix. For unknown
  45.     browsers, this may not be correct.
  46.  
  47.     For browsers, it should correctly identify all versions of:
  48.         Amaya
  49.         Galeon
  50.         iCab
  51.         Internet Explorer
  52.             For AOL versions it will identify as Internet Explorer (AOL) and the version
  53.             will be the AOL version instead of the IE version.
  54.         Konqueror
  55.         Lynx
  56.         Mozilla
  57.         Netscape Navigator/Communicator
  58.         OmniWeb
  59.         Opera
  60.         Pocket Internet Explorer for handhelds
  61.         Safari
  62.         WebTV
  63. *****************************************************************/
  64.  
  65. class OCSP_BROWSER{
  66.  
  67.     /**
  68.      * singelton
  69.      *
  70.      * @staticvar OCSP_BROWSER $myInstance 
  71.      * @since pk-08-05-15
  72.      */    
  73.     static $myInstance Null;
  74.         
  75.     var $Name = "Unknown";
  76.     var $Version = "Unknown";
  77.     var $Platform = "Unknown";
  78.     var $UserAgent = "Not reported";
  79.     var $AOL = false;
  80.  
  81.     /**
  82.      * returns the current instance
  83.      * 
  84.      * @return OCSP_BROWSER 
  85.      */
  86.     static function getInstance()
  87.     {
  88.         if (!is_object(self::$myInstance))
  89.         {
  90.             self::$myInstance new OCSP_BROWSER();
  91.         
  92.         
  93.         return self::$myInstance;
  94.     }
  95.     
  96.     
  97.     function OCSP_BROWSER(){
  98.         $agent $_SERVER['HTTP_USER_AGENT'];
  99.  
  100.         // initialize properties
  101.         $bd['platform'"Unknown";
  102.         $bd['browser'"Unknown";
  103.         $bd['version'"Unknown";
  104.         $this->UserAgent = $agent;
  105.  
  106.         // find operating system
  107.         if (eregi("win"$agent))
  108.             $bd['platform'"Windows";
  109.         elseif (eregi("mac"$agent))
  110.             $bd['platform'"MacIntosh";
  111.         elseif (eregi("linux"$agent))
  112.             $bd['platform'"Linux";
  113.         elseif (eregi("OS/2"$agent))
  114.             $bd['platform'"OS/2";
  115.         elseif (eregi("BeOS"$agent))
  116.             $bd['platform'"BeOS";
  117.  
  118.         // test for Opera
  119.         if (eregi("opera",$agent)){
  120.             $val stristr($agent"opera");
  121.             if (eregi("/"$val)){
  122.                 $val explode("/",$val);
  123.                 $bd['browser'$val[0];
  124.                 $val explode(" ",$val[1]);
  125.                 $bd['version'$val[0];
  126.             }else{
  127.                 $val explode(" ",stristr($val,"opera"));
  128.                 $bd['browser'$val[0];
  129.                 $bd['version'$val[1];
  130.             }
  131.  
  132.         // test for WebTV
  133.         }elseif(eregi("webtv",$agent)){
  134.             $val explode("/",stristr($agent,"webtv"));
  135.             $bd['browser'$val[0];
  136.             $bd['version'$val[1];
  137.  
  138.         // test for MS Internet Explorer version 1
  139.         }elseif(eregi("microsoft internet explorer"$agent)){
  140.             $bd['browser'"MSIE";
  141.             $bd['version'"1.0";
  142.             $var stristr($agent"/");
  143.             if (ereg("308|425|426|474|0b1"$var)){
  144.                 $bd['version'"1.5";
  145.             }
  146.  
  147.         // test for NetPositive
  148.         }elseif(eregi("NetPositive"$agent)){
  149.             $val explode("/",stristr($agent,"NetPositive"));
  150.             $bd['platform'"BeOS";
  151.             $bd['browser'$val[0];
  152.             $bd['version'$val[1];
  153.  
  154.         // test for MS Internet Explorer
  155.         }elseif(eregi("msie",$agent&& !eregi("opera",$agent)){
  156.             $val explode(" ",stristr($agent,"msie"));
  157.             $bd['browser'$val[0];
  158.             $bd['version'$val[1];
  159.  
  160.         // test for MS Pocket Internet Explorer
  161.         }elseif(eregi("mspie",$agent|| eregi('pocket'$agent)){
  162.             $val explode(" ",stristr($agent,"mspie"));
  163.             $bd['browser'"MSPIE";
  164.             $bd['platform'"WindowsCE";
  165.             if (eregi("mspie"$agent))
  166.                 $bd['version'$val[1];
  167.             else {
  168.                 $val explode("/",$agent);
  169.                 $bd['version'$val[1];
  170.             }
  171.  
  172.         // test for Galeon
  173.         }elseif(eregi("galeon",$agent)){
  174.             $val explode(" ",stristr($agent,"galeon"));
  175.             $val explode("/",$val[0]);
  176.             $bd['browser'$val[0];
  177.             $bd['version'$val[1];
  178.  
  179.         // test for Konqueror
  180.         }elseif(eregi("Konqueror",$agent)){
  181.             $val explode(" ",stristr($agent,"Konqueror"));
  182.             $val explode("/",$val[0]);
  183.             $bd['browser'$val[0];
  184.             $bd['version'$val[1];
  185.  
  186.         // test for iCab
  187.         }elseif(eregi("icab",$agent)){
  188.             $val explode(" ",stristr($agent,"icab"));
  189.             $bd['browser'$val[0];
  190.             $bd['version'$val[1];
  191.  
  192.         // test for OmniWeb
  193.         }elseif(eregi("omniweb",$agent)){
  194.             $val explode("/",stristr($agent,"omniweb"));
  195.             $bd['browser'$val[0];
  196.             $bd['version'$val[1];
  197.  
  198.         // test for Phoenix
  199.         }elseif(eregi("Phoenix"$agent)){
  200.             $bd['browser'"Phoenix";
  201.             $val explode("/"stristr($agent,"Phoenix/"));
  202.             $bd['version'$val[1];
  203.  
  204.         // test for Firebird
  205.         }elseif(eregi("firebird"$agent)){
  206.             $bd['browser']="Firebird";
  207.             $val stristr($agent"Firebird");
  208.             $val explode("/",$val);
  209.             $bd['version'$val[1];
  210.  
  211.         // test for Firefox
  212.         }elseif(eregi("Firefox"$agent)){
  213.             $bd['browser']="Firefox";
  214.             $val stristr($agent"Firefox");
  215.             $val explode("/",$val);
  216.             $bd['version'$val[1];
  217.  
  218.       // test for Mozilla Alpha/Beta Versions
  219.         }elseif(eregi("mozilla",$agent&&
  220.             eregi("rv:[0-9].[0-9][a-b]",$agent&& !eregi("netscape",$agent)){
  221.             $bd['browser'"Mozilla";
  222.             $val explode(" ",stristr($agent,"rv:"));
  223.             eregi("rv:[0-9].[0-9][a-b]",$agent,$val);
  224.             $bd['version'str_replace("rv:","",$val[0]);
  225.  
  226.         // test for Mozilla Stable Versions
  227.         }elseif(eregi("mozilla",$agent&&
  228.             eregi("rv:[0-9]\.[0-9]",$agent&& !eregi("netscape",$agent)){
  229.             $bd['browser'"Mozilla";
  230.             $val explode(" ",stristr($agent,"rv:"));
  231.             eregi("rv:[0-9]\.[0-9]\.[0-9]",$agent,$val);
  232.             $bd['version'str_replace("rv:","",$val[0]);
  233.  
  234.         // test for Lynx & Amaya
  235.         }elseif(eregi("libwww"$agent)){
  236.             if (eregi("amaya"$agent)){
  237.                 $val explode("/",stristr($agent,"amaya"));
  238.                 $bd['browser'"Amaya";
  239.                 $val explode(" "$val[1]);
  240.                 $bd['version'$val[0];
  241.             else {
  242.                 $val explode("/",$agent);
  243.                 $bd['browser'"Lynx";
  244.                 $bd['version'$val[1];
  245.             }
  246.  
  247.         // test for Safari
  248.         }elseif(eregi("safari"$agent)){
  249.             $bd['browser'"Safari";
  250.             $bd['version'"";
  251.  
  252.         // remaining two tests are for Netscape
  253.         }elseif(eregi("netscape",$agent)){
  254.             $val explode(" ",stristr($agent,"netscape"));
  255.             $val explode("/",$val[0]);
  256.             $bd['browser'$val[0];
  257.             $bd['version'$val[1];
  258.         }elseif(eregi("mozilla",$agent&& !eregi("rv:[0-9]\.[0-9]\.[0-9]",$agent)){
  259.             $val explode(" ",stristr($agent,"mozilla"));
  260.             $val explode("/",$val[0]);
  261.             $bd['browser'"Netscape";
  262.             $bd['version'$val[1];
  263.         }
  264.  
  265.         // clean up extraneous garbage that may be in the name
  266.         $bd['browser'ereg_replace("[^a-z,A-Z]"""$bd['browser']);
  267.         // clean up extraneous garbage that may be in the version
  268.         $bd['version'ereg_replace("[^0-9,.,a-z,A-Z]"""$bd['version']);
  269.  
  270.         // check for AOL
  271.         if (eregi("AOL"$agent)){
  272.             $var stristr($agent"AOL");
  273.             $var explode(" "$var);
  274.             $bd['aol'ereg_replace("[^0-9,.,a-z,A-Z]"""$var[1]);
  275.         }
  276.  
  277.         // finally assign our properties
  278.         $this->Name = strtoupper($bd['browser'])// <pk-07-05-09 />
  279.         $this->Version = $bd['version'];
  280.         $this->Platform = $bd['platform'];
  281.         $this->AOL = (isset($bd['aol']$bd['aol'"")// <pk-07-05-09 />
  282.     }
  283.  
  284.     /**
  285.       * returns if we have a m$ internet explorer
  286.       *
  287.       * @return boolean 
  288.       * @access public
  289.       *
  290.       * @since pk-07-05-09
  291.       */
  292.     function isIE()
  293.     {
  294.         return (($this->Name=="MSIE"|| ($this->Name == "MSPIE"));
  295.     }
  296.  
  297.     /**
  298.       * returns an ID-String (Name_version) to identify a browser
  299.       *
  300.       * @return string 
  301.       * @access public
  302.       * @since pk-07-05-09
  303.       */
  304.     function getId()
  305.     {
  306.         return $this->Name."-".$this->Version;
  307.     }
  308.  
  309.     /**
  310.       * returns the main version
  311.       *
  312.       * @return mixed (int if the first part of version is an int
  313.       */
  314.     function getMainVersionNr()
  315.     {
  316.         if (strstr($this->version,'.'))
  317.         {
  318.             $arr_version=explode('.',$this->version);
  319.             if (intval($arr_version[0]))
  320.             {
  321.                 return intval($arr_version[0]);
  322.             else {
  323.                 return $arr_version[0];
  324.             }
  325.         else {
  326.             if (intval($this->version))
  327.             {
  328.                 return intval($this->version);
  329.             else {
  330.                 return $this->version;
  331.             }
  332.         }
  333.     }
  334. }
  335. ?>

Documentation generated on Thu, 08 Jan 2009 17:45:10 +0100 by phpDocumentor 1.4.0a2