JavaScriptModules.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  7. *
  8. * @license AGPL-3.0-or-later
  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\Http\Client\IClientService;
  26. use OCP\IConfig;
  27. use OCP\IL10N;
  28. use OCP\IURLGenerator;
  29. use OCP\SetupCheck\ISetupCheck;
  30. use OCP\SetupCheck\SetupResult;
  31. use Psr\Log\LoggerInterface;
  32. /**
  33. * Checks if the webserver serves '.mjs' files using the correct MIME type
  34. */
  35. class JavaScriptModules implements ISetupCheck {
  36. public function __construct(
  37. private IL10N $l10n,
  38. private IConfig $config,
  39. private IURLGenerator $urlGenerator,
  40. private IClientService $clientService,
  41. private LoggerInterface $logger,
  42. ) {
  43. }
  44. public function getCategory(): string {
  45. return 'network';
  46. }
  47. public function getName(): string {
  48. return $this->l10n->t('JavaScript modules support');
  49. }
  50. public function run(): SetupResult {
  51. $testFile = $this->urlGenerator->linkTo('settings', 'js/esm-test.mjs');
  52. $testURLs = array_merge(
  53. [$this->urlGenerator->getAbsoluteURL($testFile)],
  54. array_map(fn (string $host): string => $host . $testFile, $this->config->getSystemValue('trusted_domains', []))
  55. );
  56. foreach ($testURLs as $testURL) {
  57. try {
  58. $client = $this->clientService->newClient();
  59. $response = $client->head($testURL, [
  60. 'connect_timeout' => 10,
  61. // Disable SSL certificate checks to allow self signed certs
  62. 'verify' => false,
  63. // Allow to connect to local server
  64. 'nextcloud' => [
  65. 'allow_local_address' => true,
  66. ],
  67. ]);
  68. if (preg_match('/(text|application)\/javascript/i', $response->getHeader('Content-Type'))) {
  69. return SetupResult::success();
  70. }
  71. } catch (\Throwable $e) {
  72. $this->logger->debug('Can not connect to local server for checking JavaScript modules support', ['exception' => $e, 'url' => $testURL]);
  73. return SetupResult::warning($this->l10n->t('Could not check for JavaScript support. Please check manually if your webserver serves `.mjs` files using the JavaScript MIME type.'));
  74. }
  75. }
  76. return SetupResult::error($this->l10n->t('Your webserver does not serve `.mjs` files using the JavaScript MIME type. This will break some apps by preventing browsers from executing the JavaScript files. You should configure your webserver to serve `.mjs` files with either the `text/javascript` or `application/javascript` MIME type.'));
  77. }
  78. }