<?PHP
// Coinurl Gateway v1.1
// Author: Ptv2
// Website: http://www.pak-soft.org/vb/
// Function getBrowser() from http://php.net/manual/en/function.get-browser.php#101125
error_reporting(0);
session_start();


// Settings
$URLs_file = "urls.dat"; // A file which contains urls 
$log_enabled  = true; // Enable/disable log - Boolean true or false.
$log_file     = "status.log"; // $log_enabled must be "true" for this to take effect
$isCloudFlare = false; // If your host/server is protected by CloudFLare set this to true. Boolean true/false only.




// Do not change anything staring from here. Those are functions and pre-set internal variables
function getCoinUrl($url)
{
    $uuid = "5165fa0fa1bb6049715674";
    $url = rawurlencode($url);

    $result = file_get_contents("https://coinurl.com/api.php?uuid={$uuid}&url={$url}");

    if($result == 'error')
        return false;
    else
        return $result;
}


$RDR   = false;
$proxy = "Not detected";
if (!file_exists($URLs_file))
    file_put_contents($URLs_file, getCoinUrl("http://www.elite7hackers.us")."\n".getCoinUrl("http://leak.sx")."\n".getCoinUrl("https://www.google.com"));

$URLs = file($URLs_file);

function getBrowser()
{
    $u_agent  = $_SERVER['HTTP_USER_AGENT'];
    $bname    = 'Unknown';
    $platform = 'Unknown';
    $version  = "";
    
    //First get the platform?
    if (preg_match('/linux/i', $u_agent)) {
        $platform = 'linux';
    } elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
        $platform = 'mac';
    } elseif (preg_match('/windows|win32/i', $u_agent)) {
        $platform = 'windows';
    }
    
    // Next get the name of the useragent yes seperately and for good reason
    if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) {
        $bname = 'Internet Explorer';
        $ub    = "MSIE";
    } elseif (preg_match('/Firefox/i', $u_agent)) {
        $bname = 'Mozilla Firefox';
        $ub    = "Firefox";
    } elseif (preg_match('/Chrome/i', $u_agent)) {
        $bname = 'Google Chrome';
        $ub    = "Chrome";
    } elseif (preg_match('/Safari/i', $u_agent)) {
        $bname = 'Apple Safari';
        $ub    = "Safari";
    } elseif (preg_match('/Opera/i', $u_agent)) {
        $bname = 'Opera';
        $ub    = "Opera";
    } elseif (preg_match('/Netscape/i', $u_agent)) {
        $bname = 'Netscape';
        $ub    = "Netscape";
    }
    
    // finally get the correct version number
    $known   = array(
        'Version',
        $ub,
        'other'
    );
    $pattern = '#(?<browser>' . join('|', $known) . ')[/ ]+(?<version>[0-9.|a-zA-Z.]*)#';
    if (!preg_match_all($pattern, $u_agent, $matches)) {
        // we have no matching number just continue
    }
    
    // see how many we have
    $i = count($matches['browser']);
    if ($i != 1) {
        //we will have two since we are not using 'other' argument yet
        //see if version is before or after the name
        if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) {
            $version = $matches['version'][0];
        } else {
            $version = $matches['version'][1];
        }
    } else {
        $version = $matches['version'][0];
    }
    
    // check if we have a number
    if ($version == null || $version == "") {
        $version = "?";
    }
    
    return array(
        'userAgent' => $u_agent,
        'name' => $bname,
        'version' => $version,
        'platform' => $platform,
        'pattern' => $pattern
    );
}


$UA = getBrowser();
$CF = "";
if (!$isCloudFlare) {
    $IP = @$_SERVER['REMOTE_ADDR'];
} else {
    $IP = @$_SERVER["HTTP_CF_CONNECTING_IP"] or $IP = @$_SERVER['REMOTE_ADDR'];
    $CF = "-> Cloudflare possible";
}

if ($_SERVER['HTTP_X_FORWARDED_FOR'] || $_SERVER['HTTP_X_FORWARDED'] || $_SERVER['HTTP_FORWARDED_FOR'] || $_SERVER['HTTP_VIA'] || in_array($_SERVER['REMOTE_PORT'], array(
    8080,
    80,
    6588,
    8000,
    9000,
    9090,
    3128,
    553,
    554
)) || @fsockopen($IP, 80, $errno, $errstr, 1)) {
    $proxy = "detected";
}



$ref = @$_SERVER['HTTP_REFERER'] or $ref = "Unknown";
$vID        = sha1(session_id() . $IP . $UA['name'] . $UA['version'] . $UA['platform'] . $proxy); // Unique visistor ID
$visit_data = "Visistor $vID from IP $IP -> Browser: $UA[name] $UA[version] | OS: $UA[platform] | Proxy: $proxy $CF\n";

class xzlog
{
    public $file = "log_file.log";
    public $swch = false;
    
    
    public function file($file)
    {
        $this->file = $file;
    }
    
    public function status($eod)
    {
        $this->swch = $eod;
    }
    
    public function write($data = "This is log comment")
    {
        if ($this->swch) {
            $log_data = date("[d/M/Y H:i:s]") . " $data";
            
            if (file_put_contents($this->file, $log_data, FILE_APPEND)) {
                return true;
            }
        }
        return false;
    }
}
$log = new xzlog;
$log->file($log_file);
$log->status($log_enabled);
$log->write($visit_data);
$log->write("Visistor referred by $ref\n");


if (isset($_GET['id'])) {
    
    $ID = preg_replace('/\D/', '', $_GET['id']);
    $log->write('$_GET[\'id\'] parameter detected -> ' . $ID . "\n");
    if (array_key_exists($ID, $URLs)) {
        $URL = trim(preg_replace('/\s+/', ' ', $URLs[$ID]));
        $log->write('$_GET[\'id\'] parameter  -> ' . $ID . " has found in database -> $URL\n");
        $RDR = true;
    }
    
} else {
    $URL_count = count($URLs) - 1;
    $ID        = mt_rand(0, $URL_count);
    $URL       = trim(preg_replace('/\s+/', ' ', $URLs[$ID]));
    $log->write("ID selected by random  -> $ID  ->  $URL\n");
    
    $RDR = true;
    
}

if ($RDR) {
    $log->write("Visistor $vID redirected to it's location.\n" . str_repeat("_", 148) . "\n");
    header("location: $URL");
    
    // Just in case if browser ignore redirect header
    $eHTML = "<html>\n<head>\n<meta http-equiv=\"refresh\" content=\"0; url=$url\">\n</head>\n<body>\n<h1><a href='$URL'>CLICK HERE</a></h1>\n</body>\n</html>";
    die($eHTML);
} else {
    $log->write('$_GET[\'id\'] parameter  -> ' . $ID . " has not found in database\n" . str_repeat("_", 148) . "\n");
    echo "<html>\n<head><title>ID Not found</title></head><body>\nThe URL ID you have selected is not available in database.<br/>\n";
    echo "Check one of the exist URLs:<br/>\n";
    foreach ($URLs as $key => $URL) {
        echo "<strong>$key -></strong> <a href='?id=$key'>$URL</a><br/>\n";
    }
    echo "</body>\n</html>";
    exit;
}

?>