2015-07-22 16:28:56 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Jaakko Salo <jaakkos@gmail.com>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Stefan Weil <sw@weilnetz.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-07-22 16:28:56 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-07-22 16:28:56 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2015-10-15 15:46:31 +03:00
|
|
|
namespace OCA\DAV\Connector\Sabre;
|
2015-07-22 16:28:56 +03:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\Exception\FileLocked;
|
2015-07-22 16:28:56 +03:00
|
|
|
use OCP\Lock\ILockingProvider;
|
|
|
|
use OCP\Lock\LockedException;
|
|
|
|
use Sabre\DAV\Exception\NotFound;
|
|
|
|
use Sabre\DAV\ServerPlugin;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
|
|
|
|
class LockPlugin extends ServerPlugin {
|
|
|
|
/**
|
|
|
|
* Reference to main server object
|
|
|
|
*
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
2020-05-24 20:03:48 +03:00
|
|
|
/**
|
|
|
|
* State of the lock
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $isLocked;
|
|
|
|
|
2015-07-22 16:28:56 +03:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function initialize(\Sabre\DAV\Server $server) {
|
|
|
|
$this->server = $server;
|
2020-03-09 18:32:04 +03:00
|
|
|
$this->server->on('beforeMethod:*', [$this, 'getLock'], 50);
|
2020-04-21 11:19:06 +03:00
|
|
|
$this->server->on('afterMethod:*', [$this, 'releaseLock'], 50);
|
2020-05-24 20:03:48 +03:00
|
|
|
$this->isLocked = false;
|
2015-07-22 16:28:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getLock(RequestInterface $request) {
|
2016-01-29 20:25:27 +03:00
|
|
|
// we can't listen on 'beforeMethod:PUT' due to order of operations with setting up the tree
|
2015-07-22 16:28:56 +03:00
|
|
|
// so instead we limit ourselves to the PUT method manually
|
2015-09-15 18:59:44 +03:00
|
|
|
if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) {
|
2015-07-22 16:28:56 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2015-11-16 18:09:04 +03:00
|
|
|
$node = $this->server->tree->getNodeForPath($request->getPath());
|
2015-07-22 16:28:56 +03:00
|
|
|
} catch (NotFound $e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ($node instanceof Node) {
|
|
|
|
try {
|
|
|
|
$node->acquireLock(ILockingProvider::LOCK_SHARED);
|
|
|
|
} catch (LockedException $e) {
|
|
|
|
throw new FileLocked($e->getMessage(), $e->getCode(), $e);
|
|
|
|
}
|
2020-05-24 20:03:48 +03:00
|
|
|
$this->isLocked = true;
|
2015-07-22 16:28:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function releaseLock(RequestInterface $request) {
|
2020-05-24 20:03:48 +03:00
|
|
|
// don't try to release the lock if we never locked one
|
|
|
|
if ($this->isLocked === false) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-15 18:59:44 +03:00
|
|
|
if ($request->getMethod() !== 'PUT' || isset($_SERVER['HTTP_OC_CHUNKED'])) {
|
2015-07-22 16:28:56 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-08-12 15:13:03 +03:00
|
|
|
try {
|
2015-11-16 18:09:04 +03:00
|
|
|
$node = $this->server->tree->getNodeForPath($request->getPath());
|
2015-08-12 15:13:03 +03:00
|
|
|
} catch (NotFound $e) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-22 16:28:56 +03:00
|
|
|
if ($node instanceof Node) {
|
|
|
|
$node->releaseLock(ILockingProvider::LOCK_SHARED);
|
2020-05-24 20:03:48 +03:00
|
|
|
$this->isLocked = false;
|
2015-07-22 16:28:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|