Advertisement
reenadak

mobiDetect.php

Sep 25th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Detect mobile devices
  5.  *
  6.  * @category Sitebase (Wim Mostmans)
  7.  * @version 1.0
  8.  */
  9. class MobileDetect {
  10.    
  11.     // Device constants
  12.     const DEVICE_ANDROID    = "android";
  13.     const DEVICE_BLACKBERRY = "blackberry";
  14.     const DEVICE_IPHONE     = "iphone";
  15.     const DEVICE_OPERA      = "opera";
  16.     const DEVICE_PALM       = "palm";
  17.     const DEVICE_WINDOWS    = "windows";
  18.     const DEVICE_GENERIC    = "generic";
  19.     const DEVICE_IPAD       = "ipad";
  20.     const DEVICE_NORMAL     = "normal";
  21.    
  22.     /**
  23.      * Hold the device useragent
  24.      *
  25.      * @var string
  26.      */
  27.     private $_useragent;
  28.    
  29.     /**
  30.      * Boolean that is set to true if
  31.      * the current device is mobile
  32.      *
  33.      * @var bool
  34.      */
  35.     private $_isMobile      = false;
  36.    
  37.     /**
  38.      * Device booleans that get set when
  39.      * the devices matches
  40.      *
  41.      * @var bool
  42.      */
  43.     private $_isAndroid     = null;
  44.     private $_isBlackberry  = null;
  45.     private $_isIphone      = null;
  46.     private $_isOpera       = null;
  47.     private $_isPalm        = null;
  48.     private $_isWindows     = null;
  49.     private $_isGeneric     = null;
  50.     private $_isIpad        = null;
  51.    
  52.     /**
  53.      * Regular expressions for the different devices
  54.      *
  55.      * @var array
  56.      */
  57.     private $_devices       = array(
  58.         "android"       => "android",
  59.         "blackberry"    => "blackberry",
  60.         "iphone"        => "(iphone|ipod)",
  61.         "opera"         => "opera mini",
  62.         "palm"          => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
  63.         "windows"       => "(iemobile|ppc|smartphone|windows phone)",
  64.         "generic"       => "(kindle|mobile|mmp|midp|o2|pda|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap)",
  65.         "ipad"          => "ipad"
  66.     );
  67.    
  68.     /**
  69.      * Constructor
  70.      *
  71.      * @access public
  72.      * @return void
  73.      */
  74.     public function __construct() {
  75.         $this->_useragent = $_SERVER['HTTP_USER_AGENT'];
  76.         foreach ($this->_devices as $device => $regexp) {
  77.             if ($this->IsDevice($device) && $this->_isMobile == FALSE) {
  78.                 $this->_isMobile = true;
  79.             }
  80.         }
  81.     }
  82.    
  83.     /**
  84.      * Check if surfing with a particular device
  85.      *
  86.      * @access private
  87.      * @param string $device
  88.      * @return bool
  89.      */
  90.     private function IsDevice($device) {
  91.    
  92.         $var    = "Is" . ucfirst($device);
  93.         $return = @$this->$var === null ? (bool) preg_match("/" . $this->_devices[strtolower($device)] . "/i", $this->_useragent) : $this->$var;
  94.         if ($device != 'generic' && $return == true) {
  95.             $this->_isGeneric = false;
  96.         }
  97.  
  98.         return $return;
  99.     }
  100.    
  101.     /**
  102.      * Get the device type
  103.      *
  104.      * @param public
  105.      * @return string
  106.      */
  107.     public function GetDevice(){
  108.         foreach($this->_devices as $device_string => $regex){
  109.             if( $this->IsDevice($device_string) ){
  110.                 return $device_string;
  111.             }
  112.         }
  113.         return self::DEVICE_NORMAL;
  114.     }
  115.  
  116.     /**
  117.      * Call methods like this IsMobile() | IsAndroid() | IsIphone() | IsBlackberry() | IsOpera() | IsPalm() | IsWindows() | IsGeneric() | IsIpad() through IsDevice()
  118.      *
  119.      * @access public
  120.      * @param string $name
  121.      * @param array $arguments
  122.      * @return bool
  123.      */
  124.     public function __call($name, $arguments) {
  125.         $device = substr($name, 2);
  126.         if ($name == "Is" . ucfirst($device)) {
  127.             return $this->IsDevice($device);
  128.         } else {
  129.             trigger_error("Method $name is not defined", E_USER_ERROR);
  130.         }
  131.     }
  132.  
  133.  
  134.     /**
  135.      * Returns true if surfing on a mobile device
  136.      *
  137.      * @access public
  138.      * @return bool
  139.      */
  140.     public function IsMobile() {
  141.         return $this->_isMobile;
  142.     }
  143.  
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement