PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress-dev/wp-content/plugins/jetpack/class.jetpack-user-agent.php

https://github.com/scottbale/kirkwoodcoc
PHP | 1475 lines | 931 code | 212 blank | 332 comment | 274 complexity | 09e87a637e0fefc9a524856fc6cf54ed MD5 | raw file
  1. <?php
  2. /**
  3. * Determine if the current User Agent matches the passed $kind
  4. *
  5. * @param string $kind Category of mobile device to check for.
  6. * Either: any, dumb, smart.
  7. * @param bool $return_matched_agent Boolean indicating if the UA should be returned
  8. *
  9. * @return bool|string Boolean indicating if current UA matches $kind. If
  10. * $return_matched_agent is true, returns the UA string
  11. */
  12. function jetpack_is_mobile( $kind = 'any', $return_matched_agent = false ) {
  13. static $kinds = array( 'smart' => false, 'dumb' => false, 'any' => false );
  14. static $first_run = true;
  15. static $matched_agent = '';
  16. // If an invalid kind is passed in, reset it to default.
  17. if ( ! isset( $kinds[ $kind ] ) ) {
  18. $kind = 'any';
  19. }
  20. if ( function_exists( 'apply_filters' ) ) {
  21. /**
  22. * Filter the value of jetpack_is_mobile before it is calculated.
  23. *
  24. * Passing a truthy value to the filter will short-circuit determining the
  25. * mobile type, returning the passed value instead.
  26. *
  27. * @since 4.2.0
  28. *
  29. * @param bool|string $matches Boolean if current UA matches $kind or not. If
  30. * $return_matched_agent is true, should return the UA string
  31. * @param string $kind Category of mobile device being checked
  32. * @param bool $return_matched_agent Boolean indicating if the UA should be returned
  33. */
  34. $pre = apply_filters( 'pre_jetpack_is_mobile', null, $kind, $return_matched_agent );
  35. if ( null !== $pre ) {
  36. return $pre;
  37. }
  38. }
  39. $ua_info = new Jetpack_User_Agent_Info();
  40. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) || strpos( strtolower( $_SERVER['HTTP_USER_AGENT'] ), 'ipad' ) ) {
  41. return false;
  42. }
  43. // Remove Samsung Galaxy tablets (SCH-I800) from being mobile devices
  44. if ( strpos( strtolower( $_SERVER['HTTP_USER_AGENT'] ) , 'sch-i800') ) {
  45. return false;
  46. }
  47. if( $ua_info->is_android_tablet() && $ua_info->is_kindle_touch() === false ) {
  48. return false;
  49. }
  50. if( $ua_info->is_blackberry_tablet() ) {
  51. return false;
  52. }
  53. if ( $first_run ) {
  54. $first_run = false;
  55. //checks for iPhoneTier devices & RichCSS devices
  56. if ( $ua_info->isTierIphone() || $ua_info->isTierRichCSS() ) {
  57. $kinds['smart'] = true;
  58. $matched_agent = $ua_info->matched_agent;
  59. }
  60. if ( ! $kinds['smart'] ) {
  61. // if smart, we are not dumb so no need to check
  62. $dumb_agents = $ua_info->dumb_agents;
  63. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  64. foreach ( $dumb_agents as $dumb_agent ) {
  65. if ( false !== strpos( $agent, $dumb_agent ) ) {
  66. $kinds['dumb'] = true;
  67. $matched_agent = $dumb_agent;
  68. break;
  69. }
  70. }
  71. if ( ! $kinds['dumb'] ) {
  72. if ( isset( $_SERVER['HTTP_X_WAP_PROFILE'] ) ) {
  73. $kinds['dumb'] = true;
  74. $matched_agent = 'http_x_wap_profile';
  75. } elseif ( isset( $_SERVER['HTTP_ACCEPT']) && ( preg_match( '/wap\.|\.wap/i', $_SERVER['HTTP_ACCEPT'] ) || false !== strpos( strtolower( $_SERVER['HTTP_ACCEPT'] ), 'application/vnd.wap.xhtml+xml' ) ) ) {
  76. $kinds['dumb'] = true;
  77. $matched_agent = 'vnd.wap.xhtml+xml';
  78. }
  79. }
  80. }
  81. if ( $kinds['dumb'] || $kinds['smart'] ) {
  82. $kinds['any'] = true;
  83. }
  84. }
  85. $value = $kinds[ $kind ];
  86. if ( $return_matched_agent ) {
  87. $value = $matched_agent;
  88. }
  89. if ( function_exists( 'apply_filters' ) ) {
  90. /**
  91. * Filter the value of jetpack_is_mobile
  92. *
  93. * @since 4.2.0
  94. *
  95. * @param bool|string $matches Boolean if current UA matches $kind or not. If
  96. * $return_matched_agent is true, should return the UA string
  97. * @param string $kind Category of mobile device being checked
  98. * @param bool $return_matched_agent Boolean indicating if the UA should be returned
  99. */
  100. $value = apply_filters( 'jetpack_is_mobile', $value, $kind, $return_matched_agent );
  101. }
  102. return $value;
  103. }
  104. class Jetpack_User_Agent_Info {
  105. public $useragent;
  106. public $matched_agent;
  107. public $isTierIphone; //Stores whether is the iPhone tier of devices.
  108. public $isTierRichCss; //Stores whether the device can probably support Rich CSS, but JavaScript (jQuery) support is not assumed.
  109. public $isTierGenericMobile; //Stores whether it is another mobile device, which cannot be assumed to support CSS or JS (eg, older BlackBerry, RAZR)
  110. private $_platform = null; //Stores the device platform name
  111. const PLATFORM_WINDOWS = 'windows';
  112. const PLATFORM_IPHONE = 'iphone';
  113. const PLATFORM_IPOD = 'ipod';
  114. const PLATFORM_IPAD = 'ipad';
  115. const PLATFORM_BLACKBERRY = 'blackberry';
  116. const PLATFORM_BLACKBERRY_10 = 'blackberry_10';
  117. const PLATFORM_SYMBIAN = 'symbian_series60';
  118. const PLATFORM_SYMBIAN_S40 = 'symbian_series40';
  119. const PLATFORM_J2ME_MIDP = 'j2me_midp';
  120. const PLATFORM_ANDROID = 'android';
  121. const PLATFORM_ANDROID_TABLET = 'android_tablet';
  122. const PLATFORM_FIREFOX_OS = 'firefoxOS';
  123. public $dumb_agents = array(
  124. 'nokia', 'blackberry', 'philips', 'samsung', 'sanyo', 'sony', 'panasonic', 'webos',
  125. 'ericsson', 'alcatel', 'palm',
  126. 'windows ce', 'opera mini', 'series60', 'series40',
  127. 'au-mic,', 'audiovox', 'avantgo', 'blazer',
  128. 'danger', 'docomo', 'epoc',
  129. 'ericy', 'i-mode', 'ipaq', 'midp-',
  130. 'mot-', 'netfront', 'nitro',
  131. 'palmsource', 'pocketpc', 'portalmmm',
  132. 'rover', 'sie-',
  133. 'symbian', 'cldc-', 'j2me',
  134. 'smartphone', 'up.browser', 'up.link',
  135. 'up.link', 'vodafone/', 'wap1.', 'wap2.', 'mobile', 'googlebot-mobile',
  136. );
  137. //The constructor. Initializes default variables.
  138. function __construct()
  139. {
  140. if ( !empty( $_SERVER['HTTP_USER_AGENT'] ) )
  141. $this->useragent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  142. }
  143. /**
  144. * This method detects the mobile User Agent name.
  145. *
  146. * @return string The matched User Agent name, false otherwise.
  147. */
  148. function get_mobile_user_agent_name() {
  149. if( $this->is_chrome_for_iOS( ) ) //keep this check before the safari rule
  150. return 'chrome-for-ios';
  151. elseif ( $this->is_iphone_or_ipod( 'iphone-safari' ) )
  152. return 'iphone';
  153. elseif ( $this->is_ipad( 'ipad-safari' ) )
  154. return 'ipad';
  155. elseif ( $this->is_android_tablet() ) //keep this check before the android rule
  156. return 'android_tablet';
  157. elseif ( $this->is_android() )
  158. return 'android';
  159. elseif ( $this->is_blackberry_10() )
  160. return 'blackberry_10';
  161. elseif ( $this->is_blackbeberry() )
  162. return 'blackberry';
  163. elseif ( $this->is_WindowsPhone7() )
  164. return 'win7';
  165. elseif ( $this->is_windows_phone_8() )
  166. return 'winphone8';
  167. elseif ( $this->is_opera_mini() )
  168. return 'opera-mini';
  169. elseif ( $this->is_opera_mini_dumb() )
  170. return 'opera-mini-dumb';
  171. elseif ( $this->is_opera_mobile() )
  172. return 'opera-mobi';
  173. elseif ( $this->is_blackberry_tablet() )
  174. return 'blackberry_tablet';
  175. elseif ( $this->is_kindle_fire() )
  176. return 'kindle-fire';
  177. elseif ( $this->is_PalmWebOS() )
  178. return 'webos';
  179. elseif ( $this->is_S60_OSSBrowser() )
  180. return 'series60';
  181. elseif ( $this->is_firefox_os() )
  182. return 'firefoxOS';
  183. elseif ( $this->is_firefox_mobile() )
  184. return 'firefox_mobile';
  185. elseif ( $this->is_MaemoTablet() )
  186. return 'maemo';
  187. elseif ( $this->is_MeeGo() )
  188. return 'meego';
  189. elseif( $this->is_TouchPad() )
  190. return 'hp_tablet';
  191. elseif ( $this->is_facebook_for_iphone() )
  192. return 'facebook-for-iphone';
  193. elseif ( $this->is_facebook_for_ipad() )
  194. return 'facebook-for-ipad';
  195. elseif ( $this->is_twitter_for_iphone() )
  196. return 'twitter-for-iphone';
  197. elseif ( $this->is_twitter_for_ipad() )
  198. return 'twitter-for-ipad';
  199. elseif ( $this->is_wordpress_for_ios() )
  200. return 'ios-app';
  201. elseif ( $this->is_iphone_or_ipod( 'iphone-not-safari' ) )
  202. return 'iphone-unknown';
  203. elseif ( $this->is_ipad( 'ipad-not-safari' ) )
  204. return 'ipad-unknown';
  205. elseif ( $this->is_Nintendo_3DS() )
  206. return 'nintendo-3ds';
  207. else {
  208. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  209. $dumb_agents = $this->dumb_agents;
  210. foreach ( $dumb_agents as $dumb_agent ) {
  211. if ( false !== strpos( $agent, $dumb_agent ) ) {
  212. return $dumb_agent;
  213. }
  214. }
  215. }
  216. return false;
  217. }
  218. /**
  219. * This method detects the mobile device's platform. All return strings are from the class constants.
  220. * Note that this function returns the platform name, not the UA name/type. You should use a different function
  221. * if you need to test the UA capabilites.
  222. *
  223. * @return string Name of the platform, false otherwise.
  224. */
  225. public function get_platform() {
  226. if ( isset( $this->_platform ) ) {
  227. return $this->_platform;
  228. }
  229. if ( strpos( $this->useragent, 'windows phone' ) !== false ) {
  230. $this->_platform = self::PLATFORM_WINDOWS;
  231. }
  232. elseif ( strpos( $this->useragent, 'windows ce' ) !== false ) {
  233. $this->_platform = self::PLATFORM_WINDOWS;
  234. }
  235. elseif ( strpos( $this->useragent, 'ipad' ) !== false ) {
  236. $this->_platform = self::PLATFORM_IPAD;
  237. }
  238. else if ( strpos( $this->useragent, 'ipod' ) !== false ) {
  239. $this->_platform = self::PLATFORM_IPOD;
  240. }
  241. else if ( strpos( $this->useragent, 'iphone' ) !== false ) {
  242. $this->_platform = self::PLATFORM_IPHONE;
  243. }
  244. elseif ( strpos( $this->useragent, 'android' ) !== false ) {
  245. if ( $this->is_android_tablet() )
  246. $this->_platform = self::PLATFORM_ANDROID_TABLET;
  247. else
  248. $this->_platform = self::PLATFORM_ANDROID;
  249. }
  250. elseif ( $this->is_kindle_fire() ) {
  251. $this->_platform = self::PLATFORM_ANDROID_TABLET;
  252. }
  253. elseif ( $this->is_blackberry_10() ) {
  254. $this->_platform = self::PLATFORM_BLACKBERRY_10;
  255. }
  256. elseif ( strpos( $this->useragent, 'blackberry' ) !== false ) {
  257. $this->_platform = self::PLATFORM_BLACKBERRY;
  258. }
  259. elseif ( $this->is_blackberry_tablet() ) {
  260. $this->_platform = self::PLATFORM_BLACKBERRY;
  261. }
  262. elseif ( $this->is_symbian_platform() ) {
  263. $this->_platform = self::PLATFORM_SYMBIAN;
  264. }
  265. elseif ( $this->is_symbian_s40_platform() ) {
  266. $this->_platform = self::PLATFORM_SYMBIAN_S40;
  267. }
  268. elseif ( $this->is_J2ME_platform() ) {
  269. $this->_platform = self::PLATFORM_J2ME_MIDP;
  270. }
  271. elseif ( $this->is_firefox_os() ) {
  272. $this->_platform = self::PLATFORM_FIREFOX_OS;
  273. }
  274. else
  275. $this->_platform = false;
  276. return $this->_platform;
  277. }
  278. /*
  279. * This method detects for UA which can display iPhone-optimized web content.
  280. * Includes iPhone, iPod Touch, Android, WebOS, Fennec (Firefox mobile), etc.
  281. *
  282. */
  283. function isTierIphone() {
  284. if ( isset( $this->isTierIphone ) ) {
  285. return $this->isTierIphone;
  286. }
  287. if ( $this->is_iphoneOrIpod() ) {
  288. $this->matched_agent = 'iphone';
  289. $this->isTierIphone = true;
  290. $this->isTierRichCss = false;
  291. $this->isTierGenericMobile = false;
  292. }
  293. elseif ( $this->is_android() ) {
  294. $this->matched_agent = 'android';
  295. $this->isTierIphone = true;
  296. $this->isTierRichCss = false;
  297. $this->isTierGenericMobile = false;
  298. }
  299. elseif ( $this->is_windows_phone_8() ) {
  300. $this->matched_agent = 'winphone8';
  301. $this->isTierIphone = true;
  302. $this->isTierRichCss = false;
  303. $this->isTierGenericMobile = false;
  304. }
  305. elseif ( $this->is_WindowsPhone7() ) {
  306. $this->matched_agent = 'win7';
  307. $this->isTierIphone = true;
  308. $this->isTierRichCss = false;
  309. $this->isTierGenericMobile = false;
  310. }
  311. elseif ( $this->is_blackberry_10() ) {
  312. $this->matched_agent = 'blackberry-10';
  313. $this->isTierIphone = true;
  314. $this->isTierRichCss = false;
  315. $this->isTierGenericMobile = false;
  316. }
  317. elseif ( $this->is_blackbeberry() && $this->detect_blackberry_browser_version() == 'blackberry-webkit' ) {
  318. $this->matched_agent = 'blackberry-webkit';
  319. $this->isTierIphone = true;
  320. $this->isTierRichCss = false;
  321. $this->isTierGenericMobile = false;
  322. }
  323. elseif ( $this->is_blackberry_tablet() ) {
  324. $this->matched_agent = 'blackberry_tablet';
  325. $this->isTierIphone = true;
  326. $this->isTierRichCss = false;
  327. $this->isTierGenericMobile = false;
  328. }
  329. elseif ( $this->is_PalmWebOS() ) {
  330. $this->matched_agent = 'webos';
  331. $this->isTierIphone = true;
  332. $this->isTierRichCss = false;
  333. $this->isTierGenericMobile = false;
  334. }
  335. elseif ( $this->is_TouchPad() ) {
  336. $this->matched_agent = 'hp_tablet';
  337. $this->isTierIphone = true;
  338. $this->isTierRichCss = false;
  339. $this->isTierGenericMobile = false;
  340. }
  341. elseif ( $this->is_firefox_os() ) {
  342. $this->matched_agent = 'firefoxOS';
  343. $this->isTierIphone = true;
  344. $this->isTierRichCss = false;
  345. $this->isTierGenericMobile = false;
  346. }
  347. elseif ( $this->is_firefox_mobile() ) {
  348. $this->matched_agent = 'fennec';
  349. $this->isTierIphone = true;
  350. $this->isTierRichCss = false;
  351. $this->isTierGenericMobile = false;
  352. }
  353. elseif ( $this->is_opera_mobile() ) {
  354. $this->matched_agent = 'opera-mobi';
  355. $this->isTierIphone = true;
  356. $this->isTierRichCss = false;
  357. $this->isTierGenericMobile = false;
  358. }
  359. elseif ( $this->is_MaemoTablet() ) {
  360. $this->matched_agent = 'maemo';
  361. $this->isTierIphone = true;
  362. $this->isTierRichCss = false;
  363. $this->isTierGenericMobile = false;
  364. }
  365. elseif ( $this->is_MeeGo() ) {
  366. $this->matched_agent = 'meego';
  367. $this->isTierIphone = true;
  368. $this->isTierRichCss = false;
  369. $this->isTierGenericMobile = false;
  370. }
  371. elseif ( $this->is_kindle_touch() ) {
  372. $this->matched_agent = 'kindle-touch';
  373. $this->isTierIphone = true;
  374. $this->isTierRichCss = false;
  375. $this->isTierGenericMobile = false;
  376. }
  377. elseif ( $this->is_Nintendo_3DS() ) {
  378. $this->matched_agent = 'nintendo-3ds';
  379. $this->isTierIphone = true;
  380. $this->isTierRichCss = false;
  381. $this->isTierGenericMobile = false;
  382. }
  383. else {
  384. $this->isTierIphone = false;
  385. }
  386. return $this->isTierIphone;
  387. }
  388. /*
  389. * This method detects for UA which are likely to be capable
  390. * but may not necessarily support JavaScript.
  391. * Excludes all iPhone Tier UA.
  392. *
  393. */
  394. function isTierRichCss(){
  395. if ( isset( $this->isTierRichCss ) ) {
  396. return $this->isTierRichCss;
  397. }
  398. if ($this->isTierIphone())
  399. return false;
  400. //The following devices are explicitly ok.
  401. if ( $this->is_S60_OSSBrowser() ) {
  402. $this->matched_agent = 'series60';
  403. $this->isTierIphone = false;
  404. $this->isTierRichCss = true;
  405. $this->isTierGenericMobile = false;
  406. }
  407. elseif ( $this->is_opera_mini() ) {
  408. $this->matched_agent = 'opera-mini';
  409. $this->isTierIphone = false;
  410. $this->isTierRichCss = true;
  411. $this->isTierGenericMobile = false;
  412. }
  413. elseif ( $this->is_blackbeberry() ) {
  414. $detectedDevice = $this->detect_blackberry_browser_version();
  415. if ( $detectedDevice === 'blackberry-5' || $detectedDevice == 'blackberry-4.7' || $detectedDevice === 'blackberry-4.6' ) {
  416. $this->matched_agent = $detectedDevice;
  417. $this->isTierIphone = false;
  418. $this->isTierRichCss = true;
  419. $this->isTierGenericMobile = false;
  420. }
  421. }
  422. else {
  423. $this->isTierRichCss = false;
  424. }
  425. return $this->isTierRichCss;
  426. }
  427. // Detects if the user is using a tablet.
  428. // props Corey Gilmore, BGR.com
  429. static function is_tablet() {
  430. return ( 0 // never true, but makes it easier to manage our list of tablet conditions
  431. || self::is_ipad()
  432. || self::is_android_tablet()
  433. || self::is_blackberry_tablet()
  434. || self::is_kindle_fire()
  435. || self::is_MaemoTablet()
  436. || self::is_TouchPad()
  437. );
  438. }
  439. /*
  440. * Detects if the current UA is the default iPhone or iPod Touch Browser.
  441. *
  442. * DEPRECATED: use is_iphone_or_ipod
  443. *
  444. */
  445. static function is_iphoneOrIpod(){
  446. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  447. return false;
  448. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  449. if ( ( strpos( $ua, 'iphone' ) !== false ) || ( strpos( $ua,'ipod' ) !== false ) ) {
  450. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  451. return false;
  452. else
  453. return true;
  454. }
  455. else
  456. return false;
  457. }
  458. /*
  459. * Detects if the current UA is iPhone Mobile Safari or another iPhone or iPod Touch Browser.
  460. *
  461. * They type can check for any iPhone, an iPhone using Safari, or an iPhone using something other than Safari.
  462. *
  463. * Note: If you want to check for Opera mini, Opera mobile or Firefox mobile (or any 3rd party iPhone browser),
  464. * you should put the check condition before the check for 'iphone-any' or 'iphone-not-safari'.
  465. * Otherwise those browsers will be 'catched' by the iphone string.
  466. *
  467. */
  468. static function is_iphone_or_ipod( $type = 'iphone-any' ) {
  469. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  470. return false;
  471. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  472. $is_iphone = ( strpos( $ua, 'iphone' ) !== false ) || ( strpos( $ua,'ipod' ) !== false );
  473. $is_safari = ( false !== strpos( $ua, 'safari' ) );
  474. if ( 'iphone-safari' == $type )
  475. return $is_iphone && $is_safari;
  476. elseif ( 'iphone-not-safari' == $type )
  477. return $is_iphone && !$is_safari;
  478. else
  479. return $is_iphone;
  480. }
  481. /*
  482. * Detects if the current UA is Chrome for iOS
  483. *
  484. * The User-Agent string in Chrome for iOS is the same as the Mobile Safari User-Agent, with CriOS/<ChromeRevision> instead of Version/<VersionNum>.
  485. * - 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
  486. */
  487. static function is_chrome_for_iOS( ) {
  488. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  489. return false;
  490. if ( self::is_iphone_or_ipod( 'iphone-safari' ) === false ) return false;
  491. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  492. if ( strpos( $ua, 'crios/' ) !== false )
  493. return true;
  494. else
  495. return false;
  496. }
  497. /*
  498. * Detects if the current UA is Twitter for iPhone
  499. *
  500. * Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_5 like Mac OS X; nb-no) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8L1 Twitter for iPhone
  501. * Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B206 Twitter for iPhone
  502. *
  503. */
  504. static function is_twitter_for_iphone( ) {
  505. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  506. return false;
  507. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  508. if ( strpos( $ua, 'ipad' ) !== false )
  509. return false;
  510. if ( strpos( $ua, 'twitter for iphone' ) !== false )
  511. return true;
  512. else
  513. return false;
  514. }
  515. /*
  516. * Detects if the current UA is Twitter for iPad
  517. *
  518. * Old version 4.X - Mozilla/5.0 (iPad; U; CPU OS 4_3_5 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Mobile/8L1 Twitter for iPad
  519. * Ver 5.0 or Higher - Mozilla/5.0 (iPad; CPU OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B206 Twitter for iPhone
  520. *
  521. */
  522. static function is_twitter_for_ipad( ) {
  523. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  524. return false;
  525. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  526. if ( strpos( $ua, 'twitter for ipad' ) !== false )
  527. return true;
  528. elseif( strpos( $ua, 'ipad' ) !== false && strpos( $ua, 'twitter for iphone' ) !== false )
  529. return true;
  530. else
  531. return false;
  532. }
  533. /*
  534. * Detects if the current UA is Facebook for iPhone
  535. * - Facebook 4020.0 (iPhone; iPhone OS 5.0.1; fr_FR)
  536. * - Mozilla/5.0 (iPhone; U; CPU iPhone OS 5_0 like Mac OS X; en_US) AppleWebKit (KHTML, like Gecko) Mobile [FBAN/FBForIPhone;FBAV/4.0.2;FBBV/4020.0;FBDV/iPhone3,1;FBMD/iPhone;FBSN/iPhone OS;FBSV/5.0;FBSS/2; FBCR/O2;FBID/phone;FBLC/en_US;FBSF/2.0]
  537. * - Mozilla/5.0 (iPhone; CPU iPhone OS 5_1_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Mobile/9B206 [FBAN/FBIOS;FBAV/5.0;FBBV/47423;FBDV/iPhone3,1;FBMD/iPhone;FBSN/iPhone OS;FBSV/5.1.1;FBSS/2; FBCR/3ITA;FBID/phone;FBLC/en_US]
  538. */
  539. static function is_facebook_for_iphone( ) {
  540. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  541. return false;
  542. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  543. if( strpos( $ua, 'iphone' ) === false )
  544. return false;
  545. if ( strpos( $ua, 'facebook' ) !== false && strpos( $ua, 'ipad' ) === false )
  546. return true;
  547. else if ( strpos( $ua, 'fbforiphone' ) !== false && strpos( $ua, 'tablet' ) === false )
  548. return true;
  549. else if ( strpos( $ua, 'fban/fbios;' ) !== false && strpos( $ua, 'tablet' ) === false ) //FB app v5.0 or higher
  550. return true;
  551. else
  552. return false;
  553. }
  554. /*
  555. * Detects if the current UA is Facebook for iPad
  556. * - Facebook 4020.0 (iPad; iPhone OS 5.0.1; en_US)
  557. * - Mozilla/5.0 (iPad; U; CPU iPhone OS 5_0 like Mac OS X; en_US) AppleWebKit (KHTML, like Gecko) Mobile [FBAN/FBForIPhone;FBAV/4.0.2;FBBV/4020.0;FBDV/iPad2,1;FBMD/iPad;FBSN/iPhone OS;FBSV/5.0;FBSS/1; FBCR/;FBID/tablet;FBLC/en_US;FBSF/1.0]
  558. * - Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Mobile/10A403 [FBAN/FBIOS;FBAV/5.0;FBBV/47423;FBDV/iPad2,1;FBMD/iPad;FBSN/iPhone OS;FBSV/6.0;FBSS/1; FBCR/;FBID/tablet;FBLC/en_US]
  559. */
  560. static function is_facebook_for_ipad( ) {
  561. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  562. return false;
  563. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  564. if ( strpos( $ua, 'ipad' ) === false )
  565. return false;
  566. if ( strpos( $ua, 'facebook' ) !== false || strpos( $ua, 'fbforiphone' ) !== false || strpos( $ua, 'fban/fbios;' ) !== false )
  567. return true;
  568. else
  569. return false;
  570. }
  571. /*
  572. * Detects if the current UA is WordPress for iOS
  573. */
  574. static function is_wordpress_for_ios( ) {
  575. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  576. return false;
  577. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  578. if ( strpos( $ua, 'wp-iphone' ) !== false )
  579. return true;
  580. else
  581. return false;
  582. }
  583. /*
  584. * Detects if the current device is an iPad.
  585. * They type can check for any iPad, an iPad using Safari, or an iPad using something other than Safari.
  586. *
  587. * Note: If you want to check for Opera mini, Opera mobile or Firefox mobile (or any 3rd party iPad browser),
  588. * you should put the check condition before the check for 'iphone-any' or 'iphone-not-safari'.
  589. * Otherwise those browsers will be 'catched' by the ipad string.
  590. *
  591. */
  592. static function is_ipad( $type = 'ipad-any' ) {
  593. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  594. return false;
  595. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  596. $is_ipad = ( false !== strpos( $ua, 'ipad' ) );
  597. $is_safari = ( false !== strpos( $ua, 'safari' ) );
  598. if ( 'ipad-safari' == $type )
  599. return $is_ipad && $is_safari;
  600. elseif ( 'ipad-not-safari' == $type )
  601. return $is_ipad && !$is_safari;
  602. else
  603. return $is_ipad;
  604. }
  605. /*
  606. * Detects if the current browser is Firefox Mobile (Fennec)
  607. *
  608. * http://www.useragentstring.com/pages/Fennec/
  609. * Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.1.1) Gecko/20110415 Firefox/4.0.2pre Fennec/4.0.1
  610. * Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1b2pre) Gecko/20081015 Fennec/1.0a1
  611. */
  612. static function is_firefox_mobile( ) {
  613. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  614. return false;
  615. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  616. if ( strpos( $ua, 'fennec' ) !== false )
  617. return true;
  618. else
  619. return false;
  620. }
  621. /*
  622. * Detects if the current browser is FirefoxOS Native browser
  623. *
  624. * Mozilla/5.0 (Mobile; rv:14.0) Gecko/14.0 Firefox/14.0
  625. *
  626. */
  627. static function is_firefox_os( ) {
  628. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  629. return false;
  630. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  631. if ( strpos( $ua, 'mozilla' ) !== false && strpos( $ua, 'mobile' ) !== false && strpos( $ua, 'gecko' ) !== false && strpos( $ua, 'firefox' ) !== false)
  632. return true;
  633. else
  634. return false;
  635. }
  636. /*
  637. * Detects if the current browser is Opera Mobile
  638. *
  639. * What is the difference between Opera Mobile and Opera Mini?
  640. * - Opera Mobile is a full Internet browser for mobile devices.
  641. * - Opera Mini always uses a transcoder to convert the page for a small display.
  642. * (it uses Opera advanced server compression technology to compress web content before it gets to a device.
  643. * The rendering engine is on Opera's server.)
  644. *
  645. * Opera/9.80 (Windows NT 6.1; Opera Mobi/14316; U; en) Presto/2.7.81 Version/11.00"
  646. * Opera/9.50 (Nintendo DSi; Opera/507; U; en-US)
  647. */
  648. static function is_opera_mobile( ) {
  649. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  650. return false;
  651. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  652. if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mobi' ) !== false )
  653. return true;
  654. elseif ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'nintendo dsi' ) !== false )
  655. return true;
  656. else
  657. return false;
  658. }
  659. /*
  660. * Detects if the current browser is Opera Mini
  661. *
  662. * Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)
  663. * Opera/9.80 (Android;Opera Mini/6.0.24212/24.746 U;en) Presto/2.5.25 Version/10.5454
  664. * Opera/9.80 (iPhone; Opera Mini/5.0.019802/18.738; U; en) Presto/2.4.15
  665. * Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
  666. * Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
  667. * Opera/9.80 (Series 60; Opera Mini/5.1.22783/23.334; U; en) Presto/2.5.25 Version/10.54
  668. * Opera/9.80 (BlackBerry; Opera Mini/5.1.22303/22.387; U; en) Presto/2.5.25 Version/10.54
  669. *
  670. */
  671. static function is_opera_mini( ) {
  672. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  673. return false;
  674. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  675. if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mini' ) !== false )
  676. return true;
  677. else
  678. return false;
  679. }
  680. /*
  681. * Detects if the current browser is Opera Mini, but not on a smart device OS(Android, iOS, etc)
  682. * Used to send users on dumb devices to m.wor
  683. */
  684. static function is_opera_mini_dumb( ) {
  685. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  686. return false;
  687. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  688. if ( self::is_opera_mini() ) {
  689. if ( strpos( $ua, 'android' ) !== false || strpos( $ua, 'iphone' ) !== false || strpos( $ua, 'ipod' ) !== false
  690. || strpos( $ua, 'ipad' ) !== false || strpos( $ua, 'blackberry' ) !== false)
  691. return false;
  692. else
  693. return true;
  694. } else {
  695. return false;
  696. }
  697. }
  698. /*
  699. * Detects if the current browser is Opera Mobile or Mini.
  700. * DEPRECATED: use is_opera_mobile or is_opera_mini
  701. *
  702. * Opera Mini 5 Beta: Opera/9.80 (J2ME/MIDP; Opera Mini/5.0.15650/756; U; en) Presto/2.2.0
  703. * Opera Mini 8: Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)
  704. */
  705. static function is_OperaMobile() {
  706. _deprecated_function( __FUNCTION__, 'always', 'is_opera_mini() or is_opera_mobile()' );
  707. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  708. return false;
  709. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  710. if ( strpos( $ua, 'opera' ) !== false ) {
  711. if ( ( strpos( $ua, 'mini' ) !== false ) || ( strpos( $ua,'mobi' ) !== false ) )
  712. return true;
  713. else
  714. return false;
  715. } else {
  716. return false;
  717. }
  718. }
  719. /*
  720. * Detects if the current browser is a Windows Phone 7 device.
  721. * ex: Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; LG; GW910)
  722. */
  723. static function is_WindowsPhone7() {
  724. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  725. return false;
  726. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  727. if ( strpos( $ua, 'windows phone os 7' ) === false ) {
  728. return false;
  729. } else {
  730. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  731. return false;
  732. else
  733. return true;
  734. }
  735. }
  736. /*
  737. * Detects if the current browser is a Windows Phone 8 device.
  738. * ex: Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; ARM; Touch; IEMobile/10.0; <Manufacturer>; <Device> [;<Operator>])
  739. */
  740. static function is_windows_phone_8() {
  741. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  742. return false;
  743. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  744. if ( strpos( $ua, 'windows phone 8' ) === false ) {
  745. return false;
  746. } else {
  747. return true;
  748. }
  749. }
  750. /*
  751. * Detects if the current browser is on a Palm device running the new WebOS. This EXCLUDES TouchPad.
  752. *
  753. * ex1: Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pre/1.1
  754. * ex2: Mozilla/5.0 (webOS/1.4.0; U; en-US) AppleWebKit/532.2 (KHTML, like Gecko) Version/1.0 Safari/532.2 Pixi/1.1
  755. *
  756. */
  757. static function is_PalmWebOS() {
  758. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  759. return false;
  760. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  761. if ( strpos( $ua, 'webos' ) === false ) {
  762. return false;
  763. } else {
  764. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  765. return false;
  766. else
  767. return true;
  768. }
  769. }
  770. /*
  771. * Detects if the current browser is the HP TouchPad default browser. This excludes phones wt WebOS.
  772. *
  773. * TouchPad Emulator: Mozilla/5.0 (hp-desktop; Linux; hpwOS/2.0; U; it-IT) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 Desktop/1.0
  774. * TouchPad: Mozilla/5.0 (hp-tablet; Linux; hpwOS/3.0.0; U; en-US) AppleWebKit/534.6 (KHTML, like Gecko) wOSBrowser/233.70 Safari/534.6 TouchPad/1.0
  775. *
  776. */
  777. static function is_TouchPad() {
  778. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  779. return false;
  780. $http_user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  781. if ( false !== strpos( $http_user_agent, 'hp-tablet' ) || false !== strpos( $http_user_agent, 'hpwos' ) || false !== strpos( $http_user_agent, 'touchpad' ) ) {
  782. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  783. return false;
  784. else
  785. return true;
  786. }
  787. else
  788. return false;
  789. }
  790. /*
  791. * Detects if the current browser is the Series 60 Open Source Browser.
  792. *
  793. * OSS Browser 3.2 on E75: Mozilla/5.0 (SymbianOS/9.3; U; Series60/3.2 NokiaE75-1/110.48.125 Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413
  794. *
  795. * 7.0 Browser (Nokia 5800 XpressMusic (v21.0.025)) : Mozilla/5.0 (SymbianOS/9.4; U; Series60/5.0 Nokia5800d-1/21.0.025; Profile/MIDP-2.1 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413
  796. *
  797. * Browser 7.1 (Nokia N97 (v12.0.024)) : Mozilla/5.0 (SymbianOS/9.4; Series60/5.0 NokiaN97-1/12.0.024; Profile/MIDP-2.1 Configuration/CLDC-1.1; en-us) AppleWebKit/525 (KHTML, like Gecko) BrowserNG/7.1.12344
  798. *
  799. */
  800. static function is_S60_OSSBrowser() {
  801. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  802. return false;
  803. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  804. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  805. return false;
  806. $pos_webkit = strpos( $agent, 'webkit' );
  807. if ( $pos_webkit !== false ) {
  808. //First, test for WebKit, then make sure it's either Symbian or S60.
  809. if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
  810. return true;
  811. } else
  812. return false;
  813. } elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent,'series60' ) !== false ) {
  814. return true;
  815. } elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent,'series60' ) !== false ) {
  816. return true;
  817. }
  818. return false;
  819. }
  820. /*
  821. *
  822. * Detects if the device platform is the Symbian Series 60.
  823. *
  824. */
  825. static function is_symbian_platform() {
  826. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  827. return false;
  828. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  829. $pos_webkit = strpos( $agent, 'webkit' );
  830. if ( $pos_webkit !== false ) {
  831. //First, test for WebKit, then make sure it's either Symbian or S60.
  832. if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
  833. return true;
  834. } else
  835. return false;
  836. } elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent,'series60' ) !== false ) {
  837. return true;
  838. } elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent,'series60' ) !== false ) {
  839. return true;
  840. } elseif ( strpos( $agent, 'opera mini' ) !== false ) {
  841. if( strpos( $agent,'symbianos' ) !== false || strpos( $agent,'symbos' ) !== false || strpos( $agent,'series 60' ) !== false )
  842. return true;
  843. }
  844. return false;
  845. }
  846. /*
  847. *
  848. * Detects if the device platform is the Symbian Series 40.
  849. * Nokia Browser for Series 40 is a proxy based browser, previously known as Ovi Browser.
  850. * This browser will report 'NokiaBrowser' in the header, however some older version will also report 'OviBrowser'.
  851. *
  852. */
  853. static function is_symbian_s40_platform() {
  854. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  855. return false;
  856. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  857. if ( strpos( $agent, 'series40' ) !== false ) {
  858. if( strpos( $agent,'nokia' ) !== false || strpos( $agent,'ovibrowser' ) !== false || strpos( $agent,'nokiabrowser' ) !== false )
  859. return true;
  860. }
  861. return false;
  862. }
  863. static function is_J2ME_platform() {
  864. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  865. return false;
  866. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  867. if ( strpos( $agent, 'j2me/midp' ) !== false ) {
  868. return true;
  869. } elseif ( strpos( $agent, 'midp' ) !== false && strpos( $agent, 'cldc' ) ) {
  870. return true;
  871. }
  872. return false;
  873. }
  874. /*
  875. * Detects if the current UA is on one of the Maemo-based Nokia Internet Tablets.
  876. */
  877. static function is_MaemoTablet() {
  878. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  879. return false;
  880. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  881. $pos_maemo = strpos( $agent, 'maemo' );
  882. if ( $pos_maemo === false ) return false;
  883. //Must be Linux + Tablet, or else it could be something else.
  884. if ( strpos( $agent, 'tablet' ) !== false && strpos( $agent, 'linux' ) !== false ) {
  885. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  886. return false;
  887. else
  888. return true;
  889. } else
  890. return false;
  891. }
  892. /*
  893. * Detects if the current UA is a MeeGo device (Nokia Smartphone).
  894. */
  895. static function is_MeeGo() {
  896. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  897. return false;
  898. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  899. if ( strpos( $ua, 'meego' ) === false ) {
  900. return false;
  901. } else {
  902. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  903. return false;
  904. else
  905. return true;
  906. }
  907. }
  908. /*
  909. is_webkit() can be used to check the User Agent for an webkit generic browser
  910. */
  911. static function is_webkit() {
  912. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  913. return false;
  914. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  915. $pos_webkit = strpos( $agent, 'webkit' );
  916. if ( $pos_webkit !== false )
  917. return true;
  918. else
  919. return false;
  920. }
  921. /**
  922. * Detects if the current browser is the Native Android browser.
  923. * @return boolean true if the browser is Android otherwise false
  924. */
  925. static function is_android() {
  926. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  927. return false;
  928. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  929. $pos_android = strpos( $agent, 'android' );
  930. if ( $pos_android !== false ) {
  931. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  932. return false;
  933. else
  934. return true;
  935. }
  936. else
  937. return false;
  938. }
  939. /**
  940. * Detects if the current browser is the Native Android Tablet browser.
  941. * Assumes 'Android' should be in the user agent, but not 'mobile'
  942. *
  943. * @return boolean true if the browser is Android and not 'mobile' otherwise false
  944. */
  945. static function is_android_tablet( ) {
  946. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  947. return false;
  948. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  949. $pos_android = strpos( $agent, 'android' );
  950. $pos_mobile = strpos( $agent, 'mobile' );
  951. $post_android_app = strpos( $agent, 'wp-android' );
  952. if ( $pos_android !== false && $pos_mobile === false && $post_android_app === false ) {
  953. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  954. return false;
  955. else
  956. return true;
  957. } else
  958. return false;
  959. }
  960. /**
  961. * Detects if the current browser is the Kindle Fire Native browser.
  962. *
  963. * Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-84) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true
  964. * Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-84) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=false
  965. *
  966. * @return boolean true if the browser is Kindle Fire Native browser otherwise false
  967. */
  968. static function is_kindle_fire( ) {
  969. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  970. return false;
  971. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  972. $pos_silk = strpos( $agent, 'silk/' );
  973. $pos_silk_acc = strpos( $agent, 'silk-accelerated=' );
  974. if ( $pos_silk !== false && $pos_silk_acc !== false )
  975. return true;
  976. else
  977. return false;
  978. }
  979. /**
  980. * Detects if the current browser is the Kindle Touch Native browser
  981. *
  982. * Mozilla/5.0 (X11; U; Linux armv7l like Android; en-us) AppleWebKit/531.2+ (KHTML, like Gecko) Version/5.0 Safari/533.2+ Kindle/3.0+
  983. *
  984. * @return boolean true if the browser is Kindle monochrome Native browser otherwise false
  985. */
  986. static function is_kindle_touch( ) {
  987. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  988. return false;
  989. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  990. $pos_kindle_touch = strpos( $agent, 'kindle/3.0+' );
  991. if ( $pos_kindle_touch !== false && self::is_kindle_fire() === false )
  992. return true;
  993. else
  994. return false;
  995. }
  996. // Detect if user agent is the WordPress.com Windows 8 app (used ONLY on the custom oauth stylesheet)
  997. static function is_windows8_auth( ) {
  998. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  999. return false;
  1000. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  1001. $pos = strpos( $agent, 'msauthhost' );
  1002. if ( $pos !== false )
  1003. return true;
  1004. else
  1005. return false;
  1006. }
  1007. // Detect if user agent is the WordPress.com Windows 8 app.
  1008. static function is_wordpress_for_win8( ) {
  1009. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  1010. return false;
  1011. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  1012. $pos = strpos( $agent, 'wp-windows8' );
  1013. if ( $pos !== false )
  1014. return true;
  1015. else
  1016. return false;
  1017. }
  1018. /*
  1019. * is_blackberry_tablet() can be used to check the User Agent for a RIM blackberry tablet
  1020. * The user agent of the BlackBerry® Tablet OS follows a format similar to the following:
  1021. * Mozilla/5.0 (PlayBook; U; RIM Tablet OS 1.0.0; en-US) AppleWebKit/534.8+ (KHTML, like Gecko) Version/0.0.1 Safari/534.8+
  1022. *
  1023. */
  1024. static function is_blackberry_tablet() {
  1025. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  1026. return false;
  1027. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  1028. $pos_playbook = stripos( $agent, 'PlayBook' );
  1029. $pos_rim_tablet = stripos( $agent, 'RIM Tablet' );
  1030. if ( ($pos_playbook === false) || ($pos_rim_tablet === false) )
  1031. {
  1032. return false;
  1033. } else {
  1034. return true;
  1035. }
  1036. }
  1037. /*
  1038. is_blackbeberry() can be used to check the User Agent for a blackberry device
  1039. Note that opera mini on BB matches this rule.
  1040. */
  1041. static function is_blackbeberry() {
  1042. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  1043. return false;
  1044. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  1045. $pos_blackberry = strpos( $agent, 'blackberry' );
  1046. if ( $pos_blackberry !== false ) {
  1047. if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() )
  1048. return false;
  1049. else
  1050. return true;
  1051. } else {
  1052. return false;
  1053. }
  1054. }
  1055. /*
  1056. is_blackberry_10() can be used to check the User Agent for a BlackBerry 10 device.
  1057. */
  1058. static function is_blackberry_10() {
  1059. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  1060. return ( strpos( $agent, 'bb10' ) !== false ) && ( strpos( $agent, 'mobile' ) !== false );
  1061. }
  1062. /**
  1063. * Retrieve the blackberry OS version.
  1064. *
  1065. * Return strings are from the following list:
  1066. * - blackberry-10
  1067. * - blackberry-7
  1068. * - blackberry-6
  1069. * - blackberry-torch //only the first edition. The 2nd edition has the OS7 onboard and doesn't need any special rule.
  1070. * - blackberry-5
  1071. * - blackberry-4.7
  1072. * - blackberry-4.6
  1073. * - blackberry-4.5
  1074. *
  1075. * @return string Version of the BB OS.
  1076. * If version is not found, get_blackbeberry_OS_version will return boolean false.
  1077. */
  1078. static function get_blackbeberry_OS_version() {
  1079. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  1080. return false;
  1081. if ( self::is_blackberry_10() ) {
  1082. return 'blackberry-10';
  1083. }
  1084. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  1085. $pos_blackberry = stripos( $agent, 'blackberry' );
  1086. if ( $pos_blackberry === false ) {
  1087. // not a blackberry device
  1088. return false;
  1089. }
  1090. // blackberry devices OS 6.0 or higher
  1091. // Mozilla/5.0 (BlackBerry; U; BlackBerry 9670; en) AppleWebKit/534.3+ (KHTML, like Gecko) Version/6.0.0.286 Mobile Safari/534.3+
  1092. // Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+
  1093. // Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0 Mobile Safari/534.11+
  1094. $pos_webkit = stripos( $agent, 'webkit' );
  1095. if ( $pos_webkit !== false ) {
  1096. // detected blackberry webkit browser
  1097. $pos_torch = stripos( $agent, 'BlackBerry 9800' );
  1098. if ( $pos_torch !== false ) {
  1099. return 'blackberry-torch'; // match the torch first edition. the 2nd edition should use the OS7 and doesn't need any special rule
  1100. } else {
  1101. // detecting the BB OS version for devices running OS 6.0 or higher
  1102. if ( preg_match( '#Version\/([\d\.]+)#i', $agent, $matches ) ) {
  1103. $version = $matches[1];
  1104. $version_num = explode( '.', $version );
  1105. if ( is_array( $version_num ) === false || count( $version_num ) <= 1 ) {
  1106. return 'blackberry-6'; // not a BB device that match our rule.
  1107. } else {
  1108. return 'blackberry-' . $version_num[0];
  1109. }
  1110. } else {
  1111. // if doesn't match returns the minimun version with a webkit browser. we should never fall here.
  1112. return 'blackberry-6'; // not a BB device that match our rule.
  1113. }
  1114. }
  1115. }
  1116. // blackberry devices <= 5.XX
  1117. // BlackBerry9000/5.0.0.93 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/179
  1118. if ( preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
  1119. $version = $matches[1];
  1120. } else {
  1121. return false; //not a BB device that match our rule.
  1122. }
  1123. $version_num = explode( '.', $version );
  1124. if ( is_array( $version_num ) === false || count( $version_num ) <= 1 ) {
  1125. return false;
  1126. }
  1127. if ( $version_num[0] == 5 ) {
  1128. return 'blackberry-5';
  1129. } elseif ( $version_num[0] == 4 && $version_num[1] == 7 ) {
  1130. return 'blackberry-4.7';
  1131. } elseif ( $version_num[0] == 4 && $version_num[1] == 6 ) {
  1132. return 'blackberry-4.6';
  1133. } elseif ( $version_num[0] == 4 && $version_num[1] == 5 ) {
  1134. return 'blackberry-4.5';
  1135. } else {
  1136. return false;
  1137. }
  1138. }
  1139. /**
  1140. * Retrieve the blackberry browser version.
  1141. *
  1142. * Return string are from the following list:
  1143. * - blackberry-10
  1144. * - blackberry-webkit
  1145. * - blackberry-5
  1146. * - blackberry-4.7
  1147. * - blackberry-4.6
  1148. *
  1149. * @return string Type of the BB browser.
  1150. * If browser's version is not found, detect_blackbeberry_browser_version will return boolean false.
  1151. */
  1152. static function detect_blackberry_browser_version() {
  1153. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
  1154. return false;
  1155. }
  1156. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  1157. if ( self::is_blackberry_10() ) {
  1158. return 'blackberry-10';
  1159. }
  1160. $pos_blackberry = strpos( $agent, 'blackberry' );
  1161. if ( $pos_blackberry === false ) {
  1162. // not a blackberry device
  1163. return false;
  1164. }
  1165. $pos_webkit = strpos( $agent, 'webkit' );
  1166. if ( ! ( $pos_webkit === false ) ) {
  1167. return 'blackberry-webkit';
  1168. } else {
  1169. if ( preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
  1170. $version = $matches[1];
  1171. } else {
  1172. return false; // not a BB device that match our rule.
  1173. }
  1174. $version_num = explode( '.', $version );
  1175. if ( is_array( $version_num ) === false || count( $version_num ) <= 1 ) {
  1176. return false;
  1177. }
  1178. if ( $version_num[0] == 5 ) {
  1179. return 'blackberry-5';
  1180. } elseif ( $version_num[0] == 4 && $version_num[1] == 7 ) {
  1181. return 'blackberry-4.7';
  1182. } elseif ( $version_num[0] == 4 && $version_num[1] == 6 ) {
  1183. return 'blackberry-4.6';
  1184. } else {
  1185. // A very old BB device is found or this is a BB device that doesn't match our rules.
  1186. return false;
  1187. }
  1188. }
  1189. }
  1190. // Checks if a visitor is coming from one of the WordPress mobile apps
  1191. static function is_mobile_app() {
  1192. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) )
  1193. return false;
  1194. $agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  1195. if ( isset( $_SERVER['X_USER_AGENT'] ) && preg_match( '|wp-webos|', $_SERVER['X_USER_AGENT'] ) )
  1196. return true; //wp4webos 1.1 or higher
  1197. $app_agents = array( 'wp-android', 'wp-blackberry', 'wp-iphone', 'wp-nokia', 'wp-webos', 'wp-windowsphone' );
  1198. // the mobile reader on iOS has an incorrect UA when loading the reader
  1199. // currently it is the default one provided by the iOS framework which
  1200. // causes problems with 2-step-auth
  1201. // User-Agent WordPress/3.1.4 CFNetwork/609 Darwin/13.0.0
  1202. $app_agents[] = 'wordpress/3.1';
  1203. foreach ( $app_agents as $app_agent ) {
  1204. if ( false !== strpos( $agent, $app_agent ) )
  1205. return true;
  1206. }
  1207. return false;
  1208. }
  1209. /*
  1210. * Detects if the current browser is Nintendo 3DS handheld.
  1211. *
  1212. * example: Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7498.US
  1213. * can differ in language, version and region
  1214. */
  1215. static function is_Nintendo_3DS() {
  1216. if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
  1217. return false;
  1218. }
  1219. $ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
  1220. if ( strpos( $ua, 'nintendo 3ds' ) !== false ) {
  1221. return true;
  1222. }
  1223. return false;
  1224. }
  1225. /**
  1226. * Was the current request made by a known bot?
  1227. *
  1228. * @return boolean
  1229. */
  1230. static function is_bot() {
  1231. static $is_bot = null;
  1232. if ( is_null( $is_bot ) ) {
  1233. $is_bot = Jetpack_User_Agent_Info::is_bot_user_agent( $_SERVER['HTTP_USER_AGENT'] );
  1234. }
  1235. return $is_bot;
  1236. }
  1237. /**
  1238. * Is the given user-agent a known bot?
  1239. * If you want an is_bot check for the current request's UA, use is_bot() instead of passing a user-agent to this method.
  1240. *
  1241. * @param $ua (string) A user-agent string
  1242. * @return boolean
  1243. */
  1244. static function is_bot_user_agent( $ua = null ) {
  1245. if ( empty( $ua ) )
  1246. return false;
  1247. $bot_agents = array(
  1248. 'alexa', 'altavista', 'ask jeeves', 'attentio', 'baiduspider', 'bingbot', 'chtml generic', 'crawler', 'fastmobilecrawl',
  1249. 'feedfetcher-google', 'firefly', 'froogle', 'gigabot', 'googlebot', 'googlebot-mobile', 'heritrix', 'ia_archiver', 'irlbot',
  1250. 'iescholar', 'infoseek', 'jumpbot', 'lycos', 'mediapartners', 'mediobot', 'motionbot', 'msnbot', 'mshots', 'openbot',
  1251. 'pss-webkit-request', 'pythumbnail', 'scooter', 'slurp', 'snapbot', 'spider', 'taptubot', 'technoratisnoop',
  1252. 'teoma', 'twiceler', 'yahooseeker', 'yahooysmcm', 'yammybot', 'ahrefsbot', 'pingdom.com_bot', 'kraken', 'yandexbot',
  1253. 'twitterbot', 'tweetmemebot', 'openhosebot', 'queryseekerspider', 'linkdexbot', 'grokkit-crawler',
  1254. 'livelapbot', 'germcrawler', 'domaintunocrawler', 'grapeshotcrawler', 'cloudflare-alwaysonline',
  1255. );
  1256. foreach ( $bot_agents as $bot_agent ) {
  1257. if ( false !== stripos( $ua, $bot_agent ) ) {
  1258. return true;
  1259. }
  1260. }
  1261. return false;
  1262. }
  1263. }