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