LockPlugin.php 2.5 KB

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