diff options
| -rw-r--r-- | public/add/index.php | 33 | ||||
| -rw-r--r-- | public/create/index.php | 23 | ||||
| -rw-r--r-- | public/delete/confirm/index.php | 31 | ||||
| -rw-r--r-- | public/delete/index.php | 20 | ||||
| -rw-r--r-- | public/edit/index.php | 16 | ||||
| -rw-r--r-- | public/store/index.php | 33 | ||||
| -rw-r--r-- | public/update/index.php | 35 | ||||
| -rw-r--r-- | src/DTS/Bookmark.php | 10 | ||||
| -rw-r--r-- | src/DTS/BookmarkRepository.php | 18 | ||||
| -rw-r--r-- | src/DTS/Functions.php | 5 | ||||
| -rw-r--r-- | src/templates/confirm_deletion.php (renamed from src/templates/delete.php) | 3 | ||||
| -rw-r--r-- | src/templates/create.php (renamed from src/templates/add.php) | 5 | ||||
| -rw-r--r-- | src/templates/edit.php | 5 | ||||
| -rw-r--r-- | src/templates/index.php | 11 |
14 files changed, 171 insertions, 77 deletions
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 @@ -<?php - -declare(strict_types=1); - -use DTS\Bookmark; -use DTS\BookmarkRepository; -use DTS\Template; -use function DTS\Functions\respondAndExit; - -require_once(__DIR__.'/../../autoload.php'); - -$config = require_once(__DIR__.'/../../config.php'); - -$bookmarks = new BookmarkRepository($config['path_to_repository']); - -$template = new Template($config['path_to_templates']); - -if ('POST' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { - $bookmark = new Bookmark( - bin2hex(random_bytes(32)), - $_POST['url'], - $_POST['title'], - $_POST['tag'], - date('Y-m-d H:i:s'), - true, - ); - - $bookmarks->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 @@ +<?php + +declare(strict_types=1); + +use DTS\Bookmark; +use DTS\Template; +use function DTS\Functions\respondAndExit; + +require_once(__DIR__.'/../../autoload.php'); + +$config = require_once(__DIR__.'/../../config.php'); + +if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'GET') { + respondAndExit(405, 'Method Not Allowed'); +} + +$template = new Template($config['path_to_templates']); + +$bookmark = new Bookmark(); + +$html = $template->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 @@ +<?php + +declare(strict_types=1); + +use DTS\BookmarkRepository; +use DTS\Template; +use function DTS\Functions\respondAndExit; + +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']); + +$bookmark = $bookmarks->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 @@ +<?php + +declare(strict_types=1); + +use DTS\Bookmark; +use DTS\BookmarkRepository; +use function DTS\Functions\respondAndExit; +use function DTS\Functions\redirectAndExit; + +require_once(__DIR__.'/../../autoload.php'); + +$config = require_once(__DIR__.'/../../config.php'); + +if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') { + respondAndExit(405, 'Method Not Allowed'); +} + +$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->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 @@ +<?php + +declare(strict_types=1); + +use DTS\BookmarkRepository; +use function DTS\Functions\respondAndExit; +use function DTS\Functions\redirectAndExit; + +require_once(__DIR__.'/../../autoload.php'); + +$config = require_once(__DIR__.'/../../config.php'); + +if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') !== 'POST') { + respondAndExit(405, 'Method Not Allowed'); +} + +$id = filter_input(INPUT_POST, 'id'); + +$bookmarks = new BookmarkRepository($config['path_to_repository']); + +$bookmark = $bookmarks->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('/'); diff --git a/src/DTS/Bookmark.php b/src/DTS/Bookmark.php index 560fad4..543a552 100644 --- a/src/DTS/Bookmark.php +++ b/src/DTS/Bookmark.php @@ -17,14 +17,4 @@ class Bookmark public string $addedAt; public bool $unread; - - function __construct(string $id, string $url, string $title, string $tag, string $addedAt, bool $unread) - { - $this->id = $id; - $this->url = $url; - $this->title = $title; - $this->tag = $tag; - $this->addedAt = $addedAt; - $this->unread = $unread; - } } diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php index 5d22100..a3fd5dd 100644 --- a/src/DTS/BookmarkRepository.php +++ b/src/DTS/BookmarkRepository.php @@ -128,14 +128,16 @@ class BookmarkRepository implements \Iterator } while (($data = fgetcsv($fp)) !== FALSE) { - $this->repository[] = new Bookmark( - $data[0], // Id. - $data[1], // Url. - $data[2], // Title. - $data[3], // Tag. - $data[4], // Added At. - (bool)$data[4], // Read. - ); + $bookmark = new Bookmark(); + + $bookmark->id = $data[0]; + $bookmark->url = $data[1]; + $bookmark->title = $data[2]; + $bookmark->tag = $data[3]; + $bookmark->addedAt = $data[4]; + $bookmark->unread = (bool)$data[5]; + + $this->repository[] = $bookmark; } fclose($fp); diff --git a/src/DTS/Functions.php b/src/DTS/Functions.php index d7ca835..2101cda 100644 --- a/src/DTS/Functions.php +++ b/src/DTS/Functions.php @@ -16,3 +16,8 @@ function respondAndExit(int $responseCode, string $header, string $body = '', ar exit(); } + +function redirectAndExit(string $location): void +{ + respondAndExit(302, 'Found', '', ["Location: $location"]); +} diff --git a/src/templates/delete.php b/src/templates/confirm_deletion.php index 1be762b..77998ff 100644 --- a/src/templates/delete.php +++ b/src/templates/confirm_deletion.php @@ -6,10 +6,11 @@ <title>Bookmarks</title> </head> <body> + <a href="/">Back</a> <form action="/delete" method="POST"> <input type="hidden" name="id" value="<?php echo $bookmark->id; ?>"/> <?php echo $bookmark->url.' '.$bookmark->title.' '.$bookmark->tag; ?> - <button type="submit">Delete</button> + <button type="submit">Delete</button> </form> </body> </html> diff --git a/src/templates/add.php b/src/templates/create.php index a44c4d3..483e49e 100644 --- a/src/templates/add.php +++ b/src/templates/create.php @@ -6,11 +6,12 @@ <title>Bookmarks</title> </head> <body> - <form action="/add" method="POST"> + <a href="/">Back</a> + <form action="/store" method="POST"> <input type="text" name="url" maxlength="512" autofocus><br> <input type="text" name="title" maxlength="256" ><br> <input type="text" name="tag" maxlength="8" ><br> - <button type="submit">Add</button> + <button type="submit">Add</button> </form> </body> </html> diff --git a/src/templates/edit.php b/src/templates/edit.php index a3c6930..d5f5a08 100644 --- a/src/templates/edit.php +++ b/src/templates/edit.php @@ -6,12 +6,13 @@ <title>Bookmarks</title> </head> <body> - <form action="/edit" method="POST"> + <a href="/">Back</a> + <form action="/update" method="POST"> <input type="hidden" name="id" value="<?php echo $bookmark->id; ?>"/> <input type="text" name="url" maxlength="512" autofocus value="<?php echo $bookmark->url; ?>"><br> <input type="text" name="title" maxlength="256" value="<?php echo $bookmark->title; ?>"><br> <input type="text" name="tag" maxlength="8" value="<?php echo $bookmark->tag; ?>"><br> - <button type="submit">Update</button> + <button type="submit">Update</button> </form> </body> </html> diff --git a/src/templates/index.php b/src/templates/index.php index 1298985..ed84fd4 100644 --- a/src/templates/index.php +++ b/src/templates/index.php @@ -6,10 +6,11 @@ <title>Bookmarks</title> </head> <body> - <ul> - <?php foreach ($bookmarks as $bookmark) { ?> - <li><a href="/edit?id=<?php echo $bookmark->id; ?>"><?php echo $bookmark->title; ?></a> | <a href="/delete?id=<?php echo $bookmark->id; ?>">Delete</a></li> - <?php } ?> - </ul> + <a href="/create">Add</a> + <ul> + <?php foreach ($bookmarks as $bookmark) { ?> + <li><a href="/edit?id=<?php echo $bookmark->id; ?>"><?php echo $bookmark->title; ?></a> | <a href="/delete/confirm?id=<?php echo $bookmark->id; ?>">Delete</a></li> + <?php } ?> + </ul> </body> </html> |
