You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

468 lines
11 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Julius Härtl <jus@bitgrid.net>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Vincent Petry <pvince81@owncloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Files\Node;
  31. use OC\Files\Filesystem;
  32. use OC\Files\Mount\MoveableMount;
  33. use OCP\Files\FileInfo;
  34. use OCP\Files\InvalidPathException;
  35. use OCP\Files\NotFoundException;
  36. use OCP\Files\NotPermittedException;
  37. use OCP\Lock\LockedException;
  38. use Symfony\Component\EventDispatcher\GenericEvent;
  39. // FIXME: this class really should be abstract
  40. class Node implements \OCP\Files\Node {
  41. /**
  42. * @var \OC\Files\View $view
  43. */
  44. protected $view;
  45. /**
  46. * @var \OC\Files\Node\Root $root
  47. */
  48. protected $root;
  49. /**
  50. * @var string $path
  51. */
  52. protected $path;
  53. /**
  54. * @var \OCP\Files\FileInfo
  55. */
  56. protected $fileInfo;
  57. /**
  58. * @param \OC\Files\View $view
  59. * @param \OCP\Files\IRootFolder $root
  60. * @param string $path
  61. * @param FileInfo $fileInfo
  62. */
  63. public function __construct($root, $view, $path, $fileInfo = null) {
  64. $this->view = $view;
  65. $this->root = $root;
  66. $this->path = $path;
  67. $this->fileInfo = $fileInfo;
  68. }
  69. /**
  70. * Creates a Node of the same type that represents a non-existing path
  71. *
  72. * @param string $path path
  73. * @return string non-existing node class
  74. * @throws \Exception
  75. */
  76. protected function createNonExistingNode($path) {
  77. throw new \Exception('Must be implemented by subclasses');
  78. }
  79. /**
  80. * Returns the matching file info
  81. *
  82. * @return FileInfo
  83. * @throws InvalidPathException
  84. * @throws NotFoundException
  85. */
  86. public function getFileInfo() {
  87. if (!Filesystem::isValidPath($this->path)) {
  88. throw new InvalidPathException();
  89. }
  90. if (!$this->fileInfo) {
  91. $fileInfo = $this->view->getFileInfo($this->path);
  92. if ($fileInfo instanceof FileInfo) {
  93. $this->fileInfo = $fileInfo;
  94. } else {
  95. throw new NotFoundException();
  96. }
  97. }
  98. return $this->fileInfo;
  99. }
  100. /**
  101. * @param string[] $hooks
  102. */
  103. protected function sendHooks($hooks, array $args = null) {
  104. $args = !empty($args) ? $args : [$this];
  105. $dispatcher = \OC::$server->getEventDispatcher();
  106. foreach ($hooks as $hook) {
  107. $this->root->emit('\OC\Files', $hook, $args);
  108. $dispatcher->dispatch('\OCP\Files::' . $hook, new GenericEvent($args));
  109. }
  110. }
  111. /**
  112. * @param int $permissions
  113. * @return bool
  114. * @throws InvalidPathException
  115. * @throws NotFoundException
  116. */
  117. protected function checkPermissions($permissions) {
  118. return ($this->getPermissions() & $permissions) === $permissions;
  119. }
  120. public function delete() {
  121. }
  122. /**
  123. * @param int $mtime
  124. * @throws InvalidPathException
  125. * @throws NotFoundException
  126. * @throws NotPermittedException
  127. */
  128. public function touch($mtime = null) {
  129. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  130. $this->sendHooks(['preTouch']);
  131. $this->view->touch($this->path, $mtime);
  132. $this->sendHooks(['postTouch']);
  133. if ($this->fileInfo) {
  134. if (is_null($mtime)) {
  135. $mtime = time();
  136. }
  137. $this->fileInfo['mtime'] = $mtime;
  138. }
  139. } else {
  140. throw new NotPermittedException();
  141. }
  142. }
  143. /**
  144. * @return \OC\Files\Storage\Storage
  145. * @throws \OCP\Files\NotFoundException
  146. */
  147. public function getStorage() {
  148. list($storage,) = $this->view->resolvePath($this->path);
  149. return $storage;
  150. }
  151. /**
  152. * @return string
  153. */
  154. public function getPath() {
  155. return $this->path;
  156. }
  157. /**
  158. * @return string
  159. */
  160. public function getInternalPath() {
  161. list(, $internalPath) = $this->view->resolvePath($this->path);
  162. return $internalPath;
  163. }
  164. /**
  165. * @return int
  166. * @throws InvalidPathException
  167. * @throws NotFoundException
  168. */
  169. public function getId() {
  170. return $this->getFileInfo()->getId();
  171. }
  172. /**
  173. * @return array
  174. */
  175. public function stat() {
  176. return $this->view->stat($this->path);
  177. }
  178. /**
  179. * @return int
  180. * @throws InvalidPathException
  181. * @throws NotFoundException
  182. */
  183. public function getMTime() {
  184. return $this->getFileInfo()->getMTime();
  185. }
  186. /**
  187. * @param bool $includeMounts
  188. * @return int
  189. * @throws InvalidPathException
  190. * @throws NotFoundException
  191. */
  192. public function getSize($includeMounts = true) {
  193. return $this->getFileInfo()->getSize($includeMounts);
  194. }
  195. /**
  196. * @return string
  197. * @throws InvalidPathException
  198. * @throws NotFoundException
  199. */
  200. public function getEtag() {
  201. return $this->getFileInfo()->getEtag();
  202. }
  203. /**
  204. * @return int
  205. * @throws InvalidPathException
  206. * @throws NotFoundException
  207. */
  208. public function getPermissions() {
  209. return $this->getFileInfo()->getPermissions();
  210. }
  211. /**
  212. * @return bool
  213. * @throws InvalidPathException
  214. * @throws NotFoundException
  215. */
  216. public function isReadable() {
  217. return $this->getFileInfo()->isReadable();
  218. }
  219. /**
  220. * @return bool
  221. * @throws InvalidPathException
  222. * @throws NotFoundException
  223. */
  224. public function isUpdateable() {
  225. return $this->getFileInfo()->isUpdateable();
  226. }
  227. /**
  228. * @return bool
  229. * @throws InvalidPathException
  230. * @throws NotFoundException
  231. */
  232. public function isDeletable() {
  233. return $this->getFileInfo()->isDeletable();
  234. }
  235. /**
  236. * @return bool
  237. * @throws InvalidPathException
  238. * @throws NotFoundException
  239. */
  240. public function isShareable() {
  241. return $this->getFileInfo()->isShareable();
  242. }
  243. /**
  244. * @return bool
  245. * @throws InvalidPathException
  246. * @throws NotFoundException
  247. */
  248. public function isCreatable() {
  249. return $this->getFileInfo()->isCreatable();
  250. }
  251. /**
  252. * @return Node
  253. */
  254. public function getParent() {
  255. $newPath = dirname($this->path);
  256. if ($newPath === '' || $newPath === '.' || $newPath === '/') {
  257. return $this->root;
  258. }
  259. return $this->root->get($newPath);
  260. }
  261. /**
  262. * @return string
  263. */
  264. public function getName() {
  265. return basename($this->path);
  266. }
  267. /**
  268. * @param string $path
  269. * @return string
  270. */
  271. protected function normalizePath($path) {
  272. if ($path === '' or $path === '/') {
  273. return '/';
  274. }
  275. //no windows style slashes
  276. $path = str_replace('\\', '/', $path);
  277. //add leading slash
  278. if ($path[0] !== '/') {
  279. $path = '/' . $path;
  280. }
  281. //remove duplicate slashes
  282. while (strpos($path, '//') !== false) {
  283. $path = str_replace('//', '/', $path);
  284. }
  285. //remove trailing slash
  286. $path = rtrim($path, '/');
  287. return $path;
  288. }
  289. /**
  290. * check if the requested path is valid
  291. *
  292. * @param string $path
  293. * @return bool
  294. */
  295. public function isValidPath($path) {
  296. if (!$path || $path[0] !== '/') {
  297. $path = '/' . $path;
  298. }
  299. if (strstr($path, '/../') || strrchr($path, '/') === '/..') {
  300. return false;
  301. }
  302. return true;
  303. }
  304. public function isMounted() {
  305. return $this->getFileInfo()->isMounted();
  306. }
  307. public function isShared() {
  308. return $this->getFileInfo()->isShared();
  309. }
  310. public function getMimeType() {
  311. return $this->getFileInfo()->getMimetype();
  312. }
  313. public function getMimePart() {
  314. return $this->getFileInfo()->getMimePart();
  315. }
  316. public function getType() {
  317. return $this->getFileInfo()->getType();
  318. }
  319. public function isEncrypted() {
  320. return $this->getFileInfo()->isEncrypted();
  321. }
  322. public function getMountPoint() {
  323. return $this->getFileInfo()->getMountPoint();
  324. }
  325. public function getOwner() {
  326. return $this->getFileInfo()->getOwner();
  327. }
  328. public function getChecksum() {
  329. }
  330. public function getExtension(): string {
  331. return $this->getFileInfo()->getExtension();
  332. }
  333. /**
  334. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  335. * @throws LockedException
  336. */
  337. public function lock($type) {
  338. $this->view->lockFile($this->path, $type);
  339. }
  340. /**
  341. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  342. * @throws LockedException
  343. */
  344. public function changeLock($type) {
  345. $this->view->changeLock($this->path, $type);
  346. }
  347. /**
  348. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  349. * @throws LockedException
  350. */
  351. public function unlock($type) {
  352. $this->view->unlockFile($this->path, $type);
  353. }
  354. /**
  355. * @param string $targetPath
  356. * @return \OC\Files\Node\Node
  357. * @throws InvalidPathException
  358. * @throws NotFoundException
  359. * @throws NotPermittedException if copy not allowed or failed
  360. */
  361. public function copy($targetPath) {
  362. $targetPath = $this->normalizePath($targetPath);
  363. $parent = $this->root->get(dirname($targetPath));
  364. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  365. $nonExisting = $this->createNonExistingNode($targetPath);
  366. $this->sendHooks(['preCopy'], [$this, $nonExisting]);
  367. $this->sendHooks(['preWrite'], [$nonExisting]);
  368. if (!$this->view->copy($this->path, $targetPath)) {
  369. throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
  370. }
  371. $targetNode = $this->root->get($targetPath);
  372. $this->sendHooks(['postCopy'], [$this, $targetNode]);
  373. $this->sendHooks(['postWrite'], [$targetNode]);
  374. return $targetNode;
  375. } else {
  376. throw new NotPermittedException('No permission to copy to path ' . $targetPath);
  377. }
  378. }
  379. /**
  380. * @param string $targetPath
  381. * @return \OC\Files\Node\Node
  382. * @throws InvalidPathException
  383. * @throws NotFoundException
  384. * @throws NotPermittedException if move not allowed or failed
  385. * @throws LockedException
  386. */
  387. public function move($targetPath) {
  388. $targetPath = $this->normalizePath($targetPath);
  389. $parent = $this->root->get(dirname($targetPath));
  390. if (
  391. $parent instanceof Folder and
  392. $this->isValidPath($targetPath) and
  393. (
  394. $parent->isCreatable() ||
  395. ($parent->getInternalPath() === '' && $parent->getMountPoint() instanceof MoveableMount)
  396. )
  397. ) {
  398. $nonExisting = $this->createNonExistingNode($targetPath);
  399. $this->sendHooks(['preRename'], [$this, $nonExisting]);
  400. $this->sendHooks(['preWrite'], [$nonExisting]);
  401. if (!$this->view->rename($this->path, $targetPath)) {
  402. throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
  403. }
  404. $targetNode = $this->root->get($targetPath);
  405. $this->sendHooks(['postRename'], [$this, $targetNode]);
  406. $this->sendHooks(['postWrite'], [$targetNode]);
  407. $this->path = $targetPath;
  408. return $targetNode;
  409. } else {
  410. throw new NotPermittedException('No permission to move to path ' . $targetPath);
  411. }
  412. }
  413. public function getCreationTime(): int {
  414. return $this->getFileInfo()->getCreationTime();
  415. }
  416. public function getUploadTime(): int {
  417. return $this->getFileInfo()->getUploadTime();
  418. }
  419. }