Advertisement
SidOfc

client.class.php

Oct 14th, 2013
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 10.82 KB | None | 0 0
  1. <?php
  2.     class Client {
  3.         const VERSION = 1;
  4.         const AUTHOR = 'Sidney Liebrand';
  5.  
  6.         public static $Browser = null;
  7.         public static $System = null;
  8.         public static $Execution = null;
  9.         private static $Compatibility = 0;
  10.         private static $browsers = array(
  11.             'Opera' => array(
  12.                 'name' => '/(OPR)|(Opera)/i',
  13.                 'alts' => '',
  14.                 'version' => '/(\sOPR\/([0-9\.]+))|(\sVersion\/([0-9\.]+))|(\sOpera\/([0-9\.]+))|(\sOpera\s([0-9\.]+))/i',
  15.                 'engine' => array(
  16.                     'name' => '/(\sGecko)|((\s|)Presto)|(\sAppleWebKit)/i',
  17.                     'version' => '/(\sAppleWebKit\/([0-9\.]+)\s)|(\srv:([0-9\.]+))|((\s|)Presto\/([0-9\.]+)\s)/i'
  18.                 )
  19.             ),
  20.             'Msie' => array(
  21.                 'name' => '/MSIE/i',
  22.                 'alts' => '',
  23.                 'version' => '/(\s|)MSIE\s([0-9\.b]+)/',
  24.                 'engine' => array(
  25.                     'name' => '!Trident',
  26.                     'version' => '/\sTrident\/([0-9\.]+)(;|\s|)/i'
  27.                 )
  28.             ),
  29.             'Maxthon' => array(
  30.                 'name' => '/Maxthon/i',
  31.                 'alts' => '',
  32.                 'version' => '/\sMaxthon(\/|[\s]{1})([0-9\.]+)/i',
  33.                 'engine' => array(
  34.                     'name' => '/(\sGecko)|(\sPresto)|(\sAppleWebKit)|(Trident)/i',
  35.                     'version' => '/(\sTrident\/([0-9\.]+))|(\sAppleWebKit\/([0-9\.]+))/i'
  36.                 )
  37.             ),
  38.             'Firefox' => array(
  39.                 'name' => '/Firefox|Iceweasel|Firebird|IceCat|Netscape|SeaMonkey|Conkeror|Kazehakase/i',
  40.                 'alts' => '/\sIceweasel|Firebird|IceCat|Netscape|SeaMonkey|Conkeror|Kazehakase/i',
  41.                 'version' => '/\sFirefox\/([0-9\.]+)|Iceweasel\/([0-9\.]+)|Firebird\/([0-9\.]+)|IceCat\/([0-9\.]+)|Netscape\/([0-9\.]+)|SeaMonkey(\/[0-9\.]+)|Conkeror(\/[0-9\.]+)|Kazehakase(\/[0-9\.]+)/i',
  42.                 'engine' => array(
  43.                     'name' => '!Gecko',
  44.                     'version' => '/\srv:([0-9\.]+)/i'
  45.                 )
  46.             ),
  47.             'Konqueror' => array(
  48.                 'name' => '/Konqueror/',
  49.                 'alts' => '',
  50.                 'version' => '/Konqueror\/([0-9\.]+)/',
  51.                 'engine' => array(
  52.                     'name' => '!Gecko',
  53.                     'version' => '/KHTML\/([0-9\.]+)/i'
  54.                 )
  55.             ),
  56.             'Rockmelt' => array(
  57.                 'name' => '/RockMelt/i',
  58.                 'alts' => '',
  59.                 'version' => '/\sRockMelt\/([0-9\.]+)/i',
  60.                 'engine' => array(
  61.                     'name' => '!Webkit',
  62.                     'version' => '/\sAppleWebKit\/([0-9\.]+)/i'
  63.                 )
  64.             ),
  65.             'Chrome' => array(
  66.                 'name' => '/Chrome|Comodo/i',
  67.                 'alts' => '',
  68.                 'version' => '/\sChrome\/([0-9\.]+)/i',
  69.                 'engine' => array(
  70.                     'name' => '!Webkit',
  71.                     'version' => '/\sAppleWebKit\/([0-9\.]+)/i'
  72.                 )
  73.             ),
  74.             'Safari' => array(
  75.                 'name' => '/Safari/i',
  76.                 'alts' => '',
  77.                 'version' => '/\sVersion\/([0-9\.]+)/i',
  78.                 'engine' => array(
  79.                     'name' => '!Webkit',
  80.                     'version' => '/\sAppleWebKit\/([0-9\.]+)/i'
  81.                 )
  82.             )
  83.         );
  84.  
  85.         public static function init() {
  86.             static::$Execution = new Stdclass;
  87.             static::$Execution->Time = new Stdclass();
  88.             static::$Execution->Memory = new Stdclass();
  89.  
  90.             static::setExecutionStart();
  91.  
  92.             static::$Browser = new Stdclass;
  93.             static::$Browser->Engine = new Stdclass;
  94.             static::$Browser->Encoding = new Stdclass;
  95.             static::$Browser->Cookies = new Stdclass;
  96.             static::$System = new Stdclass;
  97.             static::getUserAgent();
  98.             static::getInfo();
  99.  
  100.             static::setExecutionEnd();
  101.             static::setExecutionVars();
  102.         }
  103.  
  104.         private function __construct() {}
  105.         private function __clone() {}
  106.  
  107.         private static function getUserAgent($userAgent = null) {
  108.             $pattern = '/Mozilla\/[0-9\.]+(\s|)/';
  109.             if ($userAgent === null || strlen($userAgent) === 0)
  110.                 $userAgent = $_SERVER['HTTP_USER_AGENT'];
  111.             static::$Browser->UserAgent = preg_replace($pattern, '', $userAgent);
  112.         }
  113.  
  114.         private static function getInfo() {
  115.             static::setCompatibility();
  116.             static::getBrowserName();
  117.             if (static::$Browser->Name) {
  118.                 static::getAltBrowserName();
  119.                 static::getBrowserVersion();
  120.                 static::getEngineName();
  121.                 static::getEngineVersion();
  122.                 static::getBrowserEncoding();
  123.                 static::getBrowserLanguage();
  124.                 static::getCookiesEnabled();
  125.                 static::getClientSystem();
  126.                 static::getMobile();
  127.                 static::getHTMLClasses();
  128.             }
  129.         }
  130.  
  131.         private static function setExecutionVars() {
  132.             static::$Execution->Time->Total = (static::$Execution->Time->End - static::$Execution->Time->Start);
  133.             static::$Execution->Memory->Total = (static::$Execution->Memory->End - static::$Execution->Memory->Start);
  134.         }
  135.  
  136.         private static function setExecutionStart() {
  137.             static::$Execution->Time->Start = microtime(true);
  138.             static::$Execution->Memory->Start = memory_get_usage();
  139.         }
  140.  
  141.         private static function setExecutionEnd() {
  142.             static::$Execution->Time->End = microtime(true);
  143.             static::$Execution->Memory->End = memory_get_usage();
  144.         }
  145.  
  146.         private static function setCompatibility() {
  147.             static::$Compatibility = preg_match('/compatible(;|)/i', static::$Browser->UserAgent) ? 1 : 0;
  148.         }
  149.  
  150.         private static function getMobile() {
  151.             static::$System->Mobile = 0;
  152.             if (preg_match('/(Mobile)|(Phone)|(Android)|(iPhone)|(Windows\sPhone)/i', static::$Browser->UserAgent))
  153.                 static::$System->Mobile = 1;
  154.         }
  155.  
  156.         private static function getBrowserName() {
  157.             $detected = false;
  158.             while ($detected === false && list($name, $pattern) = each(static::$browsers))
  159.             if (preg_match($pattern['name'], static::$Browser->UserAgent))
  160.                 $detected = true;
  161.  
  162.             static::$Browser->Name = ucfirst(strtolower(($detected) ? $name : null));
  163.         }
  164.  
  165.         private static function getAltBrowserName() {
  166.             $pattern = strlen(static::$browsers[static::$Browser->Name]['alts']) > 0 ? static::$browsers[static::$Browser->Name]['alts'] : null;
  167.             if ($pattern === null) {
  168.                 static::$Browser->AltName = null;
  169.                 return false;
  170.             }
  171.             preg_match($pattern, static::$Browser->UserAgent, $altArr);
  172.             static::$Browser->AltName = isset($altArr[0]) ? $altArr[0] : null;
  173.         }
  174.  
  175.         private static function getBrowserVersion() {
  176.             $pattern = isset(static::$browsers[static::$Browser->Name]['version']) ? static::$browsers[static::$Browser->Name]['version'] : null;
  177.             if ($pattern === null) {
  178.                 static::$Browser->Version = null;
  179.                 return null;
  180.             }
  181.             preg_match($pattern, static::$Browser->UserAgent, $version);
  182.             $diff = (static::$Browser->Name === 'Opera');
  183.             $version = static::arrayClean($version);
  184.             static::$Browser->Version = isset($version[($diff ? 2 : 1)]) ? $version[($diff ? 2 : 1)] : (isset($version[0]) ? $version[0] : null);
  185.             static::$Browser->MajorVersion = static::getMajorVersion(static::$Browser->Version);
  186.         }
  187.  
  188.         private static function getEngineName() {
  189.             $engine = isset(static::$browsers[static::$Browser->Name]['engine']['name']) ? static::$browsers[static::$Browser->Name]['engine']['name'] : null;
  190.            
  191.             static::$Browser->Engine->Name = null;
  192.             if ($engine === null)
  193.                 return null;
  194.  
  195.             if (static::firstCharIs('!', $engine))
  196.                 static::$Browser->Engine->Name = substr($engine, 1);
  197.             else {
  198.                 preg_match($engine, static::$Browser->UserAgent, $engArr);
  199.                 $ENGINE = isset($engArr[1]) ? static::arrayClean($engArr)[1] : null;
  200.                 static::$Browser->Engine->Name = $ENGINE;
  201.             }
  202.         }
  203.  
  204.         private static function getEngineVersion() {
  205.             $pattern = isset(static::$browsers[static::$Browser->Name]['engine']['version']) ? static::$browsers[static::$Browser->Name]['engine']['version'] : null;
  206.             if (static::$Browser->Name === 'Msie' && static::$Browser->MajorVersion == 7) {
  207.                 static::$Browser->Engine->Version = 3.1;
  208.                 return true;
  209.             } else if ($pattern === null) {
  210.                 static::$Browser->Engine->Version = null;
  211.                 return null;
  212.             }
  213.             preg_match($pattern, static::$Browser->UserAgent, $engineVersion);
  214.             $diff = (static::$Browser->Name === 'Opera' || static::$Browser->Name === 'Maxthon');
  215.             static::$Browser->Engine->Version = isset($engineVersion[($diff ? 2 : 1)]) ? static::arrayClean($engineVersion)[($diff ? 2 : 1)] : null;
  216.             static::$Browser->Engine->MajorVersion = static::getMajorVersion(static::$Browser->Engine->Version);
  217.         }
  218.  
  219.         private static function getBrowserLanguage() {
  220.             if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']))
  221.                 static::$Browser->Lang = strtoupper(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
  222.         }
  223.  
  224.         private static function getBrowserEncoding() {
  225.             static::$Browser->Encoding->Accept = 0;
  226.             static::$Browser->Encoding->Options = null;
  227.             if (isset($_SERVER['HTTP_ACCEPT_ENCODING'])) {
  228.                 $mtd = explode(',', str_replace(' ', '', $_SERVER['HTTP_ACCEPT_ENCODING']));
  229.                 static::$Browser->Encoding->Accept = 1;
  230.                 static::$Browser->Encoding->Options = $mtd;
  231.             }
  232.         }
  233.  
  234.         private static function getCookiesEnabled() {
  235.             setcookie('cookie', 1, (time()+3600));
  236.             static::$Browser->Cookies->Enabled = 0;
  237.             if (isset($_COOKIE) && count($_COOKIE) > 0)
  238.                 static::$Browser->Cookies->Enabled = 1;
  239.         }
  240.  
  241.         private static function getClientSystem() {
  242.             $ua = static::$Browser->UserAgent;
  243.             if (static::$Compatibility == 1)
  244.                 $ua = str_replace('compatible', '', static::$Browser->UserAgent);
  245.  
  246.             preg_match('/(\s|)([a-zA-Z][a-zA-Z][a-z]+[a-zA-Z]+)(;|(\s|\)))/', $ua, $os);
  247.             static::$System->CPU = static::getClientCpuClass();
  248.             static::$System->IP = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null;
  249.             static::$System->ID = isset($os[1]) ? static::arrayClean($os)[1] : null;
  250.             static::$System->Port = isset($_SERVER['REMOTE_PORT']) ? $_SERVER['REMOTE_PORT'] : null;
  251.         }
  252.  
  253.         private static function getClientCpuClass() {
  254.             $x64 = '/(x64)|(x86_64)|(WOW64)|(Win64)/i';
  255.             if (preg_match($x64, static::$Browser->UserAgent))
  256.                 return 64;
  257.             return 32;
  258.         }
  259.  
  260.         private static function getHTMLClasses() {
  261.             $classStr = '';
  262.             $classStr .= static::$Browser->Name;
  263.             $classStr .= ' '. static::$Browser->Name.static::$Browser->MajorVersion;
  264.             $classStr .= strlen(static::$Browser->AltName) > 0 ? ' '. static::$Browser->AltName : '';
  265.             $classStr .= ' '. static::$Browser->Engine->Name;
  266.             $classStr .= ' '. static::$Browser->Engine->Name.static::$Browser->Engine->MajorVersion;
  267.             $classStr .= static::$System->Mobile == 1 ? ' Mobile' : '';
  268.             if(static::$System->ID) {
  269.                 $sysID = static::$System->ID;
  270.                 if (preg_match('/(mac|win)/i', $sysID))
  271.                     $sysID = substr($sysID, 0, 3);
  272.                 $classStr .= ' '. $sysID;
  273.                 $classStr .= ' '. $sysID.static::$System->CPU;
  274.             }
  275.             $classStr = implode(' ', array_unique(explode(' ', $classStr)));
  276.             $classStr = preg_replace('/[\s]+/', ' ', $classStr);
  277.             static::$Browser->HTMLClasses = preg_replace('/Unknown[a-z0-9]+\s/i', '', strtolower($classStr));
  278.         }
  279.  
  280.         private static function getMajorVersion($fullversion) {
  281.             preg_match('/([0-9a-zA-Z]+)/i', $fullversion, $versionArray);
  282.             return isset($versionArray[0]) ? $versionArray[0] : null;
  283.         }
  284.  
  285.         private static function firstCharIs($char, $string) {
  286.             return (substr($string, 0, 1) === $char) ? true : false;
  287.         }
  288.  
  289.         private static function arrayClean($array = null) {
  290.             if (!is_array($array))
  291.                 return null;
  292.             foreach ($array as $key => $value)
  293.                 $array[$key] = str_replace(' ', '', $value);
  294.             $array = array_filter($array);
  295.             $array = array_values($array);
  296.             return $array;
  297.         }
  298.     }
  299.     Client::init();
  300. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement