Completed
Push — rm/deprecated-code ( 60e936...87d3e1 )
by Jeremy
38:33 queued 26:57
created

User_Agent_Info::is_OperaMobile()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 19
rs 9.3222
c 0
b 0
f 0
1
<?php
2
/**
3
 * User agent detection for Jetpack.
4
 *
5
 * @package automattic/jetpack-device-detection
6
 *
7
 * We don't want to rename public members.
8
 * @phpcs:disable WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
9
 * @phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
10
 * @phpcs:disable WordPress.NamingConventions.ValidVariableName.PropertyNotSnakeCase
11
 * @phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
12
 */
13
14
namespace Automattic\Jetpack\Device_Detection;
15
16
/**
17
 * A class providing device properties detection.
18
 */
19
class User_Agent_Info {
20
21
	/**
22
	 * Provided or fetched User-Agent string.
23
	 *
24
	 * @var string
25
	 */
26
	public $useragent;
27
28
	/**
29
	 * A device group matched user agent, e.g. 'iphone'.
30
	 *
31
	 * @var string
32
	 */
33
	public $matched_agent;
34
35
	/**
36
	 * Stores whether is the iPhone tier of devices.
37
	 *
38
	 * @var bool
39
	 */
40
	public $isTierIphone;
41
42
	/**
43
	 * Stores whether the device can probably support Rich CSS, but JavaScript (jQuery) support is not assumed.
44
	 *
45
	 * @var bool
46
	 */
47
	public $isTierRichCss;
48
49
	/**
50
	 * Stores whether it is another mobile device, which cannot be assumed to support CSS or JS (eg, older BlackBerry, RAZR)
51
	 *
52
	 * @var bool
53
	 */
54
	public $isTierGenericMobile;
55
56
	/**
57
	 * Stores the device platform name
58
	 *
59
	 * @var null|string
60
	 */
61
	private $platform             = null;
62
	const PLATFORM_WINDOWS        = 'windows';
63
	const PLATFORM_IPHONE         = 'iphone';
64
	const PLATFORM_IPOD           = 'ipod';
65
	const PLATFORM_IPAD           = 'ipad';
66
	const PLATFORM_BLACKBERRY     = 'blackberry';
67
	const PLATFORM_BLACKBERRY_10  = 'blackberry_10';
68
	const PLATFORM_SYMBIAN        = 'symbian_series60';
69
	const PLATFORM_SYMBIAN_S40    = 'symbian_series40';
70
	const PLATFORM_J2ME_MIDP      = 'j2me_midp';
71
	const PLATFORM_ANDROID        = 'android';
72
	const PLATFORM_ANDROID_TABLET = 'android_tablet';
73
	const PLATFORM_FIREFOX_OS     = 'firefoxOS';
74
75
	/**
76
	 * A list of dumb-phone user agent parts.
77
	 *
78
	 * @var array
79
	 */
80
	public $dumb_agents = array(
81
		'nokia',
82
		'blackberry',
83
		'philips',
84
		'samsung',
85
		'sanyo',
86
		'sony',
87
		'panasonic',
88
		'webos',
89
		'ericsson',
90
		'alcatel',
91
		'palm',
92
		'windows ce',
93
		'opera mini',
94
		'series60',
95
		'series40',
96
		'au-mic,',
97
		'audiovox',
98
		'avantgo',
99
		'blazer',
100
		'danger',
101
		'docomo',
102
		'epoc',
103
		'ericy',
104
		'i-mode',
105
		'ipaq',
106
		'midp-',
107
		'mot-',
108
		'netfront',
109
		'nitro',
110
		'palmsource',
111
		'pocketpc',
112
		'portalmmm',
113
		'rover',
114
		'sie-',
115
		'symbian',
116
		'cldc-',
117
		'j2me',
118
		'smartphone',
119
		'up.browser',
120
		'up.link',
121
		'up.link',
122
		'vodafone/',
123
		'wap1.',
124
		'wap2.',
125
		'mobile',
126
		'googlebot-mobile',
127
	);
128
129
	/**
130
	 * The constructor.
131
	 *
132
	 * @param string $ua (Optional) User agent.
133
	 */
134
	public function __construct( $ua = '' ) {
135
		if ( $ua ) {
136
			$this->useragent = $ua;
137
		} else {
138
			if ( ! empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
139
				$this->useragent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
140
			}
141
		}
142
	}
143
144
	/**
145
	 * This method detects the mobile User Agent name.
146
	 *
147
	 * @return string The matched User Agent name, false otherwise.
148
	 */
149
	public function get_mobile_user_agent_name() {
150
		if ( $this->is_chrome_for_iOS() ) { // Keep this check before the safari rule.
151
			return 'chrome-for-ios';
152
		} elseif ( $this->is_iphone_or_ipod( 'iphone-safari' ) ) {
153
			return 'iphone';
154
		} elseif ( $this->is_ipad( 'ipad-safari' ) ) {
155
			return 'ipad';
156
		} elseif ( $this->is_android_tablet() ) { // Keep this check before the android rule.
157
			return 'android_tablet';
158
		} elseif ( $this->is_android() ) {
159
			return 'android';
160
		} elseif ( $this->is_blackberry_10() ) {
161
			return 'blackberry_10';
162
		} elseif ( $this->is_blackbeberry() ) {
163
			return 'blackberry';
164
		} elseif ( $this->is_WindowsPhone7() ) {
165
			return 'win7';
166
		} elseif ( $this->is_windows_phone_8() ) {
167
			return 'winphone8';
168
		} elseif ( $this->is_opera_mini() ) {
169
			return 'opera-mini';
170
		} elseif ( $this->is_opera_mini_dumb() ) {
171
			return 'opera-mini-dumb';
172
		} elseif ( $this->is_opera_mobile() ) {
173
			return 'opera-mobi';
174
		} elseif ( $this->is_blackberry_tablet() ) {
175
			return 'blackberry_tablet';
176
		} elseif ( $this->is_kindle_fire() ) {
177
			return 'kindle-fire';
178
		} elseif ( $this->is_PalmWebOS() ) {
179
			return 'webos';
180
		} elseif ( $this->is_S60_OSSBrowser() ) {
181
			return 'series60';
182
		} elseif ( $this->is_firefox_os() ) {
183
			return 'firefoxOS';
184
		} elseif ( $this->is_firefox_mobile() ) {
185
			return 'firefox_mobile';
186
		} elseif ( $this->is_MaemoTablet() ) {
187
			return 'maemo';
188
		} elseif ( $this->is_MeeGo() ) {
189
			return 'meego';
190
		} elseif ( $this->is_TouchPad() ) {
191
			return 'hp_tablet';
192
		} elseif ( $this->is_facebook_for_iphone() ) {
193
			return 'facebook-for-iphone';
194
		} elseif ( $this->is_facebook_for_ipad() ) {
195
			return 'facebook-for-ipad';
196
		} elseif ( $this->is_twitter_for_iphone() ) {
197
			return 'twitter-for-iphone';
198
		} elseif ( $this->is_twitter_for_ipad() ) {
199
			return 'twitter-for-ipad';
200
		} elseif ( $this->is_wordpress_for_ios() ) {
201
			return 'ios-app';
202
		} elseif ( $this->is_iphone_or_ipod( 'iphone-not-safari' ) ) {
203
			return 'iphone-unknown';
204
		} elseif ( $this->is_ipad( 'ipad-not-safari' ) ) {
205
			return 'ipad-unknown';
206
		} elseif ( $this->is_Nintendo_3DS() ) {
207
			return 'nintendo-3ds';
208
		} else {
209
			$agent       = strtolower( $_SERVER['HTTP_USER_AGENT'] );
210
			$dumb_agents = $this->dumb_agents;
211
			foreach ( $dumb_agents as $dumb_agent ) {
212
				if ( false !== strpos( $agent, $dumb_agent ) ) {
213
					return $dumb_agent;
214
				}
215
			}
216
		}
217
218
		return false;
219
	}
220
221
	/**
222
	 * This method detects the mobile device's platform. All return strings are from the class constants.
223
	 * Note that this function returns the platform name, not the UA name/type. You should use a different function
224
	 * if you need to test the UA capabilites.
225
	 *
226
	 * @return string Name of the platform, false otherwise.
227
	 */
228
	public function get_platform() {
229
		if ( isset( $this->platform ) ) {
230
				return $this->platform;
231
		}
232
233
		if ( strpos( $this->useragent, 'windows phone' ) !== false ) {
234
				$this->platform = self::PLATFORM_WINDOWS;
235
		} elseif ( strpos( $this->useragent, 'windows ce' ) !== false ) {
236
			$this->platform = self::PLATFORM_WINDOWS;
237
		} elseif ( strpos( $this->useragent, 'ipad' ) !== false ) {
238
			$this->platform = self::PLATFORM_IPAD;
239
		} elseif ( strpos( $this->useragent, 'ipod' ) !== false ) {
240
			$this->platform = self::PLATFORM_IPOD;
241
		} elseif ( strpos( $this->useragent, 'iphone' ) !== false ) {
242
			$this->platform = self::PLATFORM_IPHONE;
243
		} elseif ( strpos( $this->useragent, 'android' ) !== false ) {
244
			if ( $this->is_android_tablet() ) {
245
				$this->platform = self::PLATFORM_ANDROID_TABLET;
246
			} else {
247
				$this->platform = self::PLATFORM_ANDROID;
248
			}
249
		} elseif ( $this->is_kindle_fire() ) {
250
			$this->platform = self::PLATFORM_ANDROID_TABLET;
251
		} elseif ( $this->is_blackberry_10() ) {
252
			$this->platform = self::PLATFORM_BLACKBERRY_10;
253
		} elseif ( strpos( $this->useragent, 'blackberry' ) !== false ) {
254
			$this->platform = self::PLATFORM_BLACKBERRY;
255
		} elseif ( $this->is_blackberry_tablet() ) {
256
			$this->platform = self::PLATFORM_BLACKBERRY;
257
		} elseif ( $this->is_symbian_platform() ) {
258
			$this->platform = self::PLATFORM_SYMBIAN;
259
		} elseif ( $this->is_symbian_s40_platform() ) {
260
			$this->platform = self::PLATFORM_SYMBIAN_S40;
261
		} elseif ( $this->is_J2ME_platform() ) {
262
			$this->platform = self::PLATFORM_J2ME_MIDP;
263
		} elseif ( $this->is_firefox_os() ) {
264
			$this->platform = self::PLATFORM_FIREFOX_OS;
265
		} else {
266
			$this->platform = false;
0 ignored issues
show
Documentation Bug introduced by
It seems like false of type false is incompatible with the declared type null|string of property $platform.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
267
		}
268
269
		return $this->platform;
270
	}
271
272
	/**
273
	 * This method detects for UA which can display iPhone-optimized web content.
274
	 * Includes iPhone, iPod Touch, Android, WebOS, Fennec (Firefox mobile), etc.
275
	 */
276
	public function isTierIphone() {
277
		if ( isset( $this->isTierIphone ) ) {
278
			return $this->isTierIphone;
279
		}
280
		if ( $this->is_iphoneOrIpod() ) {
281
			$this->matched_agent       = 'iphone';
282
			$this->isTierIphone        = true;
283
			$this->isTierRichCss       = false;
284
			$this->isTierGenericMobile = false;
285 View Code Duplication
		} elseif ( $this->is_android() ) {
286
			$this->matched_agent       = 'android';
287
			$this->isTierIphone        = true;
288
			$this->isTierRichCss       = false;
289
			$this->isTierGenericMobile = false;
290
		} elseif ( $this->is_windows_phone_8() ) {
291
			$this->matched_agent       = 'winphone8';
292
			$this->isTierIphone        = true;
293
			$this->isTierRichCss       = false;
294
			$this->isTierGenericMobile = false;
295 View Code Duplication
		} elseif ( $this->is_WindowsPhone7() ) {
296
			$this->matched_agent       = 'win7';
297
			$this->isTierIphone        = true;
298
			$this->isTierRichCss       = false;
299
			$this->isTierGenericMobile = false;
300
		} elseif ( $this->is_blackberry_10() ) {
301
			$this->matched_agent       = 'blackberry-10';
302
			$this->isTierIphone        = true;
303
			$this->isTierRichCss       = false;
304
			$this->isTierGenericMobile = false;
305 View Code Duplication
		} elseif ( $this->is_blackbeberry() && 'blackberry-webkit' === $this->detect_blackberry_browser_version() ) {
306
			$this->matched_agent       = 'blackberry-webkit';
307
			$this->isTierIphone        = true;
308
			$this->isTierRichCss       = false;
309
			$this->isTierGenericMobile = false;
310
		} elseif ( $this->is_blackberry_tablet() ) {
311
			$this->matched_agent       = 'blackberry_tablet';
312
			$this->isTierIphone        = true;
313
			$this->isTierRichCss       = false;
314
			$this->isTierGenericMobile = false;
315 View Code Duplication
		} elseif ( $this->is_PalmWebOS() ) {
316
			$this->matched_agent       = 'webos';
317
			$this->isTierIphone        = true;
318
			$this->isTierRichCss       = false;
319
			$this->isTierGenericMobile = false;
320
		} elseif ( $this->is_TouchPad() ) {
321
			$this->matched_agent       = 'hp_tablet';
322
			$this->isTierIphone        = true;
323
			$this->isTierRichCss       = false;
324
			$this->isTierGenericMobile = false;
325 View Code Duplication
		} elseif ( $this->is_firefox_os() ) {
326
			$this->matched_agent       = 'firefoxOS';
327
			$this->isTierIphone        = true;
328
			$this->isTierRichCss       = false;
329
			$this->isTierGenericMobile = false;
330
		} elseif ( $this->is_firefox_mobile() ) {
331
			$this->matched_agent       = 'fennec';
332
			$this->isTierIphone        = true;
333
			$this->isTierRichCss       = false;
334
			$this->isTierGenericMobile = false;
335 View Code Duplication
		} elseif ( $this->is_opera_mobile() ) {
336
			$this->matched_agent       = 'opera-mobi';
337
			$this->isTierIphone        = true;
338
			$this->isTierRichCss       = false;
339
			$this->isTierGenericMobile = false;
340
		} elseif ( $this->is_MaemoTablet() ) {
341
			$this->matched_agent       = 'maemo';
342
			$this->isTierIphone        = true;
343
			$this->isTierRichCss       = false;
344
			$this->isTierGenericMobile = false;
345 View Code Duplication
		} elseif ( $this->is_MeeGo() ) {
346
			$this->matched_agent       = 'meego';
347
			$this->isTierIphone        = true;
348
			$this->isTierRichCss       = false;
349
			$this->isTierGenericMobile = false;
350
		} elseif ( $this->is_kindle_touch() ) {
351
			$this->matched_agent       = 'kindle-touch';
352
			$this->isTierIphone        = true;
353
			$this->isTierRichCss       = false;
354
			$this->isTierGenericMobile = false;
355 View Code Duplication
		} elseif ( $this->is_Nintendo_3DS() ) {
356
			$this->matched_agent       = 'nintendo-3ds';
357
			$this->isTierIphone        = true;
358
			$this->isTierRichCss       = false;
359
			$this->isTierGenericMobile = false;
360
		} else {
361
			$this->isTierIphone = false;
362
		}
363
		return $this->isTierIphone;
364
	}
365
366
	/**
367
	 * This method detects for UA which are likely to be capable
368
	 * but may not necessarily support JavaScript.
369
	 * Excludes all iPhone Tier UA.
370
	 */
371
	public function isTierRichCss() {
372
		if ( isset( $this->isTierRichCss ) ) {
373
			return $this->isTierRichCss;
374
		}
375
		if ( $this->isTierIphone() ) {
376
			return false;
377
		}
378
379
		// The following devices are explicitly ok.
380
		if ( $this->is_S60_OSSBrowser() ) {
381
			$this->matched_agent       = 'series60';
382
			$this->isTierIphone        = false;
383
			$this->isTierRichCss       = true;
384
			$this->isTierGenericMobile = false;
385 View Code Duplication
		} elseif ( $this->is_opera_mini() ) {
386
			$this->matched_agent       = 'opera-mini';
387
			$this->isTierIphone        = false;
388
			$this->isTierRichCss       = true;
389
			$this->isTierGenericMobile = false;
390
		} elseif ( $this->is_blackbeberry() ) {
391
			$detectedDevice = $this->detect_blackberry_browser_version();
392
			if (
393
				'blackberry-5' === $detectedDevice
394
				|| 'blackberry-4.7' === $detectedDevice
395
				|| 'blackberry-4.6' === $detectedDevice
396
			) {
397
				$this->matched_agent       = $detectedDevice;
398
				$this->isTierIphone        = false;
399
				$this->isTierRichCss       = true;
400
				$this->isTierGenericMobile = false;
401
			}
402
		} else {
403
			$this->isTierRichCss = false;
404
		}
405
406
		return $this->isTierRichCss;
407
	}
408
409
	/**
410
	 * Detects if the user is using a tablet.
411
	 * props Corey Gilmore, BGR.com
412
	 *
413
	 * @return bool
414
	 */
415
	public function is_tablet() {
416
		return ( 0 // Never true, but makes it easier to manage our list of tablet conditions.
417
				|| self::is_ipad()
418
				|| self::is_android_tablet()
419
				|| self::is_blackberry_tablet()
420
				|| self::is_kindle_fire()
421
				|| self::is_MaemoTablet()
422
				|| self::is_TouchPad()
423
		);
424
	}
425
426
	/**
427
	 *  Detects if the current UA is the default iPhone or iPod Touch Browser.
428
	 *
429
	 *  DEPRECATED: use is_iphone_or_ipod
430
	 */
431
	public function is_iphoneOrIpod() {
432
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
433
			return false;
434
		}
435
436
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
437 View Code Duplication
		if ( ( strpos( $ua, 'iphone' ) !== false ) || ( strpos( $ua, 'ipod' ) !== false ) ) {
438
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
439
				return false;
440
			} else {
441
				return true;
442
			}
443
		} else {
444
			return false;
445
		}
