/** * Test getOperatingSystem() * * @dataProvider getUserAgents * @group Plugins */ public function testGetOperatingSystem($userAgent, $expected) { $res = UserAgentParser::getOperatingSystem($userAgent); $this->assertEquals($expected[1][0], $res['id']); $this->assertEquals($expected[1][1], $res['name']); $this->assertEquals($expected[1][2], $res['short_name']); }
public function test_getOperatingSystem() { foreach ($this->userAgents as $userAgent => $expected) { $res = UserAgentParser::getOperatingSystem($userAgent); $ok = $expected[1][0] == $res['id'] && $expected[1][1] == $res['name'] && $expected[1][2] == $res['short_name']; $this->assertTrue($ok); if (!$ok) { var_dump(array($userAgent, $res)); } } }
public function addTopMenu() { Piwik_AddTopMenu('General_API', array('module' => 'API', 'action' => 'listAllAPI'), true, 7); if (empty($_SERVER['HTTP_USER_AGENT'])) { return; } require_once PIWIK_INCLUDE_PATH . '/libs/UserAgentParser/UserAgentParser.php'; $os = UserAgentParser::getOperatingSystem($_SERVER['HTTP_USER_AGENT']); if ($os && in_array($os['id'], array('AND', 'IPD', 'IPA', 'IPH'))) { Piwik_AddTopMenu('Piwik Mobile App', array('module' => 'Proxy', 'action' => 'redirect', 'url' => 'http://piwik.org/mobile/'), true, 4); } }
function Piwik_getBrowserShortLabel($oldLabel) { $browserId = Piwik_getBrowserId($oldLabel); $version = Piwik_getBrowserVersion($oldLabel); $browserName = UserAgentParser::getBrowserShortNameFromId($browserId); if ($browserName !== false) { return $browserName . " " . $version; } if ($browserId == 'UNK') { return Piwik_Translate('General_Unknown'); } return $oldLabel; }
protected static function init() { if (self::$init) { return; } self::$init = true; // init browser names and short names self::$browserIdToName = array_map('ucwords', array_flip(self::$browsers)); self::$browserIdToName['CH'] = "Google Chrome"; self::$browserIdToShortName = self::$browserIdToName; self::$browserIdToShortName['IE'] = "IE"; self::$browserIdToShortName['FB'] = "Firebird"; // init OS names and short names self::$operatingSystemsIdToName = array_flip(self::$operatingSystems); self::$operatingSystemsIdToShortName = array_merge(self::$operatingSystemsIdToName, array('PS3' => 'PS3', 'PSP' => 'PSP', 'IPH' => 'iPhone', 'WII' => 'WII', 'WI7' => 'Win 7', 'WVI' => 'Win Vista', 'WS3' => 'Win S2003', 'WXP' => 'Win XP', 'W98' => 'Win 98', 'W2K' => 'Win 2000', 'WNT' => 'Win NT', 'WME' => 'Win Me', 'W95' => 'Win 95', 'WCE' => 'Win CE', 'MAC' => 'Mac OS', 'LIN' => 'Linux', 'INC' => 'Inconnu', 'SOS' => 'SunOS', 'BSD' => 'FreeBSD', 'AIX' => 'AIX', 'IRI' => 'IRIX', 'HPX' => 'HPX', 'OS2' => 'OS/2', 'NBS' => 'NetBSD', 'UNK' => 'Unknown')); }
protected static function init() { if (self::$init) { return; } self::$init = true; // init browser names and short names self::$browserIdToName = array_map('ucwords', array_flip(self::$browsers)); self::$browserIdToName['AB'] = 'ABrowse'; self::$browserIdToName['AV'] = 'AmigaVoyager'; self::$browserIdToName['AW'] = 'Amiga AWeb'; self::$browserIdToName['BB'] = 'BlackBerry'; self::$browserIdToName['BX'] = 'BrowseX'; self::$browserIdToName['CO'] = 'CometBird'; self::$browserIdToName['EL'] = 'ELinks'; self::$browserIdToName['FF'] = 'Firefox'; self::$browserIdToName['HJ'] = 'HotJava'; self::$browserIdToName['IB'] = 'IBrowse'; self::$browserIdToName['IC'] = 'iCab'; self::$browserIdToName['KM'] = 'K-Meleon'; self::$browserIdToName['MC'] = 'NCSA Mosaic'; self::$browserIdToName['OW'] = 'OmniWeb'; self::$browserIdToName['SF'] = 'Safari'; self::$browserIdToName['SM'] = 'SeaMonkey'; self::$browserIdToName['WO'] = 'Palm webOS'; self::$browserIdToName['WP'] = 'WebPro'; self::$browserIdToShortName = self::$browserIdToName; self::$browserIdToShortName['AW'] = 'AWeb'; self::$browserIdToShortName['FB'] = 'Firebird'; self::$browserIdToShortName['IE'] = 'IE'; self::$browserIdToShortName['MC'] = 'Mosaic'; self::$browserIdToShortName['WO'] = 'webOS'; // init OS names and short names self::$operatingSystemsIdToName = array_merge(array_flip(self::$operatingSystems), array('IPD' => 'iPhone', 'IPA' => 'iPhone', 'WME' => 'Windows Me', 'BEO' => 'BeOS', 'T64' => 'Tru64')); self::$operatingSystemsIdToShortName = array_merge(self::$operatingSystemsIdToName, array('PS3' => 'PS3', 'PSP' => 'PSP', 'WII' => 'Wii', 'WI7' => 'Win 7', 'WVI' => 'Win Vista', 'WS3' => 'Win S2003', 'WXP' => 'Win XP', 'W98' => 'Win 98', 'W2K' => 'Win 2000', 'WNT' => 'Win NT', 'WME' => 'Win Me', 'W95' => 'Win 95', 'WCE' => 'Win CE', 'WOS' => 'webOS', 'UNK' => 'Unknown')); }
<?php require_once dirname(__FILE__) . '/UserAgentParser.php'; echo "<h2>UserAgentParser php library test</h2>"; $testUserAgent = array('my user agent' => '', 'ie8 on win7' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 3.0.04506; .NET CLR 3.5.21022; InfoPath.2; SLCC1; Zune 3.0)', 'chrome on winxp' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.48 Safari/525.19', 'IE6 on winxp' => 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.04506.648)', 'safari on winxp' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Version/3.1.2 Safari/525.21', 'FF3 on winxp' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6', 'opera 9.63 on winxp' => 'Opera/9.63 (Windows NT 5.1; U; en) Presto/2.1.1', 'Blackberry' => 'BlackBerry8700/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1', 'opera 9.30 on Nintendo Wii' => 'Opera/9.30 (Nintendo Wii; U; ; 2047-7; en)', 'iphone' => 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 2_2 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5G77 Safari/525.20', 'iPod touch' => 'Mozilla/5.0 (iPod; U; CPU like Mac OS X; en) AppleWebKit/420.1 (KHTML, like Gecko) Version/3.0 Mobile/3A100a Safari/419.3', 'iPod' => 'Mozilla/5.0 (iPod; U; CPU iPhone OS 2_2_1 like Mac OS X; en-us) AppleWebKit/525.18.1 (KHTML, like Gecko) Version/3.1.1 Mobile/5H11a Safari/525.20', 'Android' => 'Mozilla/5.0 (Linux; U; Android 1.1; en-us; dream) AppleWebKit/525.10+ (KHTML, like Gecko) Version/3.0.4 Mobile Safari/523.12.2', 'PalmOS' => 'Mozilla/5.0 [en] (PalmOS; U; WebPro/3.5; Palm-Zi72) ', 'safari on mac os X' => 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_5; en-us) AppleWebKit/527.3+ (KHTML, like Gecko) Version/3.1.2 Safari/525.20.1', 'opera 9.64 on win ME' => 'Opera/9.64 (Windows ME; U; en) Presto/2.1.1', 'opera 10.00 on XP' => 'Opera/9.80 (Windows NT 5.1; U; en) Presto/2.2.15 Version/10.00', 'iron on win7' => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/531.0 (KHTML, like Gecko) Iron/3.0.189.0 Safari/531.0'); echo "Test with another user agent: "; foreach ($testUserAgent as $name => $userAgent) { echo "<a href='?setUserAgent=" . urlencode($userAgent) . "'>{$name}</a>, "; } echo "<hr>"; if (isset($_GET['setUserAgent']) && !empty($_GET['setUserAgent'])) { echo "User Agent:"; $userAgent = urldecode($_GET['setUserAgent']); } else { echo "Your user agent:"; $userAgent = $_SERVER['HTTP_USER_AGENT']; } echo " <b>" . htmlentities($userAgent) . "</b><br><br>"; echo "Browser info:<pre>"; var_dump(UserAgentParser::getBrowser($userAgent)); echo "</pre>"; echo "Operating System info:<pre>"; var_dump(UserAgentParser::getOperatingSystem($userAgent)); echo "</pre>"; echo "<br><br><i>UserAgentParser doesn't detect your Operating System or Browser properly? <br>Please submit your user agent string and the expected result to hello at piwik.org. Patches are also welcome :-) Thanks!</i>";
/** * Gets the UserSettings information and returns them in an array of name => value * * @return array */ protected function getUserSettingsInformation() { // we already called this method before, simply returns the result if (is_array($this->userSettingsInformation)) { return $this->userSettingsInformation; } require_once PIWIK_INCLUDE_PATH . '/libs/UserAgentParser/UserAgentParser.php'; $plugin_Flash = Piwik_Common::getRequestVar('fla', 0, 'int', $this->request); $plugin_Java = Piwik_Common::getRequestVar('java', 0, 'int', $this->request); $plugin_Director = Piwik_Common::getRequestVar('dir', 0, 'int', $this->request); $plugin_Quicktime = Piwik_Common::getRequestVar('qt', 0, 'int', $this->request); $plugin_RealPlayer = Piwik_Common::getRequestVar('realp', 0, 'int', $this->request); $plugin_PDF = Piwik_Common::getRequestVar('pdf', 0, 'int', $this->request); $plugin_WindowsMedia = Piwik_Common::getRequestVar('wma', 0, 'int', $this->request); $plugin_Gears = Piwik_Common::getRequestVar('gears', 0, 'int', $this->request); $plugin_Silverlight = Piwik_Common::getRequestVar('ag', 0, 'int', $this->request); $plugin_Cookie = Piwik_Common::getRequestVar('cookie', 0, 'int', $this->request); $userAgent = Piwik_Common::sanitizeInputValues(@$_SERVER['HTTP_USER_AGENT']); $aBrowserInfo = UserAgentParser::getBrowser($userAgent); $browserName = $aBrowserInfo !== false && $aBrowserInfo['id'] !== false ? $aBrowserInfo['id'] : 'UNK'; $browserVersion = $aBrowserInfo !== false && $aBrowserInfo['version'] !== false ? $aBrowserInfo['version'] : ''; $os = UserAgentParser::getOperatingSystem($userAgent); $os = $os === false ? 'UNK' : $os['id']; $resolution = Piwik_Common::getRequestVar('res', 'unknown', 'string', $this->request); $ip = Piwik_Common::getIp(); $browserLang = Piwik_Common::getBrowserLanguage(); $configurationHash = $this->getConfigHash($os, $browserName, $browserVersion, $resolution, $plugin_Flash, $plugin_Java, $plugin_Director, $plugin_Quicktime, $plugin_RealPlayer, $plugin_PDF, $plugin_WindowsMedia, $plugin_Gears, $plugin_Silverlight, $plugin_Cookie, $ip, $browserLang); $this->userSettingsInformation = array('config_md5config' => $configurationHash, 'config_os' => $os, 'config_browser_name' => $browserName, 'config_browser_version' => $browserVersion, 'config_resolution' => $resolution, 'config_pdf' => $plugin_PDF, 'config_flash' => $plugin_Flash, 'config_java' => $plugin_Java, 'config_director' => $plugin_Director, 'config_quicktime' => $plugin_Quicktime, 'config_realplayer' => $plugin_RealPlayer, 'config_windowsmedia' => $plugin_WindowsMedia, 'config_gears' => $plugin_Gears, 'config_silverlight' => $plugin_Silverlight, 'config_cookie' => $plugin_Cookie, 'location_ip' => $ip, 'location_browser_lang' => $browserLang); return $this->userSettingsInformation; }
/** * Gets the UserSettings information and returns them in an array of name => value * * @return array */ protected function getUserSettingsInformation() { // we already called this method before, simply returns the result if (is_array($this->userSettingsInformation)) { return $this->userSettingsInformation; } require_once "UserAgentParser/UserAgentParser.php"; $plugin_Flash = Piwik_Common::getRequestVar('fla', 0, 'int', $this->request); $plugin_Director = Piwik_Common::getRequestVar('dir', 0, 'int', $this->request); $plugin_RealPlayer = Piwik_Common::getRequestVar('realp', 0, 'int', $this->request); $plugin_Pdf = Piwik_Common::getRequestVar('pdf', 0, 'int', $this->request); $plugin_WindowsMedia = Piwik_Common::getRequestVar('wma', 0, 'int', $this->request); $plugin_Cookie = Piwik_Common::getRequestVar('cookie', 0, 'int', $this->request); $userAgent = Piwik_Common::sanitizeInputValues(@$_SERVER['HTTP_USER_AGENT']); $aBrowserInfo = UserAgentParser::getBrowser($userAgent); # the !==false does not match the NULL and we have NOT NULL in the colums, so change to !empty. $browserName = !empty($aBrowserInfo) && !empty($aBrowserInfo['id']) ? $aBrowserInfo['id'] : 'UNK'; $browserVersion = !empty($aBrowserInfo) && !empty($aBrowserInfo['version']) ? $aBrowserInfo['version'] : ''; $os = UserAgentParser::getOperatingSystem($userAgent); $os = $os === false ? 'UNK' : $os['id']; $resolution = Piwik_Common::getRequestVar('res', 'unknown', 'string', $this->request); $ip = Piwik_Common::getIp(); $browserLang = Piwik_Common::getBrowserLanguage(); $configurationHash = $this->getConfigHash($os, $browserName, $browserVersion, $resolution, $plugin_Flash, $plugin_Director, $plugin_RealPlayer, $plugin_Pdf, $plugin_WindowsMedia, $plugin_Cookie, $ip, $browserLang); $this->userSettingsInformation = array('config_md5config' => $configurationHash, 'config_os' => $os, 'config_browser_name' => $browserName, 'config_browser_version' => $browserVersion, 'config_resolution' => $resolution, 'config_pdf' => $plugin_Pdf, 'config_flash' => $plugin_Flash, 'config_director' => $plugin_Director, 'config_realplayer' => $plugin_RealPlayer, 'config_windowsmedia' => $plugin_WindowsMedia, 'config_cookie' => $plugin_Cookie, 'location_ip' => $ip, 'location_browser_lang' => $browserLang); return $this->userSettingsInformation; }
function Piwik_getBrowserFamily($browserLabel) { $familyNameToUse = UserAgentParser::getBrowserFamilyFromId(substr($browserLabel, 0, 2)); return $familyNameToUse; }
/** * Constructor * * Configure the User Agent from a user agent string. * @param String $userAgentString => the user agent string. * @param UserAgentParser $userAgentParser => the parser used to parse the string. */ public function __construct($userAgentString = null, UserAgentParser $userAgentParser = null) { if ($userAgentParser == null) { $userAgentParser = new UserAgentParser(); } $this->setUserAgentString($userAgentString); $this->fromArray($userAgentParser->parse($userAgentString)); }
protected function addTopMenuMobileApp() { if (empty($_SERVER['HTTP_USER_AGENT'])) { return; } require_once PIWIK_INCLUDE_PATH . '/libs/UserAgentParser/UserAgentParser.php'; $os = \UserAgentParser::getOperatingSystem($_SERVER['HTTP_USER_AGENT']); if ($os && in_array($os['id'], array('AND', 'IPD', 'IPA', 'IPH'))) { MenuTop::addEntry('Piwik Mobile App', array('module' => 'Proxy', 'action' => 'redirect', 'url' => 'http://piwik.org/mobile/'), true, 4); } }