PageRenderTime 27ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/private/Files/Storage/Wrapper/Jail.php

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