446
	}
447
448
	/**
449
	 *  Detects if the current UA is iPhone Mobile Safari or another iPhone or iPod Touch Browser.
450
	 *
451
	 *  They type can check for any iPhone, an iPhone using Safari, or an iPhone using something other than Safari.
452
	 *
453
	 *  Note: If you want to check for Opera mini, Opera mobile or Firefox mobile (or any 3rd party iPhone browser),
454
	 *  you should put the check condition before the check for 'iphone-any' or 'iphone-not-safari'.
455
	 *  Otherwise those browsers will be 'catched' by the iphone string.
456
	 *
457
	 * @param string $type Type of iPhone detection.
458
	 */
459
	public static function is_iphone_or_ipod( $type = 'iphone-any' ) {
460
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
461
			return false;
462
		}
463
464
		$ua        = strtolower( $_SERVER['HTTP_USER_AGENT'] );
465
		$is_iphone = ( strpos( $ua, 'iphone' ) !== false ) || ( strpos( $ua, 'ipod' ) !== false );
466
		$is_safari = ( false !== strpos( $ua, 'safari' ) );
467
468
		if ( 'iphone-safari' === $type ) {
469
			return $is_iphone && $is_safari;
470
		} elseif ( 'iphone-not-safari' === $type ) {
471
			return $is_iphone && ! $is_safari;
472
		} else {
473
			return $is_iphone;
474
		}
