edit.php (2324B)
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
18 /* Ensure the new path exists */
19 $dirpath = $GLOBALS["data_dir"] . "/" . $user . "/" . $category;
20 if (opendir($dirpath) === false) {
21 mkdir(
22 $dirpath, /* directory */
23 0770, /* Permissions: rwxrwx--- */
24 true /* recursive */
25 );
26 }
27
28 /* Rename / move file */
29 $renamed = rename(
30 $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t1,
31 $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t0
32 );
33 /* if renaming failed, return "false" and exit immediately */
34 if ($renamed === false) {
35 return false;
36 }
37
38 /* cleanup directories and removed empty categories */
39 remove_empty_categories($user, basename(dirname($filepath_t1, 1)));
40 }
41
42 /* if no old file path exists, this is most likely a new note. Existing
43 * notes should never be overwritten! Their names have to be unique. */
44 $filepath_t0 = $GLOBALS["data_dir"] . "/" . $user . "/" . $filepath_t0;
45 if (empty($filepath_t1) && file_exists($filepath_t0)) {
46 /* find new unique filename by adding a counter in front */
47 $file_counter = 0;
48 while (file_exists($filepath_t0)) {
49 $file_counter++;
50 $filepath_t0 = $GLOBALS["data_dir"] . "/" . $user . "/" .$category .
51 "/" . $file_counter . "_" . $filename;
52 }
53 /* update filename, once we found a unique one */
54 $filename = $file_counter . "_" . $filename;
55 }
56
57 /* Update content of note if necessary^ */
58 $written = write_note(
59 $user,
60 $category,
61 $filename,
62 $content
63 );
64
65 /* if writing failed, return "false" and exit immediately */
66 if ($written === false) {
67 return false;
68 }
69
70 /* if all went fine, return the new filename */
71 return $filename;
72
73 } // function
74
75 ?>
76