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

127 lines
3.1 KiB
PHP
Raw Permalink 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-12-16 22:44:36 +07:00
use ${REPO_OWNER}\${REPO_NAME}\views\templater,
2024-01-11 05:38:30 +07:00
${REPO_OWNER}\${REPO_NAME}\models\core as models,
${REPO_OWNER}\${REPO_NAME}\models\session,
${REPO_OWNER}\${REPO_NAME}\models\enumerations\language;
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
*
2024-12-15 22:16:01 +07:00
* @param session $$session Instance of the session
* @param language $$language Language
* @param response $$response Response
* @param array $$errors Registry of errors
*
2024-12-15 22:16:01 +07:00
* @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
*
2024-12-15 22:16:01 +07:00
* @var session|null $$session Instance of the session
2024-01-11 04:35:40 +07:00
*/
2024-12-15 22:16:01 +07:00
protected readonly session $$session;
2024-01-11 04:35:40 +07:00
/**
* Language
*
2024-12-15 22:16:01 +07:00
* @var language $$language Language
2024-01-11 04:35:40 +07:00
*/
2024-12-15 22:16:01 +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)
*
2024-12-15 22:16:01 +07:00
* @var response $$response Response
2024-01-11 04:35:40 +07:00
*/
2024-12-15 22:16:01 +07:00
protected response $$response {
// Read
2024-12-15 22:16:01 +07:00
get => $$this->response ??= $$this->request->response();
}
2024-01-11 04:35:40 +07:00
/**
* Errors
*
2024-12-15 22:16:01 +07:00
* @var array $$errors Registry of errors
2024-01-11 04:35:40 +07:00
*/
2024-12-15 22:16:01 +07:00
protected array $$errors = [
2024-01-11 04:35:40 +07:00
'session' => [],
'account' => []
];
/**
* Constructor
2024-01-11 04:35:40 +07:00
*
2024-12-16 22:42:08 +07:00
* @param minimal $$core Instance of the MINIMAL
2024-12-15 22:16:01 +07:00
* @param bool $$initialize Initialize a controller?
2024-01-11 04:35:40 +07:00
*
* @return void
*/
2024-12-16 22:42:08 +07:00
public function __construct(minimal $$core, bool $$initialize = true)
2024-01-11 04:35:40 +07:00
{
// Blocking requests from CloudFlare (better to write this blocking into nginx config file)
2024-12-15 22:16:01 +07:00
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
2024-12-16 22:42:08 +07:00
parent::__construct(core: $$core);
2024-01-11 04:35:40 +07:00
2024-12-15 22:16:01 +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
2024-12-15 22:16:01 +07:00
$$expires = strtotime('+1 week');
2024-01-11 04:35:40 +07:00
// Initializing of default value of hash of the session
2024-12-15 22:16:01 +07:00
$$_COOKIE["session"] ??= null;
2024-01-11 04:35:40 +07:00
// Initializing of session
2024-12-15 22:16:01 +07:00
$$this->session = new session($$_COOKIE["session"], $$expires, $$this->errors['session']);
2024-01-11 04:35:40 +07:00
// Handle a problems with initializing a session
2024-12-15 22:16:01 +07:00
if (!empty($$this->errors['session'])) die;
else if ($$_COOKIE["session"] !== $$this->session->hash) {
2024-01-11 04:35:40 +07:00
// 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',
2024-12-15 22:16:01 +07:00
$$this->session->hash,
2024-01-11 04:35:40 +07:00
[
2024-12-15 22:16:01 +07:00
'expires' => $$expires,
2024-01-11 04:35:40 +07:00
'path' => '/',
'secure' => true,
'httponly' => true,
'samesite' => 'strict'
]
);
}
// Initializing of preprocessor of views
2024-12-15 22:16:01 +07:00
$$this->view = new templater($$this->session);
2024-01-11 04:35:40 +07:00
}
}
}