475
	}
476
477
	/**
478
	 *  Detects if the current UA is Chrome for iOS
479
	 *
480
	 *  The User-Agent string in Chrome for iOS is the same as the Mobile Safari User-Agent, with CriOS/<ChromeRevision> instead of Version/<VersionNum>.
481
	 *  - 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
482
	 */
483
	public static function is_chrome_for_iOS() {
484
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
485
			return false;
486
		}
487
488
		if ( self::is_iphone_or_ipod( 'iphone-safari' ) === false ) {
489
			return false;
490
		}
491
492
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
493
494
		if ( strpos( $ua, 'crios/' ) !== false ) {
495
			return true;
496
		} else {
497
			return false;
498
		}
499
	}
500
501
	/**
502
	 *  Detects if the current UA is Twitter for iPhone
503
	 *
504
	 * 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
505
	 * 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
506
	 */
507
	public static function is_twitter_for_iphone() {
508
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
509
			return false;
510
		}
511
512
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
513
514
		if ( strpos( $ua, 'ipad' ) !== false ) {
515
			return false;
516
		}
517
518
		if ( strpos( $ua, 'twitter for iphone' ) !== false ) {
519
			return true;
520
		} else {
521
			return false;
522
		}
523
	}
524
525
	/**
526
	 * Detects if the current UA is Twitter for iPad
527
	 *
528
	 * 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
529
	 * 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
530
	 */
