(Discarding information speeds up the detection a bit)
public discardBotInformation ( boolean $discard = true ) | ||
$discard | boolean |
/** * 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; }
public function __construct(RequestStack $stack, CacheProvider $cache, $deviceDetectorOptions) { $this->cacheManager = $cache; $this->requestStack = $stack; $this->setOptions($deviceDetectorOptions); if (null !== $this->requestStack && $this->requestStack->getCurrentRequest()) { $userAgent = $this->requestStack->getCurrentRequest()->headers->get('User-Agent'); } else { $userAgent = ''; } $this->deviceDetector = new \DeviceDetector\DeviceDetector($userAgent); $this->deviceDetector->setCache($this->getCacheManager()); if (!empty($this->deviceDetectorOptions['discard_bot_information'])) { $this->deviceDetector->discardBotInformation(); } if (!empty($this->deviceDetectorOptions['skip_bot_detection'])) { $this->deviceDetector->skipBotDetection(); } $this->deviceDetector->parse(); }
/** * Returns a Singleton instance of DeviceDetector for the given user agent * @param string $userAgent * @return DeviceDetector */ public static function getInstance($userAgent) { if (array_key_exists($userAgent, self::$deviceDetectorInstances)) { return self::$deviceDetectorInstances[$userAgent]; } $deviceDetector = new DeviceDetector($userAgent); $deviceDetector->discardBotInformation(); $deviceDetector->setCache(new DeviceDetectorCache(86400)); $deviceDetector->parse(); self::$deviceDetectorInstances[$userAgent] = $deviceDetector; return $deviceDetector; }
/** * 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 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()); }
/** * 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; }
/** * 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()); }