summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--includes/functions.php14
-rw-r--r--public/bookmarks/add/index.php31
-rw-r--r--public/bookmarks/index.php47
-rw-r--r--public/bookmarks/read/index.php28
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');
+