人人商城

agent.class.php 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */class Agent
  6. {
  7. const DEVICE_MOBILE = 1;
  8. const DEVICE_DESKTOP = 2;
  9. const DEVICE_UNKNOWN = -1;
  10. const BROWSER_TYPE_IPHONE = 1;
  11. const BROWSER_TYPE_IPAD = 2;
  12. const BROWSER_TYPE_IPOD = 3;
  13. const BROWSER_TYPE_ANDROID = 4;
  14. const BROWSER_TYPE_UNKNOWN = -1;
  15. const OS_TYPE_IOS = 1;
  16. const OS_TYPE_ANDROID = 2;
  17. const OS_TYPE_UNKNOWN = -1;
  18. const RETINA_TYPE_YES = 1;
  19. const RETINA_TYPE_NOT = 0;
  20. const IOS6_YES = 1;
  21. const IOS6_NOT = 0;
  22. const MICRO_MESSAGE_YES = 1;
  23. const MICRO_MESSAGE_NOT = 0;
  24. const APP_INSTALLED_YES = 1;
  25. const APP_INSTALLED_NOT = 0;
  26. public static function getDeviceInfo()
  27. {
  28. return array(
  29. 'deviceType' => self::deviceType(),
  30. 'browserType' => self::browserType(),
  31. 'isRetina' => self::isRetina(),
  32. 'osType' => self::osType(),
  33. 'isIos6' => self::isIos6(),
  34. );
  35. }
  36. public static function browserType($agent = '')
  37. {
  38. $agent = self::getAgent($agent);
  39. if (stripos($agent, 'iphone') !== false) {
  40. return self::BROWSER_TYPE_IPHONE;
  41. }
  42. if (stripos($agent, 'ipad') !== false) {
  43. return self::BROWSER_TYPE_IPAD;
  44. }
  45. if (stripos($agent, 'ipod') !== false) {
  46. return self::BROWSER_TYPE_IPOD;
  47. }
  48. if (stripos($agent, 'android') !== false) {
  49. return self::BROWSER_TYPE_ANDROID;
  50. }
  51. return self::BROWSER_TYPE_UNKNOWN;
  52. }
  53. public static function osType($agent = '')
  54. {
  55. $agent = self::getAgent($agent);
  56. $browserType = self::browserType($agent);
  57. switch ($browserType) {
  58. case self::BROWSER_TYPE_IPHONE:
  59. case self::BROWSER_TYPE_IPAD:
  60. case self::BROWSER_TYPE_IPOD:
  61. $osType = self::OS_TYPE_IOS;
  62. break;
  63. case self::BROWSER_TYPE_ANDROID:
  64. $osType = self::OS_TYPE_ANDROID;
  65. break;
  66. default:
  67. $osType = self::OS_TYPE_UNKNOWN;
  68. }
  69. return $osType;
  70. }
  71. public static function deviceType()
  72. {
  73. if (self::isMobile()) {
  74. return self::DEVICE_MOBILE;
  75. } else {
  76. return self::DEVICE_DESKTOP;
  77. }
  78. }
  79. public static function isRetina($agent = '')
  80. {
  81. $agent = self::getAgent($agent);
  82. $osType = self::osType($agent);
  83. if (($osType == self::OS_TYPE_IOS) && (self::isIos6($agent) != 1)) {
  84. return self::RETINA_TYPE_YES;
  85. } else {
  86. return self::RETINA_TYPE_NOT;
  87. }
  88. }
  89. public static function isIos6($agent = '')
  90. {
  91. $agent = self::getAgent($agent);
  92. if (stripos($agent, 'iPhone OS 6')) {
  93. return self::IOS6_YES;
  94. } else {
  95. return self::IOS6_NOT;
  96. }
  97. }
  98. public static function isMicroMessage($agent = '')
  99. {
  100. $agent = self::getAgent($agent);
  101. if (stripos($agent, 'MicroMessenger') !== false) {
  102. return self::MICRO_MESSAGE_YES;
  103. } else {
  104. return self::MICRO_MESSAGE_NOT;
  105. }
  106. }
  107. public static function isAppInstalled()
  108. {
  109. if (isset($_GET['isappinstalled']) && ($_GET['isappinstalled'] == 1)) {
  110. return self::APP_INSTALLED_YES;
  111. } else {
  112. return self::APP_INSTALLED_NOT;
  113. }
  114. }
  115. public static function isMobile()
  116. {
  117. if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
  118. return true;
  119. }
  120. if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
  121. return true;
  122. }
  123. if (isset ($_SERVER['HTTP_USER_AGENT'])) {
  124. $clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp',
  125. 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu',
  126. 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave',
  127. 'nexusone', 'cldc', 'midp', 'wap', 'mobile');
  128. if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
  129. return true;
  130. }
  131. }
  132. if (isset($_SERVER['HTTP_ACCEPT'])) {
  133. if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
  134. return true;
  135. }
  136. }
  137. return false;
  138. }
  139. public static function getAgent($agent = '')
  140. {
  141. $agent = empty($agent) ? $_SERVER['HTTP_USER_AGENT'] : $agent;
  142. return $agent;
  143. }
  144. }