xref: /webtrees/app/Services/HousekeepingService.php (revision da58fa21ddd29f92b1fde63c8ee425cf3d7f4eee)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Services;
19
20use Carbon\Carbon;
21use Exception;
22use Illuminate\Database\Capsule\Manager as DB;
23use League\Flysystem\Filesystem;
24
25/**
26 * Clean up old data, files and folders.
27 */
28class HousekeepingService
29{
30    // This is a list of old files and directories, from earlier versions of webtrees.
31    // git diff 1.7.9..master --name-status | grep ^D
32    private const OLD_PATHS = [
33        // Removed in 1.0.2
34        'language/en.mo',
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        //WT_ROOT.'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        //WT_ROOT.'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        'language/en_GB.mo',
214        // Replaced with en-GB.mo
215        'language/en_US.mo',
216        // Replaced with en-US.mo
217        'language/pt_BR.mo',
218        // Replaced with pt-BR.mo
219        'language/zh_CN.mo',
220        // Replaced with zh-Hans.mo
221        'language/extra',
222        'library',
223        'save.php',
224        'themes/clouds/css-1.6.2',
225        'themes/clouds/templates',
226        'themes/clouds/header.php',
227        'themes/clouds/footer.php',
228        'themes/colors/css-1.6.2',
229        'themes/colors/templates',
230        'themes/colors/header.php',
231        'themes/colors/footer.php',
232        'themes/fab/css-1.6.2',
233        'themes/fab/templates',
234        'themes/fab/header.php',
235        'themes/fab/footer.php',
236        'themes/minimal/css-1.6.2',
237        'themes/minimal/templates',
238        'themes/minimal/header.php',
239        'themes/minimal/footer.php',
240        'themes/webtrees/css-1.6.2',
241        'themes/webtrees/templates',
242        'themes/webtrees/header.php',
243        'themes/webtrees/footer.php',
244        'themes/xenea/css-1.6.2',
245        'themes/xenea/templates',
246        'themes/xenea/header.php',
247        'themes/xenea/footer.php',
248        // Removed in 1.7.2
249        'assets/js-1.7.0',
250        // Removed in 1.7.4
251        'assets/js-1.7.2',
252        'themes/clouds/css-1.7.0',
253        'themes/colors/css-1.7.0',
254        'themes/fab/css-1.7.0',
255        'themes/minimal/css-1.7.0',
256        'themes/webtrees/css-1.7.0',
257        'themes/xenea/css-1.7.0',
258        // Removed in 1.7.5
259        'themes/clouds/css-1.7.4',
260        'themes/colors/css-1.7.4',
261        'themes/fab/css-1.7.4',
262        'themes/minimal/css-1.7.4',
263        'themes/webtrees/css-1.7.4',
264        'themes/xenea/css-1.7.4',
265        // Removed in 1.7.7
266        'assets/js-1.7.4',
267        // Removed in 1.7.8
268        'themes/clouds/css-1.7.5',
269        'themes/colors/css-1.7.5',
270        'themes/fab/css-1.7.5',
271        'themes/minimal/css-1.7.5',
272        'themes/webtrees/css-1.7.5',
273        'themes/xenea/css-1.7.5',
274        // Removed in 2.0.0
275        'action.php',
276        'addmedia.php',
277        'addmin.php',
278        'admin_media.php',
279        'admin_media_upload.php',
280        'admin_module_blocks.php',
281        'admin_module_charts.php',
282        'admin_module_menus.php',
283        'admin_module_reports.php',
284        'admin_module_sidebar.php',
285        'admin_module_tabs.php',
286        'admin_modules.php',
287        'admin_pgv_to_wt.php',
288        'admin_site_access.php',
289        'admin_site_change.php',
290        'admin_site_clean.php',
291        'admin_site_config.php',
292        'admin_site_info.php',
293        'admin_site_logs.php',
294        'admin_site_merge.php',
295        'admin_site_readme.php',
296        'admin_site_upgrade.php',
297        'admin_trees_check.php',
298        'admin_trees_config.php',
299        'admin_trees_download.php',
300        'admin_trees_duplicates.php',
301        'admin_trees_export.php',
302        'admin_trees_manage.php',
303        'admin_trees_merge.php',
304        'admin_trees_places.php',
305        'admin_trees_renumber.php',
306        'admin_trees_unconnected.php',
307        'admin_users.php',
308        'admin_users_bulk.php',
309        'ancestry.php',
310        'app/Controller',
311        'app/HitCounter.php',
312        'app/Module/ClippingsCart/ClippingsCartController.php',
313        'app/Module/FamiliesSidebarModule.php',
314        'app/Module/FamilyTreeFavorites',
315        'app/Module/GoogleMaps',
316        'app/Module/IndividualSidebarModule.php',
317        'app/Module/PageMenuModule.php',
318        'app/Query',
319        'app/SpecialChars',
320        'assets/js-1.7.7',
321        'assets/js-1.7.9',
322        'autocomplete.php',
323        'block_edit.php',
324        'branches.php',
325        'calendar.php',
326        'compact.php',
327        'data/html_purifier_cache',
328        'descendancy.php',
329        'editnews.php',
330        'edituser.php',
331        'edit_changes.php',
332        'edit_interface.php',
333        'expand_view.php',
334        'familybook.php',
335        'famlist.php',
336        'fanchart.php',
337        'find.php',
338        'help_text.php',
339        'hourglass.php',
340        'hourglass_ajax.php',
341        'import.php',
342        'includes',
343        'index_edit.php',
344        'indilist.php',
345        'inverselink.php',
346        'lifespan.php',
347        'login.php',
348        'logout.php',
349        'mediafirewall.php',
350        'medialist.php',
351        'message.php',
352        'module.php',
353        'modules_v3',
354        'notelist.php',
355        'packages',
356        'pedigree.php',
357        'relationship.php',
358        'repolist.php',
359        'reportengine.php',
360        'search.php',
361        'search_advanced.php',
362        'site-offline.php',
363        'site-unavailable.php',
364        'sourcelist.php',
365        'statistics.php',
366        'statisticsplot.php',
367        'themes/_administration',
368        'themes/_custom',
369        'themes/clouds/css-1.7.8',
370        'themes/clouds/jquery-ui-1.11.2',
371        'themes/colors/css-1.7.8',
372        'themes/colors/jquery-ui-1.11.2',
373        'themes/fab/css-1.7.8',
374        'themes/fab/jquery-ui-1.11.2',
375        'themes/minimal/css-1.7.8',
376        'themes/minimal/jquery-ui-1.11.2',
377        'themes/webtrees/css-1.7.8',
378        'themes/webtrees/jquery-ui-1.11.2',
379        'themes/xenea/css-1.7.8',
380        'themes/xenea/jquery-ui-1.11.2',
381        'timeline.php',
382    ];
383
384    /**
385     * Delete files and folders that belonged to an earlier version of webtrees.
386     * Return a list of those that we could not delete.
387     *
388     * @param Filesystem $filesystem
389     *
390     * @return array
391     */
392    public function deleteOldWebtreesFiles(Filesystem $filesystem): array
393    {
394        $paths_to_delete = [];
395
396        foreach (self::OLD_PATHS as $path) {
397            if (!$this->deleteFileOrFolder($filesystem, $path)) {
398                $paths_to_delete[] = $path;
399            }
400        }
401
402        return $paths_to_delete;
403    }
404
405    /**
406     * Delete old cache files.
407     *
408     * @param Filesystem $filesystem
409     * @param string     $path
410     * @param int        $max_age    Seconds
411     *
412     * @return void
413     */
414    public function deleteOldFiles(Filesystem $filesystem, string $path, int $max_age)
415    {
416        $list = $filesystem->listContents($path, true);
417
418        foreach ($list as $metadata) {
419            if ($metadata['timestamp'] ?? WT_TIMESTAMP < WT_TIMESTAMP - $max_age) {
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)
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)
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 Filesystem $filesystem
458     * @param string     $path
459     *
460     * @return bool
461     */
462    private function deleteFileOrFolder(Filesystem $filesystem, string $path): bool
463    {
464        if ($filesystem->has($path)) {
465            try {
466                $metadata = $filesystem->getMetadata($path);
467
468                if ($metadata['type'] === 'dir') {
469                    $filesystem->deleteDir($path);
470                }
471
472                if ($metadata['type'] === 'file') {
473                    $filesystem->delete($path);
474                }
475            } catch (Exception $ex) {
476                return false;
477            }
478        }
479
480        return true;
481    }
482}
483