/** * @param \Wurfl\Request\GenericRequest $request */ public function __construct(GenericRequest $request) { $this->httpRequest = $request; $this->deviceUa = $this->httpRequest->getDeviceUserAgent(); $this->browserUa = $this->httpRequest->getBrowserUserAgent(); $this->deviceUaNormalized = $this->httpRequest->getUserAgentNormalized(); $this->browserUaNormalized = $this->deviceUaNormalized; $this->browser = new NameVersionPair($this); $this->os = new NameVersionPair($this); }
/** * Return the the device id for the request * * @param \Wurfl\Request\GenericRequest $request * * @return string deviceID */ public function match(GenericRequest $request) { Utils::reset(); $handlers = $this->getHandlers(); $matchResult = WurflConstants::NO_MATCH; foreach ($handlers as $handler) { /** @var $handler \Wurfl\Handlers\AbstractHandler */ $handler->setLogger($this->logger); if ($handler->canHandle($request->getUserAgentNormalized())) { $matchResult = $handler->applyMatch($request); break; } } return $matchResult; }
/** * Returns the match info for this device * * @return \Wurfl\Request\MatchInfo|null */ public function getMatchInfo() { return $this->request instanceof Request\GenericRequest ? $this->request->getMatchInfo() : null; }
/** * Adds the user agent hash and user agent to a list for retrieval * * @param \Wurfl\Request\GenericRequest $request * @param \stdClass $info */ private function addToUAList(GenericRequest $request, \stdClass $info) { $uaList = $this->getUaList(); if (isset($uaList[$info->uaHash])) { return; } // merge the old list with the new user agent $mergedInfo = (object) array_merge($uaList, array($info->uaHash => $request->getBrowserUserAgent())); // write out the data to the user agent list $uaListJSON = json_encode($mergedInfo); file_put_contents(__DIR__ . '/user-agents/ua.list.json', $uaListJSON); }
/** * Template method to apply matching system to user agent * * @param \Wurfl\Request\GenericRequest $request * * @return string Device ID */ public final function applyMatch(GenericRequest $request) { $className = get_class($this); $request->getMatchInfo()->matcher = $className; $startTime = microtime(true); $request->getMatchInfo()->cleanedUserAgent = $request->getUserAgentNormalized(); $userAgent = $this->normalizeUserAgent($request->getUserAgentNormalized()); $request->getMatchInfo()->normalizedUserAgent = $userAgent; $this->logger->debug('START: Matching For ' . $userAgent); // Get The data associated with this current handler $this->userAgentsWithDeviceID = $this->getUserAgentsWithDeviceId(); $this->userAgents = $this->getUserAgentsForBucket(); $matches = array('exact' => array('history' => '(exact),', 'function' => 'applyExactMatch', 'debug' => 'Applying Exact Match'), 'conclusive' => array('history' => '(conclusive),', 'function' => 'applyConclusiveMatch', 'debug' => 'Applying Conclusive Match'), 'recovery' => array('history' => '(recovery),', 'function' => 'applyRecoveryMatch', 'debug' => 'Applying Recovery Match'), 'recovery-catchall' => array('history' => '(recovery-catchall),', 'function' => 'applyRecoveryCatchAllMatch', 'debug' => 'Applying Catch All Recovery Match')); $deviceID = WurflConstants::NO_MATCH; foreach ($matches as $matchType => $matchProps) { $matchProps = (object) $matchProps; $request->getMatchInfo()->matcherHistory .= $className . $matchProps->history; $request->getMatchInfo()->matchType = $matchType; $request->setUserAgentsWithDeviceID($this->userAgentsWithDeviceID); $this->logger->debug($this->prefix . ' :' . $matchProps->debug . ' for ua: ' . $userAgent); $function = $matchProps->function; $deviceID = $this->{$function}($userAgent); if (!$this->isBlankOrGeneric($deviceID)) { break; } } // All attempts to match have failed if ($this->isBlankOrGeneric($deviceID)) { $request->getMatchInfo()->matchType = 'none'; if ($request->getUserAgentProfile()) { $deviceID = WurflConstants::GENERIC_MOBILE; } else { $deviceID = WurflConstants::GENERIC; } } $this->logger->debug('END: Matching For ' . $userAgent); $request->getMatchInfo()->lookupTime = microtime(true) - $startTime; return $deviceID; }
/** * Returns the device id for the device that matches the $request * * @param \Wurfl\Request\GenericRequest $request WURFL Request object * * @return string WURFL device id */ private function deviceIdForRequest(Request\GenericRequest $request) { $userAgent = $request->getUserAgent(); if (!$userAgent) { // $request->id is not set // -> do not try to get info from cache nor try to save to the cache $request->getMatchInfo()->fromCache = 'invalid id'; $request->getMatchInfo()->lookupTime = 0.0; return $this->getUserAgentHandlerChain()->match($request); } $deviceId = $this->getCacheStorage()->load($userAgent); if (empty($deviceId)) { $genericNormalizer = UserAgentHandlerChainFactory::createGenericNormalizers(); $request->setUserAgentNormalized($genericNormalizer->normalize($userAgent)); if ($this->getWurflConfig()->isHighPerformance() && Handlers\Utils::isDesktopBrowserHeavyDutyAnalysis($request->getUserAgentNormalized())) { // This device has been identified as a web browser programatically, // so no call to WURFL is necessary $deviceId = WurflConstants::GENERIC_WEB_BROWSER; } else { $deviceId = $this->getUserAgentHandlerChain()->match($request); } // save it in cache $this->getCacheStorage()->save($userAgent, $deviceId); } else { $request->getMatchInfo()->fromCache = true; $request->getMatchInfo()->lookupTime = 0.0; } return $deviceId; }
public function testConstruct() { $userAgent = 'testUA'; $header = array(Constants::HEADER_HTTP_USERAGENT => $userAgent); $object = new GenericRequest($header, $userAgent, null, false); self::assertSame($userAgent, $object->getUserAgent()); self::assertSame($userAgent, $object->getUserAgentNormalized()); self::assertSame($header, $object->getRequest()); self::assertFalse($object->isXhtmlDevice()); self::assertNull($object->getUserAgentProfile()); self::assertSame(hash('sha512', $userAgent), $object->getId()); self::assertInstanceOf('\\Wurfl\\Request\\MatchInfo', $object->getMatchInfo()); self::assertSame(array(), $object->getUserAgentsWithDeviceID()); self::assertSame($userAgent, $object->getOriginalHeader(Constants::HEADER_HTTP_USERAGENT)); self::assertNull($object->getOriginalHeader(Constants::HEADER_DEVICE_STOCK_UA)); }