From 7882449469cac005fdb073bd9c78e1dc254106d5 Mon Sep 17 00:00:00 2001 From: William Date: Sun, 23 Jan 2022 22:56:36 +0100 Subject: [PATCH] Big fat commit --- app/core/App.php | 26 ++++++++++---- app/core/Database.php | 5 +-- app/core/Session.php | 66 ++++++++++++++++++++++++++++++++++++ app/core/User.php | 29 ++++++++++++++++ app/inc.php | 12 +++++-- app/view/example.php | 2 ++ app/view/index.php | 2 +- app/view/template/header.php | 26 +++++++++++--- public/example.php | 11 ++++++ public/index.php | 4 +-- public/login.php | 15 ++++++++ public/logout.php | 6 ++++ 12 files changed, 185 insertions(+), 19 deletions(-) create mode 100644 app/core/User.php create mode 100644 app/view/example.php create mode 100644 public/example.php create mode 100644 public/logout.php 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 @@ +

Eksempel side

+

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 @@ -

Velkommen til forsida!

+

Forside

Ting fungerer noen ganger.

\ 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 @@ @@ -30,10 +37,21 @@
+ session->getFlashedMessages(); + if ($msgs) + { + foreach ($msgs as $msg) + { + echo "
{$msg["message"]}
"; + } + } + ?> \ No newline at end of file diff --git a/public/example.php b/public/example.php new file mode 100644 index 0000000..7200be2 --- /dev/null +++ b/public/example.php @@ -0,0 +1,11 @@ +session->flash('Eksempel', 'info'); +$app->session->flash('Eksempel', 'success'); +$app->session->flash('Eksempel', 'warning'); +$app->session->flash('Eksempel', 'danger'); + +$app->view('template/header', ['title' => 'Eksempel side']); +$app->view('example'); +$app->view('template/footer'); \ No newline at end of file diff --git a/public/index.php b/public/index.php index b78ce92..523cec1 100644 --- a/public/index.php +++ b/public/index.php @@ -4,7 +4,5 @@ $app = require '../app/inc.php'; $model = $app->model('Index'); $app->view('template/header', ["title" => "Forside"]); -$app->view('index', [ - "data" => $model->getUsername() -]); +$app->view('index', ["data" => $model->getUsername()]); $app->view('template/footer'); \ No newline at end of file diff --git a/public/login.php b/public/login.php index 1d87b45..7a03a04 100644 --- a/public/login.php +++ b/public/login.php @@ -1,6 +1,21 @@ user->loggedIn) { + $app->session->flash('Du er allerede pålogget'); + $app->redirect('index.php'); +} + +if ($_SERVER['REQUEST_METHOD'] === 'POST') +{ + if ($app->user->login($_POST['username'], $_POST['password'])) + { + $app->session->flash("Velkommen {$_POST['username']}!"); + $app->redirect('index.php'); + } + $app->session->flash('Feil påloggingsinformasjon', 'danger'); +} + $app->view('template/header', ["title" => "Logg inn"]); $app->view('login'); $app->view('template/footer'); \ No newline at end of file diff --git a/public/logout.php b/public/logout.php new file mode 100644 index 0000000..18db648 --- /dev/null +++ b/public/logout.php @@ -0,0 +1,6 @@ +session->clear(); +$app->session->flash('Du har blitt logget av'); +$app->redirect("{$app->config["root_url"]}login.php"); \ No newline at end of file