Skip to content

Instantly share code, notes, and snippets.

@kmod-midori
Last active December 15, 2015 10:28
Show Gist options
  • Save kmod-midori/5245585 to your computer and use it in GitHub Desktop.
Save kmod-midori/5245585 to your computer and use it in GitHub Desktop.
<?php
function detect() {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
// Identify the browser. Check Opera and Safari first in case of spoof. Let Google Chrome be identified as Safari.
if (preg_match('/opera/', $userAgent)) {
$name = 'opera';
}
elseif (preg_match('/webkit/', $userAgent)) {
$name = 'safari';
}
elseif (preg_match('/msie/', $userAgent)) {
$name = 'msie';
}
elseif (preg_match('/mozilla/', $userAgent) && !preg_match('/compatible/', $userAgent)) {
$name = 'mozilla';
}
else {
$name = 'unrecognized';
}
// What version?
if (preg_match('/.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/', $userAgent, $matches)) {
$version = $matches[1];
}
else {
$version = 'unknown';
}
// Running on what platform?
if (preg_match('/linux/', $userAgent)) {
$platform = 'linux';
}
elseif (preg_match('/macintosh|mac os x/', $userAgent)) {
$platform = 'mac';
}
elseif (preg_match('/windows|win32/', $userAgent)) {
$platform = 'windows';
}
else {
$platform = 'unrecognized';
}
return array(
'name' => $name,
'version' => $version,
'platform' => $platform,
'userAgent' => $userAgent
);
}
$agent=detect();
$platform = $agent['platform'];
$files = scandir(dirname(__FILE__).'/downloads');
$files = array_filter($files,function($var){
if($var != '.'&&$var != '..')return true;
return false;
});
switch($platform){
case 'linux':
$is_64 = !!preg_match('/amd64|x86_64/i',$_SERVER['HTTP_USER_AGENT']);
$files = array_filter($files,function($var) use ($is_64){
if($is_64){
if(preg_match('/^mycard-.+?-x86_64-linux\..*$/',$var))return true;
return false;
}
if(preg_match('/^mycard-.+?-x86-linux\..*$/',$var))return true;
return false;
});
break;
default:
$files = array_filter($files,function($var){
if(preg_match('/^mycard-.+?-win32\..*$/',$var))return true;
return false;
});
}
natcasesort($files);
$files = array_reverse($files);
$location = "http://$_SERVER[HTTP_HOST]/downloads/";
$location .= $files[0];
echo $location;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment