diff options
| author | David T. Sadler <davidtsadler@googlemail.com> | 2021-07-07 14:46:40 +0100 |
|---|---|---|
| committer | David T. Sadler <davidtsadler@googlemail.com> | 2021-07-07 14:46:40 +0100 |
| commit | 0a3a8ca0ba903aa12face3c4b7273d4da288172a (patch) | |
| tree | 9d7e209844732e5a2579f3af4c71d81af3bf58cc | |
| parent | 576160b4d38d28fc5aa70fa5c03d38712dc8ac17 (diff) | |
Refactor existing API and implement reading or urls
| -rw-r--r-- | includes/functions.php | 14 | ||||
| -rw-r--r-- | public/bookmarks/add/index.php | 31 | ||||
| -rw-r--r-- | public/bookmarks/index.php | 47 | ||||
| -rw-r--r-- | public/bookmarks/read/index.php | 28 |
4 files changed, 73 insertions, 47 deletions
diff --git a/includes/functions.php b/includes/functions.php new file mode 100644 index 0000000..b5208e6 --- /dev/null +++ b/includes/functions.php @@ -0,0 +1,14 @@ +<?php declare(strict_types=1); + +function respondAndExit(int $responseCode, string $header, array $headers = []): void +{ + header($header, false, $responseCode); + + foreach ($headers as $header) { + header($header); + } + + header('Content-type: text/plain; charset=UTF-8'); + + exit(); +} diff --git a/public/bookmarks/add/index.php b/public/bookmarks/add/index.php new file mode 100644 index 0000000..f8bd020 --- /dev/null +++ b/public/bookmarks/add/index.php @@ -0,0 +1,31 @@ +<?php declare(strict_types=1); + +error_reporting(E_ALL); + +require_once(__DIR__.'/../../../includes/functions.php'); + +$config = require_once(__DIR__.'/../../../config.php'); + +if ('POST' !== filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { + respondAndExit(405, 'Method Not Allowed'); +} +if ('Bearer '.$config['bearer_token'] !== filter_input(INPUT_SERVER, 'HTTP_AUTHORIZATION')) { + respondAndExit(401, 'Unauthorized', ['WWW-Authenticate: Bearer realm="Bookmarks"']); +} + +if ('application/x-www-form-urlencoded' !== filter_input(INPUT_SERVER, 'CONTENT_TYPE')) { + respondAndExit(415, 'Unsupported Media Type'); +} + +$url = filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL); + +if (!$url || strlen($url) > $config['max_url_length']) { + respondAndExit(400, 'Bad Request'); +} + +if (!file_put_contents($config['path_to_file'], "$url\n", FILE_APPEND)) { + respondAndExit(500, 'Internal Server Error'); +} + +respondAndExit(201, 'Created'); + diff --git a/public/bookmarks/index.php b/public/bookmarks/index.php deleted file mode 100644 index 8ecfe24..0000000 --- a/public/bookmarks/index.php +++ /dev/null @@ -1,47 +0,0 @@ -<?php declare(strict_types=1); - -error_reporting(E_ALL); - -$config = require_once('../../config.php'); - -if ('POST' !== ($_SERVER['REQUEST_METHOD'] ?? null)) { - respondAndExit([405 => 'Method Not Allowed']); -} - -if ('Bearer '.$config['bearer_token'] !== ($_SERVER['HTTP_AUTHORIZATION'] ?? null)) { - respondAndExit([ - 401 => 'Unauthorized', - 0 => 'WWW-Authenticate: Bearer realm="Bookmarks"', - ]); -} - -if ('application/x-www-form-urlencoded' !== ($_SERVER['CONTENT_TYPE'] ?? null)) { - respondAndExit([415 => 'Unsupported Media Type']); -} - -$url = filter_input(INPUT_POST, 'url', FILTER_VALIDATE_URL); - -if (!$url || !strlen($url) === 0 || strlen($url) > $config['max_url_length']) { - respondAndExit([400 => 'Bad Request']); -} - -if (!file_put_contents($config['path_to_file'], "$url\n", FILE_APPEND)) { - respondAndExit([500 => 'Internal Server Error']); -} - -respondAndExit([201 => 'Created']); - -function respondAndExit(array $headers): void -{ - foreach ($headers as $responseCode => $header) { - if ($responseCode) { - header($header, false, $responseCode); - } else { - header($header); - } - } - - header('Content-type: text/plain; charset=UTF-8'); - - exit(); -} diff --git a/public/bookmarks/read/index.php b/public/bookmarks/read/index.php new file mode 100644 index 0000000..a21dfac --- /dev/null +++ b/public/bookmarks/read/index.php @@ -0,0 +1,28 @@ +<?php declare(strict_types=1); + +error_reporting(E_ALL); + +require_once('../../../includes/functions.php'); + +$config = require_once('../../../config.php'); + +if ('GET' !== filter_input(INPUT_SERVER, 'REQUEST_METHOD')) { + respondAndExit(405, 'Method Not Allowed'); +} + +$requestedUrl = filter_input(INPUT_GET, 'url'); + +if (!$requestedUrl || strlen($requestedUrl) > $config['max_url_length']) { + respondAndExit(400, 'Bad Request'); +} + +$urls = file($config['path_to_file'], FILE_IGNORE_NEW_LINES); + +foreach ($urls as $url) { + if ($requestedUrl == $url) { + respondAndExit(308, 'Permanent Redirect', ["Location: $url"]); + } +} + +respondAndExit(404, 'Not Found'); + |
