summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid T. Sadler <davidtsadler@googlemail.com>2021-07-07 16:58:27 +0100
committerDavid T. Sadler <davidtsadler@googlemail.com>2021-07-07 16:58:27 +0100
commit3e2f64ef6f3ea5fff69a0621f42f41bdbbbf7e51 (patch)
treef5cd4e15f6724692859139848770080850991d1f
parent7b21a6654ef973639ec1fcd7958403448c1caa87 (diff)
Remove url from list when read
-rw-r--r--public/bookmarks/add/index.php1
-rw-r--r--public/bookmarks/read/index.php15
2 files changed, 10 insertions, 6 deletions
diff --git a/public/bookmarks/add/index.php b/public/bookmarks/add/index.php
index f8bd020..18db75f 100644
--- a/public/bookmarks/add/index.php
+++ b/public/bookmarks/add/index.php
@@ -28,4 +28,3 @@ if (!file_put_contents($config['path_to_file'], "$url\n", FILE_APPEND)) {
}
respondAndExit(201, 'Created');
-
diff --git a/public/bookmarks/read/index.php b/public/bookmarks/read/index.php
index a21dfac..e7af97f 100644
--- a/public/bookmarks/read/index.php
+++ b/public/bookmarks/read/index.php
@@ -18,11 +18,16 @@ if (!$requestedUrl || strlen($requestedUrl) > $config['max_url_length']) {
$urls = file($config['path_to_file'], FILE_IGNORE_NEW_LINES);
-foreach ($urls as $url) {
- if ($requestedUrl == $url) {
- respondAndExit(308, 'Permanent Redirect', ["Location: $url"]);
- }
+// Remove requested url from list if present.
+$remaingUrls = array_diff($urls, [$requestedUrl]);
+
+// Count will not have changed if requested url doesn't exist in the list.
+if (count($remaingUrls) === count($urls)) {
+ respondAndExit(404, 'Not Found');
}
-respondAndExit(404, 'Not Found');
+if (!file_put_contents($config['path_to_file'], implode("\n", $remaingUrls))) {
+ respondAndExit(500, 'Internal Server Error');
+}
+respondAndExit(308, 'Permanent Redirect', ["Location: $requestedUrl"]);