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.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Georg Ehrke <georg@owncloud.com>
|
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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
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;
|
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 {
|
|
|
|
|
2016-01-12 15:23:50 +03:00
|
|
|
const NS_OWNCLOUD = 'http://owncloud.org/ns';
|
2017-04-19 17:18:44 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Plugin constructor.
|
|
|
|
*
|
|
|
|
* @param Auth $authBackEnd
|
|
|
|
* @param IRequest $request
|
|
|
|
*/
|
2015-11-10 09:54:35 +03:00
|
|
|
public function __construct(Auth $authBackEnd, IRequest $request) {
|
|
|
|
$this->auth = $authBackEnd;
|
|
|
|
$this->request = $request;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function initialize(Server $server) {
|
|
|
|
$this->server = $server;
|
2016-01-25 19:50:39 +03:00
|
|
|
$this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}share'] = 'OCA\\DAV\\DAV\\Sharing\\Xml\\ShareRequest';
|
2016-02-02 17:13:28 +03:00
|
|
|
$this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}invite'] = 'OCA\\DAV\\DAV\\Sharing\\Xml\\Invite';
|
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
|
|
|
*/
|
|
|
|
function httpPost(RequestInterface $request, ResponseInterface $response) {
|
|
|
|
|
|
|
|
$path = $request->getPath();
|
|
|
|
|
|
|
|
// Only handling xml
|
|
|
|
$contentType = $request->getHeader('Content-Type');
|
|
|
|
if (strpos($contentType, 'application/xml') === false && strpos($contentType, 'text/xml') === false)
|
|
|
|
return;
|
|
|
|
|
|
|
|
// 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.
|
2016-01-12 15:23:50 +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');
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
function propFind(PropFind $propFind, INode $node) {
|
|
|
|
if ($node instanceof IShareable) {
|
|
|
|
|
|
|
|
$propFind->handle('{' . Plugin::NS_OWNCLOUD . '}invite', function() use ($node) {
|
|
|
|
return new Invite(
|
|
|
|
$node->getShares()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-05 18:46:37 +03:00
|
|
|
}
|