helpers.php (8136B)
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_categories(
89 string $user
90 ) {
91
92 /* initilize categories array including the "all" pseudo-category */
93 $categories = array(
94 array(
95 "name" => "",
96 "matches" => 0
97 )
98 );
99
100 /* Fetch all categories of the user and loop through them */
101 $categories_path = glob($GLOBALS["data_dir"] . "/" . $user . "/*");
102 foreach ($categories_path as $category_path) {
103
104 /* store names and number of notes for each category */
105 $n_notes = count(glob($category_path . "/" . "*.txt"));
106 array_push(
107 $categories,
108 array(
109 "name" => basename($category_path),
110 "matches" => $n_notes
111 )
112 );
113
114 /* add number of notes to the "all" pseudo-category */
115 $categories[0]["matches"] = $categories[0]["matches"] + $n_notes;
116
117 } // foreach-loop
118
119 /* sort categories by number of notes */
120 usort($categories, "sort_by_matches");
121
122 /* return categories array */
123 return $categories;
124
125 } // function
126
127
128 function gather_notes(
129 string $user,
130 string $category = ""
131 ) {
132
133 /* initilise files array */
134 $filenames = array();
135
136 /* Create full path name */
137 $dirpath = $GLOBALS["data_dir"] . "/" . $user;
138
139 /* if no category is given, search in all categories
140 * if a category is given, only search in this one */
141 if (empty($category)) {
142
143 /* list all available categories */
144 $categories_path = glob($dirpath . "/*");
145
146 } else {
147
148 $categories_path = array($dirpath . "/" . $category);
149 }
150
151 /* loop through each category path and find all files within them */
152 foreach ($categories_path as $category_path) {
153
154 $files_path = glob($category_path . "/" . "*.txt");
155
156 foreach ($files_path as $file_path) {
157
158 array_push(
159 $filenames,
160 array(
161 "category" => basename($category_path),
162 "name" => preg_replace(
163 "/\.txt$/",
164 "",
165 basename($file_path)
166 ),
167 "file" => $file_path,
168 "time" => filemtime($file_path),
169 "link" => link_in_first_line($file_path)
170 )
171 );
172
173 } // foreach-loop
174 } // foreach-loop
175
176 /* return filenames array */
177 return $filenames;
178
179 } // function
180
181 function page_title(
182 string $url
183 ) {
184
185 /* initilise title for page */
186 $title = "";
187
188 /* open url handle */
189 $handle = fopen($url, "r");
190
191 /* if connection suceeded, fetch the content up until </title> */
192 if ($handle) {
193 $string = stream_get_line($handle, 0, "</title>");
194 fclose($handle);
195 $string = explode("<title", $string)[1];
196
197 /* if a title could be gathered, clean it up */
198 if (!empty($string)) {
199 $title = trim((explode(">", $string))[1]);
200 }
201 }
202
203 /* return title */
204 return htmlspecialchars_decode(html_entity_decode($title));
205 }
206
207 function generate_filename(string $content) {
208
209 /* initilise new filename */
210 $filename = "";
211
212 /* if the first line of the content contains a URL,
213 * we may use the page title */
214 $file_link = preg_replace(
215 '/^(https{0,1}:\/\/[^\s]+)*.*$/',
216 '\1',
217 explode(PHP_EOL, $content)[0]
218 );
219 if (!empty($file_link)) {
220 $filename = page_title($file_link);
221 }
222
223 /* if filename is still unset after this and does have a link,
224 * (so the fetching failed), we can use the link itself as title. */
225 if (empty($filename) && !empty($file_link)) {
226 $filename = preg_replace(
227 '/^https{0,1}:\/\/(.*)$/',
228 '\1',
229 $file_link
230 );
231 $filename = trim($filename); // trim white space
232 }
233
234 /* if filename is still unset (there was no link), try to use the first line
235 * of the file as title (with a maximum character limit) */
236 if (empty($filename)) {
237 $filename = substr(
238 trim(explode(PHP_EOL, $content)[0]),
239 0,
240 100 // at max, 100 characters long
241 );
242 }
243
244 /* as a last resort, we use the sha256sum of the content as fallback */
245 if (empty($filename)) {
246 $filename = hash("sha256", $content, false);
247 }
248
249 /* Ensure the new filename only contains valid characters */
250 $filename = validate_input_string($filename);
251
252 /* ensure filename as a proper file extension */
253 $filename = $filename . ".txt";
254
255 /* return new filename */
256 return $filename;
257 }
258
259 function remove_empty_categories(
260 string $user,
261 string $category = ""
262 ) {
263
264 /* gather all categories of the given user or use the given category */
265 if (empty($category)) {
266 $categories_path = glob($GLOBALS["data_dir"] . "/" . $user . "/*");
267 } else {
268 $categories_path = array(
269 $GLOBALS["data_dir"] . "/" . $user . "/" . $category
270 );
271 }
272
273 /* loop through categories and remove those that are empty */
274 foreach ($categories_path as $category_path) {
275
276 /* ensure that we are indeed within a category FOLDER */
277 if (is_dir($category_path) && is_readable($category_path)) {
278
279 /* list all files in the category path, remove "." and ".." */
280 $files_in_cat = array_filter(
281 scandir($category_path),
282 function($x) { return ($x != "." && $x != ".."); }
283 );
284
285 /* if directory is empty, remove it */
286 if (count($files_in_cat) === 0) {
287 $removed = rmdir($category_path);
288 /* if removale failed, exit immediately and return "false" */
289 if ($removed === false) {
290 return false;
291 }
292 }
293 }
294 }
295
296 /* return "true" on success */
297 return true;
298
299 }
300
301 ?>