node.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. class Node implements \OCP\Files\Node {
  34. /**
  35. * @var \OC\Files\View $view
  36. */
  37. protected $view;
  38. /**
  39. * @var \OC\Files\Node\Root $root
  40. */
  41. protected $root;
  42. /**
  43. * @var string $path
  44. */
  45. protected $path;
  46. /**
  47. * @var \OCP\Files\FileInfo
  48. */
  49. protected $fileInfo;
  50. /**
  51. * @param \OC\Files\View $view
  52. * @param \OC\Files\Node\Root $root
  53. * @param string $path
  54. * @param FileInfo $fileInfo
  55. */
  56. public function __construct($root, $view, $path, $fileInfo = null) {
  57. $this->view = $view;
  58. $this->root = $root;
  59. $this->path = $path;
  60. $this->fileInfo = $fileInfo;
  61. }
  62. /**
  63. * Returns the matching file info
  64. *
  65. * @return FileInfo
  66. * @throws InvalidPathException
  67. * @throws NotFoundException
  68. */
  69. public function getFileInfo() {
  70. if (!Filesystem::isValidPath($this->path)) {
  71. throw new InvalidPathException();
  72. }
  73. if (!$this->fileInfo) {
  74. $fileInfo = $this->view->getFileInfo($this->path);
  75. if ($fileInfo instanceof FileInfo) {
  76. $this->fileInfo = $fileInfo;
  77. } else {
  78. throw new NotFoundException();
  79. }
  80. }
  81. return $this->fileInfo;
  82. }
  83. /**
  84. * @param string[] $hooks
  85. */
  86. protected function sendHooks($hooks) {
  87. foreach ($hooks as $hook) {
  88. $this->root->emit('\OC\Files', $hook, array($this));
  89. }
  90. }
  91. /**
  92. * @param int $permissions
  93. * @return bool
  94. */
  95. protected function checkPermissions($permissions) {
  96. return ($this->getPermissions() & $permissions) === $permissions;
  97. }
  98. /**
  99. * @param string $targetPath
  100. * @throws \OCP\Files\NotPermittedException
  101. * @return \OC\Files\Node\Node
  102. */
  103. public function move($targetPath) {
  104. return;
  105. }
  106. public function delete() {
  107. return;
  108. }
  109. /**
  110. * @param string $targetPath
  111. * @return \OC\Files\Node\Node
  112. */
  113. public function copy($targetPath) {
  114. return;
  115. }
  116. /**
  117. * @param int $mtime
  118. * @throws \OCP\Files\NotPermittedException
  119. */
  120. public function touch($mtime = null) {
  121. if ($this->checkPermissions(\OCP\Constants::PERMISSION_UPDATE)) {
  122. $this->sendHooks(array('preTouch'));
  123. $this->view->touch($this->path, $mtime);
  124. $this->sendHooks(array('postTouch'));
  125. if ($this->fileInfo) {
  126. if (is_null($mtime)) {
  127. $mtime = time();
  128. }
  129. $this->fileInfo['mtime'] = $mtime;
  130. }
  131. } else {
  132. throw new NotPermittedException();
  133. }
  134. }
  135. /**
  136. * @return \OC\Files\Storage\Storage
  137. * @throws \OCP\Files\NotFoundException
  138. */
  139. public function getStorage() {
  140. list($storage,) = $this->view->resolvePath($this->path);
  141. return $storage;
  142. }
  143. /**
  144. * @return string
  145. */
  146. public function getPath() {
  147. return $this->path;
  148. }
  149. /**
  150. * @return string
  151. */
  152. public function getInternalPath() {
  153. list(, $internalPath) = $this->view->resolvePath($this->path);
  154. return $internalPath;
  155. }
  156. /**
  157. * @return int
  158. * @throws InvalidPathException
  159. * @throws NotFoundException
  160. */
  161. public function getId() {
  162. return $this->getFileInfo()->getId();
  163. }
  164. /**
  165. * @return array
  166. */
  167. public function stat() {
  168. return $this->view->stat($this->path);
  169. }
  170. /**
  171. * @return int
  172. * @throws InvalidPathException
  173. * @throws NotFoundException
  174. */
  175. public function getMTime() {
  176. return $this->getFileInfo()->getMTime();
  177. }
  178. /**
  179. * @return int
  180. * @throws InvalidPathException
  181. * @throws NotFoundException
  182. */
  183. public function getSize() {
  184. return $this->getFileInfo()->getSize();
  185. }
  186. /**
  187. * @return string
  188. * @throws InvalidPathException
  189. * @throws NotFoundException
  190. */
  191. public function getEtag() {
  192. return $this->getFileInfo()->getEtag();
  193. }
  194. /**
  195. * @return int
  196. * @throws InvalidPathException
  197. * @throws NotFoundException
  198. */
  199. public function getPermissions() {
  200. return $this->getFileInfo()->getPermissions();
  201. }
  202. /**
  203. * @return bool
  204. * @throws InvalidPathException
  205. * @throws NotFoundException
  206. */
  207. public function isReadable() {
  208. return $this->getFileInfo()->isReadable();
  209. }
  210. /**
  211. * @return bool
  212. * @throws InvalidPathException
  213. * @throws NotFoundException
  214. */
  215. public function isUpdateable() {
  216. return $this->getFileInfo()->isUpdateable();
  217. }
  218. /**
  219. * @return bool
  220. * @throws InvalidPathException
  221. * @throws NotFoundException
  222. */
  223. public function isDeletable() {
  224. return $this->getFileInfo()->isDeletable();
  225. }
  226. /**
  227. * @return bool
  228. * @throws InvalidPathException
  229. * @throws NotFoundException
  230. */
  231. public function isShareable() {
  232. return $this->getFileInfo()->isShareable();
  233. }
  234. /**
  235. * @return bool
  236. * @throws InvalidPathException
  237. * @throws NotFoundException
  238. */
  239. public function isCreatable() {
  240. return $this->getFileInfo()->isCreatable();
  241. }
  242. /**
  243. * @return Node
  244. */
  245. public function getParent() {
  246. return $this->root->get(dirname($this->path));
  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. return;
  317. }
  318. }