PhpModules.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\SetupChecks;
  25. use OCP\IL10N;
  26. use OCP\IURLGenerator;
  27. use OCP\SetupCheck\ISetupCheck;
  28. use OCP\SetupCheck\SetupResult;
  29. class PhpModules implements ISetupCheck {
  30. protected const REQUIRED_MODULES = [
  31. 'ctype',
  32. 'curl',
  33. 'dom',
  34. 'fileinfo',
  35. 'gd',
  36. 'json',
  37. 'mbstring',
  38. 'openssl',
  39. 'posix',
  40. 'session',
  41. 'xml',
  42. 'xmlreader',
  43. 'xmlwriter',
  44. 'zip',
  45. 'zlib',
  46. ];
  47. protected const RECOMMENDED_MODULES = [
  48. 'bcmath',
  49. 'exif',
  50. 'gmp',
  51. 'intl',
  52. 'sodium',
  53. 'sysvsem',
  54. ];
  55. public function __construct(
  56. private IL10N $l10n,
  57. private IURLGenerator $urlGenerator,
  58. ) {
  59. }
  60. public function getName(): string {
  61. return $this->l10n->t('PHP modules');
  62. }
  63. public function getCategory(): string {
  64. return 'php';
  65. }
  66. protected function getRecommendedModuleDescription(string $module): string {
  67. return match($module) {
  68. 'intl' => $this->l10n->t('increases language translation performance and fixes sorting of non-ASCII characters'),
  69. 'sodium' => $this->l10n->t('for Argon2 for password hashing'),
  70. 'bcmath' => $this->l10n->t('for WebAuthn passwordless login'),
  71. 'gmp' => $this->l10n->t('for WebAuthn passwordless login, and SFTP storage'),
  72. 'exif' => $this->l10n->t('for picture rotation in server and metadata extraction in the Photos app'),
  73. default => '',
  74. };
  75. }
  76. public function run(): SetupResult {
  77. $missingRecommendedModules = $this->getMissingModules(self::RECOMMENDED_MODULES);
  78. $missingRequiredModules = $this->getMissingModules(self::REQUIRED_MODULES);
  79. if (!empty($missingRequiredModules)) {
  80. return SetupResult::error(
  81. $this->l10n->t('This instance is missing some required PHP modules. It is required to install them: %s.', implode(', ', $missingRequiredModules)),
  82. $this->urlGenerator->linkToDocs('admin-php-modules')
  83. );
  84. } elseif (!empty($missingRecommendedModules)) {
  85. $moduleList = implode(
  86. "\n",
  87. array_map(
  88. fn (string $module) => '- '.$module.' '.$this->getRecommendedModuleDescription($module),
  89. $missingRecommendedModules
  90. )
  91. );
  92. return SetupResult::info(
  93. $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),
  94. $this->urlGenerator->linkToDocs('admin-php-modules')
  95. );
  96. } else {
  97. return SetupResult::success();
  98. }
  99. }
  100. /**
  101. * Checks for potential PHP modules that would improve the instance
  102. *
  103. * @param string[] $modules modules to test
  104. * @return string[] A list of PHP modules which are missing
  105. */
  106. protected function getMissingModules(array $modules): array {
  107. return array_values(array_filter(
  108. $modules,
  109. fn (string $module) => !extension_loaded($module),
  110. ));
  111. }
  112. }