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 22060c1f36b3668b72838f76534d0dd7219bf7d2
parent d6a94ac90dc544c48df1d195bb93bee5b0de308d
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Fri,  1 Nov 2024 11:08:37 +0100

fix: ensure that new files never overwrite existing ones

Diffstat:
Mapi/new.php | 14++++++++++++--
Mlib/edit.php | 15+++++++++++++++
2 files changed, 27 insertions(+), 2 deletions(-)

diff --git a/api/new.php b/api/new.php @@ -76,10 +76,20 @@ $user . "/" . $category . "/" . $filename; - if (file_exists($file_path)) { - $filename = time() . "_" . $filename; + /* find new unique filename by adding a counter in front */ + if (file_exists($filepath)) { + $file_counter = 0; + while (file_exists($filepath)) { + $file_counter++; + $filepath = $GLOBALS["data_dir"] . $user . "/" . $category . "/" . + $file_counter . "_" . $filename; + } + /* update filename, once we found a unique one */ + $filename = $file_counter . "_" . $filename; + } } + /* write note */ $written = write_note( $user, $category, diff --git a/lib/edit.php b/lib/edit.php @@ -26,6 +26,21 @@ function edit_note( remove_empty_categories($user, basename(dirname($filepath_t1, 1))); } + /* if no old file path exists, this is most likely a new note. Existing + * notes should never be overwritten! Their names have to be unique. */ + $filepath_t0 = $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t0; + if (empty($filepath_t1) && file_exists($filepath_t0)) { + /* find new unique filename by adding a counter in front */ + $file_counter = 0; + while (file_exists($filepath_t0)) { + $file_counter++; + $filepath_t0 = $GLOBALS["data_dir"] . "/" . $user . "/" .$category . + "/" . $file_counter . "_" . $filename; + } + /* update filename, once we found a unique one */ + $filename = $file_counter . "_" . $filename; + } + /* Update content of note if necessary^ */ $written = write_note( $user,