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 1b03f4e12bf00ea93e0c66e560da667c4e40276f
parent 2466ff60a11dd1a3eac047b987bd238ae8bd4c88
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Fri, 25 Oct 2024 17:44:27 +0200

feat: add function to edit notes

Diffstat:
Mindex.php | 16+++++++++++++++-
Alib/edit.php | 39+++++++++++++++++++++++++++++++++++++++
Mlib/show.php | 8++++----
Mlib/write.php | 9++++++---
4 files changed, 64 insertions(+), 8 deletions(-)

diff --git a/index.php b/index.php @@ -24,7 +24,8 @@ $action = gather_post("action"); $category = gather_post("category"); $filename = gather_post("filename"); - $filename_t1 = gather_post("filename_t1"); + $filepath_t1 = gather_post("filepath_t1"); + $content = gather_post("content"); $user = gather_post("user"); ?> @@ -153,6 +154,19 @@ } } + /* Edit action */ + if ($action == "edit") { + edit_note( + $user, + $category, + $filename, + $filepath_t1, + $content + ); + /* set action to "show", so the new file will be shown afterwards */ + $action = "show"; + } + /* Show action */ if ($action == "show") { show_note( diff --git a/lib/edit.php b/lib/edit.php @@ -0,0 +1,39 @@ +<?php + +function edit_note( + string $user, + string $category, + string $filename, + string $filepath_t1, + string $content +) { + + /* Write note as if it was completely new */ + $written = write_note( + $user, + $category, + $filename, + $content + ); + + /* if writing failed, return "false" and exit immediately */ + if ($written === false) { + return false; + } + + /* construct new filepath */ + $filepath_t0 = $user . "/" . $category . "/" . $filename; + + /* if old and new filepaths differ, the file was likely renamed. + * delete the old one and exit immediately */ + if ($filepath_t1 != $filepath_t0) { + return unlink("./data/" . $filepath_t1); + } + + /* if all actions went through, return "true" */ + return true; + +} // function + +?> + diff --git a/lib/show.php b/lib/show.php @@ -13,8 +13,8 @@ function show_note( <!-- Show Text Note Form --> <form action="/" target="_self" method="post" enctype="multipart/form-data"> - <label for="note"><?php echo $GLOBALS["i18n_note"]; ?></label> - <textarea id="note" name="note"><?php echo $content; ?></textarea> + <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> <input id="category" @@ -30,8 +30,8 @@ function show_note( value="<?php echo $filename; ?>" > <input - id="filename_t1" - name="filename_t1" + id="filepath_t1" + name="filepath_t1" type="hidden" value="<?php echo $user . "/" . $category . "/" . $filename; ?>" > diff --git a/lib/write.php b/lib/write.php @@ -27,13 +27,16 @@ function write_note( ); /* Write to file */ - fwrite( + $bytes = fwrite( $file, $content ); - /* return "true" on success */ - return true; + /* close file-hanle */ + fclose($file); + + /* return "true" on success and "false" otherwise */ + return $bytes !== false; }