BlockLegacyClientPlugin.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Georg Ehrke <oc.list@georgehrke.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\DAV\Connector\Sabre;
  27. use OCP\IConfig;
  28. use OCP\IRequest;
  29. use Sabre\DAV\Server;
  30. use Sabre\DAV\ServerPlugin;
  31. use Sabre\HTTP\RequestInterface;
  32. /**
  33. * Class BlockLegacyClientPlugin is used to detect old legacy sync clients and
  34. * returns a 403 status to those clients
  35. *
  36. * @package OCA\DAV\Connector\Sabre
  37. */
  38. class BlockLegacyClientPlugin extends ServerPlugin {
  39. protected ?Server $server = null;
  40. protected IConfig $config;
  41. public function __construct(IConfig $config) {
  42. $this->config = $config;
  43. }
  44. /**
  45. * @return void
  46. */
  47. public function initialize(Server $server) {
  48. $this->server = $server;
  49. $this->server->on('beforeMethod:*', [$this, 'beforeHandler'], 200);
  50. }
  51. /**
  52. * Detects all unsupported clients and throws a \Sabre\DAV\Exception\Forbidden
  53. * exception which will result in a 403 to them.
  54. * @param RequestInterface $request
  55. * @throws \Sabre\DAV\Exception\Forbidden If the client version is not supported
  56. */
  57. public function beforeHandler(RequestInterface $request) {
  58. $userAgent = $request->getHeader('User-Agent');
  59. if ($userAgent === null) {
  60. return;
  61. }
  62. $minimumSupportedDesktopVersion = $this->config->getSystemValue('minimum.supported.desktop.version', '2.3.0');
  63. preg_match(IRequest::USER_AGENT_CLIENT_DESKTOP, $userAgent, $versionMatches);
  64. if (isset($versionMatches[1]) &&
  65. version_compare($versionMatches[1], $minimumSupportedDesktopVersion) === -1) {
  66. throw new \Sabre\DAV\Exception\Forbidden('Unsupported client version.');
  67. }
  68. }
  69. }