Big fat commit

This commit is contained in:
William 2022-01-23 22:56:36 +01:00
parent 287b2c2224
commit 7882449469
12 changed files with 185 additions and 19 deletions

View File

@ -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();
}
}

View File

@ -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");
}

View File

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

29
app/core/User.php Normal file
View File

@ -0,0 +1,29 @@
<?php
// TODO: ...
class User
{
private Session $session;
public ?bool $loggedIn;
public ?string $username;
public function __construct(Session $session)
{
$this->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;
}
}

View File

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

2
app/view/example.php Normal file
View File

@ -0,0 +1,2 @@
<h1>Eksempel side</h1>
<p>Dette er en eksempel side</p>

View File

@ -1,3 +1,3 @@
<h1>Velkommen til forsida!</h1>
<h1>Forside</h1>
<p>Ting fungerer noen ganger.</p>
<p><?=$data?></p>

View File

@ -10,15 +10,22 @@
<body>
<div id="header">
<a>Stafett for livet tellesystem</a>
<a href="<?=$this->config['root_url']?>">Stafett for livet tellesystem</a>
</div>
<hr class="hidden">
<div id="menu">
<small>
<span>Ikke pålogget</span>
<a href="<?=$this->config['root_url']?>login.php">Logg inn</a>
<?php if ($this->user->loggedIn): ?>
<span><?=htmlspecialchars($this->user->username)?></span>
<a href="<?=$this->config['root_url']?>logout.php">Logg ut</a>
<?php else: ?>
<span>Ikke pålogget</span>
<a href="<?=$this->config['root_url']?>login.php">Logg inn</a>
<?php endif; ?>
</small>
</div>
@ -30,10 +37,21 @@
<ul>
<li><a href="<?=$this->config['root_url']?>index.php">Forside</a></li>
<li><a href="<?=$this->config['root_url']?>login.php">Logg inn</a></li>
<li><a href="<?=$this->config['root_url']?>">Eksempel</a></li>
<li><a href="<?=$this->config['root_url']?>example.php">Eksempel</a></li>
</ul>
</div>
<hr class="hidden">
<div id="main">
<?php
// Display flashed messages
$msgs = $this->session->getFlashedMessages();
if ($msgs)
{
foreach ($msgs as $msg)
{
echo "<div class=\"alert {$msg["type"]}\">{$msg["message"]}</div>";
}
}
?>

11
public/example.php Normal file
View File

@ -0,0 +1,11 @@
<?php
$app = require '../app/inc.php';
$app->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');

View File

@ -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');

View File

@ -1,6 +1,21 @@
<?php
$app = require '../app/inc.php';
if ($app->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');

6
public/logout.php Normal file
View File

@ -0,0 +1,6 @@
<?php
$app = require '../app/inc.php';
$app->session->clear();
$app->session->flash('Du har blitt logget av');
$app->redirect("{$app->config["root_url"]}login.php");