Completed
Push — add/jetpack-mobile-package ( 2965e8...6108e0 )
by
unknown
40:21 queued 32:22
created

User_Agent_Info::is_blackberry_tablet()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
/**
3
 * User agent detection for Jetpack.
4
 *
5
 * @package automattic/jetpack-device-detection
6
 */
7
8
namespace Automattic\Jetpack\Device_Detection;
9
10
class User_Agent_Info {
11
12
	public $useragent;
13
	public $matched_agent;
14
	public $isTierIphone; // Stores whether is the iPhone tier of devices.
15
	public $isTierRichCss; // Stores whether the device can probably support Rich CSS, but JavaScript (jQuery) support is not assumed.
16
	public $isTierGenericMobile; // Stores whether it is another mobile device, which cannot be assumed to support CSS or JS (eg, older BlackBerry, RAZR)
17
18
	private $_platform            = null; // Stores the device platform name
19
	const PLATFORM_WINDOWS        = 'windows';
20
	const PLATFORM_IPHONE         = 'iphone';
21
	const PLATFORM_IPOD           = 'ipod';
22
	const PLATFORM_IPAD           = 'ipad';
23
	const PLATFORM_BLACKBERRY     = 'blackberry';
24
	const PLATFORM_BLACKBERRY_10  = 'blackberry_10';
25
	const PLATFORM_SYMBIAN        = 'symbian_series60';
26
	const PLATFORM_SYMBIAN_S40    = 'symbian_series40';
27
	const PLATFORM_J2ME_MIDP      = 'j2me_midp';
28
	const PLATFORM_ANDROID        = 'android';
29
	const PLATFORM_ANDROID_TABLET = 'android_tablet';
30
	const PLATFORM_FIREFOX_OS     = 'firefoxOS';
31
32
	public $dumb_agents = array(
33
		'nokia',
34
		'blackberry',
35
		'philips',
36
		'samsung',
37
		'sanyo',
38
		'sony',
39
		'panasonic',
40
		'webos',
41
		'ericsson',
42
		'alcatel',
43
		'palm',
44
		'windows ce',
45
		'opera mini',
46
		'series60',
47
		'series40',
48
		'au-mic,',
49
		'audiovox',
50
		'avantgo',
51
		'blazer',
52
		'danger',
53
		'docomo',
54
		'epoc',
55
		'ericy',
56
		'i-mode',
57
		'ipaq',
58
		'midp-',
59
		'mot-',
60
		'netfront',
61
		'nitro',
62
		'palmsource',
63
		'pocketpc',
64
		'portalmmm',
65
		'rover',
66
		'sie-',
67
		'symbian',
68
		'cldc-',
69
		'j2me',
70
		'smartphone',
71
		'up.browser',
72
		'up.link',
73
		'up.link',
74
		'vodafone/',
75
		'wap1.',
76
		'wap2.',
77
		'mobile',
78
		'googlebot-mobile',
79
	);
80
81
82
	/**
83
	 * The constructor.
84
	 *
85
	 * @param string $ua (Optional) User agent.
86
	 */
87
	function __construct( $ua = '' ) {
88
		if ( $ua ) {
89
			$this->useragent = $ua;
90
		} else {
91
			if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
92
				$this->useragent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
93
			}
94
		}
95
	}
96
97
	/**
98
	 * This method detects the mobile User Agent name.
99
	 *
100
	 * @return string The matched User Agent name, false otherwise.
101
	 */
102
	function get_mobile_user_agent_name() {
103
		if ( $this->is_chrome_for_iOS() ) { // keep this check before the safari rule
104
			return 'chrome-for-ios';
105
		} elseif ( $this->is_iphone_or_ipod( 'iphone-safari' ) ) {
106
			return 'iphone';
107
		} elseif ( $this->is_ipad( 'ipad-safari' ) ) {
108
			return 'ipad';
109
		} elseif ( $this->is_android_tablet() ) { // keep this check before the android rule
110
			return 'android_tablet';
111
		} elseif ( $this->is_android() ) {
112
			return 'android';
113
		} elseif ( $this->is_blackberry_10() ) {
114
			return 'blackberry_10';
115
		} elseif ( $this->is_blackbeberry() ) {
116
			return 'blackberry';
117
		} elseif ( $this->is_WindowsPhone7() ) {
118
			return 'win7';
119
		} elseif ( $this->is_windows_phone_8() ) {
120
			return 'winphone8';
121
		} elseif ( $this->is_opera_mini() ) {
122
			return 'opera-mini';
123
		} elseif ( $this->is_opera_mini_dumb() ) {
124
			return 'opera-mini-dumb';
125
		} elseif ( $this->is_opera_mobile() ) {
126
			return 'opera-mobi';
127
		} elseif ( $this->is_blackberry_tablet() ) {
128
			return 'blackberry_tablet';
129
		} elseif ( $this->is_kindle_fire() ) {
130
			return 'kindle-fire';
131
		} elseif ( $this->is_PalmWebOS() ) {
132
			return 'webos';
133
		} elseif ( $this->is_S60_OSSBrowser() ) {
134
			return 'series60';
135
		} elseif ( $this->is_firefox_os() ) {
136
			return 'firefoxOS';
137
		} elseif ( $this->is_firefox_mobile() ) {
138
			return 'firefox_mobile';
139
		} elseif ( $this->is_MaemoTablet() ) {
140
			return 'maemo';
141
		} elseif ( $this->is_MeeGo() ) {
142
			return 'meego';
143
		} elseif ( $this->is_TouchPad() ) {
144
			return 'hp_tablet';
145
		} elseif ( $this->is_facebook_for_iphone() ) {
146
			return 'facebook-for-iphone';
147
		} elseif ( $this->is_facebook_for_ipad() ) {
148
			return 'facebook-for-ipad';
149
		} elseif ( $this->is_twitter_for_iphone() ) {
150
			return 'twitter-for-iphone';
151
		} elseif ( $this->is_twitter_for_ipad() ) {
152
			return 'twitter-for-ipad';
153
		} elseif ( $this->is_wordpress_for_ios() ) {
154
			return 'ios-app';
155
		} elseif ( $this->is_iphone_or_ipod( 'iphone-not-safari' ) ) {
156
			return 'iphone-unknown';
157
		} elseif ( $this->is_ipad( 'ipad-not-safari' ) ) {
158
			return 'ipad-unknown';
159
		} elseif ( $this->is_Nintendo_3DS() ) {
160
			return 'nintendo-3ds';
161
		} else {
162
			$agent       = strtolower( $_SERVER['HTTP_USER_AGENT'] );
163
			$dumb_agents = $this->dumb_agents;
164
			foreach ( $dumb_agents as $dumb_agent ) {
165
				if ( false !== strpos( $agent, $dumb_agent ) ) {
166
					return $dumb_agent;
167
				}
168
			}
169
		}
170
171
		return false;
172
	}
