xref: /webtrees/app/Services/HousekeepingService.php (revision 8d87f2a299f1d90510116f2dec79c534263c3607)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Services;
21
22use Fisharebest\Webtrees\Carbon;
23use Illuminate\Database\Capsule\Manager as DB;
24use League\Flysystem\Filesystem;
25use League\Flysystem\FilesystemException;
26use League\Flysystem\FilesystemOperator;
27use League\Flysystem\UnableToDeleteDirectory;
28use League\Flysystem\UnableToDeleteFile;
29
30/**
31 * Clean up old data, files and folders.
32 */
33class HousekeepingService
34{
35    // This is a list of old files and directories, from earlier versions of webtrees.
36    // git diff 1.7.9..master --name-status | grep ^D
37    private const OLD_PATHS = [
38        // Removed in 1.0.3
39        'themechange.php',
40        // Removed in 1.1.0
41        'addremotelink.php',
42        'addsearchlink.php',
43        'client.php',
44        'dir_editor.php',
45        'editconfig_gedcom.php',
46        'editgedcoms.php',
47        'edit_merge.php',
48        'edit_news.php',
49        'genservice.php',
50        'logs.php',
51        'manageservers.php',
52        'media.php',
53        'module_admin.php',
54        //'modules', // Do not delete - users may have stored custom modules/data here
55        'opensearch.php',
56        'PEAR.php',
57        'pgv_to_wt.php',
58        'places',
59        //'robots.txt', // Do not delete this - it may contain user data
60        'serviceClientTest.php',
61        'siteconfig.php',
62        'SOAP',
63        'themes/clouds/mozilla.css',
64        'themes/clouds/netscape.css',
65        'themes/colors/mozilla.css',
66        'themes/colors/netscape.css',
67        'themes/fab/mozilla.css',
68        'themes/fab/netscape.css',
69        'themes/minimal/mozilla.css',
70        'themes/minimal/netscape.css',
71        'themes/webtrees/mozilla.css',
72        'themes/webtrees/netscape.css',
73        'themes/webtrees/style_rtl.css',
74        'themes/xenea/mozilla.css',
75        'themes/xenea/netscape.css',
76        'uploadmedia.php',
77        'useradmin.php',
78        'webservice',
79        'wtinfo.php',
80        // Removed in 1.1.2
81        'treenav.php',
82        // Removed in 1.2.0
83        'themes/clouds/jquery',
84        'themes/colors/jquery',
85        'themes/fab/jquery',
86        'themes/minimal/jquery',
87        'themes/webtrees/jquery',
88        'themes/xenea/jquery',
89        // Removed in 1.2.2
90        'themes/clouds/chrome.css',
91        'themes/clouds/opera.css',
92        'themes/clouds/print.css',
93        'themes/clouds/style_rtl.css',
94        'themes/colors/chrome.css',
95        'themes/colors/opera.css',
96        'themes/colors/print.css',
97        'themes/colors/style_rtl.css',
98        'themes/fab/chrome.css',
99        'themes/fab/opera.css',
100        'themes/minimal/chrome.css',
101        'themes/minimal/opera.css',
102        'themes/minimal/print.css',
103        'themes/minimal/style_rtl.css',
104        'themes/xenea/chrome.css',
105        'themes/xenea/opera.css',
106        'themes/xenea/print.css',
107        'themes/xenea/style_rtl.css',
108        // Removed in 1.2.3
109        'modules_v2',
110        // Removed in 1.2.4
111        'search_engine.php',
112        'themes/clouds/modules.css',
113        'themes/colors/modules.css',
114        'themes/fab/modules.css',
115        'themes/minimal/modules.css',
116        'themes/webtrees/modules.css',
117        'themes/xenea/modules.css',
118        // Removed in 1.2.5
119        'sidebar.php',
120        // Removed in 1.2.6
121        // Removed in 1.2.7
122        'login_register.php',
123        // Removed in 1.3.0
124        'admin_site_ipaddress.php',
125        'downloadgedcom.php',
126        'export_gedcom.php',
127        'gedcheck.php',
128        'images',
129        // Removed in 1.3.1
130        'imageflush.php',
131        '/lightbox/js/tip_balloon_RTL.js',
132        // Removed in 1.4.0
133        'imageview.php',
134        'media/MediaInfo.txt',
135        'media/thumbs/ThumbsInfo.txt',
136        'themes/webtrees/chrome.css',
137        // Removed in 1.4.2
138        'themes/clouds/jquery-ui-1.10.0',
139        'themes/colors/jquery-ui-1.10.0',
140        'themes/fab/jquery-ui-1.10.0',
141        'themes/minimal/jquery-ui-1.10.0',
142        'themes/webtrees/jquery-ui-1.10.0',
143        'themes/xenea/jquery-ui-1.10.0',
144        // Removed in 1.5.0
145        'themes/clouds/favicon.png',
146        'themes/clouds/images',
147        'themes/clouds/msie.css',
148        'themes/clouds/style.css',
149        'themes/colors/css',
150        'themes/colors/favicon.png',
151        'themes/colors/images',
152        'themes/colors/ipad.css',
153        'themes/colors/msie.css',
154        'themes/fab/favicon.png',
155        'themes/fab/images',
156        'themes/fab/msie.css',
157        'themes/fab/style.css',
158        'themes/minimal/favicon.png',
159        'themes/minimal/images',
160        'themes/minimal/msie.css',
161        'themes/minimal/style.css',
162        'themes/webtrees/favicon.png',
163        'themes/webtrees/images',
164        'themes/webtrees/msie.css',
165        'themes/webtrees/style.css',
166        'themes/xenea/favicon.png',
167        'themes/xenea/images',
168        'themes/xenea/msie.css',
169        'themes/xenea/style.css',
170        // Removed in 1.5.1
171        'themes/clouds/css-1.5.0',
172        'themes/colors/css-1.5.0',
173        'themes/fab/css-1.5.0',
174        'themes/minimal/css-1.5.0',
175        'themes/webtrees/css-1.5.0',
176        'themes/xenea/css-1.5.0',
177        // Removed in 1.5.2
178        'themes/clouds/css-1.5.1',
179        'themes/colors/css-1.5.1',
180        'themes/fab/css-1.5.1',
181        'themes/minimal/css-1.5.1',
182        'themes/webtrees/css-1.5.1',
183        'themes/xenea/css-1.5.1',
184        // Removed in 1.5.3
185        'readme.html',
186        'themes/clouds/css-1.5.2',
187        'themes/colors/css-1.5.2',
188        'themes/fab/css-1.5.2',
189        'themes/minimal/css-1.5.2',
190        'themes/webtrees/css-1.5.2',
191        'themes/xenea/css-1.5.2',
192        // Removed in 1.6.0
193        'downloadbackup.php',
194        'site-php-version.php',
195        'themes/clouds/css-1.5.3',
196        'themes/colors/css-1.5.3',
197        'themes/fab/css-1.5.3',
198        'themes/minimal/css-1.5.3',
199        'themes/webtrees/css-1.5.3',
200        'themes/xenea/css-1.5.3',
201        // Removed in 1.6.2
202        'themes/clouds/jquery-ui-1.10.3',
203        'themes/colors/css-1.6.0',
204        'themes/colors/jquery-ui-1.10.3',
205        'themes/fab/css-1.6.0',
206        'themes/fab/jquery-ui-1.10.3',
207        'themes/minimal/css-1.6.0',
208        'themes/minimal/jquery-ui-1.10.3',
209        'themes/webtrees/css-1.6.0',
210        'themes/webtrees/jquery-ui-1.10.3',
211        'themes/xenea/css-1.6.0',
212        'themes/xenea/jquery-ui-1.10.3',
213        // Removed in 1.7.0
214        'admin_site_other.php',
215        'js',
216        'library',
217        'save.php',
218        'themes/clouds/css-1.6.2',
219        'themes/clouds/templates',
220        'themes/clouds/header.php',
221        'themes/clouds/footer.php',
222        'themes/colors/css-1.6.2',
223        'themes/colors/templates',
224        'themes/colors/header.php',
225        'themes/colors/footer.php',
226        'themes/fab/css-1.6.2',
227        'themes/fab/templates',
228        'themes/fab/header.php',
229        'themes/fab/footer.php',
230        'themes/minimal/css-1.6.2',
231        'themes/minimal/templates',
232        'themes/minimal/header.php',
233        'themes/minimal/footer.php',
234        'themes/webtrees/css-1.6.2',
235        'themes/webtrees/templates',
236        'themes/webtrees/header.php',
237        'themes/webtrees/footer.php',
238        'themes/xenea/css-1.6.2',
239        'themes/xenea/templates',
240        'themes/xenea/header.php',
241        'themes/xenea/footer.php',
242        // Removed in 1.7.2
243        'assets/js-1.7.0',
244        // Removed in 1.7.4
245        'assets/js-1.7.2',
246        'themes/clouds/css-1.7.0',
247        'themes/colors/css-1.7.0',
248        'themes/fab/css-1.7.0',
249        'themes/minimal/css-1.7.0',
250        'themes/webtrees/css-1.7.0',
251        'themes/xenea/css-1.7.0',
252        // Removed in 1.7.5
253        'themes/clouds/css-1.7.4',
254        'themes/colors/css-1.7.4',
255        'themes/fab/css-1.7.4',
256        'themes/minimal/css-1.7.4',
257        'themes/webtrees/css-1.7.4',
258        'themes/xenea/css-1.7.4',
259        // Removed in 1.7.7
260        'assets/js-1.7.4',
261        // Removed in 1.7.8
262        'themes/clouds/css-1.7.5',
263        'themes/colors/css-1.7.5',
264        'themes/fab/css-1.7.5',
265        'themes/minimal/css-1.7.5',
266        'themes/webtrees/css-1.7.5',
267        'themes/xenea/css-1.7.5',
268        // Removed in 2.0.0
269        'action.php',
270        'addmedia.php',
271        'addmin.php',
272        'admin_media.php',
273        'admin_media_upload.php',
274        'admin_module_blocks.php',
275        'admin_module_charts.php',
276        'admin_module_menus.php',
277        'admin_module_reports.php',
278        'admin_module_sidebar.php',
279        'admin_module_tabs.php',
280        'admin_modules.php',
281        'admin_pgv_to_wt.php',
282        'admin_site_access.php',
283        'admin_site_change.php',
284        'admin_site_clean.php',
285        'admin_site_config.php',
286        'admin_site_info.php',
287        'admin_site_logs.php',
288        'admin_site_merge.php',
289        'admin_site_readme.php',
290        'admin_site_upgrade.php',
291        'admin_trees_check.php',
292        'admin_trees_config.php',
293        'admin_trees_download.php',
294        'admin_trees_duplicates.php',
295        'admin_trees_export.php',
296        'admin_trees_manage.php',
297        'admin_trees_merge.php',
298        'admin_trees_places.php',
299        'admin_trees_renumber.php',
300        'admin_trees_unconnected.php',
301        'admin_users.php',
302        'admin_users_bulk.php',
303        'ancestry.php',
304        'app/Controller',
305        'app/HitCounter.php',
306        'app/Module/ClippingsCart/ClippingsCartController.php',
307        'app/Module/FamiliesSidebarModule.php',
308        'app/Module/FamilyTreeFavorites',
309        'app/Module/GoogleMaps',
310        'app/Module/IndividualSidebarModule.php',
311        'app/Module/PageMenuModule.php',
312        'app/Query',
313        'app/SpecialChars',
314        'assets/js-1.7.7',
315        'assets/js-1.7.9',
316        'autocomplete.php',
317        'block_edit.php',
318        'branches.php',
319        'calendar.php',
320        'compact.php',
321        'data/html_purifier_cache',
322        'descendancy.php',
323        'editnews.php',
324        'edituser.php',
325        'edit_changes.php',
326        'edit_interface.php',
327        'expand_view.php',
328        'familybook.php',
329        'famlist.php',
330        'fanchart.php',
331        'find.php',
332        'help_text.php',
333        'hourglass.php',
334        'hourglass_ajax.php',
335        'import.php',
336        'includes',
337        'index_edit.php',
338        'indilist.php',
339        'inverselink.php',
340        'language',
341        'lifespan.php',
342        'login.php',
343        'logout.php',
344        'mediafirewall.php',
345        'medialist.php',
346        'message.php',
347        'module.php',
348        'modules_v3',
349        'notelist.php',
350        'packages',
351        'pedigree.php',
352        'relationship.php',
353        'repolist.php',
354        'reportengine.php',
355        'search.php',
356        'search_advanced.php',
357        'site-offline.php',
358        'site-unavailable.php',
359        'sourcelist.php',
360        'statistics.php',
361        'statisticsplot.php',
362        'themes/_administration',
363        'themes/_custom',
364        'themes/clouds/css-1.7.8',
365        'themes/clouds/jquery-ui-1.11.2',
366        'themes/colors/css-1.7.8',
367        'themes/colors/jquery-ui-1.11.2',
368        'themes/fab/css-1.7.8',
369        'themes/fab/jquery-ui-1.11.2',
370        'themes/minimal/css-1.7.8',
371        'themes/minimal/jquery-ui-1.11.2',
372        'themes/webtrees/css-1.7.8',
373        'themes/webtrees/jquery-ui-1.11.2',
374        'themes/xenea/css-1.7.8',
375        'themes/xenea/jquery-ui-1.11.2',
376        'timeline.php',
377    ];
378
379    /**
380     * Delete files and folders that belonged to an earlier version of webtrees.
381     * Return a list of those that we could not delete.
382     *
383     * @param FilesystemOperator $filesystem
384     *
385     * @return array<string>
386     */
387    public function deleteOldWebtreesFiles(FilesystemOperator $filesystem): array
388    {
389        $paths_to_delete = [];
390
391        foreach (self::OLD_PATHS as $path) {
392            if (!$this->deleteFileOrFolder($filesystem, $path)) {
393                $paths_to_delete[] = $path;
394            }
395        }
396
397        return $paths_to_delete;
398    }
399
400    /**
401     * Delete old cache files.
402     *
403     * @param FilesystemOperator $filesystem
404     * @param string             $path
405     * @param int                $max_age Seconds
406     *
407     * @return void
408     */
409    public function deleteOldFiles(FilesystemOperator $filesystem, string $path, int $max_age): void
410    {
411        $threshold = Carbon::now()->unix() - $max_age;
412
413        $list = $filesystem->listContents($path, Filesystem::LIST_DEEP);
414
415        foreach ($list as $metadata) {
416            // The timestamp can be absent or false.
417            $timestamp = $metadata['timestamp'] ?? false;
418
419            if ($timestamp !== false && $timestamp < $threshold) {
420                $this->deleteFileOrFolder($filesystem, $metadata['path']);
421            }
422        }
423    }
424
425    /**
426     * @param int $max_age_in_seconds
427     *
428     * @return void
429     */
430    public function deleteOldLogs(int $max_age_in_seconds): void
431    {
432        $timestamp = Carbon::now()->subSeconds($max_age_in_seconds);
433
434        DB::table('log')
435            ->whereIn('log_type', ['error', 'media'])
436            ->where('log_time', '<', $timestamp)
437            ->delete();
438    }
439
440    /**
441     * @param int $max_age_in_seconds
442     *
443     * @return void
444     */
445    public function deleteOldSessions(int $max_age_in_seconds): void
446    {
447        $timestamp = Carbon::now()->subSeconds($max_age_in_seconds);
448
449        DB::table('session')
450            ->where('session_time', '<', $timestamp)
451            ->delete();
452    }
453
454    /**
455     * Delete a file or folder, if we can.
456     *
457     * @param FilesystemOperator $filesystem
458     * @param string             $path
459     *
460     * @return bool
461     */
462    private function deleteFileOrFolder(FilesystemOperator $filesystem, string $path): bool
463    {
464        try {
465            $filesystem->delete($path);
466        } catch (FilesystemException | UnableToDeleteFile $ex) {
467            try {
468                $filesystem->deleteDirectory($path);
469            } catch (FilesystemException | UnableToDeleteDirectory $ex) {
470                return false;
471            }
472        }
473
474        return true;
475    }
476}
477