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 /public | |
| parent | 34ba17ceeecd3ac6be9e31be45ee76757da2dec0 (diff) | |
Add bookmarks
Diffstat (limited to 'public')
| -rw-r--r-- | public/add/index.php | 49 | ||||
| -rw-r--r-- | public/index.php | 1 |
2 files changed, 22 insertions, 28 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) { |
