diff options
| -rw-r--r-- | public/bookmarks/add/index.php | 1 | ||||
| -rw-r--r-- | public/bookmarks/read/index.php | 15 |
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"]); |
