protected function parseDevice() { if (count($this->pendingInsert) > 0) { foreach ($this->pendingInsert as $device) { $matcher = UserAgentFactory::userAgentType($this->wurfl, $device['user_agent']); $uatable = TeraWurflConfig::$TABLE_PREFIX . '_' . $matcher; } $this->pendingInsert = array(); } $this->pendingInsert[$this->xml->getAttribute('id')] = array(); $device =& $this->pendingInsert[$this->xml->getAttribute('id')]; $device = array('id' => $this->xml->getAttribute('id'), 'user_agent' => UserAgentUtils::cleanUserAgent($this->xml->getAttribute('user_agent')), 'fall_back' => $this->xml->getAttribute('fall_back')); if ($this->xml->getAttribute('actual_device_root')) { $device['actual_device_root'] = $this->xml->getAttribute('actual_device_root') == "true" ? 1 : 0; } $groupdevice = ''; $groupname = ''; $filtering = TeraWurflConfig::$CAPABILITY_FILTER ? true : false; $includegroup = false; while ($this->xml->read()) { if ($this->xml->nodeType != XMLReader::ELEMENT) { continue; } // recurse back into this function for the rest of the devices switch ($this->xml->name) { case "device": $this->parseDevice(); break; case "group": $groupname = $this->xml->getAttribute('id'); if ($filtering && $this->enabled($this->xml->getAttribute('id'))) { $includegroup = true; } else { $includegroup = false; continue; } $device[$groupname] = array(); break; case "capability": if (!$filtering || $filtering && $includegroup) { // the groupdevice array must already exist $device[$groupname][$this->xml->getAttribute('name')] = self::cleanValue($this->xml->getAttribute('value')); continue; } if ($filtering && !$includegroup && $this->enabled($this->xml->getAttribute('name'))) { // the groupdevice array might already exists if (!array_key_exists($groupname, $device)) { $device[$groupname] = array(); } $device[$groupname][$this->xml->getAttribute('name')] = self::cleanValue($this->xml->getAttribute('value')); continue; } break; } } }
/** * 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']; }
protected function loadDeviceXMLToArray(&$device) { $id = (string) $device['id']; $this->devices[$id] = array('id' => $id); $filtering = TeraWurflConfig::$CAPABILITY_FILTER ? true : false; $includegroup = false; if (isset($device['fall_back'])) { $this->devices[$id]['fall_back'] = (string) $device['fall_back']; } if (isset($device['user_agent'])) { $this->devices[$id]['user_agent'] = UserAgentUtils::cleanUserAgent((string) $device['user_agent']); } if (isset($device['actual_device_root'])) { $this->devices[$id]['actual_device_root'] = (string) $device['actual_device_root']; $this->devices[$id]['actual_device_root'] = $this->devices[$id]['actual_device_root'] ? 1 : 0; } foreach ($device->group as $group) { $groupname = (string) $group['id']; if ($filtering && $this->enabled($groupname)) { $includegroup = true; } else { $includegroup = false; } $groupdata = array(); foreach ($group->capability as $cap) { $capname = (string) $cap['name']; if (!$filtering || $filtering && $includegroup || $filtering && !$includegroup && $this->enabled($capname)) { $groupdata[$capname] = $this->cleanValue((string) $cap['value']); } } if (count($groupdata) > 0) { $this->devices[$id][$groupname] = $groupdata; } unset($groupdata); } }