PageRenderTime 53ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

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