Follow-up to issue #16878: Keep t3lib_utility_Client::getBrowserInfo backwards compat...
[Packages/TYPO3.CMS.git] / t3lib / utility / class.t3lib_utility_client.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 Oliver Hader <oliver@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Class to handle and determine browser specific information.
30 *
31 * $Id$
32 *
33 * @author Oliver Hader <oliver@typo3.org>
34 */
35 final class t3lib_utility_Client {
36
37 /**
38 * Generates an array with abstracted browser information
39 *
40 * @param string $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
41 * @return array Contains keys "browser", "version", "system"
42 */
43 public static function getBrowserInfo($userAgent) {
44 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo']:
45 $getBrowserInfoHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo'];
46 if (is_array($getBrowserInfoHooks)) {
47 foreach ($getBrowserInfoHooks as $hookFunction) {
48 $returnResult = true;
49 $hookParameters = array(
50 'userAgent' => &$userAgent,
51 'returnResult' => &$returnResult,
52 );
53
54 // need reference for third parameter in t3lib_div::callUserFunction,
55 // so create a reference to NULL
56 $null = NULL;
57 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, $null);
58 if ($returnResult && is_array($hookResult) && count($hookResult)) {
59 return $hookResult;
60 }
61 }
62 }
63
64 $userAgent = trim($userAgent);
65 $browserInfo = array(
66 'useragent' => $userAgent,
67 );
68
69 // Analyze the userAgent string
70 // Declare known browsers to look for
71
72 $known = array('msie', 'firefox', 'webkit', 'opera', 'netscape', 'konqueror',
73 'gecko', 'chrome', 'safari', 'seamonkey', 'navigator', 'mosaic',
74 'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol');
75 $matches = array();
76
77 $pattern = '#(?P<browser>' . join('|', $known) . ')[/ ]+(?P<version>[0-9]+(?:\.[0-9]+)?)#';
78 // Find all phrases (or return empty array if none found)
79 if (!preg_match_all($pattern, strtolower($userAgent), $matches)) {
80 $browserInfo['browser'] = 'unknown';
81 $browserInfo['version'] = '';
82 $browserInfo['all'] = array();
83 } else {
84 // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
85 // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
86 // in the UA). That's usually the most correct.
87 // For IE use the first match as IE sends multiple MSIE with version, from higher to lower.
88 $lastIndex = count($matches['browser']) - 1;
89 $browserInfo['browser'] = $matches['browser'][$lastIndex];
90 $browserInfo['version'] = $browserInfo['browser'] === 'msie' ? $matches['version'][0] : $matches['version'][$lastIndex];
91 //But return all parsed browsers / version in an extra array
92 for ($i = 0; $i <= $lastIndex; $i++) {
93 if (!isset($browserInfo['all'][$matches['browser'][$i]])) {
94 $browserInfo['all'][$matches['browser'][$i]] = $matches['version'][$i];
95 }
96 }
97 //Replace gecko build date with version given by rv
98 if (isset($browserInfo['all']['gecko'])) {
99 preg_match_all('/rv:([0-9\.]*)/', strtolower($userAgent), $version);
100 if ($version[1][0]) {
101 $browserInfo['all']['gecko'] = $version[1][0];
102 }
103 }
104 }
105
106 // Microsoft Documentation about Platform tokens: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
107 // 'system' is deprecated, use 'all_systems' (array) in future!
108 $browserInfo['system'] = '';
109 $browserInfo['all_systems'] = '';
110 if (strstr($userAgent, 'Win')) {
111 // windows
112 if (strstr($userAgent, 'Windows NT 6.1')) {
113 $browserInfo['system'] = 'winNT'; // backwards compatible
114 $browserInfo['all_systems'][] = 'win7';
115 $browserInfo['all_systems'][] = 'winNT';
116 } elseif (strstr($userAgent, 'Windows NT 6.0')) {
117 $browserInfo['system'] = 'winNT'; // backwards compatible
118 $browserInfo['all_systems'][] = 'winVista';
119 $browserInfo['all_systems'][] = 'winNT';
120 } elseif (strstr($userAgent, 'Windows NT 5.1')) {
121 $browserInfo['system'] = 'winNT'; // backwards compatible
122 $browserInfo['all_systems'][] = 'winXP';
123 $browserInfo['all_systems'][] = 'winNT';
124 } elseif (strstr($userAgent, 'Windows NT 5.0')) {
125 $browserInfo['system'] = 'winNT'; // backwards compatible
126 $browserInfo['all_systems'][] = 'win2k';
127 $browserInfo['all_systems'][] = 'winNT';
128 } elseif (strstr($userAgent, 'Win98') || strstr($userAgent, 'Windows 98')) {
129 $browserInfo['system'] = 'win98';
130 $browserInfo['all_systems'][] = 'win98';
131 } elseif (strstr($userAgent, 'Win95') || strstr($userAgent, 'Windows 95')) {
132 $browserInfo['system'] = 'win95';
133 $browserInfo['all_systems'][] = 'win95';
134 } elseif (strstr($userAgent, 'WinNT') || strstr($userAgent, 'Windows NT')) {
135 $browserInfo['system'] = 'winNT';
136 $browserInfo['all_systems'][] = 'winNT';
137 } elseif (strstr($userAgent, 'Win16') || strstr($userAgent, 'Windows 311')) {
138 $browserInfo['system'] = 'win311';
139 $browserInfo['all_systems'][] = 'win311';
140 }
141 } elseif (strstr($userAgent, 'Mac')) {
142 if (strstr($userAgent, 'iPad') || strstr($userAgent, 'iPhone') || strstr($userAgent, 'iPod')) {
143 $browserInfo['system'] = 'mac'; // backwards compatible
144 $browserInfo['all_systems'][] = 'iOS';
145 $browserInfo['all_systems'][] = 'mac';
146 } else {
147 $browserInfo['system'] = 'mac';
148 $browserInfo['all_systems'][] = 'mac';
149 }
150 // unixes
151 } elseif (strstr($userAgent, 'Linux')) {
152 if (strstr($userAgent, 'Android')) {
153 $browserInfo['system'] = 'linux'; // backwards compatible
154 $browserInfo['all_systems'][] = 'android';
155 $browserInfo['all_systems'][] = 'linux';
156 } else {
157 $browserInfo['system'] = 'linux';
158 $browserInfo['all_systems'][] = 'linux';
159 }
160 } elseif (strstr($userAgent, 'BSD')) {
161 $browserInfo['system'] = 'unix_bsd';
162 $browserInfo['all_systems'][] = 'unix_bsd';
163 } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
164 $browserInfo['system'] = 'unix_sgi';
165 $browserInfo['all_systems'][] = 'unix_sgi';
166 } elseif (strstr($userAgent, ' SunOS ')) {
167 $browserInfo['system'] = 'unix_sun';
168 $browserInfo['all_systems'][] = 'unix_sun';
169 } elseif (strstr($userAgent, ' HP-UX ')) {
170 $browserInfo['system'] = 'unix_hp';
171 $browserInfo['all_systems'][] = 'unix_hp';
172 } elseif (strstr($userAgent, 'CrOS')) {
173 $browserInfo['system'] = 'linux';
174 $browserInfo['all_systems'][] = 'chrome';
175 $browserInfo['all_systems'][] = 'linux';
176 }
177
178 return $browserInfo;
179 }
180
181 /**
182 * Returns the version of a browser; Basically getting doubleval() of the input string,
183 * stripping of any non-numeric values in the beginning of the string first.
184 *
185 * @param string $version: A string with version number, eg. "/7.32 blablabla"
186 * @return double Returns double value, eg. "7.32"
187 */
188 public static function getVersion($version) {
189 return doubleval(preg_replace('/^[^0-9]*/', '', $version));
190 }
191
192 /**
193 * Gets a code for a browsing device based on the input useragent string.
194 *
195 * @param string $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
196 * @return string Code for the specific device type
197 */
198 public static function getDeviceType($userAgent) {
199 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType']:
200 $getDeviceTypeHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType'];
201 if (is_array($getDeviceTypeHooks)) {
202 foreach ($getDeviceTypeHooks as $hookFunction) {
203 $returnResult = TRUE;
204 $hookParameters = array(
205 'userAgent' => &$userAgent,
206 'returnResult' => &$returnResult,
207 );
208
209 // need reference for third parameter in t3lib_div::callUserFunction,
210 // so create a reference to NULL
211 $null = NULL;
212 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, $null);
213 if ($returnResult && is_string($hookResult) && !empty($hookResult)) {
214 return $hookResult;
215 }
216 }
217 }
218
219 $userAgent = strtolower(trim($userAgent));
220 $deviceType = '';
221
222 // pda
223 if (strstr($userAgent, 'avantgo')) {
224 $deviceType = 'pda';
225 }
226 // wap
227 $browser = substr($userAgent, 0, 4);
228 $wapviwer = substr(stristr($userAgent, 'wap'), 0, 3);
229 if ($wapviwer == 'wap' ||
230 $browser == 'noki' ||
231 $browser == 'eric' ||
232 $browser == 'r380' ||
233 $browser == 'up.b' ||
234 $browser == 'winw' ||
235 $browser == 'wapa') {
236 $deviceType = 'wap';
237 }
238 // grabber
239 if (strstr($userAgent, 'g.r.a.b.') ||
240 strstr($userAgent, 'utilmind httpget') ||
241 strstr($userAgent, 'webcapture') ||
242 strstr($userAgent, 'teleport') ||
243 strstr($userAgent, 'webcopier')) {
244 $deviceType = 'grabber';
245 }
246 // robots
247 if (strstr($userAgent, 'crawler') ||
248 strstr($userAgent, 'spider') ||
249 strstr($userAgent, 'googlebot') ||
250 strstr($userAgent, 'searchbot') ||
251 strstr($userAgent, 'infoseek') ||
252 strstr($userAgent, 'altavista') ||
253 strstr($userAgent, 'diibot')) {
254 $deviceType = 'robot';
255 }
256
257 return $deviceType;
258 }
259 }
260
261 ?>