* Provides functionality for detecting the users http client. It has
* been tested against most browsers that are used today.
*
* Intended usage in a PHP is:
*
*
*
* <?php
* $browser = new BrowserDetection();
* ?>
* You are: <?php echo $browser->getBrowser() ?>
* <?php echo $browser->getVersion() ?>
* <?php echo $browser->getPlatform() ?>
*
*
* New in version 1.1:
*
- Test for accepted Mimetypes, Charsets and Languages.
* - Detection if the user has AOL
* - Detection of the most common search engines and bots
* - Better naming scheme
*
* New in version 1.1.1:
*
- Browser detection now works with PHP5
*
* @author Johannes Lietz
* @copyright 2003-2004, Johannes Lietz
* @version 1.1.1 (26. Aug. 2004)
* @package standard
*/
class BrowserDetection
{
/**
* UserAgent from HTTP request.
*
* @access private
* @var string
*/
var $strUserAgent;
/**
* Browser-Type.
*
* @access private
* @var string
*/
var $strBrowser;
/**
* Platform.
*
* @access private
* @var string
*/
var $strPlatform;
/**
* Accept from HTTP request.
*
* @access private
* @var string
*/
var $strAccept;
/**
* Accept-Language from HTTP request.
*
* @access private
* @var string
*/
var $strAcceptLanguage;
/**
* Accept-Charset from HTTP request.
*
* @access private
* @var string
*/
var $strAcceptCharset;
/**
* Version number.
*
* @access private
* @var float
*/
var $floatVersion = 0;
/**
* Indicates that a browser is a bot.
*
* @access private
* @var boolean
*/
var $boolBot = false;
/**
* Indicates that a browser is an AOL browser.
*
* @access private
* @var boolean
*/
var $boolAol = false;
/**
* Constructor. Creates new BrowserDetection from Request.
*
* @access public
*/
function __construct()
{
if (isset($_SESSION[DEFAULT_SESSION_ATTRIBUTENAME])) {
$objTmp = unserialize($_SESSION[DEFAULT_SESSION_ATTRIBUTENAME]);
$this->strUserAgent = $objTmp->strUserAgent;
$this->strBrowser = $objTmp->strBrowser;
$this->strPlatform = $objTmp->strPlatform;
$this->strAccept = $objTmp->strAccept;
$this->strAcceptLanguage = $objTmp->strAcceptLanguage;
$this->strAcceptCharset = $objTmp->strAcceptCharset;
$this->floatVersion = $objTmp->floatVersion;
$this->boolBot = $objTmp->boolBot;
$this->boolAol = $objTmp->boolAol;
}
else {
$this->strUserAgent = isset($_SERVER['HTTP_USER_AGENT']) ? strToLower($_SERVER['HTTP_USER_AGENT']) : '';
$this->strAccept = isset($_SERVER['HTTP_ACCEPT']) ? strToLower($_SERVER['HTTP_ACCEPT']) : '';
$this->strAcceptLanguage = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? strToLower($_SERVER['HTTP_ACCEPT_LANGUAGE']) : '';
$this->strAcceptCharset = isset($_SERVER['HTTP_ACCEPT_CHARSET']) ? strToLower($_SERVER['HTTP_ACCEPT_CHARSET']): '';
$this->_detect();
$_SESSION[DEFAULT_SESSION_ATTRIBUTENAME] = serialize($this);
}
}
/**
* Parses the UserAgent and sets the browsers type, version and platform
*
* @access private
*/
function _detect()
{
if ($this->strUserAgent == '')
{
return;
}
///////////////////////////////////////////////////
// Search-Engine
///////////////////////////////////////////////////
if (strpos($this->strUserAgent, 'bot') !== false
|| strpos($this->strUserAgent, 'seek') !== false
|| strpos($this->strUserAgent, 'slurp') !== false
|| strpos($this->strUserAgent, 'spider') !== false
|| strpos($this->strUserAgent, 'crawl') !== false
|| strpos($this->strUserAgent, 'search') !== false
|| strpos($this->strUserAgent, 'teoma') !== false
|| strpos($this->strUserAgent, 'sleuth') !== false
|| strpos($this->strUserAgent, 'search') !== false
|| strpos($this->strUserAgent, 'find') !== false
|| strpos($this->strUserAgent, 'fireball') !== false
|| strpos($this->strUserAgent, 'scooter') !== false
|| strpos($this->strUserAgent, 'altavista') !== false
|| strpos($this->strUserAgent, 'netcraft') !== false
|| strpos($this->strUserAgent, 'gulliver') !== false
|| strpos($this->strUserAgent, 'ferret') !== false
|| strpos($this->strUserAgent, 'informant') !== false)
{
$this->boolBot = true;
return;
}
$this->boolAol = strpos($this->strUserAgent, 'aol') !== false;
///////////////////////////////////////////////////
// Browser und Version feststellen
///////////////////////////////////////////////////
if (strpos($this->strUserAgent, OPERA) !== false)
{
$this->strBrowser = OPERA;
$this->floatVersion = $this->_parseVersion(OPERA);
}
else if (strpos($this->strUserAgent, MSIE) !== false)
{
$this->strBrowser = MSIE;
$this->floatVersion = $this->_parseVersion(MSIE);
}
else if (strpos($this->strUserAgent, SAFARI) !== false)
{
$this->strBrowser = SAFARI;
$this->floatVersion = $this->_parseVersion(SAFARI);
}
else if (strpos($this->strUserAgent, MSPIE) !== false)
{
$this->strBrowser = MSPIE;
$this->floatVersion = $this->_parseVersion(MSPIE);
}
else if (strpos($this->strUserAgent, KONQUEROR) !== false)
{
$this->strBrowser = KONQUEROR;
$this->floatVersion = $this->_parseVersion(KONQUEROR);
}
else if (strpos($this->strUserAgent, MOZILLA) !== false)
{
// "Revision builds"/Netscape ab 6.0(???)
$this->strBrowser = MOZILLA;
$this->floatVersion = $this->_parseVersion("rv:");
// "Milestone Builds"/Netscape 6.0, z.B. "m18"
if ($this->floatVersion == 5)
$this->floatVersion = 0.5;
}
else if (strpos($this->strUserAgent, NETSCAPE) !== false)
{
$this->strBrowser = NETSCAPE;
$this->floatVersion = $this->_parseVersion(NETSCAPE);
}
else if (strpos($this->strUserAgent, LYNX) !== false)
{
$this->strBrowser = LYNX;
$this->floatVersion = $this->_parseVersion(LYNX);
}
else
{
$this->strBrowser = UNKNOWN;
$this->floatVersion = 0;
}
///////////////////////////////////////////////////
// Platform feststellen
///////////////////////////////////////////////////
if (strpos($this->strUserAgent, WIN_CE) !== false)
{
$this->strPlatform = WIN_CE;
}
else if (strpos($this->strUserAgent, WIN) !== false)
{
$this->strPlatform = WIN;
}
else if (
strpos($this->strUserAgent, MAC) !== false || strpos($this->strUserAgent, 'PPC') !== false)
{
$this->strPlatform = MAC;
}
else if (
strpos($this->strUserAgent, UNIX) !== false
|| strpos($this->strUserAgent, "linux") !== false
|| strpos($this->strUserAgent, "x11") !== false
|| strpos($this->strUserAgent, "libwww") !== false
|| strpos($this->strUserAgent, "sunos") !== false)
{
$this->strPlatform = UNIX;
}
else
{
$this->strPlatform = UNKNOWN;
}
}
/**
* Parses the version number from a given String
*
* @access private
* @param string $strSearchPattern
*/
function _parseVersion($strSearchPattern)
{
$strString = substr($this->strUserAgent,
strpos($this->strUserAgent, $strSearchPattern) + strlen($strSearchPattern));
preg_match('/[0-9\.]{2,}/', $strString, $arrGrep);
if (count($arrGrep) > 0)
{
$strVersionString = trim($arrGrep[0]);
if (strpos($strVersionString, '.') !== false) {
$strMajor = substr($strVersionString, 0, strpos($strVersionString, '.'));
$strMinor = str_replace('.', '', substr($strVersionString, strpos($strVersionString, '.') + 1));
return floatval($strMajor.'.'.$strMinor);
}
else {
return floatval($strVersionString);
}
}
return 0;
}
/**
* Tests if a browser explicitely accepts a given mime type.
* "*.*" is ignored.
*
* @param string $strMimeType a RFC mimetype such as "text/html", "application/xhtml+xml" or "image/gif"
* @return boolean true if the browser explicitely accepts a given mime type.
* @since 1.1
* @access public
*/
function acceptsMimeType($strMimeType) {
if ($strMimeType == '') {
return false;
}
$strMimeType = strToLower($strMimeType);
return $this->strAccept != null && strpos($this->strAccept, $strMimeType) !== false;
}
/**
* Tests if a browser explicitely accepts a given mime type and preferes to receive it over the second one.
* "*.*" is ignored.
* Note that I assume that we do not pay attention to quantity statements such as "application/xhtml+xml;q=0.99" yet
* but only the given order. Safari e.g. does not give a special order to its mimetypes, only quantity statement, so it
* does not work with Safari!
*
* @param string $strMimeType1 a RFC mimetype such as "text/html", "application/xhtml+xml" or "image/gif"
* @param string $strMimeType2 a RFC mimetype such as "text/html", "application/xhtml+xml" or "image/gif"
* @return boolean true if the browser explicitely accepts a given mime type.
* @since 1.1
*/
function prefersMimeType($strMimeType1, $strMimeType2)
{
if ($this->strAccept == null || (!$this->strAcceptsMimeType($strMimeType1) && !$this->strAcceptsMimeType($strMimeType2))) {
return 0;
}
else if ($this->strAcceptsMimeType($strMimeType1) && !$this->strAcceptsMimeType($strMimeType2) || strpos($this->strAccept, $strMimeType1) < strpos($this->strAccept, $strMimeType1)) {
return 1;
}
else {
return 2;
}
}
/**
* Tests if a browser explicitely accepts a given language.
*
* @param string $strLanguage a ISO language such as "en", "en-us" or "de"
* @return boolean true if the browser explicitely accepts a given language.
* @since 1.1
*/
function acceptsLanguage($strLanguage) {
if ($strLanguage == '')
return false;
$strLanguage = strToLower($strLanguage);
return $this->strAcceptLanguage != '' && strpos($this->strAcceptLanguage, $strLanguage) !== false;
}
/**
* Tests if a browser explicitely accepts a given character set.
*
* @param string $strCharset a ISO language such as "utf-8", "ISO-8859-1" or "windows-1252"
* @return boolean true if the browser explicitely accepts a given language.
* @since 1.1
*/
function acceptsCharset($strCharset) {
if ($strCharset == '')
return false;
$strCharset = strToLower($strCharset);
return $this->strAcceptCharset != '' && strpos($this->strAcceptCharset, $strCharset) !== false;
}
/**
* @return String the browser type
*/
function getBrowser()
{
return $this->strBrowser;
}
/**
* @return String the platform
*/
function getPlatform()
{
return $this->strPlatform;
}
/**
* @return String the Useragent from HTTP request
*/
function getUserAgent()
{
return $this->strUserAgent;
}
/**
* @return float the version number, e.g. 5.01 or 6.23
*/
function getVersion()
{
return $this->floatVersion;
}
/** Tests if the user agent is likely to be a bot.
* Note that some bots identify themselves as an old Netscape version.
* Therefore we also return true for Netscape < 4.
*
* @return true if the user agent identifies itself as search engine or bot
* @since 1.1
*/
function isBot()
{
return $this->boolBot || ($this->isAncientNetscape() && $this->getVersion() < 4);
}
/**
* @return boolean isKonqueror
*/
function isKonqueror()
{
return $this->strBrowser == KONQUEROR;
}
/**
* @return boolean isLynx
*/
function isLynx()
{
return $this->strBrowser == LYNX;
}
/**
* true for Nescape >= 6 and all Mozilla versions
* @return boolean isMozilla
* @since 1.1
*/
function isModernMozilla()
{
return $this->strBrowser == MOZILLA;
}
/**
* true for Nescape >= 6 and all Mozilla versions
* @return boolean isMozilla
* @deprecated use isModernMozilla() instead.
*/
function isMozilla()
{
return $this->strBrowser == MOZILLA;
}
/**
* @return boolean isMsie
*/
function isMsie()
{
return $this->strBrowser == MSIE;
}
/**
* true for the MSIE mobile ("Pocket") version.
* @return boolean isMspie
*/
function isMspie()
{
return $this->strBrowser == MSPIE;
}
/**
* true for Netscape browsers prior Netscape 6
* @return boolean isNetscape4
* @deprecated use isAncientNetscape() instead
*/
function isNetscape4()
{
return isAncientNetscape();
}
/**
* true for Netscape browsers prior Netscape 6
* @return boolean isNetscape4
* @since 1.1
*/
function isAncientNetscape()
{
return $this->strBrowser == NETSCAPE;
}
/**
* true for Opera browsers. Note that Opera browsers like to
* camouflage as MSIE.
* @return boolean isOpera
*/
function isOpera()
{
return $this->strBrowser == OPERA;
}
/**
* @return boolean isSafari
*/
function isSafari()
{
return $this->strBrowser == SAFARI;
}
/**
* @return boolean isMac
*/
function isMac()
{
return $this->strPlatform == MAC;
}
/**
* @return boolean isUnix
*/
function isUnix()
{
return $this->strPlatform == UNIX;
}
/**
* @return boolean isWin
*/
function isWin()
{
return $this->strPlatform == WIN;
}
/**
* @return boolean isWindowsCE
*/
function isWindowsCE()
{
return $this->strPlatform == WIN_CE;
}
/**
* @return isAol.
* @since 1.1
*/
function isAol()
{
return $this->boolAol;
}
}
?>