[TASK] Deprecate ClientUtility
[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 * @deprecated
28 */
29 public static function getBrowserInfo($userAgent)
30 {
31 trigger_error('Method getBrowserInfo() is deprecated since v9 and will be removed with v10', E_USER_DEPRECATED);
32 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo']:
33 $getBrowserInfoHooks = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo'];
34 if (is_array($getBrowserInfoHooks)) {
35 foreach ($getBrowserInfoHooks as $hookFunction) {
36 $returnResult = true;
37 $hookParameters = [
38 'userAgent' => &$userAgent,
39 'returnResult' => &$returnResult
40 ];
41 // need reference for third parameter in \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction,
42 // so create a reference to NULL
43 $null = null;
44 $hookResult = GeneralUtility::callUserFunction($hookFunction, $hookParameters, $null);
45 if ($returnResult && is_array($hookResult) && !empty($hookResult)) {
46 return $hookResult;
47 }
48 }
49 }
50 $userAgent = trim($userAgent);
51 $browserInfo = [
52 'useragent' => $userAgent
53 ];
54 // Analyze the userAgent string
55 // Declare known browsers to look for
56 $known = [
57 'msie',
58 'firefox',
59 'webkit',
60 'opera',
61 'netscape',
62 'konqueror',
63 'gecko',
64 'chrome',
65 'safari',
66 'seamonkey',
67 'navigator',
68 'mosaic',
69 'lynx',
70 'amaya',
71 'omniweb',
72 'avant',
73 'camino',
74 'flock',
75 'aol'
76 ];
77 $matches = [];
78 $pattern = '#(?P<browser>' . implode('|', $known) . ')[/ ]+(?P<version>[0-9]+(?:\\.[0-9]+)?)#';
79 // Find all phrases (or return empty array if none found)
80 if (!preg_match_all($pattern, strtolower($userAgent), $matches)) {
81 // Microsoft Internet-Explorer 11 does not have a sign of "MSIE" or so in the useragent.
82 // All checks from the pattern above fail here. Look for a special combination of
83 // "Mozilla/5.0" in front, "Trident/7.0" in the middle and "like Gecko" at the end.
84 // The version (revision) is given as "; rv:11.0" in the useragent then.
85 unset($matches);
86 $pattern = '#mozilla/5\\.0 \\(.*trident/7\\.0.*; rv:(?P<version>[0-9]+(?:\\.[0-9]+)?)\\) like gecko#i';
87 if (preg_match_all($pattern, $userAgent, $matches)) {
88 $browserInfo['browser'] = 'msie';
89 $browserInfo['version'] = $matches['version'][0];
90 $browserInfo['all'] = ['msie' => $matches['version'][0]];
91 } else {
92 $browserInfo['browser'] = 'unknown';
93 $browserInfo['version'] = '';
94 $browserInfo['all'] = [];
95 }
96 } else {
97 // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
98 // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
99 // in the UA). That's usually the most correct.
100 // For IE use the first match as IE sends multiple MSIE with version, from higher to lower.
101 $lastIndex = count($matches['browser']) - 1;
102 $browserInfo['browser'] = $matches['browser'][$lastIndex];
103 $browserInfo['version'] = $browserInfo['browser'] === 'msie' ? $matches['version'][0] : $matches['version'][$lastIndex];
104 // But return all parsed browsers / version in an extra array
105 $browserInfo['all'] = [];
106 for ($i = 0; $i <= $lastIndex; $i++) {
107 if (!isset($browserInfo['all'][$matches['browser'][$i]])) {
108 $browserInfo['all'][$matches['browser'][$i]] = $matches['version'][$i];
109 }
110 }
111 // Replace gecko build date with version given by rv
112 if (isset($browserInfo['all']['gecko'])) {
113 preg_match_all('/rv:([0-9\\.]*)/', strtolower($userAgent), $version);
114 if ($version[1][0]) {
115 $browserInfo['all']['gecko'] = $version[1][0];
116 }
117 }
118 }
119 $browserInfo['all_systems'] = [];
120 if (strstr($userAgent, 'Win')) {
121 // Windows
122 if (strstr($userAgent, 'Windows NT 6.2') || strstr($userAgent, 'Windows NT 6.3')) {
123 $browserInfo['all_systems'][] = 'win8';
124 $browserInfo['all_systems'][] = 'winNT';
125 } elseif (strstr($userAgent, 'Windows NT 6.1')) {
126 $browserInfo['all_systems'][] = 'win7';
127 $browserInfo['all_systems'][] = 'winNT';
128 } elseif (strstr($userAgent, 'Windows NT 6.0')) {
129 $browserInfo['all_systems'][] = 'winVista';
130 $browserInfo['all_systems'][] = 'winNT';
131 } elseif (strstr($userAgent, 'Windows NT 5.1')) {
132 $browserInfo['all_systems'][] = 'winXP';
133 $browserInfo['all_systems'][] = 'winNT';
134 } elseif (strstr($userAgent, 'Windows NT 5.0')) {
135 $browserInfo['all_systems'][] = 'win2k';
136 $browserInfo['all_systems'][] = 'winNT';
137 } elseif (strstr($userAgent, 'Win98') || strstr($userAgent, 'Windows 98')) {
138 $browserInfo['all_systems'][] = 'win98';
139 } elseif (strstr($userAgent, 'Win95') || strstr($userAgent, 'Windows 95')) {
140 $browserInfo['all_systems'][] = 'win95';
141 } elseif (strstr($userAgent, 'WinNT') || strstr($userAgent, 'Windows NT')) {
142 $browserInfo['all_systems'][] = 'winNT';
143 } elseif (strstr($userAgent, 'Win16') || strstr($userAgent, 'Windows 311')) {
144 $browserInfo['all_systems'][] = 'win311';
145 }
146 } elseif (strstr($userAgent, 'Mac')) {
147 if (strstr($userAgent, 'iPad') || strstr($userAgent, 'iPhone') || strstr($userAgent, 'iPod')) {
148 $browserInfo['all_systems'][] = 'iOS';
149 $browserInfo['all_systems'][] = 'mac';
150 } else {
151 $browserInfo['all_systems'][] = 'mac';
152 }
153 } elseif (strstr($userAgent, 'Android')) {
154 $browserInfo['all_systems'][] = 'android';
155 $browserInfo['all_systems'][] = 'linux';
156 } elseif (strstr($userAgent, 'Linux')) {
157 $browserInfo['all_systems'][] = 'linux';
158 } elseif (strstr($userAgent, 'BSD')) {
159 $browserInfo['all_systems'][] = 'unix_bsd';
160 } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
161 $browserInfo['all_systems'][] = 'unix_sgi';
162 } elseif (strstr($userAgent, ' SunOS ')) {
163 $browserInfo['all_systems'][] = 'unix_sun';
164 } elseif (strstr($userAgent, ' HP-UX ')) {
165 $browserInfo['all_systems'][] = 'unix_hp';
166 } elseif (strstr($userAgent, 'CrOS')) {
167 $browserInfo['all_systems'][] = 'chrome';
168 $browserInfo['all_systems'][] = 'linux';
169 }
170 return $browserInfo;
171 }
172
173 /**
174 * Returns the version of a browser; Basically getting float value of the input string,
175 * stripping of any non-numeric values in the beginning of the string first.
176 *
177 * @param string $version A string with version number, eg. "/7.32 blablabla
178 * @return float Returns double value, eg. "7.32
179 * @deprecated
180 */
181 public static function getVersion($version)
182 {
183 trigger_error('Method getVersion() is deprecated since v9 and will be removed with v10', E_USER_DEPRECATED);
184 return (float)preg_replace('/^[^0-9]*/', '', $version);
185 }
186 }