FilesPlugin.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Michael Jobst <mjobst+github@tecratech.de>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Robin McCorkell <robin@mccorkell.me.uk>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Tobias Kaminsky <tobias@kaminsky.me>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OCA\DAV\Connector\Sabre;
  35. use OC\AppFramework\Http\Request;
  36. use OC\Metadata\IMetadataManager;
  37. use OCP\Constants;
  38. use OCP\Files\ForbiddenException;
  39. use OCP\Files\StorageNotAvailableException;
  40. use OCP\IConfig;
  41. use OCP\IPreview;
  42. use OCP\IRequest;
  43. use OCP\IUserSession;
  44. use Psr\Log\LoggerInterface;
  45. use Sabre\DAV\Exception\Forbidden;
  46. use Sabre\DAV\Exception\NotFound;
  47. use Sabre\DAV\IFile;
  48. use Sabre\DAV\PropFind;
  49. use Sabre\DAV\PropPatch;
  50. use Sabre\DAV\ServerPlugin;
  51. use Sabre\DAV\Server;
  52. use Sabre\DAV\Tree;
  53. use Sabre\HTTP\RequestInterface;
  54. use Sabre\HTTP\ResponseInterface;
  55. class FilesPlugin extends ServerPlugin {
  56. // namespace
  57. public const NS_OWNCLOUD = 'http://owncloud.org/ns';
  58. public const NS_NEXTCLOUD = 'http://nextcloud.org/ns';
  59. public const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id';
  60. public const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid';
  61. public const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions';
  62. public const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions';
  63. public const OCM_SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-cloud-mesh.org/ns}share-permissions';
  64. public const SHARE_ATTRIBUTES_PROPERTYNAME = '{http://nextcloud.org/ns}share-attributes';
  65. public const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL';
  66. public const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size';
  67. public const GETETAG_PROPERTYNAME = '{DAV:}getetag';
  68. public const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified';
  69. public const CREATIONDATE_PROPERTYNAME = '{DAV:}creationdate';
  70. public const DISPLAYNAME_PROPERTYNAME = '{DAV:}displayname';
  71. public const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id';
  72. public const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name';
  73. public const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums';
  74. public const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint';
  75. public const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview';
  76. public const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type';
  77. public const IS_ENCRYPTED_PROPERTYNAME = '{http://nextcloud.org/ns}is-encrypted';
  78. public const METADATA_ETAG_PROPERTYNAME = '{http://nextcloud.org/ns}metadata_etag';
  79. public const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time';
  80. public const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time';
  81. public const SHARE_NOTE = '{http://nextcloud.org/ns}note';
  82. public const SUBFOLDER_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-folder-count';
  83. public const SUBFILE_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-file-count';
  84. public const FILE_METADATA_SIZE = '{http://nextcloud.org/ns}file-metadata-size';
  85. /** Reference to main server object */
  86. private ?Server $server = null;
  87. private Tree $tree;
  88. private IUserSession $userSession;
  89. /**
  90. * Whether this is public webdav.
  91. * If true, some returned information will be stripped off.
  92. */
  93. private bool $isPublic;
  94. private bool $downloadAttachment;
  95. private IConfig $config;
  96. private IRequest $request;
  97. private IPreview $previewManager;
  98. public function __construct(Tree $tree,
  99. IConfig $config,
  100. IRequest $request,
  101. IPreview $previewManager,
  102. IUserSession $userSession,
  103. bool $isPublic = false,
  104. bool $downloadAttachment = true) {
  105. $this->tree = $tree;
  106. $this->config = $config;
  107. $this->request = $request;
  108. $this->userSession = $userSession;
  109. $this->isPublic = $isPublic;
  110. $this->downloadAttachment = $downloadAttachment;
  111. $this->previewManager = $previewManager;
  112. }
  113. /**
  114. * This initializes the plugin.
  115. *
  116. * This function is called by \Sabre\DAV\Server, after
  117. * addPlugin is called.
  118. *
  119. * This method should set up the required event subscriptions.
  120. *
  121. * @return void
  122. */
  123. public function initialize(Server $server) {
  124. $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
  125. $server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc';
  126. $server->protectedProperties[] = self::FILEID_PROPERTYNAME;
  127. $server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME;
  128. $server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME;
  129. $server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME;
  130. $server->protectedProperties[] = self::OCM_SHARE_PERMISSIONS_PROPERTYNAME;
  131. $server->protectedProperties[] = self::SHARE_ATTRIBUTES_PROPERTYNAME;
  132. $server->protectedProperties[] = self::SIZE_PROPERTYNAME;
  133. $server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME;
  134. $server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME;
  135. $server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME;
  136. $server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME;
  137. $server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME;
  138. $server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME;
  139. $server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME;
  140. $server->protectedProperties[] = self::IS_ENCRYPTED_PROPERTYNAME;
  141. $server->protectedProperties[] = self::SHARE_NOTE;
  142. // normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
  143. $allowedProperties = ['{DAV:}getetag'];
  144. $server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties);
  145. $this->server = $server;
  146. $this->server->on('propFind', [$this, 'handleGetProperties']);
  147. $this->server->on('propPatch', [$this, 'handleUpdateProperties']);
  148. $this->server->on('afterBind', [$this, 'sendFileIdHeader']);
  149. $this->server->on('afterWriteContent', [$this, 'sendFileIdHeader']);
  150. $this->server->on('afterMethod:GET', [$this,'httpGet']);
  151. $this->server->on('afterMethod:GET', [$this, 'handleDownloadToken']);
  152. $this->server->on('afterResponse', function ($request, ResponseInterface $response) {
  153. $body = $response->getBody();
  154. if (is_resource($body)) {
  155. fclose($body);
  156. }
  157. });
  158. $this->server->on('beforeMove', [$this, 'checkMove']);
  159. }
  160. /**
  161. * Plugin that checks if a move can actually be performed.
  162. *
  163. * @param string $source source path
  164. * @param string $destination destination path
  165. * @throws Forbidden
  166. * @throws NotFound
  167. */
  168. public function checkMove($source, $destination) {
  169. $sourceNode = $this->tree->getNodeForPath($source);
  170. if (!$sourceNode instanceof Node) {
  171. return;
  172. }
  173. [$sourceDir,] = \Sabre\Uri\split($source);
  174. [$destinationDir,] = \Sabre\Uri\split($destination);
  175. if ($sourceDir !== $destinationDir) {
  176. $sourceNodeFileInfo = $sourceNode->getFileInfo();
  177. if ($sourceNodeFileInfo === null) {
  178. throw new NotFound($source . ' does not exist');
  179. }
  180. if (!$sourceNodeFileInfo->isDeletable()) {
  181. throw new Forbidden($source . " cannot be deleted");
  182. }
  183. }
  184. }
  185. /**
  186. * This sets a cookie to be able to recognize the start of the download
  187. * the content must not be longer than 32 characters and must only contain
  188. * alphanumeric characters
  189. *
  190. * @param RequestInterface $request
  191. * @param ResponseInterface $response
  192. */
  193. public function handleDownloadToken(RequestInterface $request, ResponseInterface $response) {
  194. $queryParams = $request->getQueryParameters();
  195. /**
  196. * this sets a cookie to be able to recognize the start of the download
  197. * the content must not be longer than 32 characters and must only contain
  198. * alphanumeric characters
  199. */
  200. if (isset($queryParams['downloadStartSecret'])) {
  201. $token = $queryParams['downloadStartSecret'];
  202. if (!isset($token[32])
  203. && preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) {
  204. // FIXME: use $response->setHeader() instead
  205. setcookie('ocDownloadStarted', $token, time() + 20, '/');
  206. }
  207. }
  208. }
  209. /**
  210. * Add headers to file download
  211. *
  212. * @param RequestInterface $request
  213. * @param ResponseInterface $response
  214. */
  215. public function httpGet(RequestInterface $request, ResponseInterface $response) {
  216. // Only handle valid files
  217. $node = $this->tree->getNodeForPath($request->getPath());
  218. if (!($node instanceof IFile)) {
  219. return;
  220. }
  221. // adds a 'Content-Disposition: attachment' header in case no disposition
  222. // header has been set before
  223. if ($this->downloadAttachment &&
  224. $response->getHeader('Content-Disposition') === null) {
  225. $filename = $node->getName();
  226. if ($this->request->isUserAgent(
  227. [
  228. Request::USER_AGENT_IE,
  229. Request::USER_AGENT_ANDROID_MOBILE_CHROME,
  230. Request::USER_AGENT_FREEBOX,
  231. ])) {
  232. $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"');
  233. } else {
  234. $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename)
  235. . '; filename="' . rawurlencode($filename) . '"');
  236. }
  237. }
  238. if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
  239. //Add OC-Checksum header
  240. $checksum = $node->getChecksum();
  241. if ($checksum !== null && $checksum !== '') {
  242. $response->addHeader('OC-Checksum', $checksum);
  243. }
  244. }
  245. $response->addHeader('X-Accel-Buffering', 'no');
  246. }
  247. /**
  248. * Adds all ownCloud-specific properties
  249. *
  250. * @param PropFind $propFind
  251. * @param \Sabre\DAV\INode $node
  252. * @return void
  253. */
  254. public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
  255. $httpRequest = $this->server->httpRequest;
  256. if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
  257. /**
  258. * This was disabled, because it made dir listing throw an exception,
  259. * so users were unable to navigate into folders where one subitem
  260. * is blocked by the files_accesscontrol app, see:
  261. * https://github.com/nextcloud/files_accesscontrol/issues/65
  262. * if (!$node->getFileInfo()->isReadable()) {
  263. * // avoid detecting files through this means
  264. * throw new NotFound();
  265. * }
  266. */
  267. $propFind->handle(self::FILEID_PROPERTYNAME, function () use ($node) {
  268. return $node->getFileId();
  269. });
  270. $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) {
  271. return $node->getInternalFileId();
  272. });
  273. $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function () use ($node) {
  274. $perms = $node->getDavPermissions();
  275. if ($this->isPublic) {
  276. // remove mount information
  277. $perms = str_replace(['S', 'M'], '', $perms);
  278. }
  279. return $perms;
  280. });
  281. $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) {
  282. $user = $this->userSession->getUser();
  283. if ($user === null) {
  284. return null;
  285. }
  286. return $node->getSharePermissions(
  287. $user->getUID()
  288. );
  289. });
  290. $propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest): ?string {
  291. $user = $this->userSession->getUser();
  292. if ($user === null) {
  293. return null;
  294. }
  295. $ncPermissions = $node->getSharePermissions(
  296. $user->getUID()
  297. );
  298. $ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions);
  299. return json_encode($ocmPermissions, JSON_THROW_ON_ERROR);
  300. });
  301. $propFind->handle(self::SHARE_ATTRIBUTES_PROPERTYNAME, function () use ($node, $httpRequest) {
  302. return json_encode($node->getShareAttributes(), JSON_THROW_ON_ERROR);
  303. });
  304. $propFind->handle(self::GETETAG_PROPERTYNAME, function () use ($node): string {
  305. return $node->getETag();
  306. });
  307. $propFind->handle(self::OWNER_ID_PROPERTYNAME, function () use ($node): ?string {
  308. $owner = $node->getOwner();
  309. if (!$owner) {
  310. return null;
  311. } else {
  312. return $owner->getUID();
  313. }
  314. });
  315. $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function () use ($node): ?string {
  316. $owner = $node->getOwner();
  317. if (!$owner) {
  318. return null;
  319. } else {
  320. return $owner->getDisplayName();
  321. }
  322. });
  323. $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) {
  324. return json_encode($this->previewManager->isAvailable($node->getFileInfo()), JSON_THROW_ON_ERROR);
  325. });
  326. $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node): int|float {
  327. return $node->getSize();
  328. });
  329. $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) {
  330. return $node->getFileInfo()->getMountPoint()->getMountType();
  331. });
  332. $propFind->handle(self::SHARE_NOTE, function () use ($node, $httpRequest): ?string {
  333. $user = $this->userSession->getUser();
  334. if ($user === null) {
  335. return null;
  336. }
  337. return $node->getNoteFromShare(
  338. $user->getUID()
  339. );
  340. });
  341. $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function () use ($node) {
  342. return $this->config->getSystemValue('data-fingerprint', '');
  343. });
  344. $propFind->handle(self::CREATIONDATE_PROPERTYNAME, function () use ($node) {
  345. return (new \DateTimeImmutable())
  346. ->setTimestamp($node->getFileInfo()->getCreationTime())
  347. ->format(\DateTimeInterface::ATOM);
  348. });
  349. $propFind->handle(self::CREATION_TIME_PROPERTYNAME, function () use ($node) {
  350. return $node->getFileInfo()->getCreationTime();
  351. });
  352. /**
  353. * Return file/folder name as displayname. The primary reason to
  354. * implement it this way is to avoid costly fallback to
  355. * CustomPropertiesBackend (esp. visible when querying all files
  356. * in a folder).
  357. */
  358. $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function () use ($node) {
  359. return $node->getName();
  360. });
  361. }
  362. if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
  363. $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function () use ($node) {
  364. try {
  365. $directDownloadUrl = $node->getDirectDownload();
  366. if (isset($directDownloadUrl['url'])) {
  367. return $directDownloadUrl['url'];
  368. }
  369. } catch (StorageNotAvailableException $e) {
  370. return false;
  371. } catch (ForbiddenException $e) {
  372. return false;
  373. }
  374. return false;
  375. });
  376. $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function () use ($node) {
  377. $checksum = $node->getChecksum();
  378. if ($checksum === null || $checksum === '') {
  379. return null;
  380. }
  381. return new ChecksumList($checksum);
  382. });
  383. $propFind->handle(self::UPLOAD_TIME_PROPERTYNAME, function () use ($node) {
  384. return $node->getFileInfo()->getUploadTime();
  385. });
  386. if ($this->config->getSystemValueBool('enable_file_metadata', true)) {
  387. $propFind->handle(self::FILE_METADATA_SIZE, function () use ($node) {
  388. if (!str_starts_with($node->getFileInfo()->getMimetype(), 'image')) {
  389. return json_encode((object)[], JSON_THROW_ON_ERROR);
  390. }
  391. if ($node->hasMetadata('size')) {
  392. $sizeMetadata = $node->getMetadata('size');
  393. } else {
  394. // This code path should not be called since we try to preload
  395. // the metadata when loading the folder or the search results
  396. // in one go
  397. $metadataManager = \OC::$server->get(IMetadataManager::class);
  398. $sizeMetadata = $metadataManager->fetchMetadataFor('size', [$node->getId()])[$node->getId()];
  399. // TODO would be nice to display this in the profiler...
  400. \OC::$server->get(LoggerInterface::class)->debug('Inefficient fetching of metadata');
  401. }
  402. return $sizeMetadata->getValue();
  403. });
  404. }
  405. }
  406. if ($node instanceof Directory) {
  407. $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node) {
  408. return $node->getSize();
  409. });
  410. $propFind->handle(self::IS_ENCRYPTED_PROPERTYNAME, function () use ($node) {
  411. return $node->getFileInfo()->isEncrypted() ? '1' : '0';
  412. });
  413. $requestProperties = $propFind->getRequestedProperties();
  414. // TODO detect dynamically which metadata groups are requested and
  415. // preload all of them and not just size
  416. if ($this->config->getSystemValueBool('enable_file_metadata', true)
  417. && in_array(self::FILE_METADATA_SIZE, $requestProperties, true)) {
  418. // Preloading of the metadata
  419. $fileIds = [];
  420. foreach ($node->getChildren() as $child) {
  421. /** @var \OCP\Files\Node|Node $child */
  422. if (str_starts_with($child->getFileInfo()->getMimeType(), 'image/')) {
  423. /** @var File $child */
  424. $fileIds[] = $child->getFileInfo()->getId();
  425. }
  426. }
  427. /** @var IMetaDataManager $metadataManager */
  428. $metadataManager = \OC::$server->get(IMetadataManager::class);
  429. $preloadedMetadata = $metadataManager->fetchMetadataFor('size', $fileIds);
  430. foreach ($node->getChildren() as $child) {
  431. /** @var \OCP\Files\Node|Node $child */
  432. if (str_starts_with($child->getFileInfo()->getMimeType(), 'image')) {
  433. /** @var File $child */
  434. $child->setMetadata('size', $preloadedMetadata[$child->getFileInfo()->getId()]);
  435. }
  436. }
  437. }
  438. if (in_array(self::SUBFILE_COUNT_PROPERTYNAME, $requestProperties, true)
  439. || in_array(self::SUBFOLDER_COUNT_PROPERTYNAME, $requestProperties, true)) {
  440. $nbFiles = 0;
  441. $nbFolders = 0;
  442. foreach ($node->getChildren() as $child) {
  443. if ($child instanceof File) {
  444. $nbFiles++;
  445. } elseif ($child instanceof Directory) {
  446. $nbFolders++;
  447. }
  448. }
  449. $propFind->handle(self::SUBFILE_COUNT_PROPERTYNAME, $nbFiles);
  450. $propFind->handle(self::SUBFOLDER_COUNT_PROPERTYNAME, $nbFolders);
  451. }
  452. }
  453. }
  454. /**
  455. * translate Nextcloud permissions to OCM Permissions
  456. *
  457. * @param $ncPermissions
  458. * @return array
  459. */
  460. protected function ncPermissions2ocmPermissions($ncPermissions) {
  461. $ocmPermissions = [];
  462. if ($ncPermissions & Constants::PERMISSION_SHARE) {
  463. $ocmPermissions[] = 'share';
  464. }
  465. if ($ncPermissions & Constants::PERMISSION_READ) {
  466. $ocmPermissions[] = 'read';
  467. }
  468. if (($ncPermissions & Constants::PERMISSION_CREATE) ||
  469. ($ncPermissions & Constants::PERMISSION_UPDATE)) {
  470. $ocmPermissions[] = 'write';
  471. }
  472. return $ocmPermissions;
  473. }
  474. /**
  475. * Update ownCloud-specific properties
  476. *
  477. * @param string $path
  478. * @param PropPatch $propPatch
  479. *
  480. * @return void
  481. */
  482. public function handleUpdateProperties($path, PropPatch $propPatch) {
  483. $node = $this->tree->getNodeForPath($path);
  484. if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
  485. return;
  486. }
  487. $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function ($time) use ($node) {
  488. if (empty($time)) {
  489. return false;
  490. }
  491. $node->touch($time);
  492. return true;
  493. });
  494. $propPatch->handle(self::GETETAG_PROPERTYNAME, function ($etag) use ($node) {
  495. if (empty($etag)) {
  496. return false;
  497. }
  498. return $node->setEtag($etag) !== -1;
  499. });
  500. $propPatch->handle(self::CREATIONDATE_PROPERTYNAME, function ($time) use ($node) {
  501. if (empty($time)) {
  502. return false;
  503. }
  504. $dateTime = new \DateTimeImmutable($time);
  505. $node->setCreationTime($dateTime->getTimestamp());
  506. return true;
  507. });
  508. $propPatch->handle(self::CREATION_TIME_PROPERTYNAME, function ($time) use ($node) {
  509. if (empty($time)) {
  510. return false;
  511. }
  512. $node->setCreationTime((int) $time);
  513. return true;
  514. });
  515. /**
  516. * Disable modification of the displayname property for files and
  517. * folders via PROPPATCH. See PROPFIND for more information.
  518. */
  519. $propPatch->handle(self::DISPLAYNAME_PROPERTYNAME, function ($displayName) {
  520. return 403;
  521. });
  522. }
  523. /**
  524. * @param string $filePath
  525. * @param \Sabre\DAV\INode $node
  526. * @throws \Sabre\DAV\Exception\BadRequest
  527. */
  528. public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
  529. // chunked upload handling
  530. if (isset($_SERVER['HTTP_OC_CHUNKED'])) {
  531. [$path, $name] = \Sabre\Uri\split($filePath);
  532. $info = \OC_FileChunking::decodeName($name);
  533. if (!empty($info)) {
  534. $filePath = $path . '/' . $info['name'];
  535. }
  536. }
  537. // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
  538. if (!$this->server->tree->nodeExists($filePath)) {
  539. return;
  540. }
  541. $node = $this->server->tree->getNodeForPath($filePath);
  542. if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
  543. $fileId = $node->getFileId();
  544. if (!is_null($fileId)) {
  545. $this->server->httpResponse->setHeader('OC-FileId', $fileId);
  546. }
  547. }
  548. }
  549. }