173
174
	/**
175
	 * This method detects the mobile device's platform. All return strings are from the class constants.
176
	 * Note that this function returns the platform name, not the UA name/type. You should use a different function
177
	 * if you need to test the UA capabilites.
178
	 *
179
	 * @return string Name of the platform, false otherwise.
180
	 */
181
	public function get_platform() {
182
		if ( isset( $this->_platform ) ) {
183
				return $this->_platform;
184
		}
185
186
		if ( strpos( $this->useragent, 'windows phone' ) !== false ) {
187
				$this->_platform = self::PLATFORM_WINDOWS;
188
		} elseif ( strpos( $this->useragent, 'windows ce' ) !== false ) {
189
			$this->_platform = self::PLATFORM_WINDOWS;
190
		} elseif ( strpos( $this->useragent, 'ipad' ) !== false ) {
191
			$this->_platform = self::PLATFORM_IPAD;
192
		} elseif ( strpos( $this->useragent, 'ipod' ) !== false ) {
193
			$this->_platform = self::PLATFORM_IPOD;
194
		} elseif ( strpos( $this->useragent, 'iphone' ) !== false ) {
195
			$this->_platform = self::PLATFORM_IPHONE;
196
		} elseif ( strpos( $this->useragent, 'android' ) !== false ) {
197
			if ( $this->is_android_tablet() ) {
198
				$this->_platform = self::PLATFORM_ANDROID_TABLET;
199
			} else {
200
				$this->_platform = self::PLATFORM_ANDROID;
201
			}
202
		} elseif ( $this->is_kindle_fire() ) {
203
			$this->_platform = self::PLATFORM_ANDROID_TABLET;
204
		} elseif ( $this->is_blackberry_10() ) {
205
			$this->_platform = self::PLATFORM_BLACKBERRY_10;
206
		} elseif ( strpos( $this->useragent, 'blackberry' ) !== false ) {
207
			$this->_platform = self::PLATFORM_BLACKBERRY;
208
		} elseif ( $this->is_blackberry_tablet() ) {
209
			$this->_platform = self::PLATFORM_BLACKBERRY;
210
		} elseif ( $this->is_symbian_platform() ) {
211
			$this->_platform = self::PLATFORM_SYMBIAN;
212
		} elseif ( $this->is_symbian_s40_platform() ) {
213
			$this->_platform = self::PLATFORM_SYMBIAN_S40;
214
		} elseif ( $this->is_J2ME_platform() ) {
215
			$this->_platform = self::PLATFORM_J2ME_MIDP;
216
		} elseif ( $this->is_firefox_os() ) {
217
			$this->_platform = self::PLATFORM_FIREFOX_OS;
218
		} else {
219
			$this->_platform = false;
220
		}
221
222
		return $this->_platform;
223
	}
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 View Code Duplication
		} elseif ( $this->is_android() ) {
240
			$this->matched_agent       = 'android';
241
			$this->isTierIphone        = true;
242
			$this->isTierRichCss       = false;
243
			$this->isTierGenericMobile = false;
244
		} elseif ( $this->is_windows_phone_8() ) {
245
			$this->matched_agent       = 'winphone8';
246
			$this->isTierIphone        = true;
247
			$this->isTierRichCss       = false;
248
			$this->isTierGenericMobile = false;
249 View Code Duplication
		} elseif ( $this->is_WindowsPhone7() ) {
250
			$this->matched_agent       = 'win7';
251
			$this->isTierIphone        = true;
252
			$this->isTierRichCss       = false;
253
			$this->isTierGenericMobile = false;
254
		} elseif ( $this->is_blackberry_10() ) {
255
			$this->matched_agent       = 'blackberry-10';
256
			$this->isTierIphone        = true;
257
			$this->isTierRichCss       = false;
258
			$this->isTierGenericMobile = false;
259 View Code Duplication
		} elseif ( $this->is_blackbeberry() && $this->detect_blackberry_browser_version() == 'blackberry-webkit' ) {
260
			$this->matched_agent       = 'blackberry-webkit';
261
			$this->isTierIphone        = true;
262
			$this->isTierRichCss       = false;
263
			$this->isTierGenericMobile = false;
264
		} elseif ( $this->is_blackberry_tablet() ) {
265
			$this->matched_agent       = 'blackberry_tablet';
266
			$this->isTierIphone        = true;
267
			$this->isTierRichCss       = false;
268
			$this->isTierGenericMobile = false;
269 View Code Duplication
		} elseif ( $this->is_PalmWebOS() ) {
270
			$this->matched_agent       = 'webos';
271
			$this->isTierIphone        = true;
272
			$this->isTierRichCss       = false;
273
			$this->isTierGenericMobile = false;
274
		} elseif ( $this->is_TouchPad() ) {
275
			$this->matched_agent       = 'hp_tablet';
276
			$this->isTierIphone        = true;
277
			$this->isTierRichCss       = false;
278
			$this->isTierGenericMobile = false;
279 View Code Duplication
		} elseif ( $this->is_firefox_os() ) {
280
			$this->matched_agent       = 'firefoxOS';
281
			$this->isTierIphone        = true;
282
			$this->isTierRichCss       = false;
283
			$this->isTierGenericMobile = false;
284
		} elseif ( $this->is_firefox_mobile() ) {
285
			$this->matched_agent       = 'fennec';
286
			$this->isTierIphone        = true;
287
			$this->isTierRichCss       = false;
288
			$this->isTierGenericMobile = false;
289 View Code Duplication
		} elseif ( $this->is_opera_mobile() ) {
290
			$this->matched_agent       = 'opera-mobi';
291
			$this->isTierIphone        = true;
292
			$this->isTierRichCss       = false;
293
			$this->isTierGenericMobile = false;
294
		} elseif ( $this->is_MaemoTablet() ) {
295
			$this->matched_agent       = 'maemo';
296
			$this->isTierIphone        = true;
297
			$this->isTierRichCss       = false;
298
			$this->isTierGenericMobile = false;
299 View Code Duplication
		} elseif ( $this->is_MeeGo() ) {
300
			$this->matched_agent       = 'meego';
301
			$this->isTierIphone        = true;
302
			$this->isTierRichCss       = false;
303
			$this->isTierGenericMobile = false;
304
		} elseif ( $this->is_kindle_touch() ) {
305
			$this->matched_agent       = 'kindle-touch';
306
			$this->isTierIphone        = true;
307
			$this->isTierRichCss       = false;
308
			$this->isTierGenericMobile = false;
309 View Code Duplication
		} elseif ( $this->is_Nintendo_3DS() ) {
310
			$this->matched_agent       = 'nintendo-3ds';
311
			$this->isTierIphone        = true;
312
			$this->isTierRichCss       = false;
313
			$this->isTierGenericMobile = false;
314
		} else {
315
			$this->isTierIphone = false;
316
		}
