| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- namespace App;
- use Exception;
- use Gettext\Loader\MoLoader;
- use Gettext\Translator;
- use Gettext\TranslatorFunctions;
- use Monolog\Logger;
- use RuntimeException;
- class Core {
- public const SCRIPT_GLOBAL = 0;
- public const SCRIPT_PAGE = 1;
- private static $instance;
- /** @var User */
- protected $user;
- /** @var Breadcrumb[] */
- protected $breadcrumbs = [];
- /** @var string */
- protected $locale;
- /** @var Translator */
- protected $translator;
- /** @var string */
- protected $controllerName = "home";
- /** @var string[] Global scripts */
- protected $scripts = [];
- /** @var string[] Page scripts */
- protected $pageScripts = [];
- /** @var string[] CSS stylesheets */
- protected $stylesheets = [];
- /** @var Logger */
- protected $logger;
- // Static methods
- public static function getInstance(): Core {
- if(self::$instance === null) {
- self::$instance = new Core();
- }
- return self::$instance;
- }
- public static function hasReCaptcha(): bool {
- return !empty($_ENV["RECAPTCHA_SECRET_KEY"]);
- }
- public static function reCaptchaCheck(string $recaptcha_token): bool {
- if(!self::hasReCaptcha()) {
- return true;
- }
- $url = sprintf("https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s&remoteip=%s",
- $_ENV["RECAPTCHA_SECRET_KEY"], $recaptcha_token, $_SERVER["REMOTE_ADDR"]
- );
- $response = file_get_contents($url);
- $object = json_decode($response);
- if(!$object) {
- return false;
- }
- return $object->success && $object->score >= 0.6;
- }
- /**
- * Core constructor.
- * @throws Exception
- */
- public function __construct() {
- if(PHP_SAPI !== "cli") {
- $this->initSession();
- }
- $this->initBreadcrumbs();
- $this->initLocalisation();
- }
- // Object methods
- /**
- * @return void
- * @throws Exception
- */
- private function initSession(): void {
- session_name($_ENV["SESSION_NAME"]);
- session_start();
- $user_id = $_SESSION["user_id"] ?? null;
- if($user_id && $this->hasConnection()) {
- $this->user = User::get($user_id);
- }
- if($this->isLoggedIn()) {
- setcookie($_ENV["SESSION_NAME"], session_id(), time() + 28 * 86400, "/");
- $this->user->touch();
- }
- register_shutdown_function(static function() {
- if(empty($_SESSION) && session_status() === PHP_SESSION_ACTIVE) {
- session_destroy();
- }
- });
- }
- private function initBreadcrumbs(): void {
- $this->breadcrumbs[] = new Breadcrumb("/", $this->getSiteName());
- }
- private function initLocalisation(): void {
- $this->locale = $_ENV["LOCALE"];
- $this->translator = new Translator;
- TranslatorFunctions::register($this->translator);
- $mo_path = $_ENV["DIR_ROOT"] . "/i18n/$this->locale/LC_MESSAGES/$this->locale.mo";
- if(!file_exists($mo_path)) {
- return;
- }
- $loader = new MoLoader();
- $translations = $loader->loadFile($mo_path);
- $this->translator->addTranslations($translations->toArray());
- }
- /**
- * @param string $path
- * @param string $mode
- * @throws Exception
- */
- public function addStylesheet(string $path, string $mode = "append"): void {
- if(!file_exists($_ENV["DIR_ROOT"] . "/public/$path")) {
- throw new RuntimeException("'$path' does not exist.");
- }
- if($mode === "prepend") {
- array_unshift($this->stylesheets, $path);
- }
- else {
- $this->stylesheets[] = $path;
- }
- }
- /**
- * @param string $path File path relative to /public
- * @param int $type
- * @throws Exception
- */
- public function addScript(string $path, int $type = self::SCRIPT_GLOBAL): void {
- if(!file_exists($_ENV["DIR_ROOT"] . "/public/$path")) {
- throw new RuntimeException("'$path' does not exist.");
- }
- if($type === self::SCRIPT_PAGE) {
- $this->pageScripts[] = $path;
- }
- else {
- $this->scripts[] = $path;
- }
- }
- public function getStylesheets(): array {
- return $this->stylesheets;
- }
- public function getGlobalScripts(): array {
- return $this->scripts;
- }
- public function getPageScripts(): array {
- return $this->pageScripts;
- }
- /**
- * @return Breadcrumb[]
- */
- public function getBreadcrumbs(): array {
- return $this->breadcrumbs;
- }
- public function getLocale(): string {
- return $this->locale;
- }
- /**
- * @param string $name
- * @param array $params
- * @throws Exception
- */
- public function requireController(string $name, array $params = []): void {
- extract($params, EXTR_OVERWRITE);
- $path = $_ENV["DIR_ROOT"] . "/controllers/$name.php";
- if(!file_exists($path)) {
- throw new RuntimeException("Controller '$name' could not be found.");
- }
- $this->controllerName = $name;
- require $path;
- }
- public function getUser(): ?User {
- return $this->user;
- }
- /**
- * @return bool
- * @throws Exception
- */
- public function isLoggedIn(): bool {
- return ($this->user !== null);
- }
- /**
- * @return string
- */
- public function getControllerName(): string {
- return $this->controllerName;
- }
- public function addBreadcrumb(string $url, string $name): void {
- $this->breadcrumbs[] = new Breadcrumb($url, $name);
- }
- public function hasConnection(): bool {
- return Database::$connection !== null;
- }
- public function getSiteName(): string {
- return $_ENV["SITE_NAME"];
- }
- public function getTabTitle(): string {
- $breadcrumbs = array_reverse($this->getBreadcrumbs());
- $parts = [];
- foreach($breadcrumbs as $breadcrumb) {
- $parts[] = $breadcrumb->getName();
- }
- return implode(" | ", $parts);
- }
- public function getSettings(): object {
- $filename = "settings.json";
- if(file_exists($filename)) {
- $settings_json = file_get_contents($filename);
- $settings = json_decode($settings_json);
- }
- else {
- $settings = (object) [
- "defaultCompanyId" => null
- ];
- }
- return $settings;
- }
- public function saveSettings(object $settings): bool {
- return file_put_contents("settings.json", json_encode($settings));
- }
- /**
- * @param Logger $logger
- */
- public function setLogger(Logger $logger): void {
- $this->logger = $logger;
- }
- /**
- * @return Logger
- */
- public function getLogger(): Logger {
- return $this->logger;
- }
- }
|