agent.class.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. // 接口输出类
  3. class Agent
  4. {
  5. // 设备类型
  6. const DEVICE_MOBILE = 1;
  7. const DEVICE_DESKTOP = 2;
  8. const DEVICE_UNKNOWN = -1;
  9. // 浏览器类型
  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_XZAPP = 5;
  15. const BROWSER_TYPE_UNKNOWN = -1;
  16. // 系统类型
  17. const OS_TYPE_IOS = 1;
  18. const OS_TYPE_ANDROID = 2;
  19. const OS_TYPE_UNKNOWN = -1;
  20. // 是否RETINA屏
  21. const RETINA_TYPE_YES = 1;
  22. const RETINA_TYPE_NOT = 0;
  23. // 是否IOS6系统
  24. const IOS6_YES = 1;
  25. const IOS6_NOT = 0;
  26. // 是否微信打开
  27. const MICRO_MESSAGE_YES = 1;
  28. const MICRO_MESSAGE_NOT = 0;
  29. // APP已经安装
  30. const APP_INSTALLED_YES = 1;
  31. const APP_INSTALLED_NOT = 0;
  32. // 得到agent完整类型信息
  33. public static function getDeviceInfo()
  34. {
  35. return array(
  36. 'deviceType' => self::deviceType(),
  37. 'browserType' => self::browserType(),
  38. 'isRetina' => self::isRetina(),
  39. 'osType' => self::osType(),
  40. 'isIos6' => self::isIos6(),
  41. );
  42. }
  43. // 浏览器类型
  44. public static function browserType($agent = '')
  45. {
  46. $agent = self::getAgent($agent);
  47. if (stripos($agent, 'baiduboxapp') !== false) {
  48. return self::BROWSER_TYPE_XZAPP;
  49. }
  50. if (stripos($agent, 'iphone') !== false) {
  51. return self::BROWSER_TYPE_IPHONE;
  52. }
  53. if (stripos($agent, 'ipad') !== false) {
  54. return self::BROWSER_TYPE_IPAD;
  55. }
  56. if (stripos($agent, 'ipod') !== false) {
  57. return self::BROWSER_TYPE_IPOD;
  58. }
  59. if (stripos($agent, 'android') !== false) {
  60. return self::BROWSER_TYPE_ANDROID;
  61. }
  62. return self::BROWSER_TYPE_UNKNOWN;
  63. }
  64. // 系统类型
  65. public static function osType($agent = '')
  66. {
  67. $agent = self::getAgent($agent);
  68. $browserType = self::browserType($agent);
  69. switch ($browserType) {
  70. case self::BROWSER_TYPE_IPHONE:
  71. case self::BROWSER_TYPE_IPAD:
  72. case self::BROWSER_TYPE_IPOD:
  73. $osType = self::OS_TYPE_IOS;
  74. break;
  75. case self::BROWSER_TYPE_ANDROID:
  76. $osType = self::OS_TYPE_ANDROID;
  77. break;
  78. default:
  79. $osType = self::OS_TYPE_UNKNOWN;
  80. }
  81. return $osType;
  82. }
  83. // 设备类型
  84. public static function deviceType()
  85. {
  86. if (self::isMobile()) {
  87. return self::DEVICE_MOBILE;
  88. } else {
  89. return self::DEVICE_DESKTOP;
  90. }
  91. }
  92. // retina屏
  93. public static function isRetina($agent = '')
  94. {
  95. $agent = self::getAgent($agent);
  96. $osType = self::osType($agent);
  97. if (($osType == self::OS_TYPE_IOS) && (self::isIos6($agent) != 1)) {
  98. return self::RETINA_TYPE_YES;
  99. } else {
  100. return self::RETINA_TYPE_NOT;
  101. }
  102. }
  103. // ios6系统的手机(iphone4, iphone4s)
  104. public static function isIos6($agent = '')
  105. {
  106. $agent = self::getAgent($agent);
  107. if (stripos($agent, 'iPhone OS 6')) {
  108. return self::IOS6_YES;
  109. } else {
  110. return self::IOS6_NOT;
  111. }
  112. }
  113. // 检查是否在微信中打开
  114. public static function isMicroMessage($agent = '')
  115. {
  116. $agent = self::getAgent($agent);
  117. if (stripos($agent, 'MicroMessenger') !== false) {
  118. return self::MICRO_MESSAGE_YES;
  119. } else {
  120. return self::MICRO_MESSAGE_NOT;
  121. }
  122. }
  123. // 已安装APP
  124. public static function isAppInstalled()
  125. {
  126. if (isset($_GET['isappinstalled']) && ($_GET['isappinstalled'] == 1)) {
  127. return self::APP_INSTALLED_YES;
  128. } else {
  129. return self::APP_INSTALLED_NOT;
  130. }
  131. }
  132. // 是移动设备访问
  133. public static function isMobile()
  134. {
  135. // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
  136. if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
  137. return true;
  138. }
  139. // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
  140. if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {
  141. return true;
  142. }
  143. // 脑残法,判断手机发送的客户端标志,兼容性有待提高
  144. if (isset ($_SERVER['HTTP_USER_AGENT'])) {
  145. $clientkeywords = array('nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp',
  146. 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu',
  147. 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave',
  148. 'nexusone', 'cldc', 'midp', 'wap', 'mobile', 'WindowsWechat');
  149. // 从HTTP_USER_AGENT中查找手机浏览器的关键字
  150. if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
  151. return true;
  152. }
  153. }
  154. // 协议法,因为有可能不准确,放到最后判断
  155. if (isset($_SERVER['HTTP_ACCEPT'])) {
  156. // 如果只支持wml并且不支持html那一定是移动设备, 如果支持wml和html但是wml在html之前则是移动设备
  157. 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')))) {
  158. return true;
  159. }
  160. }
  161. return false;
  162. }
  163. public static function getAgent($agent = '')
  164. {
  165. $agent = empty($agent) ? $_SERVER['HTTP_USER_AGENT'] : $agent;
  166. return $agent;
  167. }
  168. }