Cleanup #12278: Move t3lib_matchCondition::whichDevice() to t3lib_utility_Client...
[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 if ($userAgent) {
67 // browser
68 if (strstr($userAgent,'MSIE')) {
69 $browserInfo['browser']='msie';
70 } elseif(strstr($userAgent,'Konqueror')) {
71 $browserInfo['browser']='konqueror';
72 } elseif(strstr($userAgent,'Opera')) {
73 $browserInfo['browser']='opera';
74 } elseif(strstr($userAgent,'Lynx')) {
75 $browserInfo['browser']='lynx';
76 } elseif(strstr($userAgent,'PHP')) {
77 $browserInfo['browser']='php';
78 } elseif(strstr($userAgent,'AvantGo')) {
79 $browserInfo['browser']='avantgo';
80 } elseif(strstr($userAgent,'WebCapture')) {
81 $browserInfo['browser']='acrobat';
82 } elseif(strstr($userAgent,'IBrowse')) {
83 $browserInfo['browser']='ibrowse';
84 } elseif(strstr($userAgent,'Teleport')) {
85 $browserInfo['browser']='teleport';
86 } elseif(strstr($userAgent,'Mozilla')) {
87 $browserInfo['browser']='netscape';
88 } else {
89 $browserInfo['browser']='unknown';
90 }
91
92 // version
93 switch($browserInfo['browser']) {
94 case 'netscape':
95 $browserInfo['version'] = self::getVersion(substr($userAgent, 8));
96 if (strstr($userAgent, 'Netscape6')) {
97 $browserInfo['version'] = 6;
98 }
99 break;
100 case 'msie':
101 $tmp = strstr($userAgent, 'MSIE');
102 $browserInfo['version'] = self::getVersion(substr($tmp, 4));
103 break;
104 case 'opera':
105 $tmp = strstr($userAgent, 'Opera');
106 $browserInfo['version'] = self::getVersion(substr($tmp, 5));
107 break;
108 case 'lynx':
109 $tmp = strstr($userAgent, 'Lynx/');
110 $browserInfo['version'] = self::getVersion(substr($tmp, 5));
111 break;
112 case 'php':
113 $tmp = strstr($userAgent, 'PHP/');
114 $browserInfo['version'] = self::getVersion(substr($tmp, 4));
115 break;
116 case 'avantgo':
117 $tmp = strstr($userAgent, 'AvantGo');
118 $browserInfo['version'] = self::getVersion(substr($tmp, 7));
119 break;
120 case 'acrobat':
121 $tmp = strstr($userAgent, 'WebCapture');
122 $browserInfo['version'] = self::getVersion(substr($tmp, 10));
123 break;
124 case 'ibrowse':
125 $tmp = strstr($userAgent, 'IBrowse/');
126 $browserInfo['version'] = self::getVersion(substr($tmp ,8));
127 break;
128 case 'konqueror':
129 $tmp = strstr($userAgent, 'Konqueror/');
130 $browserInfo['version'] = self::getVersion(substr($tmp, 10));
131 break;
132 }
133 // system
134 $browserInfo['system'] = '';
135 if (strstr($userAgent, 'Win')) {
136 // windows
137 if (strstr($userAgent, 'Win98') || strstr($userAgent, 'Windows 98')) {
138 $browserInfo['system'] = 'win98';
139 } elseif (strstr($userAgent, 'Win95') || strstr($userAgent, 'Windows 95')) {
140 $browserInfo['system'] = 'win95';
141 } elseif (strstr($userAgent, 'WinNT') || strstr($userAgent, 'Windows NT')) {
142 $browserInfo['system'] = 'winNT';
143 } elseif (strstr($userAgent, 'Win16') || strstr($userAgent, 'Windows 311')) {
144 $browserInfo['system'] = 'win311';
145 }
146 } elseif (strstr($userAgent,'Mac')) {
147 $browserInfo['system'] = 'mac';
148 // unixes
149 } elseif (strstr($userAgent, 'Linux')) {
150 $browserInfo['system'] = 'linux';
151 } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
152 $browserInfo['system'] = 'unix_sgi';
153 } elseif (strstr($userAgent, ' SunOS ')) {
154 $browserInfo['system'] = 'unix_sun';
155 } elseif (strstr($userAgent, ' HP-UX ')) {
156 $browserInfo['system'] = 'unix_hp';
157 }
158 }
159
160 return $browserInfo;
161 }
162
163 /**
164 * Returns the version of a browser; Basically getting doubleval() of the input string,
165 * stripping of any non-numeric values in the beginning of the string first.
166 *
167 * @param string $version: A string with version number, eg. "/7.32 blablabla"
168 * @return double Returns double value, eg. "7.32"
169 */
170 public static function getVersion($version) {
171 return doubleval(preg_replace('/^[^0-9]*/', '', $version));
172 }
173
174 /**
175 * Gets a code for a browsing device based on the input useragent string.
176 *
177 * @param string $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
178 * @return string Code for the specific device type
179 */
180 public static function getDeviceType($userAgent) {
181 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType']:
182 $getDeviceTypeHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType'];
183 if (is_array($getDeviceTypeHooks)) {
184 foreach ($getDeviceTypeHooks as $hookFunction) {
185 $returnResult = true;
186 $hookParameters = array(
187 'userAgent' => &$userAgent,
188 'returnResult' => &$returnResult,
189 );
190
191 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, NULL);
192 if ($returnResult && is_string($hookResult) && !empty($hookResult)) {
193 return $hookResult;
194 }
195 }
196 }
197
198 $userAgent = strtolower(trim($userAgent));
199 $deviceType = '';
200
201 // pda
202 if(strstr($userAgent, 'avantgo')) {
203 $deviceType = 'pda';
204 }
205 // wap
206 $browser=substr($userAgent, 0, 4);
207 $wapviwer=substr(stristr($userAgent,'wap'), 0, 3);
208 if($wapviwer == 'wap' ||
209 $browser == 'noki' ||
210 $browser == 'eric' ||
211 $browser == 'r380' ||
212 $browser == 'up.b' ||
213 $browser == 'winw' ||
214 $browser == 'wapa') {
215 $deviceType = 'wap';
216 }
217 // grabber
218 if(strstr($userAgent, 'g.r.a.b.') ||
219 strstr($userAgent, 'utilmind httpget') ||
220 strstr($userAgent, 'webcapture') ||
221 strstr($userAgent, 'teleport') ||
222 strstr($userAgent, 'webcopier')) {
223 $deviceType = 'grabber';
224 }
225 // robots
226 if(strstr($userAgent, 'crawler') ||
227 strstr($userAgent, 'spider') ||
228 strstr($userAgent, 'googlebot') ||
229 strstr($userAgent, 'searchbot') ||
230 strstr($userAgent, 'infoseek') ||
231 strstr($userAgent, 'altavista') ||
232 strstr($userAgent, 'diibot')) {
233 $deviceType = 'robot';
234 }
235
236 return $deviceType;
237 }
238 }
239 ?>