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>
|
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>
|
2016-03-02 20:25:31 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
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 {
|
|
|
|
|
|
|
|
const NS_OWNCLOUD = 'http://owncloud.org/ns';
|
2019-07-22 15:47:17 +03:00
|
|
|
const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
|
2016-03-02 20:25:31 +03:00
|
|
|
const SHARETYPES_PROPERTYNAME = '{http://owncloud.org/ns}share-types';
|
2019-07-22 15:47:17 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\Share\IManager
|
|
|
|
*/
|
|
|
|
private $shareManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \Sabre\DAV\Tree
|
|
|
|
*/
|
|
|
|
private $tree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $userId;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OCP\Files\Folder
|
|
|
|
*/
|
|
|
|
private $userFolder;
|
|
|
|
|
2019-07-30 23:35:44 +03:00
|
|
|
/** @var IShare[] */
|
2019-07-30 23:59:22 +03:00
|
|
|
private $cachedShares = [];
|
2016-03-02 20:25:31 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-30 23:35:44 +03:00
|
|
|
private function getShare(\OCP\Files\Node $node): array {
|
|
|
|
$result = [];
|
|
|
|
$requestedShareTypes = [
|
|
|
|
\OCP\Share::SHARE_TYPE_USER,
|
|
|
|
\OCP\Share::SHARE_TYPE_GROUP,
|
|
|
|
\OCP\Share::SHARE_TYPE_LINK,
|
|
|
|
\OCP\Share::SHARE_TYPE_REMOTE,
|
|
|
|
\OCP\Share::SHARE_TYPE_EMAIL,
|
|
|
|
\OCP\Share::SHARE_TYPE_ROOM,
|
|
|
|
\OCP\Share::SHARE_TYPE_CIRCLE,
|
|
|
|
];
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSharesFolder(\OCP\Files\Folder $node): array {
|
|
|
|
return $this->shareManager->getSharesInFolder(
|
|
|
|
$this->userId,
|
|
|
|
$node,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-07-30 23:59:22 +03:00
|
|
|
private function getShares(\Sabre\DAV\INode $sabreNode): array {
|
|
|
|
if (isset($this->cachedShares[$sabreNode->getId()])) {
|
|
|
|
$shares = $this->cachedShares[$sabreNode->getId()];
|
|
|
|
} else {
|
|
|
|
list($parentPath,) = \Sabre\Uri\split($sabreNode->getPath());
|
|
|
|
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) {
|
|
|
|
$node = $this->userFolder->get($sabreNode->getPath());
|
|
|
|
$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
|
|
|
|
) {
|
|
|
|
if (!($sabreNode instanceof \OCA\DAV\Connector\Sabre\Node)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// need prefetch ?
|
|
|
|
if ($sabreNode instanceof \OCA\DAV\Connector\Sabre\Directory
|
|
|
|
&& $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
|
|
|
) {
|
2016-10-04 17:08:22 +03:00
|
|
|
$folderNode = $this->userFolder->get($sabreNode->getPath());
|
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
|
|
|
}
|
|
|
|
}
|