317
		return $this->isTierIphone;
318
	}
319
320
	/*
321
		* This method detects for UA which are likely to be capable
322
		* but may not necessarily support JavaScript.
323
		* Excludes all iPhone Tier UA.
324
		*
325
		*/
326
	function isTierRichCss() {
327
		if ( isset( $this->isTierRichCss ) ) {
328
			return $this->isTierRichCss;
329
		}
330
		if ( $this->isTierIphone() ) {
331
			return false;
332
		}
333
334
		// The following devices are explicitly ok.
335
		if ( $this->is_S60_OSSBrowser() ) {
336
			$this->matched_agent       = 'series60';
337
			$this->isTierIphone        = false;
338
			$this->isTierRichCss       = true;
339
			$this->isTierGenericMobile = false;
340 View Code Duplication
		} elseif ( $this->is_opera_mini() ) {
341
			$this->matched_agent       = 'opera-mini';
342
			$this->isTierIphone        = false;
343
			$this->isTierRichCss       = true;
344
			$this->isTierGenericMobile = false;
345
		} elseif ( $this->is_blackbeberry() ) {
346
			$detectedDevice = $this->detect_blackberry_browser_version();
347
			if ( $detectedDevice === 'blackberry-5' || $detectedDevice == 'blackberry-4.7' || $detectedDevice === 'blackberry-4.6' ) {
348
				$this->matched_agent       = $detectedDevice;
349
				$this->isTierIphone        = false;
350
				$this->isTierRichCss       = true;
351
				$this->isTierGenericMobile = false;
352
			}
353
		} else {
354
			$this->isTierRichCss = false;
355
		}
356
357
		return $this->isTierRichCss;
358
	}
359
360
	// Detects if the user is using a tablet.
361
	// props Corey Gilmore, BGR.com
362
	function is_tablet() {
363
		return ( 0 // never true, but makes it easier to manage our list of tablet conditions
364
				|| self::is_ipad()
365
				|| self::is_android_tablet()
366
				|| self::is_blackberry_tablet()
367
				|| self::is_kindle_fire()
368
				|| self::is_MaemoTablet()
369
				|| self::is_TouchPad()
370
		);
371
	}
372
373
	/*
374
		*  Detects if the current UA is the default iPhone or iPod Touch Browser.
375
		*
376
		*  DEPRECATED: use is_iphone_or_ipod
377
		*
378
		*/
379
	function is_iphoneOrIpod() {
380
381
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
382
			return false;
383
		}
384
385
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
386 View Code Duplication
		if ( ( strpos( $ua, 'iphone' ) !== false ) || ( strpos( $ua, 'ipod' ) !== false ) ) {
387
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
388
				return false;
389
			} else {
390
				return true;
391
			}
392
		} else {
393
			return false;
394
		}
395
	}
396
397
398
	/*
399
		*  Detects if the current UA is iPhone Mobile Safari or another iPhone or iPod Touch Browser.
400
		*
401
		*  They type can check for any iPhone, an iPhone using Safari, or an iPhone using something other than Safari.
402
		*
403
		*  Note: If you want to check for Opera mini, Opera mobile or Firefox mobile (or any 3rd party iPhone browser),
404
		*  you should put the check condition before the check for 'iphone-any' or 'iphone-not-safari'.
405
		*  Otherwise those browsers will be 'catched' by the iphone string.
406
		*
407
		*/
408
	static function is_iphone_or_ipod( $type = 'iphone-any' ) {
409
410
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
411
			return false;
412
		}
413
414
		$ua        = strtolower( $_SERVER['HTTP_USER_AGENT'] );
415
		$is_iphone = ( strpos( $ua, 'iphone' ) !== false ) || ( strpos( $ua, 'ipod' ) !== false );
416
		$is_safari = ( false !== strpos( $ua, 'safari' ) );
417
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
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
		}
438
439
		if ( self::is_iphone_or_ipod( 'iphone-safari' ) === false ) {
440
			return false;
441
		}
442
443
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
444
445
		if ( strpos( $ua, 'crios/' ) !== false ) {
446
			return true;
447
		} else {
448
			return false;
449
		}
450
	}
451
452
453
	/*
454
		*  Detects if the current UA is Twitter for iPhone
455
		*
456
		* 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
457
		* 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
458
		*
459
		*/
460
	static function is_twitter_for_iphone() {
461
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
462
			return false;
463
		}
464
465
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
466
467
		if ( strpos( $ua, 'ipad' ) !== false ) {
468
			return false;
469
		}
470
471
		if ( strpos( $ua, 'twitter for iphone' ) !== false ) {
472
			return true;
473
		} else {
474
			return false;
475
		}
476
	}
477
478
	/*
479
		* Detects if the current UA is Twitter for iPad
480
		*
481
		* 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
482
		* 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
483
		*
484
		*/
485
	static function is_twitter_for_ipad() {
486
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
487
			return false;
488
		}
489
490
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
491
492
		if ( strpos( $ua, 'twitter for ipad' ) !== false ) {
493
			return true;
494 View Code Duplication
		} elseif ( strpos( $ua, 'ipad' ) !== false && strpos( $ua, 'twitter for iphone' ) !== false ) {
495
			return true;
496
		} else {
497
			return false;
498
		}
499
	}
