0e57ad3b85e22f15cd18d9452d5fc6d1786fc814
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / ClientUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Class to handle and determine browser specific information.
19 */
20 class ClientUtility
21 {
22 /**
23 * Generates an array with abstracted browser information
24 *
25 * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
26 * @return array Contains keys "browser", "version", "system
27 */
28 public static function getBrowserInfo($userAgent)
29 {
30 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo']:
31 $getBrowserInfoHooks = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo'];
32 if (is_array($getBrowserInfoHooks)) {
33 foreach ($getBrowserInfoHooks as $hookFunction) {
34 $returnResult = true;
35 $hookParameters = [
36 'userAgent' => &$userAgent,
37 'returnResult' => &$returnResult
38 ];
39 // need reference for third parameter in \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction,
40 // so create a reference to NULL
41 $null = null;
42 $hookResult = GeneralUtility::callUserFunction($hookFunction, $hookParameters, $null);
43 if ($returnResult && is_array($hookResult) && !empty($hookResult)) {
44 return $hookResult;
45 }
46 }
47 }
48 $userAgent = trim($userAgent);
49 $browserInfo = [
50 'useragent' => $userAgent
51 ];
52 // Analyze the userAgent string
53 // Declare known browsers to look for
54 $known = [
55 'msie',
56 'firefox',
57 'webkit',
58 'opera',
59 'netscape',
60 'konqueror',
61 'gecko',
62 'chrome',
63 'safari',
64 'seamonkey',
65 'navigator',
66 'mosaic',
67 'lynx',
68 'amaya',
69 'omniweb',
70 'avant',
71 'camino',
72 'flock',
73 'aol'
74 ];
75 $matches = [];
76 $pattern = '#(?P<browser>' . implode('|', $known) . ')[/ ]+(?P<version>[0-9]+(?:\\.[0-9]+)?)#';
77 // Find all phrases (or return empty array if none found)
78 if (!preg_match_all($pattern, strtolower($userAgent), $matches)) {
79 // Microsoft Internet-Explorer 11 does not have a sign of "MSIE" or so in the useragent.
80 // All checks from the pattern above fail here. Look for a special combination of
81 // "Mozilla/5.0" in front, "Trident/7.0" in the middle and "like Gecko" at the end.
82 // The version (revision) is given as "; rv:11.0" in the useragent then.
83 unset($matches);
84 $pattern = '#mozilla/5\\.0 \\(.*trident/7\\.0.*; rv:(?P<version>[0-9]+(?:\\.[0-9]+)?)\\) like gecko#i';
85 if (preg_match_all($pattern, $userAgent, $matches)) {
86 $browserInfo['browser'] = 'msie';
87 $browserInfo['version'] = $matches['version'][0];
88 $browserInfo['all'] = ['msie' => $matches['version'][0]];
89 } else {
90 $browserInfo['browser'] = 'unknown';
91 $browserInfo['version'] = '';
92 $browserInfo['all'] = [];
93 }
94 } else {
95 // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
96 // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
97 // in the UA). That's usually the most correct.
98 // For IE use the first match as IE sends multiple MSIE with version, from higher to lower.
99 $lastIndex = count($matches['browser']) - 1;
100 $browserInfo['browser'] = $matches['browser'][$lastIndex];
101 $browserInfo['version'] = $browserInfo['browser'] === 'msie' ? $matches['version'][0] : $matches['version'][$lastIndex];
102 // But return all parsed browsers / version in an extra array
103 $browserInfo['all'] = [];
104 for ($i = 0; $i <= $lastIndex; $i++) {
105 if (!isset($browserInfo['all'][$matches['browser'][$i]])) {
106 $browserInfo['all'][$matches['browser'][$i]] = $matches['version'][$i];
107 }
108 }
109 // Replace gecko build date with version given by rv
110 if (isset($browserInfo['all']['gecko'])) {
111 preg_match_all('/rv:([0-9\\.]*)/', strtolower($userAgent), $version);
112 if ($version[1][0]) {
113 $browserInfo['all']['gecko'] = $version[1][0];
114 }
115 }
116 }
117 $browserInfo['all_systems'] = [];
118 if (strstr($userAgent, 'Win')) {
119 // Windows
120 if (strstr($userAgent, 'Windows NT 6.2') || strstr($userAgent, 'Windows NT 6.3')) {
121 $browserInfo['all_systems'][] = 'win8';
122 $browserInfo['all_systems'][] = 'winNT';
123 } elseif (strstr($userAgent, 'Windows NT 6.1')) {
124 $browserInfo['all_systems'][] = 'win7';
125 $browserInfo['all_systems'][] = 'winNT';
126 } elseif (strstr($userAgent, 'Windows NT 6.0')) {
127 $browserInfo['all_systems'][] = 'winVista';
128 $browserInfo['all_systems'][] = 'winNT';
129 } elseif (strstr($userAgent, 'Windows NT 5.1')) {
130 $browserInfo['all_systems'][] = 'winXP';
131 $browserInfo['all_systems'][] = 'winNT';
132 } elseif (strstr($userAgent, 'Windows NT 5.0')) {
133 $browserInfo['all_systems'][] = 'win2k';
134 $browserInfo['all_systems'][] = 'winNT';
135 } elseif (strstr($userAgent, 'Win98') || strstr($userAgent, 'Windows 98')) {
136 $browserInfo['all_systems'][] = 'win98';
137 } elseif (strstr($userAgent, 'Win95') || strstr($userAgent, 'Windows 95')) {
138 $browserInfo['all_systems'][] = 'win95';
139 } elseif (strstr($userAgent, 'WinNT') || strstr($userAgent, 'Windows NT')) {
140 $browserInfo['all_systems'][] = 'winNT';
141 } elseif (strstr($userAgent, 'Win16') || strstr($userAgent, 'Windows 311')) {
142 $browserInfo['all_systems'][] = 'win311';
143 }
144 } elseif (strstr($userAgent, 'Mac')) {
145 if (strstr($userAgent, 'iPad') || strstr($userAgent, 'iPhone') || strstr($userAgent, 'iPod')) {
146 $browserInfo['all_systems'][] = 'iOS';
147 $browserInfo['all_systems'][] = 'mac';
148 } else {
149 $browserInfo['all_systems'][] = 'mac';
150 }
151 } elseif (strstr($userAgent, 'Android')) {
152 $browserInfo['all_systems'][] = 'android';
153 $browserInfo['all_systems'][] = 'linux';
154 } elseif (strstr($userAgent, 'Linux')) {
155 $browserInfo['all_systems'][] = 'linux';
156 } elseif (strstr($userAgent, 'BSD')) {
157 $browserInfo['all_systems'][] = 'unix_bsd';
158 } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
159 $browserInfo['all_systems'][] = 'unix_sgi';
160 } elseif (strstr($userAgent, ' SunOS ')) {
161 $browserInfo['all_systems'][] = 'unix_sun';
162 } elseif (strstr($userAgent, ' HP-UX ')) {
163 $browserInfo['all_systems'][] = 'unix_hp';
164 } elseif (strstr($userAgent, 'CrOS')) {
165 $browserInfo['all_systems'][] = 'chrome';
166 $browserInfo['all_systems'][] = 'linux';
167 }
168 return $browserInfo;
169 }
170
171 /**
172 * Returns the version of a browser; Basically getting float value of the input string,
173 * stripping of any non-numeric values in the beginning of the string first.
174 *
175 * @param string $version A string with version number, eg. "/7.32 blablabla
176 * @return float Returns double value, eg. "7.32
177 */
178 public static function getVersion($version)
179 {
180 return (float)preg_replace('/^[^0-9]*/', '', $version);
181 }
182
183 /**
184 * Gets a code for a browsing device based on the input useragent string.
185 *
186 * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
187 * @return string Code for the specific device type
188 */
189 public static function getDeviceType($userAgent)
190 {
191 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType']:
192 $getDeviceTypeHooks = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType'];
193 if (is_array($getDeviceTypeHooks)) {
194 foreach ($getDeviceTypeHooks as $hookFunction) {
195 $returnResult = true;
196 $hookParameters = [
197 'userAgent' => &$userAgent,
198 'returnResult' => &$returnResult
199 ];
200 // need reference for third parameter in \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction,
201 // so create a reference to NULL
202 $null = null;
203 $hookResult = \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($hookFunction, $hookParameters, $null);
204 if ($returnResult && is_string($hookResult) && !empty($hookResult)) {
205 return $hookResult;
206 }
207 }
208 }
209 $userAgent = strtolower(trim($userAgent));
210 $deviceType = '';
211 // pda
212 if (strstr($userAgent, 'avantgo')) {
213 $deviceType = 'pda';
214 }
215 // wap
216 $browser = substr($userAgent, 0, 4);
217 $wapviwer = substr(stristr($userAgent, 'wap'), 0, 3);
218 if ($wapviwer == 'wap' || $browser == 'noki' || $browser == 'eric' || $browser == 'r380' || $browser == 'up.b' || $browser == 'winw' || $browser == 'wapa') {
219 $deviceType = 'wap';
220 }
221 // grabber
222 if (strstr($userAgent, 'g.r.a.b.') || strstr($userAgent, 'utilmind httpget') || strstr($userAgent, 'webcapture') || strstr($userAgent, 'teleport') || strstr($userAgent, 'webcopier')) {
223 $deviceType = 'grabber';
224 }
225 // robots
226 if (strstr($userAgent, 'crawler') || strstr($userAgent, 'spider') || strstr($userAgent, 'googlebot') || strstr($userAgent, 'searchbot') || strstr($userAgent, 'infoseek') || strstr($userAgent, 'altavista') || strstr($userAgent, 'diibot')) {
227 $deviceType = 'robot';
228 }
229 return $deviceType;
230 }
231 }