diff --git a/public/index.php b/public/index.php index e5fd097..abf0d72 100644 --- a/public/index.php +++ b/public/index.php @@ -16,4 +16,5 @@ $router->get('/^recent\/(?\d+)$/', [$user, 'index']); $router->get('/^news\/(?\d+)$/', [$user, 'viewPost']); $router->get('/^comments\/(?[\w-]+)$/', [$user, 'viewPostComments']); $router->match('/^login$/', [$security, 'login']); -$router->run(new \Silex\DI\DI($router))->render(__DIR__ . '/../' . VIEW_PATH); +$router->match('/^register$/', [$security, 'register']); +$router->run(new \Silex\DI\DI($router))->render($router, __DIR__ . '/../' . VIEW_PATH); diff --git a/src/Silex/Controller/SecurityController.php b/src/Silex/Controller/SecurityController.php index 177008b..a2f8ba4 100644 --- a/src/Silex/Controller/SecurityController.php +++ b/src/Silex/Controller/SecurityController.php @@ -6,6 +6,7 @@ namespace Silex\Controller; use Silex\DI\DI; use Silex\Http\HttpResponse; +use Silex\Model\User; class SecurityController { @@ -19,9 +20,23 @@ class SecurityController header('Location: ' . $di->getRouter()->url('')); exit(); } - var_dump($success); $fail = !$success; } return HttpResponse::found('login', ['fail' => $fail]); } -} \ No newline at end of file + + public function register(DI $di): HttpResponse + { + $fail = false; + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $user = $di->getSecurity()->register(User::fromRawPassword($_POST['login'], $_POST['password'])); + if ($user !== null) { + http_response_code(303); + header('Location: ' . $di->getRouter()->url('')); + exit(); + } + $fail = $user === null; + } + return HttpResponse::found('register', ['fail' => $fail]); + } +} diff --git a/src/Silex/Controller/UserController.php b/src/Silex/Controller/UserController.php index 8406854..93581dd 100644 --- a/src/Silex/Controller/UserController.php +++ b/src/Silex/Controller/UserController.php @@ -28,7 +28,7 @@ class UserController } else { $nbCommentsByUser = 0; } - return new HttpResponse(200, 'home', ['news' => $news, 'page' => $page, 'nbPages' => $nbPages, 'router' => $di->getRouter(), 'nbComments' => $nbComments, 'nbCommentsByUser' => $nbCommentsByUser]); + return new HttpResponse(200, 'home', ['news' => $news, 'page' => $page, 'nbPages' => $nbPages, 'nbComments' => $nbComments, 'nbCommentsByUser' => $nbCommentsByUser]); } public function viewPost(DI $di, array $params): HttpResponse diff --git a/src/Silex/Gateway/NewsGateway.php b/src/Silex/Gateway/NewsGateway.php index eff7560..53f7a13 100644 --- a/src/Silex/Gateway/NewsGateway.php +++ b/src/Silex/Gateway/NewsGateway.php @@ -24,7 +24,7 @@ class NewsGateway */ public function getPaginatedRecentNews(int $page = 1, int $limit = 10): array { - $req = $this->pdo->prepare('SELECT title, LEFT(content, ' . self::EXCERPT_LENGTH . ') content, publication_date FROM news ORDER BY publication_date DESC LIMIT :limit OFFSET :offset;'); + $req = $this->pdo->prepare('SELECT id_news, title, LEFT(content, ' . self::EXCERPT_LENGTH . ') content, publication_date FROM news ORDER BY publication_date DESC LIMIT :limit OFFSET :offset;'); $req->bindValue('limit', $limit, PDO::PARAM_INT); $req->bindValue('offset', ($page - 1) * $limit, PDO::PARAM_INT); if (!$req->execute()) { @@ -61,6 +61,6 @@ class NewsGateway private function createNews(array $data): News { - return new News($data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date'])); + return new News(intval($data['id_news']), $data['title'], $data['content'], DateTime::createFromFormat('Y-m-d H:i:s', $data['publication_date'])); } } diff --git a/src/Silex/Gateway/UserGateway.php b/src/Silex/Gateway/UserGateway.php index 1bb3551..0467f21 100644 --- a/src/Silex/Gateway/UserGateway.php +++ b/src/Silex/Gateway/UserGateway.php @@ -33,4 +33,12 @@ class UserGateway $user = $req->fetch(); return $user === false ? null : $user; } + + public function insert(User $user): bool + { + $req = $this->pdo->prepare('INSERT INTO registered_user (login, password, role) VALUES (:login, :password, :role);'); + $req->execute(['login' => $user->getLogin(), 'password' => $user->getPasswordHash(), 'role' => $user->getRole()]); + $user->setId(intval($this->pdo->lastInsertId())); + return true; + } } diff --git a/src/Silex/Http/HttpResponse.php b/src/Silex/Http/HttpResponse.php index 0342821..113d9cf 100644 --- a/src/Silex/Http/HttpResponse.php +++ b/src/Silex/Http/HttpResponse.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace Silex\Http; +use Silex\Router\Router; + class HttpResponse { private int $status; @@ -24,7 +26,7 @@ class HttpResponse return new HttpResponse(200, $viewPath, $viewParams); } - public function render(string $viewBasePath) + public function render(Router $router, string $viewBasePath) { $params = $this->viewParams; ob_start(); diff --git a/src/Silex/Model/News.php b/src/Silex/Model/News.php index 72c8329..afc736a 100644 --- a/src/Silex/Model/News.php +++ b/src/Silex/Model/News.php @@ -8,17 +8,24 @@ use DateTime; class News { + private int $id; private string $title; private string $content; private DateTime $publicationDate; - public function __construct(string $title, string $content, DateTime $publicationDate) + public function __construct(int $id, string $title, string $content, DateTime $publicationDate) { + $this->id = $id; $this->title = $title; $this->content = $content; $this->publicationDate = $publicationDate; } + public function getId(): int + { + return $this->id; + } + public function getTitle(): string { return $this->title; diff --git a/src/Silex/Model/User.php b/src/Silex/Model/User.php index c89f60a..a5d1d58 100644 --- a/src/Silex/Model/User.php +++ b/src/Silex/Model/User.php @@ -11,11 +11,12 @@ class User private string $password; private int $role; - public static function fromRawPassword(string $login, string $password): User + public static function fromRawPassword(string $login, string $password, int $role = 0): User { $user = new User(); $user->login = $login; $user->password = password_hash($password, PASSWORD_DEFAULT); + $user->role = $role; return $user; } @@ -38,4 +39,9 @@ class User { return $this->role; } + + public function setId(int $id) + { + $this->id_user = $id; + } } diff --git a/src/Silex/Router/Router.php b/src/Silex/Router/Router.php index 3a111b4..602f0ae 100644 --- a/src/Silex/Router/Router.php +++ b/src/Silex/Router/Router.php @@ -56,7 +56,12 @@ class Router public function url(string $url): string { - return $this->basePath . '/' . $url; + if ($this->basePath !== '') { + return "/" . $this->basePath . '/' . $url; + } else { + return $this->basePath . '/' . $url; + } + } public function run(DI $di): HttpResponse diff --git a/src/Silex/Security/Security.php b/src/Silex/Security/Security.php index eabca7c..fcf44cf 100644 --- a/src/Silex/Security/Security.php +++ b/src/Silex/Security/Security.php @@ -45,4 +45,14 @@ class Security } return $this->user; } + + public function register(User $user): ?User + { + if (!$this->userGateway->insert($user)) { + return null; + } + $this->session[USER] = $user->getId(); + $this->user = $user; + return $user; + } } diff --git a/views/home.php b/views/home.php index aebf437..0f6ade4 100644 --- a/views/home.php +++ b/views/home.php @@ -7,30 +7,32 @@

Hello world!

-
-
-

- getTitle() ?> -

-
-
- +