1
0

Node.php 10 KB

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