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