You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlockLegacyClientPlugin.php 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 Sabre\DAV\ServerPlugin;
  29. use Sabre\HTTP\RequestInterface;
  30. use Sabre\DAV\Server;
  31. /**
  32. * Class BlockLegacyClientPlugin is used to detect old legacy sync clients and
  33. * returns a 403 status to those clients
  34. *
  35. * @package OCA\DAV\Connector\Sabre
  36. */
  37. class BlockLegacyClientPlugin extends ServerPlugin {
  38. protected ?Server $server = null;
  39. protected IConfig $config;
  40. public function __construct(IConfig $config) {
  41. $this->config = $config;
  42. }
  43. /**
  44. * @return void
  45. */
  46. public function initialize(Server $server) {
  47. $this->server = $server;
  48. $this->server->on('beforeMethod:*', [$this, 'beforeHandler'], 200);
  49. }
  50. /**
  51. * Detects all unsupported clients and throws a \Sabre\DAV\Exception\Forbidden
  52. * exception which will result in a 403 to them.
  53. * @param RequestInterface $request
  54. * @throws \Sabre\DAV\Exception\Forbidden If the client version is not supported
  55. */
  56. public function beforeHandler(RequestInterface $request) {
  57. $userAgent = $request->getHeader('User-Agent');
  58. if ($userAgent === null) {
  59. return;
  60. }
  61. $minimumSupportedDesktopVersion = $this->config->getSystemValue('minimum.supported.desktop.version', '2.3.0');
  62. // Match on the mirall version which is in scheme "Mozilla/5.0 (%1) mirall/%2" or
  63. // "mirall/%1" for older releases
  64. preg_match("/(?:mirall\\/)([\d.]+)/i", $userAgent, $versionMatches);
  65. if (isset($versionMatches[1]) &&
  66. version_compare($versionMatches[1], $minimumSupportedDesktopVersion) === -1) {
  67. throw new \Sabre\DAV\Exception\Forbidden('Unsupported client version.');
  68. }
  69. }
  70. }