500
501
502
	/*
503
		* Detects if the current UA is Facebook for iPhone
504
		* - Facebook 4020.0 (iPhone; iPhone OS 5.0.1; fr_FR)
505
		* - 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]
506
		* - 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]
507
		*/
508
	static function is_facebook_for_iphone() {
509
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
510
			return false;
511
		}
512
513
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
514
515
		if ( false === strpos( $ua, 'iphone' ) ) {
516
			return false;
517
		}
518
519
		if ( false !== strpos( $ua, 'facebook' ) && false === strpos( $ua, 'ipad' ) ) {
520
			return true;
521
		} elseif ( false !== strpos( $ua, 'fbforiphone' ) && false === strpos( $ua, 'tablet' ) ) {
522
			return true;
523
		} elseif ( false !== strpos( $ua, 'fban/fbios;' ) && false === strpos( $ua, 'tablet' ) ) { // FB app v5.0 or higher
524
			return true;
525
		} else {
526
			return false;
527
		}
528
	}
529
530
	/*
531
		* Detects if the current UA is Facebook for iPad
532
		* - Facebook 4020.0 (iPad; iPhone OS 5.0.1; en_US)
533
		* - 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]
534
		* - 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]
535
		*/
536 View Code Duplication
	static function is_facebook_for_ipad() {
537
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
538
			return false;
539
		}
540
541
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
542
543
		if ( false === strpos( $ua, 'ipad' ) ) {
544
			return false;
545
		}
546
547
		if ( false !== strpos( $ua, 'facebook' ) || false !== strpos( $ua, 'fbforiphone' ) || false !== strpos( $ua, 'fban/fbios;' ) ) {
548
			return true;
549
		} else {
550
			return false;
551
		}
552
	}
553
554
	/*
555
		*  Detects if the current UA is WordPress for iOS
556
		*/
557
	static function is_wordpress_for_ios() {
558
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
559
			return false;
560
		}
561
562
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
563
		if ( false !== strpos( $ua, 'wp-iphone' ) ) {
564
			return true;
565
		} else {
566
			return false;
567
		}
568
	}
569
570
	/*
571
		* Detects if the current device is an iPad.
572
		* They type can check for any iPad, an iPad using Safari, or an iPad using something other than Safari.
573
		*
574
		* Note: If you want to check for Opera mini, Opera mobile or Firefox mobile (or any 3rd party iPad browser),
575
		* you should put the check condition before the check for 'iphone-any' or 'iphone-not-safari'.
576
		* Otherwise those browsers will be 'catched' by the ipad string.
577
		*
578
	*/
579
	static function is_ipad( $type = 'ipad-any' ) {
580
581
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
582
			return false;
583
		}
584
585
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
586
587
		$is_ipad   = ( false !== strpos( $ua, 'ipad' ) );
588
		$is_safari = ( false !== strpos( $ua, 'safari' ) );
589
590
		if ( 'ipad-safari' == $type ) {
591
			return $is_ipad && $is_safari;
592
		} elseif ( 'ipad-not-safari' == $type ) {
593
			return $is_ipad && ! $is_safari;
594
		} else {
595
			return $is_ipad;
596
		}
597
	}
598
599
	/*
600
		* Detects if the current browser is Firefox Mobile (Fennec)
601
		*
602
		* http://www.useragentstring.com/pages/Fennec/
603
		* Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.1.1) Gecko/20110415 Firefox/4.0.2pre Fennec/4.0.1
604
		* Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1b2pre) Gecko/20081015 Fennec/1.0a1
605
		*/
606
	static function is_firefox_mobile() {
607
608
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
609
			return false;
610
		}
611
612
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
613
614
		if ( strpos( $ua, 'fennec' ) !== false ) {
615
			return true;
616
		} else {
617
			return false;
618
		}
619
	}
620
621
	/*
622
		* Detects if the current browser is Firefox for desktop
623
		*
624
		* https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox
625
		* Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion
626
		* The platform section will include 'Mobile' for phones and 'Tablet' for tablets.
627
		*
628
		*/
629 View Code Duplication
	static function is_firefox_desktop() {
630
631
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
632
			return false;
633
		}
634
635
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
636
637
		if ( false !== strpos( $ua, 'firefox' ) && false === strpos( $ua, 'mobile' ) && false === strpos( $ua, 'tablet' ) ) {
638
			return true;
639
		} else {
640
			return false;
641
		}
642
	}
643
644
	/*
645
		* Detects if the current browser is FirefoxOS Native browser
646
	*
647
	* Mozilla/5.0 (Mobile; rv:14.0) Gecko/14.0 Firefox/14.0
648
	*
649
	*/
650 View Code Duplication
	static function is_firefox_os() {
651
652
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
653
			return false;
654
		}
655
656
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
657
658
		if ( strpos( $ua, 'mozilla' ) !== false && strpos( $ua, 'mobile' ) !== false && strpos( $ua, 'gecko' ) !== false && strpos( $ua, 'firefox' ) !== false ) {
659
			return true;
660
		} else {
661
			return false;
662
		}
663
	}
664
665
666
	/*
667
		* Detects if the current browser is Opera Mobile
668
		*
669
		* What is the difference between Opera Mobile and Opera Mini?
670
		* - Opera Mobile is a full Internet browser for mobile devices.
671
		* - Opera Mini always uses a transcoder to convert the page for a small display.
672
		* (it uses Opera advanced server compression technology to compress web content before it gets to a device.
673
		*  The rendering engine is on Opera's server.)
674
		*
675
		* Opera/9.80 (Windows NT 6.1; Opera Mobi/14316; U; en) Presto/2.7.81 Version/11.00"
676
		* Opera/9.50 (Nintendo DSi; Opera/507; U; en-US)
677
		*/
678 View Code Duplication
	static function is_opera_mobile() {
679
680
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
681
			return false;
682
		}
683
684
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
685
686
		if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mobi' ) !== false ) {
687
			return true;
688
		} elseif ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'nintendo dsi' ) !== false ) {
689
			return true;
690
		} else {
691
			return false;
692
		}
693
	}
