public function getBotInformation() { if (!$this->deviceDetector->isBot()) { throw new \Exception(Yii::t("app", "Isn't a Bot.")); } return $this->deviceDetector->getBot(); }
/** * Parse UA string and fill in other attributes */ public function parse() { $dd = new DeviceDetector($this->user_agent); // only return true if a bot was detected (speeds up detection a bit) $dd->discardBotInformation(); $dd->parse(); if ($dd->isBot()) { $this->bot = 1; } else { $client = $dd->getClient(); if ($this->counts_as_bot($client)) { $this->bot = 1; return $this; } if (isset($client['name'])) { $this->client_name = $client['name']; } if (isset($client['version'])) { $this->client_version = $client['version']; } if (isset($client['type'])) { $this->client_type = $client['type']; } $os = $dd->getOs(); if (isset($os['name'])) { $this->os_name = $os['name']; } if (isset($os['version'])) { $this->os_version = $os['version']; } $this->device_brand = $dd->getBrand(); $this->device_model = $dd->getModel(); } return $this; }
/** * @dataProvider getBotFixtures */ public function testParseBots($fixtureData) { $ua = $fixtureData['user_agent']; $dd = new DeviceDetector($ua); $dd->parse(); $this->assertTrue($dd->isBot()); $botData = $dd->getBot(); $this->assertEquals($botData['name'], $fixtureData['name']); // client and os will always be unknown for bots $this->assertEquals($dd->getOs('short_name'), DeviceDetector::UNKNOWN); $this->assertEquals($dd->getClient('short_name'), DeviceDetector::UNKNOWN); }
/** * * @param DeviceDetector $dd * * @return bool */ private function hasResult(DeviceDetector $dd) { if ($dd->isBot() === true) { return true; } $client = $dd->getClient(); if (isset($client['name']) && $this->isRealResult($client['name'])) { return true; } $os = $dd->getOs(); if (isset($os['name']) && $this->isRealResult($os['name'])) { return true; } if ($dd->getDevice() !== null) { return true; } return false; }
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(); }
/** * Parses a useragent and returns the detected data * * ATTENTION: Use that method only for testing or very small applications * To get fast results from DeviceDetector you need to make your own implementation, * that should use one of the caching mechanisms. See README.md for more information. * * @internal * @deprecated * * @param string $ua UserAgent to parse * * @return array */ public static function getInfoFromUserAgent($ua) { $deviceDetector = new DeviceDetector($ua); $deviceDetector->parse(); if ($deviceDetector->isBot()) { return array('user_agent' => $deviceDetector->getUserAgent(), 'bot' => $deviceDetector->getBot()); } $osFamily = OperatingSystem::getOsFamily($deviceDetector->getOs('short_name')); $browserFamily = \DeviceDetector\Parser\Client\Browser::getBrowserFamily($deviceDetector->getClient('short_name')); $processed = array('user_agent' => $deviceDetector->getUserAgent(), 'os' => $deviceDetector->getOs(), 'client' => $deviceDetector->getClient(), 'device' => array('type' => $deviceDetector->getDeviceName(), 'brand' => $deviceDetector->getBrand(), 'model' => $deviceDetector->getModel()), 'os_family' => $osFamily !== false ? $osFamily : 'Unknown', 'browser_family' => $browserFamily !== false ? $browserFamily : 'Unknown'); return $processed; }
/** * @dataProvider getUserAgents */ public function testTypeMethods($useragent, $isBot, $isMobile, $isDesktop) { $dd = new DeviceDetector($useragent); $dd->discardBotInformation(); $dd->parse(); $this->assertEquals($isBot, $dd->isBot()); $this->assertEquals($isMobile, $dd->isMobile()); $this->assertEquals($isDesktop, $dd->isDesktop()); }
public function testSkipBotDetection() { $ua = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'; $dd = new DeviceDetector($ua); $dd->parse(); $this->assertFalse($dd->isMobile()); $this->assertTrue($dd->isBot()); $dd = new DeviceDetector($ua); $dd->skipBotDetection(); $dd->parse(); $this->assertTrue($dd->isMobile()); $this->assertFalse($dd->isBot()); }
/** * 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; }
<?php require_once 'vendor/autoload.php'; use DeviceDetector\DeviceDetector; $userAgent = $argv[1]; $dd = new DeviceDetector($userAgent); $dd->parse(); if ($dd->isBot()) { var_dump($botInfo = $dd->getBot()); } else { $clientInfo = $dd->getClient(); // holds information about browser, feed reader, media player, ... $osInfo = $dd->getOs(); $device = $dd->getDevice(); $brand = $dd->getBrand(); $model = $dd->getModel(); var_dump($clientInfo, $osInfo, $device, $brand, $model); }
/** * Live/Bing/MSN bot and Googlebot are evolving to detect cloaked websites. * * As a result, these sophisticated bots exhibit browsers characteristics * (cookies enabled, executing JavaScript, etc). * * @see \DeviceDetector\Parser\Bot * * @return boolean */ public function isBot() { $allowBots = Yii::$app->getRequest()->getQueryParam("bots"); $deviceDetector = new DeviceDetector($this->getUserAgent()); $deviceDetector->discardBotInformation(); $deviceDetector->parse(); return !$allowBots && ($deviceDetector->isBot() || $this->isInBotRanges()); }