PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/public/typo3/sysext/core/Classes/Utility/ClientUtility.php

https://bitbucket.org/followupcio/website
PHP | 233 lines | 170 code | 4 blank | 59 comment | 49 complexity | 8043112548515e4787a4bc5eeafe71c6 MD5 | raw file
Possible License(s): GPL-2.0, MPL-2.0-no-copyleft-exception, BSD-2-Clause
  1. <?php
  2. namespace TYPO3\CMS\Core\Utility;
  3. /*
  4. * This file is part of the TYPO3 CMS project.
  5. *
  6. * It is free software; you can redistribute it and/or modify it under
  7. * the terms of the GNU General Public License, either version 2
  8. * of the License, or any later version.
  9. *
  10. * For the full copyright and license information, please read the
  11. * LICENSE.txt file that was distributed with this source code.
  12. *
  13. * The TYPO3 project - inspiring people to share!
  14. */
  15. /**
  16. * Class to handle and determine browser specific information.
  17. */
  18. class ClientUtility
  19. {
  20. /**
  21. * Generates an array with abstracted browser information
  22. *
  23. * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
  24. * @return array Contains keys "browser", "version", "system
  25. */
  26. public static function getBrowserInfo($userAgent)
  27. {
  28. // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo']:
  29. $getBrowserInfoHooks = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getBrowserInfo'];
  30. if (is_array($getBrowserInfoHooks)) {
  31. foreach ($getBrowserInfoHooks as $hookFunction) {
  32. $returnResult = true;
  33. $hookParameters = [
  34. 'userAgent' => &$userAgent,
  35. 'returnResult' => &$returnResult
  36. ];
  37. // need reference for third parameter in \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction,
  38. // so create a reference to NULL
  39. $null = null;
  40. $hookResult = GeneralUtility::callUserFunction($hookFunction, $hookParameters, $null);
  41. if ($returnResult && is_array($hookResult) && !empty($hookResult)) {
  42. return $hookResult;
  43. }
  44. }
  45. }
  46. $userAgent = trim($userAgent);
  47. $browserInfo = [
  48. 'useragent' => $userAgent
  49. ];
  50. // Analyze the userAgent string
  51. // Declare known browsers to look for
  52. $known = [
  53. 'msie',
  54. 'firefox',
  55. 'webkit',
  56. 'opera',
  57. 'netscape',
  58. 'konqueror',
  59. 'gecko',
  60. 'chrome',
  61. 'safari',
  62. 'seamonkey',
  63. 'navigator',
  64. 'mosaic',
  65. 'lynx',
  66. 'amaya',
  67. 'omniweb',
  68. 'avant',
  69. 'camino',
  70. 'flock',
  71. 'aol'
  72. ];
  73. $matches = [];
  74. $pattern = '#(?P<browser>' . implode('|', $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. // Microsoft Internet-Explorer 11 does not have a sign of "MSIE" or so in the useragent.
  78. // All checks from the pattern above fail here. Look for a special combination of
  79. // "Mozilla/5.0" in front, "Trident/7.0" in the middle and "like Gecko" at the end.
  80. // The version (revision) is given as "; rv:11.0" in the useragent then.
  81. unset($matches);
  82. $pattern = '#mozilla/5\\.0 \\(.*trident/7\\.0.*; rv:(?P<version>[0-9]+(?:\\.[0-9]+)?)\\) like gecko#i';
  83. if (preg_match_all($pattern, $userAgent, $matches)) {
  84. $browserInfo['browser'] = 'msie';
  85. $browserInfo['version'] = $matches['version'][0];
  86. $browserInfo['all'] = ['msie' => $matches['version'][0]];
  87. } else {
  88. $browserInfo['browser'] = 'unknown';
  89. $browserInfo['version'] = '';
  90. $browserInfo['all'] = [];
  91. }
  92. } else {
  93. // Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
  94. // Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
  95. // in the UA). That's usually the most correct.
  96. // For IE use the first match as IE sends multiple MSIE with version, from higher to lower.
  97. $lastIndex = count($matches['browser']) - 1;
  98. $browserInfo['browser'] = $matches['browser'][$lastIndex];
  99. $browserInfo['version'] = $browserInfo['browser'] === 'msie' ? $matches['version'][0] : $matches['version'][$lastIndex];
  100. // But return all parsed browsers / version in an extra array
  101. $browserInfo['all'] = [];
  102. for ($i = 0; $i <= $lastIndex; $i++) {
  103. if (!isset($browserInfo['all'][$matches['browser'][$i]])) {
  104. $browserInfo['all'][$matches['browser'][$i]] = $matches['version'][$i];
  105. }
  106. }
  107. // Replace gecko build date with version given by rv
  108. if (isset($browserInfo['all']['gecko'])) {
  109. preg_match_all('/rv:([0-9\\.]*)/', strtolower($userAgent), $version);
  110. if ($version[1][0]) {
  111. $browserInfo['all']['gecko'] = $version[1][0];
  112. }
  113. }
  114. }
  115. $browserInfo['all_systems'] = [];
  116. if (strstr($userAgent, 'Win')) {
  117. // Windows
  118. if (strstr($userAgent, 'Windows NT 6.2') || strstr($userAgent, 'Windows NT 6.3')) {
  119. $browserInfo['all_systems'][] = 'win8';
  120. $browserInfo['all_systems'][] = 'winNT';
  121. } elseif (strstr($userAgent, 'Windows NT 6.1')) {
  122. $browserInfo['all_systems'][] = 'win7';
  123. $browserInfo['all_systems'][] = 'winNT';
  124. } elseif (strstr($userAgent, 'Windows NT 6.0')) {
  125. $browserInfo['all_systems'][] = 'winVista';
  126. $browserInfo['all_systems'][] = 'winNT';
  127. } elseif (strstr($userAgent, 'Windows NT 5.1')) {
  128. $browserInfo['all_systems'][] = 'winXP';
  129. $browserInfo['all_systems'][] = 'winNT';
  130. } elseif (strstr($userAgent, 'Windows NT 5.0')) {
  131. $browserInfo['all_systems'][] = 'win2k';
  132. $browserInfo['all_systems'][] = 'winNT';
  133. } elseif (strstr($userAgent, 'Win98') || strstr($userAgent, 'Windows 98')) {
  134. $browserInfo['all_systems'][] = 'win98';
  135. } elseif (strstr($userAgent, 'Win95') || strstr($userAgent, 'Windows 95')) {
  136. $browserInfo['all_systems'][] = 'win95';
  137. } elseif (strstr($userAgent, 'WinNT') || strstr($userAgent, 'Windows NT')) {
  138. $browserInfo['all_systems'][] = 'winNT';
  139. } elseif (strstr($userAgent, 'Win16') || strstr($userAgent, 'Windows 311')) {
  140. $browserInfo['all_systems'][] = 'win311';
  141. }
  142. } elseif (strstr($userAgent, 'Mac')) {
  143. if (strstr($userAgent, 'iPad') || strstr($userAgent, 'iPhone') || strstr($userAgent, 'iPod')) {
  144. $browserInfo['all_systems'][] = 'iOS';
  145. $browserInfo['all_systems'][] = 'mac';
  146. } else {
  147. $browserInfo['all_systems'][] = 'mac';
  148. }
  149. } elseif (strstr($userAgent, 'Android')) {
  150. $browserInfo['all_systems'][] = 'android';
  151. $browserInfo['all_systems'][] = 'linux';
  152. } elseif (strstr($userAgent, 'Linux')) {
  153. $browserInfo['all_systems'][] = 'linux';
  154. } elseif (strstr($userAgent, 'BSD')) {
  155. $browserInfo['all_systems'][] = 'unix_bsd';
  156. } elseif (strstr($userAgent, 'SGI') && strstr($userAgent, ' IRIX ')) {
  157. $browserInfo['all_systems'][] = 'unix_sgi';
  158. } elseif (strstr($userAgent, ' SunOS ')) {
  159. $browserInfo['all_systems'][] = 'unix_sun';
  160. } elseif (strstr($userAgent, ' HP-UX ')) {
  161. $browserInfo['all_systems'][] = 'unix_hp';
  162. } elseif (strstr($userAgent, 'CrOS')) {
  163. $browserInfo['all_systems'][] = 'chrome';
  164. $browserInfo['all_systems'][] = 'linux';
  165. }
  166. return $browserInfo;
  167. }
  168. /**
  169. * Returns the version of a browser; Basically getting float value of the input string,
  170. * stripping of any non-numeric values in the beginning of the string first.
  171. *
  172. * @param string $version A string with version number, eg. "/7.32 blablabla
  173. * @return float Returns double value, eg. "7.32
  174. */
  175. public static function getVersion($version)
  176. {
  177. return (float)preg_replace('/^[^0-9]*/', '', $version);
  178. }
  179. /**
  180. * Gets a code for a browsing device based on the input useragent string.
  181. *
  182. * @param string $userAgent The useragent string, \TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_USER_AGENT')
  183. * @return string Code for the specific device type
  184. * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
  185. */
  186. public static function getDeviceType($userAgent)
  187. {
  188. GeneralUtility::logDeprecatedFunction();
  189. // Hook: $TYPO3_CONF_VARS['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType']:
  190. $getDeviceTypeHooks = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/div/class.t3lib_utility_client.php']['getDeviceType'];
  191. if (is_array($getDeviceTypeHooks)) {
  192. foreach ($getDeviceTypeHooks as $hookFunction) {
  193. $returnResult = true;
  194. $hookParameters = [
  195. 'userAgent' => &$userAgent,
  196. 'returnResult' => &$returnResult
  197. ];
  198. // need reference for third parameter in \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction,
  199. // so create a reference to NULL
  200. $null = null;
  201. $hookResult = \TYPO3\CMS\Core\Utility\GeneralUtility::callUserFunction($hookFunction, $hookParameters, $null);
  202. if ($returnResult && is_string($hookResult) && !empty($hookResult)) {
  203. return $hookResult;
  204. }
  205. }
  206. }
  207. $userAgent = strtolower(trim($userAgent));
  208. $deviceType = '';
  209. // pda
  210. if (strstr($userAgent, 'avantgo')) {
  211. $deviceType = 'pda';
  212. }
  213. // wap
  214. $browser = substr($userAgent, 0, 4);
  215. $wapviwer = substr(stristr($userAgent, 'wap'), 0, 3);
  216. if ($wapviwer === 'wap' || $browser === 'noki' || $browser === 'eric' || $browser == 'r380' || $browser === 'up.b' || $browser === 'winw' || $browser === 'wapa') {
  217. $deviceType = 'wap';
  218. }
  219. // grabber
  220. if (strstr($userAgent, 'g.r.a.b.') || strstr($userAgent, 'utilmind httpget') || strstr($userAgent, 'webcapture') || strstr($userAgent, 'teleport') || strstr($userAgent, 'webcopier')) {
  221. $deviceType = 'grabber';
  222. }
  223. // robots
  224. if (strstr($userAgent, 'crawler') || strstr($userAgent, 'spider') || strstr($userAgent, 'googlebot') || strstr($userAgent, 'searchbot') || strstr($userAgent, 'infoseek') || strstr($userAgent, 'altavista') || strstr($userAgent, 'diibot')) {
  225. $deviceType = 'robot';
  226. }
  227. return $deviceType;
  228. }
  229. }