/** * Detects the capabilities of a device from a given user agent and optionally, the HTTP Accept Headers * @param String HTTP User Agent * @param String HTTP Accept Header * @return Bool matching device was found */ public function getDeviceCapabilitiesFromAgent($userAgent = null, $httpAccept = null) { $this->db->numQueries = 0; $this->matchData = array("num_queries" => 0, "actual_root_device" => '', "match_type" => '', "matcher" => '', "match" => false, "lookup_time" => 0, "fall_back_tree" => ''); $this->lookup_start = microtime(true); $this->foundInCache = false; $this->capabilities = array(); // Define User Agent $this->userAgent = is_null($userAgent) ? WurflSupport::getUserAgent() : $userAgent; if (strlen($this->userAgent) > 255) { $this->userAgent = substr($this->userAgent, 0, 255); } // Use the ultra high performance SimpleDesktopMatcher if enabled if (TeraWurflConfig::$SIMPLE_DESKTOP_ENGINE_ENABLE) { require_once realpath(dirname(__FILE__) . '/UserAgentMatchers/SimpleDesktopUserAgentMatcher.php'); if (SimpleDesktopUserAgentMatcher::isDesktopBrowser($userAgent)) { $this->userAgent = WurflConstants::$SIMPLE_DESKTOP_UA; } } // Define HTTP ACCEPT header. Default: DO NOT use HTTP_ACCEPT headers //$this->httpAccept= (is_null($httpAccept))? WurflSupport::getAcceptHeader(): $httpAccept; $this->userAgent = UserAgentUtils::cleanUserAgent($this->userAgent); // Check cache for device if (TeraWurflConfig::$CACHE_ENABLE) { $cacheData = $this->db->getDeviceFromCache($this->userAgent); // Found in cache if ($cacheData !== false) { $this->capabilities = $cacheData; $this->foundInCache = true; $deviceID = $cacheData['id']; } } if (!$this->foundInCache) { require_once realpath(dirname(__FILE__) . '/UserAgentMatchers/SimpleDesktopUserAgentMatcher.php'); // Find appropriate user agent matcher $this->userAgentMatcher = UserAgentFactory::createUserAgentMatcher($this, $this->userAgent); // Find the best matching WURFL ID $deviceID = $this->getDeviceIDFromUALoose($userAgent); // Get the capabilities of this device and all its ancestors $this->getFullCapabilities($deviceID); // Now add in the Tera-WURFL results array $this->lookup_end = microtime(true); $this->matchData['num_queries'] = $this->db->numQueries; $this->matchData['lookup_time'] = $this->lookup_end - $this->lookup_start; // Add the match data to the capabilities array so it gets cached $this->addCapabilities(array($this->matchDataKey => $this->matchData)); } if (TeraWurflConfig::$CACHE_ENABLE == true && !$this->foundInCache) { // Since this device was not cached, cache it now. $this->db->saveDeviceInCache($this->userAgent, $this->capabilities); } return $this->capabilities[$this->matchDataKey]['match']; }
/** * Detects the capabilities from a given request object ($_SERVER) * @param array $request_headers Request object ($_SERVER contains this data) * @return boolean Match */ public function getDeviceCapabilitiesFromRequest($request_headers = null) { $this->httpRequest = $request_headers instanceof TeraWurflHttpRequest ? $request_headers : new TeraWurflHttpRequest($request_headers); $this->db->numQueries = 0; $this->matchData = array("num_queries" => 0, "match_type" => '', "matcher" => '', "match" => false, "lookup_time" => 0); $this->lookup_start = microtime(true); $this->foundInCache = false; $this->capabilities = array(); // Use the ultra high performance SimpleDesktopMatcher if enabled if (TeraWurflConfig::$SIMPLE_DESKTOP_ENGINE_ENABLE) { require_once realpath(dirname(__FILE__) . '/UserAgentMatchers/SimpleDesktopUserAgentMatcher.php'); if (SimpleDesktopUserAgentMatcher::isDesktopBrowserHeavyDutyAnalysis($this->httpRequest)) { $this->httpRequest->user_agent->set(WurflConstants::SIMPLE_DESKTOP_UA); } } // Check cache for device if (TeraWurflConfig::$CACHE_ENABLE) { $cacheData = $this->db->getDeviceFromCache($this->httpRequest->user_agent->cleaned); // Found in cache if ($cacheData !== false) { $this->capabilities = $cacheData; $this->foundInCache = true; $deviceID = $cacheData['id']; } } if (!$this->foundInCache) { require_once realpath(dirname(__FILE__) . '/UserAgentMatchers/SimpleDesktopUserAgentMatcher.php'); // Find appropriate user agent matcher $this->userAgentMatcher = UserAgentMatcherFactory::createUserAgentMatcher($this, $this->httpRequest); // Find the best matching WURFL ID $deviceID = $this->getDeviceIDFromRequestLoose(); // Get the capabilities of this device and all its ancestors $this->getFullCapabilities($deviceID); // Now add in the Tera-WURFL results array $this->lookup_end = microtime(true); $this->matchData['num_queries'] = $this->db->numQueries; $this->matchData['lookup_time'] = $this->lookup_end - $this->lookup_start; // Add the match data to the capabilities array so it gets cached $this->addCapabilities(array($this->matchDataKey => $this->matchData)); } if (TeraWurflConfig::$CACHE_ENABLE == true && !$this->foundInCache) { // Since this device was not cached, cache it now. $this->db->saveDeviceInCache($this->httpRequest->user_agent->cleaned, $this->capabilities); } return $this->capabilities[$this->matchDataKey]['match']; }