531
	public static function is_twitter_for_ipad() {
532
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
533
			return false;
534
		}
535
536
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
537
538
		if ( strpos( $ua, 'twitter for ipad' ) !== false ) {
539
			return true;
540
		} elseif ( strpos( $ua, 'ipad' ) !== false && strpos( $ua, 'twitter for iphone' ) !== false ) {
541
			return true;
542
		} else {
543
			return false;
544
		}
545
	}
546
547
	/**
548
	 * Detects if the current UA is Facebook for iPhone
549
	 * - Facebook 4020.0 (iPhone; iPhone OS 5.0.1; fr_FR)
550
	 * - 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]
551
	 * - 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]
552
	 */
553
	public static function is_facebook_for_iphone() {
554
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
555
			return false;
556
		}
557
558
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
559
560
		if ( false === strpos( $ua, 'iphone' ) ) {
561
			return false;
562
		}
563
564
		if ( false !== strpos( $ua, 'facebook' ) && false === strpos( $ua, 'ipad' ) ) {
565
			return true;
566
		} elseif ( false !== strpos( $ua, 'fbforiphone' ) && false === strpos( $ua, 'tablet' ) ) {
567
			return true;
568
		} elseif ( false !== strpos( $ua, 'fban/fbios;' ) && false === strpos( $ua, 'tablet' ) ) { // FB app v5.0 or higher.
569
			return true;
570
		} else {
571
			return false;
572
		}
573
	}
574
575
	/**
576
	 * Detects if the current UA is Facebook for iPad
577
	 * - Facebook 4020.0 (iPad; iPhone OS 5.0.1; en_US)
578
	 * - 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]
579
	 * - 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]
580
	 */
581 View Code Duplication
	public static function is_facebook_for_ipad() {
582
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
583
			return false;
584
		}
585
586
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
587
588
		if ( false === strpos( $ua, 'ipad' ) ) {
589
			return false;
590
		}
591
592
		if ( false !== strpos( $ua, 'facebook' ) || false !== strpos( $ua, 'fbforiphone' ) || false !== strpos( $ua, 'fban/fbios;' ) ) {
593
			return true;
594
		} else {
595
			return false;
596
		}
597
	}
598
599
	/**
600
	 *  Detects if the current UA is WordPress for iOS
601
	 */
602
	public static function is_wordpress_for_ios() {
603
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
604
			return false;
605
		}
606
607
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
608
		if ( false !== strpos( $ua, 'wp-iphone' ) ) {
609
			return true;
610
		} else {
611
			return false;
612
		}
613
	}
614
615
	/**
616
	 * Detects if the current device is an iPad.
617
	 * They type can check for any iPad, an iPad using Safari, or an iPad using something other than Safari.
618
	 *
619
	 * Note: If you want to check for Opera mini, Opera mobile or Firefox mobile (or any 3rd party iPad browser),
620
	 * you should put the check condition before the check for 'iphone-any' or 'iphone-not-safari'.
621
	 * Otherwise those browsers will be 'catched' by the ipad string.
622
	 *
623
	 * @param string $type iPad type.
624
	 */
625
	public static function is_ipad( $type = 'ipad-any' ) {
626
627
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
628
			return false;
629
		}
630
631
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
632
633
		$is_ipad   = ( false !== strpos( $ua, 'ipad' ) );
634
		$is_safari = ( false !== strpos( $ua, 'safari' ) );
635
636
		if ( 'ipad-safari' === $type ) {
637
			return $is_ipad && $is_safari;
638
		} elseif ( 'ipad-not-safari' === $type ) {
639
			return $is_ipad && ! $is_safari;
640
		} else {
641
			return $is_ipad;
642
		}
643
	}
644
645
	/**
646
	 * Detects if the current browser is Firefox Mobile (Fennec)
647
	 *
648
	 * See http://www.useragentstring.com/pages/Fennec/
649
	 * Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.1.1) Gecko/20110415 Firefox/4.0.2pre Fennec/4.0.1
650
	 * Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1b2pre) Gecko/20081015 Fennec/1.0a1
651
	 */
652
	public static function is_firefox_mobile() {
653
654
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
655
			return false;
656
		}
657
658
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
659
660
		if ( strpos( $ua, 'fennec' ) !== false ) {
661
			return true;
662
		} else {
663
			return false;
664
		}
665
	}
666
667
	/**
668
	 * Detects if the current browser is Firefox for desktop
669
	 *
670
	 * See https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/User-Agent/Firefox
671
	 * Mozilla/5.0 (platform; rv:geckoversion) Gecko/geckotrail Firefox/firefoxversion
672
	 * The platform section will include 'Mobile' for phones and 'Tablet' for tablets.
673
	 */
674 View Code Duplication
	public static function is_firefox_desktop() {
675
676
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
677
			return false;
678
		}
679
680
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
681
682
		if ( false !== strpos( $ua, 'firefox' ) && false === strpos( $ua, 'mobile' ) && false === strpos( $ua, 'tablet' ) ) {
683
			return true;
684
		} else {
685
			return false;
686
		}
687
	}
688
689
	/**
690
	 * Detects if the current browser is FirefoxOS Native browser
691
	 *
692
	 * Mozilla/5.0 (Mobile; rv:14.0) Gecko/14.0 Firefox/14.0
693
	 */
694 View Code Duplication
	public static function is_firefox_os() {
695
696
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
697
			return false;
698
		}
699
700
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
701
702
		if ( strpos( $ua, 'mozilla' ) !== false && strpos( $ua, 'mobile' ) !== false && strpos( $ua, 'gecko' ) !== false && strpos( $ua, 'firefox' ) !== false ) {
703
			return true;
704
		} else {
705
			return false;
706
		}
707
	}
708
709
	/**
710
	 * Detect modern Opera desktop
711
	 *
712
	 * Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 OPR/74.0.3911.203
713
	 *
714
	 * Looking for "OPR/" specifically.
715
	 */
716 View Code Duplication
	public static function is_opera_desktop() {
717
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
718
			return false;
719
		}
720
721
		if ( false === strpos( $_SERVER['HTTP_USER_AGENT'], 'OPR/' ) ) {
722
			return false;
723
		}
724
725
		return true;
726
	}
