Abstract class for all device parsers
Inheritance: extends DeviceDetector\Parser\ParserAbstract
Ejemplo n.º 1
0
 /**
  * @dataProvider getVersionTruncationFixtures
  */
 public function versionTruncationTest($useragent, $truncationType, $osVersion, $clientVersion)
 {
     DeviceParserAbstract::setVersionTruncation($truncationType);
     $dd = new DeviceDetector($useragent);
     $this->assertEquals($osVersion, $dd->getOs('version'));
     $this->assertEquals($clientVersion, $dd->getClient('version'));
 }
Ejemplo n.º 2
0
 public function configureView(ViewDataTable $view)
 {
     $unknownTypeCount = 1;
     $view->requestConfig->filter_limit = $unknownTypeCount + count(DeviceParser::getAvailableDeviceTypeNames());
     $view->config->show_search = false;
     $view->config->show_exclude_low_population = false;
     $view->config->addTranslation('label', Piwik::translate("DevicesDetection_dataTableLabelTypes"));
 }
Ejemplo n.º 3
0
 protected function getRawMetadataDeviceType()
 {
     $deviceTypeList = implode(", ", DeviceParser::getAvailableDeviceTypeNames());
     $deviceTypeLabelToCode = function ($type) use($deviceTypeList) {
         $index = array_search(strtolower(trim(urldecode($type))), DeviceParser::getAvailableDeviceTypeNames());
         if ($index === false) {
             throw new Exception("deviceType segment must be one of: {$deviceTypeList}");
         }
         return $index;
     };
     return array('DevicesDetection_DevicesDetection', 'DevicesDetection_DeviceType', 'DevicesDetection', 'getType', 'DevicesDetection_DeviceType', 'deviceType', 'log_visit.config_device_type', $deviceTypeList, $deviceTypeLabelToCode);
 }
Ejemplo n.º 4
0
 protected function ensureDefaultRowsInTable($dataTable)
 {
     $requiredRows = array_fill(0, count(DeviceParserAbstract::getAvailableDeviceTypes()), Metrics::INDEX_NB_VISITS);
     $dataTables = array($dataTable);
     if (!$dataTable instanceof DataTable\Map) {
         foreach ($dataTables as $table) {
             if ($table->getRowsCount() == 0) {
                 continue;
             }
             foreach ($requiredRows as $requiredRow => $key) {
                 $row = $table->getRowFromLabel($requiredRow);
                 if (empty($row)) {
                     $table->addRowsFromSimpleArray(array(array('label' => $requiredRow, $key => 0)));
                 }
             }
         }
     }
 }
Ejemplo n.º 5
0
 protected function configureSegments()
 {
     $deviceTypes = DeviceParser::getAvailableDeviceTypeNames();
     $deviceTypeList = implode(", ", $deviceTypes);
     $segment = new Segment();
     $segment->setCategory('General_Visit');
     $segment->setSegment('deviceType');
     $segment->setName('DevicesDetection_DeviceType');
     $segment->setAcceptedValues($deviceTypeList);
     $segment->setSqlFilter(function ($type) use($deviceTypeList, $deviceTypes) {
         $index = array_search(strtolower(trim(urldecode($type))), $deviceTypes);
         if ($index === false) {
             throw new Exception("deviceType segment must be one of: {$deviceTypeList}");
         }
         return $index;
     });
     $this->addSegment($segment);
 }
Ejemplo n.º 6
0
 public function detect($userAgent = null, $advanced = false)
 {
     $userAgent = $userAgent ?: env('HTTP_USER_AGENT');
     DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE);
     $detect = new DeviceDetector($userAgent);
     $detect->parse();
     $return = [];
     if ($detect->isBot()) {
         $return['bot'] = $detect->getBot();
         return $return;
     }
     //device wrapper
     $devicelist = ['desktop' => 'computer', 'smartphone' => 'phone', 'tablet' => 'tablet', 'feature phone' => 'phone'];
     $os = $detect->getOs();
     $client = $detect->getClient();
     $devicename = $detect->getDeviceName();
     $devicetype = isset($devicelist[$devicename]) ? $devicelist[$devicename] : 'computer';
     //legacy params
     $return['device'] = $devicename;
     $return['type'] = $devicetype;
     $return['brand'] = $detect->getBrandName();
     $return['os'] = $os['name'];
     $return['os_version'] = $os['version'];
     $return['os_code'] = $os['short_name'];
     $return['browser'] = $client['name'];
     $return['browser_version'] = $client['version'];
     $return['browser_code'] = $client['short_name'];
     $return['browser_type'] = $client['type'];
     $return['browser_engine'] = $client['engine'];
     if (!$advanced) {
         return array_map('trim', $return);
     }
     //advanced params
     $osFamily = OperatingSystem::getOsFamily($os['short_name']);
     $return['os_family'] = $osFamily !== false ? $osFamily : 'Unknown';
     $return['model'] = $detect->getModel();
     $browserFamily = Browser::getBrowserFamily($client['short_name']);
     $return['browser_family'] = $browserFamily !== false ? $browserFamily : 'Unknown';
     $touch = $detect->isTouchEnabled();
     $return['touch'] = $touch[0];
     unset($os, $client, $osFamily, $browserFamily, $touch);
     return array_map('trim', $return);
 }
