228bad2e59
Translates errors to english Makes some User fields optional
92 lines
2.3 KiB
PHP
92 lines
2.3 KiB
PHP
<?php
|
|
require __DIR__ . '/../vendor/autoload.php';
|
|
|
|
use Kavalanche\Security\AuthenticationProvider;
|
|
use Kavalanche\Security\InMemoryUserProvider;
|
|
use Kavalanche\Security\SecurityException;
|
|
use Kavalanche\Security\UserNotAuthenticatedException;
|
|
|
|
session_start();
|
|
|
|
if (isset($_GET['logout'])) {
|
|
session_destroy();
|
|
header('Location: /');
|
|
exit;
|
|
}
|
|
|
|
function getUserArticles($user) {
|
|
$data = [
|
|
[
|
|
'user_id' => 1,
|
|
'title' => 'Secret note',
|
|
'body' => 'Secret content'
|
|
],
|
|
[
|
|
'user_id' => 2,
|
|
'title' => 'Hello world',
|
|
'body' => 'How are you doing?'
|
|
]
|
|
];
|
|
$articles = [];
|
|
foreach ($data as $article) {
|
|
if ($article['user_id'] === $user->getId()) {
|
|
$articles[] = (object) $article;
|
|
}
|
|
}
|
|
return $articles;
|
|
}
|
|
|
|
$users = unserialize(file_get_contents(__DIR__ . '/../data/users'));
|
|
$userProvider = new InMemoryUserProvider($users);
|
|
$authenticationProvider = new AuthenticationProvider($userProvider);
|
|
|
|
try {
|
|
$user = $authenticationProvider->authenticate();
|
|
} catch (SecurityException $ex) {
|
|
$user = false;
|
|
}
|
|
|
|
if (isset($_GET['login'])) {
|
|
try {
|
|
$user = $authenticationProvider->authenticate();
|
|
} catch (SecurityException $ex) {
|
|
if (!$ex instanceof UserNotAuthenticatedException) {
|
|
?>
|
|
<div class="alert"><p><?php echo $ex->getMessage(); ?></p></div>
|
|
<?php
|
|
}
|
|
?>
|
|
<form method="post">
|
|
<input type="text" name="email" id="login_username"><label for="login_username">E-mail</label><br>
|
|
<input type="password" name="password" id="login_password"><label for="login_password">Password</label><br>
|
|
<input type="submit" value="Log in">
|
|
</form>
|
|
<?php
|
|
exit;
|
|
}
|
|
}
|
|
|
|
|
|
if (!empty($user)) {
|
|
echo 'Hello ' . ($user->getName() ?? "anonymous") . ' | <a href="?logout">Log out</a>';
|
|
} else {
|
|
echo '<a href="?login">Log in</a>';
|
|
}
|
|
if (!empty($user)) {
|
|
$articles = getUserArticles($user);
|
|
} else {
|
|
$articles = [];
|
|
}
|
|
|
|
foreach ($articles as $article) {
|
|
?>
|
|
<article>
|
|
<h2><?php echo $article->title; ?></h2>
|
|
<div><?php echo $article->body; ?></div>
|
|
</article>
|
|
<?php
|
|
}
|
|
|
|
if (empty($articles)) {
|
|
echo '<p>Log in to see your posts.</p>';
|
|
}
|