helpers.php (7080B)
1 <?php
2
3 function gather_post(string $key) {
4 /* if key exists, return its value, otherwise return null */
5 if (array_key_exists($key, $_POST)) {
6 return $_POST[$key];
7 } else {
8 return "";
9 }
10 }
11
12 function gather_get(string $key) {
13 /* if key exists, return its value, otherwise return null */
14 if (array_key_exists($key, $_GET)) {
15 return $_GET[$key];
16 } else {
17 return "";
18 }
19 }
20
21 function validate_input_string(string $in) {
22 /* Replace umlauts and similar */
23 $out = str_replace(
24 array(
25 "ä","ö","ü","ß", "Ä", "Ö", "Ü",
26 "é", "à", "è", "ù", "ç", "â", "ê", "î", "ô", "û", "ë", "ï", "ü",
27 "É", "À", "È", "Ù", "Ç", "Â", "Ê", "Î", "Ô", "Û", "Ë", "Ï", "Ü"
28 ),
29 array(
30 "ae", "oe", "ue", "ss", "Ae", "Oe", "Ue",
31 "e", "a", "e", "u", "c", "a", "e", "i", "o", "u", "e", "i", "u",
32 "E", "A", "E", "U", "C", "A", "E", "I", "O", "U", "E", "I", "U"
33 ),
34 $in
35 );
36 /* Only allows certain characters for parsing reasons */
37 $out = preg_replace(
38 "/[^a-z0-9\-\_\.\s\:\,\;]/i",
39 " ",
40 $out
41 );
42 /* Remove multi-spaces */
43 $out = preg_replace(
44 "/\s+/",
45 " ",
46 $out
47 );
48 /* return trimmed string */
49 return trim($out);
50 }
51
52 function sort_by_time(array $a, array $b) {
53 // sort from oldest to newest (reversed)
54 return $b["time"] - $a["time"];
55 }
56
57 function sort_by_matches(array $a, array $b) {
58 // sort from most to fewest (reversed)
59 return $b["matches"] - $a["matches"];
60 }
61
62 function link_in_first_line(string $filepath) {
63
64 /* read first line of file */
65 $file_content = file($filepath, FILE_IGNORE_NEW_LINES);
66 if (count($file_content) > 0) {
67 $file_first_line = $file_content[0];
68 } else {
69 $file_first_line = "";
70 }
71
72 /* extract links from first line */
73 $file_link = preg_replace(
74 '/^(http(s){0,1}:\/\/[^\s]+)*.*$/',
75 '\1',
76 $file_first_line
77 );
78
79 /* return link or false, if none was found */
80 if (!empty($file_link)) {
81 return $file_link;
82 } else {
83 return false;
84 }
85
86 }
87
88 function gather_notes(
89 string $user,
90 string $category = ""
91 ) {
92
93 /* initilise files array */
94 $filenames = array();
95
96 /* Create full path name */
97 $dirpath = $GLOBALS["data_dir"] . "/" . $user;
98
99 /* if no category is given, search in all categories
100 * if a category is given, only search in this one */
101 if (empty($category)) {
102
103 /* list all available categories */
104 $categories_path = glob($dirpath . "/*");
105
106 } else {
107
108 $categories_path = array($dirpath . "/" . $category);
109 }
110
111 /* loop through each category path and find all files within them */
112 foreach ($categories_path as $category_path) {
113
114 $files_path = glob($category_path . "/" . "*.txt");
115
116 foreach ($files_path as $file_path) {
117
118 array_push(
119 $filenames,
120 array(
121 "category" => basename($category_path),
122 "name" => preg_replace(
123 "/\.txt$/",
124 "",
125 basename($file_path)
126 ),
127 "file" => $file_path,
128 "time" => filemtime($file_path),
129 "link" => link_in_first_line($file_path)
130 )
131 );
132
133 } // foreach-loop
134 } // foreach-loop
135
136 /* return filenames array */
137 return $filenames;
138
139 } // function
140
141 function page_title(
142 string $url
143 ) {
144
145 /* initilise title for page */
146 $title = "";
147
148 /* open url handle */
149 $handle = fopen($url, "r");
150
151 /* if connection suceeded, fetch the content up until </title> */
152 if ($handle) {
153 $string = stream_get_line($handle, 0, "</title>");
154 fclose($handle);
155 $string = explode("<title", $string)[1];
156
157 /* if a title could be gathered, clean it up */
158 if (!empty($string)) {
159 $title = trim((explode(">", $string))[1]);
160 }
161 }
162
163 /* return title */
164 return htmlspecialchars_decode(html_entity_decode($title));
165 }
166
167 function generate_filename(string $content) {
168
169 /* initilise new filename */
170 $filename = "";
171
172 /* if the first line of the content contains a URL,
173 * we may use the page title */
174 $file_link = preg_replace(
175 '/^(https{0,1}:\/\/[^\s]+)*.*$/',
176 '\1',
177 explode(PHP_EOL, $content)[0]
178 );
179 if (!empty($file_link)) {
180 $filename = page_title($file_link);
181 }
182
183 /* if filename is still unset after this and does have a link,
184 * (so the fetching failed), we can use the link itself as title. */
185 if (empty($filename) && !empty($file_link)) {
186 $filename = preg_replace(
187 '/^https{0,1}:\/\/(.*)$/',
188 '\1',
189 $file_link
190 );
191 $filename = trim($filename); // trim white space
192 }
193
194 /* if filename is still unset (there was no link), try to use the first line
195 * of the file as title (with a maximum character limit) */
196 if (empty($filename)) {
197 $filename = substr(
198 trim(explode(PHP_EOL, $content)[0]),
199 0,
200 100 // at max, 100 characters long
201 );
202 }
203
204 /* as a last resort, we use the sha256sum of the content as fallback */
205 if (empty($filename)) {
206 $filename = hash("sha256", $content, false);
207 }
208
209 /* Ensure the new filename only contains valid characters */
210 $filename = validate_input_string($filename);
211
212 /* ensure filename as a proper file extension */
213 $filename = $filename . ".txt";
214
215 /* return new filename */
216 return $filename;
217 }
218
219 function remove_empty_categories(
220 string $user,
221 string $category = ""
222 ) {
223
224 /* gather all categories of the given user or use the given category */
225 if (empty($category)) {
226 $categories_path = glob($GLOBALS["data_dir"] . "/" . $user . "/*");
227 } else {
228 $categories_path = array(
229 $GLOBALS["data_dir"] . "/" . $user . "/" . $category
230 );
231 }
232
233 /* loop through categories and remove those that are empty */
234 foreach ($categories_path as $category_path) {
235
236 /* ensure that we are indeed within a category FOLDER */
237 if (is_dir($category_path) && is_readable($category_path)) {
238
239 /* list all files in the category path, remove "." and ".." */
240 $files_in_cat = array_filter(
241 scandir($category_path),
242 function($x) { return ($x != "." && $x != ".."); }
243 );
244
245 /* if directory is empty, remove it */
246 if (count($files_in_cat) === 0) {
247 $removed = rmdir($category_path);
248 /* if removale failed, exit immediately and return "false" */
249 if ($removed === false) {
250 return false;
251 }
252 }
253 }
254 }
255
256 /* return "true" on success */
257 return true;
258
259 }
260
261 ?>