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