filesreportplugin.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * @author Vincent Petry <pvince81@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\DAV\Connector\Sabre;
  22. use OC\Files\View;
  23. use Sabre\DAV\Exception\NotFound;
  24. use Sabre\DAV\Exception\PreconditionFailed;
  25. use Sabre\DAV\Exception\ReportNotSupported;
  26. use Sabre\DAV\Exception\BadRequest;
  27. use Sabre\DAV\ServerPlugin;
  28. use Sabre\DAV\Tree;
  29. use Sabre\DAV\Xml\Element\Response;
  30. use Sabre\DAV\Xml\Response\MultiStatus;
  31. use Sabre\DAV\PropFind;
  32. use OCP\SystemTag\ISystemTagObjectMapper;
  33. use OCP\IUserSession;
  34. use OCP\Files\Folder;
  35. use OCP\IGroupManager;
  36. use OCP\SystemTag\ISystemTagManager;
  37. use OCP\SystemTag\TagNotFoundException;
  38. class FilesReportPlugin extends ServerPlugin {
  39. // namespace
  40. const NS_OWNCLOUD = 'http://owncloud.org/ns';
  41. const REPORT_NAME = '{http://owncloud.org/ns}filter-files';
  42. const SYSTEMTAG_PROPERTYNAME = '{http://owncloud.org/ns}systemtag';
  43. /**
  44. * Reference to main server object
  45. *
  46. * @var \Sabre\DAV\Server
  47. */
  48. private $server;
  49. /**
  50. * @var Tree
  51. */
  52. private $tree;
  53. /**
  54. * @var View
  55. */
  56. private $fileView;
  57. /**
  58. * @var ISystemTagManager
  59. */
  60. private $tagManager;
  61. /**
  62. * @var ISystemTagObjectMapper
  63. */
  64. private $tagMapper;
  65. /**
  66. * @var IUserSession
  67. */
  68. private $userSession;
  69. /**
  70. * @var IGroupManager
  71. */
  72. private $groupManager;
  73. /**
  74. * @var Folder
  75. */
  76. private $userFolder;
  77. /**
  78. * @param Tree $tree
  79. * @param View $view
  80. */
  81. public function __construct(Tree $tree,
  82. View $view,
  83. ISystemTagManager $tagManager,
  84. ISystemTagObjectMapper $tagMapper,
  85. IUserSession $userSession,
  86. IGroupManager $groupManager,
  87. Folder $userFolder
  88. ) {
  89. $this->tree = $tree;
  90. $this->fileView = $view;
  91. $this->tagManager = $tagManager;
  92. $this->tagMapper = $tagMapper;
  93. $this->userSession = $userSession;
  94. $this->groupManager = $groupManager;
  95. $this->userFolder = $userFolder;
  96. }
  97. /**
  98. * This initializes the plugin.
  99. *
  100. * This function is called by \Sabre\DAV\Server, after
  101. * addPlugin is called.
  102. *
  103. * This method should set up the required event subscriptions.
  104. *
  105. * @param \Sabre\DAV\Server $server
  106. * @return void
  107. */
  108. public function initialize(\Sabre\DAV\Server $server) {
  109. $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
  110. $this->server = $server;
  111. $this->server->on('report', array($this, 'onReport'));
  112. }
  113. /**
  114. * Returns a list of reports this plugin supports.
  115. *
  116. * This will be used in the {DAV:}supported-report-set property.
  117. *
  118. * @param string $uri
  119. * @return array
  120. */
  121. public function getSupportedReportSet($uri) {
  122. return [self::REPORT_NAME];
  123. }
  124. /**
  125. * REPORT operations to look for files
  126. *
  127. * @param string $reportName
  128. * @param [] $report
  129. * @param string $uri
  130. * @return bool
  131. * @throws NotFound
  132. * @throws ReportNotSupported
  133. */
  134. public function onReport($reportName, $report, $uri) {
  135. $reportTargetNode = $this->server->tree->getNodeForPath($uri);
  136. if (!$reportTargetNode instanceof Directory || $reportName !== self::REPORT_NAME) {
  137. throw new ReportNotSupported();
  138. }
  139. $ns = '{' . $this::NS_OWNCLOUD . '}';
  140. $requestedProps = [];
  141. $filterRules = [];
  142. // parse report properties and gather filter info
  143. foreach ($report as $reportProps) {
  144. $name = $reportProps['name'];
  145. if ($name === $ns . 'filter-rules') {
  146. $filterRules = $reportProps['value'];
  147. } else if ($name === '{DAV:}prop') {
  148. // propfind properties
  149. foreach ($reportProps['value'] as $propVal) {
  150. $requestedProps[] = $propVal['name'];
  151. }
  152. }
  153. }
  154. if (empty($filterRules)) {
  155. // an empty filter would return all existing files which would be slow
  156. throw new BadRequest('Missing filter-rule block in request');
  157. }
  158. // gather all file ids matching filter
  159. try {
  160. $resultFileIds = $this->processFilterRules($filterRules);
  161. } catch (TagNotFoundException $e) {
  162. throw new PreconditionFailed('Cannot filter by non-existing tag', 0, $e);
  163. }
  164. // find sabre nodes by file id, restricted to the root node path
  165. $results = $this->findNodesByFileIds($reportTargetNode, $resultFileIds);
  166. $responses = $this->prepareResponses($requestedProps, $results);
  167. $xml = $this->server->xml->write(
  168. '{DAV:}multistatus',
  169. new MultiStatus($responses)
  170. );
  171. $this->server->httpResponse->setStatus(207);
  172. $this->server->httpResponse->setHeader('Content-Type', 'application/xml; charset=utf-8');
  173. $this->server->httpResponse->setBody($xml);
  174. return false;
  175. }
  176. /**
  177. * Find file ids matching the given filter rules
  178. *
  179. * @param array $filterRules
  180. * @return array array of unique file id results
  181. *
  182. * @throws TagNotFoundException whenever a tag was not found
  183. */
  184. public function processFilterRules($filterRules) {
  185. $ns = '{' . $this::NS_OWNCLOUD . '}';
  186. $resultFileIds = null;
  187. $systemTagIds = [];
  188. foreach ($filterRules as $filterRule) {
  189. if ($filterRule['name'] === $ns . 'systemtag') {
  190. $systemTagIds[] = $filterRule['value'];
  191. }
  192. }
  193. // check user permissions, if applicable
  194. if (!$this->isAdmin()) {
  195. // check visibility/permission
  196. $tags = $this->tagManager->getTagsByIds($systemTagIds);
  197. $unknownTagIds = [];
  198. foreach ($tags as $tag) {
  199. if (!$tag->isUserVisible()) {
  200. $unknownTagIds[] = $tag->getId();
  201. }
  202. }
  203. if (!empty($unknownTagIds)) {
  204. throw new TagNotFoundException('Tag with ids ' . implode(', ', $unknownTagIds) . ' not found');
  205. }
  206. }
  207. // fetch all file ids and intersect them
  208. foreach ($systemTagIds as $systemTagId) {
  209. $fileIds = $this->tagMapper->getObjectIdsForTags($systemTagId, 'files');
  210. if (empty($fileIds)) {
  211. // This tag has no files, nothing can ever show up
  212. return [];
  213. }
  214. // first run ?
  215. if ($resultFileIds === null) {
  216. $resultFileIds = $fileIds;
  217. } else {
  218. $resultFileIds = array_intersect($resultFileIds, $fileIds);
  219. }
  220. if (empty($resultFileIds)) {
  221. // Empty intersection, nothing can show up anymore
  222. return [];
  223. }
  224. }
  225. return $resultFileIds;
  226. }
  227. /**
  228. * Prepare propfind response for the given nodes
  229. *
  230. * @param string[] $requestedProps requested properties
  231. * @param Node[] nodes nodes for which to fetch and prepare responses
  232. * @return Response[]
  233. */
  234. public function prepareResponses($requestedProps, $nodes) {
  235. $responses = [];
  236. foreach ($nodes as $node) {
  237. $propFind = new PropFind($node->getPath(), $requestedProps);
  238. $this->server->getPropertiesByNode($propFind, $node);
  239. // copied from Sabre Server's getPropertiesForPath
  240. $result = $propFind->getResultForMultiStatus();
  241. $result['href'] = $propFind->getPath();
  242. $resourceType = $this->server->getResourceTypeForNode($node);
  243. if (in_array('{DAV:}collection', $resourceType) || in_array('{DAV:}principal', $resourceType)) {
  244. $result['href'] .= '/';
  245. }
  246. $responses[] = new Response(
  247. rtrim($this->server->getBaseUri(), '/') . $node->getPath(),
  248. $result,
  249. 200
  250. );
  251. }
  252. return $responses;
  253. }
  254. /**
  255. * Find Sabre nodes by file ids
  256. *
  257. * @param Node $rootNode root node for search
  258. * @param array $fileIds file ids
  259. * @return Node[] array of Sabre nodes
  260. */
  261. public function findNodesByFileIds($rootNode, $fileIds) {
  262. $folder = $this->userFolder;
  263. if (trim($rootNode->getPath(), '/') !== '') {
  264. $folder = $folder->get($rootNode->getPath());
  265. }
  266. $results = [];
  267. foreach ($fileIds as $fileId) {
  268. $entry = $folder->getById($fileId);
  269. if ($entry) {
  270. $entry = current($entry);
  271. if ($entry instanceof \OCP\Files\File) {
  272. $results[] = new File($this->fileView, $entry);
  273. } else if ($entry instanceof \OCP\Files\Folder) {
  274. $results[] = new Directory($this->fileView, $entry);
  275. }
  276. }
  277. }
  278. return $results;
  279. }
  280. /**
  281. * Returns whether the currently logged in user is an administrator
  282. */
  283. private function isAdmin() {
  284. $user = $this->userSession->getUser();
  285. if ($user !== null) {
  286. return $this->groupManager->isAdmin($user->getUID());
  287. }
  288. return false;
  289. }
  290. }