PageRenderTime 76ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

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

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