From cbaedbc5251f3b127bd81242d1344c0cd3e56e0c Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Wed, 13 Oct 2021 21:51:07 +0100 Subject: Implemente redirects --- public/add/index.php | 33 --------------------------------- public/create/index.php | 23 +++++++++++++++++++++++ public/delete/confirm/index.php | 31 +++++++++++++++++++++++++++++++ public/delete/index.php | 20 ++++++++++++-------- public/edit/index.php | 16 ++++++++-------- public/store/index.php | 33 +++++++++++++++++++++++++++++++++ public/update/index.php | 35 +++++++++++++++++++++++++++++++++++ 7 files changed, 142 insertions(+), 49 deletions(-) delete mode 100644 public/add/index.php create mode 100644 public/create/index.php create mode 100644 public/delete/confirm/index.php create mode 100644 public/store/index.php create mode 100644 public/update/index.php (limited to 'public') diff --git a/public/add/index.php b/public/add/index.php deleted file mode 100644 index 11ce9bb..0000000 --- a/public/add/index.php +++ /dev/null @@ -1,33 +0,0 @@ -add($bookmark); -} - -$html = $template->render('add'); - -respondAndExit(200, 'OK', $html); diff --git a/public/create/index.php b/public/create/index.php new file mode 100644 index 0000000..00ce794 --- /dev/null +++ b/public/create/index.php @@ -0,0 +1,23 @@ +render('create', compact('bookmark')); + +respondAndExit(200, 'OK', $html); diff --git a/public/delete/confirm/index.php b/public/delete/confirm/index.php new file mode 100644 index 0000000..6636fe9 --- /dev/null +++ b/public/delete/confirm/index.php @@ -0,0 +1,31 @@ +find($id); + +if ($bookmark === null) { + respondAndExit(404, 'Not Found'); +} + +$html = $template->render('confirm_deletion', compact('bookmark')); + +respondAndExit(200, 'OK', $html); diff --git a/public/delete/index.php b/public/delete/index.php index 1b2484d..e07129a 100644 --- a/public/delete/index.php +++ b/public/delete/index.php @@ -3,25 +3,29 @@ declare(strict_types=1); use DTS\BookmarkRepository; -use DTS\Template; use function DTS\Functions\respondAndExit; +use function DTS\Functions\redirectAndExit; require_once(__DIR__.'/../../autoload.php'); $config = require_once(__DIR__.'/../../config.php'); -$bookmarks = new BookmarkRepository($config['path_to_repository']); +if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') { + respondAndExit(405, 'Method Not Allowed'); +} -$template = new Template($config['path_to_templates']); +$id = filter_input(INPUT_POST, 'id'); -$id = filter_input(INPUT_GET, 'id') ?? filter_input(INPUT_POST, 'id') ?? null; +$bookmarks = new BookmarkRepository($config['path_to_repository']); $bookmark = $bookmarks->find($id); -if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { - $bookmarks->delete($bookmark); +if ($bookmark === null) { + respondAndExit(404, 'Not Found'); } -$html = $template->render('delete', compact('bookmark')); +if (!$bookmarks->delete($bookmark)) { + respondAndExit(500, 'Internal Server Error'); +} -respondAndExit(200, 'OK', $html); +redirectAndExit('/'); diff --git a/public/edit/index.php b/public/edit/index.php index 65b3fa5..9d3d4c0 100644 --- a/public/edit/index.php +++ b/public/edit/index.php @@ -10,20 +10,20 @@ require_once(__DIR__.'/../../autoload.php'); $config = require_once(__DIR__.'/../../config.php'); +if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'GET') { + respondAndExit(405, 'Method Not Allowed'); +} + +$id = filter_input(INPUT_GET, 'id'); + $bookmarks = new BookmarkRepository($config['path_to_repository']); $template = new Template($config['path_to_templates']); -$id = filter_input(INPUT_GET, 'id') ?? filter_input(INPUT_POST, 'id') ?? null; - $bookmark = $bookmarks->find($id); -if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { - $bookmark->url = $_POST['url']; - $bookmark->title = $_POST['title']; - $bookmark->tag = $_POST['tag']; - - $bookmarks->update($bookmark); +if ($bookmark === null) { + respondAndExit(404, 'Not Found'); } $html = $template->render('edit', compact('bookmark')); diff --git a/public/store/index.php b/public/store/index.php new file mode 100644 index 0000000..2a84c59 --- /dev/null +++ b/public/store/index.php @@ -0,0 +1,33 @@ +id = bin2hex(random_bytes(32)); +$bookmark->url = $_POST['url']; +$bookmark->title = $_POST['title']; +$bookmark->tag = $_POST['tag']; +$bookmark->addedAt = date('Y-m-d H:i:s'); +$bookmark->unread = true; + +if (!$bookmarks->add($bookmark)) { + respondAndExit(500, 'Internal Server Error'); +} + +redirectAndExit('/'); diff --git a/public/update/index.php b/public/update/index.php new file mode 100644 index 0000000..fa6b482 --- /dev/null +++ b/public/update/index.php @@ -0,0 +1,35 @@ +find($id); + +if ($bookmark === null) { + respondAndExit(404, 'Not Found'); +} + +$bookmark->url = $_POST['url']; +$bookmark->title = $_POST['title']; +$bookmark->tag = $_POST['tag']; + +if (!$bookmarks->update($bookmark)) { + respondAndExit(500, 'Internal Server Error'); +} + +redirectAndExit('/'); -- cgit v1.2.3-13-gbd6f