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\ExtraInformationModule; 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' => ExtraInformationModule::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 DB::table('module')->insert(['module_name' => $module->name()]); 351 } 352 353 return $module; 354 }) 355 ->sort($this->moduleSorter()); 356 }); 357 } 358 359 /** 360 * Load a module in a static scope, to prevent it from modifying local or object variables. 361 * 362 * @param string $filename 363 * 364 * @return mixed 365 */ 366 private static function load(string $filename) 367 { 368 return include $filename; 369 } 370 371 /** 372 * A function to sort modules by name 373 * 374 * @return Closure 375 */ 376 private function moduleSorter(): Closure 377 { 378 return function (ModuleInterface $x, ModuleInterface $y): int { 379 return I18N::strcasecmp($x->title(), $y->title()); 380 }; 381 } 382 383 /** 384 * A function to sort footers 385 * 386 * @return Closure 387 */ 388 private function footerSorter(): Closure 389 { 390 return function (ModuleFooterInterface $x, ModuleFooterInterface $y): int { 391 return $x->getFooterOrder() <=> $y->getFooterOrder(); 392 }; 393 } 394 395 /** 396 * A function to sort menus 397 * 398 * @return Closure 399 */ 400 private function menuSorter(): Closure 401 { 402 return function (ModuleMenuInterface $x, ModuleMenuInterface $y): int { 403 return $x->getMenuOrder() <=> $y->getMenuOrder(); 404 }; 405 } 406 407 /** 408 * A function to sort menus 409 * 410 * @return Closure 411 */ 412 private function sidebarSorter(): Closure 413 { 414 return function (ModuleSidebarInterface $x, ModuleSidebarInterface $y): int { 415 return $x->getSidebarOrder() <=> $y->getSidebarOrder(); 416 }; 417 } 418 419 /** 420 * A function to sort menus 421 * 422 * @return Closure 423 */ 424 private function tabSorter(): Closure 425 { 426 return function (ModuleTabInterface $x, ModuleTabInterface $y): int { 427 return $x->getTabOrder() <=> $y->getTabOrder(); 428 }; 429 } 430 431 /** 432 * A function to convert modules into their titles - to create option lists, etc. 433 * 434 * @return Closure 435 */ 436 public function titleMapper(): Closure 437 { 438 return function (ModuleInterface $module): string { 439 return $module->title(); 440 }; 441 } 442 443 /** 444 * Modules which (a) provide a specific function and (b) we have permission to see. 445 * 446 * @param string $component 447 * @param Tree $tree 448 * @param UserInterface $user 449 * 450 * @return Collection|ModuleInterface[] 451 */ 452 public function findByComponent(string $component, Tree $tree, UserInterface $user): Collection 453 { 454 $interface = self::COMPONENTS[$component]; 455 456 return $this->findByInterface($interface) 457 ->filter(function (ModuleInterface $module) use ($component, $tree, $user): bool { 458 return $module->accessLevel($tree, $component) >= Auth::accessLevel($tree, $user); 459 }); 460 } 461 462 /** 463 * All modules which provide a specific function. 464 * 465 * @param string $interface 466 * @param bool $include_disabled 467 * 468 * @return Collection|ModuleInterface[] 469 */ 470 public function findByInterface(string $interface, $include_disabled = false): Collection 471 { 472 $modules = $this->all() 473 ->filter(function (ModuleInterface $module) use ($interface): bool { 474 return $module instanceof $interface; 475 }) 476 ->filter(function (ModuleInterface $module) use ($include_disabled): bool { 477 return $include_disabled || $module->isEnabled(); 478 }); 479 480 switch ($interface) { 481 case ModuleFooterInterface::class: 482 return $modules->sort($this->footerSorter()); 483 484 case ModuleMenuInterface::class: 485 return $modules->sort($this->menuSorter()); 486 487 case ModuleSidebarInterface::class: 488 return $modules->sort($this->sidebarSorter()); 489 490 case ModuleTabInterface::class: 491 return $modules->sort($this->tabSorter()); 492 } 493 494 return $modules; 495 } 496 497 /** 498 * Find a specified module, if it is currently active. 499 * 500 * @param string $module_name 501 * 502 * @return ModuleInterface|null 503 */ 504 public function findByName(string $module_name): ?ModuleInterface 505 { 506 return $this->all() 507 ->filter(function (ModuleInterface $module) use ($module_name): bool { 508 return $module->isEnabled() && $module->name() === $module_name; 509 }) 510 ->first(); 511 } 512 513 /** 514 * Find a specified module, if it is currently active. 515 * 516 * @param string $class_name 517 * 518 * @return ModuleInterface|null 519 */ 520 public function findByClass(string $class_name): ?ModuleInterface 521 { 522 return $this->all() 523 ->filter(function (ModuleInterface $module) use ($class_name): bool { 524 return $module->isEnabled() && $module instanceof $class_name; 525 }) 526 ->first(); 527 } 528 529 /** 530 * Configuration settings are available through the various "module component" pages. 531 * For modules that do not provide a component, we need to list them separately. 532 * 533 * @param string $class_name 534 * 535 * @return Collection|ModuleConfigInterface[] 536 */ 537 public function configOnlyModules(): Collection 538 { 539 return $this->findByInterface(ModuleConfigInterface::class) 540 ->filter(function (ModuleConfigInterface $module): bool { 541 foreach (self::COMPONENTS as $interface) { 542 if ($module instanceof $interface) { 543 return false; 544 } 545 } 546 547 return true; 548 }); 549 } 550 551 /** 552 * Generate a list of module names which exist in the database but not on disk. 553 * 554 * @return Collection|string[] 555 */ 556 public function deletedModules(): Collection 557 { 558 $database_modules = DB::table('module')->pluck('module_name'); 559 560 $disk_modules = $this->all() 561 ->map(function (ModuleInterface $module): string { 562 return $module->name(); 563 }); 564 565 return $database_modules->diff($disk_modules); 566 } 567} 568