pot/author/project/system/controllers/core.php

126 lines
3.0 KiB
PHP
Raw Normal View History

2024-01-11 04:35:40 +07:00
<?php
declare(strict_types=1);
2024-01-11 05:38:30 +07:00
namespace ${REPO_OWNER}\${REPO_NAME}\controllers;
2024-01-11 04:35:40 +07:00
// Files of the project
2024-01-11 05:38:30 +07:00
use ${REPO_OWNER}\${REPO_NAME}\views\manager,
${REPO_OWNER}\${REPO_NAME}\models\core as models,
${REPO_OWNER}\${REPO_NAME}\models\session;
2024-01-11 04:35:40 +07:00
// Framework for PHP
use mirzaev\minimal\core as minimal,
mirzaev\minimal\controller,
mirzaev\minimal\http\response,
mirzaev\minimal\http\enumerations\status;
2024-01-11 04:35:40 +07:00
/**
* Controllers core
2024-01-11 04:35:40 +07:00
*
2024-01-11 05:38:30 +07:00
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
*
* @param session $session Instance of the session
* @param language $language Language
* @param response $response Response
* @param array $errors Registry of errors
*
* @method void __construct(minimal $minimal, bool $initialize) Constructor
*
* @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
* @author ${REPO_OWNER} <mail@domain.zone>
2024-01-11 04:35:40 +07:00
*/
class core extends controller
{
/**
* Session
*
* @var session|null $session Instance of the session
2024-01-11 04:35:40 +07:00
*/
protected readonly session $session;
2024-01-11 04:35:40 +07:00
/**
* Language
*
* @var language $language Language
2024-01-11 04:35:40 +07:00
*/
protected language $language = language::en;
2024-01-11 04:35:40 +07:00
/**
* Response
*
* @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
*
* @var response $response Response
2024-01-11 04:35:40 +07:00
*/
protected response $response {
// Read
get => $this->response ??= $this->request->response();
}
2024-01-11 04:35:40 +07:00
/**
* Errors
*
* @var array $errors Registry of errors
2024-01-11 04:35:40 +07:00
*/
protected array $errors = [
'session' => [],
'account' => []
];
/**
* Constructor
2024-01-11 04:35:40 +07:00
*
* @param minimal $minimal Instance of the MINIMAL
2024-01-11 04:35:40 +07:00
* @param bool $initialize Initialize a controller?
*
* @return void
*/
public function __construct(minimal $minimal, bool $initialize = true)
2024-01-11 04:35:40 +07:00
{
// Blocking requests from CloudFlare (better to write this blocking into nginx config file)
if (isset($_SERVER['HTTP_USER_AGENT']) && $_SERVER['HTTP_USER_AGENT'] === 'nginx-ssl early hints') return status::bruh->label;
2024-01-11 04:35:40 +07:00
// For the extends system
parent::__construct(core: $minimal);
2024-01-11 04:35:40 +07:00
if ($initialize) {
// Requestet initializing
2024-01-11 04:35:40 +07:00
// Initializing core of the models
2024-01-11 04:35:40 +07:00
new models();
// Initializing of the date until which the session will be active
$expires = strtotime('+1 week');
// Initializing of default value of hash of the session
$_COOKIE["session"] ??= null;
// Initializing of session
$this->session = new session($_COOKIE["session"], $expires, $this->errors['session']);
// Handle a problems with initializing a session
if (!empty($this->errors['session'])) die;
else if ($_COOKIE["session"] !== $this->session->hash) {
// Hash of the session is changed (implies that the session has expired and recreated)
// Write a new hash of the session to cookies
setcookie(
'session',
$this->session->hash,
[
'expires' => $expires,
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'strict'
]
);
}
// Initializing of preprocessor of views
$this->view = new templater($this->session);
}
}
}