lockplugin.php 2.4 KB

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