123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- <?php
- namespace OCA\DAV\DAV\Sharing;
- use OCA\DAV\CalDAV\CalDavBackend;
- use OCA\DAV\CalDAV\CalendarHome;
- use OCA\DAV\Connector\Sabre\Auth;
- use OCA\DAV\DAV\Sharing\Xml\Invite;
- use OCA\DAV\DAV\Sharing\Xml\ShareRequest;
- use OCP\IConfig;
- use OCP\IRequest;
- use Sabre\DAV\Exception\NotFound;
- use Sabre\DAV\INode;
- use Sabre\DAV\PropFind;
- use Sabre\DAV\Server;
- use Sabre\DAV\ServerPlugin;
- use Sabre\HTTP\RequestInterface;
- use Sabre\HTTP\ResponseInterface;
- class Plugin extends ServerPlugin {
- public const NS_OWNCLOUD = 'http://owncloud.org/ns';
- public const NS_NEXTCLOUD = 'http://nextcloud.com/ns';
-
- private $auth;
-
- private $request;
-
- private $config;
-
- public function __construct(Auth $authBackEnd, IRequest $request, IConfig $config) {
- $this->auth = $authBackEnd;
- $this->request = $request;
- $this->config = $config;
- }
-
- protected $server;
-
- public function getFeatures() {
- return ['oc-resource-sharing'];
- }
-
- public function getPluginName() {
- return 'oc-resource-sharing';
- }
-
- public function initialize(Server $server) {
- $this->server = $server;
- $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}share'] = ShareRequest::class;
- $this->server->xml->elementMap['{' . Plugin::NS_OWNCLOUD . '}invite'] = Invite::class;
- $this->server->on('method:POST', [$this, 'httpPost']);
- $this->server->on('propFind', [$this, 'propFind']);
- }
-
- public function httpPost(RequestInterface $request, ResponseInterface $response) {
- $path = $request->getPath();
-
- $contentType = (string) $request->getHeader('Content-Type');
- if (!str_contains($contentType, 'application/xml') && !str_contains($contentType, 'text/xml')) {
- return;
- }
-
- try {
- $node = $this->server->tree->getNodeForPath($path);
- } catch (NotFound $e) {
- return;
- }
- $requestBody = $request->getBodyAsString();
-
-
-
-
-
-
-
- $request->setBody($requestBody);
- $message = $this->server->xml->parse($requestBody, $request->getUrl(), $documentType);
- switch ($documentType) {
-
-
- case '{' . self::NS_OWNCLOUD . '}share':
-
- if (!$node instanceof IShareable) {
- return;
- }
- $this->server->transactionType = 'post-oc-resource-share';
-
- $acl = $this->server->getPlugin('acl');
-
- if ($acl) {
-
- $acl->checkPrivileges($path, '{DAV:}write');
- $limitSharingToOwner = $this->config->getAppValue('dav', 'limitAddressBookAndCalendarSharingToOwner', 'no') === 'yes';
- $isOwner = $acl->getCurrentUserPrincipal() === $node->getOwner();
- if ($limitSharingToOwner && !$isOwner) {
- return;
- }
- }
- $node->updateShares($message->set, $message->remove);
- $response->setStatus(200);
-
-
- $response->setHeader('X-Sabre-Status', 'everything-went-well');
-
- return false;
- }
- }
-
- public function propFind(PropFind $propFind, INode $node) {
- if ($node instanceof CalendarHome && $propFind->getDepth() === 1) {
- $backend = $node->getCalDAVBackend();
- if ($backend instanceof CalDavBackend) {
- $calendars = $node->getChildren();
- $calendars = array_filter($calendars, function (INode $node) {
- return $node instanceof IShareable;
- });
-
- $resourceIds = array_map(function (IShareable $node) {
- return $node->getResourceId();
- }, $calendars);
- $backend->preloadShares($resourceIds);
- }
- }
- if ($node instanceof IShareable) {
- $propFind->handle('{' . Plugin::NS_OWNCLOUD . '}invite', function () use ($node) {
- return new Invite(
- $node->getShares()
- );
- });
- }
- }
- }
|