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\account_model as account,
|
|
|
|
${REPO_OWNER}\${REPO_NAME}\models\session_model as session;
|
2024-01-11 04:35:40 +07:00
|
|
|
|
|
|
|
// Library for ArangoDB
|
|
|
|
use ArangoDBClient\Document as _document;
|
|
|
|
|
|
|
|
// Framework for PHP
|
|
|
|
use mirzaev\minimal\controller;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core of controllers
|
|
|
|
*
|
2024-01-11 05:38:30 +07:00
|
|
|
* @package ${REPO_OWNER}\${REPO_NAME}\controllers
|
|
|
|
* @author ${REPO_OWNER} < mail >
|
2024-01-11 04:35:40 +07:00
|
|
|
*/
|
|
|
|
class core extends controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Postfix for name of controllers files
|
|
|
|
*/
|
|
|
|
final public const POSTFIX = '';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of a session
|
|
|
|
*/
|
|
|
|
protected readonly session $session;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance of an account
|
|
|
|
*/
|
|
|
|
protected readonly ?account $account;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registry of errors
|
|
|
|
*/
|
|
|
|
protected array $errors = [
|
|
|
|
'session' => [],
|
|
|
|
'account' => []
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor of an instance
|
|
|
|
*
|
|
|
|
* @param bool $initialize Initialize a controller?
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function __construct(bool $initialize = true)
|
|
|
|
{
|
|
|
|
// Blocking requests from CloudFlare (better to write this blocking into nginx config file)
|
|
|
|
if ($_SERVER['HTTP_USER_AGENT'] === 'nginx-ssl early hints') return;
|
|
|
|
|
|
|
|
// For the extends system
|
|
|
|
parent::__construct($initialize);
|
|
|
|
|
|
|
|
if ($initialize) {
|
|
|
|
// Initializing is requested
|
|
|
|
|
|
|
|
// Initializing of models core (connect to ArangoDB...)
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check of initialization
|
|
|
|
*
|
|
|
|
* Checks whether a property is initialized in a document instance from ArangoDB
|
|
|
|
*
|
|
|
|
* @param string $name Name of the property from ArangoDB
|
|
|
|
*
|
|
|
|
* @return bool The property is initialized?
|
|
|
|
*/
|
|
|
|
public function __isset(string $name): bool
|
|
|
|
{
|
|
|
|
// Check of initialization of the property and exit (success)
|
|
|
|
return match ($name) {
|
|
|
|
default => isset($this->{$name})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|