BlockLegacyClientPlugin.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Connector\Sabre;
  8. use OCA\Theming\ThemingDefaults;
  9. use OCP\IConfig;
  10. use OCP\IRequest;
  11. use Sabre\DAV\Server;
  12. use Sabre\DAV\ServerPlugin;
  13. use Sabre\HTTP\RequestInterface;
  14. /**
  15. * Class BlockLegacyClientPlugin is used to detect old legacy sync clients and
  16. * returns a 403 status to those clients
  17. *
  18. * @package OCA\DAV\Connector\Sabre
  19. */
  20. class BlockLegacyClientPlugin extends ServerPlugin {
  21. protected ?Server $server = null;
  22. public function __construct(
  23. private IConfig $config,
  24. private ThemingDefaults $themingDefaults,
  25. ) {
  26. }
  27. /**
  28. * @return void
  29. */
  30. public function initialize(Server $server) {
  31. $this->server = $server;
  32. $this->server->on('beforeMethod:*', [$this, 'beforeHandler'], 200);
  33. }
  34. /**
  35. * Detects all unsupported clients and throws a \Sabre\DAV\Exception\Forbidden
  36. * exception which will result in a 403 to them.
  37. * @param RequestInterface $request
  38. * @throws \Sabre\DAV\Exception\Forbidden If the client version is not supported
  39. */
  40. public function beforeHandler(RequestInterface $request) {
  41. $userAgent = $request->getHeader('User-Agent');
  42. if ($userAgent === null) {
  43. return;
  44. }
  45. $minimumSupportedDesktopVersion = $this->config->getSystemValue('minimum.supported.desktop.version', '2.3.0');
  46. preg_match(IRequest::USER_AGENT_CLIENT_DESKTOP, $userAgent, $versionMatches);
  47. if (isset($versionMatches[1]) &&
  48. version_compare($versionMatches[1], $minimumSupportedDesktopVersion) === -1) {
  49. $customClientDesktopLink = htmlspecialchars($this->themingDefaults->getSyncClientUrl());
  50. $minimumSupportedDesktopVersion = htmlspecialchars($minimumSupportedDesktopVersion);
  51. throw new \Sabre\DAV\Exception\Forbidden("This version of the client is unsupported. Upgrade to <a href=\"$customClientDesktopLink\">version $minimumSupportedDesktopVersion or later</a>.");
  52. }
  53. }
  54. }