function useragent_decode($ua){ # # a list of user agents, in order we'll match them. # e.g. we put chrome before safari because chrome also # claims it is safari (but the reverse is not true) # $agents = array( 'chrome', 'safari', 'konqueror', 'firefox', 'netscape', 'opera', 'msie', ); $engines = array( 'webkit', 'gecko', 'trident', ); $ua = StrToLower($ua); $out = array(); $temp = useragent_match($ua, $agents); $out['agent'] = $temp['token']; $out['agent_version'] = $temp['version']; $temp = useragent_match($ua, $engines); $out['engine'] = $temp['token']; $out['engine_version'] = $temp['version']; # # safari does something super annoying, putting the version in the # wrong place like: "Version/5.0.1 Safari/533.17.8" # if ($out['agent'] == 'safari'){ $temp = useragent_match($ua, array('version')); if ($temp['token']) $out['agent_version'] = $temp['version']; } return $out; }
function useragent_decode($ua) { # # a list of user agents, in order we'll match them. # e.g. we put chrome before safari because chrome also # claims it is safari (but the reverse is not true) # $agents = array('edge', 'chrome', 'safari', 'konqueror', 'firefox', 'netscape', 'opera', 'msie', 'dalvik', 'blackberry'); $engines = array('edge', 'webkit', 'gecko', 'trident', 'presto'); $ua = StrToLower($ua); $out = array(); $temp = useragent_match($ua, $agents); $out['agent'] = $temp['token']; $out['agent_version'] = $temp['version']; $temp = useragent_match($ua, $engines); $out['engine'] = $temp['token']; $out['engine_version'] = $temp['version']; # # safari does something super annoying, putting the version in the # wrong place like: "Version/5.0.1 Safari/533.17.8" # # opera does the same thing: # http://dev.opera.com/articles/view/opera-ua-string-changes/ # if ($out['agent'] == 'safari' || $out['agent'] == 'opera') { $temp = useragent_match($ua, array('version')); if ($temp['token']) { $out['agent_version'] = $temp['version']; } } if ($out['agent'] == 'blackberry' && !$out['agent_version']) { if (preg_match('!blackberry(\\d+)/(\\S+)!', $ua, $m)) { $out['agent_version'] = $m[2]; } } # # OS matching needs to do some regex transformations # $os = array('windows nt 5.1' => array('Windows', 'XP'), 'windows nt 5.2' => array('Windows', 'XP (x64)'), 'windows nt 6.0' => array('Windows', 'Vista'), 'windows nt 6.1' => array('Windows', '7'), 'windows nt 6.2' => array('Windows', '8'), 'windows nt 6.3' => array('Windows', '8.1'), 'windows nt 10.0' => array('Windows', '10'), 'android' => array('Android', ''), 'linux i686' => array('Linux', 'i686'), 'linux x86_64' => array('Linux', 'x86_64'), '(ipad; ' => array('iPad', ''), '(ipod; ' => array('iPod', ''), '(iphone; ' => array('iPhone', ''), 'blackberry' => array('Blackberry', '')); $out['os'] = null; $out['os_version'] = null; foreach ($os as $k => $v) { if (strpos($ua, $k) !== false) { $out['os'] = $v[0]; $out['os_version'] = $v[1]; break; } } if (in_array($out['os'], array('iphone', 'ipad', 'ipod'))) { if (preg_match('!os (\\d+)[._](\\d+)([._](\\d+))? like mac os x!', $ua, $m)) { $out['os_version'] = "{$m['1']}.{$m['2']}"; if (!empty($m[4])) { $out['os_version'] .= ".{$m['4']}"; } } } if ($out['os'] == 'android') { if (preg_match('!android (\\d+)\\.(\\d+)(\\.(\\d+))?!', $ua, $m)) { $out['os_version'] = "{$m['1']}.{$m['2']}"; if (!empty($m[4])) { $out['os_version'] .= ".{$m['4']}"; } } } if ($out['os'] == 'blackberry') { if (preg_match('!blackberry ?(\\d+)!', $ua, $m)) { $out['os_version'] = $m[1]; } } if (is_null($out['os'])) { if (preg_match('!mac os x (\\d+)[._](\\d+)([._](\\d+))?!', $ua, $m)) { $out['os'] = 'osx'; $out['os_version'] = "{$m['1']}.{$m['2']}"; if ($m[4]) { $out['os_version'] .= ".{$m['4']}"; } } } return $out; }