commit 4e8b19c1e73fcb0abd4a8d3c9ef7a9268b4b0f9a
parent b34c77b0ff4f84f66711b4c7e513d1aa8614c119
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Sat, 26 Oct 2024 13:35:50 +0200
feat: make data dir adjustable
Diffstat:
8 files changed, 20 insertions(+), 10 deletions(-)
diff --git a/config/config.php b/config/config.php
@@ -1,3 +1,4 @@
 <?php
 
 $GLOBALS["pw_salt"] = "set_a_proper_salt";
+$GLOBALS["data_dir"] = $_SERVER["DOCUMENT_ROOT"] . "/data";
diff --git a/lib/delete.php b/lib/delete.php
@@ -7,7 +7,7 @@ function delete_note(
 ) {
 
     /* Create full file path name */
-    $dirpath = "./data/" . $user . "/" . $category;
+    $dirpath = $GLOBALS["data_dir"] . "/" . $user . "/" . $category;
     $filepath = $dirpath . "/" . $filename;
 
     /* delete note */
diff --git a/lib/helpers.php b/lib/helpers.php
@@ -9,6 +9,15 @@ function gather_post(string $key) {
     }
 }
 
+function gather_get(string $key) {
+    /* if key exists, return its value, otherwise return null */
+    if (array_key_exists($key, $_GET)) {
+        return $_GET[$key];
+    } else {
+        return "";
+    }
+}
+
 function validate_input_string(string $in) {
     /* Only allows alphanumeric characters and any of "-_. " */
     $out = preg_replace(
@@ -59,7 +68,7 @@ function gather_notes(
     $filenames = array();
 
     /* Create full path name */
-    $dirpath = "./data/" . $user;
+    $dirpath = $GLOBALS["data_dir"] . "/" . $user;
 
     /* if no category is given, search in all categories
      * if a category is given, only search in this one */
diff --git a/lib/menus.php b/lib/menus.php
@@ -103,7 +103,7 @@ function category_menu(string $user) {
 
 <?php
     /* Fetch all categories of the user */
-    $categories_path = glob("./data/" . $user . "/*");
+    $categories_path = glob($GLOBALS["data_dir"] . "/" . $user . "/*");
     foreach ($categories_path as $category_path) {
 
         if (count(glob($category_path . "/" . "*.txt")) > 0) {
diff --git a/lib/read.php b/lib/read.php
@@ -7,7 +7,7 @@ function read_note(
 ) {
 
     /* Create full file path name */
-    $dirpath = "./data/" . $user . "/" . $category;
+    $dirpath = $GLOBALS["data_dir"] . "/" . $user . "/" . $category;
     $filepath = $dirpath . "/" . $filename;
 
     /* get filesize */
diff --git a/lib/show.php b/lib/show.php
@@ -12,7 +12,7 @@ function show_note(
 ?>
 
 <!-- Show Text Note Form -->
-<form action="/" target="_self" method="post" enctype="multipart/form-data">
+<form action="/" target="_self" method="post">
     <label for="content"><?php echo $GLOBALS["i18n_note"]; ?></label>
     <textarea id="content" name="content"><?php echo $content; ?></textarea>
     <label for="category"><?php echo $GLOBALS["i18n_category"]; ?></label>
@@ -44,7 +44,7 @@ function show_note(
     <input type="submit" value="<?php echo $GLOBALS["i18n_save"]; ?>">
 </form>
 
-<form action="/" method="post" enctype="multipart/form-data" id="delete">
+<form action="/" method="post" id="delete">
     <input
         id="category"
         name="category"
diff --git a/lib/users.php b/lib/users.php
@@ -41,7 +41,7 @@ function auth_user(
 
     /* read token file of user */
     $tokens_storage = file(
-        "./data/" . $user . "/.tokens",
+        $GLOBALS["data_dir"] . "/" . $user . "/.tokens",
         FILE_IGNORE_NEW_LINES
     );
     if ($tokens_storage === false) {
@@ -50,7 +50,7 @@ function auth_user(
 
     /* find any matches between given hash and tokens */
     $matches = preg_grep(
-        "/" . $token . "/",
+        "/^" . $token . "$/",
         $tokens_storage
     );
 
@@ -83,7 +83,7 @@ function create_auth(
     $token = create_password_hash($pass);
 
     /* generate token path */
-    $user_path = "./data/" . $user;
+    $user_path = $GLOBALS["data_dir"] . "/" . $user;
     $token_path = $user_path . "/.tokens";
 
     /* Only create new auth, if no previous token file exists already */
diff --git a/lib/write.php b/lib/write.php
@@ -8,7 +8,7 @@ function write_note(
 ) {
 
     /* Create full file path name */
-    $dirpath = "./data/" . $user . "/" . $category;
+    $dirpath = $GLOBALS["data_dir"] . "/" . $user . "/" . $category;
     $filepath = $dirpath . "/" . $filename;
 
     /* create user directory if it does not exist */