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