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