/** * @dataProvider getVersionTruncationFixtures */ public function versionTruncationTest($useragent, $truncationType, $osVersion, $clientVersion) { DeviceParserAbstract::setVersionTruncation($truncationType); $dd = new DeviceDetector($useragent); $this->assertEquals($osVersion, $dd->getOs('version')); $this->assertEquals($clientVersion, $dd->getClient('version')); }
public function detect($userAgent = null, $advanced = false) { $userAgent = $userAgent ?: env('HTTP_USER_AGENT'); DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE); $detect = new DeviceDetector($userAgent); $detect->parse(); $return = []; if ($detect->isBot()) { $return['bot'] = $detect->getBot(); return $return; } //device wrapper $devicelist = ['desktop' => 'computer', 'smartphone' => 'phone', 'tablet' => 'tablet', 'feature phone' => 'phone']; $os = $detect->getOs(); $client = $detect->getClient(); $devicename = $detect->getDeviceName(); $devicetype = isset($devicelist[$devicename]) ? $devicelist[$devicename] : 'computer'; //legacy params $return['device'] = $devicename; $return['type'] = $devicetype; $return['brand'] = $detect->getBrandName(); $return['os'] = $os['name']; $return['os_version'] = $os['version']; $return['os_code'] = $os['short_name']; $return['browser'] = $client['name']; $return['browser_version'] = $client['version']; $return['browser_code'] = $client['short_name']; $return['browser_type'] = $client['type']; $return['browser_engine'] = $client['engine']; if (!$advanced) { return array_map('trim', $return); } //advanced params $osFamily = OperatingSystem::getOsFamily($os['short_name']); $return['os_family'] = $osFamily !== false ? $osFamily : 'Unknown'; $return['model'] = $detect->getModel(); $browserFamily = Browser::getBrowserFamily($client['short_name']); $return['browser_family'] = $browserFamily !== false ? $browserFamily : 'Unknown'; $touch = $detect->isTouchEnabled(); $return['touch'] = $touch[0]; unset($os, $client, $osFamily, $browserFamily, $touch); return array_map('trim', $return); }
/** * detect request from BOT via user agent */ public function isBot($userAgent = '') { $userAgent = $userAgent ? $userAgent : $_SERVER['HTTP_USER_AGENT']; DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE); $dd = new DeviceDetector($userAgent); // OPTIONAL: If called, getBot() will only return true if a bot was detected (speeds up detection a bit) $dd->discardBotInformation(); $dd->parse(); return $dd->isBot(); }
/** * @dataProvider getFixtures */ public function testParse($fixtureData) { $ua = $fixtureData['user_agent']; DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE); $uaInfo = DeviceDetector::getInfoFromUserAgent($ua); $this->assertEquals($fixtureData, $uaInfo); }
<?php /** * Device Detector - The Universal Device Detection library for parsing User Agents * * @link http://piwik.org * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later */ require __DIR__ . '/vendor/autoload.php'; use DeviceDetector\DeviceDetector; use DeviceDetector\Parser\Device\DeviceParserAbstract; if (isset($_GET['ua'])) { $userAgent = $_GET['ua']; } else { $userAgent = $_SERVER['HTTP_USER_AGENT']; } DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE); $result = DeviceDetector::getInfoFromUserAgent($userAgent); echo '<form><input type="text" name="ua" /><input type="submit" /></form>'; echo "<pre>"; echo Spyc::YAMLDump($result, 2, 0); echo "</pre>";
/** * Information about client requesting * cache base on user agent + client ip */ public function getClientInfo($userAgent = '') { if ($this->clientInfo) { return $this->clientInfo; } $userAgent = $userAgent ? $userAgent : @$_SERVER['HTTP_USER_AGENT']; $UAHash = $this->makeUserAgentHash($userAgent); $cacheKey = "ClientInfo:{$UAHash}"; $fromCache = RedisHelper::get($cacheKey); if (!$fromCache) { DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE); $dd = new DeviceDetector($userAgent); // OPTIONAL: If called, getBot() will only return true if a bot was detected (speeds up detection a bit) $dd->discardBotInformation(); $dd->parse(); if ($dd->isBot()) { // handle bots,spiders,crawlers,... // $clientInfo = $dd->getBot(); return false; } else { $clientInfo['client'] = $dd->getClient(); // holds information about browser, feed reader, media player, ... $clientInfo['os'] = $dd->getOs(); $clientInfo['device'] = $dd->getDevice(); $clientInfo['brand'] = $dd->getBrand(); $clientInfo['model'] = $dd->getModel(); $piwik = new Common(); $clientInfo['client']['browser_language'] = $piwik::getBrowserLanguage(); } RedisHelper::set($cacheKey, json_encode($clientInfo), Config::get('cache_time.defaultCacheTimeInSeconds')); } else { $clientInfo = json_decode($fromCache, 1); } $this->clientInfo = $clientInfo; return $clientInfo; }