summaryrefslogtreecommitdiff
path: root/public
diff options
context:
space:
mode:
authorDavid T. Sadler <davidtsadler@googlemail.com>2021-10-20 20:59:55 +0100
committerDavid T. Sadler <davidtsadler@googlemail.com>2021-10-20 20:59:55 +0100
commitd4122f116c937e0ec509d8cefe540146ec27a0cd (patch)
tree481f90c4d774d32c89e612594158fe4a2d7328a4 /public
parent1a112ab14becaf6d41cd34c176cbe563d4ca9742 (diff)
Validate fields
Diffstat (limited to 'public')
-rw-r--r--public/create/index.php12
-rw-r--r--public/delete/confirm/index.php1
-rw-r--r--public/delete/index.php1
-rw-r--r--public/edit/index.php12
-rw-r--r--public/index.php1
-rw-r--r--public/store/index.php18
-rw-r--r--public/update/index.php18
7 files changed, 55 insertions, 8 deletions
diff --git a/public/create/index.php b/public/create/index.php
index 00ce794..786b8f2 100644
--- a/public/create/index.php
+++ b/public/create/index.php
@@ -3,21 +3,31 @@
declare(strict_types=1);
use DTS\Bookmark;
+use DTS\Errors;
+use DTS\Session;
use DTS\Template;
+
use function DTS\Functions\respondAndExit;
require_once(__DIR__.'/../../autoload.php');
$config = require_once(__DIR__.'/../../config.php');
+$session = Session::getInstance();
+
if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'GET') {
respondAndExit(405, 'Method Not Allowed');
}
+$errors = $session->get('errors', new Errors());
+
$template = new Template($config['path_to_templates']);
$bookmark = new Bookmark();
-$html = $template->render('create', compact('bookmark'));
+$html = $template->render('create', compact(
+ 'bookmark',
+ 'errors'
+));
respondAndExit(200, 'OK', $html);
diff --git a/public/delete/confirm/index.php b/public/delete/confirm/index.php
index 6636fe9..46c70f3 100644
--- a/public/delete/confirm/index.php
+++ b/public/delete/confirm/index.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
use DTS\BookmarkRepository;
use DTS\Template;
+
use function DTS\Functions\respondAndExit;
require_once(__DIR__.'/../../../autoload.php');
diff --git a/public/delete/index.php b/public/delete/index.php
index 9864c9a..fc677f3 100644
--- a/public/delete/index.php
+++ b/public/delete/index.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
use DTS\BookmarkRepository;
use DTS\Session;
+
use function DTS\Functions\respondAndExit;
use function DTS\Functions\redirectAndExit;
diff --git a/public/edit/index.php b/public/edit/index.php
index 9d3d4c0..fba5a90 100644
--- a/public/edit/index.php
+++ b/public/edit/index.php
@@ -3,17 +3,24 @@
declare(strict_types=1);
use DTS\BookmarkRepository;
+use DTS\Errors;
+use DTS\Session;
use DTS\Template;
+
use function DTS\Functions\respondAndExit;
require_once(__DIR__.'/../../autoload.php');
$config = require_once(__DIR__.'/../../config.php');
+$session = Session::getInstance();
+
if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'GET') {
respondAndExit(405, 'Method Not Allowed');
}
+$errors = $session->get('errors', new Errors());
+
$id = filter_input(INPUT_GET, 'id');
$bookmarks = new BookmarkRepository($config['path_to_repository']);
@@ -26,6 +33,9 @@ if ($bookmark === null) {
respondAndExit(404, 'Not Found');
}
-$html = $template->render('edit', compact('bookmark'));
+$html = $template->render('edit', compact(
+ 'bookmark',
+ 'errors'
+));
respondAndExit(200, 'OK', $html);
diff --git a/public/index.php b/public/index.php
index 025c9ec..248a3ec 100644
--- a/public/index.php
+++ b/public/index.php
@@ -5,6 +5,7 @@ declare(strict_types=1);
use DTS\BookmarkRepository;
use DTS\Session;
use DTS\Template;
+
use function DTS\Functions\respondAndExit;
require_once(__DIR__.'/../autoload.php');
diff --git a/public/store/index.php b/public/store/index.php
index 45765d5..5279e69 100644
--- a/public/store/index.php
+++ b/public/store/index.php
@@ -5,6 +5,8 @@ declare(strict_types=1);
use DTS\Bookmark;
use DTS\BookmarkRepository;
use DTS\Session;
+use DTS\Validator;
+
use function DTS\Functions\redirectAndExit;
use function DTS\Functions\respondAndExit;
@@ -18,14 +20,24 @@ if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') {
respondAndExit(405, 'Method Not Allowed');
}
+$validator = new Validator($_REQUEST);
+
+if ($validator->errors->count()) {
+ $session->set('errors', $validator->errors);
+
+ redirectAndExit('/create');
+}
+
+$validated = $validator->validated;
+
$bookmarks = new BookmarkRepository($config['path_to_repository']);
$bookmark = new Bookmark();
$bookmark->id = bin2hex(random_bytes(32));
-$bookmark->url = $_POST['url'];
-$bookmark->title = $_POST['title'];
-$bookmark->tag = $_POST['tag'];
+$bookmark->url = $validated->url;
+$bookmark->title = $validated->title;
+$bookmark->tag = $validated->tag;
$bookmark->addedAt = date('Y-m-d H:i:s');
$bookmark->unread = true;
diff --git a/public/update/index.php b/public/update/index.php
index 50d0dcf..d194dbd 100644
--- a/public/update/index.php
+++ b/public/update/index.php
@@ -4,6 +4,8 @@ declare(strict_types=1);
use DTS\BookmarkRepository;
use DTS\Session;
+use DTS\Validator;
+
use function DTS\Functions\respondAndExit;
use function DTS\Functions\redirectAndExit;
@@ -27,9 +29,19 @@ if ($bookmark === null) {
respondAndExit(404, 'Not Found');
}
-$bookmark->url = $_POST['url'];
-$bookmark->title = $_POST['title'];
-$bookmark->tag = $_POST['tag'];
+$validator = new Validator($_REQUEST);
+
+if ($validator->errors->count()) {
+ $session->set('errors', $validator->errors);
+
+ redirectAndExit("/edit?id=$bookmark->id");
+}
+
+$validated = $validator->validated;
+
+$bookmark->url = $validated->url;
+$bookmark->title = $validated->title;
+$bookmark->tag = $validated->tag;
if (!$bookmarks->update($bookmark)) {
respondAndExit(500, 'Internal Server Error');