node.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Files\Node;
  9. use OC\Files\Cache\Cache;
  10. use OC\Files\Cache\Scanner;
  11. use OCP\Files\NotFoundException;
  12. use OCP\Files\NotPermittedException;
  13. class Node implements \OCP\Files\Node {
  14. /**
  15. * @var \OC\Files\View $view
  16. */
  17. protected $view;
  18. /**
  19. * @var \OC\Files\Node\Root $root
  20. */
  21. protected $root;
  22. /**
  23. * @var string $path
  24. */
  25. protected $path;
  26. /**
  27. * @param \OC\Files\View $view
  28. * @param \OC\Files\Node\Root Root $root
  29. * @param string $path
  30. */
  31. public function __construct($root, $view, $path) {
  32. $this->view = $view;
  33. $this->root = $root;
  34. $this->path = $path;
  35. }
  36. /**
  37. * @param string[] $hooks
  38. */
  39. protected function sendHooks($hooks) {
  40. foreach ($hooks as $hook) {
  41. $this->root->emit('\OC\Files', $hook, array($this));
  42. }
  43. }
  44. /**
  45. * @param int $permissions
  46. * @return bool
  47. */
  48. protected function checkPermissions($permissions) {
  49. return ($this->getPermissions() & $permissions) === $permissions;
  50. }
  51. /**
  52. * @param string $targetPath
  53. * @throws \OCP\Files\NotPermittedException
  54. * @return \OC\Files\Node\Node
  55. */
  56. public function move($targetPath) {
  57. return;
  58. }
  59. public function delete() {
  60. return;
  61. }
  62. /**
  63. * @param string $targetPath
  64. * @return \OC\Files\Node\Node
  65. */
  66. public function copy($targetPath) {
  67. return;
  68. }
  69. /**
  70. * @param int $mtime
  71. * @throws \OCP\Files\NotPermittedException
  72. */
  73. public function touch($mtime = null) {
  74. if ($this->checkPermissions(\OCP\PERMISSION_UPDATE)) {
  75. $this->sendHooks(array('preTouch'));
  76. $this->view->touch($this->path, $mtime);
  77. $this->sendHooks(array('postTouch'));
  78. } else {
  79. throw new NotPermittedException();
  80. }
  81. }
  82. /**
  83. * @return \OC\Files\Storage\Storage
  84. * @throws \OCP\Files\NotFoundException
  85. */
  86. public function getStorage() {
  87. list($storage,) = $this->view->resolvePath($this->path);
  88. return $storage;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getPath() {
  94. return $this->path;
  95. }
  96. /**
  97. * @return string
  98. */
  99. public function getInternalPath() {
  100. list(, $internalPath) = $this->view->resolvePath($this->path);
  101. return $internalPath;
  102. }
  103. /**
  104. * @return int
  105. */
  106. public function getId() {
  107. $info = $this->view->getFileInfo($this->path);
  108. return $info['fileid'];
  109. }
  110. /**
  111. * @return array
  112. */
  113. public function stat() {
  114. return $this->view->stat($this->path);
  115. }
  116. /**
  117. * @return int
  118. */
  119. public function getMTime() {
  120. return $this->view->filemtime($this->path);
  121. }
  122. /**
  123. * @return int
  124. */
  125. public function getSize() {
  126. return $this->view->filesize($this->path);
  127. }
  128. /**
  129. * @return string
  130. */
  131. public function getEtag() {
  132. $info = $this->view->getFileInfo($this->path);
  133. return $info['etag'];
  134. }
  135. /**
  136. * @return int
  137. */
  138. public function getPermissions() {
  139. $info = $this->view->getFileInfo($this->path);
  140. return $info['permissions'];
  141. }
  142. /**
  143. * @return bool
  144. */
  145. public function isReadable() {
  146. return $this->checkPermissions(\OCP\PERMISSION_READ);
  147. }
  148. /**
  149. * @return bool
  150. */
  151. public function isUpdateable() {
  152. return $this->checkPermissions(\OCP\PERMISSION_UPDATE);
  153. }
  154. /**
  155. * @return bool
  156. */
  157. public function isDeletable() {
  158. return $this->checkPermissions(\OCP\PERMISSION_DELETE);
  159. }
  160. /**
  161. * @return bool
  162. */
  163. public function isShareable() {
  164. return $this->checkPermissions(\OCP\PERMISSION_SHARE);
  165. }
  166. /**
  167. * @return Node
  168. */
  169. public function getParent() {
  170. return $this->root->get(dirname($this->path));
  171. }
  172. /**
  173. * @return string
  174. */
  175. public function getName() {
  176. return basename($this->path);
  177. }
  178. /**
  179. * @param string $path
  180. * @return string
  181. */
  182. protected function normalizePath($path) {
  183. if ($path === '' or $path === '/') {
  184. return '/';
  185. }
  186. //no windows style slashes
  187. $path = str_replace('\\', '/', $path);
  188. //add leading slash
  189. if ($path[0] !== '/') {
  190. $path = '/' . $path;
  191. }
  192. //remove duplicate slashes
  193. while (strpos($path, '//') !== false) {
  194. $path = str_replace('//', '/', $path);
  195. }
  196. //remove trailing slash
  197. $path = rtrim($path, '/');
  198. return $path;
  199. }
  200. /**
  201. * check if the requested path is valid
  202. *
  203. * @param string $path
  204. * @return bool
  205. */
  206. public function isValidPath($path) {
  207. if (!$path || $path[0] !== '/') {
  208. $path = '/' . $path;
  209. }
  210. if (strstr($path, '/../') || strrchr($path, '/') === '/..') {
  211. return false;
  212. }
  213. return true;
  214. }
  215. }