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 return Collection::make(self::CORE_MODULES) 277 ->map(function (string $class, string $name): ModuleInterface { 278 $module = app($class); 279 280 $module->setName($name); 281 282 return $module; 283 }); 284 } 285 286 /** 287 * All custom modules in the system. Custom modules are defined in modules_v4/ 288 * 289 * @return Collection 290 */ 291 private function customModules(): Collection 292 { 293 $pattern = WT_ROOT . Webtrees::MODULES_PATH . '*/module.php'; 294 $filenames = glob($pattern); 295 296 return Collection::make($filenames) 297 ->filter(function (string $filename): bool { 298 // Special characters will break PHP variable names. 299 // This also allows us to ignore modules called "foo.example" and "foo.disable" 300 $module_name = basename(dirname($filename)); 301 302 return !Str::contains($module_name, ['.', ' ', '[', ']']) && Str::length($module_name) <= 30; 303 }) 304 ->map(function (string $filename): ?ModuleCustomInterface { 305 try { 306 $module = self::load($filename); 307 308 if ($module instanceof ModuleCustomInterface) { 309 $module_name = '_' . basename(dirname($filename)) . '_'; 310 311 $module->setName($module_name); 312 } else { 313 return null; 314 } 315 316 return $module; 317 } catch (Throwable $ex) { 318 $message = '<pre>' . e($ex->getMessage()) . "\n" . e($ex->getTraceAsString()) . '</pre>'; 319 FlashMessages::addMessage($message, 'danger'); 320 321 return null; 322 } 323 }) 324 ->filter(); 325 } 326 327 /** 328 * All modules. 329 * 330 * @return Collection|ModuleInterface[] 331 */ 332 public function all(): Collection 333 { 334 return app('cache.array')->rememberForever('all_modules', function (): Collection { 335 // Modules have a default status, order etc. 336 // We can override these from database settings. 337 $module_info = DB::table('module') 338 ->get() 339 ->mapWithKeys(function (stdClass $row): array { 340 return [$row->module_name => $row]; 341 }); 342 343 return $this->coreModules() 344 ->merge($this->customModules()) 345 ->map(function (ModuleInterface $module) use ($module_info): ModuleInterface { 346 $info = $module_info->get($module->name()); 347 348 if ($info instanceof stdClass) { 349 $module->setEnabled($info->status === 'enabled'); 350 351 if ($module instanceof ModuleFooterInterface && $info->footer_order !== null) { 352 $module->setFooterOrder((int) $info->footer_order); 353 } 354 355 if ($module instanceof ModuleMenuInterface && $info->menu_order !== null) { 356 $module->setMenuOrder((int) $info->menu_order); 357 } 358 359 if ($module instanceof ModuleSidebarInterface && $info->sidebar_order !== null) { 360 $module->setSidebarOrder((int) $info->sidebar_order); 361 } 362 363 if ($module instanceof ModuleTabInterface && $info->tab_order !== null) { 364 $module->setTabOrder((int) $info->tab_order); 365 } 366 } else { 367 $module->setEnabled($module->isEnabledByDefault()); 368 369 DB::table('module')->insert([ 370 'module_name' => $module->name(), 371 'status' => $module->isEnabled() ? 'enabled' : 'disabled', 372 ]); 373 } 374 375 return $module; 376 }); 377 }); 378 } 379 380 /** 381 * Load a module in a static scope, to prevent it from modifying local or object variables. 382 * 383 * @param string $filename 384 * 385 * @return mixed 386 */ 387 private static function load(string $filename) 388 { 389 return include $filename; 390 } 391 392 /** 393 * A function to sort modules by name 394 * 395 * @return Closure 396 */ 397 private function moduleSorter(): Closure 398 { 399 return function (ModuleInterface $x, ModuleInterface $y): int { 400 return I18N::strcasecmp($x->title(), $y->title()); 401 }; 402 } 403 404 /** 405 * A function to sort footers 406 * 407 * @return Closure 408 */ 409 private function footerSorter(): Closure 410 { 411 return function (ModuleFooterInterface $x, ModuleFooterInterface $y): int { 412 return $x->getFooterOrder() <=> $y->getFooterOrder(); 413 }; 414 } 415 416 /** 417 * A function to sort menus 418 * 419 * @return Closure 420 */ 421 private function menuSorter(): Closure 422 { 423 return function (ModuleMenuInterface $x, ModuleMenuInterface $y): int { 424 return $x->getMenuOrder() <=> $y->getMenuOrder(); 425 }; 426 } 427 428 /** 429 * A function to sort menus 430 * 431 * @return Closure 432 */ 433 private function sidebarSorter(): Closure 434 { 435 return function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int { 436 return $x->getSidebarOrder() <=> $y->getSidebarOrder(); 437 }; 438 } 439 440 /** 441 * A function to sort menus 442 * 443 * @return Closure 444 */ 445 private function tabSorter(): Closure 446 { 447 return function (ModuleTabInterface $x, ModuleTabInterface $y): int { 448 return $x->getTabOrder() <=> $y->getTabOrder(); 449 }; 450 } 451 452 /** 453 * A function filter modules by type 454 * 455 * @param string $interface 456 * 457 * @return Closure 458 */ 459 private function interfaceFilter(string $interface): Closure 460 { 461 return function (ModuleInterface $module) use ($interface): bool { 462 return $module instanceof $interface; 463 }; 464 } 465 466 /** 467 * A function filter modules by enabled/disabled 468 * 469 * @param bool $include_disabled 470 * 471 * @return Closure 472 */ 473 private function enabledFilter(bool $include_disabled): Closure 474 { 475 return function (ModuleInterface $module) use ($include_disabled): bool { 476 return $include_disabled || $module->isEnabled(); 477 }; 478 } 479 480 /** 481 * A function to convert modules into their titles - to create option lists, etc. 482 * 483 * @return Closure 484 */ 485 public function titleMapper(): Closure 486 { 487 return function (ModuleInterface $module): string { 488 return $module->title(); 489 }; 490 } 491 492 /** 493 * Modules which (a) provide a specific function and (b) we have permission to see. 494 * 495 * @param string $interface 496 * @param Tree $tree 497 * @param UserInterface $user 498 * 499 * @return Collection|ModuleInterface[] 500 */ 501 public function findByComponent(string $interface, Tree $tree, UserInterface $user): Collection 502 { 503 return $this->findByInterface($interface, false, true) 504 ->filter(function (ModuleInterface $module) use ($interface, $tree, $user): bool { 505 return $module->accessLevel($tree, $interface) >= Auth::accessLevel($tree, $user); 506 }); 507 } 508 509 /** 510 * All modules which provide a specific function. 511 * 512 * @param string $interface 513 * @param bool $include_disabled 514 * @param bool $sort 515 * 516 * @return Collection|ModuleInterface[] 517 */ 518 public function findByInterface(string $interface, $include_disabled = false, $sort = false): Collection 519 { 520 $modules = $this->all() 521 ->filter($this->interfaceFilter($interface)) 522 ->filter($this->enabledFilter($include_disabled)); 523 524 switch ($interface) { 525 case ModuleFooterInterface::class: 526 return $modules->sort($this->footerSorter()); 527 528 case ModuleMenuInterface::class: 529 return $modules->sort($this->menuSorter()); 530 531 case ModuleSidebarInterface::class: 532 return $modules->sort($this->sidebarSorter()); 533 534 case ModuleTabInterface::class: 535 return $modules->sort($this->tabSorter()); 536 537 default: 538 if ($sort) { 539 return $modules->sort($this->moduleSorter()); 540 } 541 542 return $modules; 543 } 544 } 545 546 /** 547 * Find a specified module, if it is currently active. 548 * 549 * @param string $module_name 550 * @param bool $include_disabled 551 * 552 * @return ModuleInterface|null 553 */ 554 public function findByName(string $module_name, bool $include_disabled = false): ?ModuleInterface 555 { 556 return $this->all() 557 ->filter($this->enabledFilter($include_disabled)) 558 ->filter(function (ModuleInterface $module) use ($module_name): bool { 559 return $module->name() === $module_name; 560 }) 561 ->first(); 562 } 563 564 /** 565 * Configuration settings are available through the various "module component" pages. 566 * For modules that do not provide a component, we need to list them separately. 567 * 568 * @return Collection|ModuleConfigInterface[] 569 */ 570 public function configOnlyModules(): Collection 571 { 572 return $this->findByInterface(ModuleConfigInterface::class) 573 ->filter(function (ModuleConfigInterface $module): bool { 574 foreach (self::COMPONENTS as $interface) { 575 if ($module instanceof $interface) { 576 return false; 577 } 578 } 579 580 return true; 581 }); 582 } 583 584 /** 585 * Generate a list of module names which exist in the database but not on disk. 586 * 587 * @return Collection|string[] 588 */ 589 public function deletedModules(): Collection 590 { 591 $database_modules = DB::table('module')->pluck('module_name'); 592 593 $disk_modules = $this->all() 594 ->map(function (ModuleInterface $module): string { 595 return $module->name(); 596 }); 597 598 return $database_modules->diff($disk_modules); 599 } 600} 601