Test Failed
Pull Request — master (#236)
by
unknown
04:56
created

Browser::isAppleiOS()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
dl 0
loc 10
rs 10
cc 3
nc 3
nop 0
1
<?php
2
3
/**
4
 * Copyright (c) 2018 Justin Kuenzel (jukusoft.com)
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
20
/**
21
 * Project: RocketCMS
22
 * License: Apache 2.0 license
23
 * User: Justin
24
 * Date: 05.03.2018
25
 * Time: 01:57
26
 */
27
28
class Browser {
29
30
	//cached values
31
	protected static $isMobile = false;
32
	protected static $mobile_checked = false;
33
	protected static $isTablet = false;
34
	protected static $tablet_checked = false;
35
36
	//https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php
37
38
	/**
39
	 * check, if browser is mobile
40
	 *
41
	 * @return true, if browser is mobile
42
	 */
43
	public static function isMobile () : bool {
44
		//in-memory cache
45
		if (self::$mobile_checked) {
46
			return self::$isMobile;
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::isMobile returns the type boolean which is incompatible with the documented return type true.
Loading history...
47
		}
48
49
		//customized from: https://stackoverflow.com/questions/4117555/simplest-way-to-detect-a-mobile-device
50
		//https://stackoverflow.com/questions/4117555/simplest-way-to-detect-a-mobile-device
51
		$value = preg_match("/(android|webos|avantgo|iphone|ipad|ipod|blackberry|iemobile|bolt|bo‌​ost|cricket|docomo|fone|hiptop|mini|opera mini|kitkat|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", self::getUserAgent());
52
53
		//cache values (in local in-memory cache)
54
		self::$isMobile = $value;
55
		self::$mobile_checked = true;
56
57
		return $value;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value returns the type integer which is incompatible with the type-hinted return boolean.
Loading history...
58
	}
59
60
	public static function isMobilePhone () : bool {
61
		throw new Exception("method Browser::isMobilePhone() isnt implemented yet.");
62
63
		//TODO: add code here
64
	}
65
66
	public static function isTablet () : bool {
67
		//in-memory cache
68
		if (self::$tablet_checked) {
69
			return self::$isTablet;
70
		}
71
72
		//https://www.phpclasses.org/browse/file/48225.html
73
		//https://mobiforge.com/design-development/tablet-and-mobile-device-detection-php
74
75
		//TODO: ATTENTION! Rewrite this method so it will result into better performance!
76
77
		$user_agent = self::getUserAgent();
78
79
		$tablet_browser = 0;
80
81
		if (preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i', strtolower($user_agent))) {
82
			$tablet_browser++;
83
		}
84
85
		if (strpos(strtolower($user_agent),'opera mini') > 0) {
86
			//Check for tablets on opera mini alternative headers
87
			$stock_ua = strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) ? $_SERVER['HTTP_X_OPERAMINI_PHONE_UA'] : (isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));
88
89
			if (preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i', $stock_ua)) {
90
				$tablet_browser++;
91
			}
92
		}
93
94
		$value = $tablet_browser > 0;
95
96
		//cache values (in local in-memory cache)
97
		self::$isTablet = $value;
98
		self::$tablet_checked = true;
99
100
		return $value;
101
	}
102
103
	public static function isAppleiOS () : bool {
104
		$user_agent = self::getUserAgent();
105
106
		$iPod    = stripos($user_agent,"iPod");
107
		$iPhone  = stripos($user_agent,"iPhone");
108
		$iPad    = stripos($user_agent,"iPad");
109
		//$Android = stripos($_SERVER['HTTP_USER_AGENT'],"Android");
110
		//$webOS   = stripos($_SERVER['HTTP_USER_AGENT'],"webOS");
111
112
		return $iPod !== false || $iPhone !== false || $iPad !== false;
113
	}
114
115
	public static function isAndroid () : bool {
116
		return stripos(self::getUserAgent(),'android') !== false;
117
	}
118
119
	public static function getUserAgent () : string {
120
		$user_agent = "";
121
122
		if (isset($_SERVER['HTTP_USER_AGENT'])) {
123
			$user_agent = strtolower(htmlentities($_SERVER['HTTP_USER_AGENT']));
124
		}
125
126
		//throw event, so plugins can modify user agent
127
		Events::throwEvent("get_user_agent", array(
128
			'user_agent' => &$user_agent,
129
		));
130
131
		return $user_agent;
132
	}
133
134
}
135
136
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
137