694
695
696
	/*
697
		* Detects if the current browser is Opera Mini
698
		*
699
		* Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)
700
		* Opera/9.80 (Android;Opera Mini/6.0.24212/24.746 U;en) Presto/2.5.25 Version/10.5454
701
		* Opera/9.80 (iPhone; Opera Mini/5.0.019802/18.738; U; en) Presto/2.4.15
702
		* Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
703
		* Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
704
		* Opera/9.80 (Series 60; Opera Mini/5.1.22783/23.334; U; en) Presto/2.5.25 Version/10.54
705
		* Opera/9.80 (BlackBerry; Opera Mini/5.1.22303/22.387; U; en) Presto/2.5.25 Version/10.54
706
		*
707
		*/
708
	static function is_opera_mini() {
709
710
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
711
			return false;
712
		}
713
714
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
715
716 View Code Duplication
		if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mini' ) !== false ) {
717
			return true;
718
		} else {
719
			return false;
720
		}
721
	}
722
723
	/*
724
		* Detects if the current browser is Opera Mini, but not on a smart device OS(Android, iOS, etc)
725
		* Used to send users on dumb devices to m.wor
726
		*/
727
	static function is_opera_mini_dumb() {
728
729
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
730
			return false;
731
		}
732
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
733
734
		if ( self::is_opera_mini() ) {
735
			if ( strpos( $ua, 'android' ) !== false || strpos( $ua, 'iphone' ) !== false || strpos( $ua, 'ipod' ) !== false
736
			|| strpos( $ua, 'ipad' ) !== false || strpos( $ua, 'blackberry' ) !== false ) {
737
				return false;
738
			} else {
739
				return true;
740
			}
741
		} else {
742
			return false;
743
		}
744
	}
745
746
	/*
747
		* Detects if the current browser is Opera Mobile or Mini.
748
		* DEPRECATED: use is_opera_mobile or is_opera_mini
749
		*
750
		* Opera Mini 5 Beta: Opera/9.80 (J2ME/MIDP; Opera Mini/5.0.15650/756; U; en) Presto/2.2.0
751
		* Opera Mini 8: Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)
752
		*/
753
	static function is_OperaMobile() {
754
		_deprecated_function( __FUNCTION__, 'always', 'is_opera_mini() or is_opera_mobile()' );
755
756
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
757
			return false;
758
		}
759
760
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
761
762
		if ( strpos( $ua, 'opera' ) !== false ) {
763 View Code Duplication
			if ( ( strpos( $ua, 'mini' ) !== false ) || ( strpos( $ua, 'mobi' ) !== false ) ) {
764
				return true;
765
			} else {
766
				return false;
767
			}
768
		} else {
769
			return false;
770
		}
771
	}
772
773
	/*
774
		* Detects if the current browser is a Windows Phone 7 device.
775
		* ex: Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; LG; GW910)
776
		*/
777 View Code Duplication
	static function is_WindowsPhone7() {
778
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
779
			return false;
780
		}
781
782
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
783
784
		if ( false === strpos( $ua, 'windows phone os 7' ) ) {
785
			return false;
786
		} else {
787
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
788
				return false;
789
			} else {
790
				return true;
791
			}
792
		}
793
	}
794
795
	/*
796
		* Detects if the current browser is a Windows Phone 8 device.
797
		* ex: Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; ARM; Touch; IEMobile/10.0; <Manufacturer>; <Device> [;<Operator>])
798
		*/
799
	static function is_windows_phone_8() {
800
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
801
			return false;
802
		}
803
804
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
805
		if ( strpos( $ua, 'windows phone 8' ) === false ) {
806
			return false;
807
		} else {
808
			return true;
809
		}
810
	}
811
812
813
	/*
814
		* Detects if the current browser is on a Palm device running the new WebOS. This EXCLUDES TouchPad.
815
		*
816
		* 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
817
		* 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
818
		*
819
		*/
820 View Code Duplication
	static function is_PalmWebOS() {
821
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
822
			return false;
823
		}
824
825
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
826
827
		if ( false === strpos( $ua, 'webos' ) ) {
828
			return false;
829
		} else {
830
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
831
				return false;
832
			} else {
833
				return true;
834
			}
835
		}
836
	}
837
838
	/*
839
		* Detects if the current browser is the HP TouchPad default browser. This excludes phones wt WebOS.
840
		*
841
		* 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
842
		* 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
843
		*
844
		*/
845
	static function is_TouchPad() {
846
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
847
			return false;
848
		}
849
850
		$http_user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
851
		if ( false !== strpos( $http_user_agent, 'hp-tablet' ) || false !== strpos( $http_user_agent, 'hpwos' ) || false !== strpos( $http_user_agent, 'touchpad' ) ) {
852
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
853
				return false;
854
			} else {
855
				return true;
856
			}
857
		} else {
858
			return false;
859
		}
860
	}
861
862
863
	/*
864
		* Detects if the current browser is the Series 60 Open Source Browser.
865
		*
866
		* 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
867
		*
868
		* 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
869
		*
870
		* 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
871
		*
872
		*/
873
	static function is_S60_OSSBrowser() {
874
875
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
876
			return false;
877
		}
878
879
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
880
		if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
881
			return false;
882
		}
883
884
		$pos_webkit = strpos( $agent, 'webkit' );
885
		if ( $pos_webkit !== false ) {
886
			// First, test for WebKit, then make sure it's either Symbian or S60.
887
			if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
888
					return true;
889
			} else {
890
				return false;
891
			}
892 View Code Duplication
		} elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent, 'series60' ) !== false ) {
893
			return true;
894
		} elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent, 'series60' ) !== false ) {
895
			return true;
896
		}
897
898
		return false;
899
	}
900
901
	/*
902
		*
903
		* Detects if the device platform is the Symbian Series 60.
904
		*
905
		*/
906
	static function is_symbian_platform() {
907
908
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
909
			return false;
910
		}
911
912
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
913
914
		$pos_webkit = strpos( $agent, 'webkit' );
915
		if ( $pos_webkit !== false ) {
916
			// First, test for WebKit, then make sure it's either Symbian or S60.
917
			if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
918
					return true;
919
			} else {
920
				return false;
921
			}
922 View Code Duplication
		} elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent, 'series60' ) !== false ) {
923
			return true;
924
		} elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent, 'series60' ) !== false ) {
925
			return true;
926
		} elseif ( strpos( $agent, 'opera mini' ) !== false ) {
927
			if ( strpos( $agent, 'symbianos' ) !== false || strpos( $agent, 'symbos' ) !== false || strpos( $agent, 'series 60' ) !== false ) {
928
				return true;
929
			}
930
		}