727
728
	/**
729
	 * Detects if the current browser is Opera Mobile
730
	 *
731
	 * What is the difference between Opera Mobile and Opera Mini?
732
	 * - Opera Mobile is a full Internet browser for mobile devices.
733
	 * - Opera Mini always uses a transcoder to convert the page for a small display.
734
	 * (it uses Opera advanced server compression technology to compress web content before it gets to a device.
735
	 *  The rendering engine is on Opera's server.)
736
	 *
737
	 * Opera/9.80 (Windows NT 6.1; Opera Mobi/14316; U; en) Presto/2.7.81 Version/11.00"
738
	 * Opera/9.50 (Nintendo DSi; Opera/507; U; en-US)
739
	 */
740 View Code Duplication
	public static function is_opera_mobile() {
741
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
742
			return false;
743
		}
744
745
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
746
747
		if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mobi' ) !== false ) {
748
			return true;
749
		} elseif ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'nintendo dsi' ) !== false ) {
750
			return true;
751
		} else {
752
			return false;
753
		}
754
	}
755
756
	/**
757
	 * Detects if the current browser is Opera Mini
758
	 *
759
	 * Opera/8.01 (J2ME/MIDP; Opera Mini/3.0.6306/1528; en; U; ssr)
760
	 * Opera/9.80 (Android;Opera Mini/6.0.24212/24.746 U;en) Presto/2.5.25 Version/10.5454
761
	 * Opera/9.80 (iPhone; Opera Mini/5.0.019802/18.738; U; en) Presto/2.4.15
762
	 * Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
763
	 * Opera/9.80 (J2ME/iPhone;Opera Mini/5.0.019802/886; U; ja) Presto/2.4.15
764
	 * Opera/9.80 (Series 60; Opera Mini/5.1.22783/23.334; U; en) Presto/2.5.25 Version/10.54
765
	 * Opera/9.80 (BlackBerry; Opera Mini/5.1.22303/22.387; U; en) Presto/2.5.25 Version/10.54
766
	 */
767
	public static function is_opera_mini() {
768
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
769
			return false;
770
		}
771
772
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
773
774
		if ( strpos( $ua, 'opera' ) !== false && strpos( $ua, 'mini' ) !== false ) {
775
			return true;
776
		} else {
777
			return false;
778
		}
779
	}
780
781
	/**
782
	 * Detects if the current browser is Opera Mini, but not on a smart device OS(Android, iOS, etc)
783
	 * Used to send users on dumb devices to m.wor
784
	 */
785
	public static function is_opera_mini_dumb() {
786
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
787
			return false;
788
		}
789
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
790
791
		if ( self::is_opera_mini() ) {
792
			if ( strpos( $ua, 'android' ) !== false || strpos( $ua, 'iphone' ) !== false || strpos( $ua, 'ipod' ) !== false
793
			|| strpos( $ua, 'ipad' ) !== false || strpos( $ua, 'blackberry' ) !== false ) {
794
				return false;
795
			} else {
796
				return true;
797
			}
798
		} else {
799
			return false;
800
		}
801
	}
802
803
	/**
804
	 * Detects if the current browser is a Windows Phone 7 device.
805
	 * ex: Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; Trident/3.1; IEMobile/7.0; LG; GW910)
806
	 */
807 View Code Duplication
	public static function is_WindowsPhone7() {
808
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
809
			return false;
810
		}
811
812
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
813
814
		if ( false === strpos( $ua, 'windows phone os 7' ) ) {
815
			return false;
816
		} else {
817
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
818
				return false;
819
			} else {
820
				return true;
821
			}
822
		}
823
	}
824
825
	/**
826
	 * Detects if the current browser is a Windows Phone 8 device.
827
	 * ex: Mozilla/5.0 (compatible; MSIE 10.0; Windows Phone 8.0; Trident/6.0; ARM; Touch; IEMobile/10.0; <Manufacturer>; <Device> [;<Operator>])
828
	 */
829 View Code Duplication
	public static function is_windows_phone_8() {
830
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
831
			return false;
832
		}
833
834
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
835
		if ( strpos( $ua, 'windows phone 8' ) === false ) {
836
			return false;
837
		} else {
838
			return true;
839
		}
840
	}
841
842
	/**
843
	 * Detects if the current browser is on a Palm device running the new WebOS. This EXCLUDES TouchPad.
844
	 *
845
	 * 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
846
	 * 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
847
	 */
848 View Code Duplication
	public static function is_PalmWebOS() {
849
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
850
			return false;
851
		}
852
853
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
854
855
		if ( false === strpos( $ua, 'webos' ) ) {
856
			return false;
857
		} else {
858
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
859
				return false;
860
			} else {
861
				return true;
862
			}
863
		}
864
	}
865
866
	/**
867
	 * Detects if the current browser is the HP TouchPad default browser. This excludes phones wt WebOS.
868
	 *
869
	 * 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
870
	 * 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
871
	 */
872
	public static function is_TouchPad() {
873
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
874
			return false;
875
		}
876
877
		$http_user_agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
878
		if ( false !== strpos( $http_user_agent, 'hp-tablet' ) || false !== strpos( $http_user_agent, 'hpwos' ) || false !== strpos( $http_user_agent, 'touchpad' ) ) {
879
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
880
				return false;
881
			} else {
882
				return true;
883
			}
884
		} else {
885
			return false;
886
		}
887
	}
888
889
	/**
890
	 * Detects if the current browser is the Series 60 Open Source Browser.
891
	 *
892
	 * 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
893
	 *
894
	 * 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
895
	 *
896
	 * 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
897
	 */
898
	public static function is_S60_OSSBrowser() {
899
900
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
901
			return false;
902
		}
903
904
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
905
		if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
906
			return false;
907
		}
908
909
		$pos_webkit = strpos( $agent, 'webkit' );
910
		if ( false !== $pos_webkit ) {
911
			// First, test for WebKit, then make sure it's either Symbian or S60.
912
			if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
913
					return true;
914
			} else {
915
				return false;
916
			}
917 View Code Duplication
		} elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent, 'series60' ) !== false ) {
918
			return true;
919
		} elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent, 'series60' ) !== false ) {
920
			return true;
921
		}
922
923
		return false;
924
	}
925
926
	/**
927
	 * Detects if the device platform is the Symbian Series 60.
928
	 */
929
	public static function is_symbian_platform() {
930
931
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
932
			return false;
933
		}
934
935
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
936
937
		$pos_webkit = strpos( $agent, 'webkit' );
938
		if ( false !== $pos_webkit ) {
939
			// First, test for WebKit, then make sure it's either Symbian or S60.
940
			if ( strpos( $agent, 'symbian' ) !== false || strpos( $agent, 'series60' ) !== false ) {
941
					return true;
942
			} else {
943
				return false;
944
			}
945 View Code Duplication
		} elseif ( strpos( $agent, 'symbianos' ) !== false && strpos( $agent, 'series60' ) !== false ) {
946
			return true;
947
		} elseif ( strpos( $agent, 'nokia' ) !== false && strpos( $agent, 'series60' ) !== false ) {
948
			return true;
949
		} elseif ( strpos( $agent, 'opera mini' ) !== false ) {
950
			if ( strpos( $agent, 'symbianos' ) !== false || strpos( $agent, 'symbos' ) !== false || strpos( $agent, 'series 60' ) !== false ) {
951
				return true;
952
			}
953
		}
