[TASK] Change "true" into "TRUE"
[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, 'Linux')) {
150 if (strstr($userAgent, 'Android')) {
151 $browserInfo['system'] = 'linux'; // backwards compatible
152 $browserInfo['all_systems'][] = 'android';
153 $browserInfo['all_systems'][] = 'linux';
154 } else {
155 $browserInfo['system'] = 'linux';
156 $browserInfo['all_systems'][] = 'linux';
157 }
158 } elseif (strstr($userAgent, 'BSD')) {
159 $browserInfo['system'] = 'unix_bsd';
160 $browserInfo['all_systems'][] = 'unix_bsd';
161 } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
162 $browserInfo['system'] = 'unix_sgi';
163 $browserInfo['all_systems'][] = 'unix_sgi';
164 } elseif (strstr($userAgent, ' SunOS ')) {
165 $browserInfo['system'] = 'unix_sun';
166 $browserInfo['all_systems'][] = 'unix_sun';
167 } elseif (strstr($userAgent, ' HP-UX ')) {
168 $browserInfo['system'] = 'unix_hp';
169 $browserInfo['all_systems'][] = 'unix_hp';
170 } elseif (strstr($userAgent, 'CrOS')) {
171 $browserInfo['system'] = 'linux';
172 $browserInfo['all_systems'][] = 'chrome';
173 $browserInfo['all_systems'][] = 'linux';
174 }
175
176 return $browserInfo;
177 }
178
179 /**
180 * Returns the version of a browser; Basically getting doubleval() of the input string,
181 * stripping of any non-numeric values in the beginning of the string first.
182 *
183 * @param string $version: A string with version number, eg. "/7.32 blablabla"
184 * @return double Returns double value, eg. "7.32"
185 */
186 public static function getVersion($version) {
187 return doubleval(preg_replace('/^[^0-9]*/', '', $version));
188 }
189
190 /**
191 * Gets a code for a browsing device based on the input useragent string.
192 *
193 * @param string $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
194 * @return string Code for the specific device type
195 */
196 public static function getDeviceType($userAgent) {
197 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType']:
198 $getDeviceTypeHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType'];
199 if (is_array($getDeviceTypeHooks)) {
200 foreach ($getDeviceTypeHooks as $hookFunction) {
201 $returnResult = TRUE;
202 $hookParameters = array(
203 'userAgent' => &$userAgent,
204 'returnResult' => &$returnResult,
205 );
206
207 // need reference for third parameter in t3lib_div::callUserFunction,
208 // so create a reference to NULL
209 $null = NULL;
210 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, $null);
211 if ($returnResult && is_string($hookResult) && !empty($hookResult)) {
212 return $hookResult;
213 }
214 }
215 }
216
217 $userAgent = strtolower(trim($userAgent));
218 $deviceType = '';
219
220 // pda
221 if (strstr($userAgent, 'avantgo')) {
222 $deviceType = 'pda';
223 }
224 // wap
225 $browser = substr($userAgent, 0, 4);
226 $wapviwer = substr(stristr($userAgent, 'wap'), 0, 3);
227 if ($wapviwer == 'wap' ||
228 $browser == 'noki' ||
229 $browser == 'eric' ||
230 $browser == 'r380' ||
231 $browser == 'up.b' ||
232 $browser == 'winw' ||
233 $browser == 'wapa') {
234 $deviceType = 'wap';
235 }
236 // grabber
237 if (strstr($userAgent, 'g.r.a.b.') ||
238 strstr($userAgent, 'utilmind httpget') ||
239 strstr($userAgent, 'webcapture') ||
240 strstr($userAgent, 'teleport') ||
241 strstr($userAgent, 'webcopier')) {
242 $deviceType = 'grabber';
243 }
244 // robots
245 if (strstr($userAgent, 'crawler') ||
246 strstr($userAgent, 'spider') ||
247 strstr($userAgent, 'googlebot') ||
248 strstr($userAgent, 'searchbot') ||
249 strstr($userAgent, 'infoseek') ||
250 strstr($userAgent, 'altavista') ||
251 strstr($userAgent, 'diibot')) {
252 $deviceType = 'robot';
253 }
254
255 return $deviceType;
256 }
257 }
258
259 ?>