/** * Retrieve Mobile-detect service * * @param \Zend\Http\Headers $oHeaders * * @throws \LogicException * @return \Mobile_Detect */ public function __invoke(\Zend\Http\Headers $oHeaders = null) { $oController = $this->getController(); if (!$oController) { throw new \LogicException('Controller is undefined for MobileDetect controller plugin'); } if ($oHeaders) { $this->mobileDetect->setHttpHeaders($oHeaders->toArray()); $this->mobileDetect->setUserAgent($oHeaders->get('user-agent')->getFieldValue()); } return $this->mobileDetect; }
function action() { parent::action(); if (is_array($this->arrResults) && count($this->arrResults) > 0) { $objPurchase = new SC_Helper_Purchase_Ex(); $detect = new Mobile_Detect(); foreach ($this->arrResults as &$order) { $order["detail"] = $objPurchase->getOrderDetail($order["order_id"]); if (isset($order["useragent"])) { $detect->setUserAgent($order["useragent"]); $useragent = array(); if ($detect->is("AndroidOS")) { preg_match("{.*;([^;]+) Build}", $order["useragent"], $useragent); $order["device"] = $useragent[1]; } } } } }
/** * Index Page for this controller. * * Maps to the following URL * http://example.com/index.php/welcome * - or - * http://example.com/index.php/welcome/index * - or - * Since this controller is set as the default controller in * config/routes.php, it's displayed at http://example.com/ * * So any other public methods not prefixed with an underscore will * map to /index.php/welcome/<method_name> * @see http://codeigniter.com/user_guide/general/urls.html */ public function index() { require_once 'mobile_detect.php'; $detect = new Mobile_Detect(); $detect->setUserAgent($_SERVER['HTTP_USER_AGENT']); $is_mobile = $detect->isMobile(); $is_tablet = $detect->isTablet(); $is_web = 'true'; if ($is_mobile || $is_tablet) { $is_web = 'false'; } log_message('error', 'is mobile => ' . $is_mobile); log_message('error', 'is tablet => ' . $is_tablet); log_message('error', 'is web => ' . $is_web); $js['is_web'] = $is_web; $js['page'] = ''; $this->load->view('head'); $this->load->view('index'); $this->load->view('footer'); $this->load->view('/jquery/js', $js); }
/** * Creating a route for a specific mobile operating system * The router should return that controller & method */ public function testDeviceOsRouting() { // Create a route and routecollection $route0 = new Route('/mobiletest', array('controller' => 'MobileController:android'), array(), array('device' => 'mobile', 'os' => 'android')); $route1 = new Route('/mobiletest', array('controller' => 'MobileController:ios'), array(), array('device' => 'mobile', 'os' => 'iOS', 'type' => 'iPhone')); $route2 = new Route('/mobiletest', array('controller' => 'MobileController:default'), array(), array('device' => 'mobile')); $route3 = new Route('/mobiletest', array('controller' => 'GenericController:android'), array(), array('device' => 'tablet', 'os' => 'Android', 'type' => 'SonyTablet')); $routes = new RouteCollection(); $routes->add('route0', $route0); $routes->add('route1', $route1); $routes->add('route2', $route2); $routes->add('route3', $route3); // Our test request $request = WebRequest::create('/mobiletest', 'GET', array('name' => 'Chris Noden')); $request->overrideGlobals(); // Build our fake iPhone test device object $device = new \Mobile_Detect(); $device->setUserAgent('Mozilla/5.0 (iPhone; U; CPU iPhone OS 6_0 like Mac OS X; en-us) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/10A5355d Safari/7534.48.3'); // Pass it to our WebRequest so it thinks the request came from an iPhone $request->setDevice($device); $obj = new WebRouter($request); // Pass our route collection to our WebRouter object $obj->setRouteCollection($routes); // Match the request to the route $obj->match(); $controller = $obj->getController(); $this->assertEquals('MobileController', $controller->getClassName()); $this->assertEquals('iosAction', $controller->getMethodName()); // Test using an Android Tablet $device->setUserAgent('Mozilla/5.0 (Linux; U; Android 4.0.3; ja-jp; Sony Tablet P Build/TISU0085) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Safari/534.30'); $request->setDevice($device); $obj = new WebRouter($request); // Pass our route collection to our WebRouter object $obj->setRouteCollection($routes); // Match the request to the route $obj->match(); $controller = $obj->getController(); $this->assertEquals('GenericController', $controller->getClassName()); $this->assertEquals('androidAction', $controller->getMethodName()); // Test fall through $device->setUserAgent('ChrisNoden/Synergy (Not a real browser;OK)'); $request->setDevice($device); $obj = new WebRouter($request); // Pass our route collection to our WebRouter object $obj->setRouteCollection($routes); // Match the request to the route $obj->match(); $controller = $obj->getController(); $this->assertEquals('Synergy\\Controller\\DefaultController', $controller->getClassName()); $this->assertEquals('defaultAction', $controller->getMethodName()); }
function detect_browser($user_agent = NULL) { $ua_custom = !is_null($user_agent); // Used custom user agent? if (!$ua_custom && isset($GLOBALS['cache']['detect_browser'])) { //if (isset($_COOKIE['observium_screen_ratio']) && !isset($GLOBALS['cache']['detect_browser']['screen_resolution'])) //{ // r($_COOKIE); //} // Return cached info return $GLOBALS['cache']['detect_browser']; } $detect = new Mobile_Detect(); if ($ua_custom) { // Set custom User-Agent $detect->setUserAgent($user_agent); } else { $user_agent = $_SERVER['HTTP_USER_AGENT']; } // Default type and icon $type = 'generic'; $icon = 'icon-laptop'; if ($detect->isMobile()) { // Any phone device (exclude tablets). $type = 'mobile'; $icon = 'glyphicon glyphicon-phone'; if ($detect->isTablet()) { // Any tablet device. $type = 'tablet'; $icon = 'icon-tablet'; } } // Load additional function if (!function_exists('parse_user_agent')) { include_once $GLOBALS['config']['install_dir'] . '/libs/UserAgentParser.php'; } // Detect Browser name, version and platform $ua_info = parse_user_agent($user_agent); $detect_browser = array('user_agent' => $user_agent, 'type' => $type, 'icon' => $icon, 'browser_full' => $ua_info['browser'] . ' ' . preg_replace('/^([^\\.]+(?:\\.[^\\.]+)?).*$/', '\\1', $ua_info['version']), 'browser' => $ua_info['browser'], 'version' => $ua_info['version'], 'platform' => $ua_info['platform']); // For custom UA, do not cache and return only base User-Agent info if ($ua_custom) { return $detect_browser; } // Load screen and DPI detector. This set cookies with: // $_COOKIE['observium_screen_ratio'] - if ratio >= 2, than HiDPI screen is used // $_COOKIE['observium_screen_resolution'] - screen resolution 'width x height', ie: 1920x1080 // $_COOKIE['observium_screen_size'] - current window size (less than resolution) 'width x height', ie: 1097x456 register_html_resource('js', 'observium-screen.js'); // Additional browser info (screen_ratio, screen_size, svg) if ($ua_info['browser'] == 'Firefox' && version_compare($ua_info['version'], '47.0') < 0) { // Do not use srcset in FF, while issue open: // https://bugzilla.mozilla.org/show_bug.cgi?id=1149357 // Update, seems as in 47.0 partially fixed $zoom = 1; } else { if (isset($_COOKIE['observium_screen_ratio'])) { // Note, Opera uses ratio 1.5 $zoom = round($_COOKIE['observium_screen_ratio']); // Use int zoom } else { // If JS not supported or cookie not set, use default zoom 2 (for allow srcset) $zoom = 2; } } $detect_browser['screen_ratio'] = $zoom; //$detect_browser['svg'] = ($ua_info['browser'] == 'Firefox'); // SVG supported or allowed if (isset($_COOKIE['observium_screen_resolution'])) { $detect_browser['screen_resolution'] = $_COOKIE['observium_screen_resolution']; //$detect_browser['screen_size'] = $_COOKIE['observium_screen_size']; } $GLOBALS['cache']['detect_browser'] = $detect_browser; // Store to cache //r($GLOBALS['cache']['detect_browser']); return $GLOBALS['cache']['detect_browser']; }
/** * @covers Mobile_Detect::setUserAgent, Mobile_Detect::getUserAgent */ public function testSetUserAgent() { $md = new Mobile_Detect(array()); $md->setUserAgent('hello world'); $this->assertSame('hello world', $md->getUserAgent()); }
/** * @medium * @dataProvider userAgentData */ public function testUserAgents($userAgent, $isMobile, $isTablet, $version, $model, $vendor) { //make sure we're passed valid data if (!is_string($userAgent) || !is_bool($isMobile) || !is_bool($isTablet)) { $this->markTestIncomplete("The User-Agent {$userAgent} does not have sufficient information for testing."); return; } //setup $md = new Mobile_Detect(); $md->setUserAgent($userAgent); //is mobile? $this->assertEquals($md->isMobile(), $isMobile); //is tablet? $this->assertEquals($md->isTablet(), $isTablet, 'FAILED: ' . $userAgent . ' isTablet: ' . $isTablet); if (isset($version)) { foreach ($version as $condition => $assertion) { $this->assertEquals($assertion, $md->version($condition), 'FAILED UA (version("' . $condition . '")): ' . $userAgent); } } //version property tests if (isset($version)) { foreach ($version as $property => $stringVersion) { $v = $md->version($property); $this->assertSame($stringVersion, $v); } } //@todo: model test, not sure how exactly yet //@todo: vendor test. The below is theoretical, but fails 50% of the tests... /*if (isset($vendor)) { $method = "is$vendor"; $this->assertTrue($md->{$method}(), "Expected Mobile_Detect::{$method}() to be true."); }*/ }
function detect_browser($user_agent = NULL, $return_array = FALSE) { global $cache; if (is_null($user_agent) && $cache['detect_browser']) { return $cache['detect_browser']; } include_once $GLOBALS['config']['html_dir'] . '/includes/Mobile_Detect.php'; $detect = new Mobile_Detect(); if (!is_null($user_agent)) { // Set custom User-Agent $detect->setUserAgent($user_agent); } else { $user_agent = $_SERVER['HTTP_USER_AGENT']; } $type = 'generic'; if ($detect->isMobile()) { // Any phone device (exclude tablets). $type = 'mobile'; if ($detect->isTablet()) { // Any tablet device. $type = 'tablet'; } } if ($return_array) { // Load additional function if (!function_exists('parse_user_agent')) { include_once $GLOBALS['config']['html_dir'] . '/includes/UserAgentParser.php'; } // Detect Browser name, version and platform $ua_info = parse_user_agent($user_agent); $cache['detect_browser'] = array('user_agent' => $user_agent, 'type' => $type, 'browser' => $ua_info['browser'] . ' ' . $ua_info['version'], 'platform' => $ua_info['platform']); } else { $cache['detect_browser'] = $type; } return $cache['detect_browser']; }
<?php require_once '../Mobile_Detect.php'; /* $detect = new Mobile_Detect; $detect->setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) CriOS/38.0.2125.59 Mobile/12A405 Safari/600.1.4'); var_dump($detect->version('Chrome')); var_dump($detect->version('iPhone')); */ /* $user_agents = array( 'android' => 'Mozilla/5.0 (Linux; Android 4.2; Nexus 7 Build/JOP40C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19', 'iphone6' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25', 'blackberry' => 'Mozilla/5.0 (BB10; Touch) AppleWebKit/537.10+ (KHTML, like Gecko) Version/10.0.9.2372 Mobile Safari/537.10+' ); $mobile_detect = new Mobile_Detect; foreach($user_agents as $user_agent) { $mobile_detect->setUserAgent($user_agent); var_dump($mobile_detect->isAndroidOS()); } */ $detect = new Mobile_Detect(); //$detect->setUserAgent('Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko'); //var_dump($detect->version('IE')); $detect->setUserAgent('Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko'); var_dump($detect->version('IE'));
function showOutputStatisticsPlatform() { if (loadvar(DATA_OUTPUT_TYPEDATA) != "") { if (loadvar(DATA_OUTPUT_TYPEDATA) == DATA_TEST) { $_SESSION[SURVEY_EXECUTION_MODE] = SURVEY_EXECUTION_MODE_TEST; } else { $_SESSION[SURVEY_EXECUTION_MODE] = SURVEY_EXECUTION_MODE_NORMAL; } } else { $_SESSION[SURVEY_EXECUTION_MODE] = SURVEY_EXECUTION_MODE_NORMAL; } $survey = new Survey($_SESSION['SUID']); $headers[] = array('link' => setSessionParamsHref(array('page' => 'sysadmin.output'), Language::headerOutput()), 'label' => Language::headerOutputData()); $headers[] = array('link' => setSessionParamsHref(array('page' => 'sysadmin.output.statistics'), Language::headerOutputStatistics()), 'label' => Language::headerOutputStatistics()); $headers[] = array('link' => '', 'label' => Language::headerOutputStatisticsPlatform()); $returnStr = $this->showOutputHeader($headers); $surveys = new Surveys(); $surveys = $surveys->getSurveys(); $returnStr .= '<form id=surveyform method="post">'; $returnStr .= '<span class="label label-default">' . Language::headerOutputStatisticsPlatform() . '</span>'; $returnStr .= '<div class="well well-sm">'; $returnStr .= '<table>'; //if (sizeof($surveys) > 0) { $returnStr .= $this->displayComboBox(); $returnStr .= '<tr><td>' . Language::labelOutputScreenDumpsSurvey() . '</td><td>' . $this->displaySurveys("survey", "survey", $_SESSION["SUID"]) . '</td></tr>'; $returnStr .= '<script type=text/javascript> $(document).ready(function(){ $("#survey").on("change", function(event) { document.getElementById("surveyform").submit(); }); }); </script>'; //} $returnStr .= '<tr><td>' . Language::labelOutputDataType() . '</td><td>'; $returnStr .= "<select id='typedata' class='selectpicker show-tick' name=" . DATA_OUTPUT_TYPEDATA . ">"; //$returnStr .= "<option></option>"; $selected = array('', ''); if (loadvar(DATA_OUTPUT_TYPEDATA) != "") { $selected[loadvar(DATA_OUTPUT_TYPEDATA)] = "selected"; } //print_r($selected); $returnStr .= "<option " . $selected[0] . " value=" . DATA_REAL . ">" . Language::optionsDataReal() . "</option>"; $returnStr .= "<option " . $selected[1] . " value=" . DATA_TEST . ">" . Language::optionsDataTest() . "</option>"; $returnStr .= "</select>"; $returnStr .= "</td></tr>"; $returnStr .= '<script type=text/javascript> $(document).ready(function(){ $("#typedata").on("change", function(event) { document.getElementById("surveyform").submit(); }); }); </script>'; $returnStr .= '</table><br/>'; $returnStr .= "</form>"; // high chart $returnStr .= '<script src="js/highcharts.js"></script>'; $returnStr .= '<script src="js/modules/exporting.js"></script>'; $returnStr .= '<script src="js/export-csv.js"></script>'; $data = new Data(); $data = $data->getPlatformData($_SESSION['SUID']); // determine devices $devices = array(); require_once "detection_bootstrap.php"; $detect = new Mobile_Detect(); $total = sizeof($data); if ($total == 0) { $total = 1; } $mobilecount = 0; $tabletcount = 0; $othercount = 0; $browsercounts = array(); $oscounts = array(); foreach ($data as $d) { $detect->setUserAgent($d); if ($detect->isMobile() && !$detect->isTablet()) { $mobilecount++; } else { if ($detect->isTablet()) { $tabletcount++; } else { $othercount++; } } $browser = new Browser($d); $name = $browser->getName(); if ($name == 'Navigator') { // rename if android mobile browser $name = "Android browser"; } if (isset($browsercounts[ucwords($name)])) { $browsercounts[ucwords($name)]++; } else { $browsercounts[ucwords($name)] = 1; } $os = new Os($d); $name = $os->getName(); if (isset($oscounts[ucwords($name)])) { $oscounts[ucwords($name)]++; } else { $oscounts[ucwords($name)] = 1; } } // high chart for device pie chart $returnStr .= '<div id="chart1" style="min-width: 310px; height: 400px; margin: 0 auto"></div>'; $returnStr .= $this->getDeviceData($survey->getName(), '{name: "Mobile", y: ' . round($mobilecount / $total, 2) * 100 . '}, {name: "Tablet", y: ' . round($tabletcount / $total, 2) * 100 . '}, {name: "Laptop/desktop/other", y: ' . round($othercount / $total, 2) * 100 . '}'); // high chart for browsers $returnStr .= '<br/><br/><div id="chart2" style="min-width: 310px; height: 400px; margin: 0 auto"></div>'; $browsers = ''; //print_r($browsercounts); ksort($browsercounts); foreach ($browsercounts as $b => $count) { if ($browsers != '') { $browsers .= ","; } $browsers .= '{name: "' . $b . '", y: ' . round($count / $total, 2) * 100 . '}'; } $returnStr .= $this->getBrowserData($survey->getName(), $browsers); // high chart for operating system $returnStr .= '<br/><br/><div id="chart3" style="min-width: 310px; height: 400px; margin: 0 auto"></div>'; $os = ''; //print_r($oscounts); ksort($oscounts); foreach ($oscounts as $b => $count) { if ($os != '') { $os .= ","; } $os .= '{name: "' . $b . '", y: ' . round($count / $total, 2) * 100 . '}'; } $returnStr .= $this->getOSData($survey->getName(), $os); // $returnStr .= '</div> </div>'; //container and wrap $returnStr .= $this->showFooter(false); return $returnStr; }
/** * @param $userAgent * * @return \Application\Entity\ParticipantEntity */ public function setUserAgent($userAgent) { $this->userAgent = $userAgent; $parser = \UAParser\Parser::create(); $detect = new \Mobile_Detect(); $userAgentInfo = $parser->parse($userAgent); $userAgentMobileInfo = $detect->setUserAgent($userAgent); $this->userAgentUa = $userAgentInfo->ua->family; $this->userAgentOs = $userAgentInfo->os->family; $this->userAgentDevice = $userAgentInfo->device->family; if ($detect->isMobile()) { $this->userAgentDeviceType = 'Mobile'; } elseif ($detect->isTablet()) { $this->userAgentDeviceType = 'Tablet'; } return $this; }
function get_device_info($db, $detect, $ua_string = '') { global $memcacheWorking, $memcache; $detect = new Mobile_Detect(); if ($ua_string != '') { $ua = $detect->setUserAgent($ua_string); } else { $ua = $detect->getUserAgent(); } //If Cache working if ($memcacheWorking) { $device_info = $memcache->get(md5("user-agent" . $ua . systemHash())); if (!$device_info) { $parse_info = PLATFORMS::parseUserAgentInfo($db, $detect); $memcache->set(md5("user-agent" . $ua . systemHash()), $parse_info, false); return $parse_info; } else { return $device_info; } } else { return PLATFORMS::parseUserAgentInfo($db, $detect); } }
} } /*****************************************************Left side*************************/ $left_num_tablet = 0; $left_num_tablet_iOS = 0; $left_num_tablet_android = 0; $left_num_tablet_windows = 0; $left_num_mobile = 0; $left_num_mobile_iOS = 0; $left_num_mobile_android = 0; $left_num_mobile_windows = 0; $left_num_desktop = 0; foreach ($rs_left as $userAgent_left) { $rs_useragent_left = $userAgent_left[0]; //user agent $detect->setUserAgent($rs_useragent_left); if ($detect->isMobile()) { $left_num_mobile = $left_num_mobile + 1; $mobile_left_date = getDeviceDate($userAgent_left[1]); //get device date // $mobile_left_date = $userAgent_left[1]; //get device date //datetime:result left side for process mobile bottom line chart. $rs_left_mobile[$mobile_left_date] = $rs_left_mobile[$mobile_left_date] + 1; if (iOS($rs_useragent_left)) { $left_num_mobile_iOS = $left_num_mobile_iOS + 1; } if (andoidOS($rs_useragent_left)) { $left_num_mobile_android = $left_num_mobile_android + 1; } if (windowOS($rs_useragent_left)) { $left_num_mobile_windows = $left_num_mobile_windows + 1;
function get_device_name($userAgent) { $detect = new Mobile_Detect(); $detect->setUserAgent($userAgent); $deviceType = $detect->isMobile() ? $detect->isTablet() ? 'tablet' : 'cell' : 'computer'; if ($deviceType == 'computer') { return $deviceType; } $name = 'unknow'; do { if ($detect->isiPhone()) { $name = 'iPhone'; break; } if ($detect->isBlackBerry()) { $name = 'BlackBerry'; break; } if ($detect->isHTC()) { $name = 'HTC'; break; } if ($detect->isNexus()) { $name = 'Nexus'; break; } if ($detect->isDell()) { $name = 'Dell'; break; } if ($detect->isMotorola()) { $name = 'Motorola'; break; } if ($detect->isSamsung()) { $name = 'Samsung'; break; } if ($detect->isLG()) { $name = 'LG'; break; } if ($detect->isSony()) { $name = 'Sony'; break; } if ($detect->isAsus()) { $name = 'Asus'; break; } if ($detect->isPalm()) { $name = 'Palm'; break; } if ($detect->isVertu()) { $name = 'Vertu'; break; } if ($detect->isPantech()) { $name = 'Pantech'; break; } if ($detect->isFly()) { $name = 'Fly'; break; } if ($detect->isSimValley()) { $name = 'SimValley'; break; } if ($detect->isGenericPhone()) { $name = 'Generic'; break; } if ($detect->isiPad()) { $name = 'iPad'; break; } if ($detect->isNexusTablet()) { $name = 'Nexus'; break; } if ($detect->isSamsungTablet()) { $name = 'Samsung'; break; } if ($detect->isKindle()) { $name = 'Kindle'; break; } if ($detect->isSurfaceTablet()) { $name = 'Surface'; break; } if ($detect->isAsusTablet()) { $name = 'Asus'; break; } if ($detect->isBlackBerryTablet()) { $name = 'BlackBerry'; break; } if ($detect->isHTCtablet()) { $name = 'HTC'; break; } if ($detect->isMotorolaTablet()) { $name = 'Motorola'; break; } if ($detect->isNookTablet()) { $name = 'Nook'; break; } if ($detect->isAcerTablet()) { $name = 'Acer'; break; } if ($detect->isToshibaTablet()) { $name = 'Toshiba'; break; } if ($detect->isLGTablet()) { $name = 'LG'; break; } if ($detect->isYarvikTablet()) { $name = 'Yarvik'; break; } if ($detect->isMedionTablet()) { $name = 'Medion'; break; } if ($detect->isArnovaTablet()) { $name = 'Arnova'; break; } if ($detect->isArchosTablet()) { $name = 'Archos'; break; } if ($detect->isAinolTablet()) { $name = 'Ainol'; break; } if ($detect->isSonyTablet()) { $name = 'Sony'; break; } if ($detect->isCubeTablet()) { $name = 'Cube'; break; } if ($detect->isCobyTablet()) { $name = 'Coby'; break; } if ($detect->isSMiTTablet()) { $name = 'SMiT'; break; } if ($detect->isRockChipTablet()) { $name = 'RockChip'; break; } if ($detect->isTelstraTablet()) { $name = 'Telstra'; break; } if ($detect->isFlyTablet()) { $name = 'Fly'; break; } if ($detect->isbqTablet()) { $name = 'bq'; break; } if ($detect->isHuaweiTablet()) { $name = 'Huawei'; break; } if ($detect->isNecTablet()) { $name = 'Nec'; break; } if ($detect->isPantechTablet()) { $name = 'Pantech'; break; } if ($detect->isBronchoTablet()) { $name = 'Broncho'; break; } if ($detect->isVersusTablet()) { $name = 'Versus'; break; } if ($detect->isZyncTablet()) { $name = 'Zync'; break; } if ($detect->isPositivoTablet()) { $name = 'Positivo'; break; } if ($detect->isNabiTablet()) { $name = 'Nabi'; break; } if ($detect->isPlaystationTablet()) { $name = 'Playstation'; break; } if ($detect->isGenericTablet()) { $name = 'Generic'; break; } if ($detect->isAndroidOS()) { $name = 'Android'; break; } if ($detect->isBlackBerryOS()) { $name = 'BlackBerry'; break; } if ($detect->isPalmOS()) { $name = 'Plam'; break; } if ($detect->isSymbianOS()) { $name = 'Symbian'; break; } if ($detect->isWindowsMobileOS()) { $name = 'WindowsMobile'; break; } if ($detect->isWindowsPhoneOS()) { $name = 'WindowsPhone'; break; } if ($detect->isiOS()) { $name = 'iOS'; break; } if ($detect->isMeeGoOS()) { $name = 'MeeGo'; break; } if ($detect->isMaemoOS()) { $name = 'Maemo'; break; } if ($detect->isJavaOS()) { $name = 'JavaOS'; break; } if ($detect->iswebOS()) { $name = 'WebOS'; break; } if ($detect->isbadaOS()) { $name = 'badaOS'; break; } if ($detect->isBREWOS()) { $name = 'BREWOS'; break; } } while (false); return $name . ' ' . $deviceType; }
/** * Main method - get all the source data about given user agent and parse it to create an object * with as much good and acurrate information as possible. * * @param string $user_agent user agent string * * @return UserAgentInfo */ protected static function parse($user_agent) { // 1. >>> retrieve the data from sources $bc = self::$browscap_wrapper->getInfo($user_agent); self::$mobile_detect->setUserAgent($user_agent); $md = self::$mobile_detect; $uap = self::$uaparser->parse($user_agent); // 2. >>> parse the data // Mobile_Detect class is ONLY used for mobile devices, the data it returns for other devices is of much worse quality than the two other classes $md_is_mobile = $md->isMobile(); $md_browser = $md_is_mobile ? self::parseBrowserMd($md) : false; $is_mobile = $md_is_mobile || $bc['isMobileDevice']; if (false === strpos($user_agent, 'WSCommand')) { // uaparser returns good quality results for browser detection because it's based on generic substrings $browser = self::parseBrowserUap($uap); // for mobile browsers we alternatively try Mobile_Detect if (!$browser) { $browser = $md_browser; } // if nothing worked we try browscap, this is very useful for legacy and exotic browsers if (!$browser) { $browser = self::parseBrowserBc($bc); } } else { // this is one very specific case - incorrect identification of WSCommand bot $browser = self::parseBrowserBc($bc); } // returning 'Android' for both browser and os is a bad idea if ('Android' == @$browser['name']) { $browser['name'] = 'Android Browser'; } list($device['family'], $device['version']) = self::parseDevice($md, $uap); // uaparser is usually good here too, for the same reasons as with browsers $os = self::parseOsUap($uap); // sometimes uaparser tries to guess just the OS name and fails badly ... if (!isset($os['major']) && $md_is_mobile) { $os = self::parseOsMd($md); } if (!$os) { // if nothing was found revert to browscap, good with really old user agents $os = self::parseOsBc($bc); } elseif (self::UAPARSER_GENERIC_WINDOWS == $os['name']) { // if the OS name is 'Windows' (without version) then we try to override that data using browscap, // because if it returns anything it's always very specific $tmp_os = self::parseOsBc($bc); if ($tmp_os) { $os = $tmp_os; } } $architecture = self::parseArchitecture($user_agent); // trying to mark as many bots as possible $is_bot = $bc['Crawler'] || self::UAPARSER_BOT_NAME == $uap->device->family || $md->is(self::MOBILE_DETECT_BOT_NAME) || $md->is(self::MOBILE_DETECT_MOBILE_BOT_NAME); // that's an interesting feature, so why not include it $mobile_grade = $md_is_mobile && !empty($md_browser['name']) ? $md->mobileGrade() : ''; // 3. >>> set the user agent identification level if ($is_mobile && !$is_bot && !empty($browser['name']) && !empty($os['name']) && (!empty($device['family']) || !empty($device['version']))) { // this is a mobile user (not a mobile bot) and was nicely and specifically identified by Mobile_Detect or uaparser $id_level = self::ID_LEVEL_FULL; } elseif (BrowscapWrapper::DEFAULT_NAME != $bc['Browser']) { // it's not a mobile user but it was identified by browscap, that's enough to provide full information $id_level = self::ID_LEVEL_FULL; } elseif (!empty($browser['name']) || $is_mobile || $is_bot) { // browser name is the basic data that we need and that all the parsers should provide; // if browser name is not given then mobile and bot checks are also very important $id_level = self::ID_LEVEL_PARTIAL; if (empty($browser['name'])) { // we don't want to leave the browser name empty $browser['name'] = trim('generic ' . ($is_mobile ? 'mobile ' : '') . ($is_bot ? 'bot ' : '')); } } else { // not identfied at all (some data may be present, but nothing useful) $id_level = self::ID_LEVEL_NONE; } // $browser['name'] is always filled if $id_level != self::ID_LEVEL_NONE $ua_info = new UserAgentInfo($user_agent, $id_level, self::$data_version, array('browser' => (string) @$browser['name'], 'browser_major' => (string) @$browser['major'], 'browser_minor' => (string) @$browser['minor'], 'browser_patch' => (string) @$browser['patch'], 'device_family' => $device['family'], 'device_version' => $device['version'], 'os' => (string) @$os['name'], 'os_major' => (string) @$os['major'], 'os_minor' => (string) @$os['minor'], 'os_patch' => (string) @$os['patch'], 'is_banned' => (bool) $bc['isBanned'], 'is_mobile' => $is_mobile, 'is_mobile_tablet' => $is_mobile && $md->isTablet(), 'is_bot' => $is_bot, 'is_bot_reader' => $is_bot && $bc['isSyndicationReader'], 'is_64_bit_os' => 64 == @$architecture['os'], 'is_64_bit_browser' => 64 == @$architecture['browser'], 'mobile_grade' => $mobile_grade)); return $ua_info; }
/** * Retrieve Mobile-detect service * * @param \Zend\Http\Headers $oHeaders * * @return \Mobile_Detect */ public function __invoke(\Zend\Http\Headers $oHeaders = null) { if ($oHeaders) { $this->mobileDetect->setHttpHeaders($oHeaders->toArray()); $this->mobileDetect->setUserAgent($oHeaders->get('user-agent')->getFieldValue()); } return $this->mobileDetect; }