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; } }