Initial commit
This commit is contained in:
commit
e6ad944f79
22 files changed
+6566
No files matched your search
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lewisdale\App\Controllers;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Slim\Views\Twig;
|
||||
|
||||
class SampleController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Twig $view,
|
||||
private readonly LoggerInterface $logger
|
||||
) {}
|
||||
|
||||
public function get(ServerRequestInterface $request, ResponseInterface $response)
|
||||
{
|
||||
$this->logger->info("SampleController::get() called");
|
||||
return $this->view->render($response, 'index.twig.html');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
namespace Lewisdale\App\Logging;
|
||||
|
||||
use DateTime;
|
||||
use Psr\Log\AbstractLogger;
|
||||
use Stringable;
|
||||
|
||||
class FileLogger extends AbstractLogger
|
||||
{
|
||||
function __construct(private readonly string $filename = 'php://stdout') {}
|
||||
public function log($level, Stringable|string $message, array $context = []): void
|
||||
{
|
||||
$timestamp = new DateTime();
|
||||
$callingClass = debug_backtrace()[2]['class'];
|
||||
$callingFunction = debug_backtrace()[2]['function'];
|
||||
$log = $timestamp->format(DATE_ATOM) . " [$level] $callingClass::$callingFunction $message " . json_encode($context);
|
||||
fwrite(fopen($this->filename, 'w'), $log . PHP_EOL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lewisdale\App\Models\Data;
|
||||
|
||||
use Lewisdale\App\Models\Traits\AutoUpdate;
|
||||
use Lewisdale\App\Models\View;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'users')]
|
||||
#[ORM\UniqueConstraint(name: "email", columns: ["email"])]
|
||||
#[ORM\HasLifecycleCallbacks]
|
||||
class User
|
||||
{
|
||||
use AutoUpdate;
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\Column(type: 'integer')]
|
||||
#[ORM\GeneratedValue]
|
||||
private ?int $id;
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $password;
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $email;
|
||||
#[ORM\Column(type: 'string')]
|
||||
private string $salt;
|
||||
|
||||
function __construct(
|
||||
string $password,
|
||||
string $email,
|
||||
?string $salt = null,
|
||||
?int $id = null,
|
||||
) {
|
||||
$this->password = $password;
|
||||
$this->email = $email;
|
||||
$this->id = $id;
|
||||
$this->salt = $salt ?? bin2hex(random_bytes(32));
|
||||
}
|
||||
|
||||
public function hash(string $password): string {
|
||||
return password_hash($password . $this->salt, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
public function validate(string $password): bool {
|
||||
return password_verify($password . $this->salt, $this->password);
|
||||
}
|
||||
|
||||
public function updatePassword(string $password): void {
|
||||
$this->password = $this->hash($password);
|
||||
}
|
||||
|
||||
public function toViewModel() : View\User {
|
||||
return new View\User(
|
||||
$this->email,
|
||||
$this->createdAt->format(DATE_ATOM),
|
||||
$this->updatedAt->format(DATE_ATOM),
|
||||
$this->id
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Lewisdale\App\Models\Traits;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
|
||||
trait AutoUpdate
|
||||
{
|
||||
#[ORM\Column(type: 'datetime')]
|
||||
protected DateTime $createdAt;
|
||||
#[ORM\Column(type: 'datetime')]
|
||||
protected DateTime $updatedAt;
|
||||
|
||||
#[ORM\PrePersist]
|
||||
public function onPrePersist(): void {
|
||||
$this->createdAt = new DateTime('now');
|
||||
$this->updatedAt = new DateTime('now');
|
||||
}
|
||||
|
||||
#[ORM\PreUpdate]
|
||||
public function onPreUpdate(): void {
|
||||
$this->updatedAt = new DateTime('now');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Lewisdale\App\Models\View;
|
||||
|
||||
class User
|
||||
{
|
||||
public function __construct(
|
||||
public string $email,
|
||||
public string $createdAt,
|
||||
public string $updatedAt,
|
||||
public ?int $id = null,
|
||||
) {}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace Lewisdale\App\TwigExtensions;
|
||||
|
||||
use Slim\Csrf\Guard;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFunction;
|
||||
|
||||
class CsrfExtension extends AbstractExtension
|
||||
{
|
||||
protected Guard $guard;
|
||||
|
||||
public function __construct(Guard $guard)
|
||||
{
|
||||
$this->guard = $guard;
|
||||
}
|
||||
|
||||
public function getFunctions() : array
|
||||
{
|
||||
return [
|
||||
new TwigFunction('csrf', [$this, 'csrf'])
|
||||
];
|
||||
}
|
||||
|
||||
public function csrf() : string
|
||||
{
|
||||
$nameKey = $this->guard->getTokenNameKey();
|
||||
$nameValue = $this->guard->getTokenName();
|
||||
$tokenKey = $this->guard->getTokenValueKey();
|
||||
$tokenValue = $this->guard->getTokenValue();
|
||||
|
||||
return "
|
||||
<input type=\"hidden\" name=\"$nameKey\" value=\"$nameValue\">
|
||||
<input type=\"hidden\" name=\"$tokenKey\" value=\"$tokenValue\">
|
||||
";
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
use Lewisdale\App\Controllers\SampleController;
|
||||
use Slim\Views\TwigMiddleware;
|
||||
|
||||
require_once __DIR__ . "/dependencies.php";
|
||||
|
||||
global $container;
|
||||
global $app;
|
||||
|
||||
$dotenv = Dotenv\Dotenv::createImmutable([__DIR__, __DIR__ . "/.."]);
|
||||
$dotenv->load();
|
||||
|
||||
require_once __DIR__ . "/session.php";
|
||||
|
||||
$app->add(TwigMiddleware::createFromContainer($app));
|
||||
$app->add('csrf');
|
||||
|
||||
$app->get("/", [SampleController::class, 'get']);
|
||||
|
||||
$app->addErrorMiddleware(true, true, true);
|
||||
|
||||
$app->run();
|
||||
@@ -0,0 +1,56 @@
|
||||
<?php declare(strict_types=1);
|
||||
|
||||
use DI\Container;
|
||||
use Doctrine\DBAL\DriverManager;
|
||||
use Doctrine\DBAL\Types\Type;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\ORMSetup;
|
||||
use Lewisdale\App\Logging\FileLogger;
|
||||
use Lewisdale\App\TwigExtensions\CsrfExtension;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Ramsey\Uuid\Doctrine\UuidType;
|
||||
use Slim\Factory\AppFactory;
|
||||
use Slim\Views\Twig;
|
||||
|
||||
require_once __DIR__ . "/../vendor/autoload.php";
|
||||
|
||||
$container = new Container();
|
||||
|
||||
AppFactory::setContainer($container);
|
||||
$app = AppFactory::create();
|
||||
|
||||
|
||||
$container->set('csrf', function() use ($app) {
|
||||
return new Slim\Csrf\Guard($app->getResponseFactory());
|
||||
});
|
||||
|
||||
$container->set('view', function() use ($container) {
|
||||
$twig = Twig::create(__DIR__ . "/../views");
|
||||
$twig->addExtension(new CsrfExtension($container->get('csrf')));
|
||||
|
||||
return $twig;
|
||||
});
|
||||
|
||||
$container->set(Twig::class, function() use ($container) {
|
||||
return $container->get('view');
|
||||
});
|
||||
|
||||
$container->set(LoggerInterface::class, $container->get(FileLogger::class));
|
||||
|
||||
|
||||
$container->set(EntityManager::class, static function() {
|
||||
Type::addType('uuid', UuidType::class);
|
||||
|
||||
$config = ORMSetup::createAttributeMetadataConfiguration(
|
||||
paths: array(__DIR__."/Models/Data"),
|
||||
isDevMode: true,
|
||||
);
|
||||
|
||||
$connection = DriverManager::getConnection([
|
||||
'driver' => 'pdo_sqlite',
|
||||
'path' => __DIR__ . '/../' . $_ENV["SQLITE_DB_NAME"],
|
||||
], $config);
|
||||
|
||||
return new EntityManager($connection, $config);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
session_start();
|
||||
|
||||
// Protect against session hijacking
|
||||
if (!isset($_SESSION['fingerprint'])) {
|
||||
session_regenerate_id();
|
||||
$_SESSION['fingerprint'] = $_SERVER['HTTP_USER_AGENT'];
|
||||
}
|
||||
|
||||
if ($_SESSION['fingerprint'] !== $_SERVER['HTTP_USER_AGENT']) {
|
||||
session_unset();
|
||||
}
|
||||
Reference in new issue
Block a user