FakeLockerPlugin.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 Sabre\DAV\INode;
  28. use Sabre\DAV\Locks\LockInfo;
  29. use Sabre\DAV\PropFind;
  30. use Sabre\DAV\ServerPlugin;
  31. use Sabre\DAV\Xml\Property\LockDiscovery;
  32. use Sabre\DAV\Xml\Property\SupportedLock;
  33. use Sabre\HTTP\RequestInterface;
  34. use Sabre\HTTP\ResponseInterface;
  35. /**
  36. * Class FakeLockerPlugin is a plugin only used when connections come in from
  37. * OS X via Finder. The fake locking plugin does emulate Class 2 WebDAV support
  38. * (locking of files) which allows Finder to access the storage in write mode as
  39. * well.
  40. *
  41. * No real locking is performed, instead the plugin just returns always positive
  42. * responses.
  43. *
  44. * @see https://github.com/owncloud/core/issues/17732
  45. * @package OCA\DAV\Connector\Sabre
  46. */
  47. class FakeLockerPlugin extends ServerPlugin {
  48. /** @var \Sabre\DAV\Server */
  49. private $server;
  50. /** {@inheritDoc} */
  51. public function initialize(\Sabre\DAV\Server $server) {
  52. $this->server = $server;
  53. $this->server->on('method:LOCK', [$this, 'fakeLockProvider'], 1);
  54. $this->server->on('method:UNLOCK', [$this, 'fakeUnlockProvider'], 1);
  55. $server->on('propFind', [$this, 'propFind']);
  56. $server->on('validateTokens', [$this, 'validateTokens']);
  57. }
  58. /**
  59. * Indicate that we support LOCK and UNLOCK
  60. *
  61. * @param string $path
  62. * @return string[]
  63. */
  64. public function getHTTPMethods($path) {
  65. return [
  66. 'LOCK',
  67. 'UNLOCK',
  68. ];
  69. }
  70. /**
  71. * Indicate that we support locking
  72. *
  73. * @return integer[]
  74. */
  75. public function getFeatures() {
  76. return [2];
  77. }
  78. /**
  79. * Return some dummy response for PROPFIND requests with regard to locking
  80. *
  81. * @param PropFind $propFind
  82. * @param INode $node
  83. * @return void
  84. */
  85. public function propFind(PropFind $propFind, INode $node) {
  86. $propFind->handle('{DAV:}supportedlock', function () {
  87. return new SupportedLock();
  88. });
  89. $propFind->handle('{DAV:}lockdiscovery', function () use ($propFind) {
  90. return new LockDiscovery([]);
  91. });
  92. }
  93. /**
  94. * Mark a locking token always as valid
  95. *
  96. * @param RequestInterface $request
  97. * @param array $conditions
  98. */
  99. public function validateTokens(RequestInterface $request, &$conditions) {
  100. foreach ($conditions as &$fileCondition) {
  101. if (isset($fileCondition['tokens'])) {
  102. foreach ($fileCondition['tokens'] as &$token) {
  103. if (isset($token['token'])) {
  104. if (substr($token['token'], 0, 16) === 'opaquelocktoken:') {
  105. $token['validToken'] = true;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. }
  112. /**
  113. * Fakes a successful LOCK
  114. *
  115. * @param RequestInterface $request
  116. * @param ResponseInterface $response
  117. * @return bool
  118. */
  119. public function fakeLockProvider(RequestInterface $request,
  120. ResponseInterface $response) {
  121. $lockInfo = new LockInfo();
  122. $lockInfo->token = md5($request->getPath());
  123. $lockInfo->uri = $request->getPath();
  124. $lockInfo->depth = \Sabre\DAV\Server::DEPTH_INFINITY;
  125. $lockInfo->timeout = 1800;
  126. $body = $this->server->xml->write('{DAV:}prop', [
  127. '{DAV:}lockdiscovery' =>
  128. new LockDiscovery([$lockInfo])
  129. ]);
  130. $response->setStatus(200);
  131. $response->setBody($body);
  132. return false;
  133. }
  134. /**
  135. * Fakes a successful LOCK
  136. *
  137. * @param RequestInterface $request
  138. * @param ResponseInterface $response
  139. * @return bool
  140. */
  141. public function fakeUnlockProvider(RequestInterface $request,
  142. ResponseInterface $response) {
  143. $response->setStatus(204);
  144. $response->setHeader('Content-Length', '0');
  145. return false;
  146. }
  147. }