931
932
		return false;
933
	}
934
935
	/*
936
		*
937
		* Detects if the device platform is the Symbian Series 40.
938
		* Nokia Browser for Series 40 is a proxy based browser, previously known as Ovi Browser.
939
		* This browser will report 'NokiaBrowser' in the header, however some older version will also report 'OviBrowser'.
940
		*
941
		*/
942 View Code Duplication
	static function is_symbian_s40_platform() {
943
944
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
945
			return false;
946
		}
947
948
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
949
950
		if ( strpos( $agent, 'series40' ) !== false ) {
951
			if ( strpos( $agent, 'nokia' ) !== false || strpos( $agent, 'ovibrowser' ) !== false || strpos( $agent, 'nokiabrowser' ) !== false ) {
952
				return true;
953
			}
954
		}
955
956
		return false;
957
	}
958
959 View Code Duplication
	static function is_J2ME_platform() {
960
961
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
962
			return false;
963
		}
964
965
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
966
967
		if ( strpos( $agent, 'j2me/midp' ) !== false ) {
968
			return true;
969
		} elseif ( strpos( $agent, 'midp' ) !== false && strpos( $agent, 'cldc' ) ) {
970
			return true;
971
		}
972
973
		return false;
974
	}
975
976
977
	/*
978
		* Detects if the current UA is on one of the Maemo-based Nokia Internet Tablets.
979
		*/
980
	static function is_MaemoTablet() {
981
982
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
983
			return false;
984
		}
985
986
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
987
988
		$pos_maemo = strpos( $agent, 'maemo' );
989
		if ( $pos_maemo === false ) {
990
			return false;
991
		}
992
993
		// Must be Linux + Tablet, or else it could be something else.
994 View Code Duplication
		if ( strpos( $agent, 'tablet' ) !== false && strpos( $agent, 'linux' ) !== false ) {
995
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
996
				return false;
997
			} else {
998
				return true;
999
			}
1000
		} else {
1001
			return false;
1002
		}
1003
	}
1004
1005
	/*
1006
		* Detects if the current UA is a MeeGo device (Nokia Smartphone).
1007
		*/
1008 View Code Duplication
	static function is_MeeGo() {
1009
1010
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1011
			return false;
1012
		}
1013
1014
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1015
1016
		if ( false === strpos( $ua, 'meego' ) ) {
1017
			return false;
1018
		} else {
1019
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1020
				return false;
1021
			} else {
1022
				return true;
1023
			}
1024
		}
1025
	}
1026
1027
1028
	/*
1029
		is_webkit() can be used to check the User Agent for an webkit generic browser
1030
		*/
1031 View Code Duplication
	static function is_webkit() {
1032
1033
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1034
			return false;
1035
		}
1036
1037
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1038
1039
		$pos_webkit = strpos( $agent, 'webkit' );
1040
1041
		if ( $pos_webkit !== false ) {
1042
			return true;
1043
		} else {
1044
			return false;
1045
		}
1046
	}
1047
1048
	/**
1049
	 * Detects if the current browser is the Native Android browser.
1050
	 *
1051
	 * @return boolean true if the browser is Android otherwise false
1052
	 */
1053 View Code Duplication
	static function is_android() {
1054
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1055
			return false;
1056
		}
1057
1058
		$agent       = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1059
		$pos_android = strpos( $agent, 'android' );
1060
		if ( $pos_android !== false ) {
1061
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1062
				return false;
1063
			} else {
1064
				return true;
1065
			}
1066
		} else {
1067
			return false;
1068
		}
1069
	}
1070
1071
1072
	/**
1073
	 * Detects if the current browser is the Native Android Tablet browser.
1074
	 *  Assumes 'Android' should be in the user agent, but not 'mobile'
1075
	 *
1076
	 * @return boolean true if the browser is Android and not 'mobile' otherwise false
1077
	 */
1078
	static function is_android_tablet() {
1079
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1080
			return false;
1081
		}
1082
1083
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1084
1085
		$pos_android      = strpos( $agent, 'android' );
1086
		$pos_mobile       = strpos( $agent, 'mobile' );
1087
		$post_android_app = strpos( $agent, 'wp-android' );
1088
1089
		if ( false !== $pos_android && false === $pos_mobile && false === $post_android_app ) {
1090
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1091
				return false;
1092
			} else {
1093
				return true;
1094
			}
1095
		} else {
1096
			return false;
1097
		}
1098
	}
1099
1100
	/**
1101
	 * Detects if the current browser is the Kindle Fire Native browser.
1102
	 *
1103
	 * 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
1104
	 * 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
1105
	 *
1106
	 * @return boolean true if the browser is Kindle Fire Native browser otherwise false
1107
	 */
1108
	static function is_kindle_fire() {
1109
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1110
			return false;
1111
		}
1112
1113
		$agent        = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1114
		$pos_silk     = strpos( $agent, 'silk/' );
1115
		$pos_silk_acc = strpos( $agent, 'silk-accelerated=' );
1116
		if ( $pos_silk !== false && $pos_silk_acc !== false ) {
1117
			return true;
1118
		} else {
1119
			return false;
1120
		}
1121
	}
1122
1123
1124
	/**
1125
	 * Detects if the current browser is the Kindle Touch Native browser
1126
	 *
1127
	 * 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+
1128
	 *
1129
	 * @return boolean true if the browser is Kindle monochrome Native browser otherwise false
1130
	 */
1131 View Code Duplication
	static function is_kindle_touch() {
1132
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1133
			return false;
1134
		}
1135
		$agent            = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1136
		$pos_kindle_touch = strpos( $agent, 'kindle/3.0+' );
1137
		if ( false !== $pos_kindle_touch && false === self::is_kindle_fire() ) {
1138
			return true;
1139
		} else {
1140
			return false;
1141
		}
1142
	}
1143
1144
1145
	// Detect if user agent is the WordPress.com Windows 8 app (used ONLY on the custom oauth stylesheet)
1146 View Code Duplication
	static function is_windows8_auth() {
1147
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1148
			return false;
1149
		}
1150
1151
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1152
		$pos   = strpos( $agent, 'msauthhost' );
1153
		if ( false !== $pos ) {
1154
			return true;
1155
		} else {
1156
			return false;
1157
		}
1158
	}
1159
1160
	// Detect if user agent is the WordPress.com Windows 8 app.
