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

edit.php (1957B)


      1 <?php
      2 
      3 function edit_note(
      4     string $user,
      5     string $category,
      6     string $filename,
      7     string $filepath_t1,
      8     string $content
      9 ) {
     10 
     11     /* construct new filepath */
     12     $filepath_t0 = $category . "/" . $filename;
     13 
     14     /* if old and new filepaths differ, the file was likely renamed.
     15      * delete the old one and return immediately */
     16     if ($filepath_t1 != $filepath_t0 && !empty($filepath_t1)) {
     17         $renamed = rename(
     18             $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t1,
     19             $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t0
     20         );
     21         /* if renaming failed, return "false" and exit immediately */
     22         if ($renamed === false) {
     23             return false;
     24         }
     25         /* cleanup directories and removed empty categories */
     26         remove_empty_categories($user, basename(dirname($filepath_t1, 1)));
     27     }
     28 
     29     /* if no old file path exists, this is most likely a new note. Existing
     30      * notes should never be overwritten! Their names have to be unique. */
     31     $filepath_t0 = $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t0;
     32     if (empty($filepath_t1) && file_exists($filepath_t0)) {
     33         /* find new unique filename by adding a counter in front */
     34         $file_counter = 0;
     35         while (file_exists($filepath_t0)) {
     36             $file_counter++;
     37             $filepath_t0 = $GLOBALS["data_dir"] . "/" . $user . "/" .$category .
     38                 "/" . $file_counter . "_" . $filename;
     39         }
     40         /* update filename, once we found a unique one */
     41         $filename = $file_counter . "_" . $filename;
     42     }
     43 
     44     /* Update content of note if necessary^ */
     45     $written = write_note(
     46         $user,
     47         $category,
     48         $filename,
     49         $content
     50     );
     51 
     52     /* if writing failed, return "false" and exit immediately */
     53     if ($written === false) {
     54         return false;
     55     }
     56 
     57     /* if all went fine, return the new filename */
     58     return $filename;
     59 
     60 } // function
     61 
     62 ?>
     63