TYPO3 API  SVNRelease
class.t3lib_utility_client.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003  * Copyright notice
00004  *
00005  * (c) 2009-2011 Oliver Hader <oliver@typo3.org>
00006  * All rights reserved
00007  *
00008  * This script is part of the TYPO3 project. The TYPO3 project is
00009  * free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version.
00013  *
00014  * The GNU General Public License can be found at
00015  * http://www.gnu.org/copyleft/gpl.html.
00016  * A copy is found in the textfile GPL.txt and important notices to the license
00017  * from the author is found in LICENSE.txt distributed with these scripts.
00018  *
00019  *
00020  * This script is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * This copyright notice MUST APPEAR in all copies of the script!
00026  ***************************************************************/
00027 
00028 /**
00029  * Class to handle and determine browser specific information.
00030  *
00031  * $Id: class.t3lib_utility_client.php 10549 2011-02-22 21:57:13Z steffenk $
00032  *
00033  * @author  Oliver Hader <oliver@typo3.org>
00034  */
00035 final class t3lib_utility_Client {
00036 
00037     /**
00038      * Generates an array with abstracted browser information
00039      *
00040      * @param   string      $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
00041      * @return  array       Contains keys "browser", "version", "system"
00042      */
00043     public static function getBrowserInfo($userAgent) {
00044             // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo']:
00045         $getBrowserInfoHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo'];
00046         if (is_array($getBrowserInfoHooks)) {
00047             foreach ($getBrowserInfoHooks as $hookFunction) {
00048                 $returnResult = true;
00049                 $hookParameters = array(
00050                     'userAgent' => &$userAgent,
00051                     'returnResult' => &$returnResult,
00052                 );
00053 
00054                     // need reference for third parameter in t3lib_div::callUserFunction,
00055                     // so create a reference to NULL
00056                 $null = NULL;
00057                 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, $null);
00058                 if ($returnResult && is_array($hookResult) && count($hookResult)) {
00059                     return $hookResult;
00060                 }
00061             }
00062         }
00063 
00064         $userAgent = trim($userAgent);
00065         $browserInfo = array(
00066             'useragent' => $userAgent,
00067         );
00068 
00069             // Analyze the userAgent string
00070             // Declare known browsers to look for
00071 
00072         $known = array('msie', 'firefox', 'webkit', 'opera', 'netscape', 'konqueror',
00073                        'gecko', 'chrome', 'safari', 'seamonkey', 'navigator', 'mosaic',
00074                        'lynx', 'amaya', 'omniweb', 'avant', 'camino', 'flock', 'aol');
00075         $matches = array();
00076 
00077         $pattern = '#(?P<browser>' . join('|', $known) . ')[/ ]+(?P<version>[0-9]+(?:\.[0-9]+)?)#';
00078             // Find all phrases (or return empty array if none found)
00079         if (!preg_match_all($pattern, strtolower($userAgent), $matches)) {
00080             $browserInfo['browser'] = 'unknown';
00081             $browserInfo['version'] = '';
00082             $browserInfo['all'] = array();
00083         } else {
00084                 // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
00085                 // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
00086                 // in the UA).  That's usually the most correct.
00087                 // For IE use the first match as IE sends multiple MSIE with version, from higher to lower.
00088             $lastIndex = count($matches['browser']) - 1;
00089             $browserInfo['browser'] = $matches['browser'][$lastIndex];
00090             $browserInfo['version'] = $browserInfo['browser'] === 'msie' ? $matches['version'][0] : $matches['version'][$lastIndex];
00091                 //But return all parsed browsers / version in an extra array
00092             for ($i = 0; $i <= $lastIndex; $i++) {
00093                 if (!isset($browserInfo['all'][$matches['browser'][$i]])) {
00094                     $browserInfo['all'][$matches['browser'][$i]] = $matches['version'][$i];
00095                 }
00096             }
00097                 //Replace gecko build date with version given by rv
00098             if (isset($browserInfo['all']['gecko'])) {
00099                 preg_match_all('/rv:([0-9\.]*)/', strtolower($userAgent), $version);
00100                 if ($version[1][0]) {
00101                     $browserInfo['all']['gecko'] = $version[1][0];
00102                 }
00103             }
00104         }
00105 
00106             // Microsoft Documentation about Platform tokens: http://msdn.microsoft.com/en-us/library/ms537503(VS.85).aspx
00107             // 'system' is deprecated, use 'all_systems' (array) in future!
00108         $browserInfo['system'] = '';
00109         $browserInfo['all_systems'] = array();
00110         if (strstr($userAgent, 'Win')) {
00111                 // windows
00112             if (strstr($userAgent, 'Windows NT 6.1')) {
00113                 $browserInfo['system'] = 'winNT'; // backwards compatible
00114                 $browserInfo['all_systems'][] = 'win7';
00115                 $browserInfo['all_systems'][] = 'winNT';
00116             } elseif (strstr($userAgent, 'Windows NT 6.0')) {
00117                 $browserInfo['system'] = 'winNT'; // backwards compatible
00118                 $browserInfo['all_systems'][] = 'winVista';
00119                 $browserInfo['all_systems'][] = 'winNT';
00120             } elseif (strstr($userAgent, 'Windows NT 5.1')) {
00121                 $browserInfo['system'] = 'winNT'; // backwards compatible
00122                 $browserInfo['all_systems'][] = 'winXP';
00123                 $browserInfo['all_systems'][] = 'winNT';
00124             } elseif (strstr($userAgent, 'Windows NT 5.0')) {
00125                 $browserInfo['system'] = 'winNT'; // backwards compatible
00126                 $browserInfo['all_systems'][] = 'win2k';
00127                 $browserInfo['all_systems'][] = 'winNT';
00128             } elseif (strstr($userAgent, 'Win98') || strstr($userAgent, 'Windows 98')) {
00129                 $browserInfo['system'] = 'win98';
00130                 $browserInfo['all_systems'][] = 'win98';
00131             } elseif (strstr($userAgent, 'Win95') || strstr($userAgent, 'Windows 95')) {
00132                 $browserInfo['system'] = 'win95';
00133                 $browserInfo['all_systems'][] = 'win95';
00134             } elseif (strstr($userAgent, 'WinNT') || strstr($userAgent, 'Windows NT')) {
00135                 $browserInfo['system'] = 'winNT';
00136                 $browserInfo['all_systems'][] = 'winNT';
00137             } elseif (strstr($userAgent, 'Win16') || strstr($userAgent, 'Windows 311')) {
00138                 $browserInfo['system'] = 'win311';
00139                 $browserInfo['all_systems'][] = 'win311';
00140             }
00141         } elseif (strstr($userAgent, 'Mac')) {
00142             if (strstr($userAgent, 'iPad') || strstr($userAgent, 'iPhone') || strstr($userAgent, 'iPod')) {
00143                 $browserInfo['system'] = 'mac'; // backwards compatible
00144                 $browserInfo['all_systems'][] = 'iOS';
00145                 $browserInfo['all_systems'][] = 'mac';
00146             } else {
00147                 $browserInfo['system'] = 'mac';
00148                 $browserInfo['all_systems'][] = 'mac';
00149             }
00150                 // unixes
00151         } elseif (strstr($userAgent, 'Linux')) {
00152             if (strstr($userAgent, 'Android')) {
00153                 $browserInfo['system'] = 'linux'; // backwards compatible
00154                 $browserInfo['all_systems'][] = 'android';
00155                 $browserInfo['all_systems'][] = 'linux';
00156             } else {
00157                 $browserInfo['system'] = 'linux';
00158                 $browserInfo['all_systems'][] = 'linux';
00159             }
00160         } elseif (strstr($userAgent, 'BSD')) {
00161             $browserInfo['system'] = 'unix_bsd';
00162             $browserInfo['all_systems'][] = 'unix_bsd';
00163         } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
00164             $browserInfo['system'] = 'unix_sgi';
00165             $browserInfo['all_systems'][] = 'unix_sgi';
00166         } elseif (strstr($userAgent, ' SunOS ')) {
00167             $browserInfo['system'] = 'unix_sun';
00168             $browserInfo['all_systems'][] = 'unix_sun';
00169         } elseif (strstr($userAgent, ' HP-UX ')) {
00170             $browserInfo['system'] = 'unix_hp';
00171             $browserInfo['all_systems'][] = 'unix_hp';
00172         } elseif (strstr($userAgent, 'CrOS')) {
00173             $browserInfo['system'] = 'linux';
00174             $browserInfo['all_systems'][] = 'chrome';
00175             $browserInfo['all_systems'][] = 'linux';
00176         }
00177 
00178         return $browserInfo;
00179     }
00180 
00181     /**
00182      * Returns the version of a browser; Basically getting doubleval() of the input string,
00183      * stripping of any non-numeric values in the beginning of the string first.
00184      *
00185      * @param   string      $version: A string with version number, eg. "/7.32 blablabla"
00186      * @return  double      Returns double value, eg. "7.32"
00187      */
00188     public static function getVersion($version) {
00189         return doubleval(preg_replace('/^[^0-9]*/', '', $version));
00190     }
00191 
00192     /**
00193      * Gets a code for a browsing device based on the input useragent string.
00194      *
00195      * @param   string      $userAgent: The useragent string, t3lib_div::getIndpEnv('HTTP_USER_AGENT')
00196      * @return  string      Code for the specific device type
00197      */
00198     public static function getDeviceType($userAgent) {
00199             // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType']:
00200         $getDeviceTypeHooks =& $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType'];
00201         if (is_array($getDeviceTypeHooks)) {
00202             foreach ($getDeviceTypeHooks as $hookFunction) {
00203                 $returnResult = TRUE;
00204                 $hookParameters = array(
00205                     'userAgent' => &$userAgent,
00206                     'returnResult' => &$returnResult,
00207                 );
00208 
00209                     // need reference for third parameter in t3lib_div::callUserFunction,
00210                     // so create a reference to NULL
00211                 $null = NULL;
00212                 $hookResult = t3lib_div::callUserFunction($hookFunction, $hookParameters, $null);
00213                 if ($returnResult && is_string($hookResult) && !empty($hookResult)) {
00214                     return $hookResult;
00215                 }
00216             }
00217         }
00218 
00219         $userAgent = strtolower(trim($userAgent));
00220         $deviceType = '';
00221 
00222             // pda
00223         if (strstr($userAgent, 'avantgo')) {
00224             $deviceType = 'pda';
00225         }
00226             // wap
00227         $browser = substr($userAgent, 0, 4);
00228         $wapviwer = substr(stristr($userAgent, 'wap'), 0, 3);
00229         if ($wapviwer == 'wap' ||
00230             $browser == 'noki' ||
00231             $browser == 'eric' ||
00232             $browser == 'r380' ||
00233             $browser == 'up.b' ||
00234             $browser == 'winw' ||
00235             $browser == 'wapa') {
00236             $deviceType = 'wap';
00237         }
00238             // grabber
00239         if (strstr($userAgent, 'g.r.a.b.') ||
00240             strstr($userAgent, 'utilmind httpget') ||
00241             strstr($userAgent, 'webcapture') ||
00242             strstr($userAgent, 'teleport') ||
00243             strstr($userAgent, 'webcopier')) {
00244             $deviceType = 'grabber';
00245         }
00246             // robots
00247         if (strstr($userAgent, 'crawler') ||
00248             strstr($userAgent, 'spider') ||
00249             strstr($userAgent, 'googlebot') ||
00250             strstr($userAgent, 'searchbot') ||
00251             strstr($userAgent, 'infoseek') ||
00252             strstr($userAgent, 'altavista') ||
00253             strstr($userAgent, 'diibot')) {
00254             $deviceType = 'robot';
00255         }
00256 
00257         return $deviceType;
00258     }
00259 }
00260 
00261 ?>