sharedstorage.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Michael Gapczynski
  6. * @copyright 2011 Michael Gapczynski mtgap@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library 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
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OC\Files\Storage;
  23. /**
  24. * Convert target path to source path and pass the function call to the correct storage provider
  25. */
  26. class Shared extends \OC\Files\Storage\Common {
  27. private $sharedFolder;
  28. private $files = array();
  29. public function __construct($arguments) {
  30. $this->sharedFolder = $arguments['sharedFolder'];
  31. }
  32. public function getId() {
  33. return 'shared::' . $this->sharedFolder;
  34. }
  35. /**
  36. * @brief Get the source file path, permissions, and owner for a shared file
  37. * @param string Shared target file path
  38. * @return Returns array with the keys path, permissions, and owner or false if not found
  39. */
  40. public function getFile($target) {
  41. if (!isset($this->files[$target])) {
  42. // Check for partial files
  43. if (pathinfo($target, PATHINFO_EXTENSION) === 'part') {
  44. $source = \OC_Share_Backend_File::getSource(substr($target, 0, -5));
  45. if ($source) {
  46. $source['path'] .= '.part';
  47. // All partial files have delete permission
  48. $source['permissions'] |= \OCP\PERMISSION_DELETE;
  49. }
  50. } else {
  51. $source = \OC_Share_Backend_File::getSource($target);
  52. }
  53. $this->files[$target] = $source;
  54. }
  55. return $this->files[$target];
  56. }
  57. /**
  58. * @brief Get the source file path for a shared file
  59. * @param string Shared target file path
  60. * @return string source file path or false if not found
  61. */
  62. public function getSourcePath($target) {
  63. $source = $this->getFile($target);
  64. if ($source) {
  65. if (!isset($source['fullPath'])) {
  66. \OC\Files\Filesystem::initMountPoints($source['fileOwner']);
  67. $mount = \OC\Files\Filesystem::getMountByNumericId($source['storage']);
  68. if (is_array($mount)) {
  69. $this->files[$target]['fullPath'] = $mount[key($mount)]->getMountPoint() . $source['path'];
  70. } else {
  71. $this->files[$target]['fullPath'] = false;
  72. }
  73. }
  74. return $this->files[$target]['fullPath'];
  75. }
  76. return false;
  77. }
  78. /**
  79. * @brief Get the permissions granted for a shared file
  80. * @param string Shared target file path
  81. * @return int CRUDS permissions granted or false if not found
  82. */
  83. public function getPermissions($target) {
  84. $source = $this->getFile($target);
  85. if ($source) {
  86. return $source['permissions'];
  87. }
  88. return false;
  89. }
  90. public function mkdir($path) {
  91. if ($path == '' || $path == '/' || !$this->isCreatable(dirname($path))) {
  92. return false;
  93. } else if ($source = $this->getSourcePath($path)) {
  94. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  95. return $storage->mkdir($internalPath);
  96. }
  97. return false;
  98. }
  99. public function rmdir($path) {
  100. if (($source = $this->getSourcePath($path)) && $this->isDeletable($path)) {
  101. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  102. return $storage->rmdir($internalPath);
  103. }
  104. return false;
  105. }
  106. public function opendir($path) {
  107. if ($path == '' || $path == '/') {
  108. $files = \OCP\Share::getItemsSharedWith('file', \OC_Share_Backend_Folder::FORMAT_OPENDIR);
  109. \OC\Files\Stream\Dir::register('shared', $files);
  110. return opendir('fakedir://shared');
  111. } else if ($source = $this->getSourcePath($path)) {
  112. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  113. return $storage->opendir($internalPath);
  114. }
  115. return false;
  116. }
  117. public function is_dir($path) {
  118. if ($path == '' || $path == '/') {
  119. return true;
  120. } else if ($source = $this->getSourcePath($path)) {
  121. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  122. return $storage->is_dir($internalPath);
  123. }
  124. return false;
  125. }
  126. public function is_file($path) {
  127. if ($source = $this->getSourcePath($path)) {
  128. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  129. return $storage->is_file($internalPath);
  130. }
  131. return false;
  132. }
  133. public function stat($path) {
  134. if ($path == '' || $path == '/') {
  135. $stat['size'] = $this->filesize($path);
  136. $stat['mtime'] = $this->filemtime($path);
  137. return $stat;
  138. } else if ($source = $this->getSourcePath($path)) {
  139. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  140. return $storage->stat($internalPath);
  141. }
  142. return false;
  143. }
  144. public function filetype($path) {
  145. if ($path == '' || $path == '/') {
  146. return 'dir';
  147. } else if ($source = $this->getSourcePath($path)) {
  148. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  149. return $storage->filetype($internalPath);
  150. }
  151. return false;
  152. }
  153. public function filesize($path) {
  154. if ($path == '' || $path == '/' || $this->is_dir($path)) {
  155. return 0;
  156. } else if ($source = $this->getSourcePath($path)) {
  157. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  158. return $storage->filesize($internalPath);
  159. }
  160. return false;
  161. }
  162. public function isCreatable($path) {
  163. if ($path == '') {
  164. return false;
  165. }
  166. return ($this->getPermissions($path) & \OCP\PERMISSION_CREATE);
  167. }
  168. public function isReadable($path) {
  169. return $this->file_exists($path);
  170. }
  171. public function isUpdatable($path) {
  172. if ($path == '') {
  173. return false;
  174. }
  175. return ($this->getPermissions($path) & \OCP\PERMISSION_UPDATE);
  176. }
  177. public function isDeletable($path) {
  178. if ($path == '') {
  179. return true;
  180. }
  181. return ($this->getPermissions($path) & \OCP\PERMISSION_DELETE);
  182. }
  183. public function isSharable($path) {
  184. if ($path == '') {
  185. return false;
  186. }
  187. return ($this->getPermissions($path) & \OCP\PERMISSION_SHARE);
  188. }
  189. public function file_exists($path) {
  190. if ($path == '' || $path == '/') {
  191. return true;
  192. } else if ($source = $this->getSourcePath($path)) {
  193. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  194. return $storage->file_exists($internalPath);
  195. }
  196. return false;
  197. }
  198. public function filemtime($path) {
  199. if ($path == '' || $path == '/') {
  200. $mtime = 0;
  201. $dh = $this->opendir($path);
  202. if (is_resource($dh)) {
  203. while (($filename = readdir($dh)) !== false) {
  204. $tempmtime = $this->filemtime($filename);
  205. if ($tempmtime > $mtime) {
  206. $mtime = $tempmtime;
  207. }
  208. }
  209. }
  210. return $mtime;
  211. } else {
  212. $source = $this->getSourcePath($path);
  213. if ($source) {
  214. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  215. return $storage->filemtime($internalPath);
  216. }
  217. }
  218. }
  219. public function file_get_contents($path) {
  220. $source = $this->getSourcePath($path);
  221. if ($source) {
  222. $info = array(
  223. 'target' => $this->sharedFolder . $path,
  224. 'source' => $source,
  225. );
  226. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
  227. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  228. return $storage->file_get_contents($internalPath);
  229. }
  230. }
  231. public function file_put_contents($path, $data) {
  232. if ($source = $this->getSourcePath($path)) {
  233. // Check if permission is granted
  234. if (($this->file_exists($path) && !$this->isUpdatable($path))
  235. || ($this->is_dir($path) && !$this->isCreatable($path))
  236. ) {
  237. return false;
  238. }
  239. $info = array(
  240. 'target' => $this->sharedFolder . $path,
  241. 'source' => $source,
  242. );
  243. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
  244. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  245. $result = $storage->file_put_contents($internalPath, $data);
  246. return $result;
  247. }
  248. return false;
  249. }
  250. public function unlink($path) {
  251. // Delete the file if DELETE permission is granted
  252. if ($source = $this->getSourcePath($path)) {
  253. if ($this->isDeletable($path)) {
  254. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  255. return $storage->unlink($internalPath);
  256. } else if (dirname($path) == '/' || dirname($path) == '.') {
  257. // Unshare the file from the user if in the root of the Shared folder
  258. if ($this->is_dir($path)) {
  259. $itemType = 'folder';
  260. } else {
  261. $itemType = 'file';
  262. }
  263. return \OCP\Share::unshareFromSelf($itemType, $path);
  264. }
  265. }
  266. return false;
  267. }
  268. public function rename($path1, $path2) {
  269. // Check for partial files
  270. if (pathinfo($path1, PATHINFO_EXTENSION) === 'part') {
  271. if ($oldSource = $this->getSourcePath($path1)) {
  272. list($storage, $oldInternalPath) = \OC\Files\Filesystem::resolvePath($oldSource);
  273. $newInternalPath = substr($oldInternalPath, 0, -5);
  274. return $storage->rename($oldInternalPath, $newInternalPath);
  275. }
  276. } else {
  277. // Renaming/moving is only allowed within shared folders
  278. $pos1 = strpos($path1, '/', 1);
  279. $pos2 = strpos($path2, '/', 1);
  280. if ($pos1 !== false && $pos2 !== false && ($oldSource = $this->getSourcePath($path1))) {
  281. $newSource = $this->getSourcePath(dirname($path2)) . '/' . basename($path2);
  282. // Within the same folder, we only need UPDATE permissions
  283. if (dirname($path1) == dirname($path2) and $this->isUpdatable($path1)) {
  284. list($storage, $oldInternalPath) = \OC\Files\Filesystem::resolvePath($oldSource);
  285. list(, $newInternalPath) = \OC\Files\Filesystem::resolvePath($newSource);
  286. return $storage->rename($oldInternalPath, $newInternalPath);
  287. // otherwise DELETE and CREATE permissions required
  288. } elseif ($this->isDeletable($path1) && $this->isCreatable(dirname($path2))) {
  289. $rootView = new \OC\Files\View('');
  290. return $rootView->rename($oldSource, $newSource);
  291. }
  292. }
  293. }
  294. return false;
  295. }
  296. public function copy($path1, $path2) {
  297. // Copy the file if CREATE permission is granted
  298. if ($this->isCreatable(dirname($path2))) {
  299. $oldSource = $this->getSourcePath($path1);
  300. $newSource = $this->getSourcePath(dirname($path2)) . '/' . basename($path2);
  301. $rootView = new \OC\Files\View('');
  302. return $rootView->copy($oldSource, $newSource);
  303. }
  304. return false;
  305. }
  306. public function fopen($path, $mode) {
  307. if ($source = $this->getSourcePath($path)) {
  308. switch ($mode) {
  309. case 'r+':
  310. case 'rb+':
  311. case 'w+':
  312. case 'wb+':
  313. case 'x+':
  314. case 'xb+':
  315. case 'a+':
  316. case 'ab+':
  317. case 'w':
  318. case 'wb':
  319. case 'x':
  320. case 'xb':
  321. case 'a':
  322. case 'ab':
  323. $exists = $this->file_exists($path);
  324. if ($exists && !$this->isUpdatable($path)) {
  325. return false;
  326. }
  327. if (!$exists && !$this->isCreatable(dirname($path))) {
  328. return false;
  329. }
  330. }
  331. $info = array(
  332. 'target' => $this->sharedFolder . $path,
  333. 'source' => $source,
  334. 'mode' => $mode,
  335. );
  336. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
  337. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  338. return $storage->fopen($internalPath, $mode);
  339. }
  340. return false;
  341. }
  342. public function getMimeType($path) {
  343. if ($path == '' || $path == '/') {
  344. return 'httpd/unix-directory';
  345. }
  346. if ($source = $this->getSourcePath($path)) {
  347. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  348. return $storage->getMimeType($internalPath);
  349. }
  350. return false;
  351. }
  352. public function free_space($path) {
  353. if ($path == '') {
  354. return \OC\Files\SPACE_UNKNOWN;
  355. }
  356. $source = $this->getSourcePath($path);
  357. if ($source) {
  358. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  359. return $storage->free_space($internalPath);
  360. }
  361. }
  362. public function getLocalFile($path) {
  363. if ($source = $this->getSourcePath($path)) {
  364. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  365. return $storage->getLocalFile($internalPath);
  366. }
  367. return false;
  368. }
  369. public function touch($path, $mtime = null) {
  370. if ($source = $this->getSourcePath($path)) {
  371. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  372. return $storage->touch($internalPath, $mtime);
  373. }
  374. return false;
  375. }
  376. public static function setup($options) {
  377. if (!\OCP\User::isLoggedIn() || \OCP\User::getUser() != $options['user']
  378. || \OCP\Share::getItemsSharedWith('file')
  379. ) {
  380. $user_dir = $options['user_dir'];
  381. \OC\Files\Filesystem::mount('\OC\Files\Storage\Shared',
  382. array('sharedFolder' => '/Shared'),
  383. $user_dir . '/Shared/');
  384. }
  385. }
  386. public function hasUpdated($path, $time) {
  387. if ($path == '') {
  388. return false;
  389. }
  390. return $this->filemtime($path) > $time;
  391. }
  392. public function getCache($path = '') {
  393. return new \OC\Files\Cache\Shared_Cache($this);
  394. }
  395. public function getScanner($path = '') {
  396. return new \OC\Files\Cache\Scanner($this);
  397. }
  398. public function getPermissionsCache($path = '') {
  399. return new \OC\Files\Cache\Shared_Permissions($this);
  400. }
  401. public function getWatcher($path = '') {
  402. return new \OC\Files\Cache\Shared_Watcher($this);
  403. }
  404. public function getOwner($path) {
  405. if ($path == '') {
  406. return false;
  407. }
  408. $source = $this->getFile($path);
  409. if ($source) {
  410. return $source['fileOwner'];
  411. }
  412. return false;
  413. }
  414. public function getETag($path) {
  415. if ($path == '') {
  416. return parent::getETag($path);
  417. }
  418. if ($source = $this->getSourcePath($path)) {
  419. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  420. return $storage->getETag($internalPath);
  421. }
  422. return null;
  423. }
  424. }