954
955
		return false;
956
	}
957
958
	/**
959
	 * Detects if the device platform is the Symbian Series 40.
960
	 * Nokia Browser for Series 40 is a proxy based browser, previously known as Ovi Browser.
961
	 * This browser will report 'NokiaBrowser' in the header, however some older version will also report 'OviBrowser'.
962
	 */
963 View Code Duplication
	public static function is_symbian_s40_platform() {
964
965
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
966
			return false;
967
		}
968
969
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
970
971
		if ( strpos( $agent, 'series40' ) !== false ) {
972
			if ( strpos( $agent, 'nokia' ) !== false || strpos( $agent, 'ovibrowser' ) !== false || strpos( $agent, 'nokiabrowser' ) !== false ) {
973
				return true;
974
			}
975
		}
976
977
		return false;
978
	}
979
980
	/**
981
	 * Returns if the device belongs to J2ME capable family.
982
	 *
983
	 * @return bool
984
	 */
985 View Code Duplication
	public static function is_J2ME_platform() {
986
987
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
988
			return false;
989
		}
990
991
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
992
993
		if ( strpos( $agent, 'j2me/midp' ) !== false ) {
994
			return true;
995
		} elseif ( strpos( $agent, 'midp' ) !== false && strpos( $agent, 'cldc' ) ) {
996
			return true;
997
		}
998
		return false;
999
	}
1000
1001
	/**
1002
	 * Detects if the current UA is on one of the Maemo-based Nokia Internet Tablets.
1003
	 */
1004
	public static function is_MaemoTablet() {
1005
1006
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1007
			return false;
1008
		}
1009
1010
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1011
1012
		$pos_maemo = strpos( $agent, 'maemo' );
1013
		if ( false === $pos_maemo ) {
1014
			return false;
1015
		}
1016
1017
		// Must be Linux + Tablet, or else it could be something else.
1018 View Code Duplication
		if ( strpos( $agent, 'tablet' ) !== false && strpos( $agent, 'linux' ) !== false ) {
1019
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1020
				return false;
1021
			} else {
1022
				return true;
1023
			}
1024
		} else {
1025
			return false;
1026
		}
1027
	}
1028
1029
	/**
1030
	 * Detects if the current UA is a MeeGo device (Nokia Smartphone).
1031
	 */
1032 View Code Duplication
	public static function is_MeeGo() {
1033
1034
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1035
			return false;
1036
		}
1037
1038
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1039
1040
		if ( false === strpos( $ua, 'meego' ) ) {
1041
			return false;
1042
		} else {
1043
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1044
				return false;
1045
			} else {
1046
				return true;
1047
			}
1048
		}
1049
	}
1050
1051
	/**
1052
	 * The is_webkit() method can be used to check the User Agent for an webkit generic browser.
1053
	 */
1054 View Code Duplication
	public static function is_webkit() {
1055
1056
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1057
			return false;
1058
		}
1059
1060
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1061
1062
		$pos_webkit = strpos( $agent, 'webkit' );
1063
1064
		if ( false !== $pos_webkit ) {
1065
			return true;
1066
		} else {
1067
			return false;
1068
		}
1069
	}
1070
1071
	/**
1072
	 * Detects if the current browser is the Native Android browser.
1073
	 *
1074
	 * @return boolean true if the browser is Android otherwise false
1075
	 */
1076 View Code Duplication
	public static function is_android() {
1077
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1078
			return false;
1079
		}
1080
1081
		$agent       = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1082
		$pos_android = strpos( $agent, 'android' );
1083
		if ( false !== $pos_android ) {
1084
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1085
				return false;
1086
			} else {
1087
				return true;
1088
			}
1089
		} else {
1090
			return false;
1091
		}
1092
	}
1093
1094
	/**
1095
	 * Detects if the current browser is the Native Android Tablet browser.
1096
	 * Assumes 'Android' should be in the user agent, but not 'mobile'
1097
	 *
1098
	 * @return boolean true if the browser is Android and not 'mobile' otherwise false
1099
	 */
1100
	public static function is_android_tablet() {
1101
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1102
			return false;
1103
		}
1104
1105
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1106
1107
		$pos_android      = strpos( $agent, 'android' );
1108
		$pos_mobile       = strpos( $agent, 'mobile' );
1109
		$post_android_app = strpos( $agent, 'wp-android' );
1110
1111
		if ( false !== $pos_android && false === $pos_mobile && false === $post_android_app ) {
1112
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1113
				return false;
1114
			} else {
1115
				return true;
1116
			}
1117
		} else {
1118
			return false;
1119
		}
1120
	}
1121
1122
	/**
1123
	 * Detects if the current browser is the Kindle Fire Native browser.
1124
	 *
1125
	 * 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
1126
	 * 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
1127
	 *
1128
	 * @return boolean true if the browser is Kindle Fire Native browser otherwise false
1129
	 */
1130
	public static function is_kindle_fire() {
1131
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1132
			return false;
1133
		}
1134
1135
		$agent        = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1136
		$pos_silk     = strpos( $agent, 'silk/' );
1137
		$pos_silk_acc = strpos( $agent, 'silk-accelerated=' );
1138
		if ( false !== $pos_silk && false !== $pos_silk_acc ) {
1139
			return true;
1140
		} else {
1141
			return false;
1142
		}
1143
	}
1144
1145
	/**
1146
	 * Detects if the current browser is the Kindle Touch Native browser
1147
	 *
1148
	 * 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+
1149
	 *
1150
	 * @return boolean true if the browser is Kindle monochrome Native browser otherwise false
1151
	 */
1152 View Code Duplication
	public static function is_kindle_touch() {
1153
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1154
			return false;
1155
		}
1156
		$agent            = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1157
		$pos_kindle_touch = strpos( $agent, 'kindle/3.0+' );
1158
		if ( false !== $pos_kindle_touch && false === self::is_kindle_fire() ) {
1159
			return true;
1160
		} else {
1161
			return false;
1162
		}
1163
	}
1164
1165
	/**
1166
	 * Detect if user agent is the WordPress.com Windows 8 app (used ONLY on the custom oauth stylesheet)
1167
	 */
1168 View Code Duplication
	public static function is_windows8_auth() {
1169
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1170
			return false;
1171
		}
1172
1173
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1174
		$pos   = strpos( $agent, 'msauthhost' );
1175
		if ( false !== $pos ) {
1176
			return true;
1177
		} else {
1178
			return false;
1179
		}
1180
	}
1181
1182
	/**
1183
	 * Detect if user agent is the WordPress.com Windows 8 app.
1184
	 */
1185 View Code Duplication
	public static function is_wordpress_for_win8() {
1186
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1187
			return false;
1188
		}
