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 Closure; 21use Fisharebest\Webtrees\Auth; 22use Fisharebest\Webtrees\Contracts\UserInterface; 23use Fisharebest\Webtrees\FlashMessages; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Module\AhnentafelReportModule; 26use Fisharebest\Webtrees\Module\AlbumModule; 27use Fisharebest\Webtrees\Module\AncestorsChartModule; 28use Fisharebest\Webtrees\Module\BatchUpdateModule; 29use Fisharebest\Webtrees\Module\BingWebmasterToolsModule; 30use Fisharebest\Webtrees\Module\BirthDeathMarriageReportModule; 31use Fisharebest\Webtrees\Module\BirthReportModule; 32use Fisharebest\Webtrees\Module\BranchesListModule; 33use Fisharebest\Webtrees\Module\CalendarMenuModule; 34use Fisharebest\Webtrees\Module\CemeteryReportModule; 35use Fisharebest\Webtrees\Module\CensusAssistantModule; 36use Fisharebest\Webtrees\Module\ChangeReportModule; 37use Fisharebest\Webtrees\Module\ChartsBlockModule; 38use Fisharebest\Webtrees\Module\ChartsMenuModule; 39use Fisharebest\Webtrees\Module\CkeditorModule; 40use Fisharebest\Webtrees\Module\ClippingsCartModule; 41use Fisharebest\Webtrees\Module\CloudsTheme; 42use Fisharebest\Webtrees\Module\ColorsTheme; 43use Fisharebest\Webtrees\Module\CompactTreeChartModule; 44use Fisharebest\Webtrees\Module\ContactsFooterModule; 45use Fisharebest\Webtrees\Module\CookieWarningModule; 46use Fisharebest\Webtrees\Module\DeathReportModule; 47use Fisharebest\Webtrees\Module\DescendancyChartModule; 48use Fisharebest\Webtrees\Module\DescendancyModule; 49use Fisharebest\Webtrees\Module\DescendancyReportModule; 50use Fisharebest\Webtrees\Module\IndividualMetadataModule; 51use Fisharebest\Webtrees\Module\FabTheme; 52use Fisharebest\Webtrees\Module\FactSourcesReportModule; 53use Fisharebest\Webtrees\Module\FamilyBookChartModule; 54use Fisharebest\Webtrees\Module\FamilyGroupReportModule; 55use Fisharebest\Webtrees\Module\FamilyListModule; 56use Fisharebest\Webtrees\Module\FamilyNavigatorModule; 57use Fisharebest\Webtrees\Module\FamilyTreeFavoritesModule; 58use Fisharebest\Webtrees\Module\FamilyTreeNewsModule; 59use Fisharebest\Webtrees\Module\FamilyTreeStatisticsModule; 60use Fisharebest\Webtrees\Module\FanChartModule; 61use Fisharebest\Webtrees\Module\FrequentlyAskedQuestionsModule; 62use Fisharebest\Webtrees\Module\GoogleAnalyticsModule; 63use Fisharebest\Webtrees\Module\GoogleWebmasterToolsModule; 64use Fisharebest\Webtrees\Module\HitCountFooterModule; 65use Fisharebest\Webtrees\Module\HourglassChartModule; 66use Fisharebest\Webtrees\Module\HtmlBlockModule; 67use Fisharebest\Webtrees\Module\IndividualFactsTabModule; 68use Fisharebest\Webtrees\Module\IndividualFamiliesReportModule; 69use Fisharebest\Webtrees\Module\IndividualListModule; 70use Fisharebest\Webtrees\Module\IndividualReportModule; 71use Fisharebest\Webtrees\Module\InteractiveTreeModule; 72use Fisharebest\Webtrees\Module\LifespansChartModule; 73use Fisharebest\Webtrees\Module\ListsMenuModule; 74use Fisharebest\Webtrees\Module\LoggedInUsersModule; 75use Fisharebest\Webtrees\Module\LoginBlockModule; 76use Fisharebest\Webtrees\Module\MarriageReportModule; 77use Fisharebest\Webtrees\Module\MatomoAnalyticsModule; 78use Fisharebest\Webtrees\Module\MediaListModule; 79use Fisharebest\Webtrees\Module\MediaTabModule; 80use Fisharebest\Webtrees\Module\MinimalTheme; 81use Fisharebest\Webtrees\Module\MissingFactsReportModule; 82use Fisharebest\Webtrees\Module\ModuleAnalyticsInterface; 83use Fisharebest\Webtrees\Module\ModuleBlockInterface; 84use Fisharebest\Webtrees\Module\ModuleChartInterface; 85use Fisharebest\Webtrees\Module\ModuleConfigInterface; 86use Fisharebest\Webtrees\Module\ModuleCustomInterface; 87use Fisharebest\Webtrees\Module\ModuleFooterInterface; 88use Fisharebest\Webtrees\Module\ModuleHistoricEventsInterface; 89use Fisharebest\Webtrees\Module\ModuleInterface; 90use Fisharebest\Webtrees\Module\ModuleLanguageInterface; 91use Fisharebest\Webtrees\Module\ModuleListInterface; 92use Fisharebest\Webtrees\Module\ModuleMenuInterface; 93use Fisharebest\Webtrees\Module\ModuleReportInterface; 94use Fisharebest\Webtrees\Module\ModuleSidebarInterface; 95use Fisharebest\Webtrees\Module\ModuleTabInterface; 96use Fisharebest\Webtrees\Module\ModuleThemeInterface; 97use Fisharebest\Webtrees\Module\NoteListModule; 98use Fisharebest\Webtrees\Module\NotesTabModule; 99use Fisharebest\Webtrees\Module\OccupationReportModule; 100use Fisharebest\Webtrees\Module\OnThisDayModule; 101use Fisharebest\Webtrees\Module\PedigreeChartModule; 102use Fisharebest\Webtrees\Module\PedigreeMapModule; 103use Fisharebest\Webtrees\Module\PedigreeReportModule; 104use Fisharebest\Webtrees\Module\PlaceHierarchyListModule; 105use Fisharebest\Webtrees\Module\PlacesModule; 106use Fisharebest\Webtrees\Module\PoweredByWebtreesModule; 107use Fisharebest\Webtrees\Module\RecentChangesModule; 108use Fisharebest\Webtrees\Module\RelatedIndividualsReportModule; 109use Fisharebest\Webtrees\Module\RelationshipsChartModule; 110use Fisharebest\Webtrees\Module\RelativesTabModule; 111use Fisharebest\Webtrees\Module\ReportsMenuModule; 112use Fisharebest\Webtrees\Module\RepositoryListModule; 113use Fisharebest\Webtrees\Module\ResearchTaskModule; 114use Fisharebest\Webtrees\Module\ReviewChangesModule; 115use Fisharebest\Webtrees\Module\SearchMenuModule; 116use Fisharebest\Webtrees\Module\SiteMapModule; 117use Fisharebest\Webtrees\Module\SlideShowModule; 118use Fisharebest\Webtrees\Module\SourceListModule; 119use Fisharebest\Webtrees\Module\SourcesTabModule; 120use Fisharebest\Webtrees\Module\StatcounterModule; 121use Fisharebest\Webtrees\Module\StatisticsChartModule; 122use Fisharebest\Webtrees\Module\StoriesModule; 123use Fisharebest\Webtrees\Module\ThemeSelectModule; 124use Fisharebest\Webtrees\Module\TimelineChartModule; 125use Fisharebest\Webtrees\Module\TopGivenNamesModule; 126use Fisharebest\Webtrees\Module\TopPageViewsModule; 127use Fisharebest\Webtrees\Module\TopSurnamesModule; 128use Fisharebest\Webtrees\Module\TreesMenuModule; 129use Fisharebest\Webtrees\Module\UpcomingAnniversariesModule; 130use Fisharebest\Webtrees\Module\UserFavoritesModule; 131use Fisharebest\Webtrees\Module\UserJournalModule; 132use Fisharebest\Webtrees\Module\UserMessagesModule; 133use Fisharebest\Webtrees\Module\UserWelcomeModule; 134use Fisharebest\Webtrees\Module\WebtreesTheme; 135use Fisharebest\Webtrees\Module\WelcomeBlockModule; 136use Fisharebest\Webtrees\Module\XeneaTheme; 137use Fisharebest\Webtrees\Module\YahrzeitModule; 138use Fisharebest\Webtrees\Tree; 139use Fisharebest\Webtrees\Webtrees; 140use Illuminate\Database\Capsule\Manager as DB; 141use Illuminate\Support\Collection; 142use Illuminate\Support\Str; 143use stdClass; 144use Throwable; 145 146/** 147 * Functions for managing and maintaining modules. 148 */ 149class ModuleService 150{ 151 // Components are managed together in the control panel. 152 private const COMPONENTS = [ 153 ModuleAnalyticsInterface::class, 154 ModuleBlockInterface::class, 155 ModuleChartInterface::class, 156 ModuleFooterInterface::class, 157 ModuleHistoricEventsInterface::class, 158 ModuleLanguageInterface::class, 159 ModuleListInterface::class, 160 ModuleMenuInterface::class, 161 ModuleReportInterface::class, 162 ModuleSidebarInterface::class, 163 ModuleTabInterface::class, 164 ModuleThemeInterface::class, 165 ]; 166 167 // Array keys are module names, and should match module names from earlier versions of webtrees. 168 private const CORE_MODULES = [ 169 'GEDFact_assistant' => CensusAssistantModule::class, 170 'ahnentafel_report' => AhnentafelReportModule::class, 171 'ancestors_chart' => AncestorsChartModule::class, 172 'batch_update' => BatchUpdateModule::class, 173 'bdm_report' => BirthDeathMarriageReportModule::class, 174 'bing-webmaster-tools' => BingWebmasterToolsModule::class, 175 'birth_report' => BirthReportModule::class, 176 'branches_list' => BranchesListModule::class, 177 'calendar-menu' => CalendarMenuModule::class, 178 'cemetery_report' => CemeteryReportModule::class, 179 'change_report' => ChangeReportModule::class, 180 'charts' => ChartsBlockModule::class, 181 'charts-menu' => ChartsMenuModule::class, 182 'ckeditor' => CkeditorModule::class, 183 'clippings' => ClippingsCartModule::class, 184 'clouds' => CloudsTheme::class, 185 'colors' => ColorsTheme::class, 186 'compact-chart' => CompactTreeChartModule::class, 187 'contact-links' => ContactsFooterModule::class, 188 'cookie-warning' => CookieWarningModule::class, 189 'death_report' => DeathReportModule::class, 190 'descendancy' => DescendancyModule::class, 191 'descendancy_chart' => DescendancyChartModule::class, 192 'descendancy_report' => DescendancyReportModule::class, 193 'extra_info' => IndividualMetadataModule::class, 194 'fab' => FabTheme::class, 195 'fact_sources' => FactSourcesReportModule::class, 196 'family_book_chart' => FamilyBookChartModule::class, 197 'family_group_report' => FamilyGroupReportModule::class, 198 'family_list' => FamilyListModule::class, 199 'family_nav' => FamilyNavigatorModule::class, 200 'fan_chart' => FanChartModule::class, 201 'faq' => FrequentlyAskedQuestionsModule::class, 202 'gedcom_block' => WelcomeBlockModule::class, 203 'gedcom_favorites' => FamilyTreeFavoritesModule::class, 204 'gedcom_news' => FamilyTreeNewsModule::class, 205 'gedcom_stats' => FamilyTreeStatisticsModule::class, 206 'google-analytics' => GoogleAnalyticsModule::class, 207 'google-webmaster-tools' => GoogleWebmasterToolsModule::class, 208 'hit-counter' => HitCountFooterModule::class, 209 'hourglass_chart' => HourglassChartModule::class, 210 'html' => HtmlBlockModule::class, 211 'individual_ext_report' => IndividualFamiliesReportModule::class, 212 'individual_list' => IndividualListModule::class, 213 'individual_report' => IndividualReportModule::class, 214 'lifespans_chart' => LifespansChartModule::class, 215 'lightbox' => AlbumModule::class, 216 'lists-menu' => ListsMenuModule::class, 217 'logged_in' => LoggedInUsersModule::class, 218 'login_block' => LoginBlockModule::class, 219 'marriage_report' => MarriageReportModule::class, 220 'matomo-analytics' => MatomoAnalyticsModule::class, 221 'media' => MediaTabModule::class, 222 'media_list' => MediaListModule::class, 223 'minimal' => MinimalTheme::class, 224 'missing_facts_report' => MissingFactsReportModule::class, 225 'notes' => NotesTabModule::class, 226 'note_list' => NoteListModule::class, 227 'occupation_report' => OccupationReportModule::class, 228 'pedigree-map' => PedigreeMapModule::class, 229 'pedigree_chart' => PedigreeChartModule::class, 230 'pedigree_report' => PedigreeReportModule::class, 231 'personal_facts' => IndividualFactsTabModule::class, 232 'places' => PlacesModule::class, 233 'places_list' => PlaceHierarchyListModule::class, 234 'powered-by-webtrees' => PoweredByWebtreesModule::class, 235 'random_media' => SlideShowModule::class, 236 'recent_changes' => RecentChangesModule::class, 237 'relationships_chart' => RelationshipsChartModule::class, 238 'relative_ext_report' => RelatedIndividualsReportModule::class, 239 'relatives' => RelativesTabModule::class, 240 'reports-menu' => ReportsMenuModule::class, 241 'repository_list' => RepositoryListModule::class, 242 'review_changes' => ReviewChangesModule::class, 243 'search-menu' => SearchMenuModule::class, 244 'sitemap' => SiteMapModule::class, 245 'source_list' => SourceListModule::class, 246 'sources_tab' => SourcesTabModule::class, 247 'statcounter' => StatcounterModule::class, 248 'statistics_chart' => StatisticsChartModule::class, 249 'stories' => StoriesModule::class, 250 'theme_select' => ThemeSelectModule::class, 251 'timeline_chart' => TimelineChartModule::class, 252 'todays_events' => OnThisDayModule::class, 253 'todo' => ResearchTaskModule::class, 254 'top10_givnnames' => TopGivenNamesModule::class, 255 'top10_pageviews' => TopPageViewsModule::class, 256 'top10_surnames' => TopSurnamesModule::class, 257 'tree' => InteractiveTreeModule::class, 258 'trees-menu' => TreesMenuModule::class, 259 'upcoming_events' => UpcomingAnniversariesModule::class, 260 'user_blog' => UserJournalModule::class, 261 'user_favorites' => UserFavoritesModule::class, 262 'user_messages' => UserMessagesModule::class, 263 'user_welcome' => UserWelcomeModule::class, 264 'webtrees' => WebtreesTheme::class, 265 'xenea' => XeneaTheme::class, 266 'yahrzeit' => YahrzeitModule::class, 267 ]; 268 269 /** 270 * All core modules in the system. 271 * 272 * @return Collection 273 */ 274 private function coreModules(): Collection 275 { 276 $modules = new Collection(self::CORE_MODULES); 277 278 return $modules->map(function (string $class, string $name): ModuleInterface { 279 $module = app()->make($class); 280 281 $module->setName($name); 282 283 return $module; 284 }); 285 } 286 287 /** 288 * All custom modules in the system. Custom modules are defined in modules_v4/ 289 * 290 * @return Collection 291 */ 292 private function customModules(): Collection 293 { 294 $pattern = WT_ROOT . Webtrees::MODULES_PATH . '*/module.php'; 295 $filenames = glob($pattern); 296 297 return (new Collection($filenames)) 298 ->filter(function (string $filename): bool { 299 // Special characters will break PHP variable names. 300 // This also allows us to ignore modules called "foo.example" and "foo.disable" 301 $module_name = basename(dirname($filename)); 302 303 return !Str::contains($module_name, ['.', ' ', '[', ']']) && Str::length($module_name) <= 30; 304 }) 305 ->map(function (string $filename): ?ModuleCustomInterface { 306 try { 307 $module = self::load($filename); 308 309 if ($module instanceof ModuleCustomInterface) { 310 $module_name = '_' . basename(dirname($filename)) . '_'; 311 312 $module->setName($module_name); 313 } else { 314 return null; 315 } 316 317 return $module; 318 } catch (Throwable $ex) { 319 $message = '<pre>' . e($ex->getMessage()) . "\n" . e($ex->getTraceAsString()) . '</pre>'; 320 FlashMessages::addMessage($message, 'danger'); 321 322 return null; 323 } 324 }) 325 ->filter(); 326 } 327 328 /** 329 * All modules. 330 * 331 * @return Collection|ModuleInterface[] 332 */ 333 public function all(): Collection 334 { 335 return app('cache.array')->rememberForever('all_modules', function (): Collection { 336 // Modules have a default status, order etc. 337 // We can override these from database settings. 338 $module_info = DB::table('module') 339 ->get() 340 ->mapWithKeys(function (stdClass $row): array { 341 return [$row->module_name => $row]; 342 }); 343 344 return $this->coreModules() 345 ->merge($this->customModules()) 346 ->map(function (ModuleInterface $module) use ($module_info): ModuleInterface { 347 $info = $module_info->get($module->name()); 348 349 if ($info instanceof stdClass) { 350 $module->setEnabled($info->status === 'enabled'); 351 352 if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) { 353 $module->setFooterOrder((int) $info->footer_order); 354 } 355 356 if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) { 357 $module->setMenuOrder((int) $info->menu_order); 358 } 359 360 if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) { 361 $module->setSidebarOrder((int) $info->sidebar_order); 362 } 363 364 if ($module instanceof ModuleTabInterface && $info->tab_order !== null) { 365 $module->setTabOrder((int) $info->tab_order); 366 } 367 } else { 368 $module->setEnabled($module->isEnabledByDefault()); 369 370 DB::table('module')->insert([ 371 'module_name' => $module->name(), 372 'status' => $module->isEnabled() ? 'enabled' : 'disabled', 373 ]); 374 } 375 376 return $module; 377 }) 378 ->sort($this->moduleSorter()); 379 }); 380 } 381 382 /** 383 * Load a module in a static scope, to prevent it from modifying local or object variables. 384 * 385 * @param string $filename 386 * 387 * @return mixed 388 */ 389 private static function load(string $filename) 390 { 391 return include $filename; 392 } 393 394 /** 395 * A function to sort modules by name 396 * 397 * @return Closure 398 */ 399 private function moduleSorter(): Closure 400 { 401 return function (ModuleInterface $x, ModuleInterface $y): int { 402 return I18N::strcasecmp($x->title(), $y->title()); 403 }; 404 } 405 406 /** 407 * A function to sort footers 408 * 409 * @return Closure 410 */ 411 private function footerSorter(): Closure 412 { 413 return function (ModuleFooterInterface $x, ModuleFooterInterface $y): int { 414 return $x->getFooterOrder() <=> $y->getFooterOrder(); 415 }; 416 } 417 418 /** 419 * A function to sort menus 420 * 421 * @return Closure 422 */ 423 private function menuSorter(): Closure 424 { 425 return function (ModuleMenuInterface $x, ModuleMenuInterface $y): int { 426 return $x->getMenuOrder() <=> $y->getMenuOrder(); 427 }; 428 } 429 430 /** 431 * A function to sort menus 432 * 433 * @return Closure 434 */ 435 private function sidebarSorter(): Closure 436 { 437 return function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int { 438 return $x->getSidebarOrder() <=> $y->getSidebarOrder(); 439 }; 440 } 441 442 /** 443 * A function to sort menus 444 * 445 * @return Closure 446 */ 447 private function tabSorter(): Closure 448 { 449 return function (ModuleTabInterface $x, ModuleTabInterface $y): int { 450 return $x->getTabOrder() <=> $y->getTabOrder(); 451 }; 452 } 453 454 /** 455 * A function to convert modules into their titles - to create option lists, etc. 456 * 457 * @return Closure 458 */ 459 public function titleMapper(): Closure 460 { 461 return function (ModuleInterface $module): string { 462 return $module->title(); 463 }; 464 } 465 466 /** 467 * Modules which (a) provide a specific function and (b) we have permission to see. 468 * 469 * @param string $interface 470 * @param Tree $tree 471 * @param UserInterface $user 472 * 473 * @return Collection|ModuleInterface[] 474 */ 475 public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection 476 { 477 return $this->findByInterface($interface) 478 ->filter(function (ModuleInterface $module) use ($interface, $tree, $user): bool { 479 return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user); 480 }); 481 } 482 483 /** 484 * All modules which provide a specific function. 485 * 486 * @param string $interface 487 * @param bool $include_disabled 488 * 489 * @return Collection|ModuleInterface[] 490 */ 491 public function findByInterface(string $interface, $include_disabled = false): Collection 492 { 493 $modules = $this->all() 494 ->filter(function (ModuleInterface $module) use ($interface): bool { 495 return $module instanceof $interface; 496 }) 497 ->filter(function (ModuleInterface $module) use ($include_disabled): bool { 498 return $include_disabled || $module->isEnabled(); 499 }); 500 501 switch ($interface) { 502 case ModuleFooterInterface::class: 503 return $modules->sort($this->footerSorter()); 504 505 case ModuleMenuInterface::class: 506 return $modules->sort($this->menuSorter()); 507 508 case ModuleSidebarInterface::class: 509 return $modules->sort($this->sidebarSorter()); 510 511 case ModuleTabInterface::class: 512 return $modules->sort($this->tabSorter()); 513 } 514 515 return $modules; 516 } 517 518 /** 519 * Find a specified module, if it is currently active. 520 * 521 * @param string $module_name 522 * 523 * @return ModuleInterface|null 524 */ 525 public function findByName(string $module_name): ?ModuleInterface 526 { 527 return $this->all() 528 ->filter(function (ModuleInterface $module) use ($module_name): bool { 529 return $module->isEnabled() && $module->name() === $module_name; 530 }) 531 ->first(); 532 } 533 534 /** 535 * Find a specified module, if it is currently active. 536 * 537 * @param string $class_name 538 * 539 * @return ModuleInterface|null 540 */ 541 public function findByClass(string $class_name): ?ModuleInterface 542 { 543 return $this->all() 544 ->filter(function (ModuleInterface $module) use ($class_name): bool { 545 return $module->isEnabled() && $module instanceof $class_name; 546 }) 547 ->first(); 548 } 549 550 /** 551 * Configuration settings are available through the various "module component" pages. 552 * For modules that do not provide a component, we need to list them separately. 553 * 554 * @return Collection|ModuleConfigInterface[] 555 */ 556 public function configOnlyModules(): Collection 557 { 558 return $this->findByInterface(ModuleConfigInterface::class) 559 ->filter(function (ModuleConfigInterface $module): bool { 560 foreach (self::COMPONENTS as $interface) { 561 if ($module instanceof $interface) { 562 return false; 563 } 564 } 565 566 return true; 567 }); 568 } 569 570 /** 571 * Generate a list of module names which exist in the database but not on disk. 572 * 573 * @return Collection|string[] 574 */ 575 public function deletedModules(): Collection 576 { 577 $database_modules = DB::table('module')->pluck('module_name'); 578 579 $disk_modules = $this->all() 580 ->map(function (ModuleInterface $module): string { 581 return $module->name(); 582 }); 583 584 return $database_modules->diff($disk_modules); 585 } 586} 587