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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  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, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\DAV\Connector\Sabre;
  26. use OCP\IConfig;
  27. use Sabre\DAV\ServerPlugin;
  28. use Sabre\HTTP\RequestInterface;
  29. /**
  30. * Class BlockLegacyClientPlugin is used to detect old legacy sync clients and
  31. * returns a 403 status to those clients
  32. *
  33. * @package OCA\DAV\Connector\Sabre
  34. */
  35. class BlockLegacyClientPlugin extends ServerPlugin {
  36. /** @var \Sabre\DAV\Server */
  37. protected $server;
  38. /** @var IConfig */
  39. protected $config;
  40. /**
  41. * @param IConfig $config
  42. */
  43. public function __construct(IConfig $config) {
  44. $this->config = $config;
  45. }
  46. /**
  47. * @param \Sabre\DAV\Server $server
  48. * @return void
  49. */
  50. public function initialize(\Sabre\DAV\Server $server) {
  51. $this->server = $server;
  52. $this->server->on('beforeMethod:*', [$this, 'beforeHandler'], 200);
  53. }
  54. /**
  55. * Detects all unsupported clients and throws a \Sabre\DAV\Exception\Forbidden
  56. * exception which will result in a 403 to them.
  57. * @param RequestInterface $request
  58. * @throws \Sabre\DAV\Exception\Forbidden If the client version is not supported
  59. */
  60. public function beforeHandler(RequestInterface $request) {
  61. $userAgent = $request->getHeader('User-Agent');
  62. if($userAgent === null) {
  63. return;
  64. }
  65. $minimumSupportedDesktopVersion = $this->config->getSystemValue('minimum.supported.desktop.version', '2.0.0');
  66. // Match on the mirall version which is in scheme "Mozilla/5.0 (%1) mirall/%2" or
  67. // "mirall/%1" for older releases
  68. preg_match("/(?:mirall\\/)([\d.]+)/i", $userAgent, $versionMatches);
  69. if(isset($versionMatches[1]) &&
  70. version_compare($versionMatches[1], $minimumSupportedDesktopVersion) === -1) {
  71. throw new \Sabre\DAV\Exception\Forbidden('Unsupported client version.');
  72. }
  73. }
  74. }