2016-02-03 23:55:04 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2016-02-03 23:55:04 +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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Connector\Sabre;
|
|
|
|
|
|
|
|
use OC\Files\View;
|
2019-03-13 02:42:39 +03:00
|
|
|
use OCP\App\IAppManager;
|
2016-02-03 23:55:04 +03:00
|
|
|
use Sabre\DAV\Exception\PreconditionFailed;
|
|
|
|
use Sabre\DAV\Exception\BadRequest;
|
|
|
|
use Sabre\DAV\ServerPlugin;
|
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
use Sabre\DAV\Xml\Element\Response;
|
|
|
|
use Sabre\DAV\Xml\Response\MultiStatus;
|
|
|
|
use Sabre\DAV\PropFind;
|
|
|
|
use OCP\SystemTag\ISystemTagObjectMapper;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\IGroupManager;
|
|
|
|
use OCP\SystemTag\ISystemTagManager;
|
|
|
|
use OCP\SystemTag\TagNotFoundException;
|
2016-09-12 18:09:46 +03:00
|
|
|
use OCP\ITagManager;
|
2016-02-03 23:55:04 +03:00
|
|
|
|
|
|
|
class FilesReportPlugin extends ServerPlugin {
|
|
|
|
|
|
|
|
// namespace
|
|
|
|
const NS_OWNCLOUD = 'http://owncloud.org/ns';
|
|
|
|
const REPORT_NAME = '{http://owncloud.org/ns}filter-files';
|
|
|
|
const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag';
|
2017-09-30 00:43:04 +03:00
|
|
|
const CIRCLE_PROPERTYNAME = '{http://owncloud.org/ns}circle';
|
2016-02-03 23:55:04 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reference to main server object
|
|
|
|
*
|
|
|
|
* @var \Sabre\DAV\Server
|
|
|
|
*/
|
|
|
|
private $server;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Tree
|
|
|
|
*/
|
|
|
|
private $tree;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var View
|
|
|
|
*/
|
|
|
|
private $fileView;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ISystemTagManager
|
|
|
|
*/
|
|
|
|
private $tagManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ISystemTagObjectMapper
|
|
|
|
*/
|
|
|
|
private $tagMapper;
|
|
|
|
|
2016-09-12 18:09:46 +03:00
|
|
|
/**
|
|
|
|
* Manager for private tags
|
|
|
|
*
|
|
|
|
* @var ITagManager
|
|
|
|
*/
|
|
|
|
private $fileTagger;
|
|
|
|
|
2016-02-03 23:55:04 +03:00
|
|
|
/**
|
|
|
|
* @var IUserSession
|
|
|
|
*/
|
|
|
|
private $userSession;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var IGroupManager
|
|
|
|
*/
|
|
|
|
private $groupManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Folder
|
|
|
|
*/
|
|
|
|
private $userFolder;
|
|
|
|
|
2019-03-13 02:42:39 +03:00
|
|
|
/**
|
|
|
|
* @var IAppManager
|
|
|
|
*/
|
|
|
|
private $appManager;
|
|
|
|
|
2016-02-03 23:55:04 +03:00
|
|
|
/**
|
|
|
|
* @param Tree $tree
|
|
|
|
* @param View $view
|
2016-09-12 18:09:46 +03:00
|
|
|
* @param ISystemTagManager $tagManager
|
|
|
|
* @param ISystemTagObjectMapper $tagMapper
|
|
|
|
* @param ITagManager $fileTagger manager for private tags
|
|
|
|
* @param IUserSession $userSession
|
|
|
|
* @param IGroupManager $groupManager
|
2016-11-08 12:03:25 +03:00
|
|
|
* @param Folder $userFolder
|
2019-03-13 02:42:39 +03:00
|
|
|
* @param IAppManager $appManager
|
2016-02-03 23:55:04 +03:00
|
|
|
*/
|
|
|
|
public function __construct(Tree $tree,
|
|
|
|
View $view,
|
|
|
|
ISystemTagManager $tagManager,
|
|
|
|
ISystemTagObjectMapper $tagMapper,
|
2016-09-12 18:09:46 +03:00
|
|
|
ITagManager $fileTagger,
|
2016-02-03 23:55:04 +03:00
|
|
|
IUserSession $userSession,
|
|
|
|
IGroupManager $groupManager,
|
2019-03-13 02:42:39 +03:00
|
|
|
Folder $userFolder,
|
|
|
|
IAppManager $appManager
|
2016-02-03 23:55:04 +03:00
|
|
|
) {
|
|
|
|
$this->tree = $tree;
|
|
|
|
$this->fileView = $view;
|
|
|
|
$this->tagManager = $tagManager;
|
|
|
|
$this->tagMapper = $tagMapper;
|
2016-09-12 18:09:46 +03:00
|
|
|
$this->fileTagger = $fileTagger;
|
2016-02-03 23:55:04 +03:00
|
|
|
$this->userSession = $userSession;
|
|
|
|
$this->groupManager = $groupManager;
|
|
|
|
$this->userFolder = $userFolder;
|
2019-03-13 02:42:39 +03:00
|
|
|
$this->appManager = $appManager;
|
2016-02-03 23:55:04 +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 \Sabre\DAV\Server $server
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function initialize(\Sabre\DAV\Server $server) {
|
|
|
|
|
|
|
|
$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
|
|
|
|
|
|
|
|
$this->server = $server;
|
|
|
|
$this->server->on('report', array($this, 'onReport'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of reports this plugin supports.
|
|
|
|
*
|
|
|
|
* This will be used in the {DAV:}supported-report-set property.
|
|
|
|
*
|
|
|
|
* @param string $uri
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getSupportedReportSet($uri) {
|
|
|
|
return [self::REPORT_NAME];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* REPORT operations to look for files
|
|
|
|
*
|
|
|
|
* @param string $reportName
|
2016-11-08 12:03:25 +03:00
|
|
|
* @param $report
|
2016-02-03 23:55:04 +03:00
|
|
|
* @param string $uri
|
|
|
|
* @return bool
|
2016-11-08 12:03:25 +03:00
|
|
|
* @throws BadRequest
|
|
|
|
* @throws PreconditionFailed
|
|
|
|
* @internal param $ [] $report
|
2016-02-03 23:55:04 +03:00
|
|
|
*/
|
|
|
|
public function onReport($reportName, $report, $uri) {
|
|
|
|
$reportTargetNode = $this->server->tree->getNodeForPath($uri);
|
|
|
|
if (!$reportTargetNode instanceof Directory || $reportName !== self::REPORT_NAME) {
|
2016-10-04 17:08:22 +03:00
|
|
|
return;
|
2016-02-03 23:55:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$ns = '{' . $this::NS_OWNCLOUD . '}';
|
|
|
|
$requestedProps = [];
|
|
|
|
$filterRules = [];
|
|
|
|
|
|
|
|
// parse report properties and gather filter info
|
|
|
|
foreach ($report as $reportProps) {
|
|
|
|
$name = $reportProps['name'];
|
|
|
|
if ($name === $ns . 'filter-rules') {
|
|
|
|
$filterRules = $reportProps['value'];
|
|
|
|
} else if ($name === '{DAV:}prop') {
|
|
|
|
// propfind properties
|
|
|
|
foreach ($reportProps['value'] as $propVal) {
|
|
|
|
$requestedProps[] = $propVal['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($filterRules)) {
|
|
|
|
// an empty filter would return all existing files which would be slow
|
|
|
|
throw new BadRequest('Missing filter-rule block in request');
|
|
|
|
}
|
|
|
|
|
|
|
|
// gather all file ids matching filter
|
|
|
|
try {
|
|
|
|
$resultFileIds = $this->processFilterRules($filterRules);
|
|
|
|
} catch (TagNotFoundException $e) {
|
|
|
|
throw new PreconditionFailed('Cannot filter by non-existing tag', 0, $e);
|
|
|
|
}
|
|
|
|
|
|
|
|
// find sabre nodes by file id, restricted to the root node path
|
|
|
|
$results = $this->findNodesByFileIds($reportTargetNode, $resultFileIds);
|
|
|
|
|
2016-10-04 17:08:22 +03:00
|
|
|
$filesUri = $this->getFilesBaseUri($uri, $reportTargetNode->getPath());
|
|
|
|
$responses = $this->prepareResponses($filesUri, $requestedProps, $results);
|
2016-02-03 23:55:04 +03:00
|
|
|
|
|
|
|
$xml = $this->server->xml->write(
|
|
|
|
'{DAV:}multistatus',
|
|
|
|
new MultiStatus($responses)
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->server->httpResponse->setStatus(207);
|
|
|
|
$this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
|
|
|
|
$this->server->httpResponse->setBody($xml);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-04 17:08:22 +03:00
|
|
|
/**
|
|
|
|
* Returns the base uri of the files root by removing
|
|
|
|
* the subpath from the URI
|
|
|
|
*
|
|
|
|
* @param string $uri URI from this request
|
|
|
|
* @param string $subPath subpath to remove from the URI
|
|
|
|
*
|
|
|
|
* @return string files base uri
|
|
|
|
*/
|
|
|
|
private function getFilesBaseUri($uri, $subPath) {
|
|
|
|
$uri = trim($uri, '/');
|
|
|
|
$subPath = trim($subPath, '/');
|
|
|
|
if (empty($subPath)) {
|
|
|
|
$filesUri = $uri;
|
|
|
|
} else {
|
|
|
|
$filesUri = substr($uri, 0, strlen($uri) - strlen($subPath));
|
|
|
|
}
|
|
|
|
$filesUri = trim($filesUri, '/');
|
|
|
|
if (empty($filesUri)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
return '/' . $filesUri;
|
|
|
|
}
|
|
|
|
|
2016-02-03 23:55:04 +03:00
|
|
|
/**
|
|
|
|
* Find file ids matching the given filter rules
|
|
|
|
*
|
|
|
|
* @param array $filterRules
|
|
|
|
* @return array array of unique file id results
|
|
|
|
*
|
|
|
|
* @throws TagNotFoundException whenever a tag was not found
|
|
|
|
*/
|
2016-02-22 17:26:59 +03:00
|
|
|
protected function processFilterRules($filterRules) {
|
2016-02-03 23:55:04 +03:00
|
|
|
$ns = '{' . $this::NS_OWNCLOUD . '}';
|
2016-02-09 14:07:30 +03:00
|
|
|
$resultFileIds = null;
|
2016-02-03 23:55:04 +03:00
|
|
|
$systemTagIds = [];
|
2017-09-30 00:43:04 +03:00
|
|
|
$circlesIds = [];
|
2016-09-12 18:09:46 +03:00
|
|
|
$favoriteFilter = null;
|
2016-02-03 23:55:04 +03:00
|
|
|
foreach ($filterRules as $filterRule) {
|
|
|
|
if ($filterRule['name'] === $ns . 'systemtag') {
|
|
|
|
$systemTagIds[] = $filterRule['value'];
|
|
|
|
}
|
2017-09-30 00:43:04 +03:00
|
|
|
if ($filterRule['name'] === self::CIRCLE_PROPERTYNAME) {
|
|
|
|
$circlesIds[] = $filterRule['value'];
|
|
|
|
}
|
2016-09-12 18:09:46 +03:00
|
|
|
if ($filterRule['name'] === $ns . 'favorite') {
|
|
|
|
$favoriteFilter = true;
|
|
|
|
}
|
2017-09-30 00:43:04 +03:00
|
|
|
|
2016-09-12 18:09:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($favoriteFilter !== null) {
|
|
|
|
$resultFileIds = $this->fileTagger->load('files')->getFavorites();
|
|
|
|
if (empty($resultFileIds)) {
|
|
|
|
return [];
|
|
|
|
}
|
2016-02-03 23:55:04 +03:00
|
|
|
}
|
|
|
|
|
2016-09-12 18:09:46 +03:00
|
|
|
if (!empty($systemTagIds)) {
|
|
|
|
$fileIds = $this->getSystemTagFileIds($systemTagIds);
|
|
|
|
if (empty($resultFileIds)) {
|
|
|
|
$resultFileIds = $fileIds;
|
|
|
|
} else {
|
|
|
|
$resultFileIds = array_intersect($fileIds, $resultFileIds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-30 00:43:04 +03:00
|
|
|
if (!empty($circlesIds)) {
|
|
|
|
$fileIds = $this->getCirclesFileIds($circlesIds);
|
|
|
|
if (empty($resultFileIds)) {
|
|
|
|
$resultFileIds = $fileIds;
|
|
|
|
} else {
|
|
|
|
$resultFileIds = array_intersect($fileIds, $resultFileIds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 18:09:46 +03:00
|
|
|
return $resultFileIds;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getSystemTagFileIds($systemTagIds) {
|
|
|
|
$resultFileIds = null;
|
|
|
|
|
2016-02-03 23:55:04 +03:00
|
|
|
// check user permissions, if applicable
|
|
|
|
if (!$this->isAdmin()) {
|
|
|
|
// check visibility/permission
|
|
|
|
$tags = $this->tagManager->getTagsByIds($systemTagIds);
|
|
|
|
$unknownTagIds = [];
|
|
|
|
foreach ($tags as $tag) {
|
|
|
|
if (!$tag->isUserVisible()) {
|
|
|
|
$unknownTagIds[] = $tag->getId();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($unknownTagIds)) {
|
|
|
|
throw new TagNotFoundException('Tag with ids ' . implode(', ', $unknownTagIds) . ' not found');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// fetch all file ids and intersect them
|
|
|
|
foreach ($systemTagIds as $systemTagId) {
|
|
|
|
$fileIds = $this->tagMapper->getObjectIdsForTags($systemTagId, 'files');
|
|
|
|
|
2016-02-09 13:39:22 +03:00
|
|
|
if (empty($fileIds)) {
|
2016-02-09 14:07:30 +03:00
|
|
|
// This tag has no files, nothing can ever show up
|
2016-02-09 13:39:22 +03:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// first run ?
|
2016-02-09 14:07:30 +03:00
|
|
|
if ($resultFileIds === null) {
|
2016-02-03 23:55:04 +03:00
|
|
|
$resultFileIds = $fileIds;
|
|
|
|
} else {
|
|
|
|
$resultFileIds = array_intersect($resultFileIds, $fileIds);
|
|
|
|
}
|
2016-02-09 14:07:30 +03:00
|
|
|
|
|
|
|
if (empty($resultFileIds)) {
|
|
|
|
// Empty intersection, nothing can show up anymore
|
|
|
|
return [];
|
|
|
|
}
|
2016-02-03 23:55:04 +03:00
|
|
|
}
|
|
|
|
return $resultFileIds;
|
|
|
|
}
|
|
|
|
|
2017-09-30 00:43:04 +03:00
|
|
|
/**
|
|
|
|
* @suppress PhanUndeclaredClassMethod
|
|
|
|
* @param array $circlesIds
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getCirclesFileIds(array $circlesIds) {
|
2019-03-14 01:01:47 +03:00
|
|
|
if (!$this->appManager->isEnabledForUser('circles') || !class_exists('\OCA\Circles\Api\v1\Circles')) {
|
|
|
|
return [];
|
2017-09-30 00:43:04 +03:00
|
|
|
}
|
|
|
|
return \OCA\Circles\Api\v1\Circles::getFilesForCircles($circlesIds);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-03 23:55:04 +03:00
|
|
|
/**
|
|
|
|
* Prepare propfind response for the given nodes
|
|
|
|
*
|
2016-10-04 17:08:22 +03:00
|
|
|
* @param string $filesUri $filesUri URI leading to root of the files URI,
|
|
|
|
* with a leading slash but no trailing slash
|
2016-02-03 23:55:04 +03:00
|
|
|
* @param string[] $requestedProps requested properties
|
|
|
|
* @param Node[] nodes nodes for which to fetch and prepare responses
|
|
|
|
* @return Response[]
|
|
|
|
*/
|
2016-10-04 17:08:22 +03:00
|
|
|
public function prepareResponses($filesUri, $requestedProps, $nodes) {
|
2016-02-03 23:55:04 +03:00
|
|
|
$responses = [];
|
|
|
|
foreach ($nodes as $node) {
|
2016-10-04 17:08:22 +03:00
|
|
|
$propFind = new PropFind($filesUri . $node->getPath(), $requestedProps);
|
2016-02-03 23:55:04 +03:00
|
|
|
|
|
|
|
$this->server->getPropertiesByNode($propFind, $node);
|
|
|
|
// copied from Sabre Server's getPropertiesForPath
|
|
|
|
$result = $propFind->getResultForMultiStatus();
|
|
|
|
$result['href'] = $propFind->getPath();
|
|
|
|
|
|
|
|
$resourceType = $this->server->getResourceTypeForNode($node);
|
|
|
|
if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
|
|
|
|
$result['href'] .= '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
$responses[] = new Response(
|
2016-10-04 17:08:22 +03:00
|
|
|
rtrim($this->server->getBaseUri(), '/') . $filesUri . $node->getPath(),
|
2016-02-03 23:55:04 +03:00
|
|
|
$result,
|
|
|
|
200
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return $responses;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find Sabre nodes by file ids
|
|
|
|
*
|
|
|
|
* @param Node $rootNode root node for search
|
|
|
|
* @param array $fileIds file ids
|
|
|
|
* @return Node[] array of Sabre nodes
|
|
|
|
*/
|
|
|
|
public function findNodesByFileIds($rootNode, $fileIds) {
|
|
|
|
$folder = $this->userFolder;
|
|
|
|
if (trim($rootNode->getPath(), '/') !== '') {
|
|
|
|
$folder = $folder->get($rootNode->getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
foreach ($fileIds as $fileId) {
|
|
|
|
$entry = $folder->getById($fileId);
|
|
|
|
if ($entry) {
|
|
|
|
$entry = current($entry);
|
|
|
|
if ($entry instanceof \OCP\Files\File) {
|
|
|
|
$results[] = new File($this->fileView, $entry);
|
|
|
|
} else if ($entry instanceof \OCP\Files\Folder) {
|
|
|
|
$results[] = new Directory($this->fileView, $entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether the currently logged in user is an administrator
|
|
|
|
*/
|
|
|
|
private function isAdmin() {
|
|
|
|
$user = $this->userSession->getUser();
|
|
|
|
if ($user !== null) {
|
|
|
|
return $this->groupManager->isAdmin($user->getUID());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|