*/ protected $helpers = ['common']; /** * Session instance */ protected $session; /** * Current authenticated user */ public $user; /** * Authentication status */ public $authenticated = false; /** * Application version */ public $version = '1.00.81'; /** * User Model instance */ protected $userModel; /** * @return void */ public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { // Do Not Edit This Line parent::initController($request, $response, $logger); // Preload any models, libraries, etc, here. $this->session = \Config\Services::session(); // Make version available to all views $renderer = \Config\Services::renderer(); $renderer->setVar('version', $this->version); // Initialize user session handler $this->handleSession(); } /** * Handle session and authentication */ protected function handleSession(): void { // Garbage collection for old sessions $this->garbageCiSessions(); // Set secure site in production $this->setSecureSite(); // Retrieve user session $retrieveSession = new RetrieveUserSession($this->session); $retrieveSession->handle(); // Get the authenticated flag and user ID from constants $this->authenticated = defined('AUTHENTICATED') ? AUTHENTICATED : false; if ($this->authenticated && defined('USERID') && USERID) { $this->user = \User::find(USERID); } } /** * Garbage collection for old CI sessions */ protected function garbageCiSessions(): void { $number = random_int(0, 100); if ($number !== 17) { return; } $db = \Config\Database::connect(); $db->query("DELETE FROM ci_sessions WHERE TIMESTAMPDIFF(MINUTE, FROM_UNIXTIME(timestamp), NOW()) > 60"); } /** * Force HTTPS in production */ protected function setSecureSite(): void { if (($_ENV['ENVIRONMENT'] ?? 'development') !== 'production') { return; } if (!isset($_SERVER["HTTPS"]) || $_SERVER["HTTPS"] !== "on") { header("Location: " . ($_ENV['APP_URL'] ?? '')); exit; } } /** * Clean input value (replaces CI3's clean_input method) */ protected function cleanInput(string $key): string { $value = $this->request->getPost($key) ?? $this->request->getGet($key) ?? ''; return str_replace('null', '', $value); } /** * Return JSON response (replaces CI3's json method) */ protected function json($data): ResponseInterface { return $this->response ->setContentType('application/json') ->setJSON($data); } /** * Check if user is authorized (placeholder for subclass implementation) */ public function unAuthorized(): void { // Override in subclasses if needed } }