View file system/inc/classes/browser.class.php

File size: 5.78Kb
<?
class ua_browser{
	/**
	* Обнаружение браузера имен и версий Chrome, Firefox, Internet Explorer, Opera и Safari.
	*
	* Возвращает Array ('имя' => название браузера (как написано здесь ^),
	* 'Версии' => Array (основной версии, дополнительный подрывной деятельности, выпуска, сборки)).
    */
	static function detectBrowser($userAgent = null)
	{
		is_null($userAgent) && ($userAgent = $_SERVER['HTTP_USER_AGENT']);
		$name = null;
		$version = array(null, null, null, null);
		if (false !== strpos($userAgent, 'Opera/'))
		{
			//http://www.useragentstring.com/pages/Opera/
			$name = 'Opera';
			if (false !== strpos($userAgent, 'Version/')) // http://dev.opera.com/articles/view/opera-ua-string-changes/
			{
				preg_match('#Version/(\d{1,2})\.(\d{1,2})#i', $userAgent, $versionMatch);
				isset($versionMatch[1]) && $version[0] = (int)$versionMatch[1];
				isset($versionMatch[2]) && $version[1] = (int)$versionMatch[2];
			}
			else
			{
				preg_match('#Opera/(\d{1,2})\.(\d{1,2})#i', $userAgent, $versionMatch);
				isset($versionMatch[1]) && $version[0] = (int)$versionMatch[1];
				isset($versionMatch[2]) && $version[1] = (int)$versionMatch[2];
			}
		}
		else if (false !== strpos($userAgent, 'Opera '))
		{
			//http://www.useragentstring.com/pages/Opera/
			$name = 'Opera';
			preg_match('#Opera (\d{1,2})\.(\d{1,2})#i', $userAgent, $versionMatch);
			isset($versionMatch[1]) && $version[0] = (int)$versionMatch[1];
			isset($versionMatch[2]) && $version[1] = (int)$versionMatch[2];
		}
		else if (false !== strpos($userAgent, 'Firefox/'))
		{
			// http://www.useragentstring.com/pages/Firefox/
			$name = 'Firefox';
			preg_match('#Firefox/(\d{1,2})\.(\d{1,2})(\.(\d{1,2})(\.(\d{1,2}))?)?#i', $userAgent, $versionMatch);
			isset($versionMatch[1]) && $version[0] = (int)$versionMatch[1];
			isset($versionMatch[2]) && $version[1] = (int)$versionMatch[2];
			isset($versionMatch[4]) && $version[2] = (int)$versionMatch[4];
			isset($versionMatch[6]) && $version[3] = (int)$versionMatch[6];
		}
		else if (false !== strpos($userAgent, 'MSIE '))
		{
			//http://www.useragentstring.com/pages/Internet%20Explorer/
			$name = 'Internet Explorer';
			preg_match('#MSIE (\d{1,2})\.(\d{1,2})#i', $userAgent, $versionMatch);
			isset($versionMatch[1]) && $version[0] = (int)$versionMatch[1];
			isset($versionMatch[2]) && $version[1] = (int)$versionMatch[2];
		}
		else if (false !== strpos($userAgent, 'Iceweasel/')) // Firefox in Debian
		{
			// http://www.useragentstring.com/pages/Iceweasel/
			$name = 'Firefox'; //Iceweasel is identical to Firefox! no need to differt them
			preg_match('#Iceweasel/(\d{1,2})\.(\d{1,2})(\.(\d{1,2})(\.(\d{1,2}))?)?#i', $userAgent, $versionMatch);
			isset($versionMatch[1]) && $version[0] = (int)$versionMatch[1];
			isset($versionMatch[2]) && $version[1] = (int)$versionMatch[2];
			isset($versionMatch[4]) && $version[2] = (int)$versionMatch[4];
			isset($versionMatch[6]) && $version[3] = (int)$versionMatch[6];
		}
		else if (false !== strpos($userAgent, 'Chrome/'))
		{
			// http://www.useragentstring.com/pages/Chrome/
			$name = 'Chrome';
			preg_match('#Chrome/(\d{1,2})\.(\d{1,3})\.(\d{1,3}).(\d{1,3})#i', $userAgent, $versionMatch);
			isset($versionMatch[1]) && $version[0] = (int)$versionMatch[1];
			isset($versionMatch[2]) && $version[1] = (int)$versionMatch[2];
			isset($versionMatch[3]) && $version[2] = (int)$versionMatch[3];
			isset($versionMatch[4]) && $version[3] = (int)$versionMatch[4];
		}
		else if (false !== strpos($userAgent, 'Safari/'))
		{
			// http://www.useragentstring.com/pages/Safari/
			$name = 'Safari';
			/* Uncomment this block of code if u want to use Version/ tag
			 * instead of Safari/Build tag. Old Safari browsers haven’t Version/ tag
			 * and their version was marked as build number (ex. 528.16).
			if (false !== strpos($userAgent, 'Version/')) // old versions of Safari doesn't have Version tag in UserAgent
			{
				preg_match('#Version/(\d{1,2})\.(\d{1,2})(\.(\d{1,2}))?#i', $userAgent, $versionMatch);
				isset($versionMatch[1]) && $version[0] = (int)$versionMatch[1];
				isset($versionMatch[2]) && $version[1] = (int)$versionMatch[2];
				isset($versionMatch[4]) && $version[2] = (int)$versionMatch[4];
			}
			else
			{*/
				preg_match('#Safari/(\d{1,3})\.(\d{1,2})(\.(\d{1,2}))?#i', $userAgent, $versionMatch);
				isset($versionMatch[1]) && $version[0] = (int)$versionMatch[1];
				isset($versionMatch[2]) && $version[1] = (int)$versionMatch[2];
				isset($versionMatch[4]) && $version[2] = (int)$versionMatch[4];
			//}
		}
		
		return array('name' => $name, 'version' => $version);
	}
	
	/**
	* Сравните версии браузера.
	*
	* Возвращает ЦЕЛОЕ (0), если версия Aqual до $ условия,
	* ЦЕЛОЕ (-1), если версия ниже $ условия,
	* ЦЕЛОЕ (1), если версия новее, чем $ условиями.
	*
	* Возвращает NULL в случае любой ошибки
	*/
	static public function checkForBrowserVersion(array $browser, array $conditions)
	{
		if (!isset($browser['name']) || !isset($conditions[$browser['name']])
			|| !isset($browser['version']) || count($browser['version']) < 1)
		{
			return null;
		}
		
		$cnd = $conditions[$browser['name']]; // 0=>, 1=>, 2=>
		if (!is_array($cnd))
		{
			return null;
		}
		
		for ($i = 0; $i < count($cnd); $i++)
		{
			if ($browser['version'][$i] < $cnd[$i])
			{
				return -1;
			}
			else if ($browser['version'][$i] > $cnd[$i])
			{
				return 1;
			}
		}
		
		return 0;
	}
}	
	
	$browsers = ua_browser::detectBrowser($_SERVER['HTTP_USER_AGENT']);
	

?>