<?php

/**
* constants for browser version syntax
*/
define('MAJOR', 0);
define('MINOR', 1);
define('BUILD', 2);
define('REVISION', 3);
define('FULL', 4);

/**
* constants for browsers identifiers
*/
define('ALL',               'default');
define('INTERNET_EXPLORER', 'Internet Explorer');
define('FIREFOX',           'Firefox');
define('CHROME',          	'Google Chrome');
define('OPERA',             'Opera');
define('SAFARI',            'Safari');
define('NETSCAPE',          'Netscape');
define('MOZILLA',           'Mozilla');
define('WEBTV',             'WebTV');
define('KONQUEROR',         'Konqueror');
define('GALEON',            'Galeon');
define('NETPOSITIVE',       'NetPositive');
define('IE_POCKET',         'IEPocket');
define('POCKET',            'Pocket');


/**
* Class to detect which browser is accessing the website
*
* @author Cedric Francoys
*/
class egBrowser {
    /**
    *
    * @var string $version
    */
    var $version;

    /**
    *
    * @var $userAgent
    */
    var $userAgent;

    /**
    *
    * @var string $platform
    */
    var $platform;

    /**
    *
    * @var string $locale
    */
    var $locale;

    /**
    *
    * @var string type
    */
    var $type;

    /**
    * Class constructor
    *
    * @param void $
    * @return void
    */
    function egBrowser() {
        $this->type = null;
        $this->userAgent = $_SERVER['HTTP_USER_AGENT'];

        $this->detectOperatingSystem();
        $this->detectLocale();
        $this->detectBrowser();
    }

    /**
    */
    function getPlatform() {
        return $this->platform;
    }

    /**
    */
    function getType() {
        return $this->type;
    }

    /**
    * version syntax : major.minor[.build[.revision]]
    */
    function getVersion($part = FULL) {
        $elems = explode(".", $this->version);

        switch ($part) {
            case MAJOR :
                if (isset($elems[MAJOR])) return $elems[MAJOR];
                return 0;
            case MINOR :
                if (isset($elems[MINOR])) return $elems[MINOR];
                return 0;
            case BUILD :
                if (isset($elems[BUILD])) return $elems[BUILD];
                return 0;
            case REVISION :
                if (isset($elems[REVISION])) return $elems[REVISION];
                return 0;
        }
        return $this->version;
    }

    /**
    * Method to check for current browser
    *
    * @param string $ browser to compare with
    * @return bool true if current browser matches the given browser
    */
    function isBrowser($browser) {
        if (isset($this->type) && $this->type == $browser) {
            return true;
        }
        return false;
    }

    /**
    * Method to get the browser details from the USER_AGENT string
    *
    * @param void $
    * @return string property platform
    */
    function detectOperatingSystem() {
        $signatures['Windows']  = "win";
        $signatures['Linux']    = "linux";
        $signatures['MacOS']    = "mac";
        $signatures['OS/2']     = "OS/2";
        $signatures['BeOS']     = "BeOS";

        foreach($signatures as $key => $value) {
            if (eregi($value, $this->userAgent)) {
                $this->platform = $key;
                break;
            }
        }
    }

