if no user agent is given, it uses {@see \BrowscapPHP\Helper\Support} to get it
public getBrowser ( string $userAgent = null ) : stdClass | ||
$userAgent | string | the user agent string |
return | stdClass | the object containing the browsers details. Array if $return_array is set to true. |
/** * @dataProvider providerUserAgent * @depends testCheckProperties * @group compare * * @param string $userAgent */ public function testCompare($userAgent) { $libResult = get_browser($userAgent); $bcResult = self::$object->getBrowser($userAgent); foreach (array_keys($this->properties) as $bcProp) { if (in_array($bcProp, array('browser_name_regex', 'browser_name_pattern', 'Parent'))) { continue; } $bcProp = strtolower($bcProp); self::assertObjectHasAttribute($bcProp, $libResult, 'Actual library result does not have "' . $bcProp . '" property'); self::assertObjectHasAttribute($bcProp, $bcResult, 'Actual browscap result does not have "' . $bcProp . '" property'); $libValue = (string) $libResult->{$bcProp}; $bcValue = (string) $bcResult->{$bcProp}; self::assertSame($libValue, $bcValue, 'Expected actual "' . $bcProp . '" to be "' . $libValue . '" (was "' . $bcValue . '"; used pattern: ' . $bcResult->browser_name_pattern . ')'); } }
/** * @dataProvider userAgentDataProvider * @coversNothing * * @param string $userAgent * @param array $expectedProperties * @param bool $lite * @param bool $standard * * @throws \Exception * @throws \BrowscapPHP\Exception * * @group intergration * @group useragenttest * @group lite */ public function testUserAgentsLite($userAgent, $expectedProperties, $lite = true, $standard = true) { if (!is_array($expectedProperties) || !count($expectedProperties)) { self::markTestSkipped('Could not run test - no properties were defined to test'); } if (!$lite) { self::markTestSkipped('Test skipped - Browser/Platform/Version not defined for Lite Mode'); } static $updatedLiteCache = false; if (!$updatedLiteCache) { self::$browscap->convertFile(self::$buildFolder . '/lite_php_browscap.ini'); $updatedLiteCache = true; } $actualProps = (array) self::$browscap->getBrowser($userAgent); foreach ($expectedProperties as $propName => $propValue) { if (!self::$propertyHolder->isOutputProperty($propName)) { continue; } if (!self::$propertyHolder->isLiteModeProperty($propName)) { continue; } $propName = strtolower($propName); self::assertArrayHasKey($propName, $actualProps, 'Actual properties did not have "' . $propName . '" property'); self::assertSame($propValue, $actualProps[$propName], 'Expected actual "' . $propName . '" to be "' . $propValue . '" (was "' . $actualProps[$propName] . '"; used pattern: ' . $actualProps['browser_name_pattern'] . ')'); } }
/** * */ public function testGetBrowserWithDefaultResult() { $formatter = $this->getMock('\\BrowscapPHP\\Formatter\\PhpGetBrowser', array('getData'), array(), '', false); $formatter->expects(self::once())->method('getData')->will(self::returnValue(null)); $parser = $this->getMock('\\BrowscapPHP\\Parser\\Ini', array('getBrowser'), array(), '', false); $parser->expects(self::once())->method('getBrowser')->will(self::returnValue(null)); $this->object->setFormatter($formatter); $this->object->setParser($parser); $result = $this->object->getBrowser('Mozilla/5.0 (compatible; Ask Jeeves/Teoma)'); self::assertNull($result); }
/** * @param string $ua * @param string $mode * * @return string */ private function testUA($ua, $mode) { $data = $this->browscap->getBrowser($ua, true); if ($mode === GrepCommand::MODE_UNMATCHED && $data['Browser'] === 'Default Browser') { $this->logger->info($ua); return GrepCommand::MODE_UNMATCHED; } elseif ($mode === GrepCommand::MODE_MATCHED && $data['Browser'] !== 'Default Browser') { $this->logger->info($ua); return GrepCommand::MODE_MATCHED; } return GrepCommand::FOUND_INVISIBLE; }
/** * */ public function testGetBrowserWithDefaultResult() { $formatter = $this->getMockBuilder(\BrowscapPHP\Formatter\PhpGetBrowser::class)->disableOriginalConstructor()->setMethods(['getData'])->getMock(); $formatter->expects(self::once())->method('getData')->will(self::returnValue(null)); $parser = $this->getMockBuilder(\BrowscapPHP\Parser\Ini::class)->disableOriginalConstructor()->setMethods(['getBrowser'])->getMock(); $parser->expects(self::once())->method('getBrowser')->will(self::returnValue(null)); $cache = $this->getMockBuilder(\BrowscapPHP\Cache\BrowscapCache::class)->disableOriginalConstructor()->setMethods(['getVersion'])->getMock(); $cache->expects(self::once())->method('getVersion')->will(self::returnValue(1)); $this->object->setFormatter($formatter); $this->object->setParser($parser); $this->object->setCache($cache); $result = $this->object->getBrowser('Mozilla/5.0 (compatible; Ask Jeeves/Teoma)'); self::assertNull($result); }
/** * @param InputInterface $input * @param OutputInterface $output * * @return int|null|void */ protected function execute(InputInterface $input, OutputInterface $output) { $loggerHelper = new LoggerHelper(); $logger = $loggerHelper->create($input->getOption('debug')); $browscap = new Browscap(); $browscap->setLogger($logger)->setCache($this->getCache($input)); $result = $browscap->getBrowser($input->getArgument('user-agent')); if (!defined('JSON_PRETTY_PRINT')) { // not defined in PHP 5.3 define('JSON_PRETTY_PRINT', 128); } $output->writeln(json_encode($result, JSON_PRETTY_PRINT)); }
/** * @param \Symfony\Component\Console\Output\OutputInterface $output * @param \BrowscapPHP\Util\Logfile\ReaderCollection $collection * @param \BrowscapPHP\Browscap $browscap * @param int $line * * @throws UnknownBrowserException * @throws UnknownBrowserTypeException * @throws UnknownDeviceException * @throws UnknownEngineException * @throws UnknownPlatformException * @throws \Exception */ private function handleLine(OutputInterface $output, ReaderCollection $collection, Browscap $browscap, $line) { $userAgentString = ''; try { $userAgentString = $collection->read($line); try { $this->getResult($browscap->getBrowser($userAgentString)); } catch (\Exception $e) { $this->undefinedClients[] = $userAgentString; throw $e; } $type = '.'; ++$this->countOk; } catch (ReaderException $e) { $type = 'E'; ++$this->countNok; } catch (UnknownBrowserTypeException $e) { $type = 'T'; ++$this->countNok; } catch (UnknownBrowserException $e) { $type = 'B'; ++$this->countNok; } catch (UnknownPlatformException $e) { $type = 'P'; ++$this->countNok; } catch (UnknownDeviceException $e) { $type = 'D'; ++$this->countNok; } catch (UnknownEngineException $e) { $type = 'N'; ++$this->countNok; } catch (\Exception $e) { $type = 'U'; ++$this->countNok; } $this->outputProgress($output, $type); // count all useragents if (isset($this->uas[$userAgentString])) { ++$this->uas[$userAgentString]; } else { $this->uas[$userAgentString] = 1; } if ('.' !== $type && 'E' !== $type) { // count all undetected useragents grouped by detection error if (!isset($this->uasWithType[$type])) { $this->uasWithType[$type] = []; } if (isset($this->uasWithType[$type][$userAgentString])) { ++$this->uasWithType[$type][$userAgentString]; } else { $this->uasWithType[$type][$userAgentString] = 1; } } }
/** * Display the sessions and logs informations. * * @return void */ public function security() { $records = $this->SessionsActivity->getOnlineSessionsForUser($this->Auth->user('id')); $browscap = new Browscap(); $sessions = []; foreach ($records as $record) { $infos = $browscap->getBrowser($record->user_agent); $record->infos = $infos; array_push($sessions, $record); } $this->loadModel('UsersLogs'); $this->paginate = ['maxLimit' => 25]; $logs = $this->UsersLogs->find()->where(['UsersLogs.user_id' => $this->Auth->user('id')])->order(['UsersLogs.created' => 'DESC'])->formatResults(function ($logs) use($browscap) { return $logs->map(function ($log) use($browscap) { $log->infos = $browscap->getBrowser($log->user_agent); return $log; }); }); $logs = $this->paginate($logs); $user = $this->Users->find()->where(['Users.id' => $this->Auth->user('id')])->select(['id', 'two_factor_auth_enabled'])->first(); $this->set(compact('sessions', 'logs', 'user')); }