blocklegacyclientplugin.php 2.5 KB

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