/core/components/modmobile/model/detectmobile.class.php

https://github.com/jgulledge19/modMobile · PHP · 128 lines · 84 code · 14 blank · 30 comment · 15 complexity · 549bbb8180932b069b3e0eaecd9ca0f4 MD5 · raw file

  1. <?php
  2. /**
  3. * This is a class that simply tells if a browser is on a mobile device
  4. */
  5. class DetectMobile {
  6. /**
  7. * @var $get_var - this is the name of the value that you wish to have
  8. * retrieve weather or not the mode is mobile or full(PC). Default is
  9. * modxSiteTemplate, this is an example of what URL you would need to create
  10. * like mypage.html?modxSiteTemplate=mobile
  11. */
  12. protected $get_var = 'modxSiteTemplate';
  13. /**
  14. * @params $site_mode - set the default mode: mobile or full
  15. */
  16. function __construct($get_var='modxSiteTemplate'){
  17. $this->get_var = $get_var;
  18. //
  19. }
  20. /**
  21. * @description run detect user preferance if the user does not have a preferance
  22. * then detect if it is a mobile device
  23. * @return string - mobile or full
  24. */
  25. public function run(){
  26. $user_preferance = $this->getSitePreferance();
  27. if ( !empty($user_preferance)){
  28. return $user_preferance;
  29. }
  30. if ( $this->isMobile() ) {
  31. return 'mobile';
  32. }
  33. return 'full';
  34. }
  35. /**
  36. * @description determines if the browser is mobile and returns true if it is
  37. * and false if its mobile info cannot be found
  38. *
  39. * @returns boolean
  40. */
  41. public function isMobile() {
  42. $_SERVER['ALL_HTTP'] = isset($_SERVER['ALL_HTTP']) ? $_SERVER['ALL_HTTP'] : '';
  43. $mobile_browser = false; //default: not mobile
  44. $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  45. if ( preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|BlackBerry|Android)/i', $user_agent) ) {
  46. $mobile_browser = true;
  47. } elseif((isset($_SERVER['HTTP_ACCEPT'])) and
  48. (strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml') !== false)){
  49. $mobile_browser = true;
  50. } elseif(isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
  51. $mobile_browser = true;
  52. } elseif(isset($_SERVER['HTTP_PROFILE'])) {
  53. $mobile_browser = true;
  54. } elseif (strpos($user_agent, 'ipad') !== false) {
  55. $mobile_browser = true;
  56. } elseif (strpos($user_agent,' ppc;')>0) {
  57. $mobile_browser = true;
  58. } elseif (strpos($user_agent,'iemobile')>0) {
  59. $mobile_browser = true;
  60. } else {
  61. $mobile_ua = substr($user_agent,0,4);
  62. $mobile_agents = array(
  63. 'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
  64. 'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
  65. 'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
  66. 'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
  67. 'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
  68. 'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
  69. 'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
  70. 'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
  71. 'wapr','webc','winw','winw','xda','xda-'
  72. );
  73. if(in_array($mobile_ua, $mobile_agents)) {
  74. $mobile_browser = true;
  75. }
  76. if (strpos(strtolower($_SERVER['ALL_HTTP']), 'operamini') !== false) {
  77. $mobile_browser = true;
  78. }
  79. }
  80. if (strpos($user_agent,'windows ce')>0) {
  81. $mobile_browser = true;
  82. } elseif (strpos($user_agent,'windows')>0) {
  83. $mobile_browser = false;
  84. }
  85. // safari Desktop browser
  86. if ( strpos($user_agent, 'safari') > 0 && strpos($user_agent, 'mobile') === 0 ) {
  87. $mobile_browser = false;
  88. }
  89. if ($mobile_browser) {
  90. setcookie($this->get_var,'mobile', time()+60*60*24*30 , '/');
  91. return true;
  92. }
  93. return false;
  94. }
  95. /**
  96. * @description gets a $_GET or cookie value if it is set to allow user to set their preferance
  97. *
  98. * @return string - mobile/full or NULL if it is not set
  99. */
  100. public function getSitePreferance(){
  101. if ( isset($_GET[$this->get_var]) && !empty($_GET[$this->get_var]) ) {
  102. if ( $_GET[$this->get_var] == 'mobile' ) {
  103. setcookie($this->get_var,'mobile', time()+60*60*24*30 , '/');
  104. return 'mobile';
  105. } else {
  106. setcookie($this->get_var,'full', time()+60*60*24*30 , '/');
  107. return 'full';
  108. }
  109. } elseif( isset($_COOKIE[$this->get_var]) ) {
  110. return $_COOKIE[$this->get_var];
  111. }
  112. return NULL;
  113. }
  114. }