1
0

PhpModules.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\SetupChecks;
  8. use OCP\IL10N;
  9. use OCP\IURLGenerator;
  10. use OCP\SetupCheck\ISetupCheck;
  11. use OCP\SetupCheck\SetupResult;
  12. class PhpModules implements ISetupCheck {
  13. protected const REQUIRED_MODULES = [
  14. 'ctype',
  15. 'curl',
  16. 'dom',
  17. 'fileinfo',
  18. 'gd',
  19. 'json',
  20. 'mbstring',
  21. 'openssl',
  22. 'posix',
  23. 'session',
  24. 'xml',
  25. 'xmlreader',
  26. 'xmlwriter',
  27. 'zip',
  28. 'zlib',
  29. ];
  30. protected const RECOMMENDED_MODULES = [
  31. 'bcmath',
  32. 'exif',
  33. 'gmp',
  34. 'intl',
  35. 'sodium',
  36. 'sysvsem',
  37. ];
  38. public function __construct(
  39. private IL10N $l10n,
  40. private IURLGenerator $urlGenerator,
  41. ) {
  42. }
  43. public function getName(): string {
  44. return $this->l10n->t('PHP modules');
  45. }
  46. public function getCategory(): string {
  47. return 'php';
  48. }
  49. protected function getRecommendedModuleDescription(string $module): string {
  50. return match($module) {
  51. 'intl' => $this->l10n->t('increases language translation performance and fixes sorting of non-ASCII characters'),
  52. 'sodium' => $this->l10n->t('for Argon2 for password hashing'),
  53. 'bcmath' => $this->l10n->t('for WebAuthn passwordless login'),
  54. 'gmp' => $this->l10n->t('for WebAuthn passwordless login, and SFTP storage'),
  55. 'exif' => $this->l10n->t('for picture rotation in server and metadata extraction in the Photos app'),
  56. default => '',
  57. };
  58. }
  59. public function run(): SetupResult {
  60. $missingRecommendedModules = $this->getMissingModules(self::RECOMMENDED_MODULES);
  61. $missingRequiredModules = $this->getMissingModules(self::REQUIRED_MODULES);
  62. if (!empty($missingRequiredModules)) {
  63. return SetupResult::error(
  64. $this->l10n->t('This instance is missing some required PHP modules. It is required to install them: %s.', implode(', ', $missingRequiredModules)),
  65. $this->urlGenerator->linkToDocs('admin-php-modules')
  66. );
  67. } elseif (!empty($missingRecommendedModules)) {
  68. $moduleList = implode(
  69. "\n",
  70. array_map(
  71. fn (string $module) => '- ' . $module . ' ' . $this->getRecommendedModuleDescription($module),
  72. $missingRecommendedModules
  73. )
  74. );
  75. return SetupResult::info(
  76. $this->l10n->t("This instance is missing some recommended PHP modules. For improved performance and better compatibility it is highly recommended to install them:\n%s", $moduleList),
  77. $this->urlGenerator->linkToDocs('admin-php-modules')
  78. );
  79. } else {
  80. return SetupResult::success();
  81. }
  82. }
  83. /**
  84. * Checks for potential PHP modules that would improve the instance
  85. *
  86. * @param string[] $modules modules to test
  87. * @return string[] A list of PHP modules which are missing
  88. */
  89. protected function getMissingModules(array $modules): array {
  90. return array_values(array_filter(
  91. $modules,
  92. fn (string $module) => !extension_loaded($module),
  93. ));
  94. }
  95. }