Node.php 11 KB

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