commit d88035f8dd32420713d28bf26d9496c59c8bb6c1
parent 75ea7feb7ce62c5b4d8a82f91f5101b45353eaf2
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date: Fri, 25 Oct 2024 17:16:01 +0200
feat: gather post inputs via helper function
Diffstat:
2 files changed, 29 insertions(+), 13 deletions(-)
diff --git a/index.php b/index.php
@@ -5,7 +5,7 @@
<?php
/* DEVELOPMENT SETTINGS */
- if (is_null($_POST["action"])) {
+ if (!array_key_exists("action", $_POST)) {
$_POST["action"] = "list";
}
$_POST["user"] = "test_user";
@@ -20,6 +20,13 @@
include($conf);
}
+ /* Gather POST input */
+ $action = gather_post("action");
+ $category = gather_post("category");
+ $filename = gather_post("filename");
+ $filename_t1 = gather_post("filename_t1");
+ $user = gather_post("user");
+
?>
<!DOCTYPE html>
@@ -45,7 +52,7 @@
id="user"
name="user"
type="hidden"
- value="<?php echo $_POST["user"]; ?>"
+ value="<?php echo $user; ?>"
>
<input
id="category"
@@ -81,7 +88,7 @@
id="user"
name="user"
type="hidden"
- value="<?php echo $_POST["user"]; ?>"
+ value="<?php echo $user; ?>"
>
<input
id="action"
@@ -97,7 +104,7 @@
<?php
/* Fetch all categories of the user */
- $categories_path = glob("./data/" . $_POST["user"] . "/*");
+ $categories_path = glob("./data/" . $user . "/*");
foreach ($categories_path as $category_path) {
?>
<form action="/" target="_self" method="post">
@@ -105,7 +112,7 @@
id="user"
name="user"
type="hidden"
- value="<?php echo $_POST["user"]; ?>"
+ value="<?php echo $user; ?>"
>
<input
id="category"
@@ -133,25 +140,25 @@
<?php
/* Listing action */
- if ($_POST["action"] == "list") {
+ if ($action == "list") {
/* if no category is given, list all. otherwise only show given */
- if (!is_null($_POST["category"])) {
+ if (!is_null($category)) {
$categories_path = array(
- "./data/" . $_POST["user"] . "/" . $_POST["category"]
+ "./data/" . $user . "/" . $category
);
}
foreach ($categories_path as $category_path) {
- list_notes($_POST["user"], basename($category_path));
+ list_notes($user, basename($category_path));
}
}
/* Show action */
- if ($_POST["action"] == "show") {
+ if ($action == "show") {
show_note(
- $_POST["user"],
- $_POST["category"],
- $_POST["filename"]
+ $user,
+ $category,
+ $filename
);
}
diff --git a/lib/helpers.php b/lib/helpers.php
@@ -1,5 +1,14 @@
<?php
+function gather_post(string $key) {
+ /* if key exists, return its value, otherwise return null */
+ if (array_key_exists($key, $_POST)) {
+ return $_POST[$key];
+ } else {
+ return null;
+ }
+}
+
function sort_by_time(array $a, array $b) {
// sort from oldest to newest (reversed)
return $b["time"] - $a["time"];