Node.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  9. * @author Jakob Sack <mail@jakobsack.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  12. * @author Klaas Freitag <freitag@owncloud.com>
  13. * @author Markus Goetz <markus@woboq.com>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. * @author Tobias Kaminsky <tobias@kaminsky.me>
  19. * @author Vincent Petry <vincent@nextcloud.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OCA\DAV\Connector\Sabre;
  37. use OC\Files\Mount\MoveableMount;
  38. use OC\Files\Node\File;
  39. use OC\Files\Node\Folder;
  40. use OC\Files\View;
  41. use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
  42. use OCP\Files\DavUtil;
  43. use OCP\Files\FileInfo;
  44. use OCP\Files\IRootFolder;
  45. use OCP\Files\StorageNotAvailableException;
  46. use OCP\Share\Exceptions\ShareNotFound;
  47. use OCP\Share\IManager;
  48. abstract class Node implements \Sabre\DAV\INode {
  49. /**
  50. * @var View
  51. */
  52. protected $fileView;
  53. /**
  54. * The path to the current node
  55. *
  56. * @var string
  57. */
  58. protected $path;
  59. /**
  60. * node properties cache
  61. *
  62. * @var array
  63. */
  64. protected $property_cache = null;
  65. protected FileInfo $info;
  66. /**
  67. * @var IManager
  68. */
  69. protected $shareManager;
  70. protected \OCP\Files\Node $node;
  71. /**
  72. * Sets up the node, expects a full path name
  73. */
  74. public function __construct(View $view, FileInfo $info, IManager $shareManager = null) {
  75. $this->fileView = $view;
  76. $this->path = $this->fileView->getRelativePath($info->getPath());
  77. $this->info = $info;
  78. if ($shareManager) {
  79. $this->shareManager = $shareManager;
  80. } else {
  81. $this->shareManager = \OC::$server->getShareManager();
  82. }
  83. if ($info instanceof Folder || $info instanceof File) {
  84. $this->node = $info;
  85. } else {
  86. // The Node API assumes that the view passed doesn't have a fake root
  87. $rootView = \OC::$server->get(View::class);
  88. $root = \OC::$server->get(IRootFolder::class);
  89. if ($info->getType() === FileInfo::TYPE_FOLDER) {
  90. $this->node = new Folder($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
  91. } else {
  92. $this->node = new File($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info);
  93. }
  94. }
  95. }
  96. protected function refreshInfo(): void {
  97. $info = $this->fileView->getFileInfo($this->path);
  98. if ($info === false) {
  99. throw new \Sabre\DAV\Exception('Failed to get fileinfo for '. $this->path);
  100. }
  101. $this->info = $info;
  102. $root = \OC::$server->get(IRootFolder::class);
  103. $rootView = \OC::$server->get(View::class);
  104. if ($this->info->getType() === FileInfo::TYPE_FOLDER) {
  105. $this->node = new Folder($root, $rootView, $this->path, $this->info);
  106. } else {
  107. $this->node = new File($root, $rootView, $this->path, $this->info);
  108. }
  109. }
  110. /**
  111. * Returns the name of the node
  112. *
  113. * @return string
  114. */
  115. public function getName() {
  116. return $this->info->getName();
  117. }
  118. /**
  119. * Returns the full path
  120. *
  121. * @return string
  122. */
  123. public function getPath() {
  124. return $this->path;
  125. }
  126. /**
  127. * Renames the node
  128. *
  129. * @param string $name The new name
  130. * @throws \Sabre\DAV\Exception\BadRequest
  131. * @throws \Sabre\DAV\Exception\Forbidden
  132. */
  133. public function setName($name) {
  134. // rename is only allowed if the update privilege is granted
  135. if (!($this->info->isUpdateable() || ($this->info->getMountPoint() instanceof MoveableMount && $this->info->getInternalPath() === ''))) {
  136. throw new \Sabre\DAV\Exception\Forbidden();
  137. }
  138. [$parentPath,] = \Sabre\Uri\split($this->path);
  139. [, $newName] = \Sabre\Uri\split($name);
  140. // verify path of the target
  141. $this->verifyPath();
  142. $newPath = $parentPath . '/' . $newName;
  143. if (!$this->fileView->rename($this->path, $newPath)) {
  144. throw new \Sabre\DAV\Exception('Failed to rename '. $this->path . ' to ' . $newPath);
  145. }
  146. $this->path = $newPath;
  147. $this->refreshInfo();
  148. }
  149. public function setPropertyCache($property_cache) {
  150. $this->property_cache = $property_cache;
  151. }
  152. /**
  153. * Returns the last modification time, as a unix timestamp
  154. *
  155. * @return int timestamp as integer
  156. */
  157. public function getLastModified() {
  158. $timestamp = $this->info->getMtime();
  159. if (!empty($timestamp)) {
  160. return (int)$timestamp;
  161. }
  162. return $timestamp;
  163. }
  164. /**
  165. * sets the last modification time of the file (mtime) to the value given
  166. * in the second parameter or to now if the second param is empty.
  167. * Even if the modification time is set to a custom value the access time is set to now.
  168. */
  169. public function touch($mtime) {
  170. $mtime = $this->sanitizeMtime($mtime);
  171. $this->fileView->touch($this->path, $mtime);
  172. $this->refreshInfo();
  173. }
  174. /**
  175. * Returns the ETag for a file
  176. *
  177. * An ETag is a unique identifier representing the current version of the
  178. * file. If the file changes, the ETag MUST change. The ETag is an
  179. * arbitrary string, but MUST be surrounded by double-quotes.
  180. *
  181. * Return null if the ETag can not effectively be determined
  182. *
  183. * @return string
  184. */
  185. public function getETag() {
  186. return '"' . $this->info->getEtag() . '"';
  187. }
  188. /**
  189. * Sets the ETag
  190. *
  191. * @param string $etag
  192. *
  193. * @return int file id of updated file or -1 on failure
  194. */
  195. public function setETag($etag) {
  196. return $this->fileView->putFileInfo($this->path, ['etag' => $etag]);
  197. }
  198. public function setCreationTime(int $time) {
  199. return $this->fileView->putFileInfo($this->path, ['creation_time' => $time]);
  200. }
  201. public function setUploadTime(int $time) {
  202. return $this->fileView->putFileInfo($this->path, ['upload_time' => $time]);
  203. }
  204. /**
  205. * Returns the size of the node, in bytes
  206. *
  207. * @psalm-suppress ImplementedReturnTypeMismatch \Sabre\DAV\IFile::getSize signature does not support 32bit
  208. * @return int|float
  209. */
  210. public function getSize(): int|float {
  211. return $this->info->getSize();
  212. }
  213. /**
  214. * Returns the cache's file id
  215. *
  216. * @return int
  217. */
  218. public function getId() {
  219. return $this->info->getId();
  220. }
  221. /**
  222. * @return string|null
  223. */
  224. public function getFileId() {
  225. if ($id = $this->info->getId()) {
  226. return DavUtil::getDavFileId($id);
  227. }
  228. return null;
  229. }
  230. /**
  231. * @return integer
  232. */
  233. public function getInternalFileId() {
  234. return $this->info->getId();
  235. }
  236. public function getInternalPath(): string {
  237. return $this->info->getInternalPath();
  238. }
  239. /**
  240. * @param string $user
  241. * @return int
  242. */
  243. public function getSharePermissions($user) {
  244. // check of we access a federated share
  245. if ($user !== null) {
  246. try {
  247. $share = $this->shareManager->getShareByToken($user);
  248. return $share->getPermissions();
  249. } catch (ShareNotFound $e) {
  250. // ignore
  251. }
  252. }
  253. try {
  254. $storage = $this->info->getStorage();
  255. } catch (StorageNotAvailableException $e) {
  256. $storage = null;
  257. }
  258. if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
  259. /** @var \OCA\Files_Sharing\SharedStorage $storage */
  260. $permissions = (int)$storage->getShare()->getPermissions();
  261. } else {
  262. $permissions = $this->info->getPermissions();
  263. }
  264. /*
  265. * We can always share non moveable mount points with DELETE and UPDATE
  266. * Eventually we need to do this properly
  267. */
  268. $mountpoint = $this->info->getMountPoint();
  269. if (!($mountpoint instanceof MoveableMount)) {
  270. $mountpointpath = $mountpoint->getMountPoint();
  271. if (str_ends_with($mountpointpath, '/')) {
  272. $mountpointpath = substr($mountpointpath, 0, -1);
  273. }
  274. if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) {
  275. $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE;
  276. }
  277. }
  278. /*
  279. * Files can't have create or delete permissions
  280. */
  281. if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) {
  282. $permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE);
  283. }
  284. return $permissions;
  285. }
  286. /**
  287. * @return array
  288. */
  289. public function getShareAttributes(): array {
  290. $attributes = [];
  291. try {
  292. $storage = $this->info->getStorage();
  293. } catch (StorageNotAvailableException $e) {
  294. $storage = null;
  295. }
  296. if ($storage && $storage->instanceOfStorage(\OCA\Files_Sharing\SharedStorage::class)) {
  297. /** @var \OCA\Files_Sharing\SharedStorage $storage */
  298. $attributes = $storage->getShare()->getAttributes();
  299. if ($attributes === null) {
  300. return [];
  301. } else {
  302. return $attributes->toArray();
  303. }
  304. }
  305. return $attributes;
  306. }
  307. /**
  308. * @param string $user
  309. * @return string
  310. */
  311. public function getNoteFromShare($user) {
  312. if ($user === null) {
  313. return '';
  314. }
  315. // Retrieve note from the share object already loaded into
  316. // memory, to avoid additional database queries.
  317. $storage = $this->getNode()->getStorage();
  318. if (!$storage->instanceOfStorage(\OCA\Files_Sharing\SharedStorage::class)) {
  319. return '';
  320. }
  321. /** @var \OCA\Files_Sharing\SharedStorage $storage */
  322. $share = $storage->getShare();
  323. $note = $share->getNote();
  324. if ($share->getShareOwner() !== $user) {
  325. return $note;
  326. }
  327. return '';
  328. }
  329. /**
  330. * @return string
  331. */
  332. public function getDavPermissions() {
  333. return DavUtil::getDavPermissions($this->info);
  334. }
  335. public function getOwner() {
  336. return $this->info->getOwner();
  337. }
  338. protected function verifyPath() {
  339. try {
  340. $fileName = basename($this->info->getPath());
  341. $this->fileView->verifyPath($this->path, $fileName);
  342. } catch (\OCP\Files\InvalidPathException $ex) {
  343. throw new InvalidPath($ex->getMessage());
  344. }
  345. }
  346. /**
  347. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  348. */
  349. public function acquireLock($type) {
  350. $this->fileView->lockFile($this->path, $type);
  351. }
  352. /**
  353. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  354. */
  355. public function releaseLock($type) {
  356. $this->fileView->unlockFile($this->path, $type);
  357. }
  358. /**
  359. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  360. */
  361. public function changeLock($type) {
  362. $this->fileView->changeLock($this->path, $type);
  363. }
  364. public function getFileInfo() {
  365. return $this->info;
  366. }
  367. public function getNode(): \OCP\Files\Node {
  368. return $this->node;
  369. }
  370. protected function sanitizeMtime($mtimeFromRequest) {
  371. return MtimeSanitizer::sanitizeMtime($mtimeFromRequest);
  372. }
  373. }