Node.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Connector\Sabre;
  8. use OC\Files\Mount\MoveableMount;
  9. use OC\Files\Node\File;
  10. use OC\Files\Node\Folder;
  11. use OC\Files\View;
  12. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  13. use OCP\Files\DavUtil;
  14. use OCP\Files\FileInfo;
  15. use OCP\Files\IRootFolder;
  16. use OCP\Files\NotFoundException;
  17. use OCP\Files\Storage\ISharedStorage;
  18. use OCP\Files\StorageNotAvailableException;
  19. use OCP\Share\Exceptions\ShareNotFound;
  20. use OCP\Share\IManager;
  21. abstract class Node implements \Sabre\DAV\INode {
  22. /**
  23. * @var View
  24. */
  25. protected $fileView;
  26. /**
  27. * The path to the current node
  28. *
  29. * @var string
  30. */
  31. protected $path;
  32. /**
  33. * node properties cache
  34. *
  35. * @var array
  36. */
  37. protected $property_cache = null;
  38. protected FileInfo $info;
  39. /**
  40. * @var IManager
  41. */
  42. protected $shareManager;
  43. protected \OCP\Files\Node $node;
  44. /**
  45. * Sets up the node, expects a full path name
  46. */
  47. public function __construct(View $view, FileInfo $info, ?IManager $shareManager = null) {
  48. $this->fileView = $view;
  49. $this->path = $this->fileView->getRelativePath($info->getPath());
  50. $this->info = $info;
  51. if ($shareManager) {
  52. $this->shareManager = $shareManager;
  53. } else {
  54. $this->shareManager = \OC::$server->getShareManager();
  55. }
  56. if ($info instanceof Folder || $info instanceof File) {
  57. $this->node = $info;
  58. } else {
  59. // The Node API assumes that the view passed doesn't have a fake root
  60. $rootView = \OC::$server->get(View::class);
  61. $root = \OC::$server->get(IRootFolder::class);
  62. if ($info->getType() === FileInfo::TYPE_FOLDER) {
  63. $this->node = new Folder($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
  64. } else {
  65. $this->node = new File($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
  66. }
  67. }
  68. }
  69. protected function refreshInfo(): void {
  70. $info = $this->fileView->getFileInfo($this->path);
  71. if ($info === false) {
  72. throw new \Sabre\DAV\Exception('Failed to get fileinfo for '. $this->path);
  73. }
  74. $this->info = $info;
  75. $root = \OC::$server->get(IRootFolder::class);
  76. $rootView = \OC::$server->get(View::class);
  77. if ($this->info->getType() === FileInfo::TYPE_FOLDER) {
  78. $this->node = new Folder($root, $rootView, $this->path, $this->info);
  79. } else {
  80. $this->node = new File($root, $rootView, $this->path, $this->info);
  81. }
  82. }
  83. /**
  84. * Returns the name of the node
  85. *
  86. * @return string
  87. */
  88. public function getName() {
  89. return $this->info->getName();
  90. }
  91. /**
  92. * Returns the full path
  93. *
  94. * @return string
  95. */
  96. public function getPath() {
  97. return $this->path;
  98. }
  99. /**
  100. * Renames the node
  101. *
  102. * @param string $name The new name
  103. * @throws \Sabre\DAV\Exception\BadRequest
  104. * @throws \Sabre\DAV\Exception\Forbidden
  105. */
  106. public function setName($name) {
  107. // rename is only allowed if the delete privilege is granted
  108. // (basically rename is a copy with delete of the original node)
  109. if (!($this->info->isDeletable() || ($this->info->getMountPoint() instanceof MoveableMount && $this->info->getInternalPath() === ''))) {
  110. throw new \Sabre\DAV\Exception\Forbidden();
  111. }
  112. [$parentPath,] = \Sabre\Uri\split($this->path);
  113. [, $newName] = \Sabre\Uri\split($name);
  114. $newPath = $parentPath . '/' . $newName;
  115. // verify path of the target
  116. $this->verifyPath($newPath);
  117. if (!$this->fileView->rename($this->path, $newPath)) {
  118. throw new \Sabre\DAV\Exception('Failed to rename '. $this->path . ' to ' . $newPath);
  119. }
  120. $this->path = $newPath;
  121. $this->refreshInfo();
  122. }
  123. public function setPropertyCache($property_cache) {
  124. $this->property_cache = $property_cache;
  125. }
  126. /**
  127. * Returns the last modification time, as a unix timestamp
  128. *
  129. * @return int timestamp as integer
  130. */
  131. public function getLastModified() {
  132. $timestamp = $this->info->getMtime();
  133. if (!empty($timestamp)) {
  134. return (int)$timestamp;
  135. }
  136. return $timestamp;
  137. }
  138. /**
  139. * sets the last modification time of the file (mtime) to the value given
  140. * in the second parameter or to now if the second param is empty.
  141. * Even if the modification time is set to a custom value the access time is set to now.
  142. */
  143. public function touch($mtime) {
  144. $mtime = $this->sanitizeMtime($mtime);
  145. $this->fileView->touch($this->path, $mtime);
  146. $this->refreshInfo();
  147. }
  148. /**
  149. * Returns the ETag for a file
  150. *
  151. * An ETag is a unique identifier representing the current version of the
  152. * file. If the file changes, the ETag MUST change. The ETag is an
  153. * arbitrary string, but MUST be surrounded by double-quotes.
  154. *
  155. * Return null if the ETag can not effectively be determined
  156. *
  157. * @return string
  158. */
  159. public function getETag() {
  160. return '"' . $this->info->getEtag() . '"';
  161. }
  162. /**
  163. * Sets the ETag
  164. *
  165. * @param string $etag
  166. *
  167. * @return int file id of updated file or -1 on failure
  168. */
  169. public function setETag($etag) {
  170. return $this->fileView->putFileInfo($this->path, ['etag' => $etag]);
  171. }
  172. public function setCreationTime(int $time) {
  173. return $this->fileView->putFileInfo($this->path, ['creation_time' => $time]);
  174. }
  175. public function setUploadTime(int $time) {
  176. return $this->fileView->putFileInfo($this->path, ['upload_time' => $time]);
  177. }
  178. /**
  179. * Returns the size of the node, in bytes
  180. *
  181. * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
  182. * @return int|float
  183. */
  184. public function getSize(): int|float {
  185. return $this->info->getSize();
  186. }
  187. /**
  188. * Returns the cache's file id
  189. *
  190. * @return int
  191. */
  192. public function getId() {
  193. return $this->info->getId();
  194. }
  195. /**
  196. * @return string|null
  197. */
  198. public function getFileId() {
  199. if ($id = $this->info->getId()) {
  200. return DavUtil::getDavFileId($id);
  201. }
  202. return null;
  203. }
  204. /**
  205. * @return integer
  206. */
  207. public function getInternalFileId() {
  208. return $this->info->getId();
  209. }
  210. public function getInternalPath(): string {
  211. return $this->info->getInternalPath();
  212. }
  213. /**
  214. * @param string $user
  215. * @return int
  216. */
  217. public function getSharePermissions($user) {
  218. // check of we access a federated share
  219. if ($user !== null) {
  220. try {
  221. $share = $this->shareManager->getShareByToken($user);
  222. return $share->getPermissions();
  223. } catch (ShareNotFound $e) {
  224. // ignore
  225. }
  226. }
  227. try {
  228. $storage = $this->info->getStorage();
  229. } catch (StorageNotAvailableException $e) {
  230. $storage = null;
  231. }
  232. if ($storage && $storage->instanceOfStorage(ISharedStorage::class)) {
  233. /** @var ISharedStorage $storage */
  234. $permissions = (int)$storage->getShare()->getPermissions();
  235. } else {
  236. $permissions = $this->info->getPermissions();
  237. }
  238. /*
  239. * We can always share non moveable mount points with DELETE and UPDATE
  240. * Eventually we need to do this properly
  241. */
  242. $mountpoint = $this->info->getMountPoint();
  243. if (!($mountpoint instanceof MoveableMount)) {
  244. $mountpointpath = $mountpoint->getMountPoint();
  245. if (str_ends_with($mountpointpath, '/')) {
  246. $mountpointpath = substr($mountpointpath, 0, -1);
  247. }
  248. if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) {
  249. $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
  250. }
  251. }
  252. /*
  253. * Files can't have create or delete permissions
  254. */
  255. if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
  256. $permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE);
  257. }
  258. return $permissions;
  259. }
  260. /**
  261. * @return array
  262. */
  263. public function getShareAttributes(): array {
  264. try {
  265. $storage = $this->node->getStorage();
  266. } catch (NotFoundException $e) {
  267. return [];
  268. }
  269. $attributes = [];
  270. if ($storage->instanceOfStorage(ISharedStorage::class)) {
  271. /** @var ISharedStorage $storage */
  272. $attributes = $storage->getShare()->getAttributes();
  273. if ($attributes === null) {
  274. return [];
  275. } else {
  276. return $attributes->toArray();
  277. }
  278. }
  279. return $attributes;
  280. }
  281. public function getNoteFromShare(?string $user): ?string {
  282. try {
  283. $storage = $this->node->getStorage();
  284. } catch (NotFoundException) {
  285. return null;
  286. }
  287. if ($storage->instanceOfStorage(ISharedStorage::class)) {
  288. /** @var ISharedStorage $storage */
  289. $share = $storage->getShare();
  290. if ($user === $share->getShareOwner()) {
  291. // Note is only for recipient not the owner
  292. return null;
  293. }
  294. return $share->getNote();
  295. }
  296. return null;
  297. }
  298. /**
  299. * @return string
  300. */
  301. public function getDavPermissions() {
  302. return DavUtil::getDavPermissions($this->info);
  303. }
  304. public function getOwner() {
  305. return $this->info->getOwner();
  306. }
  307. protected function verifyPath(?string $path = null): void {
  308. try {
  309. $path = $path ?? $this->info->getPath();
  310. $this->fileView->verifyPath(
  311. dirname($path),
  312. basename($path),
  313. );
  314. } catch (\OCP\Files\InvalidPathException $ex) {
  315. throw new InvalidPath($ex->getMessage());
  316. }
  317. }
  318. /**
  319. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  320. */
  321. public function acquireLock($type) {
  322. $this->fileView->lockFile($this->path, $type);
  323. }
  324. /**
  325. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  326. */
  327. public function releaseLock($type) {
  328. $this->fileView->unlockFile($this->path, $type);
  329. }
  330. /**
  331. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  332. */
  333. public function changeLock($type) {
  334. $this->fileView->changeLock($this->path, $type);
  335. }
  336. public function getFileInfo() {
  337. return $this->info;
  338. }
  339. public function getNode(): \OCP\Files\Node {
  340. return $this->node;
  341. }
  342. protected function sanitizeMtime($mtimeFromRequest) {
  343. return MtimeSanitizer::sanitizeMtime($mtimeFromRequest);
  344. }
  345. }