/** * Test getBrowser() * * @dataProvider getUserAgents * @group Plugins */ public function testGetBrowser($userAgent, $expected) { $res = UserAgentParser::getBrowser($userAgent); $family = false; if ($res === false) { $this->assertFalse($expected[0]); } else { $family = \Piwik\Plugins\UserSettings\getBrowserFamily($res['id']); $this->assertEquals($expected[0][0], $res['id']); $this->assertEquals($expected[0][1], $res['name']); $this->assertEquals($expected[0][2], $res['short_name']); $this->assertEquals($expected[0][3], $res['version']); $this->assertEquals($expected[0][4], $res['major_number']); $this->assertEquals($expected[0][5], $res['minor_number']); $this->assertEquals($expected[0][6], $family); } }
public function test_getBrowser() { foreach ($this->userAgents as $userAgent => $expected) { $res = UserAgentParser::getBrowser($userAgent); $family = false; if ($res === false) { $ok = $res === $expected[0]; } else { $family = Piwik_getBrowserFamily($res['id']); $ok = $expected[0][0] == $res['id'] && $expected[0][1] == $res['name'] && $expected[0][2] == $res['short_name'] && $expected[0][3] == $res['version'] && $expected[0][4] == $res['major_number'] && $expected[0][5] == $res['minor_number'] && $expected[0][6] == $family; } $this->assertTrue($ok); if (!$ok) { var_dump(array($userAgent, $res, $family)); } } }
<?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; }