Node.php 9.8 KB

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