Cleanup: Set SVN end of line property
[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 $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 = '#(?P<browser>' . join('|', $known) . ')[/ ]+(?P<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 //Replace gecko build date with version given by rv
93 if (isset($browserInfo['all']['gecko'])) {
94 preg_match_all('/rv:([0-9\.]*)/', strtolower($userAgent), $version);
95 if ($version[1][0]) {
96 $browserInfo['all']['gecko'] = $version[1][0];
97 }
98 }
99 }
100
101 // system
102 $browserInfo['system'] = '';
103 if (strstr($userAgent, 'Win')) {
104 // windows
105 if (strstr($userAgent, 'Win98') || strstr($userAgent, 'Windows 98')) {
106 $browserInfo['system'] = 'win98';
107 } elseif (strstr($userAgent, 'Win95') || strstr($userAgent, 'Windows 95')) {
108 $browserInfo['system'] = 'win95';
109 } elseif (strstr($userAgent, 'WinNT') || strstr($userAgent, 'Windows NT')) {
110 $browserInfo['system'] = 'winNT';
111 } elseif (strstr($userAgent, 'Win16') || strstr($userAgent, 'Windows 311')) {
112 $browserInfo['system'] = 'win311';
113 }
114 } elseif (strstr($userAgent,'Mac')) {
115 $browserInfo['system'] = 'mac';
116 // unixes
117 } elseif (strstr($userAgent, 'Linux')) {
118 $browserInfo['system'] = 'linux';
119 } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
120 $browserInfo['system'] = 'unix_sgi';
121 } elseif (strstr($userAgent, ' SunOS ')) {
122 $browserInfo['system'] = 'unix_sun';
123 } elseif (strstr($userAgent, ' HP-UX ')) {
124 $browserInfo['system'] = 'unix_hp';
125 }
126
127 return $browserInfo;
128 }
129
130 /**
131 * Returns the version of a browser; Basically getting doubleval() of the input string,
132 * stripping of any non-numeric values in the beginning of the string first.
133 *
134 * @param string $version: A string with version number, eg. "/7.32 blablabla"
135 * @return double Returns double value, eg. "7.32"
136 */
137 public static function getVersion($version) {
138 return doubleval(preg_replace('/^[^0-9]*/', '', $version));
139 }
140
141 /**
142 * Gets a code for a browsing device based on the input useragent string.
143 *
144 * @param string $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
145 * @return string Code for the specific device type
146 */
147 public static function getDeviceType($userAgent) {
148 // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType']:
149 $getDeviceTypeHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType'];
150 if (is_array($getDeviceTypeHooks)) {
151 foreach ($getDeviceTypeHooks as $hookFunction) {
152 $returnResult = true;
153 $hookParameters = array(
154 'userAgent' => &$userAgent,
155 'returnResult' => &$returnResult,
156 );
157
158 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, NULL);
159 if ($returnResult && is_string($hookResult) && !empty($hookResult)) {
160 return $hookResult;
161 }
162 }
163 }
164
165 $userAgent = strtolower(trim($userAgent));
166 $deviceType = '';
167
168 // pda
169 if(strstr($userAgent, 'avantgo')) {
170 $deviceType = 'pda';
171 }
172 // wap
173 $browser=substr($userAgent, 0, 4);
174 $wapviwer=substr(stristr($userAgent,'wap'), 0, 3);
175 if($wapviwer == 'wap' ||
176 $browser == 'noki' ||
177 $browser == 'eric' ||
178 $browser == 'r380' ||
179 $browser == 'up.b' ||
180 $browser == 'winw' ||
181 $browser == 'wapa') {
182 $deviceType = 'wap';
183 }
184 // grabber
185 if(strstr($userAgent, 'g.r.a.b.') ||
186 strstr($userAgent, 'utilmind httpget') ||
187 strstr($userAgent, 'webcapture') ||
188 strstr($userAgent, 'teleport') ||
189 strstr($userAgent, 'webcopier')) {
190 $deviceType = 'grabber';
191 }
192 // robots
193 if(strstr($userAgent, 'crawler') ||
194 strstr($userAgent, 'spider') ||
195 strstr($userAgent, 'googlebot') ||
196 strstr($userAgent, 'searchbot') ||
197 strstr($userAgent, 'infoseek') ||
198 strstr($userAgent, 'altavista') ||
199 strstr($userAgent, 'diibot')) {
200 $deviceType = 'robot';
201 }
202
203 return $deviceType;
204 }
205 }
206 ?>