    /**
    * Method to get the locale details from the USER_AGENT string
    *
    * @param void $
    * @return string property platform
    */
    function detectLocale() {
  		$locale = array(
      	'af'     => 'Afrikaans',
  		'sq'     => 'Albanian',
  		'ar-ae'  => 'Arabic - United Arab Emirates',
  		'ar-bh'  => 'Arabic - Bahrain',
  		'ar-dz'  => 'Arabic - Algeria',
  		'ar-eg'  => 'Arabic - Egypt',
  		'ar-iq'  => 'Arabic - Iraq',
  		'ar-jo'  => 'Arabic - Jordan',
  		'ar-kw'  => 'Arabic - Kuwait',
  		'ar-lb'  => 'Arabic - Lebanon',
  		'ar-ly'  => 'Arabic - Libya',
  		'ar-ma'  => 'Arabic - Morocco',
  		'ar-om'  => 'Arabic - Oman',
  		'ar-qa'  => 'Arabic - Qatar',
  		'ar-sa'  => 'Arabic - Saudi Arabia',
  		'ar-sy'  => 'Arabic - Syria',
  		'ar-tn'  => 'Arabic - Tunisia',
  		'ar-ye'  => 'Arabic - Yemen',
  		'hy'     => 'Armenian',
  		'az-az'  => 'Azeri - Latin',
  		'az-az'  => 'Azeri - Cyrillic',
  		'eu'     => 'Basque',
  		'be'     => 'Belarusian',
  		'bg'     => 'Bulgarian',
  		'ca'     => 'Catalan',
  		'zh-cn'  => 'Chinese - China',
  		'zh-hk'  => 'Chinese - Hong Kong S.A.R.',
  		'zh-mo'  => 'Chinese - Macau S.A.R',
  		'zh-sg'  => 'Chinese - Singapore',
  		'zh-tw'  => 'Chinese - Taiwan',
  		'hr'     => 'Croatian',
  		'cs'     => 'Czech',
  		'da'     => 'Danish',
  		'nl-nl'  => 'Dutch - The Netherlands',
  		'nl-be'  => 'Dutch - Belgium',
  		'en-au'  => 'English - Australia',
  		'en-bz'  => 'English - Belize',
  		'en-ca'  => 'English - Canada',
  		'en-cb'  => 'English - Carribbean',
  		'en-ie'  => 'English - Ireland',
  		'en-jm'  => 'English - Jamaica',
  		'en-nz'  => 'English - New Zealand',
  		'en-ph'  => 'English - Phillippines',
  		'en-za'  => 'English - South Africa',
  		'en-tt'  => 'English - Trinidad',
  		'en-gb'  => 'English - United Kingdom',
  		'en-us'  => 'English - United States',
  		'et'     => 'Estonian',
  		'fa'     => 'Farsi',
  		'fi'     => 'Finnish',
  		'fo'     => 'Faroese',
  		'fr-fr'  => 'French - France',
  		'fr-be'  => 'French - Belgium',
  		'fr-ca'  => 'French - Canada',
  		'fr-lu'  => 'French - Luxembourg',
  		'fr-ch'  => 'French - Switzerland',
  		'gd-ie'  => 'Gaelic - Ireland',
  		'gd'     => 'Gaelic - Scotland',
  		'de-de'  => 'German - Germany',
  		'de-at'  => 'German - Austria',
  		'de-li'  => 'German - Liechtenstein',
  		'de-lu'  => 'German - Luxembourg',
  		'de-ch'  => 'German - Switzerland',
  		'el'     => 'Greek',
  		'he'     => 'Hebrew',
  		'hi'     => 'Hindi',
  		'hu'     => 'Hungarian',
  		'is'     => 'Icelandic',
  		'id'     => 'Indonesian',
  		'it-it'  => 'Italian - Italy',
  		'it-ch'  => 'Italian - Switzerland',
  		'ja'     => 'Japanese',
  		'ko'     => 'Korean',
  		'lv'     => 'Latvian',
  		'lt'     => 'Lithuanian',
  		'mk'     => 'FYRO Macedonian',
  		'ms-my'  => 'Malay - Malaysia',
  		'ms-bn'  => 'Malay - Brunei',
  		'mt'     => 'Maltese',
  		'mr'     => 'Marathi',
  		'no-no'  => 'Norwegian - Bokmål',
  		'no-no'  => 'Norwegian - Nynorsk',
  		'pl'     => 'Polish',
  		'pt-pt'  => 'Portuguese - Portugal',
  		'pt-br'  => 'Portuguese - Brazil',
  		'rm'     => 'Raeto-Romance',
  		'ro'     => 'Romanian - Romania',
  		'ro-mo'  => 'Romanian - Moldova',
  		'ru'     => 'Russian',
  		'ru-mo'  => 'Russian - Moldova',
  		'sa'     => 'Sanskrit',
  		'sr-sp'  => 'Serbian - Cyrillic',
  		'sr-sp'  => 'Serbian - Latin',
  		'tn'     => 'Setsuana',
  		'sl'     => 'Slovenian',
  		'sk'     => 'Slovak',
  		'sb'     => 'Sorbian',
  		'es-es'  => 'Spanish - Spain',
  		'es-ar'  => 'Spanish - Argentina',
  		'es-bo'  => 'Spanish - Bolivia',
  		'es-cl'  => 'Spanish - Chile',
  		'es-co'  => 'Spanish - Colombia',
  		'es-cr'  => 'Spanish - Costa Rica',
  		'es-do'  => 'Spanish - Dominican Republic',
  		'es-ec'  => 'Spanish - Ecuador',
  		'es-gt'  => 'Spanish - Guatemala',
  		'es-hn'  => 'Spanish - Honduras',
  		'es-mx'  => 'Spanish - Mexico',
  		'es-ni'  => 'Spanish - Nicaragua',
  		'es-pa'  => 'Spanish - Panama',
  		'es-pe'  => 'Spanish - Peru',
  		'es-pr'  => 'Spanish - Puerto Rico',
  		'es-py'  => 'Spanish - Paraguay',
  		'es-sv'  => 'Spanish - El Salvador',
  		'es-uy'  => 'Spanish - Uruguay',
  		'es-ve'  => 'Spanish - Venezuela',
  		'sx'     => 'Sutu',
  		'sw'     => 'Swahili',
  		'sv-se'  => 'Swedish - Sweden',
  		'sv-fi'  => 'Swedish - Finland',
  		'ta'     => 'Tamil',
  		'tt'     => 'Tatar',
  		'th'     => 'Thai',
  		'tr'     => 'Turkish',
  		'ts'     => 'Tsonga',
  		'uk'     => 'Ukrainian',
  		'ur'     => 'Urdu',
  		'uz-uz'  => 'Uzbek - Cyrillic',
  		'uz-uz'  => 'Uzbek - Latin',
  		'vi'     => 'Vietnamese',
  		'xh'     => 'Xhosa',
  		'yi'     => 'Yiddish',
  		'zu'     => 'Zulu');

  		$this->locale = 'unknown';

      foreach($locale as $key => $value) {
          if(eregi($key, $this->userAgent)) {
              $this->locale = $value;
              break;
          }
      }
    }

