From ea1269577fcd9d9b05f4b29ae2ad0d7c4c9a6ca2 Mon Sep 17 00:00:00 2001 From: "David T. Sadler" Date: Thu, 8 Jul 2021 21:47:20 +0100 Subject: Shorten urls --- make_bookmarklet/index.php | 2 +- public/add/index.php | 38 ++++++++++++++++++++++++++++++++++++++ public/bookmarks/add/index.php | 38 -------------------------------------- public/bookmarks/read/index.php | 33 --------------------------------- public/read/index.php | 33 +++++++++++++++++++++++++++++++++ scripts/email_bookmarks.php | 2 +- 6 files changed, 73 insertions(+), 73 deletions(-) create mode 100644 public/add/index.php delete mode 100644 public/bookmarks/add/index.php delete mode 100644 public/bookmarks/read/index.php create mode 100644 public/read/index.php diff --git a/make_bookmarklet/index.php b/make_bookmarklet/index.php index f964003..67dea14 100644 --- a/make_bookmarklet/index.php +++ b/make_bookmarklet/index.php @@ -7,7 +7,7 @@ $config = require_once(__DIR__.'/../config.php'); $booklet = <<< EOF_JS javascript: (() => { async function bookmarkPage(page) { - const response = await fetch('{$config['site']}/bookmarks/add/', { + const response = await fetch('{$config['site']}/add/', { method: 'POST', mode: 'cors', cache: 'no-cache', diff --git a/public/add/index.php b/public/add/index.php new file mode 100644 index 0000000..12119b6 --- /dev/null +++ b/public/add/index.php @@ -0,0 +1,38 @@ + $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/add/index.php b/public/bookmarks/add/index.php deleted file mode 100644 index f387bc6..0000000 --- a/public/bookmarks/add/index.php +++ /dev/null @@ -1,38 +0,0 @@ - $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/read/index.php b/public/bookmarks/read/index.php deleted file mode 100644 index 6a4a216..0000000 --- a/public/bookmarks/read/index.php +++ /dev/null @@ -1,33 +0,0 @@ - $config['max_url_length']) { - respondAndExit(400, 'Bad Request'); -} - -$urls = file($config['path_to_file'], FILE_IGNORE_NEW_LINES); - -// 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'); -} - -if (!file_put_contents($config['path_to_file'], implode("\n", $remaingUrls))) { - respondAndExit(500, 'Internal Server Error'); -} - -respondAndExit(308, 'Permanent Redirect', ["Location: $requestedUrl"]); diff --git a/public/read/index.php b/public/read/index.php new file mode 100644 index 0000000..87857da --- /dev/null +++ b/public/read/index.php @@ -0,0 +1,33 @@ + $config['max_url_length']) { + respondAndExit(400, 'Bad Request'); +} + +$urls = file($config['path_to_file'], FILE_IGNORE_NEW_LINES); + +// 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'); +} + +if (!file_put_contents($config['path_to_file'], implode("\n", $remaingUrls))) { + respondAndExit(500, 'Internal Server Error'); +} + +respondAndExit(308, 'Permanent Redirect', ["Location: $requestedUrl"]); diff --git a/scripts/email_bookmarks.php b/scripts/email_bookmarks.php index 4b616e7..9a63d94 100644 --- a/scripts/email_bookmarks.php +++ b/scripts/email_bookmarks.php @@ -29,7 +29,7 @@ function buildMessage(array $urls, string $site): string { $urls = array_map(function ($url) use ($site) { return sprintf( - '
  • %s
  • ', + '
  • %s
  • ', $site, urlencode($url), htmlentities($url) -- cgit v1.2.3-13-gbd6f