PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/files_sharing/lib/sharedstorage.php

https://github.com/sezuan/core
PHP | 473 lines | 382 code | 41 blank | 50 comment | 130 complexity | 68733b7013c9d85ad1145acf5377bed5 MD5 | raw file
Possible License(s): AGPL-3.0, AGPL-1.0, MPL-2.0-no-copyleft-exception
  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. private 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. private 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. if ($dh = $this->opendir($path)) {
  202. while (($filename = readdir($dh)) !== false) {
  203. $tempmtime = $this->filemtime($filename);
  204. if ($tempmtime > $mtime) {
  205. $mtime = $tempmtime;
  206. }
  207. }
  208. }
  209. return $mtime;
  210. } else {
  211. $source = $this->getSourcePath($path);
  212. if ($source) {
  213. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  214. return $storage->filemtime($internalPath);
  215. }
  216. }
  217. }
  218. public function file_get_contents($path) {
  219. $source = $this->getSourcePath($path);
  220. if ($source) {
  221. $info = array(
  222. 'target' => $this->sharedFolder.$path,
  223. 'source' => $source,
  224. );
  225. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info);
  226. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  227. return $storage->file_get_contents($internalPath);
  228. }
  229. }
  230. public function file_put_contents($path, $data) {
  231. if ($source = $this->getSourcePath($path)) {
  232. // Check if permission is granted
  233. if (($this->file_exists($path) && !$this->isUpdatable($path))
  234. || ($this->is_dir($path) && !$this->isCreatable($path))) {
  235. return false;
  236. }
  237. $info = array(
  238. 'target' => $this->sharedFolder.$path,
  239. 'source' => $source,
  240. );
  241. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info);
  242. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  243. $result = $storage->file_put_contents($internalPath, $data);
  244. return $result;
  245. }
  246. return false;
  247. }
  248. public function unlink($path) {
  249. // Delete the file if DELETE permission is granted
  250. if ($source = $this->getSourcePath($path)) {
  251. if ($this->isDeletable($path)) {
  252. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  253. return $storage->unlink($internalPath);
  254. } else if (dirname($path) == '/' || dirname($path) == '.') {
  255. // Unshare the file from the user if in the root of the Shared folder
  256. if ($this->is_dir($path)) {
  257. $itemType = 'folder';
  258. } else {
  259. $itemType = 'file';
  260. }
  261. return \OCP\Share::unshareFromSelf($itemType, $path);
  262. }
  263. }
  264. return false;
  265. }
  266. public function rename($path1, $path2) {
  267. // Check for partial files
  268. if (pathinfo($path1, PATHINFO_EXTENSION) === 'part') {
  269. if ($oldSource = $this->getSourcePath($path1)) {
  270. list($storage, $oldInternalPath) = \OC\Files\Filesystem::resolvePath($oldSource);
  271. $newInternalPath = substr($oldInternalPath, 0, -5);
  272. return $storage->rename($oldInternalPath, $newInternalPath);
  273. }
  274. } else {
  275. // Renaming/moving is only allowed within shared folders
  276. $pos1 = strpos($path1, '/', 1);
  277. $pos2 = strpos($path2, '/', 1);
  278. if ($pos1 !== false && $pos2 !== false && ($oldSource = $this->getSourcePath($path1))) {
  279. $newSource = $this->getSourcePath(dirname($path2)).'/'.basename($path2);
  280. if (dirname($path1) == dirname($path2)) {
  281. // Rename the file if UPDATE permission is granted
  282. if ($this->isUpdatable($path1)) {
  283. list($storage, $oldInternalPath) = \OC\Files\Filesystem::resolvePath($oldSource);
  284. list( , $newInternalPath) = \OC\Files\Filesystem::resolvePath($newSource);
  285. return $storage->rename($oldInternalPath, $newInternalPath);
  286. }
  287. } else {
  288. // Move the file if DELETE and CREATE permissions are granted
  289. if ($this->isDeletable($path1) && $this->isCreatable(dirname($path2))) {
  290. // Get the root shared folder
  291. $folder1 = substr($path1, 0, $pos1);
  292. $folder2 = substr($path2, 0, $pos2);
  293. // Copy and unlink the file if it exists in a different shared folder
  294. if ($folder1 != $folder2) {
  295. if ($this->copy($path1, $path2)) {
  296. return $this->unlink($path1);
  297. }
  298. } else {
  299. list($storage, $oldInternalPath) = \OC\Files\Filesystem::resolvePath($oldSource);
  300. list( , $newInternalPath) = \OC\Files\Filesystem::resolvePath($newSource);
  301. return $storage->rename($oldInternalPath, $newInternalPath);
  302. }
  303. }
  304. }
  305. }
  306. }
  307. return false;
  308. }
  309. public function copy($path1, $path2) {
  310. // Copy the file if CREATE permission is granted
  311. if ($this->isCreatable(dirname($path2))) {
  312. $source = $this->fopen($path1, 'r');
  313. $target = $this->fopen($path2, 'w');
  314. list ($count, $result) = \OC_Helper::streamCopy($source, $target);
  315. return $result;
  316. }
  317. return false;
  318. }
  319. public function fopen($path, $mode) {
  320. if ($source = $this->getSourcePath($path)) {
  321. switch ($mode) {
  322. case 'r+':
  323. case 'rb+':
  324. case 'w+':
  325. case 'wb+':
  326. case 'x+':
  327. case 'xb+':
  328. case 'a+':
  329. case 'ab+':
  330. case 'w':
  331. case 'wb':
  332. case 'x':
  333. case 'xb':
  334. case 'a':
  335. case 'ab':
  336. if (!$this->isUpdatable($path)) {
  337. return false;
  338. }
  339. }
  340. $info = array(
  341. 'target' => $this->sharedFolder.$path,
  342. 'source' => $source,
  343. 'mode' => $mode,
  344. );
  345. \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info);
  346. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  347. return $storage->fopen($internalPath, $mode);
  348. }
  349. return false;
  350. }
  351. public function getMimeType($path) {
  352. if ($path == '' || $path == '/') {
  353. return 'httpd/unix-directory';
  354. }
  355. if ($source = $this->getSourcePath($path)) {
  356. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  357. return $storage->getMimeType($internalPath);
  358. }
  359. return false;
  360. }
  361. public function free_space($path) {
  362. if ($path == '') {
  363. return \OC\Files\FREE_SPACE_UNKNOWN;
  364. }
  365. $source = $this->getSourcePath($path);
  366. if ($source) {
  367. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  368. return $storage->free_space($internalPath);
  369. }
  370. }
  371. public function getLocalFile($path) {
  372. if ($source = $this->getSourcePath($path)) {
  373. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  374. return $storage->getLocalFile($internalPath);
  375. }
  376. return false;
  377. }
  378. public function touch($path, $mtime = null) {
  379. if ($source = $this->getSourcePath($path)) {
  380. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  381. return $storage->touch($internalPath, $mtime);
  382. }
  383. return false;
  384. }
  385. public static function setup($options) {
  386. if (!\OCP\User::isLoggedIn() || \OCP\User::getUser() != $options['user']
  387. || \OCP\Share::getItemsSharedWith('file')) {
  388. $user_dir = $options['user_dir'];
  389. \OC\Files\Filesystem::mount('\OC\Files\Storage\Shared',
  390. array('sharedFolder' => '/Shared'),
  391. $user_dir.'/Shared/');
  392. }
  393. }
  394. public function hasUpdated($path, $time) {
  395. if ($path == '') {
  396. return false;
  397. }
  398. return $this->filemtime($path) > $time;
  399. }
  400. public function getCache($path = '') {
  401. return new \OC\Files\Cache\Shared_Cache($this);
  402. }
  403. public function getScanner($path = '') {
  404. return new \OC\Files\Cache\Scanner($this);
  405. }
  406. public function getPermissionsCache($path = '') {
  407. return new \OC\Files\Cache\Shared_Permissions($this);
  408. }
  409. public function getWatcher($path = '') {
  410. return new \OC\Files\Cache\Shared_Watcher($this);
  411. }
  412. public function getOwner($path) {
  413. if ($path == '') {
  414. return false;
  415. }
  416. $source = $this->getFile($path);
  417. if ($source) {
  418. return $source['fileOwner'];
  419. }
  420. return false;
  421. }
  422. public function getETag($path) {
  423. if ($path == '') {
  424. return parent::getETag($path);
  425. }
  426. if ($source = $this->getSourcePath($path)) {
  427. list($storage, $internalPath) = \OC\Files\Filesystem::resolvePath($source);
  428. return $storage->getETag($internalPath);
  429. }
  430. return null;
  431. }
  432. }