diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-07 21:31:17 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-10-07 21:31:17 +0100 |
| commit | 0f273c0a8ead390346571c4be2296fa6e384eff6 (patch) | |
| tree | 292fc180a277a5324df73c5c8b02c8c673e812a3 | |
| parent | 34ba17ceeecd3ac6be9e31be45ee76757da2dec0 (diff) | |
Add bookmarks
| -rw-r--r-- | public/add/index.php | 49 | ||||
| -rw-r--r-- | public/index.php | 1 | ||||
| -rw-r--r-- | src/DTS/Bookmark.php | 9 | ||||
| -rw-r--r-- | src/DTS/BookmarkRepository.php | 25 | ||||
| -rw-r--r-- | src/DTS/Functions.php | 2 | ||||
| -rw-r--r-- | src/templates/add.php | 16 | ||||
| -rw-r--r-- | src/templates/index.php | 1 |
7 files changed, 68 insertions, 35 deletions
diff --git a/public/add/index.php b/public/add/index.php index 12119b6..11ce9bb 100644 --- a/public/add/index.php +++ b/public/add/index.php @@ -1,38 +1,33 @@ -<?php declare(strict_types=1); +<?php -error_reporting(E_ALL); +declare(strict_types=1); -require_once(__DIR__.'/../../includes/functions.php'); +use DTS\Bookmark; +use DTS\BookmarkRepository; +use DTS\Template; +use function DTS\Functions\respondAndExit; -$config = require_once(__DIR__.'/../../config.php'); - -if ('OPTIONS' === filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { - respondAndExit(200, 'OK', [ - 'Access-Control-Allow-Methods: POST', - 'Access-Control-Allow-Headers: Content-Type, Authorization', - ]); -} +require_once(__DIR__.'/../../autoload.php'); -if ('POST' !== filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { - respondAndExit(405, 'Method Not Allowed'); -} +$config = require_once(__DIR__.'/../../config.php'); -if ('Bearer '.$config['bearer_token'] !== filter_input(INPUT_SERVER, 'HTTP_AUTHORIZATION')) { - respondAndExit(401, 'Unauthorized', ['WWW-Authenticate: Bearer realm="Bookmarks"']); -} +$bookmarks = new BookmarkRepository($config['path_to_repository']); -if ('application/x-www-form-urlencoded' !== filter_input(INPUT_SERVER, 'CONTENT_TYPE')) { - respondAndExit(415, 'Unsupported Media Type'); -} +$template = new Template($config['path_to_templates']); -$url = filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL); +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, + ); -if (!$url || strlen($url) > $config['max_url_length']) { - respondAndExit(400, 'Bad Request'); + $bookmarks->add($bookmark); } -if (!file_put_contents($config['path_to_file'], "$url\n", FILE_APPEND)) { - respondAndExit(500, 'Internal Server Error'); -} +$html = $template->render('add'); -respondAndExit(201, 'Created'); +respondAndExit(200, 'OK', $html); diff --git a/public/index.php b/public/index.php index 9b4ac6e..2a9ccb7 100644 --- a/public/index.php +++ b/public/index.php @@ -18,7 +18,6 @@ $sort = $_GET['sort'] ?? null; $tag = $_GET['tag'] ?? null; - $bookmarks->load(); if ($tag !== null) { diff --git a/src/DTS/Bookmark.php b/src/DTS/Bookmark.php index 93c7a29..560fad4 100644 --- a/src/DTS/Bookmark.php +++ b/src/DTS/Bookmark.php @@ -10,18 +10,21 @@ class Bookmark public string $url; + public string $title; + public string $tag; public string $addedAt; - public bool $read; + public bool $unread; - function __construct(string $id, string $url, string $tag, string $addedAt, bool $read) + 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->read = $read; + $this->unread = $unread; } } diff --git a/src/DTS/BookmarkRepository.php b/src/DTS/BookmarkRepository.php index deb188c..ab54594 100644 --- a/src/DTS/BookmarkRepository.php +++ b/src/DTS/BookmarkRepository.php @@ -31,8 +31,9 @@ class BookmarkRepository implements \Iterator $this->repository[] = new Bookmark( $data[0], // Id. $data[1], // Url. - $data[2], // Tag. - $data[3], // Added At. + $data[2], // Title. + $data[3], // Tag. + $data[4], // Added At. (bool)$data[4], // Read. ); } @@ -60,6 +61,26 @@ class BookmarkRepository implements \Iterator return $this; } + public function add(Bookmark $bookmark): bool + { + if (($fp = fopen($this->pathToRepository, 'a')) === FALSE) { + throw new \Exception("Unable to open repository {$this->pathToRepository}"); + } + + $saved = fputcsv($fp, [ + $bookmark->id, + $bookmark->url, + $bookmark->title, + $bookmark->tag, + $bookmark->addedAt, + (int)$bookmark->unread, + ]); + + fclose($fp); + + return $saved !== false; + } + public function current(): mixed { return $this->repository[$this->position]; diff --git a/src/DTS/Functions.php b/src/DTS/Functions.php index b7f9cb3..d7ca835 100644 --- a/src/DTS/Functions.php +++ b/src/DTS/Functions.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace DTS\Functions; -function respondAndExit(int $responseCode, string $header, string $body, array $headers = []): void +function respondAndExit(int $responseCode, string $header, string $body = '', array $headers = []): void { header($header, false, $responseCode); diff --git a/src/templates/add.php b/src/templates/add.php new file mode 100644 index 0000000..a44c4d3 --- /dev/null +++ b/src/templates/add.php @@ -0,0 +1,16 @@ +<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>Bookmarks</title> + </head> + <body> + <form action="/add" 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> + </form> + </body> +</html> diff --git a/src/templates/index.php b/src/templates/index.php index 034f09c..9656551 100644 --- a/src/templates/index.php +++ b/src/templates/index.php @@ -6,7 +6,6 @@ <title>Bookmarks</title> </head> <body> - <h1>Hello <?php echo 'World'; ?></h1> <ul> <?php foreach ($bookmarks as $bookmark) { ?> <li><?php echo "[$bookmark->id] $bookmark->url"; ?></li> |
