pub / rememori

Simple file-based bookmarking and notes application
git clone https://src.jayvii.de/pub/rememori.git
Home | Log | Files | Exports | Refs | README | RSS

commit 8738f816e72e8d1d897fc13029d7bdeae49680eb
parent 4e8b19c1e73fcb0abd4a8d3c9ef7a9268b4b0f9a
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Sat, 26 Oct 2024 13:36:13 +0200

feat: REST API for writing new notes

Diffstat:
Aapi/new.php | 81+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 81 insertions(+), 0 deletions(-)

diff --git a/api/new.php b/api/new.php @@ -0,0 +1,81 @@ +<?php + + /* Load library functions */ + foreach (glob($_SERVER["DOCUMENT_ROOT"] . "/lib/*.php") as $lib) { + include($lib); + } + + /* Load configurations */ + foreach (glob($_SERVER["DOCUMENT_ROOT"] . "/config/*.php") as $conf) { + include($conf); + } + + /* Gather POST input and validate if necessary */ + $category = validate_input_string(gather_post("category")); + $filename = validate_input_string(gather_post("filename")); + $content = gather_post("content"); + $user = validate_input_string(gather_post("user")); + $token = validate_input_string(gather_post("token")); + + /* Fallback to GET request, if no POST was found */ + if (empty($category)) { + $category = validate_input_string(gather_get("category")); + } + if (empty($filename)) { + $filename = validate_input_string(gather_get("filename")); + } + if (empty($content)) { + $content = gather_get("content"); + } + if (empty($user)) { + $user = validate_input_string(gather_get("user")); + } + if (empty($token)) { + $token = validate_input_string(gather_get("token")); + } + + /* run authentification method. exit immediately if it fails */ + $auth = auth_user($user, $token, -1); + if ($auth !== true) { + exit("You could not be authenticated!"); + } + + /* if category is unset, set it! */ + if (empty($category)) { + $category = "unknown"; + } + + /* if filename is empty, figure it out */ + if (empty($filename)) { + $filename = generate_filename($content); + } + + /* ensure the given filename has a valid extension */ + if (count(preg_grep("/\.txt$/", array($filename), PREG_GREP_INVERT)) > 0) { + $filename = $filename . ".txt"; + } + + /* rename file if it already exists */ + $file_path = $GLOBALS["data_dir"] . "/" . + $user . "/" . + $category . "/" . + $filename; + if (file_exists($file_path)) { + $filename = time() . "_" . $filename; + } + + $written = write_note( + $user, + $category, + $filename, + $content + ); + + if ($written === true) { + echo "Written as " . $user . "/" . $category . "/" . $filename; + exit(); + } else { + exit("Writing failed!" . $written); + } + +?>