2015-11-05 18:46:37 +03:00
|
|
|
<?php
|
2016-01-12 17:02:16 +03:00
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
|
|
|
* @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/>
|
2016-01-12 17:02:16 +03:00
|
|
|
*
|
|
|
|
*/
|
2016-01-25 19:50:39 +03:00
|
|
|
|
|
|
|
namespace OCA\DAV\DAV\Sharing;
|
2015-11-05 18:46:37 +03:00
|
|
|
|
2015-11-10 09:54:35 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\Auth;
|
2016-02-02 17:13:28 +03:00
|
|
|
use OCA\DAV\DAV\Sharing\Xml\Invite;
|
2018-01-26 01:16:13 +03:00
|
|
|
use OCA\DAV\DAV\Sharing\Xml\ShareRequest;
|
2020-08-11 10:24:08 +03:00
|
|
|
use OCP\IConfig;
|
2015-11-10 09:54:35 +03:00
|
|
|
use OCP\IRequest;
|
2015-11-05 18:46:37 +03:00
|
|
|
use Sabre\DAV\Exception\NotFound;
|
2016-02-02 17:13:28 +03:00
|
|
|
use Sabre\DAV\INode;
|
|
|
|
use Sabre\DAV\PropFind;
|
2015-11-05 18:46:37 +03:00
|
|
|
use Sabre\DAV\Server;
|
|
|
|
use Sabre\DAV\ServerPlugin;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
|
|
|
|
class Plugin extends ServerPlugin {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const NS_OWNCLOUD = 'http://owncloud.org/ns';
|
|
|
|
public const NS_NEXTCLOUD = 'http://nextcloud.com/ns';
|
2016-01-12 15:23:50 +03:00
|
|
|
|
2015-12-14 18:14:35 +03:00
|
|
|
/** @var Auth */
|
|
|
|
private $auth;
|
|
|
|
|
|
|
|
/** @var IRequest */
|
|
|
|
private $request;
|
|
|
|
|
2020-08-11 10:24:08 +03:00
|
|
|
/** @var IConfig */
|
|
|
|
private $config;
|
|
|
|
|
2015-12-14 18:14:35 +03:00
|
|
|
/**
|
|
|
|
* Plugin constructor.
|
|
|
|
*
|
|
|
|
* @param Auth $authBackEnd
|
|
|
|
* @param IRequest $request
|
2020-08-11 10:24:08 +03:00
|
|
|
* @param IConfig $config
|
2015-12-14 18:14:35 +03:00
|
|
|
*/
|
2020-08-11 10:24:08 +03:00
|
|
|
public function __construct(Auth $authBackEnd, IRequest $request, IConfig $config) {
|
2015-11-10 09:54:35 +03:00
|
|
|
$this->auth = $authBackEnd;
|
|
|
|
$this->request = $request;
|
2020-08-11 10:24:08 +03:00
|
|
|
$this->config = $config;
|
2015-11-10 09:54:35 +03:00
|
|
|
}
|
|
|
|
|
2015-11-05 18:46:37 +03:00
|
|
|
/**
|
|
|
|
* Reference to SabreDAV server object.
|
|
|
|
*
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
protected $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method should return a list of server-features.
|
|
|
|
*
|
|
|
|
* This is for example 'versioning' and is added to the DAV: header
|
|
|
|
* in an OPTIONS response.
|
|
|
|
*
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return string[]
|
2015-11-05 18:46:37 +03:00
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function getFeatures() {
|
2016-01-25 19:50:39 +03:00
|
|
|
return ['oc-resource-sharing'];
|
2015-11-05 18:46:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a plugin name.
|
|
|
|
*
|
|
|
|
* Using this name other plugins will be able to access other plugins
|
|
|
|
* using Sabre\DAV\Server::getPlugin
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function getPluginName() {
|
2016-01-25 19:50:39 +03:00
|
|
|
return 'oc-resource-sharing';
|
2015-11-05 18:46:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This initializes the plugin.
|
|
|
|
*
|
|
|
|
* This function is called by Sabre\DAV\Server, after
|
|
|
|
* addPlugin is called.
|
|
|
|
*
|
|
|
|
* This method should set up the required event subscriptions.
|
|
|
|
*
|
|
|
|
* @param Server $server
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function initialize(Server $server) {
|
2015-11-05 18:46:37 +03:00
|
|
|
$this->server = $server;
|
2018-01-26 01:16:13 +03:00
|
|
|
$this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}share'] = ShareRequest::class;
|
|
|
|
$this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}invite'] = Invite::class;
|
2015-11-05 18:46:37 +03:00
|
|
|
|
|
|
|
$this->server->on('method:POST', [$this, 'httpPost']);
|
2016-02-02 17:13:28 +03:00
|
|
|
$this->server->on('propFind', [$this, 'propFind']);
|
2015-11-05 18:46:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-25 19:50:39 +03:00
|
|
|
* We intercept this to handle POST requests on a dav resource.
|
2015-11-05 18:46:37 +03:00
|
|
|
*
|
|
|
|
* @param RequestInterface $request
|
|
|
|
* @param ResponseInterface $response
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return null|false
|
2015-11-05 18:46:37 +03:00
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function httpPost(RequestInterface $request, ResponseInterface $response) {
|
2015-11-05 18:46:37 +03:00
|
|
|
$path = $request->getPath();
|
|
|
|
|
|
|
|
// Only handling xml
|
|
|
|
$contentType = $request->getHeader('Content-Type');
|
2020-04-10 15:19:56 +03:00
|
|
|
if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false) {
|
2015-11-05 18:46:37 +03:00
|
|
|
return;
|
2020-04-10 15:19:56 +03:00
|
|
|
}
|
2015-11-05 18:46:37 +03:00
|
|
|
|
|
|
|
// Making sure the node exists
|
|
|
|
try {
|
|
|
|
$node = $this->server->tree->getNodeForPath($path);
|
|
|
|
} catch (NotFound $e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$requestBody = $request->getBodyAsString();
|
|
|
|
|
|
|
|
// If this request handler could not deal with this POST request, it
|
|
|
|
// will return 'null' and other plugins get a chance to handle the
|
|
|
|
// request.
|
|
|
|
//
|
|
|
|
// However, we already requested the full body. This is a problem,
|
|
|
|
// because a body can only be read once. This is why we preemptively
|
|
|
|
// re-populated the request body with the existing data.
|
|
|
|
$request->setBody($requestBody);
|
|
|
|
|
2015-12-14 18:14:35 +03:00
|
|
|
$message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
|
2015-11-05 18:46:37 +03:00
|
|
|
|
|
|
|
switch ($documentType) {
|
|
|
|
|
|
|
|
// Dealing with the 'share' document, which modified invitees on a
|
|
|
|
// calendar.
|
2020-04-09 17:17:53 +03:00
|
|
|
case '{' . self::NS_OWNCLOUD . '}share':
|
2015-11-05 18:46:37 +03:00
|
|
|
|
|
|
|
// We can only deal with IShareableCalendar objects
|
2016-01-25 19:50:39 +03:00
|
|
|
if (!$node instanceof IShareable) {
|
2015-11-05 18:46:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-26 00:49:26 +03:00
|
|
|
$this->server->transactionType = 'post-oc-resource-share';
|
2015-11-05 18:46:37 +03:00
|
|
|
|
|
|
|
// Getting ACL info
|
|
|
|
$acl = $this->server->getPlugin('acl');
|
|
|
|
|
|
|
|
// If there's no ACL support, we allow everything
|
|
|
|
if ($acl) {
|
2015-12-14 18:14:35 +03:00
|
|
|
/** @var \Sabre\DAVACL\Plugin $acl */
|
2015-11-05 18:46:37 +03:00
|
|
|
$acl->checkPrivileges($path, '{DAV:}write');
|
2020-08-11 10:24:08 +03:00
|
|
|
|
|
|
|
$limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
|
|
|
|
$isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
|
|
|
|
if ($limitSharingToOwner && !$isOwner) {
|
|
|
|
return;
|
|
|
|
}
|
2015-11-05 18:46:37 +03:00
|
|
|
}
|
|
|
|
|
2015-12-14 18:14:35 +03:00
|
|
|
$node->updateShares($message->set, $message->remove);
|
2015-11-05 18:46:37 +03:00
|
|
|
|
|
|
|
$response->setStatus(200);
|
|
|
|
// Adding this because sending a response body may cause issues,
|
|
|
|
// and I wanted some type of indicator the response was handled.
|
|
|
|
$response->setHeader('X-Sabre-Status', 'everything-went-well');
|
|
|
|
|
|
|
|
// Breaking the event chain
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-02 17:13:28 +03:00
|
|
|
/**
|
|
|
|
* This event is triggered when properties are requested for a certain
|
|
|
|
* node.
|
|
|
|
*
|
|
|
|
* This allows us to inject any properties early.
|
|
|
|
*
|
|
|
|
* @param PropFind $propFind
|
|
|
|
* @param INode $node
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-10 17:51:06 +03:00
|
|
|
public function propFind(PropFind $propFind, INode $node) {
|
2016-02-02 17:13:28 +03:00
|
|
|
if ($node instanceof IShareable) {
|
2020-04-09 14:53:40 +03:00
|
|
|
$propFind->handle('{' . Plugin::NS_OWNCLOUD . '}invite', function () use ($node) {
|
2016-02-02 17:13:28 +03:00
|
|
|
return new Invite(
|
|
|
|
$node->getShares()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-11-05 18:46:37 +03:00
|
|
|
}
|