jail.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Files\Storage\Wrapper;
  24. use OC\Files\Cache\Wrapper\CacheJail;
  25. use OCP\Lock\ILockingProvider;
  26. /**
  27. * Jail to a subdirectory of the wrapped storage
  28. *
  29. * This restricts access to a subfolder of the wrapped storage with the subfolder becoming the root folder new storage
  30. */
  31. class Jail extends Wrapper {
  32. /**
  33. * @var string
  34. */
  35. protected $rootPath;
  36. /**
  37. * @param array $arguments ['storage' => $storage, 'mask' => $root]
  38. *
  39. * $storage: The storage that will be wrapper
  40. * $root: The folder in the wrapped storage that will become the root folder of the wrapped storage
  41. */
  42. public function __construct($arguments) {
  43. parent::__construct($arguments);
  44. $this->rootPath = $arguments['root'];
  45. }
  46. protected function getSourcePath($path) {
  47. if ($path === '') {
  48. return $this->rootPath;
  49. } else {
  50. return $this->rootPath . '/' . $path;
  51. }
  52. }
  53. public function getId() {
  54. return 'link:' . parent::getId() . ':' . $this->rootPath;
  55. }
  56. /**
  57. * see http://php.net/manual/en/function.mkdir.php
  58. *
  59. * @param string $path
  60. * @return bool
  61. */
  62. public function mkdir($path) {
  63. return $this->storage->mkdir($this->getSourcePath($path));
  64. }
  65. /**
  66. * see http://php.net/manual/en/function.rmdir.php
  67. *
  68. * @param string $path
  69. * @return bool
  70. */
  71. public function rmdir($path) {
  72. return $this->storage->rmdir($this->getSourcePath($path));
  73. }
  74. /**
  75. * see http://php.net/manual/en/function.opendir.php
  76. *
  77. * @param string $path
  78. * @return resource
  79. */
  80. public function opendir($path) {
  81. return $this->storage->opendir($this->getSourcePath($path));
  82. }
  83. /**
  84. * see http://php.net/manual/en/function.is_dir.php
  85. *
  86. * @param string $path
  87. * @return bool
  88. */
  89. public function is_dir($path) {
  90. return $this->storage->is_dir($this->getSourcePath($path));
  91. }
  92. /**
  93. * see http://php.net/manual/en/function.is_file.php
  94. *
  95. * @param string $path
  96. * @return bool
  97. */
  98. public function is_file($path) {
  99. return $this->storage->is_file($this->getSourcePath($path));
  100. }
  101. /**
  102. * see http://php.net/manual/en/function.stat.php
  103. * only the following keys are required in the result: size and mtime
  104. *
  105. * @param string $path
  106. * @return array
  107. */
  108. public function stat($path) {
  109. return $this->storage->stat($this->getSourcePath($path));
  110. }
  111. /**
  112. * see http://php.net/manual/en/function.filetype.php
  113. *
  114. * @param string $path
  115. * @return bool
  116. */
  117. public function filetype($path) {
  118. return $this->storage->filetype($this->getSourcePath($path));
  119. }
  120. /**
  121. * see http://php.net/manual/en/function.filesize.php
  122. * The result for filesize when called on a folder is required to be 0
  123. *
  124. * @param string $path
  125. * @return int
  126. */
  127. public function filesize($path) {
  128. return $this->storage->filesize($this->getSourcePath($path));
  129. }
  130. /**
  131. * check if a file can be created in $path
  132. *
  133. * @param string $path
  134. * @return bool
  135. */
  136. public function isCreatable($path) {
  137. return $this->storage->isCreatable($this->getSourcePath($path));
  138. }
  139. /**
  140. * check if a file can be read
  141. *
  142. * @param string $path
  143. * @return bool
  144. */
  145. public function isReadable($path) {
  146. return $this->storage->isReadable($this->getSourcePath($path));
  147. }
  148. /**
  149. * check if a file can be written to
  150. *
  151. * @param string $path
  152. * @return bool
  153. */
  154. public function isUpdatable($path) {
  155. return $this->storage->isUpdatable($this->getSourcePath($path));
  156. }
  157. /**
  158. * check if a file can be deleted
  159. *
  160. * @param string $path
  161. * @return bool
  162. */
  163. public function isDeletable($path) {
  164. return $this->storage->isDeletable($this->getSourcePath($path));
  165. }
  166. /**
  167. * check if a file can be shared
  168. *
  169. * @param string $path
  170. * @return bool
  171. */
  172. public function isSharable($path) {
  173. return $this->storage->isSharable($this->getSourcePath($path));
  174. }
  175. /**
  176. * get the full permissions of a path.
  177. * Should return a combination of the PERMISSION_ constants defined in lib/public/constants.php
  178. *
  179. * @param string $path
  180. * @return int
  181. */
  182. public function getPermissions($path) {
  183. return $this->storage->getPermissions($this->getSourcePath($path));
  184. }
  185. /**
  186. * see http://php.net/manual/en/function.file_exists.php
  187. *
  188. * @param string $path
  189. * @return bool
  190. */
  191. public function file_exists($path) {
  192. return $this->storage->file_exists($this->getSourcePath($path));
  193. }
  194. /**
  195. * see http://php.net/manual/en/function.filemtime.php
  196. *
  197. * @param string $path
  198. * @return int
  199. */
  200. public function filemtime($path) {
  201. return $this->storage->filemtime($this->getSourcePath($path));
  202. }
  203. /**
  204. * see http://php.net/manual/en/function.file_get_contents.php
  205. *
  206. * @param string $path
  207. * @return string
  208. */
  209. public function file_get_contents($path) {
  210. return $this->storage->file_get_contents($this->getSourcePath($path));
  211. }
  212. /**
  213. * see http://php.net/manual/en/function.file_put_contents.php
  214. *
  215. * @param string $path
  216. * @param string $data
  217. * @return bool
  218. */
  219. public function file_put_contents($path, $data) {
  220. return $this->storage->file_put_contents($this->getSourcePath($path), $data);
  221. }
  222. /**
  223. * see http://php.net/manual/en/function.unlink.php
  224. *
  225. * @param string $path
  226. * @return bool
  227. */
  228. public function unlink($path) {
  229. return $this->storage->unlink($this->getSourcePath($path));
  230. }
  231. /**
  232. * see http://php.net/manual/en/function.rename.php
  233. *
  234. * @param string $path1
  235. * @param string $path2
  236. * @return bool
  237. */
  238. public function rename($path1, $path2) {
  239. return $this->storage->rename($this->getSourcePath($path1), $this->getSourcePath($path2));
  240. }
  241. /**
  242. * see http://php.net/manual/en/function.copy.php
  243. *
  244. * @param string $path1
  245. * @param string $path2
  246. * @return bool
  247. */
  248. public function copy($path1, $path2) {
  249. return $this->storage->copy($this->getSourcePath($path1), $this->getSourcePath($path2));
  250. }
  251. /**
  252. * see http://php.net/manual/en/function.fopen.php
  253. *
  254. * @param string $path
  255. * @param string $mode
  256. * @return resource
  257. */
  258. public function fopen($path, $mode) {
  259. return $this->storage->fopen($this->getSourcePath($path), $mode);
  260. }
  261. /**
  262. * get the mimetype for a file or folder
  263. * The mimetype for a folder is required to be "httpd/unix-directory"
  264. *
  265. * @param string $path
  266. * @return string
  267. */
  268. public function getMimeType($path) {
  269. return $this->storage->getMimeType($this->getSourcePath($path));
  270. }
  271. /**
  272. * see http://php.net/manual/en/function.hash.php
  273. *
  274. * @param string $type
  275. * @param string $path
  276. * @param bool $raw
  277. * @return string
  278. */
  279. public function hash($type, $path, $raw = false) {
  280. return $this->storage->hash($type, $this->getSourcePath($path), $raw);
  281. }
  282. /**
  283. * see http://php.net/manual/en/function.free_space.php
  284. *
  285. * @param string $path
  286. * @return int
  287. */
  288. public function free_space($path) {
  289. return $this->storage->free_space($this->getSourcePath($path));
  290. }
  291. /**
  292. * search for occurrences of $query in file names
  293. *
  294. * @param string $query
  295. * @return array
  296. */
  297. public function search($query) {
  298. return $this->storage->search($query);
  299. }
  300. /**
  301. * see http://php.net/manual/en/function.touch.php
  302. * If the backend does not support the operation, false should be returned
  303. *
  304. * @param string $path
  305. * @param int $mtime
  306. * @return bool
  307. */
  308. public function touch($path, $mtime = null) {
  309. return $this->storage->touch($this->getSourcePath($path), $mtime);
  310. }
  311. /**
  312. * get the path to a local version of the file.
  313. * The local version of the file can be temporary and doesn't have to be persistent across requests
  314. *
  315. * @param string $path
  316. * @return string
  317. */
  318. public function getLocalFile($path) {
  319. return $this->storage->getLocalFile($this->getSourcePath($path));
  320. }
  321. /**
  322. * check if a file or folder has been updated since $time
  323. *
  324. * @param string $path
  325. * @param int $time
  326. * @return bool
  327. *
  328. * hasUpdated for folders should return at least true if a file inside the folder is add, removed or renamed.
  329. * returning true for other changes in the folder is optional
  330. */
  331. public function hasUpdated($path, $time) {
  332. return $this->storage->hasUpdated($this->getSourcePath($path), $time);
  333. }
  334. /**
  335. * get a cache instance for the storage
  336. *
  337. * @param string $path
  338. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the cache
  339. * @return \OC\Files\Cache\Cache
  340. */
  341. public function getCache($path = '', $storage = null) {
  342. if (!$storage) {
  343. $storage = $this;
  344. }
  345. $sourceCache = $this->storage->getCache($this->getSourcePath($path), $storage);
  346. return new CacheJail($sourceCache, $this->rootPath);
  347. }
  348. /**
  349. * get the user id of the owner of a file or folder
  350. *
  351. * @param string $path
  352. * @return string
  353. */
  354. public function getOwner($path) {
  355. return $this->storage->getOwner($this->getSourcePath($path));
  356. }
  357. /**
  358. * get a watcher instance for the cache
  359. *
  360. * @param string $path
  361. * @param \OC\Files\Storage\Storage (optional) the storage to pass to the watcher
  362. * @return \OC\Files\Cache\Watcher
  363. */
  364. public function getWatcher($path = '', $storage = null) {
  365. if (!$storage) {
  366. $storage = $this;
  367. }
  368. return $this->storage->getWatcher($this->getSourcePath($path), $storage);
  369. }
  370. /**
  371. * get the ETag for a file or folder
  372. *
  373. * @param string $path
  374. * @return string
  375. */
  376. public function getETag($path) {
  377. return $this->storage->getETag($this->getSourcePath($path));
  378. }
  379. /**
  380. * @param string $path
  381. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  382. * @param \OCP\Lock\ILockingProvider $provider
  383. * @throws \OCP\Lock\LockedException
  384. */
  385. public function acquireLock($path, $type, ILockingProvider $provider) {
  386. $this->storage->acquireLock($this->getSourcePath($path), $type, $provider);
  387. }
  388. /**
  389. * @param string $path
  390. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  391. * @param \OCP\Lock\ILockingProvider $provider
  392. */
  393. public function releaseLock($path, $type, ILockingProvider $provider) {
  394. $this->storage->releaseLock($this->getSourcePath($path), $type, $provider);
  395. }
  396. /**
  397. * @param string $path
  398. * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE
  399. * @param \OCP\Lock\ILockingProvider $provider
  400. */
  401. public function changeLock($path, $type, ILockingProvider $provider) {
  402. $this->storage->changeLock($this->getSourcePath($path), $type, $provider);
  403. }
  404. }