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