UserAgentParser.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * Parses a user agent string into its important parts
  4. *
  5. * @author Jesse G. Donat <donatj@gmail.com>
  6. * @link https://github.com/donatj/PhpUserAgent
  7. * @link http://donatstudios.com/PHP-Parser-HTTP_USER_AGENT
  8. * @param string|null $u_agent User agent string to parse or null. Uses $_SERVER['HTTP_USER_AGENT'] on NULL
  9. * @throws \InvalidArgumentException on not having a proper user agent to parse.
  10. * @return string[] an array with browser, version and platform keys
  11. */
  12. function parse_user_agent( $u_agent = null ) {
  13. if( is_null($u_agent) ) {
  14. if( isset($_SERVER['HTTP_USER_AGENT']) ) {
  15. $u_agent = $_SERVER['HTTP_USER_AGENT'];
  16. } else {
  17. throw new \InvalidArgumentException('parse_user_agent requires a user agent');
  18. }
  19. }
  20. $platform = null;
  21. $browser = null;
  22. $version = null;
  23. $empty = array( 'platform' => $platform, 'browser' => $browser, 'version' => $version );
  24. if( !$u_agent ) return $empty;
  25. if( preg_match('/\((.*?)\)/im', $u_agent, $parent_matches) ) {
  26. preg_match_all('/(?P<platform>BB\d+;|Android|CrOS|Tizen|iPhone|iPad|iPod|Linux|(Open|Net|Free)BSD|Macintosh|Windows(\ Phone)?|Silk|linux-gnu|BlackBerry|PlayBook|X11|(New\ )?Nintendo\ (WiiU?|3?DS|Switch)|Xbox(\ One)?)
  27. (?:\ [^;]*)?
  28. (?:;|$)/imx', $parent_matches[1], $result, PREG_PATTERN_ORDER);
  29. $priority = array( 'Xbox One', 'Xbox', 'Windows Phone', 'Tizen', 'Android', 'FreeBSD', 'NetBSD', 'OpenBSD', 'CrOS', 'X11' );
  30. $result['platform'] = array_unique($result['platform']);
  31. if( count($result['platform']) > 1 ) {
  32. if( $keys = array_intersect($priority, $result['platform']) ) {
  33. $platform = reset($keys);
  34. } else {
  35. $platform = $result['platform'][0];
  36. }
  37. } elseif( isset($result['platform'][0]) ) {
  38. $platform = $result['platform'][0];
  39. }
  40. }
  41. if( $platform == 'linux-gnu' || $platform == 'X11' ) {
  42. $platform = 'Linux';
  43. } elseif( $platform == 'CrOS' ) {
  44. $platform = 'Chrome OS';
  45. }
  46. preg_match_all('%(?P<browser>Camino|Kindle(\ Fire)?|Firefox|Iceweasel|IceCat|Safari|MSIE|Trident|AppleWebKit|
  47. TizenBrowser|(?:Headless)?Chrome|Vivaldi|IEMobile|Opera|OPR|Silk|Midori|Edge|CriOS|UCBrowser|Puffin|SamsungBrowser|
  48. Baiduspider|Googlebot|YandexBot|bingbot|Lynx|Version|Wget|curl|
  49. Valve\ Steam\ Tenfoot|
  50. NintendoBrowser|PLAYSTATION\ (\d|Vita)+)
  51. (?:\)?;?)
  52. (?:(?:[:/ ])(?P<version>[0-9A-Z.]+)|/(?:[A-Z]*))%ix',
  53. $u_agent, $result, PREG_PATTERN_ORDER);
  54. // If nothing matched, return null (to avoid undefined index errors)
  55. if( !isset($result['browser'][0]) || !isset($result['version'][0]) ) {
  56. if( preg_match('%^(?!Mozilla)(?P<browser>[A-Z0-9\-]+)(/(?P<version>[0-9A-Z.]+))?%ix', $u_agent, $result) ) {
  57. return array( 'platform' => $platform ?: null, 'browser' => $result['browser'], 'version' => isset($result['version']) ? $result['version'] ?: null : null );
  58. }
  59. return $empty;
  60. }
  61. if( preg_match('/rv:(?P<version>[0-9A-Z.]+)/si', $u_agent, $rv_result) ) {
  62. $rv_result = $rv_result['version'];
  63. }
  64. $browser = $result['browser'][0];
  65. $version = $result['version'][0];
  66. $lowerBrowser = array_map('strtolower', $result['browser']);
  67. $find = function ( $search, &$key, &$value = null ) use ( $lowerBrowser ) {
  68. $search = (array)$search;
  69. foreach( $search as $val ) {
  70. $xkey = array_search(strtolower($val), $lowerBrowser);
  71. if( $xkey !== false ) {
  72. $value = $val;
  73. $key = $xkey;
  74. return true;
  75. }
  76. }
  77. return false;
  78. };
  79. $key = 0;
  80. $val = '';
  81. if( $browser == 'Iceweasel' || strtolower($browser) == 'icecat' ) {
  82. $browser = 'Firefox';
  83. } elseif( $find('Playstation Vita', $key) ) {
  84. $platform = 'PlayStation Vita';
  85. $browser = 'Browser';
  86. } elseif( $find(array( 'Kindle Fire', 'Silk' ), $key, $val) ) {
  87. $browser = $val == 'Silk' ? 'Silk' : 'Kindle';
  88. $platform = 'Kindle Fire';
  89. if( !($version = $result['version'][$key]) || !is_numeric($version[0]) ) {
  90. $version = $result['version'][array_search('Version', $result['browser'])];
  91. }
  92. } elseif( $find('NintendoBrowser', $key) || $platform == 'Nintendo 3DS' ) {
  93. $browser = 'NintendoBrowser';
  94. $version = $result['version'][$key];
  95. } elseif( $find('Kindle', $key, $platform) ) {
  96. $browser = $result['browser'][$key];
  97. $version = $result['version'][$key];
  98. } elseif( $find('OPR', $key) ) {
  99. $browser = 'Opera Next';
  100. $version = $result['version'][$key];
  101. } elseif( $find('Opera', $key, $browser) ) {
  102. $find('Version', $key);
  103. $version = $result['version'][$key];
  104. } elseif( $find('Puffin', $key, $browser) ) {
  105. $version = $result['version'][$key];
  106. if( strlen($version) > 3 ) {
  107. $part = substr($version, -2);
  108. if( ctype_upper($part) ) {
  109. $version = substr($version, 0, -2);
  110. $flags = array( 'IP' => 'iPhone', 'IT' => 'iPad', 'AP' => 'Android', 'AT' => 'Android', 'WP' => 'Windows Phone', 'WT' => 'Windows' );
  111. if( isset($flags[$part]) ) {
  112. $platform = $flags[$part];
  113. }
  114. }
  115. }
  116. } elseif( $find(array( 'IEMobile', 'Edge', 'Midori', 'Vivaldi', 'SamsungBrowser', 'Valve Steam Tenfoot', 'Chrome', 'HeadlessChrome' ), $key, $browser) ) {
  117. $version = $result['version'][$key];
  118. } elseif( $rv_result && $find('Trident', $key) ) {
  119. $browser = 'MSIE';
  120. $version = $rv_result;
  121. } elseif( $find('UCBrowser', $key) ) {
  122. $browser = 'UC Browser';
  123. $version = $result['version'][$key];
  124. } elseif( $find('CriOS', $key) ) {
  125. $browser = 'Chrome';
  126. $version = $result['version'][$key];
  127. } elseif( $browser == 'AppleWebKit' ) {
  128. if( $platform == 'Android' ) {
  129. // $key = 0;
  130. $browser = 'Android Browser';
  131. } elseif( strpos($platform, 'BB') === 0 ) {
  132. $browser = 'BlackBerry Browser';
  133. $platform = 'BlackBerry';
  134. } elseif( $platform == 'BlackBerry' || $platform == 'PlayBook' ) {
  135. $browser = 'BlackBerry Browser';
  136. } else {
  137. $find('Safari', $key, $browser) || $find('TizenBrowser', $key, $browser);
  138. }
  139. $find('Version', $key);
  140. $version = $result['version'][$key];
  141. } elseif( $pKey = preg_grep('/playstation \d/i', array_map('strtolower', $result['browser'])) ) {
  142. $pKey = reset($pKey);
  143. $platform = 'PlayStation ' . preg_replace('/[^\d]/i', '', $pKey);
  144. $browser = 'NetFront';
  145. }
  146. return array( 'platform' => $platform ?: null, 'browser' => $browser ?: null, 'version' => $version ?: null );
  147. }