Completed
Push — branch-4.0 ( fc0d6f...bf547c )
by
unknown
08:22
created

Jetpack_User_Agent_Info::is_windows8_auth()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Importance

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