blocklegacyclientplugin.php 2.4 KB

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