LockPlugin.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. * @author Stefan Weil <sw@weilnetz.de>
  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 OCA\DAV\Connector\Sabre\Exception\FileLocked;
  27. use OCA\DAV\Connector\Sabre\Node;
  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. }