@@ -46,442 +46,442 @@ |
||
| 46 | 46 | */ |
| 47 | 47 | class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage { |
| 48 | 48 | |
| 49 | - /** @var \OCP\Share\IShare */ |
|
| 50 | - private $superShare; |
|
| 51 | - |
|
| 52 | - /** @var \OCP\Share\IShare[] */ |
|
| 53 | - private $groupedShares; |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @var \OC\Files\View |
|
| 57 | - */ |
|
| 58 | - private $ownerView; |
|
| 59 | - |
|
| 60 | - private $initialized = false; |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * @var ICacheEntry |
|
| 64 | - */ |
|
| 65 | - private $sourceRootInfo; |
|
| 66 | - |
|
| 67 | - /** @var string */ |
|
| 68 | - private $user; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @var \OCP\ILogger |
|
| 72 | - */ |
|
| 73 | - private $logger; |
|
| 74 | - |
|
| 75 | - /** @var IStorage */ |
|
| 76 | - private $nonMaskedStorage; |
|
| 77 | - |
|
| 78 | - private $options; |
|
| 79 | - |
|
| 80 | - public function __construct($arguments) { |
|
| 81 | - $this->ownerView = $arguments['ownerView']; |
|
| 82 | - $this->logger = \OC::$server->getLogger(); |
|
| 83 | - |
|
| 84 | - $this->superShare = $arguments['superShare']; |
|
| 85 | - $this->groupedShares = $arguments['groupedShares']; |
|
| 86 | - |
|
| 87 | - $this->user = $arguments['user']; |
|
| 88 | - |
|
| 89 | - parent::__construct([ |
|
| 90 | - 'storage' => null, |
|
| 91 | - 'root' => null, |
|
| 92 | - ]); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * @return ICacheEntry |
|
| 97 | - */ |
|
| 98 | - private function getSourceRootInfo() { |
|
| 99 | - if (is_null($this->sourceRootInfo)) { |
|
| 100 | - if (is_null($this->superShare->getNodeCacheEntry())) { |
|
| 101 | - $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath); |
|
| 102 | - } else { |
|
| 103 | - $this->sourceRootInfo = $this->superShare->getNodeCacheEntry(); |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - return $this->sourceRootInfo; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - private function init() { |
|
| 110 | - if ($this->initialized) { |
|
| 111 | - return; |
|
| 112 | - } |
|
| 113 | - $this->initialized = true; |
|
| 114 | - try { |
|
| 115 | - Filesystem::initMountPoints($this->superShare->getShareOwner()); |
|
| 116 | - $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); |
|
| 117 | - list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath); |
|
| 118 | - $this->storage = new PermissionsMask([ |
|
| 119 | - 'storage' => $this->nonMaskedStorage, |
|
| 120 | - 'mask' => $this->superShare->getPermissions() |
|
| 121 | - ]); |
|
| 122 | - } catch (NotFoundException $e) { |
|
| 123 | - // original file not accessible or deleted, set FailedStorage |
|
| 124 | - $this->storage = new FailedStorage(['exception' => $e]); |
|
| 125 | - $this->rootPath = ''; |
|
| 126 | - } catch (NoUserException $e) { |
|
| 127 | - // sharer user deleted, set FailedStorage |
|
| 128 | - $this->storage = new FailedStorage(['exception' => $e]); |
|
| 129 | - $this->rootPath = ''; |
|
| 130 | - } catch (\Exception $e) { |
|
| 131 | - $this->storage = new FailedStorage(['exception' => $e]); |
|
| 132 | - $this->rootPath = ''; |
|
| 133 | - $this->logger->logException($e); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - if (!$this->nonMaskedStorage) { |
|
| 137 | - $this->nonMaskedStorage = $this->storage; |
|
| 138 | - } |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @inheritdoc |
|
| 143 | - */ |
|
| 144 | - public function instanceOfStorage($class) { |
|
| 145 | - if ($class === '\OC\Files\Storage\Common') { |
|
| 146 | - return true; |
|
| 147 | - } |
|
| 148 | - if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) { |
|
| 149 | - return false; |
|
| 150 | - } |
|
| 151 | - return parent::instanceOfStorage($class); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - public function getShareId() { |
|
| 158 | - return $this->superShare->getId(); |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - private function isValid() { |
|
| 162 | - return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - /** |
|
| 166 | - * get id of the mount point |
|
| 167 | - * |
|
| 168 | - * @return string |
|
| 169 | - */ |
|
| 170 | - public function getId() { |
|
| 171 | - return 'shared::' . $this->getMountPoint(); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * Get the permissions granted for a shared file |
|
| 176 | - * |
|
| 177 | - * @param string $target Shared target file path |
|
| 178 | - * @return int CRUDS permissions granted |
|
| 179 | - */ |
|
| 180 | - public function getPermissions($target = '') { |
|
| 181 | - if (!$this->isValid()) { |
|
| 182 | - return 0; |
|
| 183 | - } |
|
| 184 | - $permissions = $this->superShare->getPermissions(); |
|
| 185 | - // part files and the mount point always have delete permissions |
|
| 186 | - if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') { |
|
| 187 | - $permissions |= \OCP\Constants::PERMISSION_DELETE; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 191 | - $permissions &= ~\OCP\Constants::PERMISSION_SHARE; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - return $permissions; |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - public function isCreatable($path) { |
|
| 198 | - return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - public function isReadable($path) { |
|
| 202 | - if (!$this->isValid()) { |
|
| 203 | - return false; |
|
| 204 | - } |
|
| 205 | - if (!$this->file_exists($path)) { |
|
| 206 | - return false; |
|
| 207 | - } |
|
| 208 | - /** @var IStorage $storage */ |
|
| 209 | - /** @var string $internalPath */ |
|
| 210 | - list($storage, $internalPath) = $this->resolvePath($path); |
|
| 211 | - return $storage->isReadable($internalPath); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - public function isUpdatable($path) { |
|
| 215 | - return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - public function isDeletable($path) { |
|
| 219 | - return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - public function isSharable($path) { |
|
| 223 | - if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) { |
|
| 224 | - return false; |
|
| 225 | - } |
|
| 226 | - return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - public function fopen($path, $mode) { |
|
| 230 | - if ($source = $this->getSourcePath($path)) { |
|
| 231 | - switch ($mode) { |
|
| 232 | - case 'r+': |
|
| 233 | - case 'rb+': |
|
| 234 | - case 'w+': |
|
| 235 | - case 'wb+': |
|
| 236 | - case 'x+': |
|
| 237 | - case 'xb+': |
|
| 238 | - case 'a+': |
|
| 239 | - case 'ab+': |
|
| 240 | - case 'w': |
|
| 241 | - case 'wb': |
|
| 242 | - case 'x': |
|
| 243 | - case 'xb': |
|
| 244 | - case 'a': |
|
| 245 | - case 'ab': |
|
| 246 | - $creatable = $this->isCreatable($path); |
|
| 247 | - $updatable = $this->isUpdatable($path); |
|
| 248 | - // if neither permissions given, no need to continue |
|
| 249 | - if (!$creatable && !$updatable) { |
|
| 250 | - return false; |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - $exists = $this->file_exists($path); |
|
| 254 | - // if a file exists, updatable permissions are required |
|
| 255 | - if ($exists && !$updatable) { |
|
| 256 | - return false; |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - // part file is allowed if !$creatable but the final file is $updatable |
|
| 260 | - if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') { |
|
| 261 | - if (!$exists && !$creatable) { |
|
| 262 | - return false; |
|
| 263 | - } |
|
| 264 | - } |
|
| 265 | - } |
|
| 266 | - $info = array( |
|
| 267 | - 'target' => $this->getMountPoint() . $path, |
|
| 268 | - 'source' => $source, |
|
| 269 | - 'mode' => $mode, |
|
| 270 | - ); |
|
| 271 | - \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info); |
|
| 272 | - return $this->nonMaskedStorage->fopen($this->getSourcePath($path), $mode); |
|
| 273 | - } |
|
| 274 | - return false; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - /** |
|
| 278 | - * see http://php.net/manual/en/function.rename.php |
|
| 279 | - * |
|
| 280 | - * @param string $path1 |
|
| 281 | - * @param string $path2 |
|
| 282 | - * @return bool |
|
| 283 | - */ |
|
| 284 | - public function rename($path1, $path2) { |
|
| 285 | - $this->init(); |
|
| 286 | - $isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part'; |
|
| 287 | - $targetExists = $this->file_exists($path2); |
|
| 288 | - $sameFodler = dirname($path1) === dirname($path2); |
|
| 289 | - |
|
| 290 | - if ($targetExists || ($sameFodler && !$isPartFile)) { |
|
| 291 | - if (!$this->isUpdatable('')) { |
|
| 292 | - return false; |
|
| 293 | - } |
|
| 294 | - } else { |
|
| 295 | - if (!$this->isCreatable('')) { |
|
| 296 | - return false; |
|
| 297 | - } |
|
| 298 | - } |
|
| 299 | - |
|
| 300 | - return $this->nonMaskedStorage->rename($this->getSourcePath($path1), $this->getSourcePath($path2)); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - /** |
|
| 304 | - * return mount point of share, relative to data/user/files |
|
| 305 | - * |
|
| 306 | - * @return string |
|
| 307 | - */ |
|
| 308 | - public function getMountPoint() { |
|
| 309 | - return $this->superShare->getTarget(); |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - /** |
|
| 313 | - * @param string $path |
|
| 314 | - */ |
|
| 315 | - public function setMountPoint($path) { |
|
| 316 | - $this->superShare->setTarget($path); |
|
| 317 | - |
|
| 318 | - foreach ($this->groupedShares as $share) { |
|
| 319 | - $share->setTarget($path); |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - /** |
|
| 324 | - * get the user who shared the file |
|
| 325 | - * |
|
| 326 | - * @return string |
|
| 327 | - */ |
|
| 328 | - public function getSharedFrom() { |
|
| 329 | - return $this->superShare->getShareOwner(); |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - /** |
|
| 333 | - * @return \OCP\Share\IShare |
|
| 334 | - */ |
|
| 335 | - public function getShare() { |
|
| 336 | - return $this->superShare; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * return share type, can be "file" or "folder" |
|
| 341 | - * |
|
| 342 | - * @return string |
|
| 343 | - */ |
|
| 344 | - public function getItemType() { |
|
| 345 | - return $this->superShare->getNodeType(); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - public function getCache($path = '', $storage = null) { |
|
| 349 | - if ($this->cache) { |
|
| 350 | - return $this->cache; |
|
| 351 | - } |
|
| 352 | - if (!$storage) { |
|
| 353 | - $storage = $this; |
|
| 354 | - } |
|
| 355 | - $this->cache = new \OCA\Files_Sharing\Cache($storage, $this->getSourceRootInfo(), $this->superShare); |
|
| 356 | - return $this->cache; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - public function getScanner($path = '', $storage = null) { |
|
| 360 | - if (!$storage) { |
|
| 361 | - $storage = $this; |
|
| 362 | - } |
|
| 363 | - return new \OCA\Files_Sharing\Scanner($storage); |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - public function getPropagator($storage = null) { |
|
| 367 | - if (isset($this->propagator)) { |
|
| 368 | - return $this->propagator; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - if (!$storage) { |
|
| 372 | - $storage = $this; |
|
| 373 | - } |
|
| 374 | - $this->propagator = new \OCA\Files_Sharing\SharedPropagator($storage, \OC::$server->getDatabaseConnection()); |
|
| 375 | - return $this->propagator; |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - public function getOwner($path) { |
|
| 379 | - return $this->superShare->getShareOwner(); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - /** |
|
| 383 | - * unshare complete storage, also the grouped shares |
|
| 384 | - * |
|
| 385 | - * @return bool |
|
| 386 | - */ |
|
| 387 | - public function unshareStorage() { |
|
| 388 | - foreach ($this->groupedShares as $share) { |
|
| 389 | - \OC::$server->getShareManager()->deleteFromSelf($share, $this->user); |
|
| 390 | - } |
|
| 391 | - return true; |
|
| 392 | - } |
|
| 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 | - * @throws \OCP\Lock\LockedException |
|
| 399 | - */ |
|
| 400 | - public function acquireLock($path, $type, ILockingProvider $provider) { |
|
| 401 | - /** @var \OCP\Files\Storage $targetStorage */ |
|
| 402 | - list($targetStorage, $targetInternalPath) = $this->resolvePath($path); |
|
| 403 | - $targetStorage->acquireLock($targetInternalPath, $type, $provider); |
|
| 404 | - // lock the parent folders of the owner when locking the share as recipient |
|
| 405 | - if ($path === '') { |
|
| 406 | - $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); |
|
| 407 | - $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); |
|
| 408 | - } |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * @param string $path |
|
| 413 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 414 | - * @param \OCP\Lock\ILockingProvider $provider |
|
| 415 | - */ |
|
| 416 | - public function releaseLock($path, $type, ILockingProvider $provider) { |
|
| 417 | - /** @var \OCP\Files\Storage $targetStorage */ |
|
| 418 | - list($targetStorage, $targetInternalPath) = $this->resolvePath($path); |
|
| 419 | - $targetStorage->releaseLock($targetInternalPath, $type, $provider); |
|
| 420 | - // unlock the parent folders of the owner when unlocking the share as recipient |
|
| 421 | - if ($path === '') { |
|
| 422 | - $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); |
|
| 423 | - $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * @param string $path |
|
| 429 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 430 | - * @param \OCP\Lock\ILockingProvider $provider |
|
| 431 | - */ |
|
| 432 | - public function changeLock($path, $type, ILockingProvider $provider) { |
|
| 433 | - /** @var \OCP\Files\Storage $targetStorage */ |
|
| 434 | - list($targetStorage, $targetInternalPath) = $this->resolvePath($path); |
|
| 435 | - $targetStorage->changeLock($targetInternalPath, $type, $provider); |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - /** |
|
| 439 | - * @return array [ available, last_checked ] |
|
| 440 | - */ |
|
| 441 | - public function getAvailability() { |
|
| 442 | - // shares do not participate in availability logic |
|
| 443 | - return [ |
|
| 444 | - 'available' => true, |
|
| 445 | - 'last_checked' => 0 |
|
| 446 | - ]; |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * @param bool $available |
|
| 451 | - */ |
|
| 452 | - public function setAvailability($available) { |
|
| 453 | - // shares do not participate in availability logic |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - public function getSourceStorage() { |
|
| 457 | - $this->init(); |
|
| 458 | - return $this->nonMaskedStorage; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - public function getWrapperStorage() { |
|
| 462 | - $this->init(); |
|
| 463 | - return $this->storage; |
|
| 464 | - } |
|
| 465 | - |
|
| 466 | - public function file_get_contents($path) { |
|
| 467 | - $info = [ |
|
| 468 | - 'target' => $this->getMountPoint() . '/' . $path, |
|
| 469 | - 'source' => $this->getSourcePath($path), |
|
| 470 | - ]; |
|
| 471 | - \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info); |
|
| 472 | - return parent::file_get_contents($path); |
|
| 473 | - } |
|
| 474 | - |
|
| 475 | - public function file_put_contents($path, $data) { |
|
| 476 | - $info = [ |
|
| 477 | - 'target' => $this->getMountPoint() . '/' . $path, |
|
| 478 | - 'source' => $this->getSourcePath($path), |
|
| 479 | - ]; |
|
| 480 | - \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info); |
|
| 481 | - return parent::file_put_contents($path, $data); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - public function setMountOptions(array $options) { |
|
| 485 | - $this->mountOptions = $options; |
|
| 486 | - } |
|
| 49 | + /** @var \OCP\Share\IShare */ |
|
| 50 | + private $superShare; |
|
| 51 | + |
|
| 52 | + /** @var \OCP\Share\IShare[] */ |
|
| 53 | + private $groupedShares; |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @var \OC\Files\View |
|
| 57 | + */ |
|
| 58 | + private $ownerView; |
|
| 59 | + |
|
| 60 | + private $initialized = false; |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * @var ICacheEntry |
|
| 64 | + */ |
|
| 65 | + private $sourceRootInfo; |
|
| 66 | + |
|
| 67 | + /** @var string */ |
|
| 68 | + private $user; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @var \OCP\ILogger |
|
| 72 | + */ |
|
| 73 | + private $logger; |
|
| 74 | + |
|
| 75 | + /** @var IStorage */ |
|
| 76 | + private $nonMaskedStorage; |
|
| 77 | + |
|
| 78 | + private $options; |
|
| 79 | + |
|
| 80 | + public function __construct($arguments) { |
|
| 81 | + $this->ownerView = $arguments['ownerView']; |
|
| 82 | + $this->logger = \OC::$server->getLogger(); |
|
| 83 | + |
|
| 84 | + $this->superShare = $arguments['superShare']; |
|
| 85 | + $this->groupedShares = $arguments['groupedShares']; |
|
| 86 | + |
|
| 87 | + $this->user = $arguments['user']; |
|
| 88 | + |
|
| 89 | + parent::__construct([ |
|
| 90 | + 'storage' => null, |
|
| 91 | + 'root' => null, |
|
| 92 | + ]); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * @return ICacheEntry |
|
| 97 | + */ |
|
| 98 | + private function getSourceRootInfo() { |
|
| 99 | + if (is_null($this->sourceRootInfo)) { |
|
| 100 | + if (is_null($this->superShare->getNodeCacheEntry())) { |
|
| 101 | + $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath); |
|
| 102 | + } else { |
|
| 103 | + $this->sourceRootInfo = $this->superShare->getNodeCacheEntry(); |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + return $this->sourceRootInfo; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + private function init() { |
|
| 110 | + if ($this->initialized) { |
|
| 111 | + return; |
|
| 112 | + } |
|
| 113 | + $this->initialized = true; |
|
| 114 | + try { |
|
| 115 | + Filesystem::initMountPoints($this->superShare->getShareOwner()); |
|
| 116 | + $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); |
|
| 117 | + list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath); |
|
| 118 | + $this->storage = new PermissionsMask([ |
|
| 119 | + 'storage' => $this->nonMaskedStorage, |
|
| 120 | + 'mask' => $this->superShare->getPermissions() |
|
| 121 | + ]); |
|
| 122 | + } catch (NotFoundException $e) { |
|
| 123 | + // original file not accessible or deleted, set FailedStorage |
|
| 124 | + $this->storage = new FailedStorage(['exception' => $e]); |
|
| 125 | + $this->rootPath = ''; |
|
| 126 | + } catch (NoUserException $e) { |
|
| 127 | + // sharer user deleted, set FailedStorage |
|
| 128 | + $this->storage = new FailedStorage(['exception' => $e]); |
|
| 129 | + $this->rootPath = ''; |
|
| 130 | + } catch (\Exception $e) { |
|
| 131 | + $this->storage = new FailedStorage(['exception' => $e]); |
|
| 132 | + $this->rootPath = ''; |
|
| 133 | + $this->logger->logException($e); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + if (!$this->nonMaskedStorage) { |
|
| 137 | + $this->nonMaskedStorage = $this->storage; |
|
| 138 | + } |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @inheritdoc |
|
| 143 | + */ |
|
| 144 | + public function instanceOfStorage($class) { |
|
| 145 | + if ($class === '\OC\Files\Storage\Common') { |
|
| 146 | + return true; |
|
| 147 | + } |
|
| 148 | + if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) { |
|
| 149 | + return false; |
|
| 150 | + } |
|
| 151 | + return parent::instanceOfStorage($class); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + public function getShareId() { |
|
| 158 | + return $this->superShare->getId(); |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + private function isValid() { |
|
| 162 | + return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + /** |
|
| 166 | + * get id of the mount point |
|
| 167 | + * |
|
| 168 | + * @return string |
|
| 169 | + */ |
|
| 170 | + public function getId() { |
|
| 171 | + return 'shared::' . $this->getMountPoint(); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * Get the permissions granted for a shared file |
|
| 176 | + * |
|
| 177 | + * @param string $target Shared target file path |
|
| 178 | + * @return int CRUDS permissions granted |
|
| 179 | + */ |
|
| 180 | + public function getPermissions($target = '') { |
|
| 181 | + if (!$this->isValid()) { |
|
| 182 | + return 0; |
|
| 183 | + } |
|
| 184 | + $permissions = $this->superShare->getPermissions(); |
|
| 185 | + // part files and the mount point always have delete permissions |
|
| 186 | + if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') { |
|
| 187 | + $permissions |= \OCP\Constants::PERMISSION_DELETE; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + if (\OCP\Util::isSharingDisabledForUser()) { |
|
| 191 | + $permissions &= ~\OCP\Constants::PERMISSION_SHARE; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + return $permissions; |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + public function isCreatable($path) { |
|
| 198 | + return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + public function isReadable($path) { |
|
| 202 | + if (!$this->isValid()) { |
|
| 203 | + return false; |
|
| 204 | + } |
|
| 205 | + if (!$this->file_exists($path)) { |
|
| 206 | + return false; |
|
| 207 | + } |
|
| 208 | + /** @var IStorage $storage */ |
|
| 209 | + /** @var string $internalPath */ |
|
| 210 | + list($storage, $internalPath) = $this->resolvePath($path); |
|
| 211 | + return $storage->isReadable($internalPath); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + public function isUpdatable($path) { |
|
| 215 | + return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + public function isDeletable($path) { |
|
| 219 | + return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + public function isSharable($path) { |
|
| 223 | + if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) { |
|
| 224 | + return false; |
|
| 225 | + } |
|
| 226 | + return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + public function fopen($path, $mode) { |
|
| 230 | + if ($source = $this->getSourcePath($path)) { |
|
| 231 | + switch ($mode) { |
|
| 232 | + case 'r+': |
|
| 233 | + case 'rb+': |
|
| 234 | + case 'w+': |
|
| 235 | + case 'wb+': |
|
| 236 | + case 'x+': |
|
| 237 | + case 'xb+': |
|
| 238 | + case 'a+': |
|
| 239 | + case 'ab+': |
|
| 240 | + case 'w': |
|
| 241 | + case 'wb': |
|
| 242 | + case 'x': |
|
| 243 | + case 'xb': |
|
| 244 | + case 'a': |
|
| 245 | + case 'ab': |
|
| 246 | + $creatable = $this->isCreatable($path); |
|
| 247 | + $updatable = $this->isUpdatable($path); |
|
| 248 | + // if neither permissions given, no need to continue |
|
| 249 | + if (!$creatable && !$updatable) { |
|
| 250 | + return false; |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + $exists = $this->file_exists($path); |
|
| 254 | + // if a file exists, updatable permissions are required |
|
| 255 | + if ($exists && !$updatable) { |
|
| 256 | + return false; |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + // part file is allowed if !$creatable but the final file is $updatable |
|
| 260 | + if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') { |
|
| 261 | + if (!$exists && !$creatable) { |
|
| 262 | + return false; |
|
| 263 | + } |
|
| 264 | + } |
|
| 265 | + } |
|
| 266 | + $info = array( |
|
| 267 | + 'target' => $this->getMountPoint() . $path, |
|
| 268 | + 'source' => $source, |
|
| 269 | + 'mode' => $mode, |
|
| 270 | + ); |
|
| 271 | + \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info); |
|
| 272 | + return $this->nonMaskedStorage->fopen($this->getSourcePath($path), $mode); |
|
| 273 | + } |
|
| 274 | + return false; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + /** |
|
| 278 | + * see http://php.net/manual/en/function.rename.php |
|
| 279 | + * |
|
| 280 | + * @param string $path1 |
|
| 281 | + * @param string $path2 |
|
| 282 | + * @return bool |
|
| 283 | + */ |
|
| 284 | + public function rename($path1, $path2) { |
|
| 285 | + $this->init(); |
|
| 286 | + $isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part'; |
|
| 287 | + $targetExists = $this->file_exists($path2); |
|
| 288 | + $sameFodler = dirname($path1) === dirname($path2); |
|
| 289 | + |
|
| 290 | + if ($targetExists || ($sameFodler && !$isPartFile)) { |
|
| 291 | + if (!$this->isUpdatable('')) { |
|
| 292 | + return false; |
|
| 293 | + } |
|
| 294 | + } else { |
|
| 295 | + if (!$this->isCreatable('')) { |
|
| 296 | + return false; |
|
| 297 | + } |
|
| 298 | + } |
|
| 299 | + |
|
| 300 | + return $this->nonMaskedStorage->rename($this->getSourcePath($path1), $this->getSourcePath($path2)); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + /** |
|
| 304 | + * return mount point of share, relative to data/user/files |
|
| 305 | + * |
|
| 306 | + * @return string |
|
| 307 | + */ |
|
| 308 | + public function getMountPoint() { |
|
| 309 | + return $this->superShare->getTarget(); |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + /** |
|
| 313 | + * @param string $path |
|
| 314 | + */ |
|
| 315 | + public function setMountPoint($path) { |
|
| 316 | + $this->superShare->setTarget($path); |
|
| 317 | + |
|
| 318 | + foreach ($this->groupedShares as $share) { |
|
| 319 | + $share->setTarget($path); |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + /** |
|
| 324 | + * get the user who shared the file |
|
| 325 | + * |
|
| 326 | + * @return string |
|
| 327 | + */ |
|
| 328 | + public function getSharedFrom() { |
|
| 329 | + return $this->superShare->getShareOwner(); |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + /** |
|
| 333 | + * @return \OCP\Share\IShare |
|
| 334 | + */ |
|
| 335 | + public function getShare() { |
|
| 336 | + return $this->superShare; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * return share type, can be "file" or "folder" |
|
| 341 | + * |
|
| 342 | + * @return string |
|
| 343 | + */ |
|
| 344 | + public function getItemType() { |
|
| 345 | + return $this->superShare->getNodeType(); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + public function getCache($path = '', $storage = null) { |
|
| 349 | + if ($this->cache) { |
|
| 350 | + return $this->cache; |
|
| 351 | + } |
|
| 352 | + if (!$storage) { |
|
| 353 | + $storage = $this; |
|
| 354 | + } |
|
| 355 | + $this->cache = new \OCA\Files_Sharing\Cache($storage, $this->getSourceRootInfo(), $this->superShare); |
|
| 356 | + return $this->cache; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + public function getScanner($path = '', $storage = null) { |
|
| 360 | + if (!$storage) { |
|
| 361 | + $storage = $this; |
|
| 362 | + } |
|
| 363 | + return new \OCA\Files_Sharing\Scanner($storage); |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + public function getPropagator($storage = null) { |
|
| 367 | + if (isset($this->propagator)) { |
|
| 368 | + return $this->propagator; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + if (!$storage) { |
|
| 372 | + $storage = $this; |
|
| 373 | + } |
|
| 374 | + $this->propagator = new \OCA\Files_Sharing\SharedPropagator($storage, \OC::$server->getDatabaseConnection()); |
|
| 375 | + return $this->propagator; |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + public function getOwner($path) { |
|
| 379 | + return $this->superShare->getShareOwner(); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + /** |
|
| 383 | + * unshare complete storage, also the grouped shares |
|
| 384 | + * |
|
| 385 | + * @return bool |
|
| 386 | + */ |
|
| 387 | + public function unshareStorage() { |
|
| 388 | + foreach ($this->groupedShares as $share) { |
|
| 389 | + \OC::$server->getShareManager()->deleteFromSelf($share, $this->user); |
|
| 390 | + } |
|
| 391 | + return true; |
|
| 392 | + } |
|
| 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 | + * @throws \OCP\Lock\LockedException |
|
| 399 | + */ |
|
| 400 | + public function acquireLock($path, $type, ILockingProvider $provider) { |
|
| 401 | + /** @var \OCP\Files\Storage $targetStorage */ |
|
| 402 | + list($targetStorage, $targetInternalPath) = $this->resolvePath($path); |
|
| 403 | + $targetStorage->acquireLock($targetInternalPath, $type, $provider); |
|
| 404 | + // lock the parent folders of the owner when locking the share as recipient |
|
| 405 | + if ($path === '') { |
|
| 406 | + $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); |
|
| 407 | + $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); |
|
| 408 | + } |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * @param string $path |
|
| 413 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 414 | + * @param \OCP\Lock\ILockingProvider $provider |
|
| 415 | + */ |
|
| 416 | + public function releaseLock($path, $type, ILockingProvider $provider) { |
|
| 417 | + /** @var \OCP\Files\Storage $targetStorage */ |
|
| 418 | + list($targetStorage, $targetInternalPath) = $this->resolvePath($path); |
|
| 419 | + $targetStorage->releaseLock($targetInternalPath, $type, $provider); |
|
| 420 | + // unlock the parent folders of the owner when unlocking the share as recipient |
|
| 421 | + if ($path === '') { |
|
| 422 | + $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); |
|
| 423 | + $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * @param string $path |
|
| 429 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
|
| 430 | + * @param \OCP\Lock\ILockingProvider $provider |
|
| 431 | + */ |
|
| 432 | + public function changeLock($path, $type, ILockingProvider $provider) { |
|
| 433 | + /** @var \OCP\Files\Storage $targetStorage */ |
|
| 434 | + list($targetStorage, $targetInternalPath) = $this->resolvePath($path); |
|
| 435 | + $targetStorage->changeLock($targetInternalPath, $type, $provider); |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + /** |
|
| 439 | + * @return array [ available, last_checked ] |
|
| 440 | + */ |
|
| 441 | + public function getAvailability() { |
|
| 442 | + // shares do not participate in availability logic |
|
| 443 | + return [ |
|
| 444 | + 'available' => true, |
|
| 445 | + 'last_checked' => 0 |
|
| 446 | + ]; |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * @param bool $available |
|
| 451 | + */ |
|
| 452 | + public function setAvailability($available) { |
|
| 453 | + // shares do not participate in availability logic |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + public function getSourceStorage() { |
|
| 457 | + $this->init(); |
|
| 458 | + return $this->nonMaskedStorage; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + public function getWrapperStorage() { |
|
| 462 | + $this->init(); |
|
| 463 | + return $this->storage; |
|
| 464 | + } |
|
| 465 | + |
|
| 466 | + public function file_get_contents($path) { |
|
| 467 | + $info = [ |
|
| 468 | + 'target' => $this->getMountPoint() . '/' . $path, |
|
| 469 | + 'source' => $this->getSourcePath($path), |
|
| 470 | + ]; |
|
| 471 | + \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info); |
|
| 472 | + return parent::file_get_contents($path); |
|
| 473 | + } |
|
| 474 | + |
|
| 475 | + public function file_put_contents($path, $data) { |
|
| 476 | + $info = [ |
|
| 477 | + 'target' => $this->getMountPoint() . '/' . $path, |
|
| 478 | + 'source' => $this->getSourcePath($path), |
|
| 479 | + ]; |
|
| 480 | + \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info); |
|
| 481 | + return parent::file_put_contents($path, $data); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + public function setMountOptions(array $options) { |
|
| 485 | + $this->mountOptions = $options; |
|
| 486 | + } |
|
| 487 | 487 | } |