LockPlugin.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Stefan Weil <sw@weilnetz.de>
  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 OCA\DAV\Connector\Sabre\Exception\FileLocked;
  28. use OCP\Lock\ILockingProvider;
  29. use OCP\Lock\LockedException;
  30. use Sabre\DAV\Exception\NotFound;
  31. use Sabre\DAV\ServerPlugin;
  32. use Sabre\HTTP\RequestInterface;
  33. class LockPlugin extends ServerPlugin {
  34. /**
  35. * Reference to main server object
  36. *
  37. * @var \Sabre\DAV\Server
  38. */
  39. private $server;
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function initialize(\Sabre\DAV\Server $server) {
  44. $this->server = $server;
  45. $this->server->on('beforeMethod:*', [$this, 'getLock'], 50);
  46. $this->server->on('afterMethod:*', [$this, 'releaseLock'], 50);
  47. }
  48. public function getLock(RequestInterface $request) {
  49. // we can't listen on 'beforeMethod:PUT' due to order of operations with setting up the tree
  50. // so instead we limit ourselves to the PUT method manually
  51. if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) {
  52. return;
  53. }
  54. try {
  55. $node = $this->server->tree->getNodeForPath($request->getPath());
  56. } catch (NotFound $e) {
  57. return;
  58. }
  59. if ($node instanceof Node) {
  60. try {
  61. $node->acquireLock(ILockingProvider::LOCK_SHARED);
  62. } catch (LockedException $e) {
  63. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  64. }
  65. }
  66. }
  67. public function releaseLock(RequestInterface $request) {
  68. if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) {
  69. return;
  70. }
  71. try {
  72. $node = $this->server->tree->getNodeForPath($request->getPath());
  73. } catch (NotFound $e) {
  74. return;
  75. }
  76. if ($node instanceof Node) {
  77. $node->releaseLock(ILockingProvider::LOCK_SHARED);
  78. }
  79. }
  80. }