From edf446fae669f3370609f5a955c3d6c863a93524 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Sun, 26 Jan 2025 22:00:21 +0200 Subject: feat: Replace sessionStorage with (custom) cookieStorage --- controllers/archive.php | 6 ++++-- controllers/list.php | 3 ++- controllers/login.php | 4 ++-- controllers/meta.php | 20 +++++++++++++++----- views/archive/index.php | 10 ---------- views/authenticate.js | 23 ++++++++++++++++++++++- views/global/router.php | 2 ++ views/login/index.php | 2 +- views/logout/index.php | 8 +++----- views/newlist/index.php | 7 +------ views/profile/authenticate.php | 3 +-- 11 files changed, 53 insertions(+), 35 deletions(-) diff --git a/controllers/archive.php b/controllers/archive.php index dbce6c3..0941d12 100644 --- a/controllers/archive.php +++ b/controllers/archive.php @@ -5,13 +5,15 @@ use DOMDocument; use Exception; function on_post() { + global $TOKEN; + $WEBSITE_CATEGORY = 'page_url'; $DOWNLOADS_FOLDER = getenv('ARCHIVES_DIR'); $website_url = $_POST[$WEBSITE_CATEGORY]; $uid = 1; - if (array_key_exists('token', $_POST) && strlen($_POST['token']) === 36) { + if ($TOKEN !== "") { try { - $uid = Database\Cookie::fromDB($_POST['token'])->UID; + $uid = Database\Cookie::fromDB($TOKEN)->UID; } catch (Exception $e) {} } diff --git a/controllers/list.php b/controllers/list.php index 0ec33d1..2dea9ec 100644 --- a/controllers/list.php +++ b/controllers/list.php @@ -4,11 +4,12 @@ use Database; use Exception; function on_post() { + global $TOKEN; global $list_status; $list_status = ""; try { - $uid = Database\Cookie::fromDB($_POST['token'])->UID; + $uid = Database\Cookie::fromDB($TOKEN)->UID; Database\ArchiveList::create($uid, $_POST["name"], $_POST["description"]); } catch(Exception $e) { diff --git a/controllers/login.php b/controllers/login.php index 8b640ce..179afe6 100644 --- a/controllers/login.php +++ b/controllers/login.php @@ -23,9 +23,9 @@ function on_post() { } function on_delete() { + global $TOKEN; try { - $headers = apache_request_headers(); - Database\Cookie::delete($headers["Authorization"]); + Database\Cookie::delete($TOKEN); } catch(Exception $e) {} } diff --git a/controllers/meta.php b/controllers/meta.php index e20bbfc..91a8ade 100644 --- a/controllers/meta.php +++ b/controllers/meta.php @@ -6,8 +6,18 @@ function call_handler(string $name) { } } -switch ($_SERVER['REQUEST_METHOD']) { - case 'POST': call_handler('Controller\on_post'); break; - case 'PUT': call_handler('Controller\on_put'); break; - case 'DELETE': call_handler('Controller\on_delete'); break; -}; +$TOKEN = (array_key_exists('token', $_COOKIE)) ? ($_COOKIE['token'] ?? "") : (""); + +function request_handler() { + if ($_SERVER['REQUEST_METHOD'] === 'POST') { + if (array_key_exists('method', $_POST)) { + switch ($_POST['method']) { + case 'PUT': call_handler('Controller\on_put'); return; + case 'DELETE': call_handler('Controller\on_delete'); return; + case 'PATCH': call_handler('Controller\on_patch'); return; + } + } + call_handler('Controller\on_post'); + } +} +request_handler(); diff --git a/views/archive/index.php b/views/archive/index.php index ac6ada6..7de2dff 100644 --- a/views/archive/index.php +++ b/views/archive/index.php @@ -18,13 +18,8 @@
-
- @@ -61,12 +56,7 @@

"" hasn't been archived yet!

-
- diff --git a/views/authenticate.js b/views/authenticate.js index 8b158ce..398794a 100644 --- a/views/authenticate.js +++ b/views/authenticate.js @@ -1,3 +1,25 @@ +var cookieStorage = { + getItem: function(index) { + let cookies = document.cookie.split(';'); + for (cookie of cookies) { + let values = cookie.trim().split('='); + if (values[0] === index) { + return values[1]; + } + } + return undefined; + }, + setItem: function(index, value, expires = 'Fri, 31 Dec 9999 23:59:59 GMT') { + let cookie = index + '=' + value + ';'; + cookie += 'expires=' + expires + ';'; + cookie += 'path=/'; + document.cookie = cookie; + }, + removeItem: function(index) { + cookieStorage.setItem(index, "", 'Thu, 01 Jan 1970 00:00:00 GMT'); + }, +}; + var authentication_response = null; var authentication_callbacks = []; @@ -9,7 +31,6 @@ function requestAuthentication() { authentication_response = (request.status == 200) ? request.responseText : ""; } request.open("POST", "/authenticate", true); - request.setRequestHeader("Authorization", sessionStorage.getItem("token")); request.send(null); } requestAuthentication(); diff --git a/views/global/router.php b/views/global/router.php index fa9348d..cd9b304 100644 --- a/views/global/router.php +++ b/views/global/router.php @@ -40,6 +40,8 @@ foreach (glob($MODELS_DIR . '/*.php') as $filename) { require_once $filename; } +$TOKEN = (array_key_exists('token', $_COOKIE)) ? ($_COOKIE['token'] ?? "") : (""); + if (str_ends_with($view, '.php')) { require_once $view; } diff --git a/views/login/index.php b/views/login/index.php index 94faafe..da0d8e2 100644 --- a/views/login/index.php +++ b/views/login/index.php @@ -10,7 +10,7 @@

diff --git a/views/logout/index.php b/views/logout/index.php index fc9af5a..87878ed 100644 --- a/views/logout/index.php +++ b/views/logout/index.php @@ -1,14 +1,11 @@

Logging you out...

diff --git a/views/newlist/index.php b/views/newlist/index.php index 289c9da..72ac7a3 100644 --- a/views/newlist/index.php +++ b/views/newlist/index.php @@ -1,5 +1,5 @@ @@ -19,10 +19,5 @@ - - diff --git a/views/profile/authenticate.php b/views/profile/authenticate.php index afe1ca7..e4cf47e 100644 --- a/views/profile/authenticate.php +++ b/views/profile/authenticate.php @@ -9,8 +9,7 @@ if ($_SERVER['REQUEST_METHOD'] !== 'POST') { } try { - $headers = apache_request_headers(); - $user = Database\Cookie::fromDB($headers["Authorization"]); + $user = Database\Cookie::fromDB($TOKEN); http_response_code(200); header('Content-Type: text/plain'); -- cgit v1.2.3