session = new Session($_SESSION['user' . Config::basepath()] ?? []); $this->usermanager = new Modeluser(); $this->setuser(); $this->router = $router; $this->pagemanager = new Modelpage(); $this->initplates(); $this->now = new DateTimeImmutable("now", timezone_open("Europe/Paris")); } public function setuser() { // check session, then cookies if (!empty($this->session->user)) { $user = $this->usermanager->get($this->session->user); } elseif (!empty($_COOKIE['authtoken'])) { try { $modelconnect = new Modelconnect(); $datas = $modelconnect->checkcookie(); $user = $this->usermanager->get($datas['userid']); if ($user !== false && $user->checksession($datas['wsession'])) { $this->session->addtosession("wsession", $datas['wsession']); $this->session->addtosession("user", $datas['userid']); } else { $user = false; } } catch (Exception $e) { Model::sendflashmessage("Invalid Autentification cookie exist : $e", "warning"); } } // create visitor if (empty($user)) { $this->user = new User(); } else { $this->user = $user; } } public function initplates() { $router = $this->router; $this->plates = new Engine(Model::TEMPLATES_DIR); $this->plates->registerFunction('url', function (string $string, array $vars = [], string $get = '') { return $this->generate($string, $vars, $get); }); $this->plates->registerFunction('upage', function (string $string, string $id) { return $this->generate($string, ['page' => $id]); }); $this->plates->addData(['flashmessages' => Model::getflashmessages()]); } public function showtemplate($template, $params) { $params = array_merge($this->commonsparams(), $params); echo $this->plates->render($template, $params); } public function commonsparams() { $commonsparams = []; $commonsparams['router'] = $this->router; $commonsparams['user'] = $this->user; $commonsparams['pagelist'] = $this->pagemanager->list(); $commonsparams['css'] = Model::assetscsspath(); $commonsparams['now'] = new DateTimeImmutable(); return $commonsparams; } /** * Generate the URL for a named route. Replace regexes with supplied parameters. * * @param string $route The name of the route. * @param array $params Associative array of parameters to replace placeholders with. * @param string $get Optionnal query GET parameters formated * @return string The URL of the route with named parameters in place. * @throws InvalidArgumentException If the route does not exist. */ public function generate(string $route, array $params = [], string $get = ''): string { try { return $this->router->generate($route, $params) . $get; } catch (Exception $e) { throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e); } } public function redirect($url) { header('Location: ' . $url); exit; } public function routedirect(string $route, array $vars = []) { $this->redirect($this->generate($route, $vars)); } public function routedirectget(string $route, array $vars = []) { $get = '?'; foreach ($vars as $key => $value) { $get .= $key . '=' . $value . '&'; } $get = rtrim($get, '&'); $this->redirect($this->generate($route, []) . $get); } public function error(int $code) { http_response_code($code); exit; } /** * */ public function sendstatflashmessage(int $count, int $total, string $message) { if ($count === $total) { Model::sendflashmessage($count . ' / ' . $total . ' ' . $message, 'success'); } elseif ($count > 0) { Model::sendflashmessage($count . ' / ' . $total . ' ' . $message, 'warning'); } else { Model::sendflashmessage($count . ' / ' . $total . ' ' . $message, 'error'); } } }