    function detectBrowser() {
        $signatures[INTERNET_EXPLORER] = "msie";
        $signatures[FIREFOX]           = "Firefox";
//        $signatures[CHROME]            = "Chrome";
        $signatures[OPERA]             = "opera";
        $signatures[SAFARI]            = "Safari";
        $signatures[NETSCAPE]          = "netscape";
        $signatures[MOZILLA]           = "mozilla";
        $signatures[WEBTV]             = "webtv";
        $signatures[KONQUEROR]         = "Konqueror";
        $signatures[GALEON]            = "galeon";
        $signatures[NETPOSITIVE]       = "NetPositive";
        $signatures[IE_POCKET]         = "mspie";
        $signatures[POCKET]            = "pocket";

        $this->type = '';

        foreach($signatures as $browser_id => $signature) {
            if(strlen($this->type) > 0) {
              break;
            }
            if(eregi($signature, $this->userAgent)) {
                switch($browser_id) {
                    case INTERNET_EXPLORER:
                        $val = explode(" ", stristr($this->userAgent, $signature));
                        $this->version = substr_replace($val[1], "", -1);
                        if (!eregi("opera", $this->userAgent)) {
                            $this->type = $browser_id;
                            break;
                        }
                    case OPERA:
                        $val = stristr($this->userAgent, $signature);
                        if (eregi("/", $val)) {
                            $val = explode("/", $val);
                            $val = explode(" ", $val[1]);
                            $this->version = $val[0];
                        } else {
                            $val = explode(" ", stristr($val, $signature));
                            $this->version = $val[1];
                        }
                        $this->type = $browser_id;
                        break;
                    case FIREFOX:
                        $val = stristr($this->userAgent, $signature);
                        $val = explode("/", $val);
                        $this->version = $val[1];
                        $this->type = $browser_id;
                        break;
                    case SAFARI:
                        $val = stristr($this->userAgent, $signature);
                        $val = explode("/", $val);
                        $this->version = $val[1];
                        $this->type = $browser_id;
                        break;
                    case MOZILLA:
                        $val = explode(" ", stristr($this->userAgent, "rv:"));
                        eregi("rv:[0-9]\.[0-9]\.[0-9]", $this->userAgent, $val);
                        $this->version = str_replace("rv:", "", $val[0]);
                        if(!eregi("netscape", $this->userAgent)) {
                            $this->type = $browser_id;
                            break;
                        }
                    case NETSCAPE:
                        $val = explode(" ", stristr($this->userAgent, $signature));
                        $val = explode("/", $val[0]);
                        $this->version = $val[1];
                        $this->type = $browser_id;
                        break;
                    case KONQUEROR:
                        $val = explode(" ", stristr($this->userAgent, $signature));
                        $val = explode("/", $val[0]);
                        $this->version = str_replace(")", "", $val[1]);
                        $this->type = $browser_id;
                        break;
                    case GALEON:
                        $val = explode(" ", stristr($this->userAgent, $signature));
                        $val = explode("/", $val[0]);
                        $this->version = $val[1];
                        $this->type = $browser_id;
                        break;
                    case NETPOSITIVE:
                        $val = explode("/", stristr($this->userAgent, $signature));
                        $this->version = $val[1];
                        $this->type = $browser_id;
                        break;
                    case WEBTV:
                        $val = explode("/", stristr($this->userAgent, $signature));
                        $this->version = $val[1];
                        $this->type = $browser_id;
                        break;
                    case IE_POCKET:
                    case POCKET:
                        $val = explode(" ", stristr($this->userAgent, $signature));
                        $this->platform = "WindowsCE";
                        if (eregi("mspie", $this->userAgent))
                            $this->version = $val[1];
                        else {
                            $val = explode("/", $this->userAgent);
                            $this->version = $val[1];
                        }
                        $this->type = $browser_id;
                        break;
                }
                break;
            }
        }
    }
}
?>