1189
1190
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1191
		$pos   = strpos( $agent, 'wp-windows8' );
1192
		if ( false !== $pos ) {
1193
			return true;
1194
		} else {
1195
			return false;
1196
		}
1197
	}
1198
1199
	/**
1200
	 * Detect if user agent is the WordPress.com Desktop app.
1201
	 */
1202 View Code Duplication
	public static function is_wordpress_desktop_app() {
1203
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1204
			return false;
1205
		}
1206
1207
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1208
		$pos   = strpos( $agent, 'WordPressDesktop' );
1209
		if ( false !== $pos ) {
1210
			return true;
1211
		} else {
1212
			return false;
1213
		}
1214
	}
1215
1216
	/**
1217
	 * The is_blackberry_tablet() method can be used to check the User Agent for a RIM blackberry tablet.
1218
	 * The user agent of the BlackBerry® Tablet OS follows a format similar to the following:
1219
	 * 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+
1220
	 */
1221
	public static function is_blackberry_tablet() {
1222
1223
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1224
			return false;
1225
		}
1226
1227
		$agent          = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1228
		$pos_playbook   = stripos( $agent, 'PlayBook' );
1229
		$pos_rim_tablet = stripos( $agent, 'RIM Tablet' );
1230
1231
		if ( ( false === $pos_playbook ) || ( false === $pos_rim_tablet ) ) {
1232
			return false;
1233
		} else {
1234
			return true;
1235
		}
1236
	}
1237
1238
	/**
1239
	 * The is_blackbeberry() method can be used to check the User Agent for a blackberry device.
1240
	 * Note that opera mini on BB matches this rule.
1241
	 */
1242 View Code Duplication
	public static function is_blackbeberry() {
1243
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1244
			return false;
1245
		}
1246
1247
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1248
1249
		$pos_blackberry = strpos( $agent, 'blackberry' );
1250
		if ( false !== $pos_blackberry ) {
1251
			if ( self::is_opera_mini() || self::is_opera_mobile() || self::is_firefox_mobile() ) {
1252
				return false;
1253
			} else {
1254
				return true;
1255
			}
1256
		} else {
1257
			return false;
1258
		}
1259
	}
1260
1261
	/**
1262
	 * The is_blackberry_10() method can be used to check the User Agent for a BlackBerry 10 device.
1263
	 */
1264
	public static function is_blackberry_10() {
1265
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1266
		return ( strpos( $agent, 'bb10' ) !== false ) && ( strpos( $agent, 'mobile' ) !== false );
1267
	}
1268
1269
	/**
1270
	 * Retrieve the blackberry OS version.
1271
	 *
1272
	 * Return strings are from the following list:
1273
	 * - blackberry-10
1274
	 * - blackberry-7
1275
	 * - blackberry-6
1276
	 * - blackberry-torch //only the first edition. The 2nd edition has the OS7 onboard and doesn't need any special rule.
1277
	 * - blackberry-5
1278
	 * - blackberry-4.7
1279
	 * - blackberry-4.6
1280
	 * - blackberry-4.5
1281
	 *
1282
	 * @return string Version of the BB OS.
1283
	 * If version is not found, get_blackbeberry_OS_version will return boolean false.
1284
	 */
1285
	public static function get_blackbeberry_OS_version() {
1286
1287
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1288
			return false;
1289
		}
1290
1291
		if ( self::is_blackberry_10() ) {
1292
			return 'blackberry-10';
1293
		}
1294
1295
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1296
1297
		$pos_blackberry = stripos( $agent, 'blackberry' );
1298
		if ( false === $pos_blackberry ) {
1299
			// Not a blackberry device.
1300
			return false;
1301
		}
1302
1303
		// Blackberry devices OS 6.0 or higher.
1304
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9670; en) AppleWebKit/534.3+ (KHTML, like Gecko) Version/6.0.0.286 Mobile Safari/534.3+.
1305
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9800; en) AppleWebKit/534.1+ (KHTML, Like Gecko) Version/6.0.0.141 Mobile Safari/534.1+.
1306
		// Mozilla/5.0 (BlackBerry; U; BlackBerry 9900; en-US) AppleWebKit/534.11+ (KHTML, like Gecko) Version/7.0.0 Mobile Safari/534.11+.
1307
		$pos_webkit = stripos( $agent, 'webkit' );
1308
		if ( false !== $pos_webkit ) {
1309
			// Detected blackberry webkit browser.
1310
			$pos_torch = stripos( $agent, 'BlackBerry 9800' );
1311
			if ( false !== $pos_torch ) {
1312
				return 'blackberry-torch'; // Match the torch first edition. the 2nd edition should use the OS7 and doesn't need any special rule.
1313
			} else {
1314
				// Detecting the BB OS version for devices running OS 6.0 or higher.
1315
				if ( preg_match( '#Version\/([\d\.]+)#i', $agent, $matches ) ) {
1316
					$version     = $matches[1];
1317
					$version_num = explode( '.', $version );
1318
					if ( false === is_array( $version_num ) || count( $version_num ) <= 1 ) {
1319
						return 'blackberry-6'; // not a BB device that match our rule.
1320
					} else {
1321
						return 'blackberry-' . $version_num[0];
1322
					}
1323
				} else {
1324
					// if doesn't match returns the minimun version with a webkit browser. we should never fall here.
1325
					return 'blackberry-6'; // not a BB device that match our rule.
1326
				}
1327
			}
1328
		}
1329
1330
		// Blackberry devices <= 5.XX.
1331
		// BlackBerry9000/5.0.0.93 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/179.
1332
		if ( preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
1333
			$version = $matches[1];
1334
		} else {
1335
			return false; // not a BB device that match our rule.
1336
		}
1337
1338
		$version_num = explode( '.', $version );
1339
1340 View Code Duplication
		if ( is_array( $version_num ) === false || count( $version_num ) <= 1 ) {
1341
			return false;
1342
		}
1343
1344
		$version_num_major = (int) $version_num[0];
1345
		$version_num_minor = (int) $version_num[1];
1346
1347
		if ( 5 === $version_num_major ) {
1348
			return 'blackberry-5';
1349
		} elseif ( 4 === $version_num_major && 7 === $version_num_minor ) {
1350
			return 'blackberry-4.7';
1351
		} elseif ( 4 === $version_num_major && 6 === $version_num_minor ) {
1352
			return 'blackberry-4.6';
1353
		} elseif ( 4 === $version_num_major && 5 === $version_num_minor ) {
1354
			return 'blackberry-4.5';
1355
		} else {
1356
			return false;
1357
		}
1358
1359
	}
1360
1361
	/**
1362
	 * Retrieve the blackberry browser version.
1363
	 *
1364
	 * Return string are from the following list:
1365
	 * - blackberry-10
1366
	 * - blackberry-webkit
1367
	 * - blackberry-5
1368
	 * - blackberry-4.7
1369
	 * - blackberry-4.6
1370
	 *
1371
	 * @return string Type of the BB browser.
1372
	 * If browser's version is not found, detect_blackbeberry_browser_version will return boolean false.
1373
	 */
