1
0

Node.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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 OCP\Files\FileInfo;
  30. use OCP\Files\InvalidPathException;
  31. use OCP\Files\NotFoundException;
  32. use OCP\Files\NotPermittedException;
  33. // FIXME: this class really should be abstract
  34. class Node implements \OCP\Files\Node {
  35. /**
  36. * @var \OC\Files\View $view
  37. */
  38. protected $view;
  39. /**
  40. * @var \OC\Files\Node\Root $root
  41. */
  42. protected $root;
  43. /**
  44. * @var string $path
  45. */
  46. protected $path;
  47. /**
  48. * @var \OCP\Files\FileInfo
  49. */
  50. protected $fileInfo;
  51. /**
  52. * @param \OC\Files\View $view
  53. * @param \OCP\Files\IRootFolder $root
  54. * @param string $path
  55. * @param FileInfo $fileInfo
  56. */
  57. public function __construct($root, $view, $path, $fileInfo = null) {
  58. $this->view = $view;
  59. $this->root = $root;
  60. $this->path = $path;
  61. $this->fileInfo = $fileInfo;
  62. }
  63. /**
  64. * Creates a Node of the same type that represents a non-existing path
  65. *
  66. * @param string $path path
  67. * @return string non-existing node class
  68. */
  69. protected function createNonExistingNode($path) {
  70. throw new \Exception('Must be implemented by subclasses');
  71. }
  72. /**
  73. * Returns the matching file info
  74. *
  75. * @return FileInfo
  76. * @throws InvalidPathException
  77. * @throws NotFoundException
  78. */
  79. public function getFileInfo() {
  80. if (!Filesystem::isValidPath($this->path)) {
  81. throw new InvalidPathException();
  82. }
  83. if (!$this->fileInfo) {
  84. $fileInfo = $this->view->getFileInfo($this->path);
  85. if ($fileInfo instanceof FileInfo) {
  86. $this->fileInfo = $fileInfo;
  87. } else {
  88. throw new NotFoundException();
  89. }
  90. }
  91. return $this->fileInfo;
  92. }
  93. /**
  94. * @param string[] $hooks
  95. */
  96. protected function sendHooks($hooks) {
  97. foreach ($hooks as $hook) {
  98. $this->root->emit('\OC\Files', $hook, array($this));
  99. }
  100. }
  101. /**
  102. * @param int $permissions
  103. * @return bool
  104. */
  105. protected function checkPermissions($permissions) {
  106. return ($this->getPermissions() & $permissions) === $permissions;
  107. }
  108. public function delete() {
  109. }
  110. /**
  111. * @param int $mtime
  112. * @throws \OCP\Files\NotPermittedException
  113. */
  114. public function touch($mtime = null) {
  115. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  116. $this->sendHooks(array('preTouch'));
  117. $this->view->touch($this->path, $mtime);
  118. $this->sendHooks(array('postTouch'));
  119. if ($this->fileInfo) {
  120. if (is_null($mtime)) {
  121. $mtime = time();
  122. }
  123. $this->fileInfo['mtime'] = $mtime;
  124. }
  125. } else {
  126. throw new NotPermittedException();
  127. }
  128. }
  129. /**
  130. * @return \OC\Files\Storage\Storage
  131. * @throws \OCP\Files\NotFoundException
  132. */
  133. public function getStorage() {
  134. list($storage,) = $this->view->resolvePath($this->path);
  135. return $storage;
  136. }
  137. /**
  138. * @return string
  139. */
  140. public function getPath() {
  141. return $this->path;
  142. }
  143. /**
  144. * @return string
  145. */
  146. public function getInternalPath() {
  147. list(, $internalPath) = $this->view->resolvePath($this->path);
  148. return $internalPath;
  149. }
  150. /**
  151. * @return int
  152. * @throws InvalidPathException
  153. * @throws NotFoundException
  154. */
  155. public function getId() {
  156. return $this->getFileInfo()->getId();
  157. }
  158. /**
  159. * @return array
  160. */
  161. public function stat() {
  162. return $this->view->stat($this->path);
  163. }
  164. /**
  165. * @return int
  166. * @throws InvalidPathException
  167. * @throws NotFoundException
  168. */
  169. public function getMTime() {
  170. return $this->getFileInfo()->getMTime();
  171. }
  172. /**
  173. * @return int
  174. * @throws InvalidPathException
  175. * @throws NotFoundException
  176. */
  177. public function getSize() {
  178. return $this->getFileInfo()->getSize();
  179. }
  180. /**
  181. * @return string
  182. * @throws InvalidPathException
  183. * @throws NotFoundException
  184. */
  185. public function getEtag() {
  186. return $this->getFileInfo()->getEtag();
  187. }
  188. /**
  189. * @return int
  190. * @throws InvalidPathException
  191. * @throws NotFoundException
  192. */
  193. public function getPermissions() {
  194. return $this->getFileInfo()->getPermissions();
  195. }
  196. /**
  197. * @return bool
  198. * @throws InvalidPathException
  199. * @throws NotFoundException
  200. */
  201. public function isReadable() {
  202. return $this->getFileInfo()->isReadable();
  203. }
  204. /**
  205. * @return bool
  206. * @throws InvalidPathException
  207. * @throws NotFoundException
  208. */
  209. public function isUpdateable() {
  210. return $this->getFileInfo()->isUpdateable();
  211. }
  212. /**
  213. * @return bool
  214. * @throws InvalidPathException
  215. * @throws NotFoundException
  216. */
  217. public function isDeletable() {
  218. return $this->getFileInfo()->isDeletable();
  219. }
  220. /**
  221. * @return bool
  222. * @throws InvalidPathException
  223. * @throws NotFoundException
  224. */
  225. public function isShareable() {
  226. return $this->getFileInfo()->isShareable();
  227. }
  228. /**
  229. * @return bool
  230. * @throws InvalidPathException
  231. * @throws NotFoundException
  232. */
  233. public function isCreatable() {
  234. return $this->getFileInfo()->isCreatable();
  235. }
  236. /**
  237. * @return Node
  238. */
  239. public function getParent() {
  240. $newPath = dirname($this->path);
  241. if ($newPath === '' || $newPath === '.' || $newPath === '/') {
  242. return $this->root;
  243. }
  244. return $this->root->get($newPath);
  245. }
  246. /**
  247. * @return string
  248. */
  249. public function getName() {
  250. return basename($this->path);
  251. }
  252. /**
  253. * @param string $path
  254. * @return string
  255. */
  256. protected function normalizePath($path) {
  257. if ($path === '' or $path === '/') {
  258. return '/';
  259. }
  260. //no windows style slashes
  261. $path = str_replace('\\', '/', $path);
  262. //add leading slash
  263. if ($path[0] !== '/') {
  264. $path = '/' . $path;
  265. }
  266. //remove duplicate slashes
  267. while (strpos($path, '//') !== false) {
  268. $path = str_replace('//', '/', $path);
  269. }
  270. //remove trailing slash
  271. $path = rtrim($path, '/');
  272. return $path;
  273. }
  274. /**
  275. * check if the requested path is valid
  276. *
  277. * @param string $path
  278. * @return bool
  279. */
  280. public function isValidPath($path) {
  281. if (!$path || $path[0] !== '/') {
  282. $path = '/' . $path;
  283. }
  284. if (strstr($path, '/../') || strrchr($path, '/') === '/..') {
  285. return false;
  286. }
  287. return true;
  288. }
  289. public function isMounted() {
  290. return $this->getFileInfo()->isMounted();
  291. }
  292. public function isShared() {
  293. return $this->getFileInfo()->isShared();
  294. }
  295. public function getMimeType() {
  296. return $this->getFileInfo()->getMimetype();
  297. }
  298. public function getMimePart() {
  299. return $this->getFileInfo()->getMimePart();
  300. }
  301. public function getType() {
  302. return $this->getFileInfo()->getType();
  303. }
  304. public function isEncrypted() {
  305. return $this->getFileInfo()->isEncrypted();
  306. }
  307. public function getMountPoint() {
  308. return $this->getFileInfo()->getMountPoint();
  309. }
  310. public function getOwner() {
  311. return $this->getFileInfo()->getOwner();
  312. }
  313. public function getChecksum() {
  314. }
  315. public function getExtension(): string {
  316. return $this->getFileInfo()->getExtension();
  317. }
  318. /**
  319. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  320. * @throws \OCP\Lock\LockedException
  321. */
  322. public function lock($type) {
  323. $this->view->lockFile($this->path, $type);
  324. }
  325. /**
  326. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  327. * @throws \OCP\Lock\LockedException
  328. */
  329. public function changeLock($type) {
  330. $this->view->changeLock($this->path, $type);
  331. }
  332. /**
  333. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  334. * @throws \OCP\Lock\LockedException
  335. */
  336. public function unlock($type) {
  337. $this->view->unlockFile($this->path, $type);
  338. }
  339. /**
  340. * @param string $targetPath
  341. * @throws \OCP\Files\NotPermittedException if copy not allowed or failed
  342. * @return \OC\Files\Node\Node
  343. */
  344. public function copy($targetPath) {
  345. $targetPath = $this->normalizePath($targetPath);
  346. $parent = $this->root->get(dirname($targetPath));
  347. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  348. $nonExisting = $this->createNonExistingNode($targetPath);
  349. $this->root->emit('\OC\Files', 'preCopy', [$this, $nonExisting]);
  350. $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]);
  351. if (!$this->view->copy($this->path, $targetPath)) {
  352. throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
  353. }
  354. $targetNode = $this->root->get($targetPath);
  355. $this->root->emit('\OC\Files', 'postCopy', [$this, $targetNode]);
  356. $this->root->emit('\OC\Files', 'postWrite', [$targetNode]);
  357. return $targetNode;
  358. } else {
  359. throw new NotPermittedException('No permission to copy to path ' . $targetPath);
  360. }
  361. }
  362. /**
  363. * @param string $targetPath
  364. * @throws \OCP\Files\NotPermittedException if move not allowed or failed
  365. * @return \OC\Files\Node\Node
  366. */
  367. public function move($targetPath) {
  368. $targetPath = $this->normalizePath($targetPath);
  369. $parent = $this->root->get(dirname($targetPath));
  370. if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
  371. $nonExisting = $this->createNonExistingNode($targetPath);
  372. $this->root->emit('\OC\Files', 'preRename', [$this, $nonExisting]);
  373. $this->root->emit('\OC\Files', 'preWrite', [$nonExisting]);
  374. if (!$this->view->rename($this->path, $targetPath)) {
  375. throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
  376. }
  377. $targetNode = $this->root->get($targetPath);
  378. $this->root->emit('\OC\Files', 'postRename', [$this, $targetNode]);
  379. $this->root->emit('\OC\Files', 'postWrite', [$targetNode]);
  380. $this->path = $targetPath;
  381. return $targetNode;
  382. } else {
  383. throw new NotPermittedException('No permission to move to path ' . $targetPath);
  384. }
  385. }
  386. }