Fixed bug #16878: Make t3lib_utility_Client::getBrowserInfo() aware of more operating...
[Packages/TYPO3.CMS.git] / t3lib / utility / class.t3lib_utility_client.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2010 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 // system
107 // Microsoft Documentation about Platform tokens: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
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'] = 'win7';
114 $browserInfo['all_systems'][] = 'winNT';
115 } elseif (strstr($userAgent, 'Windows NT 6.0')) {
116 $browserInfo['system'] = 'winVista';
117 $browserInfo['all_systems'][] = 'winNT';
118 } elseif (strstr($userAgent, 'Windows NT 5.1')) {
119 $browserInfo['system'] = 'winXP';
120 $browserInfo['all_systems'][] = 'winNT';
121 } elseif (strstr($userAgent, 'Windows NT 5.0')) {
122 $browserInfo['system'] = 'win2k';
123 $browserInfo['all_systems'][] = 'winNT';
124 } elseif (strstr($userAgent, 'Win98') || strstr($userAgent, 'Windows 98')) {
125 $browserInfo['system'] = 'win98';
126 } elseif (strstr($userAgent, 'Win95') || strstr($userAgent, 'Windows 95')) {
127 $browserInfo['system'] = 'win95';
128 } elseif (strstr($userAgent, 'WinNT') || strstr($userAgent, 'Windows NT')) {
129 $browserInfo['system'] = 'winNT';
130 } elseif (strstr($userAgent, 'Win16') || strstr($userAgent, 'Windows 311')) {
131 $browserInfo['system'] = 'win311';
132 }
133 } elseif (strstr($userAgent, 'Mac')) {
134 if (strstr($userAgent, 'iPad') || strstr($userAgent, 'iPhone') || strstr($userAgent, 'iPod')) {
135 $browserInfo['system'] = 'iOS';
136 $browserInfo['all_systems'][] = 'mac';
137 } else {
138 $browserInfo['system'] = 'mac';
139 }
140 // unixes
141 } elseif (strstr($userAgent, 'Linux')) {
142 if (strstr($userAgent, 'Android')) {
143 $browserInfo['system'] = 'android';
144 $browserInfo['all_systems'][] = 'linux';
145 } else {
146 $browserInfo['system'] = 'linux';
147 }
148 } elseif (strstr($userAgent, 'BSD')) {
149 $browserInfo['system'] = 'unix_bsd';
150 } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
151 $browserInfo['system'] = 'unix_sgi';
152 } elseif (strstr($userAgent, ' SunOS ')) {
153 $browserInfo['system'] = 'unix_sun';
154 } elseif (strstr($userAgent, ' HP-UX ')) {
155 $browserInfo['system'] = 'unix_hp';
156 } elseif (strstr($userAgent, 'CrOS')) {
157 $browserInfo['system'] = 'chrome';
158 $browserInfo['all_systems'][] = 'linux';
159 }
160
161 return $browserInfo;
162 }
163
164 /**
165 * Returns the version of a browser; Basically getting doubleval() of the input string,
166 * stripping of any non-numeric values in the beginning of the string first.
167 *
168 * @param string $version: A string with version number, eg. "/7.32 blablabla"
169 * @return double Returns double value, eg. "7.32"
170 */
171 public static function getVersion($version) {
172 return doubleval(preg_replace('/^[^0-9]*/', '', $version));
173 }
174
175 /**
176 * Gets a code for a browsing device based on the input useragent string.
177 *
178 * @param string $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
179 * @return string Code for the specific device type
180 */
181 public static function getDeviceType($userAgent) {
182 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType']:
183 $getDeviceTypeHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType'];
184 if (is_array($getDeviceTypeHooks)) {
185 foreach ($getDeviceTypeHooks as $hookFunction) {
186 $returnResult = TRUE;
187 $hookParameters = array(
188 'userAgent' => &$userAgent,
189 'returnResult' => &$returnResult,
190 );
191
192 // need reference for third parameter in t3lib_div::callUserFunction,
193 // so create a reference to NULL
194 $null = NULL;
195 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, $null);
196 if ($returnResult && is_string($hookResult) && !empty($hookResult)) {
197 return $hookResult;
198 }
199 }
200 }
201
202 $userAgent = strtolower(trim($userAgent));
203 $deviceType = '';
204
205 // pda
206 if (strstr($userAgent, 'avantgo')) {
207 $deviceType = 'pda';
208 }
209 // wap
210 $browser = substr($userAgent, 0, 4);
211 $wapviwer = substr(stristr($userAgent, 'wap'), 0, 3);
212 if ($wapviwer == 'wap' ||
213 $browser == 'noki' ||
214 $browser == 'eric' ||
215 $browser == 'r380' ||
216 $browser == 'up.b' ||
217 $browser == 'winw' ||
218 $browser == 'wapa') {
219 $deviceType = 'wap';
220 }
221 // grabber
222 if (strstr($userAgent, 'g.r.a.b.') ||
223 strstr($userAgent, 'utilmind httpget') ||
224 strstr($userAgent, 'webcapture') ||
225 strstr($userAgent, 'teleport') ||
226 strstr($userAgent, 'webcopier')) {
227 $deviceType = 'grabber';
228 }
229 // robots
230 if (strstr($userAgent, 'crawler') ||
231 strstr($userAgent, 'spider') ||
232 strstr($userAgent, 'googlebot') ||
233 strstr($userAgent, 'searchbot') ||
234 strstr($userAgent, 'infoseek') ||
235 strstr($userAgent, 'altavista') ||
236 strstr($userAgent, 'diibot')) {
237 $deviceType = 'robot';
238 }
239
240 return $deviceType;
241 }
242 }
243
244 ?>