Ejemplo n.º 7
0
 public function showList()
 {
     Piwik::checkUserHasSomeAdminAccess();
     $view = new View('@DevicesDetection/list');
     $type = Common::getRequestVar('type', 'brands', 'string');
     $list = array();
     switch ($type) {
         case 'brands':
             $availableBrands = \DeviceDetector\Parser\Device\DeviceParserAbstract::$deviceBrands;
             foreach ($availableBrands as $short => $name) {
                 if ($name != 'Unknown') {
                     $list[$name] = getBrandLogo($name);
                 }
             }
             break;
         case 'browsers':
             $availableBrowsers = \DeviceDetector\Parser\Client\Browser::getAvailableBrowsers();
             foreach ($availableBrowsers as $short => $name) {
                 $list[$name] = getBrowserLogo($short);
             }
             break;
         case 'browserfamilies':
             $availableBrowserFamilies = \DeviceDetector\Parser\Client\Browser::getAvailableBrowserFamilies();
             foreach ($availableBrowserFamilies as $name => $browsers) {
                 $list[$name] = getBrowserFamilyLogo($name);
             }
             break;
         case 'os':
             $availableOSs = \DeviceDetector\Parser\OperatingSystem::getAvailableOperatingSystems();
             foreach ($availableOSs as $short => $name) {
                 $list[$name] = getOsLogo($short);
             }
             break;
         case 'osfamilies':
             $osFamilies = \DeviceDetector\Parser\OperatingSystem::getAvailableOperatingSystemFamilies();
             foreach ($osFamilies as $name => $oss) {
                 $list[$name] = getOsFamilyLogo($name);
             }
             break;
         case 'devicetypes':
             $deviceTypes = \DeviceDetector\Parser\Device\DeviceParserAbstract::getAvailableDeviceTypes();
             foreach ($deviceTypes as $name => $id) {
                 $list[$name] = getDeviceTypeLogo($name);
             }
             break;
     }
     $view->itemList = $list;
     return $view->render();
 }
Ejemplo n.º 8
0
 /**
  * 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();
 }
Ejemplo n.º 9
0
function getDeviceTypeLogo($label)
{
    if (is_numeric($label) && in_array($label, DeviceParser::getAvailableDeviceTypes())) {
        $label = array_search($label, DeviceParser::getAvailableDeviceTypes());
    }
    $label = strtolower($label);
    $deviceTypeLogos = array("desktop" => "normal.gif", "smartphone" => "smartphone.png", "tablet" => "tablet.png", "tv" => "tv.png", "feature phone" => "mobile.gif", "console" => "console.gif", "car browser" => "carbrowser.png", "camera" => "camera.png");
    if (!array_key_exists($label, $deviceTypeLogos)) {
        $label = 'unknown.gif';
    } else {
        $label = $deviceTypeLogos[$label];
    }
    $path = 'plugins/DevicesDetection/images/screens/' . $label;
    return $path;
}
Ejemplo n.º 10
0
 /**
  * Returns the full device brand name extracted from the parsed UA
  *
  * @see self::$deviceBrand for available device brands
  *
  * @return string
  */
 public function getBrandName()
 {
     return DeviceParserAbstract::getFullName($this->getBrand());
 }
 public function testGetFullName()
 {
     $this->assertEquals('', DeviceParserAbstract::getFullName('Invalid'));
     $this->assertEquals('Unknown', DeviceParserAbstract::getFullName('XX'));
     $this->assertEquals('Google', DeviceParserAbstract::getFullName('GO'));
 }
Ejemplo n.º 12
0
 /**
  * @dataProvider getFixtures
  */
 public function testParse($fixtureData)
 {
     $ua = $fixtureData['user_agent'];
     DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE);
     $uaInfo = DeviceDetector::getInfoFromUserAgent($ua);
     $this->assertEquals($fixtureData, $uaInfo);
 }
Ejemplo n.º 13
0
<?php

/**
 * Device Detector - The Universal Device Detection library for parsing User Agents
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */
require __DIR__ . '/vendor/autoload.php';
use DeviceDetector\DeviceDetector;
use DeviceDetector\Parser\Device\DeviceParserAbstract;
if (isset($_GET['ua'])) {
    $userAgent = $_GET['ua'];
} else {
    $userAgent = $_SERVER['HTTP_USER_AGENT'];
}
DeviceParserAbstract::setVersionTruncation(DeviceParserAbstract::VERSION_TRUNCATION_NONE);
$result = DeviceDetector::getInfoFromUserAgent($userAgent);
echo '<form><input type="text" name="ua" /><input type="submit" /></form>';
echo "<pre>";
echo Spyc::YAMLDump($result, 2, 0);
echo "</pre>";
Ejemplo n.º 14
0
 /**
  * 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;
 }