diff --git a/app/core/App.php b/app/core/App.php index 994a996..f130c68 100644 --- a/app/core/App.php +++ b/app/core/App.php @@ -2,15 +2,22 @@ class App { - public array $config; - public object $database; - public object $session; + public array $config; + public Database $database; + public Session $session; + public User $user; - public function __construct(array $config, Database $database, Session $session) + public function __construct( + array $config, + Database $database, + Session $session, + User $user + ) { - $this->config = $config; + $this->config = $config; $this->database = $database; - $this->session = $session; + $this->session = $session; + $this->user = $user; } // Grab model @@ -40,4 +47,11 @@ class App // Convert and respond with data echo json_encode($data); } + + // Redirect to given url + public function redirect(string $url): void + { + header("Location: $url"); + die(); + } } \ No newline at end of file diff --git a/app/core/Database.php b/app/core/Database.php index fd85524..397d70a 100644 --- a/app/core/Database.php +++ b/app/core/Database.php @@ -4,11 +4,12 @@ // TODO: refactor and add different driver implementations class Database { - public object $conn; // Holds PDO connection object + public PDO $conn; public function __construct(array $config) { - if ($config['name'] !== 'mysql') { + if ($config['name'] !== 'mysql') + { throw new Exception("Database error: ".$config['name']." is not implemented"); } diff --git a/app/core/Session.php b/app/core/Session.php index 4a9a17f..83fe466 100644 --- a/app/core/Session.php +++ b/app/core/Session.php @@ -11,4 +11,70 @@ class Session session_start(); } } + + public function has(string $key): bool + { + return array_key_exists($key, $_SESSION); + } + + public function get(string $key): mixed + { + if ($this->has($key)) + { + return $_SESSION[$key]; + } + return NULL; + } + + public function set(string $key, mixed $value): void + { + $_SESSION[$key] = $value; + } + + public function remove(string $key): void + { + if ($this->has($key)) + { + unset($_SESSION[$key]); + } + } + + public function clear(): void + { + session_unset(); + } + + // TODO: throwaway code; rewrite for readability and also implement proper flashing by removing messages after one request + public function flash(string $msg, string $type = 'info'): void + { + $key = 'flashed_messages'; + if (!$this->has($key)) { + $this->set($key, []); + } + if (count($this->get($key)) >= 100) { + $this->remove($key); + throw new Exception('Too many flashed messages!'); + } + $msgs = $this->get($key); + $msgs[] = [ + "message" => htmlspecialchars($msg), + "type" => $type + ]; + $this->set( + $key, + $msgs + ); + } + + public function getFlashedMessages(): ?array + { + $key = 'flashed_messages'; + if ($this->has($key)) { + $msgs = $this->get($key); + $this->remove($key); + return $msgs; + } + return NULL; + } + // END TODO; } \ No newline at end of file diff --git a/app/core/User.php b/app/core/User.php new file mode 100644 index 0000000..a38e323 --- /dev/null +++ b/app/core/User.php @@ -0,0 +1,29 @@ +session = $session; + + $this->loggedIn = $this->session->get('loggedIn'); + $this->username = $this->session->get('username'); + } + + public function login(string $username, string $password): bool + { + if ($username === 'William' && $password === 'William') + { + $this->session->set('loggedIn', TRUE); + $this->session->set('username', 'William'); + return TRUE; + } + return FALSE; + } +} \ No newline at end of file diff --git a/app/inc.php b/app/inc.php index fc6ab31..fa486a2 100644 --- a/app/inc.php +++ b/app/inc.php @@ -12,6 +12,9 @@ Tread carefully */ +// Disable type coercion +declare(strict_types=1); + // Autoloader spl_autoload_register(function ($class_name) { require __DIR__ . '/core/' . $class_name . '.php'; @@ -21,18 +24,21 @@ spl_autoload_register(function ($class_name) { $config = require __DIR__ . '/config.php'; $database = new Database($config['database']); $session = new Session; +$user = new User($session); $app = new App( $config, $database, - $session + $session, + $user ); -// We will want to use $app instead +// We will use $app instead unset( $config, $database, - $session + $session, + $user ); return $app; \ No newline at end of file diff --git a/app/view/example.php b/app/view/example.php new file mode 100644 index 0000000..cdc9d08 --- /dev/null +++ b/app/view/example.php @@ -0,0 +1,2 @@ +
Dette er en eksempel side
\ No newline at end of file diff --git a/app/view/index.php b/app/view/index.php index e2bad4d..2cc9fda 100644 --- a/app/view/index.php +++ b/app/view/index.php @@ -1,3 +1,3 @@ -Ting fungerer noen ganger.
=$data?>
\ No newline at end of file diff --git a/app/view/template/header.php b/app/view/template/header.php index 202a4f7..65aaa4c 100644 --- a/app/view/template/header.php +++ b/app/view/template/header.php @@ -10,15 +10,22 @@