Fixed bug #14458: getBrowserInfo fails with IE UA Strings
[Packages/TYPO3.CMS.git] / t3lib / utility / class.t3lib_utility_client.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 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 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, NULL);
55 if ($returnResult && is_array($hookResult) && count($hookResult)) {
56 return $hookResult;
57 }
58 }
59 }
60
61 $userAgent = trim($userAgent);
62 $browserInfo = array(
63 'useragent' => $userAgent,
64 );
65
66 // Analyze the userAgent string
67 // Declare known browsers to look for
68
69 $known = array('msie', 'firefox', 'webkit', 'opera', 'netscape', 'konqueror',
70 'gecko', 'chrome', 'safari', 'seamonkey', 'navigator', 'mosaic',
71 'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol');
72 $matches = array();
73
74 $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';
75 // Find all phrases (or return empty array if none found)
76 if (!preg_match_all($pattern, strtolower($userAgent), $matches)) {
77 $browserInfo['browser'] = 'unknown';
78 } else {
79 // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
80 // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
81 // in the UA). That's usually the most correct.
82 // For IE use the first match as IE sends multiple MSIE with version, from higher to lower.
83 $lastIndex = count($matches['browser']) - 1;
84 $browserInfo['browser'] = $matches['browser'][$lastIndex];
85 $browserInfo['version'] = $browserInfo['browser'] === 'msie' ? $matches['version'][0] : $matches['version'][$lastIndex];
86 //But return all parsed browsers / version in an extra array
87 for ($i = 0; $i <= $lastIndex; $i++) {
88 if (!isset($browserInfo['all'][$matches['browser'][$i]])) {
89 $browserInfo['all'][$matches['browser'][$i]] = $matches['version'][$i];
90 }
91 }
92 }
93
94
95 // system
96 $browserInfo['system'] = '';
97 if (strstr($userAgent, 'Win')) {
98 // windows
99 if (strstr($userAgent, 'Win98') || strstr($userAgent, 'Windows 98')) {
100 $browserInfo['system'] = 'win98';
101 } elseif (strstr($userAgent, 'Win95') || strstr($userAgent, 'Windows 95')) {
102 $browserInfo['system'] = 'win95';
103 } elseif (strstr($userAgent, 'WinNT') || strstr($userAgent, 'Windows NT')) {
104 $browserInfo['system'] = 'winNT';
105 } elseif (strstr($userAgent, 'Win16') || strstr($userAgent, 'Windows 311')) {
106 $browserInfo['system'] = 'win311';
107 }
108 } elseif (strstr($userAgent,'Mac')) {
109 $browserInfo['system'] = 'mac';
110 // unixes
111 } elseif (strstr($userAgent, 'Linux')) {
112 $browserInfo['system'] = 'linux';
113 } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
114 $browserInfo['system'] = 'unix_sgi';
115 } elseif (strstr($userAgent, ' SunOS ')) {
116 $browserInfo['system'] = 'unix_sun';
117 } elseif (strstr($userAgent, ' HP-UX ')) {
118 $browserInfo['system'] = 'unix_hp';
119 }
120
121 return $browserInfo;
122 }
123
124 /**
125 * Returns the version of a browser; Basically getting doubleval() of the input string,
126 * stripping of any non-numeric values in the beginning of the string first.
127 *
128 * @param string $version: A string with version number, eg. "/7.32 blablabla"
129 * @return double Returns double value, eg. "7.32"
130 */
131 public static function getVersion($version) {
132 return doubleval(preg_replace('/^[^0-9]*/', '', $version));
133 }
134
135 /**
136 * Gets a code for a browsing device based on the input useragent string.
137 *
138 * @param string $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
139 * @return string Code for the specific device type
140 */
141 public static function getDeviceType($userAgent) {
142 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType']:
143 $getDeviceTypeHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType'];
144 if (is_array($getDeviceTypeHooks)) {
145 foreach ($getDeviceTypeHooks as $hookFunction) {
146 $returnResult = true;
147 $hookParameters = array(
148 'userAgent' => &$userAgent,
149 'returnResult' => &$returnResult,
150 );
151
152 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, NULL);
153 if ($returnResult && is_string($hookResult) && !empty($hookResult)) {
154 return $hookResult;
155 }
156 }
157 }
158
159 $userAgent = strtolower(trim($userAgent));
160 $deviceType = '';
161
162 // pda
163 if(strstr($userAgent, 'avantgo')) {
164 $deviceType = 'pda';
165 }
166 // wap
167 $browser=substr($userAgent, 0, 4);
168 $wapviwer=substr(stristr($userAgent,'wap'), 0, 3);
169 if($wapviwer == 'wap' ||
170 $browser == 'noki' ||
171 $browser == 'eric' ||
172 $browser == 'r380' ||
173 $browser == 'up.b' ||
174 $browser == 'winw' ||
175 $browser == 'wapa') {
176 $deviceType = 'wap';
177 }
178 // grabber
179 if(strstr($userAgent, 'g.r.a.b.') ||
180 strstr($userAgent, 'utilmind httpget') ||
181 strstr($userAgent, 'webcapture') ||
182 strstr($userAgent, 'teleport') ||
183 strstr($userAgent, 'webcopier')) {
184 $deviceType = 'grabber';
185 }
186 // robots
187 if(strstr($userAgent, 'crawler') ||
188 strstr($userAgent, 'spider') ||
189 strstr($userAgent, 'googlebot') ||
190 strstr($userAgent, 'searchbot') ||
191 strstr($userAgent, 'infoseek') ||
192 strstr($userAgent, 'altavista') ||
193 strstr($userAgent, 'diibot')) {
194 $deviceType = 'robot';
195 }
196
197 return $deviceType;
198 }
199 }
200 ?>