2016-03-02 20:25:31 +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 Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Tobias Kaminsky <tobias@kaminsky.me>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2016-03-02 20:25:31 +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/>
|
2016-03-02 20:25:31 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-03-02 20:25:31 +03:00
|
|
|
namespace OCA\DAV\Connector\Sabre;
|
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
use OCA\DAV\Connector\Sabre\Node as DavNode;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\NotFoundException;
|
2016-03-02 20:25:31 +03:00
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\Share\IShare;
|
2019-11-22 22:52:10 +03:00
|
|
|
use Sabre\DAV\PropFind;
|
2016-03-02 20:25:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sabre Plugin to provide share-related properties
|
|
|
|
*/
|
|
|
|
class SharesPlugin extends \Sabre\DAV\ServerPlugin {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const NS_OWNCLOUD = 'http://owncloud.org/ns';
|
|
|
|
public const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
|
|
|
|
public const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types';
|
|
|
|
public const SHAREES_PROPERTYNAME = '{http://nextcloud.org/ns}sharees';
|
2016-03-02 20:25:31 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to main server object
|
|
|
|
*
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
/** @var \OCP\Share\IManager */
|
2016-03-02 20:25:31 +03:00
|
|
|
private $shareManager;
|
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
/** @var \Sabre\DAV\Tree */
|
2016-03-02 20:25:31 +03:00
|
|
|
private $tree;
|
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
/** @var string */
|
2016-03-02 20:25:31 +03:00
|
|
|
private $userId;
|
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
/** @var \OCP\Files\Folder */
|
2016-03-02 20:25:31 +03:00
|
|
|
private $userFolder;
|
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
/** @var IShare[][] */
|
2019-07-30 23:59:22 +03:00
|
|
|
private $cachedShares = [];
|
2016-03-02 20:25:31 +03:00
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
/** @var string[] */
|
2016-11-09 18:59:36 +03:00
|
|
|
private $cachedFolders = [];
|
|
|
|
|
2016-03-02 20:25:31 +03:00
|
|
|
/**
|
|
|
|
* @param \Sabre\DAV\Tree $tree tree
|
|
|
|
* @param IUserSession $userSession user session
|
|
|
|
* @param \OCP\Files\Folder $userFolder user home folder
|
|
|
|
* @param \OCP\Share\IManager $shareManager share manager
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
\Sabre\DAV\Tree $tree,
|
|
|
|
IUserSession $userSession,
|
|
|
|
\OCP\Files\Folder $userFolder,
|
|
|
|
\OCP\Share\IManager $shareManager
|
|
|
|
) {
|
|
|
|
$this->tree = $tree;
|
|
|
|
$this->shareManager = $shareManager;
|
|
|
|
$this->userFolder = $userFolder;
|
|
|
|
$this->userId = $userSession->getUser()->getUID();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 \Sabre\DAV\Server $server
|
|
|
|
*/
|
|
|
|
public function initialize(\Sabre\DAV\Server $server) {
|
|
|
|
$server->xml->namespacesMap[self::NS_OWNCLOUD] = 'oc';
|
2018-01-26 01:16:13 +03:00
|
|
|
$server->xml->elementMap[self::SHARETYPES_PROPERTYNAME] = ShareTypeList::class;
|
2016-03-02 20:25:31 +03:00
|
|
|
$server->protectedProperties[] = self::SHARETYPES_PROPERTYNAME;
|
2019-07-22 15:47:17 +03:00
|
|
|
$server->protectedProperties[] = self::SHAREES_PROPERTYNAME;
|
2016-03-02 20:25:31 +03:00
|
|
|
|
|
|
|
$this->server = $server;
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->server->on('propFind', [$this, 'handleGetProperties']);
|
2016-03-02 20:25:31 +03:00
|
|
|
}
|
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
/**
|
|
|
|
* @param \OCP\Files\Node $node
|
|
|
|
* @return IShare[]
|
|
|
|
*/
|
2019-07-30 23:35:44 +03:00
|
|
|
private function getShare(\OCP\Files\Node $node): array {
|
|
|
|
$result = [];
|
|
|
|
$requestedShareTypes = [
|
2020-06-24 17:49:16 +03:00
|
|
|
IShare::TYPE_USER,
|
|
|
|
IShare::TYPE_GROUP,
|
|
|
|
IShare::TYPE_LINK,
|
|
|
|
IShare::TYPE_REMOTE,
|
|
|
|
IShare::TYPE_EMAIL,
|
|
|
|
IShare::TYPE_ROOM,
|
|
|
|
IShare::TYPE_CIRCLE,
|
2020-12-08 14:56:54 +03:00
|
|
|
IShare::TYPE_DECK,
|
2019-07-30 23:35:44 +03:00
|
|
|
];
|
|
|
|
foreach ($requestedShareTypes as $requestedShareType) {
|
|
|
|
$shares = $this->shareManager->getSharesBy(
|
|
|
|
$this->userId,
|
|
|
|
$requestedShareType,
|
|
|
|
$node,
|
|
|
|
false,
|
2019-08-08 12:01:23 +03:00
|
|
|
-1
|
2019-07-30 23:35:44 +03:00
|
|
|
);
|
|
|
|
foreach ($shares as $share) {
|
|
|
|
$result[] = $share;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
/**
|
|
|
|
* @param Folder $node
|
|
|
|
* @return IShare[][]
|
|
|
|
*/
|
|
|
|
private function getSharesFolder(Folder $node): array {
|
2019-07-30 23:35:44 +03:00
|
|
|
return $this->shareManager->getSharesInFolder(
|
|
|
|
$this->userId,
|
|
|
|
$node,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-03-02 17:54:54 +03:00
|
|
|
/**
|
|
|
|
* @param DavNode $sabreNode
|
|
|
|
* @return IShare[]
|
|
|
|
*/
|
|
|
|
private function getShares(DavNode $sabreNode): array {
|
2019-07-30 23:59:22 +03:00
|
|
|
if (isset($this->cachedShares[$sabreNode->getId()])) {
|
|
|
|
$shares = $this->cachedShares[$sabreNode->getId()];
|
|
|
|
} else {
|
2021-01-12 12:15:48 +03:00
|
|
|
[$parentPath,] = \Sabre\Uri\split($sabreNode->getPath());
|
2019-07-30 23:59:22 +03:00
|
|
|
if ($parentPath === '') {
|
|
|
|
$parentPath = '/';
|
|
|
|
}
|
|
|
|
// if we already cached the folder this file is in we know there are no shares for this file
|
|
|
|
if (array_search($parentPath, $this->cachedFolders) === false) {
|
2021-03-02 17:54:54 +03:00
|
|
|
try {
|
|
|
|
$node = $this->userFolder->get($sabreNode->getPath());
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-07-30 23:59:22 +03:00
|
|
|
$shares = $this->getShare($node);
|
2019-08-01 11:02:25 +03:00
|
|
|
$this->cachedShares[$sabreNode->getId()] = $shares;
|
2019-07-30 23:59:22 +03:00
|
|
|
} else {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $shares;
|
|
|
|
}
|
|
|
|
|
2016-03-02 20:25:31 +03:00
|
|
|
/**
|
|
|
|
* Adds shares to propfind response
|
|
|
|
*
|
|
|
|
* @param PropFind $propFind propfind object
|
|
|
|
* @param \Sabre\DAV\INode $sabreNode sabre node
|
|
|
|
*/
|
|
|
|
public function handleGetProperties(
|
|
|
|
PropFind $propFind,
|
|
|
|
\Sabre\DAV\INode $sabreNode
|
|
|
|
) {
|
2021-03-02 17:54:54 +03:00
|
|
|
if (!($sabreNode instanceof DavNode)) {
|
2016-03-02 20:25:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need prefetch ?
|
2021-03-02 17:54:54 +03:00
|
|
|
if ($sabreNode instanceof Directory
|
2016-03-02 20:25:31 +03:00
|
|
|
&& $propFind->getDepth() !== 0
|
2019-07-30 23:35:44 +03:00
|
|
|
&& (
|
|
|
|
!is_null($propFind->getStatus(self::SHARETYPES_PROPERTYNAME)) ||
|
|
|
|
!is_null($propFind->getStatus(self::SHAREES_PROPERTYNAME))
|
|
|
|
)
|
2016-03-02 20:25:31 +03:00
|
|
|
) {
|
2021-03-02 17:54:54 +03:00
|
|
|
try {
|
|
|
|
$folderNode = $this->userFolder->get($sabreNode->getPath());
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
// If the folder can't be properly found just return
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!($folderNode instanceof Folder)) {
|
|
|
|
// Safety check
|
|
|
|
return;
|
|
|
|
}
|
2016-03-02 20:25:31 +03:00
|
|
|
|
2019-08-01 11:02:25 +03:00
|
|
|
$this->cachedFolders[] = $sabreNode->getPath();
|
2019-07-30 23:35:44 +03:00
|
|
|
$childShares = $this->getSharesFolder($folderNode);
|
|
|
|
foreach ($childShares as $id => $shares) {
|
|
|
|
$this->cachedShares[$id] = $shares;
|
|
|
|
}
|
2016-03-02 20:25:31 +03:00
|
|
|
}
|
|
|
|
|
2016-06-18 23:04:56 +03:00
|
|
|
$propFind->handle(self::SHARETYPES_PROPERTYNAME, function () use ($sabreNode) {
|
2019-07-30 23:59:22 +03:00
|
|
|
$shares = $this->getShares($sabreNode);
|
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
$shareTypes = array_unique(array_map(function (IShare $share) {
|
2019-07-30 23:59:22 +03:00
|
|
|
return $share->getShareType();
|
|
|
|
}, $shares));
|
2016-03-02 20:25:31 +03:00
|
|
|
|
|
|
|
return new ShareTypeList($shareTypes);
|
|
|
|
});
|
2019-07-22 15:47:17 +03:00
|
|
|
|
2020-04-09 14:53:40 +03:00
|
|
|
$propFind->handle(self::SHAREES_PROPERTYNAME, function () use ($sabreNode) {
|
2019-07-30 23:59:22 +03:00
|
|
|
$shares = $this->getShares($sabreNode);
|
2019-07-22 15:47:17 +03:00
|
|
|
|
2019-07-30 23:51:08 +03:00
|
|
|
return new ShareeList($shares);
|
2019-07-23 11:31:13 +03:00
|
|
|
});
|
2016-03-02 20:25:31 +03:00
|
|
|
}
|
|
|
|
}
|