/** * Detect what device the current session was requested from. * * @param $simulate Name of device to simulate (self::DEVICE_*) * @return MobileDeviceDetection */ public static function detect(MobileDevice $device = null) { $result = new MobileDetectorResults(); $data = $_SERVER; // Simulate a particular device? if ($device) { $data = $device->simulate($data); } // Something to do with detecting WAP support: if (isset($data['HTTP_X_WAP_PROFILE']) || preg_match('%wap\\.|\\.wap%i', $data['HTTP_ACCEPT'])) { $result->pass(); } // Make sure no negative matches apply, not a mobile: foreach (self::$negatives as $match) { if (!preg_match($match, $data['HTTP_USER_AGENT'])) { continue; } $result->fail(); return $result; } // Check for generic mobile device, not a mobile: foreach (self::$positives as $match) { if (!preg_match($match, $data['HTTP_USER_AGENT'])) { continue; } $result->pass(); break; } // Check for matches, is a mobile: foreach (self::$devices as $device) { $device->detect($data, $result); } // No decision yet, not a mobile: if (!$result->done()) { $result->fail(); } return $result; }
<?php class DeviceWebOS extends MobileDevice { // WebOS Tablet / HP TouchPad / WebOS v3.0 const webOS3_Tablet = 'Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; de-DE) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0'; // WebOS Phone / HP Pre 3 / WebOS v2.0 const webOS2_Phone = 'Mozilla/5.0 (Linux; webOS/2.1.2; U; xx-xx) AppleWebKit/534.6 (KHTML, like Gecko) webOSBrowser/221.11 Safari/534.6 Pre/3.0'; // WebOS Phone / Palm Pre / WebOS v1.0 const webOS1_Phone = 'Mozilla/5.0 (webOS/1.0; U; en-US) AppleWebKit/525.27.1 (KHTML, like Gecko) Version/1.0 Safari/525.27.1 Pre/1.0'; protected static $instance; public function initialize() { $this->{'name'} = 'WebOS'; $this->{'user-agent'} = self::webOS1_Phone; // Positive matching: $this->allow('%webos|hpwOS%i'); // Device: $this->setIf('hardware-type', '%.%', 'mobile'); $this->setIf('hardware-type', '%hp-tablet%', 'tablet'); // Operating system: $this->setIf('system-version', '%(webOS|hpwOS)/([0-9]+(\\.[0-9]+)+);%', '\\2'); } } MobileDevice::instance('DeviceWebOS');
class DeviceBlackBerry extends MobileDevice { // BlackBerry Tablet / Model PlayBook / OS v2.0 const BlackBerryPlayBook = 'Mozilla/5.0 (PlayBook; U; RIM Tablet OS 2.0.0; en-US) AppleWebKit/535.1+ (KHTML, like Gecko) Version/7.2.0.0 Safari/535.1+'; // BlackBerry Phone / Model 9900 / OS v7.1 const BlackBerry9900 = 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.1.0.346 Mobile Safari/534.11+'; // BlackBerry Phone / Model 9800 / OS v6.0 const BlackBerry9800 = 'Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+'; // BlackBerry Phone / Model 9700 / OS v5.0 const BlackBerry9700 = 'BlackBerry9700/5.0.0.593 Profile/MIDP-2.1 Configuration/CLDC-1.1 VendorID/1'; // BlackBerry Phone / Model 9000 / OS v4.6 const BlackBerry9000 = 'BlackBerry9000/4.6.0.303 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/1'; protected static $instance; public function initialize() { $this->{'name'} = 'BlackBerry'; $this->{'user-agent'} = self::BlackBerry9000; // Positive matching: $this->allow('%blackberry%i', '%playbook%i'); // Hardware: $this->setIfNot('device-type', '%PlayBook%', 'mobile'); $this->setIf('device-type', '%PlayBook%', 'tablet'); $this->setIf('device-model', '%PlayBook%', '\\0'); // Operating system: $this->setIf('system-version', '%(BlackBerry[ ]?[0-9]{4}|\\bVersion)/([0-9]+(\\.[0-9]+)+)%', '\\2'); $this->setIf('system-version', '%RIM Tablet OS ([0-9]+(\\.[0-9]+)+)%', '\\1'); } } MobileDevice::instance('DeviceBlackBerry');
<?php class DevicePalm extends MobileDevice { protected static $instance; public function initialize() { $this->{'name'} = 'Palm'; // Positive matching: $this->allow('%palmsource%i', '%palmos%i'); $this->setIfNot('hardware-type', '%.%', 'mobile'); } } MobileDevice::instance('DevicePalm');
// iOS / Apple iPad / OS v6.0 / Safari const TABLET_OSv6_SAFARI = 'Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25'; // iOS / Apple iPad / OS v5.1 / Chrome v19 const TABLET_OSv5_CHROMEv19 = 'Mozilla/5.0 (iPad; U; CPU OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3'; // iOS / Applie iPad / OS v3.2 / Safari const TABLET_OSv3_SAFARI = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7W367a Safari/531.21.10'; // iOS / Apple iPhone / OS v6.0 / Safari const PHONE_OSv6_SAFARI = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25'; // iOS / Apple iPhone / OS v5.1 / Chrome v19 const PHONE_OSv5_CHROMEv19 = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_1_1 like Mac OS X; en) AppleWebKit/534.46.0 (KHTML, like Gecko) CriOS/19.0.1084.60 Mobile/9B206 Safari/7534.48.3'; // iOS / Applie iPhone / OS v3.2 / Safari const PHONE_OSv3_SAFARI = 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7W367a Safari/531.21.10'; protected static $instance; public function initialize() { $this->{'name'} = 'iOS'; $this->{'user-agent'} = self::PHONE_OSv3_SAFARI; // Positive matching: $this->allow('%ipad|iphone%i'); // Chrome: $this->setIf('browser-name', '%CriOS/([0-9]+(\\.[0-9]+)+)%', 'chrome'); $this->setIf('browser-version', '%CriOS/([0-9]+(\\.[0-9]+)+)%', '\\1'); // Device: $this->setIf('hardware-type', '%.%', 'mobile'); $this->setIf('hardware-type', '%iPad%', 'tablet'); // Operating system: $this->setIf('system-version', '%(CPU OS|CPU iPhone OS) ([0-9])_([0-9])%', '\\2.\\3'); } } MobileDevice::instance('DeviceIOS');
<?php class DeviceAndroid extends MobileDevice { // Android Phone / Samsung Galaxy Nexus / OS v4.0 / Chrome v18 const PHONE_OSv4_CHROMEv18 = 'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19'; // Android Phone / HTC Sensation / OS v4.0 / Android Browser const PHONE_OSv4_NATIVE = 'Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30'; // Android Phone / HTC Desire Z / OS v2.2 / Android Browser const PHONE_OSv2_NATIVE = 'Mozilla/5.0 (Linux; U; Android 2.2.1; en-gb; HTC_DesireZ_A7272 Build/FRG83D) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1'; protected static $instance; public function initialize() { $this->{'name'} = 'Android'; $this->{'user-agent'} = self::PHONE_OSv2_NATIVE; // Positive matching: $this->allow('%android%i'); // Chrome: $this->setIf('browser-name', '%Chrome/([0-9]+(\\.[0-9]+)+)%', 'chrome'); $this->setIf('browser-version', '%Chrome/([0-9]+(\\.[0-9]+)+)%', '\\1'); // Device: $this->setIf('hardware-type', '%Mobile%', 'mobile'); $this->setIfNot('hardware-type', '%Mobile%', 'tablet'); // Operating system: $this->setIf('system-version', '%Android ([0-9]+(\\.[0-9]+)+);%', '\\1'); } } MobileDevice::instance('DeviceAndroid');