generated from mirzaev/pot
	Initial commit
This commit is contained in:
		
							
								
								
									
										126
									
								
								author/project/system/controllers/core.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										126
									
								
								author/project/system/controllers/core.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,126 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace mirzaev\shabashka\controllers;
 | 
			
		||||
 | 
			
		||||
// Files of the project
 | 
			
		||||
use mirzaev\shabashka\views\templater,
 | 
			
		||||
	mirzaev\shabashka\models\core as models,
 | 
			
		||||
	mirzaev\shabashka\models\session,
 | 
			
		||||
	mirzaev\shabashka\models\enumerations\language;
 | 
			
		||||
 | 
			
		||||
// Framework for PHP
 | 
			
		||||
use mirzaev\minimal\core as minimal,
 | 
			
		||||
	mirzaev\minimal\controller,
 | 
			
		||||
	mirzaev\minimal\http\response,
 | 
			
		||||
	mirzaev\minimal\http\enumerations\status;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Controllers core
 | 
			
		||||
 *
 | 
			
		||||
 * @package mirzaev\shabashka\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 mirzaev <mail@domain.zone>
 | 
			
		||||
 */
 | 
			
		||||
class core extends controller
 | 
			
		||||
{
 | 
			
		||||
	/**
 | 
			
		||||
	 * Session
 | 
			
		||||
	 * 
 | 
			
		||||
	 * @var session|null $session Instance of the session
 | 
			
		||||
	 */
 | 
			
		||||
	protected readonly session $session;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Language
 | 
			
		||||
	 *
 | 
			
		||||
	 * @var language $language Language
 | 
			
		||||
	 */
 | 
			
		||||
	protected language $language = language::en;
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Response
 | 
			
		||||
	 *
 | 
			
		||||
	 * @see https://wiki.php.net/rfc/property-hooks (find a table about backed and virtual hooks)
 | 
			
		||||
	 *
 | 
			
		||||
	 * @var response $response Response
 | 
			
		||||
	 */
 | 
			
		||||
	protected response $response {
 | 
			
		||||
		// Read
 | 
			
		||||
		get => $this->response ??= $this->request->response();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Errors
 | 
			
		||||
	 *
 | 
			
		||||
	 * @var array $errors Registry of errors
 | 
			
		||||
	 */
 | 
			
		||||
	protected array $errors = [
 | 
			
		||||
		'session' => [],
 | 
			
		||||
		'account' => []
 | 
			
		||||
	];
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Constructor
 | 
			
		||||
	 *
 | 
			
		||||
	 * @param minimal $core Instance of the MINIMAL
 | 
			
		||||
	 * @param bool $initialize Initialize a controller?
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return void
 | 
			
		||||
	 */
 | 
			
		||||
	public function __construct(minimal $core, bool $initialize = true)
 | 
			
		||||
	{
 | 
			
		||||
		// 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;
 | 
			
		||||
 | 
			
		||||
		// For the extends system
 | 
			
		||||
		parent::__construct(core: $core);
 | 
			
		||||
 | 
			
		||||
		if ($initialize) {
 | 
			
		||||
			// Requestet initializing
 | 
			
		||||
 | 
			
		||||
			// Initializing core of the models
 | 
			
		||||
			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);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										70
									
								
								author/project/system/controllers/index.php
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										70
									
								
								author/project/system/controllers/index.php
									
									
									
									
									
										Executable file
									
								
							@@ -0,0 +1,70 @@
 | 
			
		||||
<?php
 | 
			
		||||
 | 
			
		||||
declare(strict_types=1);
 | 
			
		||||
 | 
			
		||||
namespace mirzaev\shabashka\controllers;
 | 
			
		||||
 | 
			
		||||
// Files of the project
 | 
			
		||||
use mirzaev\shabashka\controllers\core;
 | 
			
		||||
 | 
			
		||||
// Framework for PHP
 | 
			
		||||
use mirzaev\minimal\http\enumerations\content,
 | 
			
		||||
  mirzaev\minimal\http\enumerations\status;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Index
 | 
			
		||||
 *
 | 
			
		||||
 * @package mirzaev\shabashka\controllers
 | 
			
		||||
 *
 | 
			
		||||
 * @param array $errors Registry of errors
 | 
			
		||||
 *
 | 
			
		||||
 * @method null index() Main page
 | 
			
		||||
 *
 | 
			
		||||
 * @license http://www.wtfpl.net/ Do What The Fuck You Want To Public License
 | 
			
		||||
 * @author mirzaev <mail@domain.zone>
 | 
			
		||||
 */
 | 
			
		||||
final class index extends core
 | 
			
		||||
{
 | 
			
		||||
	/**
 | 
			
		||||
	 * Errors
 | 
			
		||||
	 *
 | 
			
		||||
	 * @var array $errors Registry of errors
 | 
			
		||||
	 */
 | 
			
		||||
	protected array $errors = [
 | 
			
		||||
		'session' => []
 | 
			
		||||
	];
 | 
			
		||||
 | 
			
		||||
	/**
 | 
			
		||||
	 * Main page
 | 
			
		||||
	 *
 | 
			
		||||
	 * @return null
 | 
			
		||||
	 */
 | 
			
		||||
	public function index(): null
 | 
			
		||||
	{
 | 
			
		||||
		if (str_contains($this->request->headers['accept'], content::any->value)) {
 | 
			
		||||
			// Request for any response
 | 
			
		||||
 | 
			
		||||
			// Render page
 | 
			
		||||
			$page = $this->view->render('index.html');
 | 
			
		||||
 | 
			
		||||
			// Sending response
 | 
			
		||||
			$this->response
 | 
			
		||||
				->start()
 | 
			
		||||
				->clean()
 | 
			
		||||
				->sse()
 | 
			
		||||
				->write($page)
 | 
			
		||||
				->validate($this->request)
 | 
			
		||||
				?->body()
 | 
			
		||||
				->end();
 | 
			
		||||
 | 
			
		||||
			// Deinitializing rendered page
 | 
			
		||||
			unset($page);
 | 
			
		||||
 | 
			
		||||
			// Exit (success)
 | 
			
		||||
			return null;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		// Exit (fail)
 | 
			
		||||
		return null;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user