1161 View Code Duplication
	static function is_wordpress_for_win8() {
1162
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1163
			return false;
1164
		}
1165
1166
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1167
		$pos   = strpos( $agent, 'wp-windows8' );
1168
		if ( false !== $pos ) {
1169
			return true;
1170
		} else {
1171
			return false;
1172
		}
1173
	}
1174
1175
1176
	// Detect if user agent is the WordPress.com Desktop app.
1177
	static function is_wordpress_desktop_app() {
1178
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1179
			return false;
1180
		}
1181
1182
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1183
		$pos   = strpos( $agent, 'WordPressDesktop' );
1184
		if ( false !== $pos ) {
1185
			return true;
1186
		} else {
1187
			return false;
1188
		}
1189
	}
1190
1191
1192
	/*
1193
		* is_blackberry_tablet() can be used to check the User Agent for a RIM blackberry tablet
1194
		* The user agent of the BlackBerry® Tablet OS follows a format similar to the following:
1195
		* 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+
1196
		*
1197
		*/
1198
	static function is_blackberry_tablet() {
1199
1200
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1201
			return false;
1202
		}
1203
1204
		$agent          = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1205
		$pos_playbook   = stripos( $agent, 'PlayBook' );
1206
		$pos_rim_tablet = stripos( $agent, 'RIM Tablet' );
1207
1208
		if ( ( false === $pos_playbook ) || ( false === $pos_rim_tablet ) ) {
1209
			return false;
1210
		} else {
1211
			return true;
1212
		}
1213
	}
1214
1215
	/*
1216
		is_blackbeberry() can be used to check the User Agent for a blackberry device
1217
		Note that opera mini on BB matches this rule.
1218
		*/
1219 View Code Duplication
	static function is_blackbeberry() {
1220
1221
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1222
			return false;
1223
		}
1224
1225
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1226
1227
		$pos_blackberry = strpos( $agent, 'blackberry' );
1228
		if ( $pos_blackberry !== false ) {
1229
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1230
				return false;
1231
			} else {
1232
				return true;
1233
			}
1234
		} else {
1235
			return false;
1236
		}
1237
	}
1238
1239
	/*
1240
		is_blackberry_10() can be used to check the User Agent for a BlackBerry 10 device.
1241
	*/
1242
	static function is_blackberry_10() {
1243
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1244
		return ( strpos( $agent, 'bb10' ) !== false ) && ( strpos( $agent, 'mobile' ) !== false );
1245
	}
1246
1247
	/**
1248
	 * Retrieve the blackberry OS version.
1249
	 *
1250
	 * Return strings are from the following list:
1251
	 * - blackberry-10
1252
	 * - blackberry-7
1253
	 * - blackberry-6
1254
	 * - blackberry-torch //only the first edition. The 2nd edition has the OS7 onboard and doesn't need any special rule.
1255
	 * - blackberry-5
1256
	 * - blackberry-4.7
1257
	 * - blackberry-4.6
1258
	 * - blackberry-4.5
1259
	 *
1260
	 * @return string Version of the BB OS.
1261
	 * If version is not found, get_blackbeberry_OS_version will return boolean false.
1262
	 */
1263
	static function get_blackbeberry_OS_version() {
1264
1265
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1266
			return false;
1267
		}
1268
1269
		if ( self::is_blackberry_10() ) {
1270
			return 'blackberry-10';
1271
		}
1272
1273
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1274
1275
		$pos_blackberry = stripos( $agent, 'blackberry' );
1276
		if ( false === $pos_blackberry ) {
1277
			// not a blackberry device
1278
			return false;
1279
		}
1280
1281
		// blackberry devices OS 6.0 or higher
1282
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9670; en) AppleWebKit/534.3+ (KHTML, like Gecko) Version/6.0.0.286 Mobile Safari/534.3+
1283
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+
1284
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0 Mobile Safari/534.11+
1285
		$pos_webkit = stripos( $agent, 'webkit' );
1286
		if ( false !== $pos_webkit ) {
1287
			// detected blackberry webkit browser
1288
			$pos_torch = stripos( $agent, 'BlackBerry 9800' );
1289
			if ( false !== $pos_torch ) {
1290
				return 'blackberry-torch'; // match the torch first edition. the 2nd edition should use the OS7 and doesn't need any special rule
1291
			} else {
1292
				// detecting the BB OS version for devices running OS 6.0 or higher
1293
				if ( preg_match( '#Version\/([\d\.]+)#i', $agent, $matches ) ) {
1294
					$version     = $matches[1];
1295
					$version_num = explode( '.', $version );
1296
					if ( false === is_array( $version_num ) || count( $version_num ) <= 1 ) {
1297
						return 'blackberry-6'; // not a BB device that match our rule.
1298
					} else {
1299
						return 'blackberry-' . $version_num[0];
1300
					}
1301
				} else {
1302
					// if doesn't match returns the minimun version with a webkit browser. we should never fall here.
1303
					return 'blackberry-6'; // not a BB device that match our rule.
1304
				}
1305
			}
1306
		}
1307
1308
		// blackberry devices <= 5.XX
1309
		// BlackBerry9000/5.0.0.93 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/179
1310
		if ( preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
1311
			$version = $matches[1];
1312
		} else {
1313
			return false; // not a BB device that match our rule.
1314
		}
1315
1316
		$version_num = explode( '.', $version );
1317
1318 View Code Duplication
		if ( is_array( $version_num ) === false || count( $version_num ) <= 1 ) {
1319
			return false;
1320
		}
1321
		if ( $version_num[0] == 5 ) {
1322
			return 'blackberry-5';
1323
		} elseif ( $version_num[0] == 4 && $version_num[1] == 7 ) {
1324
			return 'blackberry-4.7';
1325
		} elseif ( $version_num[0] == 4 && $version_num[1] == 6 ) {
1326
			return 'blackberry-4.6';
1327
		} elseif ( $version_num[0] == 4 && $version_num[1] == 5 ) {
1328
			return 'blackberry-4.5';
1329
		} else {
1330
			return false;
1331
		}
1332
1333
	}
1334
1335
	/**
1336
	 * Retrieve the blackberry browser version.
1337
	 *
1338
	 * Return string are from the following list:
1339
	 * - blackberry-10
1340
	 * - blackberry-webkit
1341
	 * - blackberry-5
1342
	 * - blackberry-4.7
1343
	 * - blackberry-4.6
1344
	 *
1345
	 * @return string Type of the BB browser.
1346
	 * If browser's version is not found, detect_blackbeberry_browser_version will return boolean false.
1347
	 */
