LockPlugin.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Jaakko Salo <jaakkos@gmail.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Stefan Weil <sw@weilnetz.de>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Connector\Sabre;
  28. use OCA\DAV\Connector\Sabre\Exception\FileLocked;
  29. use OCP\Lock\ILockingProvider;
  30. use OCP\Lock\LockedException;
  31. use Sabre\DAV\Exception\NotFound;
  32. use Sabre\DAV\ServerPlugin;
  33. use Sabre\HTTP\RequestInterface;
  34. class LockPlugin extends ServerPlugin {
  35. /**
  36. * Reference to main server object
  37. *
  38. * @var \Sabre\DAV\Server
  39. */
  40. private $server;
  41. /**
  42. * State of the lock
  43. *
  44. * @var bool
  45. */
  46. private $isLocked;
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function initialize(\Sabre\DAV\Server $server) {
  51. $this->server = $server;
  52. $this->server->on('beforeMethod:*', [$this, 'getLock'], 50);
  53. $this->server->on('afterMethod:*', [$this, 'releaseLock'], 50);
  54. $this->isLocked = false;
  55. }
  56. public function getLock(RequestInterface $request) {
  57. // we can't listen on 'beforeMethod:PUT' due to order of operations with setting up the tree
  58. // so instead we limit ourselves to the PUT method manually
  59. if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) {
  60. return;
  61. }
  62. try {
  63. $node = $this->server->tree->getNodeForPath($request->getPath());
  64. } catch (NotFound $e) {
  65. return;
  66. }
  67. if ($node instanceof Node) {
  68. try {
  69. $node->acquireLock(ILockingProvider::LOCK_SHARED);
  70. } catch (LockedException $e) {
  71. throw new FileLocked($e->getMessage(), $e->getCode(), $e);
  72. }
  73. $this->isLocked = true;
  74. }
  75. }
  76. public function releaseLock(RequestInterface $request) {
  77. // don't try to release the lock if we never locked one
  78. if ($this->isLocked === false) {
  79. return;
  80. }
  81. if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) {
  82. return;
  83. }
  84. try {
  85. $node = $this->server->tree->getNodeForPath($request->getPath());
  86. } catch (NotFound $e) {
  87. return;
  88. }
  89. if ($node instanceof Node) {
  90. $node->releaseLock(ILockingProvider::LOCK_SHARED);
  91. $this->isLocked = false;
  92. }
  93. }
  94. }