WebdavEndpoint.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  8. *
  9. * @license AGPL-3.0-or-later
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\DAV\SetupChecks;
  26. use OCA\Settings\SetupChecks\CheckServerResponseTrait;
  27. use OCP\Http\Client\IClientService;
  28. use OCP\IConfig;
  29. use OCP\IL10N;
  30. use OCP\IURLGenerator;
  31. use OCP\SetupCheck\ISetupCheck;
  32. use OCP\SetupCheck\SetupResult;
  33. use Psr\Log\LoggerInterface;
  34. class WebdavEndpoint implements ISetupCheck {
  35. use CheckServerResponseTrait;
  36. public function __construct(
  37. protected IL10N $l10n,
  38. protected IConfig $config,
  39. protected IURLGenerator $urlGenerator,
  40. protected IClientService $clientService,
  41. protected LoggerInterface $logger,
  42. ) {
  43. }
  44. public function getCategory(): string {
  45. return 'network';
  46. }
  47. public function getName(): string {
  48. return $this->l10n->t('WebDAV endpoint');
  49. }
  50. public function run(): SetupResult {
  51. $urls = [
  52. ['propfind', '/remote.php/webdav', [207, 401]],
  53. ];
  54. foreach ($urls as [$verb,$url,$validStatuses]) {
  55. $works = null;
  56. foreach ($this->runRequest($verb, $url, ['httpErrors' => false]) as $response) {
  57. // Check that the response status matches
  58. $works = in_array($response->getStatusCode(), $validStatuses);
  59. // Skip the other requests if one works
  60. if ($works === true) {
  61. break;
  62. }
  63. }
  64. // If 'works' is null then we could not connect to the server
  65. if ($works === null) {
  66. return SetupResult::info(
  67. $this->l10n->t('Could not check that your web server is properly set up to allow file synchronization over WebDAV. Please check manually.') . "\n" . $this->serverConfigHelp(),
  68. $this->urlGenerator->linkToDocs('admin-setup-well-known-URL'),
  69. );
  70. }
  71. // Otherwise if we fail we can abort here
  72. if ($works === false) {
  73. return SetupResult::error(
  74. $this->l10n->t('Your web server is not yet properly set up to allow file synchronization, because the WebDAV interface seems to be broken.') . "\n" . $this->serverConfigHelp(),
  75. );
  76. }
  77. }
  78. return SetupResult::success(
  79. $this->l10n->t('Your web server is properly set up to allow file synchronization over WebDAV.')
  80. );
  81. }
  82. }