|
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; |
|
|
|
|
|
|
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|boost|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; |
|
|
|
|
|
|
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
|
|
|
?> |
|
|
|
|
|
|
137
|
|
|
|