1374
	public static function detect_blackberry_browser_version() {
1375
1376
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1377
			return false;
1378
		}
1379
1380
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1381
1382
		if ( self::is_blackberry_10() ) {
1383
			return 'blackberry-10';
1384
		}
1385
1386
		$pos_blackberry = strpos( $agent, 'blackberry' );
1387
		if ( false === $pos_blackberry ) {
1388
			// Not a blackberry device.
1389
			return false;
1390
		}
1391
1392
		$pos_webkit = strpos( $agent, 'webkit' );
1393
1394
		if ( ! ( false === $pos_webkit ) ) {
1395
			return 'blackberry-webkit';
1396
		} else {
1397
			if ( ! preg_match( '#BlackBerry\w+\/([\d\.]+)#i', $agent, $matches ) ) {
1398
				return false; // not a BB device that match our rule.
1399
			}
1400
1401
			$version_num = explode( '.', $matches[1] );
1402
1403 View Code Duplication
			if ( false === is_array( $version_num ) || count( $version_num ) <= 1 ) {
1404
				return false;
1405
			}
1406
1407
			$version_num_major = (int) $version_num[0];
1408
			$version_num_minor = (int) $version_num[1];
1409
1410
			if ( 5 === $version_num_major ) {
1411
				return 'blackberry-5';
1412
			} elseif ( 4 === $version_num_major && 7 === $version_num_minor ) {
1413
				return 'blackberry-4.7';
1414
			} elseif ( 4 === $version_num_major && 6 === $version_num_minor ) {
1415
				return 'blackberry-4.6';
1416
			} else {
1417
				// A very old BB device is found or this is a BB device that doesn't match our rules.
1418
				return false;
1419
			}
1420
		}
1421
1422
	}
1423
1424
	/**
1425
	 * Checks if a visitor is coming from one of the WordPress mobile apps.
1426
	 *
1427
	 * @return bool
1428
	 */
1429
	public static function is_mobile_app() {
1430
1431
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1432
			return false;
1433
		}
1434
1435
		$agent = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1436
1437
		if ( isset( $_SERVER['X_USER_AGENT'] ) && preg_match( '|wp-webos|', $_SERVER['X_USER_AGENT'] ) ) {
1438
			return true; // Wp4webos 1.1 or higher.
1439
		}
1440
1441
		$app_agents = array( 'wp-android', 'wp-blackberry', 'wp-iphone', 'wp-nokia', 'wp-webos', 'wp-windowsphone' );
1442
		// the mobile reader on iOS has an incorrect UA when loading the reader
1443
		// currently it is the default one provided by the iOS framework which
1444
		// causes problems with 2-step-auth
1445
		// User-Agent	WordPress/3.1.4 CFNetwork/609 Darwin/13.0.0.
1446
		$app_agents[] = 'wordpress/3.1';
1447
1448
		foreach ( $app_agents as $app_agent ) {
1449
			if ( false !== strpos( $agent, $app_agent ) ) {
1450
				return true;
1451
			}
1452
		}
1453
		return false;
1454
	}
1455
1456
	/**
1457
	 * Detects if the current browser is Nintendo 3DS handheld.
1458
	 *
1459
	 * Example: Mozilla/5.0 (Nintendo 3DS; U; ; en) Version/1.7498.US
1460
	 * can differ in language, version and region
1461
	 */
1462 View Code Duplication
	public static function is_Nintendo_3DS() {
1463
		if ( empty( $_SERVER['HTTP_USER_AGENT'] ) ) {
1464
			return false;
1465
		}
1466
1467
		$ua = strtolower( $_SERVER['HTTP_USER_AGENT'] );
1468
		if ( strpos( $ua, 'nintendo 3ds' ) !== false ) {
1469
			return true;
1470
		}
1471
		return false;
1472
	}
1473
1474
	/**
1475
	 * Was the current request made by a known bot?
1476
	 *
1477
	 * @return boolean
1478
	 */
1479
	public static function is_bot() {
1480
		static $is_bot = null;
1481
1482
		if ( is_null( $is_bot ) ) {
1483
			$is_bot = self::is_bot_user_agent( $_SERVER['HTTP_USER_AGENT'] );
1484
		}
1485
1486
		return $is_bot;
1487
	}
1488
1489
	/**
1490
	 * Is the given user-agent a known bot?
1491
	 * 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.
1492
	 *
1493
	 * @param string $ua A user-agent string.
0 ignored issues
show
Documentation introduced by
Should the type for parameter $ua not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
1494
	 *
1495
	 * @return boolean
1496
	 */
1497
	public static function is_bot_user_agent( $ua = null ) {
1498
1499
		if ( empty( $ua ) ) {
1500
			return false;
1501
		}
1502
1503
		$bot_agents = array(
1504
			'alexa',
1505
			'altavista',
1506
			'ask jeeves',
1507
			'attentio',
1508
			'baiduspider',
1509
			'bingbot',
1510
			'chtml generic',
1511
			'crawler',
1512
			'fastmobilecrawl',
1513
			'feedfetcher-google',
1514
			'firefly',
1515
			'froogle',
1516
			'gigabot',
1517
			'googlebot',
1518
			'googlebot-mobile',
1519
			'heritrix',
1520
			'httrack',
1521
			'ia_archiver',
1522
			'irlbot',
1523
			'iescholar',
1524
			'infoseek',
1525
			'jumpbot',
1526
			'linkcheck',
1527
			'lycos',
1528
			'mediapartners',
1529
			'mediobot',
1530
			'motionbot',
1531
			'msnbot',
1532
			'mshots',
1533
			'openbot',
1534
			'pss-webkit-request',
1535
			'pythumbnail',
1536
			'scooter',
1537
			'slurp',
1538
			'snapbot',
1539
			'spider',
1540
			'taptubot',
1541
			'technoratisnoop',
1542
			'teoma',
1543
			'twiceler',
1544
			'yahooseeker',
1545
			'yahooysmcm',
1546
			'yammybot',
1547
			'ahrefsbot',
1548
			'pingdom.com_bot',
1549
			'kraken',
1550
			'yandexbot',
1551
			'twitterbot',
1552
			'tweetmemebot',
1553
			'openhosebot',
1554
			'queryseekerspider',
1555
			'linkdexbot',
1556
			'grokkit-crawler',
1557
			'livelapbot',
1558
			'germcrawler',
1559
			'domaintunocrawler',
1560
			'grapeshotcrawler',
1561
			'cloudflare-alwaysonline',
1562
		);
1563
1564
		foreach ( $bot_agents as $bot_agent ) {
1565
			if ( false !== stripos( $ua, $bot_agent ) ) {
1566
				return true;
1567
			}
1568
		}
1569
1570
		return false;
1571
	}
1572
}
1573