From 83b8ec179947cb29955a10d948b429a0b9842a53 Mon Sep 17 00:00:00 2001 From: Jonathan Flueren Date: Wed, 16 Aug 2023 16:46:54 +0200 Subject: [PATCH] Move messages to php sessions --- website/index.php | 188 ++++++++++++++++++++++++---------------------- 1 file changed, 100 insertions(+), 88 deletions(-) diff --git a/website/index.php b/website/index.php index 4a101ed..88520af 100644 --- a/website/index.php +++ b/website/index.php @@ -1,11 +1,12 @@ setTimestamp($present["timestamp"]); -function hash_mac($mac) { +function hash_mac($mac) +{ $normalized_mac = str_replace('-', ':', strtolower($mac)); $hashed_mac = hash('sha256', $normalized_mac); return $hashed_mac; } -function add_identity($name, $mac) { +function add_identity($name, $mac) +{ if ( - preg_match('/' . $name_pattern . '/', $name) != 1 || - preg_match('/' . $mac_pattern . '/', $mac) != 1 - ) { - http_response_code(400); - die("Bad data"); - } - - $hashed_mac = hash_mac($mac); - - $identities = json_decode(file_get_contents(IDENTITIES_FILE), true); - - $url = strtok($_SERVER['REQUEST_URI'], '?'); + preg_match('/' . NAME_PATTERN . '/', $name) != 1 || + preg_match('/' . MAC_PATTERN . '/', $mac) != 1 + ) { + http_response_code(400); + die("Bad data"); + } - foreach ($identities as $identity) { - if ($identity['name'] == $name) { - header("Location: " . $url . "?dup_name", true, 303); - exit(); - } - if ($identity['mac_hash'] == $hashed_mac) { - header("Location: " . $url . "?dup_mac", true, 303); - exit(); - } - } - - array_push($identities, array("name" => $name, "mac_hash" => $hashed_mac)); - file_put_contents(IDENTITIES_FILE, json_encode($identities)); - - header("Location: " . $url . "?succ", true, 303); - exit(); + $hashed_mac = hash_mac($mac); + + $identities = json_decode(file_get_contents(IDENTITIES_FILE), true); + + $url = strtok($_SERVER['REQUEST_URI'], '?'); + + foreach ($identities as $identity) { + if ($identity['name'] == $name) { + $_SESSION['form_success'] = false; + $_SESSION['form_success_message'] = 'Name already in use, please choose a different one.'; + header("Location: " . $url, true, 303); + exit(); + } + if ($identity['mac_hash'] == $hashed_mac) { + $_SESSION['form_success'] = false; + $_SESSION['form_success_message'] = 'MAC already set up, please remove it first to change name.'; + header("Location: " . $url, true, 303); + exit(); + } + } + + array_push($identities, array("name" => $name, "mac_hash" => $hashed_mac)); + file_put_contents(IDENTITIES_FILE, json_encode($identities)); + + $_SESSION['form_success'] = true; + $_SESSION['form_success_message'] = 'Identity successfully saved.'; + header("Location: " . $url, true, 303); + exit(); } -function remove_identity($mac) { - if (preg_match('/' . $mac_pattern . '/', $mac) != 1) { - http_response_code(400); - die("Bad data"); - } +function remove_identity($mac) +{ + if (preg_match('/' . MAC_PATTERN . '/', $mac) != 1) { + http_response_code(400); + die("Bad data"); + } - $hashed_mac = hash_mac($mac); + $hashed_mac = hash_mac($mac); - $identities = json_decode(file_get_contents(IDENTITIES_FILE), true); + $identities = json_decode(file_get_contents(IDENTITIES_FILE), true); - $new_identities = array(); + $new_identities = array(); - foreach ($identities as $identity) { + foreach ($identities as $identity) { if ($identity['mac_hash'] != $hashed_mac) { array_push($new_identities, $identity); } - file_put_contents(IDENTITIES_FILE, json_encode($new_identities)); + file_put_contents(IDENTITIES_FILE, json_encode($new_identities)); - header("Location: " . strtok($_SERVER['REQUEST_URI'], '?'), true, 303); - exit(); + $_SESSION['form_success'] = true; + $_SESSION['form_success_message'] = 'Identity successfully removed.'; + header("Location: " . strtok($_SERVER['REQUEST_URI'], '?'), true, 303); + exit(); } } if ($_SERVER['REQUEST_METHOD'] == "POST") { if ( - isset($_POST['name']) && + isset($_POST['name']) && isset($_POST['mac']) ) { add_identity($_POST['name'], $_POST['mac']); - } - else if (isset($_POST['remove-mac'])) { + } else if (isset($_POST['remove-mac'])) { remove_identity($_POST['remove-mac']); } } ?> - - <?php echo $page_title; ?> - - - - - - + + + <?php echo PAGE_TITLE; ?> + + + + + + + + +

Jetzt gerade im CZI (letztes Update von format('H:i:s d.m.Y'); ?>)


- + - - - - - + + +
Name


- + if (isset($_SESSION['form_success'])) { + if ($_SESSION['form_success'] === false) { + ?> + - - - - -
+ + +
Neues Gerät tracken
-
-
+
+
@@ -159,7 +170,7 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
Gerät entfernen
-
+
@@ -167,5 +178,6 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") {
- + + \ No newline at end of file