1348
	static function detect_blackberry_browser_version() {
1349
1350
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1351
			return false;
1352
		}
1353
1354
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1355
1356
		if ( self::is_blackberry_10() ) {
1357
			return 'blackberry-10';
1358
		}
1359
1360
		$pos_blackberry = strpos( $agent, 'blackberry' );
1361
		if ( false === $pos_blackberry ) {
1362
			// not a blackberry device
1363
			return false;
1364
		}
1365
1366
		$pos_webkit = strpos( $agent, 'webkit' );
1367
1368
		if ( ! ( false === $pos_webkit ) ) {
1369
			return 'blackberry-webkit';
1370
		} else {
1371
			if ( preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
1372
				$version = $matches[1];
1373
			} else {
1374
				return false; // not a BB device that match our rule.
1375
			}
1376
1377
			$version_num = explode( '.', $version );
1378
1379 View Code Duplication
			if ( false === is_array( $version_num ) || count( $version_num ) <= 1 ) {
1380
				return false;
1381
			}
1382
1383
			if ( $version_num[0] == 5 ) {
1384
				return 'blackberry-5';
1385
			} elseif ( $version_num[0] == 4 && $version_num[1] == 7 ) {
1386
				return 'blackberry-4.7';
1387
			} elseif ( $version_num[0] == 4 && $version_num[1] == 6 ) {
1388
				return 'blackberry-4.6';
1389
			} else {
1390
				// A very old BB device is found or this is a BB device that doesn't match our rules.
1391
				return false;
1392
			}
1393
		}
1394
1395
	}
1396
1397
	// Checks if a visitor is coming from one of the WordPress mobile apps
1398
	static function is_mobile_app() {
1399
1400
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1401
			return false;
1402
		}
1403
1404
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1405
1406
		if ( isset( $_SERVER['X_USER_AGENT'] ) && preg_match( '|wp-webos|', $_SERVER['X_USER_AGENT'] ) ) {
1407
			return true; // wp4webos 1.1 or higher
1408
		}
1409
1410
		$app_agents = array( 'wp-android', 'wp-blackberry', 'wp-iphone', 'wp-nokia', 'wp-webos', 'wp-windowsphone' );
1411
		// the mobile reader on iOS has an incorrect UA when loading the reader
1412
		// currently it is the default one provided by the iOS framework which
1413
		// causes problems with 2-step-auth
1414
		// User-Agent	WordPress/3.1.4 CFNetwork/609 Darwin/13.0.0
1415
		$app_agents[] = 'wordpress/3.1';
1416
1417
		foreach ( $app_agents as $app_agent ) {
1418
			if ( false !== strpos( $agent, $app_agent ) ) {
1419
				return true;
1420
			}
1421
		}
1422
		return false;
1423
	}
1424
1425
	/*
1426
		* Detects if the current browser is Nintendo 3DS handheld.
1427
		*
1428
		* example: Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7498.US
1429
		* can differ in language, version and region
1430
		*/
1431
	static function is_Nintendo_3DS() {
1432
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1433
			return false;
1434
		}
1435
1436
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1437
		if ( strpos( $ua, 'nintendo 3ds' ) !== false ) {
1438
			return true;
1439
		}
1440
		return false;
1441
	}
1442
1443
	/**
1444
	 * Was the current request made by a known bot?
1445
	 *
1446
	 * @return boolean
1447
	 */
1448
	static function is_bot() {
1449
		static $is_bot = null;
1450
1451
		if ( is_null( $is_bot ) ) {
1452
			$is_bot = self::is_bot_user_agent( $_SERVER['HTTP_USER_AGENT'] );
1453
		}
1454
1455
		return $is_bot;
1456
	}
1457
1458
	/**
1459
	 * Is the given user-agent a known bot?
1460
	 * 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.
1461
	 *
1462
	 * @param $ua (string) A user-agent string
1463
	 * @return boolean
1464
	 */
1465
	static function is_bot_user_agent( $ua = null ) {
1466
1467
		if ( empty( $ua ) ) {
1468
			return false;
1469
		}
1470
1471
		$bot_agents = array(
1472
			'alexa',
1473
			'altavista',
1474
			'ask jeeves',
1475
			'attentio',
1476
			'baiduspider',
1477
			'bingbot',
1478
			'chtml generic',
1479
			'crawler',
1480
			'fastmobilecrawl',
1481
			'feedfetcher-google',
1482
			'firefly',
1483
			'froogle',
1484
			'gigabot',
1485
			'googlebot',
1486
			'googlebot-mobile',
1487
			'heritrix',
1488
			'httrack',
1489
			'ia_archiver',
1490
			'irlbot',
1491
			'iescholar',
1492
			'infoseek',
1493
			'jumpbot',
1494
			'linkcheck',
1495
			'lycos',
1496
			'mediapartners',
1497
			'mediobot',
1498
			'motionbot',
1499
			'msnbot',
1500
			'mshots',
1501
			'openbot',
1502
			'pss-webkit-request',
1503
			'pythumbnail',
1504
			'scooter',
1505
			'slurp',
1506
			'snapbot',
1507
			'spider',
1508
			'taptubot',
1509
			'technoratisnoop',
1510
			'teoma',
1511
			'twiceler',
1512
			'yahooseeker',
1513
			'yahooysmcm',
1514
			'yammybot',
1515
			'ahrefsbot',
1516
			'pingdom.com_bot',
1517
			'kraken',
1518
			'yandexbot',
1519
			'twitterbot',
1520
			'tweetmemebot',
1521
			'openhosebot',
1522
			'queryseekerspider',
1523
			'linkdexbot',
1524
			'grokkit-crawler',
1525
			'livelapbot',
1526
			'germcrawler',
1527
			'domaintunocrawler',
1528
			'grapeshotcrawler',
1529
			'cloudflare-alwaysonline',
1530
		);
1531
1532
		foreach ( $bot_agents as $bot_agent ) {
1533
			if ( false !== stripos( $ua, $bot_agent ) ) {
1534
				return true;
1535
			}
1536
		}
1537
1538
		return false;
1539
	}
1540
}
1541