| @@ -48,450 +48,450 @@ | ||
| 48 | 48 | */ | 
| 49 | 49 |  class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedStorage { | 
| 50 | 50 | |
| 51 | - /** @var \OCP\Share\IShare */ | |
| 52 | - private $superShare; | |
| 53 | - | |
| 54 | - /** @var \OCP\Share\IShare[] */ | |
| 55 | - private $groupedShares; | |
| 56 | - | |
| 57 | - /** | |
| 58 | - * @var \OC\Files\View | |
| 59 | - */ | |
| 60 | - private $ownerView; | |
| 61 | - | |
| 62 | - private $initialized = false; | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * @var ICacheEntry | |
| 66 | - */ | |
| 67 | - private $sourceRootInfo; | |
| 68 | - | |
| 69 | - /** @var string */ | |
| 70 | - private $user; | |
| 71 | - | |
| 72 | - /** | |
| 73 | - * @var \OCP\ILogger | |
| 74 | - */ | |
| 75 | - private $logger; | |
| 76 | - | |
| 77 | - /** @var IStorage */ | |
| 78 | - private $nonMaskedStorage; | |
| 79 | - | |
| 80 | - private $options; | |
| 81 | - | |
| 82 | - /** @var boolean */ | |
| 83 | - private $sharingDisabledForUser; | |
| 84 | - | |
| 85 | -	public function __construct($arguments) { | |
| 86 | - $this->ownerView = $arguments['ownerView']; | |
| 87 | - $this->logger = \OC::$server->getLogger(); | |
| 88 | - | |
| 89 | - $this->superShare = $arguments['superShare']; | |
| 90 | - $this->groupedShares = $arguments['groupedShares']; | |
| 91 | - | |
| 92 | - $this->user = $arguments['user']; | |
| 93 | -		if (isset($arguments['sharingDisabledForUser'])) { | |
| 94 | - $this->sharingDisabledForUser = $arguments['sharingDisabledForUser']; | |
| 95 | -		} else { | |
| 96 | - $this->sharingDisabledForUser = false; | |
| 97 | - } | |
| 98 | - | |
| 99 | - parent::__construct([ | |
| 100 | - 'storage' => null, | |
| 101 | - 'root' => null, | |
| 102 | - ]); | |
| 103 | - } | |
| 104 | - | |
| 105 | - /** | |
| 106 | - * @return ICacheEntry | |
| 107 | - */ | |
| 108 | -	private function getSourceRootInfo() { | |
| 109 | -		if (is_null($this->sourceRootInfo)) { | |
| 110 | -			if (is_null($this->superShare->getNodeCacheEntry())) { | |
| 111 | - $this->init(); | |
| 112 | - $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath); | |
| 113 | -			} else { | |
| 114 | - $this->sourceRootInfo = $this->superShare->getNodeCacheEntry(); | |
| 115 | - } | |
| 116 | - } | |
| 117 | - return $this->sourceRootInfo; | |
| 118 | - } | |
| 119 | - | |
| 120 | -	private function init() { | |
| 121 | -		if ($this->initialized) { | |
| 122 | - return; | |
| 123 | - } | |
| 124 | - $this->initialized = true; | |
| 125 | -		try { | |
| 126 | - Filesystem::initMountPoints($this->superShare->getShareOwner()); | |
| 127 | - $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); | |
| 128 | - list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath); | |
| 129 | - $this->storage = new PermissionsMask([ | |
| 130 | - 'storage' => $this->nonMaskedStorage, | |
| 131 | - 'mask' => $this->superShare->getPermissions() | |
| 132 | - ]); | |
| 133 | -		} catch (NotFoundException $e) { | |
| 134 | - // original file not accessible or deleted, set FailedStorage | |
| 135 | - $this->storage = new FailedStorage(['exception' => $e]); | |
| 136 | - $this->cache = new FailedCache(); | |
| 137 | - $this->rootPath = ''; | |
| 138 | -		} catch (NoUserException $e) { | |
| 139 | - // sharer user deleted, set FailedStorage | |
| 140 | - $this->storage = new FailedStorage(['exception' => $e]); | |
| 141 | - $this->cache = new FailedCache(); | |
| 142 | - $this->rootPath = ''; | |
| 143 | -		} catch (\Exception $e) { | |
| 144 | - $this->storage = new FailedStorage(['exception' => $e]); | |
| 145 | - $this->cache = new FailedCache(); | |
| 146 | - $this->rootPath = ''; | |
| 147 | - $this->logger->logException($e); | |
| 148 | - } | |
| 149 | - | |
| 150 | -		if (!$this->nonMaskedStorage) { | |
| 151 | - $this->nonMaskedStorage = $this->storage; | |
| 152 | - } | |
| 153 | - } | |
| 154 | - | |
| 155 | - /** | |
| 156 | - * @inheritdoc | |
| 157 | - */ | |
| 158 | -	public function instanceOfStorage($class) { | |
| 159 | -		if ($class === '\OC\Files\Storage\Common') { | |
| 160 | - return true; | |
| 161 | - } | |
| 162 | -		if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) { | |
| 163 | - return false; | |
| 164 | - } | |
| 165 | - return parent::instanceOfStorage($class); | |
| 166 | - } | |
| 167 | - | |
| 168 | - /** | |
| 169 | - * @return string | |
| 170 | - */ | |
| 171 | -	public function getShareId() { | |
| 172 | - return $this->superShare->getId(); | |
| 173 | - } | |
| 174 | - | |
| 175 | -	private function isValid() { | |
| 176 | - return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE; | |
| 177 | - } | |
| 178 | - | |
| 179 | - /** | |
| 180 | - * get id of the mount point | |
| 181 | - * | |
| 182 | - * @return string | |
| 183 | - */ | |
| 184 | -	public function getId() { | |
| 185 | - return 'shared::' . $this->getMountPoint(); | |
| 186 | - } | |
| 187 | - | |
| 188 | - /** | |
| 189 | - * Get the permissions granted for a shared file | |
| 190 | - * | |
| 191 | - * @param string $target Shared target file path | |
| 192 | - * @return int CRUDS permissions granted | |
| 193 | - */ | |
| 194 | -	public function getPermissions($target = '') { | |
| 195 | -		if (!$this->isValid()) { | |
| 196 | - return 0; | |
| 197 | - } | |
| 198 | - $permissions = $this->superShare->getPermissions(); | |
| 199 | - // part files and the mount point always have delete permissions | |
| 200 | -		if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') { | |
| 201 | - $permissions |= \OCP\Constants::PERMISSION_DELETE; | |
| 202 | - } | |
| 203 | - | |
| 204 | -		if ($this->sharingDisabledForUser) { | |
| 205 | - $permissions &= ~\OCP\Constants::PERMISSION_SHARE; | |
| 206 | - } | |
| 207 | - | |
| 208 | - return $permissions; | |
| 209 | - } | |
| 210 | - | |
| 211 | -	public function isCreatable($path) { | |
| 212 | - return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE); | |
| 213 | - } | |
| 214 | - | |
| 215 | -	public function isReadable($path) { | |
| 216 | -		if (!$this->isValid()) { | |
| 217 | - return false; | |
| 218 | - } | |
| 219 | -		if (!$this->file_exists($path)) { | |
| 220 | - return false; | |
| 221 | - } | |
| 222 | - /** @var IStorage $storage */ | |
| 223 | - /** @var string $internalPath */ | |
| 224 | - list($storage, $internalPath) = $this->resolvePath($path); | |
| 225 | - return $storage->isReadable($internalPath); | |
| 226 | - } | |
| 227 | - | |
| 228 | -	public function isUpdatable($path) { | |
| 229 | - return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE); | |
| 230 | - } | |
| 231 | - | |
| 232 | -	public function isDeletable($path) { | |
| 233 | - return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE); | |
| 234 | - } | |
| 235 | - | |
| 236 | -	public function isSharable($path) { | |
| 237 | -		if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) { | |
| 238 | - return false; | |
| 239 | - } | |
| 240 | - return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE); | |
| 241 | - } | |
| 242 | - | |
| 243 | -	public function fopen($path, $mode) { | |
| 244 | -		if ($source = $this->getUnjailedPath($path)) { | |
| 245 | -			switch ($mode) { | |
| 246 | - case 'r+': | |
| 247 | - case 'rb+': | |
| 248 | - case 'w+': | |
| 249 | - case 'wb+': | |
| 250 | - case 'x+': | |
| 251 | - case 'xb+': | |
| 252 | - case 'a+': | |
| 253 | - case 'ab+': | |
| 254 | - case 'w': | |
| 255 | - case 'wb': | |
| 256 | - case 'x': | |
| 257 | - case 'xb': | |
| 258 | - case 'a': | |
| 259 | - case 'ab': | |
| 260 | - $creatable = $this->isCreatable($path); | |
| 261 | - $updatable = $this->isUpdatable($path); | |
| 262 | - // if neither permissions given, no need to continue | |
| 263 | -					if (!$creatable && !$updatable) { | |
| 264 | - return false; | |
| 265 | - } | |
| 266 | - | |
| 267 | - $exists = $this->file_exists($path); | |
| 268 | - // if a file exists, updatable permissions are required | |
| 269 | -					if ($exists && !$updatable) { | |
| 270 | - return false; | |
| 271 | - } | |
| 272 | - | |
| 273 | - // part file is allowed if !$creatable but the final file is $updatable | |
| 274 | -					if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') { | |
| 275 | -						if (!$exists && !$creatable) { | |
| 276 | - return false; | |
| 277 | - } | |
| 278 | - } | |
| 279 | - } | |
| 280 | - $info = array( | |
| 281 | - 'target' => $this->getMountPoint() . $path, | |
| 282 | - 'source' => $source, | |
| 283 | - 'mode' => $mode, | |
| 284 | - ); | |
| 285 | -			\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info); | |
| 286 | - return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode); | |
| 287 | - } | |
| 288 | - return false; | |
| 289 | - } | |
| 290 | - | |
| 291 | - /** | |
| 292 | - * see http://php.net/manual/en/function.rename.php | |
| 293 | - * | |
| 294 | - * @param string $path1 | |
| 295 | - * @param string $path2 | |
| 296 | - * @return bool | |
| 297 | - */ | |
| 298 | -	public function rename($path1, $path2) { | |
| 299 | - $this->init(); | |
| 300 | - $isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part'; | |
| 301 | - $targetExists = $this->file_exists($path2); | |
| 302 | - $sameFodler = dirname($path1) === dirname($path2); | |
| 303 | - | |
| 304 | -		if ($targetExists || ($sameFodler && !$isPartFile)) { | |
| 305 | -			if (!$this->isUpdatable('')) { | |
| 306 | - return false; | |
| 307 | - } | |
| 308 | -		} else { | |
| 309 | -			if (!$this->isCreatable('')) { | |
| 310 | - return false; | |
| 311 | - } | |
| 312 | - } | |
| 313 | - | |
| 314 | - return $this->nonMaskedStorage->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2)); | |
| 315 | - } | |
| 316 | - | |
| 317 | - /** | |
| 318 | - * return mount point of share, relative to data/user/files | |
| 319 | - * | |
| 320 | - * @return string | |
| 321 | - */ | |
| 322 | -	public function getMountPoint() { | |
| 323 | - return $this->superShare->getTarget(); | |
| 324 | - } | |
| 325 | - | |
| 326 | - /** | |
| 327 | - * @param string $path | |
| 328 | - */ | |
| 329 | -	public function setMountPoint($path) { | |
| 330 | - $this->superShare->setTarget($path); | |
| 331 | - | |
| 332 | -		foreach ($this->groupedShares as $share) { | |
| 333 | - $share->setTarget($path); | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - /** | |
| 338 | - * get the user who shared the file | |
| 339 | - * | |
| 340 | - * @return string | |
| 341 | - */ | |
| 342 | -	public function getSharedFrom() { | |
| 343 | - return $this->superShare->getShareOwner(); | |
| 344 | - } | |
| 345 | - | |
| 346 | - /** | |
| 347 | - * @return \OCP\Share\IShare | |
| 348 | - */ | |
| 349 | -	public function getShare() { | |
| 350 | - return $this->superShare; | |
| 351 | - } | |
| 352 | - | |
| 353 | - /** | |
| 354 | - * return share type, can be "file" or "folder" | |
| 355 | - * | |
| 356 | - * @return string | |
| 357 | - */ | |
| 358 | -	public function getItemType() { | |
| 359 | - return $this->superShare->getNodeType(); | |
| 360 | - } | |
| 361 | - | |
| 362 | - /** | |
| 363 | - * @param string $path | |
| 364 | - * @param null $storage | |
| 365 | - * @return Cache | |
| 366 | - */ | |
| 367 | -	public function getCache($path = '', $storage = null) { | |
| 368 | -		if ($this->cache) { | |
| 369 | - return $this->cache; | |
| 370 | - } | |
| 371 | -		if (!$storage) { | |
| 372 | - $storage = $this; | |
| 373 | - } | |
| 374 | -		if ($this->storage instanceof FailedStorage) { | |
| 375 | - return new FailedCache(); | |
| 376 | - } | |
| 377 | - $this->cache = new \OCA\Files_Sharing\Cache($storage, $this->getSourceRootInfo(), $this->superShare); | |
| 378 | - return $this->cache; | |
| 379 | - } | |
| 380 | - | |
| 381 | -	public function getScanner($path = '', $storage = null) { | |
| 382 | -		if (!$storage) { | |
| 383 | - $storage = $this; | |
| 384 | - } | |
| 385 | - return new \OCA\Files_Sharing\Scanner($storage); | |
| 386 | - } | |
| 387 | - | |
| 388 | -	public function getOwner($path) { | |
| 389 | - return $this->superShare->getShareOwner(); | |
| 390 | - } | |
| 391 | - | |
| 392 | - /** | |
| 393 | - * unshare complete storage, also the grouped shares | |
| 394 | - * | |
| 395 | - * @return bool | |
| 396 | - */ | |
| 397 | -	public function unshareStorage() { | |
| 398 | -		foreach ($this->groupedShares as $share) { | |
| 399 | - \OC::$server->getShareManager()->deleteFromSelf($share, $this->user); | |
| 400 | - } | |
| 401 | - return true; | |
| 402 | - } | |
| 403 | - | |
| 404 | - /** | |
| 405 | - * @param string $path | |
| 406 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 407 | - * @param \OCP\Lock\ILockingProvider $provider | |
| 408 | - * @throws \OCP\Lock\LockedException | |
| 409 | - */ | |
| 410 | -	public function acquireLock($path, $type, ILockingProvider $provider) { | |
| 411 | - /** @var \OCP\Files\Storage $targetStorage */ | |
| 412 | - list($targetStorage, $targetInternalPath) = $this->resolvePath($path); | |
| 413 | - $targetStorage->acquireLock($targetInternalPath, $type, $provider); | |
| 414 | - // lock the parent folders of the owner when locking the share as recipient | |
| 415 | -		if ($path === '') { | |
| 416 | - $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); | |
| 417 | - $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); | |
| 418 | - } | |
| 419 | - } | |
| 420 | - | |
| 421 | - /** | |
| 422 | - * @param string $path | |
| 423 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 424 | - * @param \OCP\Lock\ILockingProvider $provider | |
| 425 | - */ | |
| 426 | -	public function releaseLock($path, $type, ILockingProvider $provider) { | |
| 427 | - /** @var \OCP\Files\Storage $targetStorage */ | |
| 428 | - list($targetStorage, $targetInternalPath) = $this->resolvePath($path); | |
| 429 | - $targetStorage->releaseLock($targetInternalPath, $type, $provider); | |
| 430 | - // unlock the parent folders of the owner when unlocking the share as recipient | |
| 431 | -		if ($path === '') { | |
| 432 | - $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); | |
| 433 | - $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); | |
| 434 | - } | |
| 435 | - } | |
| 436 | - | |
| 437 | - /** | |
| 438 | - * @param string $path | |
| 439 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 440 | - * @param \OCP\Lock\ILockingProvider $provider | |
| 441 | - */ | |
| 442 | -	public function changeLock($path, $type, ILockingProvider $provider) { | |
| 443 | - /** @var \OCP\Files\Storage $targetStorage */ | |
| 444 | - list($targetStorage, $targetInternalPath) = $this->resolvePath($path); | |
| 445 | - $targetStorage->changeLock($targetInternalPath, $type, $provider); | |
| 446 | - } | |
| 447 | - | |
| 448 | - /** | |
| 449 | - * @return array [ available, last_checked ] | |
| 450 | - */ | |
| 451 | -	public function getAvailability() { | |
| 452 | - // shares do not participate in availability logic | |
| 453 | - return [ | |
| 454 | - 'available' => true, | |
| 455 | - 'last_checked' => 0 | |
| 456 | - ]; | |
| 457 | - } | |
| 458 | - | |
| 459 | - /** | |
| 460 | - * @param bool $available | |
| 461 | - */ | |
| 462 | -	public function setAvailability($available) { | |
| 463 | - // shares do not participate in availability logic | |
| 464 | - } | |
| 465 | - | |
| 466 | -	public function getSourceStorage() { | |
| 467 | - $this->init(); | |
| 468 | - return $this->nonMaskedStorage; | |
| 469 | - } | |
| 470 | - | |
| 471 | -	public function getWrapperStorage() { | |
| 472 | - $this->init(); | |
| 473 | - return $this->storage; | |
| 474 | - } | |
| 475 | - | |
| 476 | -	public function file_get_contents($path) { | |
| 477 | - $info = [ | |
| 478 | - 'target' => $this->getMountPoint() . '/' . $path, | |
| 479 | - 'source' => $this->getUnjailedPath($path), | |
| 480 | - ]; | |
| 481 | -		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info); | |
| 482 | - return parent::file_get_contents($path); | |
| 483 | - } | |
| 484 | - | |
| 485 | -	public function file_put_contents($path, $data) { | |
| 486 | - $info = [ | |
| 487 | - 'target' => $this->getMountPoint() . '/' . $path, | |
| 488 | - 'source' => $this->getUnjailedPath($path), | |
| 489 | - ]; | |
| 490 | -		\OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info); | |
| 491 | - return parent::file_put_contents($path, $data); | |
| 492 | - } | |
| 493 | - | |
| 494 | -	public function setMountOptions(array $options) { | |
| 495 | - $this->mountOptions = $options; | |
| 496 | - } | |
| 51 | + /** @var \OCP\Share\IShare */ | |
| 52 | + private $superShare; | |
| 53 | + | |
| 54 | + /** @var \OCP\Share\IShare[] */ | |
| 55 | + private $groupedShares; | |
| 56 | + | |
| 57 | + /** | |
| 58 | + * @var \OC\Files\View | |
| 59 | + */ | |
| 60 | + private $ownerView; | |
| 61 | + | |
| 62 | + private $initialized = false; | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * @var ICacheEntry | |
| 66 | + */ | |
| 67 | + private $sourceRootInfo; | |
| 68 | + | |
| 69 | + /** @var string */ | |
| 70 | + private $user; | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * @var \OCP\ILogger | |
| 74 | + */ | |
| 75 | + private $logger; | |
| 76 | + | |
| 77 | + /** @var IStorage */ | |
| 78 | + private $nonMaskedStorage; | |
| 79 | + | |
| 80 | + private $options; | |
| 81 | + | |
| 82 | + /** @var boolean */ | |
| 83 | + private $sharingDisabledForUser; | |
| 84 | + | |
| 85 | +    public function __construct($arguments) { | |
| 86 | + $this->ownerView = $arguments['ownerView']; | |
| 87 | + $this->logger = \OC::$server->getLogger(); | |
| 88 | + | |
| 89 | + $this->superShare = $arguments['superShare']; | |
| 90 | + $this->groupedShares = $arguments['groupedShares']; | |
| 91 | + | |
| 92 | + $this->user = $arguments['user']; | |
| 93 | +        if (isset($arguments['sharingDisabledForUser'])) { | |
| 94 | + $this->sharingDisabledForUser = $arguments['sharingDisabledForUser']; | |
| 95 | +        } else { | |
| 96 | + $this->sharingDisabledForUser = false; | |
| 97 | + } | |
| 98 | + | |
| 99 | + parent::__construct([ | |
| 100 | + 'storage' => null, | |
| 101 | + 'root' => null, | |
| 102 | + ]); | |
| 103 | + } | |
| 104 | + | |
| 105 | + /** | |
| 106 | + * @return ICacheEntry | |
| 107 | + */ | |
| 108 | +    private function getSourceRootInfo() { | |
| 109 | +        if (is_null($this->sourceRootInfo)) { | |
| 110 | +            if (is_null($this->superShare->getNodeCacheEntry())) { | |
| 111 | + $this->init(); | |
| 112 | + $this->sourceRootInfo = $this->nonMaskedStorage->getCache()->get($this->rootPath); | |
| 113 | +            } else { | |
| 114 | + $this->sourceRootInfo = $this->superShare->getNodeCacheEntry(); | |
| 115 | + } | |
| 116 | + } | |
| 117 | + return $this->sourceRootInfo; | |
| 118 | + } | |
| 119 | + | |
| 120 | +    private function init() { | |
| 121 | +        if ($this->initialized) { | |
| 122 | + return; | |
| 123 | + } | |
| 124 | + $this->initialized = true; | |
| 125 | +        try { | |
| 126 | + Filesystem::initMountPoints($this->superShare->getShareOwner()); | |
| 127 | + $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); | |
| 128 | + list($this->nonMaskedStorage, $this->rootPath) = $this->ownerView->resolvePath($sourcePath); | |
| 129 | + $this->storage = new PermissionsMask([ | |
| 130 | + 'storage' => $this->nonMaskedStorage, | |
| 131 | + 'mask' => $this->superShare->getPermissions() | |
| 132 | + ]); | |
| 133 | +        } catch (NotFoundException $e) { | |
| 134 | + // original file not accessible or deleted, set FailedStorage | |
| 135 | + $this->storage = new FailedStorage(['exception' => $e]); | |
| 136 | + $this->cache = new FailedCache(); | |
| 137 | + $this->rootPath = ''; | |
| 138 | +        } catch (NoUserException $e) { | |
| 139 | + // sharer user deleted, set FailedStorage | |
| 140 | + $this->storage = new FailedStorage(['exception' => $e]); | |
| 141 | + $this->cache = new FailedCache(); | |
| 142 | + $this->rootPath = ''; | |
| 143 | +        } catch (\Exception $e) { | |
| 144 | + $this->storage = new FailedStorage(['exception' => $e]); | |
| 145 | + $this->cache = new FailedCache(); | |
| 146 | + $this->rootPath = ''; | |
| 147 | + $this->logger->logException($e); | |
| 148 | + } | |
| 149 | + | |
| 150 | +        if (!$this->nonMaskedStorage) { | |
| 151 | + $this->nonMaskedStorage = $this->storage; | |
| 152 | + } | |
| 153 | + } | |
| 154 | + | |
| 155 | + /** | |
| 156 | + * @inheritdoc | |
| 157 | + */ | |
| 158 | +    public function instanceOfStorage($class) { | |
| 159 | +        if ($class === '\OC\Files\Storage\Common') { | |
| 160 | + return true; | |
| 161 | + } | |
| 162 | +        if (in_array($class, ['\OC\Files\Storage\Home', '\OC\Files\ObjectStore\HomeObjectStoreStorage'])) { | |
| 163 | + return false; | |
| 164 | + } | |
| 165 | + return parent::instanceOfStorage($class); | |
| 166 | + } | |
| 167 | + | |
| 168 | + /** | |
| 169 | + * @return string | |
| 170 | + */ | |
| 171 | +    public function getShareId() { | |
| 172 | + return $this->superShare->getId(); | |
| 173 | + } | |
| 174 | + | |
| 175 | +    private function isValid() { | |
| 176 | + return $this->getSourceRootInfo() && ($this->getSourceRootInfo()->getPermissions() & Constants::PERMISSION_SHARE) === Constants::PERMISSION_SHARE; | |
| 177 | + } | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * get id of the mount point | |
| 181 | + * | |
| 182 | + * @return string | |
| 183 | + */ | |
| 184 | +    public function getId() { | |
| 185 | + return 'shared::' . $this->getMountPoint(); | |
| 186 | + } | |
| 187 | + | |
| 188 | + /** | |
| 189 | + * Get the permissions granted for a shared file | |
| 190 | + * | |
| 191 | + * @param string $target Shared target file path | |
| 192 | + * @return int CRUDS permissions granted | |
| 193 | + */ | |
| 194 | +    public function getPermissions($target = '') { | |
| 195 | +        if (!$this->isValid()) { | |
| 196 | + return 0; | |
| 197 | + } | |
| 198 | + $permissions = $this->superShare->getPermissions(); | |
| 199 | + // part files and the mount point always have delete permissions | |
| 200 | +        if ($target === '' || pathinfo($target, PATHINFO_EXTENSION) === 'part') { | |
| 201 | + $permissions |= \OCP\Constants::PERMISSION_DELETE; | |
| 202 | + } | |
| 203 | + | |
| 204 | +        if ($this->sharingDisabledForUser) { | |
| 205 | + $permissions &= ~\OCP\Constants::PERMISSION_SHARE; | |
| 206 | + } | |
| 207 | + | |
| 208 | + return $permissions; | |
| 209 | + } | |
| 210 | + | |
| 211 | +    public function isCreatable($path) { | |
| 212 | + return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_CREATE); | |
| 213 | + } | |
| 214 | + | |
| 215 | +    public function isReadable($path) { | |
| 216 | +        if (!$this->isValid()) { | |
| 217 | + return false; | |
| 218 | + } | |
| 219 | +        if (!$this->file_exists($path)) { | |
| 220 | + return false; | |
| 221 | + } | |
| 222 | + /** @var IStorage $storage */ | |
| 223 | + /** @var string $internalPath */ | |
| 224 | + list($storage, $internalPath) = $this->resolvePath($path); | |
| 225 | + return $storage->isReadable($internalPath); | |
| 226 | + } | |
| 227 | + | |
| 228 | +    public function isUpdatable($path) { | |
| 229 | + return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_UPDATE); | |
| 230 | + } | |
| 231 | + | |
| 232 | +    public function isDeletable($path) { | |
| 233 | + return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_DELETE); | |
| 234 | + } | |
| 235 | + | |
| 236 | +    public function isSharable($path) { | |
| 237 | +        if (\OCP\Util::isSharingDisabledForUser() || !\OC\Share\Share::isResharingAllowed()) { | |
| 238 | + return false; | |
| 239 | + } | |
| 240 | + return ($this->getPermissions($path) & \OCP\Constants::PERMISSION_SHARE); | |
| 241 | + } | |
| 242 | + | |
| 243 | +    public function fopen($path, $mode) { | |
| 244 | +        if ($source = $this->getUnjailedPath($path)) { | |
| 245 | +            switch ($mode) { | |
| 246 | + case 'r+': | |
| 247 | + case 'rb+': | |
| 248 | + case 'w+': | |
| 249 | + case 'wb+': | |
| 250 | + case 'x+': | |
| 251 | + case 'xb+': | |
| 252 | + case 'a+': | |
| 253 | + case 'ab+': | |
| 254 | + case 'w': | |
| 255 | + case 'wb': | |
| 256 | + case 'x': | |
| 257 | + case 'xb': | |
| 258 | + case 'a': | |
| 259 | + case 'ab': | |
| 260 | + $creatable = $this->isCreatable($path); | |
| 261 | + $updatable = $this->isUpdatable($path); | |
| 262 | + // if neither permissions given, no need to continue | |
| 263 | +                    if (!$creatable && !$updatable) { | |
| 264 | + return false; | |
| 265 | + } | |
| 266 | + | |
| 267 | + $exists = $this->file_exists($path); | |
| 268 | + // if a file exists, updatable permissions are required | |
| 269 | +                    if ($exists && !$updatable) { | |
| 270 | + return false; | |
| 271 | + } | |
| 272 | + | |
| 273 | + // part file is allowed if !$creatable but the final file is $updatable | |
| 274 | +                    if (pathinfo($path, PATHINFO_EXTENSION) !== 'part') { | |
| 275 | +                        if (!$exists && !$creatable) { | |
| 276 | + return false; | |
| 277 | + } | |
| 278 | + } | |
| 279 | + } | |
| 280 | + $info = array( | |
| 281 | + 'target' => $this->getMountPoint() . $path, | |
| 282 | + 'source' => $source, | |
| 283 | + 'mode' => $mode, | |
| 284 | + ); | |
| 285 | +            \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'fopen', $info); | |
| 286 | + return $this->nonMaskedStorage->fopen($this->getUnjailedPath($path), $mode); | |
| 287 | + } | |
| 288 | + return false; | |
| 289 | + } | |
| 290 | + | |
| 291 | + /** | |
| 292 | + * see http://php.net/manual/en/function.rename.php | |
| 293 | + * | |
| 294 | + * @param string $path1 | |
| 295 | + * @param string $path2 | |
| 296 | + * @return bool | |
| 297 | + */ | |
| 298 | +    public function rename($path1, $path2) { | |
| 299 | + $this->init(); | |
| 300 | + $isPartFile = pathinfo($path1, PATHINFO_EXTENSION) === 'part'; | |
| 301 | + $targetExists = $this->file_exists($path2); | |
| 302 | + $sameFodler = dirname($path1) === dirname($path2); | |
| 303 | + | |
| 304 | +        if ($targetExists || ($sameFodler && !$isPartFile)) { | |
| 305 | +            if (!$this->isUpdatable('')) { | |
| 306 | + return false; | |
| 307 | + } | |
| 308 | +        } else { | |
| 309 | +            if (!$this->isCreatable('')) { | |
| 310 | + return false; | |
| 311 | + } | |
| 312 | + } | |
| 313 | + | |
| 314 | + return $this->nonMaskedStorage->rename($this->getUnjailedPath($path1), $this->getUnjailedPath($path2)); | |
| 315 | + } | |
| 316 | + | |
| 317 | + /** | |
| 318 | + * return mount point of share, relative to data/user/files | |
| 319 | + * | |
| 320 | + * @return string | |
| 321 | + */ | |
| 322 | +    public function getMountPoint() { | |
| 323 | + return $this->superShare->getTarget(); | |
| 324 | + } | |
| 325 | + | |
| 326 | + /** | |
| 327 | + * @param string $path | |
| 328 | + */ | |
| 329 | +    public function setMountPoint($path) { | |
| 330 | + $this->superShare->setTarget($path); | |
| 331 | + | |
| 332 | +        foreach ($this->groupedShares as $share) { | |
| 333 | + $share->setTarget($path); | |
| 334 | + } | |
| 335 | + } | |
| 336 | + | |
| 337 | + /** | |
| 338 | + * get the user who shared the file | |
| 339 | + * | |
| 340 | + * @return string | |
| 341 | + */ | |
| 342 | +    public function getSharedFrom() { | |
| 343 | + return $this->superShare->getShareOwner(); | |
| 344 | + } | |
| 345 | + | |
| 346 | + /** | |
| 347 | + * @return \OCP\Share\IShare | |
| 348 | + */ | |
| 349 | +    public function getShare() { | |
| 350 | + return $this->superShare; | |
| 351 | + } | |
| 352 | + | |
| 353 | + /** | |
| 354 | + * return share type, can be "file" or "folder" | |
| 355 | + * | |
| 356 | + * @return string | |
| 357 | + */ | |
| 358 | +    public function getItemType() { | |
| 359 | + return $this->superShare->getNodeType(); | |
| 360 | + } | |
| 361 | + | |
| 362 | + /** | |
| 363 | + * @param string $path | |
| 364 | + * @param null $storage | |
| 365 | + * @return Cache | |
| 366 | + */ | |
| 367 | +    public function getCache($path = '', $storage = null) { | |
| 368 | +        if ($this->cache) { | |
| 369 | + return $this->cache; | |
| 370 | + } | |
| 371 | +        if (!$storage) { | |
| 372 | + $storage = $this; | |
| 373 | + } | |
| 374 | +        if ($this->storage instanceof FailedStorage) { | |
| 375 | + return new FailedCache(); | |
| 376 | + } | |
| 377 | + $this->cache = new \OCA\Files_Sharing\Cache($storage, $this->getSourceRootInfo(), $this->superShare); | |
| 378 | + return $this->cache; | |
| 379 | + } | |
| 380 | + | |
| 381 | +    public function getScanner($path = '', $storage = null) { | |
| 382 | +        if (!$storage) { | |
| 383 | + $storage = $this; | |
| 384 | + } | |
| 385 | + return new \OCA\Files_Sharing\Scanner($storage); | |
| 386 | + } | |
| 387 | + | |
| 388 | +    public function getOwner($path) { | |
| 389 | + return $this->superShare->getShareOwner(); | |
| 390 | + } | |
| 391 | + | |
| 392 | + /** | |
| 393 | + * unshare complete storage, also the grouped shares | |
| 394 | + * | |
| 395 | + * @return bool | |
| 396 | + */ | |
| 397 | +    public function unshareStorage() { | |
| 398 | +        foreach ($this->groupedShares as $share) { | |
| 399 | + \OC::$server->getShareManager()->deleteFromSelf($share, $this->user); | |
| 400 | + } | |
| 401 | + return true; | |
| 402 | + } | |
| 403 | + | |
| 404 | + /** | |
| 405 | + * @param string $path | |
| 406 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 407 | + * @param \OCP\Lock\ILockingProvider $provider | |
| 408 | + * @throws \OCP\Lock\LockedException | |
| 409 | + */ | |
| 410 | +    public function acquireLock($path, $type, ILockingProvider $provider) { | |
| 411 | + /** @var \OCP\Files\Storage $targetStorage */ | |
| 412 | + list($targetStorage, $targetInternalPath) = $this->resolvePath($path); | |
| 413 | + $targetStorage->acquireLock($targetInternalPath, $type, $provider); | |
| 414 | + // lock the parent folders of the owner when locking the share as recipient | |
| 415 | +        if ($path === '') { | |
| 416 | + $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); | |
| 417 | + $this->ownerView->lockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); | |
| 418 | + } | |
| 419 | + } | |
| 420 | + | |
| 421 | + /** | |
| 422 | + * @param string $path | |
| 423 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 424 | + * @param \OCP\Lock\ILockingProvider $provider | |
| 425 | + */ | |
| 426 | +    public function releaseLock($path, $type, ILockingProvider $provider) { | |
| 427 | + /** @var \OCP\Files\Storage $targetStorage */ | |
| 428 | + list($targetStorage, $targetInternalPath) = $this->resolvePath($path); | |
| 429 | + $targetStorage->releaseLock($targetInternalPath, $type, $provider); | |
| 430 | + // unlock the parent folders of the owner when unlocking the share as recipient | |
| 431 | +        if ($path === '') { | |
| 432 | + $sourcePath = $this->ownerView->getPath($this->superShare->getNodeId()); | |
| 433 | + $this->ownerView->unlockFile(dirname($sourcePath), ILockingProvider::LOCK_SHARED, true); | |
| 434 | + } | |
| 435 | + } | |
| 436 | + | |
| 437 | + /** | |
| 438 | + * @param string $path | |
| 439 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 440 | + * @param \OCP\Lock\ILockingProvider $provider | |
| 441 | + */ | |
| 442 | +    public function changeLock($path, $type, ILockingProvider $provider) { | |
| 443 | + /** @var \OCP\Files\Storage $targetStorage */ | |
| 444 | + list($targetStorage, $targetInternalPath) = $this->resolvePath($path); | |
| 445 | + $targetStorage->changeLock($targetInternalPath, $type, $provider); | |
| 446 | + } | |
| 447 | + | |
| 448 | + /** | |
| 449 | + * @return array [ available, last_checked ] | |
| 450 | + */ | |
| 451 | +    public function getAvailability() { | |
| 452 | + // shares do not participate in availability logic | |
| 453 | + return [ | |
| 454 | + 'available' => true, | |
| 455 | + 'last_checked' => 0 | |
| 456 | + ]; | |
| 457 | + } | |
| 458 | + | |
| 459 | + /** | |
| 460 | + * @param bool $available | |
| 461 | + */ | |
| 462 | +    public function setAvailability($available) { | |
| 463 | + // shares do not participate in availability logic | |
| 464 | + } | |
| 465 | + | |
| 466 | +    public function getSourceStorage() { | |
| 467 | + $this->init(); | |
| 468 | + return $this->nonMaskedStorage; | |
| 469 | + } | |
| 470 | + | |
| 471 | +    public function getWrapperStorage() { | |
| 472 | + $this->init(); | |
| 473 | + return $this->storage; | |
| 474 | + } | |
| 475 | + | |
| 476 | +    public function file_get_contents($path) { | |
| 477 | + $info = [ | |
| 478 | + 'target' => $this->getMountPoint() . '/' . $path, | |
| 479 | + 'source' => $this->getUnjailedPath($path), | |
| 480 | + ]; | |
| 481 | +        \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_get_contents', $info); | |
| 482 | + return parent::file_get_contents($path); | |
| 483 | + } | |
| 484 | + | |
| 485 | +    public function file_put_contents($path, $data) { | |
| 486 | + $info = [ | |
| 487 | + 'target' => $this->getMountPoint() . '/' . $path, | |
| 488 | + 'source' => $this->getUnjailedPath($path), | |
| 489 | + ]; | |
| 490 | +        \OCP\Util::emitHook('\OC\Files\Storage\Shared', 'file_put_contents', $info); | |
| 491 | + return parent::file_put_contents($path, $data); | |
| 492 | + } | |
| 493 | + | |
| 494 | +    public function setMountOptions(array $options) { | |
| 495 | + $this->mountOptions = $options; | |
| 496 | + } | |
| 497 | 497 | } | 
| @@ -38,228 +38,228 @@ | ||
| 38 | 38 | * Shared mount points can be moved by the user | 
| 39 | 39 | */ | 
| 40 | 40 |  class SharedMount extends MountPoint implements MoveableMount { | 
| 41 | - /** | |
| 42 | - * @var \OCA\Files_Sharing\SharedStorage $storage | |
| 43 | - */ | |
| 44 | - protected $storage = null; | |
| 45 | - | |
| 46 | - /** | |
| 47 | - * @var \OC\Files\View | |
| 48 | - */ | |
| 49 | - private $recipientView; | |
| 50 | - | |
| 51 | - /** | |
| 52 | - * @var string | |
| 53 | - */ | |
| 54 | - private $user; | |
| 55 | - | |
| 56 | - /** @var \OCP\Share\IShare */ | |
| 57 | - private $superShare; | |
| 58 | - | |
| 59 | - /** @var \OCP\Share\IShare[] */ | |
| 60 | - private $groupedShares; | |
| 61 | - | |
| 62 | - /** | |
| 63 | - * @param string $storage | |
| 64 | - * @param SharedMount[] $mountpoints | |
| 65 | - * @param array $arguments | |
| 66 | - * @param IStorageFactory $loader | |
| 67 | - * @param View $recipientView | |
| 68 | - */ | |
| 69 | -	public function __construct($storage, array $mountpoints, $arguments, IStorageFactory $loader, View $recipientView, CappedMemoryCache $folderExistCache) { | |
| 70 | - $this->user = $arguments['user']; | |
| 71 | - $this->recipientView = $recipientView; | |
| 72 | - | |
| 73 | - $this->superShare = $arguments['superShare']; | |
| 74 | - $this->groupedShares = $arguments['groupedShares']; | |
| 75 | - | |
| 76 | - $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache); | |
| 77 | - $absMountPoint = '/' . $this->user . '/files' . $newMountPoint; | |
| 78 | - parent::__construct($storage, $absMountPoint, $arguments, $loader); | |
| 79 | - } | |
| 80 | - | |
| 81 | - /** | |
| 82 | - * check if the parent folder exists otherwise move the mount point up | |
| 83 | - * | |
| 84 | - * @param \OCP\Share\IShare $share | |
| 85 | - * @param SharedMount[] $mountpoints | |
| 86 | - * @return string | |
| 87 | - */ | |
| 88 | -	private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints, CappedMemoryCache $folderExistCache) { | |
| 89 | - | |
| 90 | - $mountPoint = basename($share->getTarget()); | |
| 91 | - $parent = dirname($share->getTarget()); | |
| 92 | - | |
| 93 | -		if ($folderExistCache->hasKey($parent)) { | |
| 94 | - $parentExists = $folderExistCache->get($parent); | |
| 95 | -		} else { | |
| 96 | - $parentExists = $this->recipientView->is_dir($parent); | |
| 97 | - $folderExistCache->set($parent, $parentExists); | |
| 98 | - } | |
| 99 | -		if (!$parentExists) { | |
| 100 | - $parent = Helper::getShareFolder($this->recipientView); | |
| 101 | - } | |
| 102 | - | |
| 103 | - $newMountPoint = $this->generateUniqueTarget( | |
| 104 | - \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), | |
| 105 | - $this->recipientView, | |
| 106 | - $mountpoints | |
| 107 | - ); | |
| 108 | - | |
| 109 | -		if ($newMountPoint !== $share->getTarget()) { | |
| 110 | - $this->updateFileTarget($newMountPoint, $share); | |
| 111 | - } | |
| 112 | - | |
| 113 | - return $newMountPoint; | |
| 114 | - } | |
| 115 | - | |
| 116 | - /** | |
| 117 | - * update fileTarget in the database if the mount point changed | |
| 118 | - * | |
| 119 | - * @param string $newPath | |
| 120 | - * @param \OCP\Share\IShare $share | |
| 121 | - * @return bool | |
| 122 | - */ | |
| 123 | -	private function updateFileTarget($newPath, &$share) { | |
| 124 | - $share->setTarget($newPath); | |
| 125 | - | |
| 126 | -		foreach ($this->groupedShares as $tmpShare) { | |
| 127 | - $tmpShare->setTarget($newPath); | |
| 128 | - \OC::$server->getShareManager()->moveShare($tmpShare, $this->user); | |
| 129 | - } | |
| 130 | - } | |
| 131 | - | |
| 132 | - | |
| 133 | - /** | |
| 134 | - * @param string $path | |
| 135 | - * @param View $view | |
| 136 | - * @param SharedMount[] $mountpoints | |
| 137 | - * @return mixed | |
| 138 | - */ | |
| 139 | -	private function generateUniqueTarget($path, $view, array $mountpoints) { | |
| 140 | - $pathinfo = pathinfo($path); | |
| 141 | - $ext = (isset($pathinfo['extension'])) ? '.' . $pathinfo['extension'] : ''; | |
| 142 | - $name = $pathinfo['filename']; | |
| 143 | - $dir = $pathinfo['dirname']; | |
| 144 | - | |
| 145 | - $i = 2; | |
| 146 | - $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; | |
| 147 | -		while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) { | |
| 148 | -			$path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext); | |
| 149 | - $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; | |
| 150 | - $i++; | |
| 151 | - } | |
| 152 | - | |
| 153 | - return $path; | |
| 154 | - } | |
| 155 | - | |
| 156 | - /** | |
| 157 | - * Format a path to be relative to the /user/files/ directory | |
| 158 | - * | |
| 159 | - * @param string $path the absolute path | |
| 160 | - * @return string e.g. turns '/admin/files/test.txt' into '/test.txt' | |
| 161 | - * @throws \OCA\Files_Sharing\Exceptions\BrokenPath | |
| 162 | - */ | |
| 163 | -	protected function stripUserFilesPath($path) { | |
| 164 | - $trimmed = ltrim($path, '/'); | |
| 165 | -		$split = explode('/', $trimmed); | |
| 166 | - | |
| 167 | - // it is not a file relative to data/user/files | |
| 168 | -		if (count($split) < 3 || $split[1] !== 'files') { | |
| 169 | -			\OCP\Util::writeLog('file sharing', | |
| 170 | - 'Can not strip userid and "files/" from path: ' . $path, | |
| 171 | - \OCP\Util::ERROR); | |
| 172 | -			throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10); | |
| 173 | - } | |
| 174 | - | |
| 175 | - // skip 'user' and 'files' | |
| 176 | - $sliced = array_slice($split, 2); | |
| 177 | -		$relPath = implode('/', $sliced); | |
| 178 | - | |
| 179 | - return '/' . $relPath; | |
| 180 | - } | |
| 181 | - | |
| 182 | - /** | |
| 183 | - * Move the mount point to $target | |
| 184 | - * | |
| 185 | - * @param string $target the target mount point | |
| 186 | - * @return bool | |
| 187 | - */ | |
| 188 | -	public function moveMount($target) { | |
| 189 | - | |
| 190 | - $relTargetPath = $this->stripUserFilesPath($target); | |
| 191 | - $share = $this->storage->getShare(); | |
| 192 | - | |
| 193 | - $result = true; | |
| 194 | - | |
| 195 | -		try { | |
| 196 | - $this->updateFileTarget($relTargetPath, $share); | |
| 197 | - $this->setMountPoint($target); | |
| 198 | - $this->storage->setMountPoint($relTargetPath); | |
| 199 | -		} catch (\Exception $e) { | |
| 200 | -			\OCP\Util::writeLog('file sharing', | |
| 201 | - 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"', | |
| 202 | - \OCP\Util::ERROR); | |
| 203 | - } | |
| 204 | - | |
| 205 | - return $result; | |
| 206 | - } | |
| 207 | - | |
| 208 | - /** | |
| 209 | - * Remove the mount points | |
| 210 | - * | |
| 211 | - * @return bool | |
| 212 | - */ | |
| 213 | -	public function removeMount() { | |
| 214 | - $mountManager = \OC\Files\Filesystem::getMountManager(); | |
| 215 | - /** @var $storage \OCA\Files_Sharing\SharedStorage */ | |
| 216 | - $storage = $this->getStorage(); | |
| 217 | - $result = $storage->unshareStorage(); | |
| 218 | - $mountManager->removeMount($this->mountPoint); | |
| 219 | - | |
| 220 | - return $result; | |
| 221 | - } | |
| 222 | - | |
| 223 | - /** | |
| 224 | - * @return \OCP\Share\IShare | |
| 225 | - */ | |
| 226 | -	public function getShare() { | |
| 227 | - return $this->superShare; | |
| 228 | - } | |
| 229 | - | |
| 230 | - /** | |
| 231 | - * Get the file id of the root of the storage | |
| 232 | - * | |
| 233 | - * @return int | |
| 234 | - */ | |
| 235 | -	public function getStorageRootId() { | |
| 236 | - return $this->getShare()->getNodeId(); | |
| 237 | - } | |
| 238 | - | |
| 239 | - /** | |
| 240 | - * @return int | |
| 241 | - */ | |
| 242 | -	public function getNumericStorageId() { | |
| 243 | -		if (!is_null($this->getShare()->getNodeCacheEntry())) { | |
| 244 | - return $this->getShare()->getNodeCacheEntry()->getStorageId(); | |
| 245 | -		} else { | |
| 246 | - $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); | |
| 247 | - | |
| 248 | -			$query = $builder->select('storage') | |
| 249 | -				->from('filecache') | |
| 250 | -				->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId()))); | |
| 251 | - | |
| 252 | - $result = $query->execute(); | |
| 253 | - $row = $result->fetch(); | |
| 254 | - $result->closeCursor(); | |
| 255 | -			if ($row) { | |
| 256 | - return (int)$row['storage']; | |
| 257 | - } | |
| 258 | - return -1; | |
| 259 | - } | |
| 260 | - } | |
| 261 | - | |
| 262 | -	public function getMountType() { | |
| 263 | - return 'shared'; | |
| 264 | - } | |
| 41 | + /** | |
| 42 | + * @var \OCA\Files_Sharing\SharedStorage $storage | |
| 43 | + */ | |
| 44 | + protected $storage = null; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * @var \OC\Files\View | |
| 48 | + */ | |
| 49 | + private $recipientView; | |
| 50 | + | |
| 51 | + /** | |
| 52 | + * @var string | |
| 53 | + */ | |
| 54 | + private $user; | |
| 55 | + | |
| 56 | + /** @var \OCP\Share\IShare */ | |
| 57 | + private $superShare; | |
| 58 | + | |
| 59 | + /** @var \OCP\Share\IShare[] */ | |
| 60 | + private $groupedShares; | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * @param string $storage | |
| 64 | + * @param SharedMount[] $mountpoints | |
| 65 | + * @param array $arguments | |
| 66 | + * @param IStorageFactory $loader | |
| 67 | + * @param View $recipientView | |
| 68 | + */ | |
| 69 | +    public function __construct($storage, array $mountpoints, $arguments, IStorageFactory $loader, View $recipientView, CappedMemoryCache $folderExistCache) { | |
| 70 | + $this->user = $arguments['user']; | |
| 71 | + $this->recipientView = $recipientView; | |
| 72 | + | |
| 73 | + $this->superShare = $arguments['superShare']; | |
| 74 | + $this->groupedShares = $arguments['groupedShares']; | |
| 75 | + | |
| 76 | + $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache); | |
| 77 | + $absMountPoint = '/' . $this->user . '/files' . $newMountPoint; | |
| 78 | + parent::__construct($storage, $absMountPoint, $arguments, $loader); | |
| 79 | + } | |
| 80 | + | |
| 81 | + /** | |
| 82 | + * check if the parent folder exists otherwise move the mount point up | |
| 83 | + * | |
| 84 | + * @param \OCP\Share\IShare $share | |
| 85 | + * @param SharedMount[] $mountpoints | |
| 86 | + * @return string | |
| 87 | + */ | |
| 88 | +    private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints, CappedMemoryCache $folderExistCache) { | |
| 89 | + | |
| 90 | + $mountPoint = basename($share->getTarget()); | |
| 91 | + $parent = dirname($share->getTarget()); | |
| 92 | + | |
| 93 | +        if ($folderExistCache->hasKey($parent)) { | |
| 94 | + $parentExists = $folderExistCache->get($parent); | |
| 95 | +        } else { | |
| 96 | + $parentExists = $this->recipientView->is_dir($parent); | |
| 97 | + $folderExistCache->set($parent, $parentExists); | |
| 98 | + } | |
| 99 | +        if (!$parentExists) { | |
| 100 | + $parent = Helper::getShareFolder($this->recipientView); | |
| 101 | + } | |
| 102 | + | |
| 103 | + $newMountPoint = $this->generateUniqueTarget( | |
| 104 | + \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), | |
| 105 | + $this->recipientView, | |
| 106 | + $mountpoints | |
| 107 | + ); | |
| 108 | + | |
| 109 | +        if ($newMountPoint !== $share->getTarget()) { | |
| 110 | + $this->updateFileTarget($newMountPoint, $share); | |
| 111 | + } | |
| 112 | + | |
| 113 | + return $newMountPoint; | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * update fileTarget in the database if the mount point changed | |
| 118 | + * | |
| 119 | + * @param string $newPath | |
| 120 | + * @param \OCP\Share\IShare $share | |
| 121 | + * @return bool | |
| 122 | + */ | |
| 123 | +    private function updateFileTarget($newPath, &$share) { | |
| 124 | + $share->setTarget($newPath); | |
| 125 | + | |
| 126 | +        foreach ($this->groupedShares as $tmpShare) { | |
| 127 | + $tmpShare->setTarget($newPath); | |
| 128 | + \OC::$server->getShareManager()->moveShare($tmpShare, $this->user); | |
| 129 | + } | |
| 130 | + } | |
| 131 | + | |
| 132 | + | |
| 133 | + /** | |
| 134 | + * @param string $path | |
| 135 | + * @param View $view | |
| 136 | + * @param SharedMount[] $mountpoints | |
| 137 | + * @return mixed | |
| 138 | + */ | |
| 139 | +    private function generateUniqueTarget($path, $view, array $mountpoints) { | |
| 140 | + $pathinfo = pathinfo($path); | |
| 141 | + $ext = (isset($pathinfo['extension'])) ? '.' . $pathinfo['extension'] : ''; | |
| 142 | + $name = $pathinfo['filename']; | |
| 143 | + $dir = $pathinfo['dirname']; | |
| 144 | + | |
| 145 | + $i = 2; | |
| 146 | + $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; | |
| 147 | +        while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) { | |
| 148 | +            $path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext); | |
| 149 | + $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; | |
| 150 | + $i++; | |
| 151 | + } | |
| 152 | + | |
| 153 | + return $path; | |
| 154 | + } | |
| 155 | + | |
| 156 | + /** | |
| 157 | + * Format a path to be relative to the /user/files/ directory | |
| 158 | + * | |
| 159 | + * @param string $path the absolute path | |
| 160 | + * @return string e.g. turns '/admin/files/test.txt' into '/test.txt' | |
| 161 | + * @throws \OCA\Files_Sharing\Exceptions\BrokenPath | |
| 162 | + */ | |
| 163 | +    protected function stripUserFilesPath($path) { | |
| 164 | + $trimmed = ltrim($path, '/'); | |
| 165 | +        $split = explode('/', $trimmed); | |
| 166 | + | |
| 167 | + // it is not a file relative to data/user/files | |
| 168 | +        if (count($split) < 3 || $split[1] !== 'files') { | |
| 169 | +            \OCP\Util::writeLog('file sharing', | |
| 170 | + 'Can not strip userid and "files/" from path: ' . $path, | |
| 171 | + \OCP\Util::ERROR); | |
| 172 | +            throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10); | |
| 173 | + } | |
| 174 | + | |
| 175 | + // skip 'user' and 'files' | |
| 176 | + $sliced = array_slice($split, 2); | |
| 177 | +        $relPath = implode('/', $sliced); | |
| 178 | + | |
| 179 | + return '/' . $relPath; | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Move the mount point to $target | |
| 184 | + * | |
| 185 | + * @param string $target the target mount point | |
| 186 | + * @return bool | |
| 187 | + */ | |
| 188 | +    public function moveMount($target) { | |
| 189 | + | |
| 190 | + $relTargetPath = $this->stripUserFilesPath($target); | |
| 191 | + $share = $this->storage->getShare(); | |
| 192 | + | |
| 193 | + $result = true; | |
| 194 | + | |
| 195 | +        try { | |
| 196 | + $this->updateFileTarget($relTargetPath, $share); | |
| 197 | + $this->setMountPoint($target); | |
| 198 | + $this->storage->setMountPoint($relTargetPath); | |
| 199 | +        } catch (\Exception $e) { | |
| 200 | +            \OCP\Util::writeLog('file sharing', | |
| 201 | + 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"', | |
| 202 | + \OCP\Util::ERROR); | |
| 203 | + } | |
| 204 | + | |
| 205 | + return $result; | |
| 206 | + } | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * Remove the mount points | |
| 210 | + * | |
| 211 | + * @return bool | |
| 212 | + */ | |
| 213 | +    public function removeMount() { | |
| 214 | + $mountManager = \OC\Files\Filesystem::getMountManager(); | |
| 215 | + /** @var $storage \OCA\Files_Sharing\SharedStorage */ | |
| 216 | + $storage = $this->getStorage(); | |
| 217 | + $result = $storage->unshareStorage(); | |
| 218 | + $mountManager->removeMount($this->mountPoint); | |
| 219 | + | |
| 220 | + return $result; | |
| 221 | + } | |
| 222 | + | |
| 223 | + /** | |
| 224 | + * @return \OCP\Share\IShare | |
| 225 | + */ | |
| 226 | +    public function getShare() { | |
| 227 | + return $this->superShare; | |
| 228 | + } | |
| 229 | + | |
| 230 | + /** | |
| 231 | + * Get the file id of the root of the storage | |
| 232 | + * | |
| 233 | + * @return int | |
| 234 | + */ | |
| 235 | +    public function getStorageRootId() { | |
| 236 | + return $this->getShare()->getNodeId(); | |
| 237 | + } | |
| 238 | + | |
| 239 | + /** | |
| 240 | + * @return int | |
| 241 | + */ | |
| 242 | +    public function getNumericStorageId() { | |
| 243 | +        if (!is_null($this->getShare()->getNodeCacheEntry())) { | |
| 244 | + return $this->getShare()->getNodeCacheEntry()->getStorageId(); | |
| 245 | +        } else { | |
| 246 | + $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); | |
| 247 | + | |
| 248 | +            $query = $builder->select('storage') | |
| 249 | +                ->from('filecache') | |
| 250 | +                ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId()))); | |
| 251 | + | |
| 252 | + $result = $query->execute(); | |
| 253 | + $row = $result->fetch(); | |
| 254 | + $result->closeCursor(); | |
| 255 | +            if ($row) { | |
| 256 | + return (int)$row['storage']; | |
| 257 | + } | |
| 258 | + return -1; | |
| 259 | + } | |
| 260 | + } | |
| 261 | + | |
| 262 | +    public function getMountType() { | |
| 263 | + return 'shared'; | |
| 264 | + } | |
| 265 | 265 | } | 
| @@ -74,7 +74,7 @@ discard block | ||
| 74 | 74 | $this->groupedShares = $arguments['groupedShares']; | 
| 75 | 75 | |
| 76 | 76 | $newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache); | 
| 77 | - $absMountPoint = '/' . $this->user . '/files' . $newMountPoint; | |
| 77 | + $absMountPoint = '/'.$this->user.'/files'.$newMountPoint; | |
| 78 | 78 | parent::__construct($storage, $absMountPoint, $arguments, $loader); | 
| 79 | 79 | } | 
| 80 | 80 | |
| @@ -101,7 +101,7 @@ discard block | ||
| 101 | 101 | } | 
| 102 | 102 | |
| 103 | 103 | $newMountPoint = $this->generateUniqueTarget( | 
| 104 | - \OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), | |
| 104 | + \OC\Files\Filesystem::normalizePath($parent.'/'.$mountPoint), | |
| 105 | 105 | $this->recipientView, | 
| 106 | 106 | $mountpoints | 
| 107 | 107 | ); | 
| @@ -138,15 +138,15 @@ discard block | ||
| 138 | 138 | */ | 
| 139 | 139 |  	private function generateUniqueTarget($path, $view, array $mountpoints) { | 
| 140 | 140 | $pathinfo = pathinfo($path); | 
| 141 | - $ext = (isset($pathinfo['extension'])) ? '.' . $pathinfo['extension'] : ''; | |
| 141 | + $ext = (isset($pathinfo['extension'])) ? '.'.$pathinfo['extension'] : ''; | |
| 142 | 142 | $name = $pathinfo['filename']; | 
| 143 | 143 | $dir = $pathinfo['dirname']; | 
| 144 | 144 | |
| 145 | 145 | $i = 2; | 
| 146 | - $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; | |
| 146 | + $absolutePath = $this->recipientView->getAbsolutePath($path).'/'; | |
| 147 | 147 |  		while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) { | 
| 148 | -			$path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext); | |
| 149 | - $absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; | |
| 148 | +			$path = Filesystem::normalizePath($dir.'/'.$name.' ('.$i.')'.$ext); | |
| 149 | + $absolutePath = $this->recipientView->getAbsolutePath($path).'/'; | |
| 150 | 150 | $i++; | 
| 151 | 151 | } | 
| 152 | 152 | |
| @@ -167,7 +167,7 @@ discard block | ||
| 167 | 167 | // it is not a file relative to data/user/files | 
| 168 | 168 |  		if (count($split) < 3 || $split[1] !== 'files') { | 
| 169 | 169 |  			\OCP\Util::writeLog('file sharing', | 
| 170 | - 'Can not strip userid and "files/" from path: ' . $path, | |
| 170 | + 'Can not strip userid and "files/" from path: '.$path, | |
| 171 | 171 | \OCP\Util::ERROR); | 
| 172 | 172 |  			throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10); | 
| 173 | 173 | } | 
| @@ -176,7 +176,7 @@ discard block | ||
| 176 | 176 | $sliced = array_slice($split, 2); | 
| 177 | 177 |  		$relPath = implode('/', $sliced); | 
| 178 | 178 | |
| 179 | - return '/' . $relPath; | |
| 179 | + return '/'.$relPath; | |
| 180 | 180 | } | 
| 181 | 181 | |
| 182 | 182 | /** | 
| @@ -198,7 +198,7 @@ discard block | ||
| 198 | 198 | $this->storage->setMountPoint($relTargetPath); | 
| 199 | 199 |  		} catch (\Exception $e) { | 
| 200 | 200 |  			\OCP\Util::writeLog('file sharing', | 
| 201 | - 'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"', | |
| 201 | + 'Could not rename mount point for shared folder "'.$this->getMountPoint().'" to "'.$target.'"', | |
| 202 | 202 | \OCP\Util::ERROR); | 
| 203 | 203 | } | 
| 204 | 204 | |
| @@ -253,7 +253,7 @@ discard block | ||
| 253 | 253 | $row = $result->fetch(); | 
| 254 | 254 | $result->closeCursor(); | 
| 255 | 255 |  			if ($row) { | 
| 256 | - return (int)$row['storage']; | |
| 256 | + return (int) $row['storage']; | |
| 257 | 257 | } | 
| 258 | 258 | return -1; | 
| 259 | 259 | } | 
| @@ -35,189 +35,189 @@ | ||
| 35 | 35 | use OCP\Share\IManager; | 
| 36 | 36 | |
| 37 | 37 |  class MountProvider implements IMountProvider { | 
| 38 | - /** | |
| 39 | - * @var \OCP\IConfig | |
| 40 | - */ | |
| 41 | - protected $config; | |
| 42 | - | |
| 43 | - /** | |
| 44 | - * @var IManager | |
| 45 | - */ | |
| 46 | - protected $shareManager; | |
| 47 | - | |
| 48 | - /** | |
| 49 | - * @var ILogger | |
| 50 | - */ | |
| 51 | - protected $logger; | |
| 52 | - | |
| 53 | - /** | |
| 54 | - * @param \OCP\IConfig $config | |
| 55 | - * @param IManager $shareManager | |
| 56 | - * @param ILogger $logger | |
| 57 | - */ | |
| 58 | -	public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) { | |
| 59 | - $this->config = $config; | |
| 60 | - $this->shareManager = $shareManager; | |
| 61 | - $this->logger = $logger; | |
| 62 | - } | |
| 63 | - | |
| 64 | - | |
| 65 | - /** | |
| 66 | - * Get all mountpoints applicable for the user and check for shares where we need to update the etags | |
| 67 | - * | |
| 68 | - * @param \OCP\IUser $user | |
| 69 | - * @param \OCP\Files\Storage\IStorageFactory $storageFactory | |
| 70 | - * @return \OCP\Files\Mount\IMountPoint[] | |
| 71 | - */ | |
| 72 | -	public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { | |
| 73 | - | |
| 74 | - $shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1); | |
| 75 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1)); | |
| 76 | - $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_CIRCLE, null, -1)); | |
| 77 | - | |
| 78 | - // filter out excluded shares and group shares that includes self | |
| 79 | -		$shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { | |
| 80 | - return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); | |
| 81 | - }); | |
| 82 | - | |
| 83 | - $superShares = $this->buildSuperShares($shares, $user); | |
| 84 | - | |
| 85 | - $mounts = []; | |
| 86 | -		$view = new View('/' . $user->getUID() . '/files'); | |
| 87 | - $ownerViews = []; | |
| 88 | - $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID()); | |
| 89 | - $foldersExistCache = new CappedMemoryCache(); | |
| 90 | -		foreach ($superShares as $share) { | |
| 91 | -			try { | |
| 92 | - /** @var \OCP\Share\IShare $parentShare */ | |
| 93 | - $parentShare = $share[0]; | |
| 94 | - $owner = $parentShare->getShareOwner(); | |
| 95 | -				if (!isset($ownerViews[$owner])) { | |
| 96 | -					$ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files'); | |
| 97 | - } | |
| 98 | - $mount = new SharedMount( | |
| 99 | - '\OCA\Files_Sharing\SharedStorage', | |
| 100 | - $mounts, | |
| 101 | - [ | |
| 102 | - 'user' => $user->getUID(), | |
| 103 | - // parent share | |
| 104 | - 'superShare' => $parentShare, | |
| 105 | - // children/component of the superShare | |
| 106 | - 'groupedShares' => $share[1], | |
| 107 | - 'ownerView' => $ownerViews[$owner], | |
| 108 | - 'sharingDisabledForUser' => $sharingDisabledForUser | |
| 109 | - ], | |
| 110 | - $storageFactory, | |
| 111 | - $view, | |
| 112 | - $foldersExistCache | |
| 113 | - ); | |
| 114 | - $mounts[$mount->getMountPoint()] = $mount; | |
| 115 | -			} catch (\Exception $e) { | |
| 116 | - $this->logger->logException($e); | |
| 117 | -				$this->logger->error('Error while trying to create shared mount'); | |
| 118 | - } | |
| 119 | - } | |
| 120 | - | |
| 121 | - // array_filter removes the null values from the array | |
| 122 | - return array_values(array_filter($mounts)); | |
| 123 | - } | |
| 124 | - | |
| 125 | - /** | |
| 126 | - * Groups shares by path (nodeId) and target path | |
| 127 | - * | |
| 128 | - * @param \OCP\Share\IShare[] $shares | |
| 129 | - * @return \OCP\Share\IShare[][] array of grouped shares, each element in the | |
| 130 | - * array is a group which itself is an array of shares | |
| 131 | - */ | |
| 132 | -	private function groupShares(array $shares) { | |
| 133 | - $tmp = []; | |
| 134 | - | |
| 135 | -		foreach ($shares as $share) { | |
| 136 | -			if (!isset($tmp[$share->getNodeId()])) { | |
| 137 | - $tmp[$share->getNodeId()] = []; | |
| 138 | - } | |
| 139 | - $tmp[$share->getNodeId()][] = $share; | |
| 140 | - } | |
| 141 | - | |
| 142 | - $result = []; | |
| 143 | - // sort by stime, the super share will be based on the least recent share | |
| 144 | -		foreach ($tmp as &$tmp2) { | |
| 145 | -			@usort($tmp2, function($a, $b) { | |
| 146 | -				if ($a->getShareTime() <= $b->getShareTime()) { | |
| 147 | - return -1; | |
| 148 | - } | |
| 149 | - return 1; | |
| 150 | - }); | |
| 151 | - $result[] = $tmp2; | |
| 152 | - } | |
| 153 | - | |
| 154 | - return array_values($result); | |
| 155 | - } | |
| 156 | - | |
| 157 | - /** | |
| 158 | - * Build super shares (virtual share) by grouping them by node id and target, | |
| 159 | - * then for each group compute the super share and return it along with the matching | |
| 160 | - * grouped shares. The most permissive permissions are used based on the permissions | |
| 161 | - * of all shares within the group. | |
| 162 | - * | |
| 163 | - * @param \OCP\Share\IShare[] $allShares | |
| 164 | - * @param \OCP\IUser $user user | |
| 165 | - * @return array Tuple of [superShare, groupedShares] | |
| 166 | - */ | |
| 167 | -	private function buildSuperShares(array $allShares, \OCP\IUser $user) { | |
| 168 | - $result = []; | |
| 169 | - | |
| 170 | - $groupedShares = $this->groupShares($allShares); | |
| 171 | - | |
| 172 | - /** @var \OCP\Share\IShare[] $shares */ | |
| 173 | -		foreach ($groupedShares as $shares) { | |
| 174 | -			if (count($shares) === 0) { | |
| 175 | - continue; | |
| 176 | - } | |
| 177 | - | |
| 178 | - $superShare = $this->shareManager->newShare(); | |
| 179 | - | |
| 180 | - // compute super share based on first entry of the group | |
| 181 | - $superShare->setId($shares[0]->getId()) | |
| 182 | - ->setShareOwner($shares[0]->getShareOwner()) | |
| 183 | - ->setNodeId($shares[0]->getNodeId()) | |
| 184 | - ->setTarget($shares[0]->getTarget()); | |
| 185 | - | |
| 186 | - // use most permissive permissions | |
| 187 | - $permissions = 0; | |
| 188 | -			foreach ($shares as $share) { | |
| 189 | - $permissions |= $share->getPermissions(); | |
| 190 | -				if ($share->getTarget() !== $superShare->getTarget()) { | |
| 191 | - // adjust target, for database consistency | |
| 192 | - $share->setTarget($superShare->getTarget()); | |
| 193 | -					try { | |
| 194 | - $this->shareManager->moveShare($share, $user->getUID()); | |
| 195 | -					} catch (\InvalidArgumentException $e) { | |
| 196 | - // ignore as it is not important and we don't want to | |
| 197 | - // block FS setup | |
| 198 | - | |
| 199 | - // the subsequent code anyway only uses the target of the | |
| 200 | - // super share | |
| 201 | - | |
| 202 | - // such issue can usually happen when dealing with | |
| 203 | - // null groups which usually appear with group backend | |
| 204 | - // caching inconsistencies | |
| 205 | - $this->logger->debug( | |
| 206 | - 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), | |
| 207 | - ['app' => 'files_sharing'] | |
| 208 | - ); | |
| 209 | - } | |
| 210 | - } | |
| 211 | -				if (!is_null($share->getNodeCacheEntry())) { | |
| 212 | - $superShare->setNodeCacheEntry($share->getNodeCacheEntry()); | |
| 213 | - } | |
| 214 | - } | |
| 215 | - | |
| 216 | - $superShare->setPermissions($permissions); | |
| 217 | - | |
| 218 | - $result[] = [$superShare, $shares]; | |
| 219 | - } | |
| 220 | - | |
| 221 | - return $result; | |
| 222 | - } | |
| 38 | + /** | |
| 39 | + * @var \OCP\IConfig | |
| 40 | + */ | |
| 41 | + protected $config; | |
| 42 | + | |
| 43 | + /** | |
| 44 | + * @var IManager | |
| 45 | + */ | |
| 46 | + protected $shareManager; | |
| 47 | + | |
| 48 | + /** | |
| 49 | + * @var ILogger | |
| 50 | + */ | |
| 51 | + protected $logger; | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * @param \OCP\IConfig $config | |
| 55 | + * @param IManager $shareManager | |
| 56 | + * @param ILogger $logger | |
| 57 | + */ | |
| 58 | +    public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) { | |
| 59 | + $this->config = $config; | |
| 60 | + $this->shareManager = $shareManager; | |
| 61 | + $this->logger = $logger; | |
| 62 | + } | |
| 63 | + | |
| 64 | + | |
| 65 | + /** | |
| 66 | + * Get all mountpoints applicable for the user and check for shares where we need to update the etags | |
| 67 | + * | |
| 68 | + * @param \OCP\IUser $user | |
| 69 | + * @param \OCP\Files\Storage\IStorageFactory $storageFactory | |
| 70 | + * @return \OCP\Files\Mount\IMountPoint[] | |
| 71 | + */ | |
| 72 | +    public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) { | |
| 73 | + | |
| 74 | + $shares = $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_USER, null, -1); | |
| 75 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_GROUP, null, -1)); | |
| 76 | + $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_CIRCLE, null, -1)); | |
| 77 | + | |
| 78 | + // filter out excluded shares and group shares that includes self | |
| 79 | +        $shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { | |
| 80 | + return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); | |
| 81 | + }); | |
| 82 | + | |
| 83 | + $superShares = $this->buildSuperShares($shares, $user); | |
| 84 | + | |
| 85 | + $mounts = []; | |
| 86 | +        $view = new View('/' . $user->getUID() . '/files'); | |
| 87 | + $ownerViews = []; | |
| 88 | + $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID()); | |
| 89 | + $foldersExistCache = new CappedMemoryCache(); | |
| 90 | +        foreach ($superShares as $share) { | |
| 91 | +            try { | |
| 92 | + /** @var \OCP\Share\IShare $parentShare */ | |
| 93 | + $parentShare = $share[0]; | |
| 94 | + $owner = $parentShare->getShareOwner(); | |
| 95 | +                if (!isset($ownerViews[$owner])) { | |
| 96 | +                    $ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files'); | |
| 97 | + } | |
| 98 | + $mount = new SharedMount( | |
| 99 | + '\OCA\Files_Sharing\SharedStorage', | |
| 100 | + $mounts, | |
| 101 | + [ | |
| 102 | + 'user' => $user->getUID(), | |
| 103 | + // parent share | |
| 104 | + 'superShare' => $parentShare, | |
| 105 | + // children/component of the superShare | |
| 106 | + 'groupedShares' => $share[1], | |
| 107 | + 'ownerView' => $ownerViews[$owner], | |
| 108 | + 'sharingDisabledForUser' => $sharingDisabledForUser | |
| 109 | + ], | |
| 110 | + $storageFactory, | |
| 111 | + $view, | |
| 112 | + $foldersExistCache | |
| 113 | + ); | |
| 114 | + $mounts[$mount->getMountPoint()] = $mount; | |
| 115 | +            } catch (\Exception $e) { | |
| 116 | + $this->logger->logException($e); | |
| 117 | +                $this->logger->error('Error while trying to create shared mount'); | |
| 118 | + } | |
| 119 | + } | |
| 120 | + | |
| 121 | + // array_filter removes the null values from the array | |
| 122 | + return array_values(array_filter($mounts)); | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Groups shares by path (nodeId) and target path | |
| 127 | + * | |
| 128 | + * @param \OCP\Share\IShare[] $shares | |
| 129 | + * @return \OCP\Share\IShare[][] array of grouped shares, each element in the | |
| 130 | + * array is a group which itself is an array of shares | |
| 131 | + */ | |
| 132 | +    private function groupShares(array $shares) { | |
| 133 | + $tmp = []; | |
| 134 | + | |
| 135 | +        foreach ($shares as $share) { | |
| 136 | +            if (!isset($tmp[$share->getNodeId()])) { | |
| 137 | + $tmp[$share->getNodeId()] = []; | |
| 138 | + } | |
| 139 | + $tmp[$share->getNodeId()][] = $share; | |
| 140 | + } | |
| 141 | + | |
| 142 | + $result = []; | |
| 143 | + // sort by stime, the super share will be based on the least recent share | |
| 144 | +        foreach ($tmp as &$tmp2) { | |
| 145 | +            @usort($tmp2, function($a, $b) { | |
| 146 | +                if ($a->getShareTime() <= $b->getShareTime()) { | |
| 147 | + return -1; | |
| 148 | + } | |
| 149 | + return 1; | |
| 150 | + }); | |
| 151 | + $result[] = $tmp2; | |
| 152 | + } | |
| 153 | + | |
| 154 | + return array_values($result); | |
| 155 | + } | |
| 156 | + | |
| 157 | + /** | |
| 158 | + * Build super shares (virtual share) by grouping them by node id and target, | |
| 159 | + * then for each group compute the super share and return it along with the matching | |
| 160 | + * grouped shares. The most permissive permissions are used based on the permissions | |
| 161 | + * of all shares within the group. | |
| 162 | + * | |
| 163 | + * @param \OCP\Share\IShare[] $allShares | |
| 164 | + * @param \OCP\IUser $user user | |
| 165 | + * @return array Tuple of [superShare, groupedShares] | |
| 166 | + */ | |
| 167 | +    private function buildSuperShares(array $allShares, \OCP\IUser $user) { | |
| 168 | + $result = []; | |
| 169 | + | |
| 170 | + $groupedShares = $this->groupShares($allShares); | |
| 171 | + | |
| 172 | + /** @var \OCP\Share\IShare[] $shares */ | |
| 173 | +        foreach ($groupedShares as $shares) { | |
| 174 | +            if (count($shares) === 0) { | |
| 175 | + continue; | |
| 176 | + } | |
| 177 | + | |
| 178 | + $superShare = $this->shareManager->newShare(); | |
| 179 | + | |
| 180 | + // compute super share based on first entry of the group | |
| 181 | + $superShare->setId($shares[0]->getId()) | |
| 182 | + ->setShareOwner($shares[0]->getShareOwner()) | |
| 183 | + ->setNodeId($shares[0]->getNodeId()) | |
| 184 | + ->setTarget($shares[0]->getTarget()); | |
| 185 | + | |
| 186 | + // use most permissive permissions | |
| 187 | + $permissions = 0; | |
| 188 | +            foreach ($shares as $share) { | |
| 189 | + $permissions |= $share->getPermissions(); | |
| 190 | +                if ($share->getTarget() !== $superShare->getTarget()) { | |
| 191 | + // adjust target, for database consistency | |
| 192 | + $share->setTarget($superShare->getTarget()); | |
| 193 | +                    try { | |
| 194 | + $this->shareManager->moveShare($share, $user->getUID()); | |
| 195 | +                    } catch (\InvalidArgumentException $e) { | |
| 196 | + // ignore as it is not important and we don't want to | |
| 197 | + // block FS setup | |
| 198 | + | |
| 199 | + // the subsequent code anyway only uses the target of the | |
| 200 | + // super share | |
| 201 | + | |
| 202 | + // such issue can usually happen when dealing with | |
| 203 | + // null groups which usually appear with group backend | |
| 204 | + // caching inconsistencies | |
| 205 | + $this->logger->debug( | |
| 206 | + 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), | |
| 207 | + ['app' => 'files_sharing'] | |
| 208 | + ); | |
| 209 | + } | |
| 210 | + } | |
| 211 | +                if (!is_null($share->getNodeCacheEntry())) { | |
| 212 | + $superShare->setNodeCacheEntry($share->getNodeCacheEntry()); | |
| 213 | + } | |
| 214 | + } | |
| 215 | + | |
| 216 | + $superShare->setPermissions($permissions); | |
| 217 | + | |
| 218 | + $result[] = [$superShare, $shares]; | |
| 219 | + } | |
| 220 | + | |
| 221 | + return $result; | |
| 222 | + } | |
| 223 | 223 | } | 
| @@ -76,14 +76,14 @@ discard block | ||
| 76 | 76 | $shares = array_merge($shares, $this->shareManager->getSharedWith($user->getUID(), \OCP\Share::SHARE_TYPE_CIRCLE, null, -1)); | 
| 77 | 77 | |
| 78 | 78 | // filter out excluded shares and group shares that includes self | 
| 79 | -		$shares = array_filter($shares, function (\OCP\Share\IShare $share) use ($user) { | |
| 79 | +		$shares = array_filter($shares, function(\OCP\Share\IShare $share) use ($user) { | |
| 80 | 80 | return $share->getPermissions() > 0 && $share->getShareOwner() !== $user->getUID(); | 
| 81 | 81 | }); | 
| 82 | 82 | |
| 83 | 83 | $superShares = $this->buildSuperShares($shares, $user); | 
| 84 | 84 | |
| 85 | 85 | $mounts = []; | 
| 86 | -		$view = new View('/' . $user->getUID() . '/files'); | |
| 86 | +		$view = new View('/'.$user->getUID().'/files'); | |
| 87 | 87 | $ownerViews = []; | 
| 88 | 88 | $sharingDisabledForUser = $this->shareManager->sharingDisabledForUser($user->getUID()); | 
| 89 | 89 | $foldersExistCache = new CappedMemoryCache(); | 
| @@ -93,7 +93,7 @@ discard block | ||
| 93 | 93 | $parentShare = $share[0]; | 
| 94 | 94 | $owner = $parentShare->getShareOwner(); | 
| 95 | 95 |  				if (!isset($ownerViews[$owner])) { | 
| 96 | -					$ownerViews[$owner] = new View('/' . $parentShare->getShareOwner() . '/files'); | |
| 96 | +					$ownerViews[$owner] = new View('/'.$parentShare->getShareOwner().'/files'); | |
| 97 | 97 | } | 
| 98 | 98 | $mount = new SharedMount( | 
| 99 | 99 | '\OCA\Files_Sharing\SharedStorage', | 
| @@ -203,7 +203,7 @@ discard block | ||
| 203 | 203 | // null groups which usually appear with group backend | 
| 204 | 204 | // caching inconsistencies | 
| 205 | 205 | $this->logger->debug( | 
| 206 | - 'Could not adjust share target for share ' . $share->getId() . ' to make it consistent: ' . $e->getMessage(), | |
| 206 | + 'Could not adjust share target for share '.$share->getId().' to make it consistent: '.$e->getMessage(), | |
| 207 | 207 | ['app' => 'files_sharing'] | 
| 208 | 208 | ); | 
| 209 | 209 | } | 
| @@ -82,2092 +82,2092 @@ | ||
| 82 | 82 | * \OC\Files\Storage\Storage object | 
| 83 | 83 | */ | 
| 84 | 84 |  class View { | 
| 85 | - /** @var string */ | |
| 86 | - private $fakeRoot = ''; | |
| 87 | - | |
| 88 | - /** | |
| 89 | - * @var \OCP\Lock\ILockingProvider | |
| 90 | - */ | |
| 91 | - protected $lockingProvider; | |
| 92 | - | |
| 93 | - private $lockingEnabled; | |
| 94 | - | |
| 95 | - private $updaterEnabled = true; | |
| 96 | - | |
| 97 | - /** @var \OC\User\Manager */ | |
| 98 | - private $userManager; | |
| 99 | - | |
| 100 | - /** @var \OCP\ILogger */ | |
| 101 | - private $logger; | |
| 102 | - | |
| 103 | - /** | |
| 104 | - * @param string $root | |
| 105 | - * @throws \Exception If $root contains an invalid path | |
| 106 | - */ | |
| 107 | -	public function __construct($root = '') { | |
| 108 | -		if (is_null($root)) { | |
| 109 | -			throw new \InvalidArgumentException('Root can\'t be null'); | |
| 110 | - } | |
| 111 | -		if (!Filesystem::isValidPath($root)) { | |
| 112 | - throw new \Exception(); | |
| 113 | - } | |
| 114 | - | |
| 115 | - $this->fakeRoot = $root; | |
| 116 | - $this->lockingProvider = \OC::$server->getLockingProvider(); | |
| 117 | - $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); | |
| 118 | - $this->userManager = \OC::$server->getUserManager(); | |
| 119 | - $this->logger = \OC::$server->getLogger(); | |
| 120 | - } | |
| 121 | - | |
| 122 | -	public function getAbsolutePath($path = '/') { | |
| 123 | -		if ($path === null) { | |
| 124 | - return null; | |
| 125 | - } | |
| 126 | - $this->assertPathLength($path); | |
| 127 | -		if ($path === '') { | |
| 128 | - $path = '/'; | |
| 129 | - } | |
| 130 | -		if ($path[0] !== '/') { | |
| 131 | - $path = '/' . $path; | |
| 132 | - } | |
| 133 | - return $this->fakeRoot . $path; | |
| 134 | - } | |
| 135 | - | |
| 136 | - /** | |
| 137 | - * change the root to a fake root | |
| 138 | - * | |
| 139 | - * @param string $fakeRoot | |
| 140 | - * @return boolean|null | |
| 141 | - */ | |
| 142 | -	public function chroot($fakeRoot) { | |
| 143 | -		if (!$fakeRoot == '') { | |
| 144 | -			if ($fakeRoot[0] !== '/') { | |
| 145 | - $fakeRoot = '/' . $fakeRoot; | |
| 146 | - } | |
| 147 | - } | |
| 148 | - $this->fakeRoot = $fakeRoot; | |
| 149 | - } | |
| 150 | - | |
| 151 | - /** | |
| 152 | - * get the fake root | |
| 153 | - * | |
| 154 | - * @return string | |
| 155 | - */ | |
| 156 | -	public function getRoot() { | |
| 157 | - return $this->fakeRoot; | |
| 158 | - } | |
| 159 | - | |
| 160 | - /** | |
| 161 | - * get path relative to the root of the view | |
| 162 | - * | |
| 163 | - * @param string $path | |
| 164 | - * @return string | |
| 165 | - */ | |
| 166 | -	public function getRelativePath($path) { | |
| 167 | - $this->assertPathLength($path); | |
| 168 | -		if ($this->fakeRoot == '') { | |
| 169 | - return $path; | |
| 170 | - } | |
| 171 | - | |
| 172 | -		if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { | |
| 173 | - return '/'; | |
| 174 | - } | |
| 175 | - | |
| 176 | - // missing slashes can cause wrong matches! | |
| 177 | - $root = rtrim($this->fakeRoot, '/') . '/'; | |
| 178 | - | |
| 179 | -		if (strpos($path, $root) !== 0) { | |
| 180 | - return null; | |
| 181 | -		} else { | |
| 182 | - $path = substr($path, strlen($this->fakeRoot)); | |
| 183 | -			if (strlen($path) === 0) { | |
| 184 | - return '/'; | |
| 185 | -			} else { | |
| 186 | - return $path; | |
| 187 | - } | |
| 188 | - } | |
| 189 | - } | |
| 190 | - | |
| 191 | - /** | |
| 192 | - * get the mountpoint of the storage object for a path | |
| 193 | - * ( note: because a storage is not always mounted inside the fakeroot, the | |
| 194 | - * returned mountpoint is relative to the absolute root of the filesystem | |
| 195 | - * and does not take the chroot into account ) | |
| 196 | - * | |
| 197 | - * @param string $path | |
| 198 | - * @return string | |
| 199 | - */ | |
| 200 | -	public function getMountPoint($path) { | |
| 201 | - return Filesystem::getMountPoint($this->getAbsolutePath($path)); | |
| 202 | - } | |
| 203 | - | |
| 204 | - /** | |
| 205 | - * get the mountpoint of the storage object for a path | |
| 206 | - * ( note: because a storage is not always mounted inside the fakeroot, the | |
| 207 | - * returned mountpoint is relative to the absolute root of the filesystem | |
| 208 | - * and does not take the chroot into account ) | |
| 209 | - * | |
| 210 | - * @param string $path | |
| 211 | - * @return \OCP\Files\Mount\IMountPoint | |
| 212 | - */ | |
| 213 | -	public function getMount($path) { | |
| 214 | - return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); | |
| 215 | - } | |
| 216 | - | |
| 217 | - /** | |
| 218 | - * resolve a path to a storage and internal path | |
| 219 | - * | |
| 220 | - * @param string $path | |
| 221 | - * @return array an array consisting of the storage and the internal path | |
| 222 | - */ | |
| 223 | -	public function resolvePath($path) { | |
| 224 | - $a = $this->getAbsolutePath($path); | |
| 225 | - $p = Filesystem::normalizePath($a); | |
| 226 | - return Filesystem::resolvePath($p); | |
| 227 | - } | |
| 228 | - | |
| 229 | - /** | |
| 230 | - * return the path to a local version of the file | |
| 231 | - * we need this because we can't know if a file is stored local or not from | |
| 232 | - * outside the filestorage and for some purposes a local file is needed | |
| 233 | - * | |
| 234 | - * @param string $path | |
| 235 | - * @return string | |
| 236 | - */ | |
| 237 | -	public function getLocalFile($path) { | |
| 238 | - $parent = substr($path, 0, strrpos($path, '/')); | |
| 239 | - $path = $this->getAbsolutePath($path); | |
| 240 | - list($storage, $internalPath) = Filesystem::resolvePath($path); | |
| 241 | -		if (Filesystem::isValidPath($parent) and $storage) { | |
| 242 | - return $storage->getLocalFile($internalPath); | |
| 243 | -		} else { | |
| 244 | - return null; | |
| 245 | - } | |
| 246 | - } | |
| 247 | - | |
| 248 | - /** | |
| 249 | - * @param string $path | |
| 250 | - * @return string | |
| 251 | - */ | |
| 252 | -	public function getLocalFolder($path) { | |
| 253 | - $parent = substr($path, 0, strrpos($path, '/')); | |
| 254 | - $path = $this->getAbsolutePath($path); | |
| 255 | - list($storage, $internalPath) = Filesystem::resolvePath($path); | |
| 256 | -		if (Filesystem::isValidPath($parent) and $storage) { | |
| 257 | - return $storage->getLocalFolder($internalPath); | |
| 258 | -		} else { | |
| 259 | - return null; | |
| 260 | - } | |
| 261 | - } | |
| 262 | - | |
| 263 | - /** | |
| 264 | - * the following functions operate with arguments and return values identical | |
| 265 | - * to those of their PHP built-in equivalents. Mostly they are merely wrappers | |
| 266 | - * for \OC\Files\Storage\Storage via basicOperation(). | |
| 267 | - */ | |
| 268 | -	public function mkdir($path) { | |
| 269 | -		return $this->basicOperation('mkdir', $path, array('create', 'write')); | |
| 270 | - } | |
| 271 | - | |
| 272 | - /** | |
| 273 | - * remove mount point | |
| 274 | - * | |
| 275 | - * @param \OC\Files\Mount\MoveableMount $mount | |
| 276 | - * @param string $path relative to data/ | |
| 277 | - * @return boolean | |
| 278 | - */ | |
| 279 | -	protected function removeMount($mount, $path) { | |
| 280 | -		if ($mount instanceof MoveableMount) { | |
| 281 | - // cut of /user/files to get the relative path to data/user/files | |
| 282 | -			$pathParts = explode('/', $path, 4); | |
| 283 | - $relPath = '/' . $pathParts[3]; | |
| 284 | - $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); | |
| 285 | - \OC_Hook::emit( | |
| 286 | - Filesystem::CLASSNAME, "umount", | |
| 287 | - array(Filesystem::signal_param_path => $relPath) | |
| 288 | - ); | |
| 289 | - $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); | |
| 290 | - $result = $mount->removeMount(); | |
| 291 | - $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); | |
| 292 | -			if ($result) { | |
| 293 | - \OC_Hook::emit( | |
| 294 | - Filesystem::CLASSNAME, "post_umount", | |
| 295 | - array(Filesystem::signal_param_path => $relPath) | |
| 296 | - ); | |
| 297 | - } | |
| 298 | - $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); | |
| 299 | - return $result; | |
| 300 | -		} else { | |
| 301 | - // do not allow deleting the storage's root / the mount point | |
| 302 | - // because for some storages it might delete the whole contents | |
| 303 | - // but isn't supposed to work that way | |
| 304 | - return false; | |
| 305 | - } | |
| 306 | - } | |
| 307 | - | |
| 308 | -	public function disableCacheUpdate() { | |
| 309 | - $this->updaterEnabled = false; | |
| 310 | - } | |
| 311 | - | |
| 312 | -	public function enableCacheUpdate() { | |
| 313 | - $this->updaterEnabled = true; | |
| 314 | - } | |
| 315 | - | |
| 316 | -	protected function writeUpdate(Storage $storage, $internalPath, $time = null) { | |
| 317 | -		if ($this->updaterEnabled) { | |
| 318 | -			if (is_null($time)) { | |
| 319 | - $time = time(); | |
| 320 | - } | |
| 321 | - $storage->getUpdater()->update($internalPath, $time); | |
| 322 | - } | |
| 323 | - } | |
| 324 | - | |
| 325 | -	protected function removeUpdate(Storage $storage, $internalPath) { | |
| 326 | -		if ($this->updaterEnabled) { | |
| 327 | - $storage->getUpdater()->remove($internalPath); | |
| 328 | - } | |
| 329 | - } | |
| 330 | - | |
| 331 | -	protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { | |
| 332 | -		if ($this->updaterEnabled) { | |
| 333 | - $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); | |
| 334 | - } | |
| 335 | - } | |
| 336 | - | |
| 337 | - /** | |
| 338 | - * @param string $path | |
| 339 | - * @return bool|mixed | |
| 340 | - */ | |
| 341 | -	public function rmdir($path) { | |
| 342 | - $absolutePath = $this->getAbsolutePath($path); | |
| 343 | - $mount = Filesystem::getMountManager()->find($absolutePath); | |
| 344 | -		if ($mount->getInternalPath($absolutePath) === '') { | |
| 345 | - return $this->removeMount($mount, $absolutePath); | |
| 346 | - } | |
| 347 | -		if ($this->is_dir($path)) { | |
| 348 | -			$result = $this->basicOperation('rmdir', $path, array('delete')); | |
| 349 | -		} else { | |
| 350 | - $result = false; | |
| 351 | - } | |
| 352 | - | |
| 353 | -		if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete | |
| 354 | - $storage = $mount->getStorage(); | |
| 355 | - $internalPath = $mount->getInternalPath($absolutePath); | |
| 356 | - $storage->getUpdater()->remove($internalPath); | |
| 357 | - } | |
| 358 | - return $result; | |
| 359 | - } | |
| 360 | - | |
| 361 | - /** | |
| 362 | - * @param string $path | |
| 363 | - * @return resource | |
| 364 | - */ | |
| 365 | -	public function opendir($path) { | |
| 366 | -		return $this->basicOperation('opendir', $path, array('read')); | |
| 367 | - } | |
| 368 | - | |
| 369 | - /** | |
| 370 | - * @param $handle | |
| 371 | - * @return mixed | |
| 372 | - */ | |
| 373 | -	public function readdir($handle) { | |
| 374 | -		$fsLocal = new Storage\Local(array('datadir' => '/')); | |
| 375 | - return $fsLocal->readdir($handle); | |
| 376 | - } | |
| 377 | - | |
| 378 | - /** | |
| 379 | - * @param string $path | |
| 380 | - * @return bool|mixed | |
| 381 | - */ | |
| 382 | -	public function is_dir($path) { | |
| 383 | -		if ($path == '/') { | |
| 384 | - return true; | |
| 385 | - } | |
| 386 | -		return $this->basicOperation('is_dir', $path); | |
| 387 | - } | |
| 388 | - | |
| 389 | - /** | |
| 390 | - * @param string $path | |
| 391 | - * @return bool|mixed | |
| 392 | - */ | |
| 393 | -	public function is_file($path) { | |
| 394 | -		if ($path == '/') { | |
| 395 | - return false; | |
| 396 | - } | |
| 397 | -		return $this->basicOperation('is_file', $path); | |
| 398 | - } | |
| 399 | - | |
| 400 | - /** | |
| 401 | - * @param string $path | |
| 402 | - * @return mixed | |
| 403 | - */ | |
| 404 | -	public function stat($path) { | |
| 405 | -		return $this->basicOperation('stat', $path); | |
| 406 | - } | |
| 407 | - | |
| 408 | - /** | |
| 409 | - * @param string $path | |
| 410 | - * @return mixed | |
| 411 | - */ | |
| 412 | -	public function filetype($path) { | |
| 413 | -		return $this->basicOperation('filetype', $path); | |
| 414 | - } | |
| 415 | - | |
| 416 | - /** | |
| 417 | - * @param string $path | |
| 418 | - * @return mixed | |
| 419 | - */ | |
| 420 | -	public function filesize($path) { | |
| 421 | -		return $this->basicOperation('filesize', $path); | |
| 422 | - } | |
| 423 | - | |
| 424 | - /** | |
| 425 | - * @param string $path | |
| 426 | - * @return bool|mixed | |
| 427 | - * @throws \OCP\Files\InvalidPathException | |
| 428 | - */ | |
| 429 | -	public function readfile($path) { | |
| 430 | - $this->assertPathLength($path); | |
| 431 | - @ob_end_clean(); | |
| 432 | - $handle = $this->fopen($path, 'rb'); | |
| 433 | -		if ($handle) { | |
| 434 | - $chunkSize = 8192; // 8 kB chunks | |
| 435 | -			while (!feof($handle)) { | |
| 436 | - echo fread($handle, $chunkSize); | |
| 437 | - flush(); | |
| 438 | - } | |
| 439 | - fclose($handle); | |
| 440 | - $size = $this->filesize($path); | |
| 441 | - return $size; | |
| 442 | - } | |
| 443 | - return false; | |
| 444 | - } | |
| 445 | - | |
| 446 | - /** | |
| 447 | - * @param string $path | |
| 448 | - * @param int $from | |
| 449 | - * @param int $to | |
| 450 | - * @return bool|mixed | |
| 451 | - * @throws \OCP\Files\InvalidPathException | |
| 452 | - * @throws \OCP\Files\UnseekableException | |
| 453 | - */ | |
| 454 | -	public function readfilePart($path, $from, $to) { | |
| 455 | - $this->assertPathLength($path); | |
| 456 | - @ob_end_clean(); | |
| 457 | - $handle = $this->fopen($path, 'rb'); | |
| 458 | -		if ($handle) { | |
| 459 | - $chunkSize = 8192; // 8 kB chunks | |
| 460 | - $startReading = true; | |
| 461 | - | |
| 462 | -			if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { | |
| 463 | - // forward file handle via chunked fread because fseek seem to have failed | |
| 464 | - | |
| 465 | - $end = $from + 1; | |
| 466 | -				while (!feof($handle) && ftell($handle) < $end) { | |
| 467 | - $len = $from - ftell($handle); | |
| 468 | -					if ($len > $chunkSize) { | |
| 469 | - $len = $chunkSize; | |
| 470 | - } | |
| 471 | - $result = fread($handle, $len); | |
| 472 | - | |
| 473 | -					if ($result === false) { | |
| 474 | - $startReading = false; | |
| 475 | - break; | |
| 476 | - } | |
| 477 | - } | |
| 478 | - } | |
| 479 | - | |
| 480 | -			if ($startReading) { | |
| 481 | - $end = $to + 1; | |
| 482 | -				while (!feof($handle) && ftell($handle) < $end) { | |
| 483 | - $len = $end - ftell($handle); | |
| 484 | -					if ($len > $chunkSize) { | |
| 485 | - $len = $chunkSize; | |
| 486 | - } | |
| 487 | - echo fread($handle, $len); | |
| 488 | - flush(); | |
| 489 | - } | |
| 490 | - $size = ftell($handle) - $from; | |
| 491 | - return $size; | |
| 492 | - } | |
| 493 | - | |
| 494 | -			throw new \OCP\Files\UnseekableException('fseek error'); | |
| 495 | - } | |
| 496 | - return false; | |
| 497 | - } | |
| 498 | - | |
| 499 | - /** | |
| 500 | - * @param string $path | |
| 501 | - * @return mixed | |
| 502 | - */ | |
| 503 | -	public function isCreatable($path) { | |
| 504 | -		return $this->basicOperation('isCreatable', $path); | |
| 505 | - } | |
| 506 | - | |
| 507 | - /** | |
| 508 | - * @param string $path | |
| 509 | - * @return mixed | |
| 510 | - */ | |
| 511 | -	public function isReadable($path) { | |
| 512 | -		return $this->basicOperation('isReadable', $path); | |
| 513 | - } | |
| 514 | - | |
| 515 | - /** | |
| 516 | - * @param string $path | |
| 517 | - * @return mixed | |
| 518 | - */ | |
| 519 | -	public function isUpdatable($path) { | |
| 520 | -		return $this->basicOperation('isUpdatable', $path); | |
| 521 | - } | |
| 522 | - | |
| 523 | - /** | |
| 524 | - * @param string $path | |
| 525 | - * @return bool|mixed | |
| 526 | - */ | |
| 527 | -	public function isDeletable($path) { | |
| 528 | - $absolutePath = $this->getAbsolutePath($path); | |
| 529 | - $mount = Filesystem::getMountManager()->find($absolutePath); | |
| 530 | -		if ($mount->getInternalPath($absolutePath) === '') { | |
| 531 | - return $mount instanceof MoveableMount; | |
| 532 | - } | |
| 533 | -		return $this->basicOperation('isDeletable', $path); | |
| 534 | - } | |
| 535 | - | |
| 536 | - /** | |
| 537 | - * @param string $path | |
| 538 | - * @return mixed | |
| 539 | - */ | |
| 540 | -	public function isSharable($path) { | |
| 541 | -		return $this->basicOperation('isSharable', $path); | |
| 542 | - } | |
| 543 | - | |
| 544 | - /** | |
| 545 | - * @param string $path | |
| 546 | - * @return bool|mixed | |
| 547 | - */ | |
| 548 | -	public function file_exists($path) { | |
| 549 | -		if ($path == '/') { | |
| 550 | - return true; | |
| 551 | - } | |
| 552 | -		return $this->basicOperation('file_exists', $path); | |
| 553 | - } | |
| 554 | - | |
| 555 | - /** | |
| 556 | - * @param string $path | |
| 557 | - * @return mixed | |
| 558 | - */ | |
| 559 | -	public function filemtime($path) { | |
| 560 | -		return $this->basicOperation('filemtime', $path); | |
| 561 | - } | |
| 562 | - | |
| 563 | - /** | |
| 564 | - * @param string $path | |
| 565 | - * @param int|string $mtime | |
| 566 | - * @return bool | |
| 567 | - */ | |
| 568 | -	public function touch($path, $mtime = null) { | |
| 569 | -		if (!is_null($mtime) and !is_numeric($mtime)) { | |
| 570 | - $mtime = strtotime($mtime); | |
| 571 | - } | |
| 572 | - | |
| 573 | -		$hooks = array('touch'); | |
| 574 | - | |
| 575 | -		if (!$this->file_exists($path)) { | |
| 576 | - $hooks[] = 'create'; | |
| 577 | - $hooks[] = 'write'; | |
| 578 | - } | |
| 579 | -		$result = $this->basicOperation('touch', $path, $hooks, $mtime); | |
| 580 | -		if (!$result) { | |
| 581 | - // If create file fails because of permissions on external storage like SMB folders, | |
| 582 | - // check file exists and return false if not. | |
| 583 | -			if (!$this->file_exists($path)) { | |
| 584 | - return false; | |
| 585 | - } | |
| 586 | -			if (is_null($mtime)) { | |
| 587 | - $mtime = time(); | |
| 588 | - } | |
| 589 | - //if native touch fails, we emulate it by changing the mtime in the cache | |
| 590 | -			$this->putFileInfo($path, array('mtime' => floor($mtime))); | |
| 591 | - } | |
| 592 | - return true; | |
| 593 | - } | |
| 594 | - | |
| 595 | - /** | |
| 596 | - * @param string $path | |
| 597 | - * @return mixed | |
| 598 | - */ | |
| 599 | -	public function file_get_contents($path) { | |
| 600 | -		return $this->basicOperation('file_get_contents', $path, array('read')); | |
| 601 | - } | |
| 602 | - | |
| 603 | - /** | |
| 604 | - * @param bool $exists | |
| 605 | - * @param string $path | |
| 606 | - * @param bool $run | |
| 607 | - */ | |
| 608 | -	protected function emit_file_hooks_pre($exists, $path, &$run) { | |
| 609 | -		if (!$exists) { | |
| 610 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( | |
| 611 | - Filesystem::signal_param_path => $this->getHookPath($path), | |
| 612 | - Filesystem::signal_param_run => &$run, | |
| 613 | - )); | |
| 614 | -		} else { | |
| 615 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( | |
| 616 | - Filesystem::signal_param_path => $this->getHookPath($path), | |
| 617 | - Filesystem::signal_param_run => &$run, | |
| 618 | - )); | |
| 619 | - } | |
| 620 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( | |
| 621 | - Filesystem::signal_param_path => $this->getHookPath($path), | |
| 622 | - Filesystem::signal_param_run => &$run, | |
| 623 | - )); | |
| 624 | - } | |
| 625 | - | |
| 626 | - /** | |
| 627 | - * @param bool $exists | |
| 628 | - * @param string $path | |
| 629 | - */ | |
| 630 | -	protected function emit_file_hooks_post($exists, $path) { | |
| 631 | -		if (!$exists) { | |
| 632 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( | |
| 633 | - Filesystem::signal_param_path => $this->getHookPath($path), | |
| 634 | - )); | |
| 635 | -		} else { | |
| 636 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( | |
| 637 | - Filesystem::signal_param_path => $this->getHookPath($path), | |
| 638 | - )); | |
| 639 | - } | |
| 640 | - \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( | |
| 641 | - Filesystem::signal_param_path => $this->getHookPath($path), | |
| 642 | - )); | |
| 643 | - } | |
| 644 | - | |
| 645 | - /** | |
| 646 | - * @param string $path | |
| 647 | - * @param mixed $data | |
| 648 | - * @return bool|mixed | |
| 649 | - * @throws \Exception | |
| 650 | - */ | |
| 651 | -	public function file_put_contents($path, $data) { | |
| 652 | -		if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier | |
| 653 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); | |
| 654 | - if (Filesystem::isValidPath($path) | |
| 655 | - and !Filesystem::isFileBlacklisted($path) | |
| 656 | -			) { | |
| 657 | - $path = $this->getRelativePath($absolutePath); | |
| 658 | - | |
| 659 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); | |
| 660 | - | |
| 661 | - $exists = $this->file_exists($path); | |
| 662 | - $run = true; | |
| 663 | -				if ($this->shouldEmitHooks($path)) { | |
| 664 | - $this->emit_file_hooks_pre($exists, $path, $run); | |
| 665 | - } | |
| 666 | -				if (!$run) { | |
| 667 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 668 | - return false; | |
| 669 | - } | |
| 670 | - | |
| 671 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 672 | - | |
| 673 | - /** @var \OC\Files\Storage\Storage $storage */ | |
| 674 | - list($storage, $internalPath) = $this->resolvePath($path); | |
| 675 | - $target = $storage->fopen($internalPath, 'w'); | |
| 676 | -				if ($target) { | |
| 677 | - list (, $result) = \OC_Helper::streamCopy($data, $target); | |
| 678 | - fclose($target); | |
| 679 | - fclose($data); | |
| 680 | - | |
| 681 | - $this->writeUpdate($storage, $internalPath); | |
| 682 | - | |
| 683 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); | |
| 684 | - | |
| 685 | -					if ($this->shouldEmitHooks($path) && $result !== false) { | |
| 686 | - $this->emit_file_hooks_post($exists, $path); | |
| 687 | - } | |
| 688 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 689 | - return $result; | |
| 690 | -				} else { | |
| 691 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 692 | - return false; | |
| 693 | - } | |
| 694 | -			} else { | |
| 695 | - return false; | |
| 696 | - } | |
| 697 | -		} else { | |
| 698 | -			$hooks = ($this->file_exists($path)) ? array('update', 'write') : array('create', 'write'); | |
| 699 | -			return $this->basicOperation('file_put_contents', $path, $hooks, $data); | |
| 700 | - } | |
| 701 | - } | |
| 702 | - | |
| 703 | - /** | |
| 704 | - * @param string $path | |
| 705 | - * @return bool|mixed | |
| 706 | - */ | |
| 707 | -	public function unlink($path) { | |
| 708 | -		if ($path === '' || $path === '/') { | |
| 709 | - // do not allow deleting the root | |
| 710 | - return false; | |
| 711 | - } | |
| 712 | - $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; | |
| 713 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); | |
| 714 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); | |
| 715 | -		if ($mount and $mount->getInternalPath($absolutePath) === '') { | |
| 716 | - return $this->removeMount($mount, $absolutePath); | |
| 717 | - } | |
| 718 | -		if ($this->is_dir($path)) { | |
| 719 | -			$result = $this->basicOperation('rmdir', $path, ['delete']); | |
| 720 | -		} else { | |
| 721 | -			$result = $this->basicOperation('unlink', $path, ['delete']); | |
| 722 | - } | |
| 723 | -		if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete | |
| 724 | - $storage = $mount->getStorage(); | |
| 725 | - $internalPath = $mount->getInternalPath($absolutePath); | |
| 726 | - $storage->getUpdater()->remove($internalPath); | |
| 727 | - return true; | |
| 728 | -		} else { | |
| 729 | - return $result; | |
| 730 | - } | |
| 731 | - } | |
| 732 | - | |
| 733 | - /** | |
| 734 | - * @param string $directory | |
| 735 | - * @return bool|mixed | |
| 736 | - */ | |
| 737 | -	public function deleteAll($directory) { | |
| 738 | - return $this->rmdir($directory); | |
| 739 | - } | |
| 740 | - | |
| 741 | - /** | |
| 742 | - * Rename/move a file or folder from the source path to target path. | |
| 743 | - * | |
| 744 | - * @param string $path1 source path | |
| 745 | - * @param string $path2 target path | |
| 746 | - * | |
| 747 | - * @return bool|mixed | |
| 748 | - */ | |
| 749 | -	public function rename($path1, $path2) { | |
| 750 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); | |
| 751 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); | |
| 752 | - $result = false; | |
| 753 | - if ( | |
| 754 | - Filesystem::isValidPath($path2) | |
| 755 | - and Filesystem::isValidPath($path1) | |
| 756 | - and !Filesystem::isFileBlacklisted($path2) | |
| 757 | -		) { | |
| 758 | - $path1 = $this->getRelativePath($absolutePath1); | |
| 759 | - $path2 = $this->getRelativePath($absolutePath2); | |
| 760 | - $exists = $this->file_exists($path2); | |
| 761 | - | |
| 762 | -			if ($path1 == null or $path2 == null) { | |
| 763 | - return false; | |
| 764 | - } | |
| 765 | - | |
| 766 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); | |
| 767 | -			try { | |
| 768 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); | |
| 769 | - | |
| 770 | - $run = true; | |
| 771 | -				if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { | |
| 772 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation | |
| 773 | - $this->emit_file_hooks_pre($exists, $path2, $run); | |
| 774 | -				} elseif ($this->shouldEmitHooks($path1)) { | |
| 775 | - \OC_Hook::emit( | |
| 776 | - Filesystem::CLASSNAME, Filesystem::signal_rename, | |
| 777 | - array( | |
| 778 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), | |
| 779 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), | |
| 780 | - Filesystem::signal_param_run => &$run | |
| 781 | - ) | |
| 782 | - ); | |
| 783 | - } | |
| 784 | -				if ($run) { | |
| 785 | - $this->verifyPath(dirname($path2), basename($path2)); | |
| 786 | - | |
| 787 | - $manager = Filesystem::getMountManager(); | |
| 788 | - $mount1 = $this->getMount($path1); | |
| 789 | - $mount2 = $this->getMount($path2); | |
| 790 | - $storage1 = $mount1->getStorage(); | |
| 791 | - $storage2 = $mount2->getStorage(); | |
| 792 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); | |
| 793 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); | |
| 794 | - | |
| 795 | - $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); | |
| 796 | -					try { | |
| 797 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); | |
| 798 | - | |
| 799 | -						if ($internalPath1 === '') { | |
| 800 | -							if ($mount1 instanceof MoveableMount) { | |
| 801 | -								if ($this->isTargetAllowed($absolutePath2)) { | |
| 802 | - /** | |
| 803 | - * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 | |
| 804 | - */ | |
| 805 | - $sourceMountPoint = $mount1->getMountPoint(); | |
| 806 | - $result = $mount1->moveMount($absolutePath2); | |
| 807 | - $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); | |
| 808 | -								} else { | |
| 809 | - $result = false; | |
| 810 | - } | |
| 811 | -							} else { | |
| 812 | - $result = false; | |
| 813 | - } | |
| 814 | - // moving a file/folder within the same mount point | |
| 815 | -						} elseif ($storage1 === $storage2) { | |
| 816 | -							if ($storage1) { | |
| 817 | - $result = $storage1->rename($internalPath1, $internalPath2); | |
| 818 | -							} else { | |
| 819 | - $result = false; | |
| 820 | - } | |
| 821 | - // moving a file/folder between storages (from $storage1 to $storage2) | |
| 822 | -						} else { | |
| 823 | - $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); | |
| 824 | - } | |
| 825 | - | |
| 826 | -						if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { | |
| 827 | - // if it was a rename from a part file to a regular file it was a write and not a rename operation | |
| 828 | - $this->writeUpdate($storage2, $internalPath2); | |
| 829 | -						} else if ($result) { | |
| 830 | -							if ($internalPath1 !== '') { // don't do a cache update for moved mounts | |
| 831 | - $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); | |
| 832 | - } | |
| 833 | - } | |
| 834 | -					} catch(\Exception $e) { | |
| 835 | - throw $e; | |
| 836 | -					} finally { | |
| 837 | - $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); | |
| 838 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); | |
| 839 | - } | |
| 840 | - | |
| 841 | -					if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { | |
| 842 | -						if ($this->shouldEmitHooks()) { | |
| 843 | - $this->emit_file_hooks_post($exists, $path2); | |
| 844 | - } | |
| 845 | -					} elseif ($result) { | |
| 846 | -						if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { | |
| 847 | - \OC_Hook::emit( | |
| 848 | - Filesystem::CLASSNAME, | |
| 849 | - Filesystem::signal_post_rename, | |
| 850 | - array( | |
| 851 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), | |
| 852 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) | |
| 853 | - ) | |
| 854 | - ); | |
| 855 | - } | |
| 856 | - } | |
| 857 | - } | |
| 858 | -			} catch(\Exception $e) { | |
| 859 | - throw $e; | |
| 860 | -			} finally { | |
| 861 | - $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); | |
| 862 | - $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); | |
| 863 | - } | |
| 864 | - } | |
| 865 | - return $result; | |
| 866 | - } | |
| 867 | - | |
| 868 | - /** | |
| 869 | - * Copy a file/folder from the source path to target path | |
| 870 | - * | |
| 871 | - * @param string $path1 source path | |
| 872 | - * @param string $path2 target path | |
| 873 | - * @param bool $preserveMtime whether to preserve mtime on the copy | |
| 874 | - * | |
| 875 | - * @return bool|mixed | |
| 876 | - */ | |
| 877 | -	public function copy($path1, $path2, $preserveMtime = false) { | |
| 878 | - $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); | |
| 879 | - $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); | |
| 880 | - $result = false; | |
| 881 | - if ( | |
| 882 | - Filesystem::isValidPath($path2) | |
| 883 | - and Filesystem::isValidPath($path1) | |
| 884 | - and !Filesystem::isFileBlacklisted($path2) | |
| 885 | -		) { | |
| 886 | - $path1 = $this->getRelativePath($absolutePath1); | |
| 887 | - $path2 = $this->getRelativePath($absolutePath2); | |
| 888 | - | |
| 889 | -			if ($path1 == null or $path2 == null) { | |
| 890 | - return false; | |
| 891 | - } | |
| 892 | - $run = true; | |
| 893 | - | |
| 894 | - $this->lockFile($path2, ILockingProvider::LOCK_SHARED); | |
| 895 | - $this->lockFile($path1, ILockingProvider::LOCK_SHARED); | |
| 896 | - $lockTypePath1 = ILockingProvider::LOCK_SHARED; | |
| 897 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; | |
| 898 | - | |
| 899 | -			try { | |
| 900 | - | |
| 901 | - $exists = $this->file_exists($path2); | |
| 902 | -				if ($this->shouldEmitHooks()) { | |
| 903 | - \OC_Hook::emit( | |
| 904 | - Filesystem::CLASSNAME, | |
| 905 | - Filesystem::signal_copy, | |
| 906 | - array( | |
| 907 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), | |
| 908 | - Filesystem::signal_param_newpath => $this->getHookPath($path2), | |
| 909 | - Filesystem::signal_param_run => &$run | |
| 910 | - ) | |
| 911 | - ); | |
| 912 | - $this->emit_file_hooks_pre($exists, $path2, $run); | |
| 913 | - } | |
| 914 | -				if ($run) { | |
| 915 | - $mount1 = $this->getMount($path1); | |
| 916 | - $mount2 = $this->getMount($path2); | |
| 917 | - $storage1 = $mount1->getStorage(); | |
| 918 | - $internalPath1 = $mount1->getInternalPath($absolutePath1); | |
| 919 | - $storage2 = $mount2->getStorage(); | |
| 920 | - $internalPath2 = $mount2->getInternalPath($absolutePath2); | |
| 921 | - | |
| 922 | - $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); | |
| 923 | - $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; | |
| 924 | - | |
| 925 | -					if ($mount1->getMountPoint() == $mount2->getMountPoint()) { | |
| 926 | -						if ($storage1) { | |
| 927 | - $result = $storage1->copy($internalPath1, $internalPath2); | |
| 928 | -						} else { | |
| 929 | - $result = false; | |
| 930 | - } | |
| 931 | -					} else { | |
| 932 | - $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); | |
| 933 | - } | |
| 934 | - | |
| 935 | - $this->writeUpdate($storage2, $internalPath2); | |
| 936 | - | |
| 937 | - $this->changeLock($path2, ILockingProvider::LOCK_SHARED); | |
| 938 | - $lockTypePath2 = ILockingProvider::LOCK_SHARED; | |
| 939 | - | |
| 940 | -					if ($this->shouldEmitHooks() && $result !== false) { | |
| 941 | - \OC_Hook::emit( | |
| 942 | - Filesystem::CLASSNAME, | |
| 943 | - Filesystem::signal_post_copy, | |
| 944 | - array( | |
| 945 | - Filesystem::signal_param_oldpath => $this->getHookPath($path1), | |
| 946 | - Filesystem::signal_param_newpath => $this->getHookPath($path2) | |
| 947 | - ) | |
| 948 | - ); | |
| 949 | - $this->emit_file_hooks_post($exists, $path2); | |
| 950 | - } | |
| 951 | - | |
| 952 | - } | |
| 953 | -			} catch (\Exception $e) { | |
| 954 | - $this->unlockFile($path2, $lockTypePath2); | |
| 955 | - $this->unlockFile($path1, $lockTypePath1); | |
| 956 | - throw $e; | |
| 957 | - } | |
| 958 | - | |
| 959 | - $this->unlockFile($path2, $lockTypePath2); | |
| 960 | - $this->unlockFile($path1, $lockTypePath1); | |
| 961 | - | |
| 962 | - } | |
| 963 | - return $result; | |
| 964 | - } | |
| 965 | - | |
| 966 | - /** | |
| 967 | - * @param string $path | |
| 968 | - * @param string $mode 'r' or 'w' | |
| 969 | - * @return resource | |
| 970 | - */ | |
| 971 | -	public function fopen($path, $mode) { | |
| 972 | -		$mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support | |
| 973 | - $hooks = array(); | |
| 974 | -		switch ($mode) { | |
| 975 | - case 'r': | |
| 976 | - $hooks[] = 'read'; | |
| 977 | - break; | |
| 978 | - case 'r+': | |
| 979 | - case 'w+': | |
| 980 | - case 'x+': | |
| 981 | - case 'a+': | |
| 982 | - $hooks[] = 'read'; | |
| 983 | - $hooks[] = 'write'; | |
| 984 | - break; | |
| 985 | - case 'w': | |
| 986 | - case 'x': | |
| 987 | - case 'a': | |
| 988 | - $hooks[] = 'write'; | |
| 989 | - break; | |
| 990 | - default: | |
| 991 | -				\OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR); | |
| 992 | - } | |
| 993 | - | |
| 994 | -		if ($mode !== 'r' && $mode !== 'w') { | |
| 995 | -			\OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); | |
| 996 | - } | |
| 997 | - | |
| 998 | -		return $this->basicOperation('fopen', $path, $hooks, $mode); | |
| 999 | - } | |
| 1000 | - | |
| 1001 | - /** | |
| 1002 | - * @param string $path | |
| 1003 | - * @return bool|string | |
| 1004 | - * @throws \OCP\Files\InvalidPathException | |
| 1005 | - */ | |
| 1006 | -	public function toTmpFile($path) { | |
| 1007 | - $this->assertPathLength($path); | |
| 1008 | -		if (Filesystem::isValidPath($path)) { | |
| 1009 | - $source = $this->fopen($path, 'r'); | |
| 1010 | -			if ($source) { | |
| 1011 | - $extension = pathinfo($path, PATHINFO_EXTENSION); | |
| 1012 | - $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); | |
| 1013 | - file_put_contents($tmpFile, $source); | |
| 1014 | - return $tmpFile; | |
| 1015 | -			} else { | |
| 1016 | - return false; | |
| 1017 | - } | |
| 1018 | -		} else { | |
| 1019 | - return false; | |
| 1020 | - } | |
| 1021 | - } | |
| 1022 | - | |
| 1023 | - /** | |
| 1024 | - * @param string $tmpFile | |
| 1025 | - * @param string $path | |
| 1026 | - * @return bool|mixed | |
| 1027 | - * @throws \OCP\Files\InvalidPathException | |
| 1028 | - */ | |
| 1029 | -	public function fromTmpFile($tmpFile, $path) { | |
| 1030 | - $this->assertPathLength($path); | |
| 1031 | -		if (Filesystem::isValidPath($path)) { | |
| 1032 | - | |
| 1033 | - // Get directory that the file is going into | |
| 1034 | - $filePath = dirname($path); | |
| 1035 | - | |
| 1036 | - // Create the directories if any | |
| 1037 | -			if (!$this->file_exists($filePath)) { | |
| 1038 | - $result = $this->createParentDirectories($filePath); | |
| 1039 | -				if ($result === false) { | |
| 1040 | - return false; | |
| 1041 | - } | |
| 1042 | - } | |
| 1043 | - | |
| 1044 | - $source = fopen($tmpFile, 'r'); | |
| 1045 | -			if ($source) { | |
| 1046 | - $result = $this->file_put_contents($path, $source); | |
| 1047 | - // $this->file_put_contents() might have already closed | |
| 1048 | - // the resource, so we check it, before trying to close it | |
| 1049 | - // to avoid messages in the error log. | |
| 1050 | -				if (is_resource($source)) { | |
| 1051 | - fclose($source); | |
| 1052 | - } | |
| 1053 | - unlink($tmpFile); | |
| 1054 | - return $result; | |
| 1055 | -			} else { | |
| 1056 | - return false; | |
| 1057 | - } | |
| 1058 | -		} else { | |
| 1059 | - return false; | |
| 1060 | - } | |
| 1061 | - } | |
| 1062 | - | |
| 1063 | - | |
| 1064 | - /** | |
| 1065 | - * @param string $path | |
| 1066 | - * @return mixed | |
| 1067 | - * @throws \OCP\Files\InvalidPathException | |
| 1068 | - */ | |
| 1069 | -	public function getMimeType($path) { | |
| 1070 | - $this->assertPathLength($path); | |
| 1071 | -		return $this->basicOperation('getMimeType', $path); | |
| 1072 | - } | |
| 1073 | - | |
| 1074 | - /** | |
| 1075 | - * @param string $type | |
| 1076 | - * @param string $path | |
| 1077 | - * @param bool $raw | |
| 1078 | - * @return bool|null|string | |
| 1079 | - */ | |
| 1080 | -	public function hash($type, $path, $raw = false) { | |
| 1081 | - $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; | |
| 1082 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); | |
| 1083 | -		if (Filesystem::isValidPath($path)) { | |
| 1084 | - $path = $this->getRelativePath($absolutePath); | |
| 1085 | -			if ($path == null) { | |
| 1086 | - return false; | |
| 1087 | - } | |
| 1088 | -			if ($this->shouldEmitHooks($path)) { | |
| 1089 | - \OC_Hook::emit( | |
| 1090 | - Filesystem::CLASSNAME, | |
| 1091 | - Filesystem::signal_read, | |
| 1092 | - array(Filesystem::signal_param_path => $this->getHookPath($path)) | |
| 1093 | - ); | |
| 1094 | - } | |
| 1095 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); | |
| 1096 | -			if ($storage) { | |
| 1097 | - $result = $storage->hash($type, $internalPath, $raw); | |
| 1098 | - return $result; | |
| 1099 | - } | |
| 1100 | - } | |
| 1101 | - return null; | |
| 1102 | - } | |
| 1103 | - | |
| 1104 | - /** | |
| 1105 | - * @param string $path | |
| 1106 | - * @return mixed | |
| 1107 | - * @throws \OCP\Files\InvalidPathException | |
| 1108 | - */ | |
| 1109 | -	public function free_space($path = '/') { | |
| 1110 | - $this->assertPathLength($path); | |
| 1111 | -		$result = $this->basicOperation('free_space', $path); | |
| 1112 | -		if ($result === null) { | |
| 1113 | - throw new InvalidPathException(); | |
| 1114 | - } | |
| 1115 | - return $result; | |
| 1116 | - } | |
| 1117 | - | |
| 1118 | - /** | |
| 1119 | - * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage | |
| 1120 | - * | |
| 1121 | - * @param string $operation | |
| 1122 | - * @param string $path | |
| 1123 | - * @param array $hooks (optional) | |
| 1124 | - * @param mixed $extraParam (optional) | |
| 1125 | - * @return mixed | |
| 1126 | - * @throws \Exception | |
| 1127 | - * | |
| 1128 | - * This method takes requests for basic filesystem functions (e.g. reading & writing | |
| 1129 | - * files), processes hooks and proxies, sanitises paths, and finally passes them on to | |
| 1130 | - * \OC\Files\Storage\Storage for delegation to a storage backend for execution | |
| 1131 | - */ | |
| 1132 | -	private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { | |
| 1133 | - $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; | |
| 1134 | - $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); | |
| 1135 | - if (Filesystem::isValidPath($path) | |
| 1136 | - and !Filesystem::isFileBlacklisted($path) | |
| 1137 | -		) { | |
| 1138 | - $path = $this->getRelativePath($absolutePath); | |
| 1139 | -			if ($path == null) { | |
| 1140 | - return false; | |
| 1141 | - } | |
| 1142 | - | |
| 1143 | -			if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { | |
| 1144 | - // always a shared lock during pre-hooks so the hook can read the file | |
| 1145 | - $this->lockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1146 | - } | |
| 1147 | - | |
| 1148 | - $run = $this->runHooks($hooks, $path); | |
| 1149 | - /** @var \OC\Files\Storage\Storage $storage */ | |
| 1150 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); | |
| 1151 | -			if ($run and $storage) { | |
| 1152 | -				if (in_array('write', $hooks) || in_array('delete', $hooks)) { | |
| 1153 | - $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 1154 | - } | |
| 1155 | -				try { | |
| 1156 | -					if (!is_null($extraParam)) { | |
| 1157 | - $result = $storage->$operation($internalPath, $extraParam); | |
| 1158 | -					} else { | |
| 1159 | - $result = $storage->$operation($internalPath); | |
| 1160 | - } | |
| 1161 | -				} catch (\Exception $e) { | |
| 1162 | -					if (in_array('write', $hooks) || in_array('delete', $hooks)) { | |
| 1163 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 1164 | -					} else if (in_array('read', $hooks)) { | |
| 1165 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1166 | - } | |
| 1167 | - throw $e; | |
| 1168 | - } | |
| 1169 | - | |
| 1170 | -				if ($result && in_array('delete', $hooks) and $result) { | |
| 1171 | - $this->removeUpdate($storage, $internalPath); | |
| 1172 | - } | |
| 1173 | -				if ($result && in_array('write', $hooks) and $operation !== 'fopen') { | |
| 1174 | - $this->writeUpdate($storage, $internalPath); | |
| 1175 | - } | |
| 1176 | -				if ($result && in_array('touch', $hooks)) { | |
| 1177 | - $this->writeUpdate($storage, $internalPath, $extraParam); | |
| 1178 | - } | |
| 1179 | - | |
| 1180 | -				if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { | |
| 1181 | - $this->changeLock($path, ILockingProvider::LOCK_SHARED); | |
| 1182 | - } | |
| 1183 | - | |
| 1184 | - $unlockLater = false; | |
| 1185 | -				if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { | |
| 1186 | - $unlockLater = true; | |
| 1187 | - // make sure our unlocking callback will still be called if connection is aborted | |
| 1188 | - ignore_user_abort(true); | |
| 1189 | -					$result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { | |
| 1190 | -						if (in_array('write', $hooks)) { | |
| 1191 | - $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 1192 | -						} else if (in_array('read', $hooks)) { | |
| 1193 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1194 | - } | |
| 1195 | - }); | |
| 1196 | - } | |
| 1197 | - | |
| 1198 | -				if ($this->shouldEmitHooks($path) && $result !== false) { | |
| 1199 | -					if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open | |
| 1200 | - $this->runHooks($hooks, $path, true); | |
| 1201 | - } | |
| 1202 | - } | |
| 1203 | - | |
| 1204 | - if (!$unlockLater | |
| 1205 | -					&& (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) | |
| 1206 | -				) { | |
| 1207 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1208 | - } | |
| 1209 | - return $result; | |
| 1210 | -			} else { | |
| 1211 | - $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1212 | - } | |
| 1213 | - } | |
| 1214 | - return null; | |
| 1215 | - } | |
| 1216 | - | |
| 1217 | - /** | |
| 1218 | - * get the path relative to the default root for hook usage | |
| 1219 | - * | |
| 1220 | - * @param string $path | |
| 1221 | - * @return string | |
| 1222 | - */ | |
| 1223 | -	private function getHookPath($path) { | |
| 1224 | -		if (!Filesystem::getView()) { | |
| 1225 | - return $path; | |
| 1226 | - } | |
| 1227 | - return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); | |
| 1228 | - } | |
| 1229 | - | |
| 1230 | -	private function shouldEmitHooks($path = '') { | |
| 1231 | -		if ($path && Cache\Scanner::isPartialFile($path)) { | |
| 1232 | - return false; | |
| 1233 | - } | |
| 1234 | -		if (!Filesystem::$loaded) { | |
| 1235 | - return false; | |
| 1236 | - } | |
| 1237 | - $defaultRoot = Filesystem::getRoot(); | |
| 1238 | -		if ($defaultRoot === null) { | |
| 1239 | - return false; | |
| 1240 | - } | |
| 1241 | -		if ($this->fakeRoot === $defaultRoot) { | |
| 1242 | - return true; | |
| 1243 | - } | |
| 1244 | - $fullPath = $this->getAbsolutePath($path); | |
| 1245 | - | |
| 1246 | -		if ($fullPath === $defaultRoot) { | |
| 1247 | - return true; | |
| 1248 | - } | |
| 1249 | - | |
| 1250 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); | |
| 1251 | - } | |
| 1252 | - | |
| 1253 | - /** | |
| 1254 | - * @param string[] $hooks | |
| 1255 | - * @param string $path | |
| 1256 | - * @param bool $post | |
| 1257 | - * @return bool | |
| 1258 | - */ | |
| 1259 | -	private function runHooks($hooks, $path, $post = false) { | |
| 1260 | - $relativePath = $path; | |
| 1261 | - $path = $this->getHookPath($path); | |
| 1262 | - $prefix = ($post) ? 'post_' : ''; | |
| 1263 | - $run = true; | |
| 1264 | -		if ($this->shouldEmitHooks($relativePath)) { | |
| 1265 | -			foreach ($hooks as $hook) { | |
| 1266 | -				if ($hook != 'read') { | |
| 1267 | - \OC_Hook::emit( | |
| 1268 | - Filesystem::CLASSNAME, | |
| 1269 | - $prefix . $hook, | |
| 1270 | - array( | |
| 1271 | - Filesystem::signal_param_run => &$run, | |
| 1272 | - Filesystem::signal_param_path => $path | |
| 1273 | - ) | |
| 1274 | - ); | |
| 1275 | -				} elseif (!$post) { | |
| 1276 | - \OC_Hook::emit( | |
| 1277 | - Filesystem::CLASSNAME, | |
| 1278 | - $prefix . $hook, | |
| 1279 | - array( | |
| 1280 | - Filesystem::signal_param_path => $path | |
| 1281 | - ) | |
| 1282 | - ); | |
| 1283 | - } | |
| 1284 | - } | |
| 1285 | - } | |
| 1286 | - return $run; | |
| 1287 | - } | |
| 1288 | - | |
| 1289 | - /** | |
| 1290 | - * check if a file or folder has been updated since $time | |
| 1291 | - * | |
| 1292 | - * @param string $path | |
| 1293 | - * @param int $time | |
| 1294 | - * @return bool | |
| 1295 | - */ | |
| 1296 | -	public function hasUpdated($path, $time) { | |
| 1297 | -		return $this->basicOperation('hasUpdated', $path, array(), $time); | |
| 1298 | - } | |
| 1299 | - | |
| 1300 | - /** | |
| 1301 | - * @param string $ownerId | |
| 1302 | - * @return \OC\User\User | |
| 1303 | - */ | |
| 1304 | -	private function getUserObjectForOwner($ownerId) { | |
| 1305 | - $owner = $this->userManager->get($ownerId); | |
| 1306 | -		if ($owner instanceof IUser) { | |
| 1307 | - return $owner; | |
| 1308 | -		} else { | |
| 1309 | - return new User($ownerId, null); | |
| 1310 | - } | |
| 1311 | - } | |
| 1312 | - | |
| 1313 | - /** | |
| 1314 | - * Get file info from cache | |
| 1315 | - * | |
| 1316 | - * If the file is not in cached it will be scanned | |
| 1317 | - * If the file has changed on storage the cache will be updated | |
| 1318 | - * | |
| 1319 | - * @param \OC\Files\Storage\Storage $storage | |
| 1320 | - * @param string $internalPath | |
| 1321 | - * @param string $relativePath | |
| 1322 | - * @return array|bool | |
| 1323 | - */ | |
| 1324 | -	private function getCacheEntry($storage, $internalPath, $relativePath) { | |
| 1325 | - $cache = $storage->getCache($internalPath); | |
| 1326 | - $data = $cache->get($internalPath); | |
| 1327 | - $watcher = $storage->getWatcher($internalPath); | |
| 1328 | - | |
| 1329 | -		try { | |
| 1330 | - // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data | |
| 1331 | -			if (!$data || $data['size'] === -1) { | |
| 1332 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1333 | -				if (!$storage->file_exists($internalPath)) { | |
| 1334 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1335 | - return false; | |
| 1336 | - } | |
| 1337 | - $scanner = $storage->getScanner($internalPath); | |
| 1338 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); | |
| 1339 | - $data = $cache->get($internalPath); | |
| 1340 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1341 | -			} else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { | |
| 1342 | - $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1343 | - $watcher->update($internalPath, $data); | |
| 1344 | - $storage->getPropagator()->propagateChange($internalPath, time()); | |
| 1345 | - $data = $cache->get($internalPath); | |
| 1346 | - $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1347 | - } | |
| 1348 | -		} catch (LockedException $e) { | |
| 1349 | - // if the file is locked we just use the old cache info | |
| 1350 | - } | |
| 1351 | - | |
| 1352 | - return $data; | |
| 1353 | - } | |
| 1354 | - | |
| 1355 | - /** | |
| 1356 | - * get the filesystem info | |
| 1357 | - * | |
| 1358 | - * @param string $path | |
| 1359 | - * @param boolean|string $includeMountPoints true to add mountpoint sizes, | |
| 1360 | - * 'ext' to add only ext storage mount point sizes. Defaults to true. | |
| 1361 | - * defaults to true | |
| 1362 | - * @return \OC\Files\FileInfo|false False if file does not exist | |
| 1363 | - */ | |
| 1364 | -	public function getFileInfo($path, $includeMountPoints = true) { | |
| 1365 | - $this->assertPathLength($path); | |
| 1366 | -		if (!Filesystem::isValidPath($path)) { | |
| 1367 | - return false; | |
| 1368 | - } | |
| 1369 | -		if (Cache\Scanner::isPartialFile($path)) { | |
| 1370 | - return $this->getPartFileInfo($path); | |
| 1371 | - } | |
| 1372 | - $relativePath = $path; | |
| 1373 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); | |
| 1374 | - | |
| 1375 | - $mount = Filesystem::getMountManager()->find($path); | |
| 1376 | - $storage = $mount->getStorage(); | |
| 1377 | - $internalPath = $mount->getInternalPath($path); | |
| 1378 | -		if ($storage) { | |
| 1379 | - $data = $this->getCacheEntry($storage, $internalPath, $relativePath); | |
| 1380 | - | |
| 1381 | -			if (!$data instanceof ICacheEntry) { | |
| 1382 | - return false; | |
| 1383 | - } | |
| 1384 | - | |
| 1385 | -			if ($mount instanceof MoveableMount && $internalPath === '') { | |
| 1386 | - $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; | |
| 1387 | - } | |
| 1388 | - | |
| 1389 | - $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); | |
| 1390 | - $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); | |
| 1391 | - | |
| 1392 | -			if ($data and isset($data['fileid'])) { | |
| 1393 | -				if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { | |
| 1394 | - //add the sizes of other mount points to the folder | |
| 1395 | - $extOnly = ($includeMountPoints === 'ext'); | |
| 1396 | - $mounts = Filesystem::getMountManager()->findIn($path); | |
| 1397 | -					$info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { | |
| 1398 | - $subStorage = $mount->getStorage(); | |
| 1399 | - return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); | |
| 1400 | - })); | |
| 1401 | - } | |
| 1402 | - } | |
| 1403 | - | |
| 1404 | - return $info; | |
| 1405 | - } | |
| 1406 | - | |
| 1407 | - return false; | |
| 1408 | - } | |
| 1409 | - | |
| 1410 | - /** | |
| 1411 | - * get the content of a directory | |
| 1412 | - * | |
| 1413 | - * @param string $directory path under datadirectory | |
| 1414 | - * @param string $mimetype_filter limit returned content to this mimetype or mimepart | |
| 1415 | - * @return FileInfo[] | |
| 1416 | - */ | |
| 1417 | -	public function getDirectoryContent($directory, $mimetype_filter = '') { | |
| 1418 | - $this->assertPathLength($directory); | |
| 1419 | -		if (!Filesystem::isValidPath($directory)) { | |
| 1420 | - return []; | |
| 1421 | - } | |
| 1422 | - $path = $this->getAbsolutePath($directory); | |
| 1423 | - $path = Filesystem::normalizePath($path); | |
| 1424 | - $mount = $this->getMount($directory); | |
| 1425 | - $storage = $mount->getStorage(); | |
| 1426 | - $internalPath = $mount->getInternalPath($path); | |
| 1427 | -		if ($storage) { | |
| 1428 | - $cache = $storage->getCache($internalPath); | |
| 1429 | - $user = \OC_User::getUser(); | |
| 1430 | - | |
| 1431 | - $data = $this->getCacheEntry($storage, $internalPath, $directory); | |
| 1432 | - | |
| 1433 | -			if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { | |
| 1434 | - return []; | |
| 1435 | - } | |
| 1436 | - | |
| 1437 | - $folderId = $data['fileid']; | |
| 1438 | - $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter | |
| 1439 | - | |
| 1440 | - $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); | |
| 1441 | - | |
| 1442 | -			$fileNames = array_map(function(ICacheEntry $content) { | |
| 1443 | - return $content->getName(); | |
| 1444 | - }, $contents); | |
| 1445 | - /** | |
| 1446 | - * @var \OC\Files\FileInfo[] $fileInfos | |
| 1447 | - */ | |
| 1448 | -			$fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { | |
| 1449 | -				if ($sharingDisabled) { | |
| 1450 | - $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; | |
| 1451 | - } | |
| 1452 | - $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); | |
| 1453 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); | |
| 1454 | - }, $contents); | |
| 1455 | - $files = array_combine($fileNames, $fileInfos); | |
| 1456 | - | |
| 1457 | - //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders | |
| 1458 | - $mounts = Filesystem::getMountManager()->findIn($path); | |
| 1459 | - $dirLength = strlen($path); | |
| 1460 | -			foreach ($mounts as $mount) { | |
| 1461 | - $mountPoint = $mount->getMountPoint(); | |
| 1462 | - $subStorage = $mount->getStorage(); | |
| 1463 | -				if ($subStorage) { | |
| 1464 | -					$subCache = $subStorage->getCache(''); | |
| 1465 | - | |
| 1466 | -					$rootEntry = $subCache->get(''); | |
| 1467 | -					if (!$rootEntry) { | |
| 1468 | -						$subScanner = $subStorage->getScanner(''); | |
| 1469 | -						try { | |
| 1470 | -							$subScanner->scanFile(''); | |
| 1471 | -						} catch (\OCP\Files\StorageNotAvailableException $e) { | |
| 1472 | - continue; | |
| 1473 | -						} catch (\OCP\Files\StorageInvalidException $e) { | |
| 1474 | - continue; | |
| 1475 | -						} catch (\Exception $e) { | |
| 1476 | - // sometimes when the storage is not available it can be any exception | |
| 1477 | - \OCP\Util::writeLog( | |
| 1478 | - 'core', | |
| 1479 | - 'Exception while scanning storage "' . $subStorage->getId() . '": ' . | |
| 1480 | - get_class($e) . ': ' . $e->getMessage(), | |
| 1481 | - \OCP\Util::ERROR | |
| 1482 | - ); | |
| 1483 | - continue; | |
| 1484 | - } | |
| 1485 | -						$rootEntry = $subCache->get(''); | |
| 1486 | - } | |
| 1487 | - | |
| 1488 | -					if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { | |
| 1489 | - $relativePath = trim(substr($mountPoint, $dirLength), '/'); | |
| 1490 | -						if ($pos = strpos($relativePath, '/')) { | |
| 1491 | - //mountpoint inside subfolder add size to the correct folder | |
| 1492 | - $entryName = substr($relativePath, 0, $pos); | |
| 1493 | -							foreach ($files as &$entry) { | |
| 1494 | -								if ($entry->getName() === $entryName) { | |
| 1495 | - $entry->addSubEntry($rootEntry, $mountPoint); | |
| 1496 | - } | |
| 1497 | - } | |
| 1498 | -						} else { //mountpoint in this folder, add an entry for it | |
| 1499 | - $rootEntry['name'] = $relativePath; | |
| 1500 | - $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; | |
| 1501 | - $permissions = $rootEntry['permissions']; | |
| 1502 | - // do not allow renaming/deleting the mount point if they are not shared files/folders | |
| 1503 | - // for shared files/folders we use the permissions given by the owner | |
| 1504 | -							if ($mount instanceof MoveableMount) { | |
| 1505 | - $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; | |
| 1506 | -							} else { | |
| 1507 | - $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); | |
| 1508 | - } | |
| 1509 | - | |
| 1510 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ | |
| 1511 | - | |
| 1512 | - // if sharing was disabled for the user we remove the share permissions | |
| 1513 | -							if (\OCP\Util::isSharingDisabledForUser()) { | |
| 1514 | - $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; | |
| 1515 | - } | |
| 1516 | - | |
| 1517 | -							$owner = $this->getUserObjectForOwner($subStorage->getOwner('')); | |
| 1518 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); | |
| 1519 | - } | |
| 1520 | - } | |
| 1521 | - } | |
| 1522 | - } | |
| 1523 | - | |
| 1524 | -			if ($mimetype_filter) { | |
| 1525 | -				$files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { | |
| 1526 | -					if (strpos($mimetype_filter, '/')) { | |
| 1527 | - return $file->getMimetype() === $mimetype_filter; | |
| 1528 | -					} else { | |
| 1529 | - return $file->getMimePart() === $mimetype_filter; | |
| 1530 | - } | |
| 1531 | - }); | |
| 1532 | - } | |
| 1533 | - | |
| 1534 | - return array_values($files); | |
| 1535 | -		} else { | |
| 1536 | - return []; | |
| 1537 | - } | |
| 1538 | - } | |
| 1539 | - | |
| 1540 | - /** | |
| 1541 | - * change file metadata | |
| 1542 | - * | |
| 1543 | - * @param string $path | |
| 1544 | - * @param array|\OCP\Files\FileInfo $data | |
| 1545 | - * @return int | |
| 1546 | - * | |
| 1547 | - * returns the fileid of the updated file | |
| 1548 | - */ | |
| 1549 | -	public function putFileInfo($path, $data) { | |
| 1550 | - $this->assertPathLength($path); | |
| 1551 | -		if ($data instanceof FileInfo) { | |
| 1552 | - $data = $data->getData(); | |
| 1553 | - } | |
| 1554 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); | |
| 1555 | - /** | |
| 1556 | - * @var \OC\Files\Storage\Storage $storage | |
| 1557 | - * @var string $internalPath | |
| 1558 | - */ | |
| 1559 | - list($storage, $internalPath) = Filesystem::resolvePath($path); | |
| 1560 | -		if ($storage) { | |
| 1561 | - $cache = $storage->getCache($path); | |
| 1562 | - | |
| 1563 | -			if (!$cache->inCache($internalPath)) { | |
| 1564 | - $scanner = $storage->getScanner($internalPath); | |
| 1565 | - $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); | |
| 1566 | - } | |
| 1567 | - | |
| 1568 | - return $cache->put($internalPath, $data); | |
| 1569 | -		} else { | |
| 1570 | - return -1; | |
| 1571 | - } | |
| 1572 | - } | |
| 1573 | - | |
| 1574 | - /** | |
| 1575 | - * search for files with the name matching $query | |
| 1576 | - * | |
| 1577 | - * @param string $query | |
| 1578 | - * @return FileInfo[] | |
| 1579 | - */ | |
| 1580 | -	public function search($query) { | |
| 1581 | -		return $this->searchCommon('search', array('%' . $query . '%')); | |
| 1582 | - } | |
| 1583 | - | |
| 1584 | - /** | |
| 1585 | - * search for files with the name matching $query | |
| 1586 | - * | |
| 1587 | - * @param string $query | |
| 1588 | - * @return FileInfo[] | |
| 1589 | - */ | |
| 1590 | -	public function searchRaw($query) { | |
| 1591 | -		return $this->searchCommon('search', array($query)); | |
| 1592 | - } | |
| 1593 | - | |
| 1594 | - /** | |
| 1595 | - * search for files by mimetype | |
| 1596 | - * | |
| 1597 | - * @param string $mimetype | |
| 1598 | - * @return FileInfo[] | |
| 1599 | - */ | |
| 1600 | -	public function searchByMime($mimetype) { | |
| 1601 | -		return $this->searchCommon('searchByMime', array($mimetype)); | |
| 1602 | - } | |
| 1603 | - | |
| 1604 | - /** | |
| 1605 | - * search for files by tag | |
| 1606 | - * | |
| 1607 | - * @param string|int $tag name or tag id | |
| 1608 | - * @param string $userId owner of the tags | |
| 1609 | - * @return FileInfo[] | |
| 1610 | - */ | |
| 1611 | -	public function searchByTag($tag, $userId) { | |
| 1612 | -		return $this->searchCommon('searchByTag', array($tag, $userId)); | |
| 1613 | - } | |
| 1614 | - | |
| 1615 | - /** | |
| 1616 | - * @param string $method cache method | |
| 1617 | - * @param array $args | |
| 1618 | - * @return FileInfo[] | |
| 1619 | - */ | |
| 1620 | -	private function searchCommon($method, $args) { | |
| 1621 | - $files = array(); | |
| 1622 | - $rootLength = strlen($this->fakeRoot); | |
| 1623 | - | |
| 1624 | -		$mount = $this->getMount(''); | |
| 1625 | - $mountPoint = $mount->getMountPoint(); | |
| 1626 | - $storage = $mount->getStorage(); | |
| 1627 | -		if ($storage) { | |
| 1628 | -			$cache = $storage->getCache(''); | |
| 1629 | - | |
| 1630 | - $results = call_user_func_array(array($cache, $method), $args); | |
| 1631 | -			foreach ($results as $result) { | |
| 1632 | -				if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { | |
| 1633 | - $internalPath = $result['path']; | |
| 1634 | - $path = $mountPoint . $result['path']; | |
| 1635 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); | |
| 1636 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); | |
| 1637 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); | |
| 1638 | - } | |
| 1639 | - } | |
| 1640 | - | |
| 1641 | - $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); | |
| 1642 | -			foreach ($mounts as $mount) { | |
| 1643 | - $mountPoint = $mount->getMountPoint(); | |
| 1644 | - $storage = $mount->getStorage(); | |
| 1645 | -				if ($storage) { | |
| 1646 | -					$cache = $storage->getCache(''); | |
| 1647 | - | |
| 1648 | - $relativeMountPoint = substr($mountPoint, $rootLength); | |
| 1649 | - $results = call_user_func_array(array($cache, $method), $args); | |
| 1650 | -					if ($results) { | |
| 1651 | -						foreach ($results as $result) { | |
| 1652 | - $internalPath = $result['path']; | |
| 1653 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); | |
| 1654 | - $path = rtrim($mountPoint . $internalPath, '/'); | |
| 1655 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); | |
| 1656 | - $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); | |
| 1657 | - } | |
| 1658 | - } | |
| 1659 | - } | |
| 1660 | - } | |
| 1661 | - } | |
| 1662 | - return $files; | |
| 1663 | - } | |
| 1664 | - | |
| 1665 | - /** | |
| 1666 | - * Get the owner for a file or folder | |
| 1667 | - * | |
| 1668 | - * @param string $path | |
| 1669 | - * @return string the user id of the owner | |
| 1670 | - * @throws NotFoundException | |
| 1671 | - */ | |
| 1672 | -	public function getOwner($path) { | |
| 1673 | - $info = $this->getFileInfo($path); | |
| 1674 | -		if (!$info) { | |
| 1675 | - throw new NotFoundException($path . ' not found while trying to get owner'); | |
| 1676 | - } | |
| 1677 | - return $info->getOwner()->getUID(); | |
| 1678 | - } | |
| 1679 | - | |
| 1680 | - /** | |
| 1681 | - * get the ETag for a file or folder | |
| 1682 | - * | |
| 1683 | - * @param string $path | |
| 1684 | - * @return string | |
| 1685 | - */ | |
| 1686 | -	public function getETag($path) { | |
| 1687 | - /** | |
| 1688 | - * @var Storage\Storage $storage | |
| 1689 | - * @var string $internalPath | |
| 1690 | - */ | |
| 1691 | - list($storage, $internalPath) = $this->resolvePath($path); | |
| 1692 | -		if ($storage) { | |
| 1693 | - return $storage->getETag($internalPath); | |
| 1694 | -		} else { | |
| 1695 | - return null; | |
| 1696 | - } | |
| 1697 | - } | |
| 1698 | - | |
| 1699 | - /** | |
| 1700 | - * Get the path of a file by id, relative to the view | |
| 1701 | - * | |
| 1702 | - * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file | |
| 1703 | - * | |
| 1704 | - * @param int $id | |
| 1705 | - * @throws NotFoundException | |
| 1706 | - * @return string | |
| 1707 | - */ | |
| 1708 | -	public function getPath($id) { | |
| 1709 | - $id = (int)$id; | |
| 1710 | - $manager = Filesystem::getMountManager(); | |
| 1711 | - $mounts = $manager->findIn($this->fakeRoot); | |
| 1712 | - $mounts[] = $manager->find($this->fakeRoot); | |
| 1713 | - // reverse the array so we start with the storage this view is in | |
| 1714 | - // which is the most likely to contain the file we're looking for | |
| 1715 | - $mounts = array_reverse($mounts); | |
| 1716 | -		foreach ($mounts as $mount) { | |
| 1717 | - /** | |
| 1718 | - * @var \OC\Files\Mount\MountPoint $mount | |
| 1719 | - */ | |
| 1720 | -			if ($mount->getStorage()) { | |
| 1721 | - $cache = $mount->getStorage()->getCache(); | |
| 1722 | -				if (!$cache) { | |
| 1723 | -					throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); | |
| 1724 | - } | |
| 1725 | - $internalPath = $cache->getPathById($id); | |
| 1726 | -				if (is_string($internalPath)) { | |
| 1727 | - $fullPath = $mount->getMountPoint() . $internalPath; | |
| 1728 | -					if (!is_null($path = $this->getRelativePath($fullPath))) { | |
| 1729 | - return $path; | |
| 1730 | - } | |
| 1731 | - } | |
| 1732 | - } | |
| 1733 | - } | |
| 1734 | -		throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); | |
| 1735 | - } | |
| 1736 | - | |
| 1737 | - /** | |
| 1738 | - * @param string $path | |
| 1739 | - * @throws InvalidPathException | |
| 1740 | - */ | |
| 1741 | -	private function assertPathLength($path) { | |
| 1742 | - $maxLen = min(PHP_MAXPATHLEN, 4000); | |
| 1743 | - // Check for the string length - performed using isset() instead of strlen() | |
| 1744 | - // because isset() is about 5x-40x faster. | |
| 1745 | -		if (isset($path[$maxLen])) { | |
| 1746 | - $pathLen = strlen($path); | |
| 1747 | -			throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); | |
| 1748 | - } | |
| 1749 | - } | |
| 1750 | - | |
| 1751 | - /** | |
| 1752 | - * check if it is allowed to move a mount point to a given target. | |
| 1753 | - * It is not allowed to move a mount point into a different mount point or | |
| 1754 | - * into an already shared folder | |
| 1755 | - * | |
| 1756 | - * @param string $target path | |
| 1757 | - * @return boolean | |
| 1758 | - */ | |
| 1759 | -	private function isTargetAllowed($target) { | |
| 1760 | - | |
| 1761 | - list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); | |
| 1762 | -		if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { | |
| 1763 | -			\OCP\Util::writeLog('files', | |
| 1764 | - 'It is not allowed to move one mount point into another one', | |
| 1765 | - \OCP\Util::DEBUG); | |
| 1766 | - return false; | |
| 1767 | - } | |
| 1768 | - | |
| 1769 | - // note: cannot use the view because the target is already locked | |
| 1770 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); | |
| 1771 | -		if ($fileId === -1) { | |
| 1772 | - // target might not exist, need to check parent instead | |
| 1773 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); | |
| 1774 | - } | |
| 1775 | - | |
| 1776 | - // check if any of the parents were shared by the current owner (include collections) | |
| 1777 | - $shares = \OCP\Share::getItemShared( | |
| 1778 | - 'folder', | |
| 1779 | - $fileId, | |
| 1780 | - \OCP\Share::FORMAT_NONE, | |
| 1781 | - null, | |
| 1782 | - true | |
| 1783 | - ); | |
| 1784 | - | |
| 1785 | -		if (count($shares) > 0) { | |
| 1786 | -			\OCP\Util::writeLog('files', | |
| 1787 | - 'It is not allowed to move one mount point into a shared folder', | |
| 1788 | - \OCP\Util::DEBUG); | |
| 1789 | - return false; | |
| 1790 | - } | |
| 1791 | - | |
| 1792 | - return true; | |
| 1793 | - } | |
| 1794 | - | |
| 1795 | - /** | |
| 1796 | - * Get a fileinfo object for files that are ignored in the cache (part files) | |
| 1797 | - * | |
| 1798 | - * @param string $path | |
| 1799 | - * @return \OCP\Files\FileInfo | |
| 1800 | - */ | |
| 1801 | -	private function getPartFileInfo($path) { | |
| 1802 | - $mount = $this->getMount($path); | |
| 1803 | - $storage = $mount->getStorage(); | |
| 1804 | - $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); | |
| 1805 | - $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); | |
| 1806 | - return new FileInfo( | |
| 1807 | - $this->getAbsolutePath($path), | |
| 1808 | - $storage, | |
| 1809 | - $internalPath, | |
| 1810 | - [ | |
| 1811 | - 'fileid' => null, | |
| 1812 | - 'mimetype' => $storage->getMimeType($internalPath), | |
| 1813 | - 'name' => basename($path), | |
| 1814 | - 'etag' => null, | |
| 1815 | - 'size' => $storage->filesize($internalPath), | |
| 1816 | - 'mtime' => $storage->filemtime($internalPath), | |
| 1817 | - 'encrypted' => false, | |
| 1818 | - 'permissions' => \OCP\Constants::PERMISSION_ALL | |
| 1819 | - ], | |
| 1820 | - $mount, | |
| 1821 | - $owner | |
| 1822 | - ); | |
| 1823 | - } | |
| 1824 | - | |
| 1825 | - /** | |
| 1826 | - * @param string $path | |
| 1827 | - * @param string $fileName | |
| 1828 | - * @throws InvalidPathException | |
| 1829 | - */ | |
| 1830 | -	public function verifyPath($path, $fileName) { | |
| 1831 | -		try { | |
| 1832 | - /** @type \OCP\Files\Storage $storage */ | |
| 1833 | - list($storage, $internalPath) = $this->resolvePath($path); | |
| 1834 | - $storage->verifyPath($internalPath, $fileName); | |
| 1835 | -		} catch (ReservedWordException $ex) { | |
| 1836 | -			$l = \OC::$server->getL10N('lib'); | |
| 1837 | -			throw new InvalidPathException($l->t('File name is a reserved word')); | |
| 1838 | -		} catch (InvalidCharacterInPathException $ex) { | |
| 1839 | -			$l = \OC::$server->getL10N('lib'); | |
| 1840 | -			throw new InvalidPathException($l->t('File name contains at least one invalid character')); | |
| 1841 | -		} catch (FileNameTooLongException $ex) { | |
| 1842 | -			$l = \OC::$server->getL10N('lib'); | |
| 1843 | -			throw new InvalidPathException($l->t('File name is too long')); | |
| 1844 | -		} catch (InvalidDirectoryException $ex) { | |
| 1845 | -			$l = \OC::$server->getL10N('lib'); | |
| 1846 | -			throw new InvalidPathException($l->t('Dot files are not allowed')); | |
| 1847 | -		} catch (EmptyFileNameException $ex) { | |
| 1848 | -			$l = \OC::$server->getL10N('lib'); | |
| 1849 | -			throw new InvalidPathException($l->t('Empty filename is not allowed')); | |
| 1850 | - } | |
| 1851 | - } | |
| 1852 | - | |
| 1853 | - /** | |
| 1854 | - * get all parent folders of $path | |
| 1855 | - * | |
| 1856 | - * @param string $path | |
| 1857 | - * @return string[] | |
| 1858 | - */ | |
| 1859 | -	private function getParents($path) { | |
| 1860 | - $path = trim($path, '/'); | |
| 1861 | -		if (!$path) { | |
| 1862 | - return []; | |
| 1863 | - } | |
| 1864 | - | |
| 1865 | -		$parts = explode('/', $path); | |
| 1866 | - | |
| 1867 | - // remove the single file | |
| 1868 | - array_pop($parts); | |
| 1869 | -		$result = array('/'); | |
| 1870 | - $resultPath = ''; | |
| 1871 | -		foreach ($parts as $part) { | |
| 1872 | -			if ($part) { | |
| 1873 | - $resultPath .= '/' . $part; | |
| 1874 | - $result[] = $resultPath; | |
| 1875 | - } | |
| 1876 | - } | |
| 1877 | - return $result; | |
| 1878 | - } | |
| 1879 | - | |
| 1880 | - /** | |
| 1881 | - * Returns the mount point for which to lock | |
| 1882 | - * | |
| 1883 | - * @param string $absolutePath absolute path | |
| 1884 | - * @param bool $useParentMount true to return parent mount instead of whatever | |
| 1885 | - * is mounted directly on the given path, false otherwise | |
| 1886 | - * @return \OC\Files\Mount\MountPoint mount point for which to apply locks | |
| 1887 | - */ | |
| 1888 | -	private function getMountForLock($absolutePath, $useParentMount = false) { | |
| 1889 | - $results = []; | |
| 1890 | - $mount = Filesystem::getMountManager()->find($absolutePath); | |
| 1891 | -		if (!$mount) { | |
| 1892 | - return $results; | |
| 1893 | - } | |
| 1894 | - | |
| 1895 | -		if ($useParentMount) { | |
| 1896 | - // find out if something is mounted directly on the path | |
| 1897 | - $internalPath = $mount->getInternalPath($absolutePath); | |
| 1898 | -			if ($internalPath === '') { | |
| 1899 | - // resolve the parent mount instead | |
| 1900 | - $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); | |
| 1901 | - } | |
| 1902 | - } | |
| 1903 | - | |
| 1904 | - return $mount; | |
| 1905 | - } | |
| 1906 | - | |
| 1907 | - /** | |
| 1908 | - * Lock the given path | |
| 1909 | - * | |
| 1910 | - * @param string $path the path of the file to lock, relative to the view | |
| 1911 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 1912 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 1913 | - * | |
| 1914 | - * @return bool False if the path is excluded from locking, true otherwise | |
| 1915 | - * @throws \OCP\Lock\LockedException if the path is already locked | |
| 1916 | - */ | |
| 1917 | -	private function lockPath($path, $type, $lockMountPoint = false) { | |
| 1918 | - $absolutePath = $this->getAbsolutePath($path); | |
| 1919 | - $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 1920 | -		if (!$this->shouldLockFile($absolutePath)) { | |
| 1921 | - return false; | |
| 1922 | - } | |
| 1923 | - | |
| 1924 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); | |
| 1925 | -		if ($mount) { | |
| 1926 | -			try { | |
| 1927 | - $storage = $mount->getStorage(); | |
| 1928 | -				if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { | |
| 1929 | - $storage->acquireLock( | |
| 1930 | - $mount->getInternalPath($absolutePath), | |
| 1931 | - $type, | |
| 1932 | - $this->lockingProvider | |
| 1933 | - ); | |
| 1934 | - } | |
| 1935 | -			} catch (\OCP\Lock\LockedException $e) { | |
| 1936 | - // rethrow with the a human-readable path | |
| 1937 | - throw new \OCP\Lock\LockedException( | |
| 1938 | - $this->getPathRelativeToFiles($absolutePath), | |
| 1939 | - $e | |
| 1940 | - ); | |
| 1941 | - } | |
| 1942 | - } | |
| 1943 | - | |
| 1944 | - return true; | |
| 1945 | - } | |
| 1946 | - | |
| 1947 | - /** | |
| 1948 | - * Change the lock type | |
| 1949 | - * | |
| 1950 | - * @param string $path the path of the file to lock, relative to the view | |
| 1951 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 1952 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 1953 | - * | |
| 1954 | - * @return bool False if the path is excluded from locking, true otherwise | |
| 1955 | - * @throws \OCP\Lock\LockedException if the path is already locked | |
| 1956 | - */ | |
| 1957 | -	public function changeLock($path, $type, $lockMountPoint = false) { | |
| 1958 | - $path = Filesystem::normalizePath($path); | |
| 1959 | - $absolutePath = $this->getAbsolutePath($path); | |
| 1960 | - $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 1961 | -		if (!$this->shouldLockFile($absolutePath)) { | |
| 1962 | - return false; | |
| 1963 | - } | |
| 1964 | - | |
| 1965 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); | |
| 1966 | -		if ($mount) { | |
| 1967 | -			try { | |
| 1968 | - $storage = $mount->getStorage(); | |
| 1969 | -				if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { | |
| 1970 | - $storage->changeLock( | |
| 1971 | - $mount->getInternalPath($absolutePath), | |
| 1972 | - $type, | |
| 1973 | - $this->lockingProvider | |
| 1974 | - ); | |
| 1975 | - } | |
| 1976 | -			} catch (\OCP\Lock\LockedException $e) { | |
| 1977 | -				try { | |
| 1978 | - // rethrow with the a human-readable path | |
| 1979 | - throw new \OCP\Lock\LockedException( | |
| 1980 | - $this->getPathRelativeToFiles($absolutePath), | |
| 1981 | - $e | |
| 1982 | - ); | |
| 1983 | -				} catch (\InvalidArgumentException $e) { | |
| 1984 | - throw new \OCP\Lock\LockedException( | |
| 1985 | - $absolutePath, | |
| 1986 | - $e | |
| 1987 | - ); | |
| 1988 | - } | |
| 1989 | - } | |
| 1990 | - } | |
| 1991 | - | |
| 1992 | - return true; | |
| 1993 | - } | |
| 1994 | - | |
| 1995 | - /** | |
| 1996 | - * Unlock the given path | |
| 1997 | - * | |
| 1998 | - * @param string $path the path of the file to unlock, relative to the view | |
| 1999 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 2000 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 2001 | - * | |
| 2002 | - * @return bool False if the path is excluded from locking, true otherwise | |
| 2003 | - */ | |
| 2004 | -	private function unlockPath($path, $type, $lockMountPoint = false) { | |
| 2005 | - $absolutePath = $this->getAbsolutePath($path); | |
| 2006 | - $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 2007 | -		if (!$this->shouldLockFile($absolutePath)) { | |
| 2008 | - return false; | |
| 2009 | - } | |
| 2010 | - | |
| 2011 | - $mount = $this->getMountForLock($absolutePath, $lockMountPoint); | |
| 2012 | -		if ($mount) { | |
| 2013 | - $storage = $mount->getStorage(); | |
| 2014 | -			if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { | |
| 2015 | - $storage->releaseLock( | |
| 2016 | - $mount->getInternalPath($absolutePath), | |
| 2017 | - $type, | |
| 2018 | - $this->lockingProvider | |
| 2019 | - ); | |
| 2020 | - } | |
| 2021 | - } | |
| 2022 | - | |
| 2023 | - return true; | |
| 2024 | - } | |
| 2025 | - | |
| 2026 | - /** | |
| 2027 | - * Lock a path and all its parents up to the root of the view | |
| 2028 | - * | |
| 2029 | - * @param string $path the path of the file to lock relative to the view | |
| 2030 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 2031 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 2032 | - * | |
| 2033 | - * @return bool False if the path is excluded from locking, true otherwise | |
| 2034 | - */ | |
| 2035 | -	public function lockFile($path, $type, $lockMountPoint = false) { | |
| 2036 | - $absolutePath = $this->getAbsolutePath($path); | |
| 2037 | - $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 2038 | -		if (!$this->shouldLockFile($absolutePath)) { | |
| 2039 | - return false; | |
| 2040 | - } | |
| 2041 | - | |
| 2042 | - $this->lockPath($path, $type, $lockMountPoint); | |
| 2043 | - | |
| 2044 | - $parents = $this->getParents($path); | |
| 2045 | -		foreach ($parents as $parent) { | |
| 2046 | - $this->lockPath($parent, ILockingProvider::LOCK_SHARED); | |
| 2047 | - } | |
| 2048 | - | |
| 2049 | - return true; | |
| 2050 | - } | |
| 2051 | - | |
| 2052 | - /** | |
| 2053 | - * Unlock a path and all its parents up to the root of the view | |
| 2054 | - * | |
| 2055 | - * @param string $path the path of the file to lock relative to the view | |
| 2056 | - * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 2057 | - * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 2058 | - * | |
| 2059 | - * @return bool False if the path is excluded from locking, true otherwise | |
| 2060 | - */ | |
| 2061 | -	public function unlockFile($path, $type, $lockMountPoint = false) { | |
| 2062 | - $absolutePath = $this->getAbsolutePath($path); | |
| 2063 | - $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 2064 | -		if (!$this->shouldLockFile($absolutePath)) { | |
| 2065 | - return false; | |
| 2066 | - } | |
| 2067 | - | |
| 2068 | - $this->unlockPath($path, $type, $lockMountPoint); | |
| 2069 | - | |
| 2070 | - $parents = $this->getParents($path); | |
| 2071 | -		foreach ($parents as $parent) { | |
| 2072 | - $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); | |
| 2073 | - } | |
| 2074 | - | |
| 2075 | - return true; | |
| 2076 | - } | |
| 2077 | - | |
| 2078 | - /** | |
| 2079 | - * Only lock files in data/user/files/ | |
| 2080 | - * | |
| 2081 | - * @param string $path Absolute path to the file/folder we try to (un)lock | |
| 2082 | - * @return bool | |
| 2083 | - */ | |
| 2084 | -	protected function shouldLockFile($path) { | |
| 2085 | - $path = Filesystem::normalizePath($path); | |
| 2086 | - | |
| 2087 | -		$pathSegments = explode('/', $path); | |
| 2088 | -		if (isset($pathSegments[2])) { | |
| 2089 | - // E.g.: /username/files/path-to-file | |
| 2090 | - return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); | |
| 2091 | - } | |
| 2092 | - | |
| 2093 | - return strpos($path, '/appdata_') !== 0; | |
| 2094 | - } | |
| 2095 | - | |
| 2096 | - /** | |
| 2097 | - * Shortens the given absolute path to be relative to | |
| 2098 | - * "$user/files". | |
| 2099 | - * | |
| 2100 | - * @param string $absolutePath absolute path which is under "files" | |
| 2101 | - * | |
| 2102 | - * @return string path relative to "files" with trimmed slashes or null | |
| 2103 | - * if the path was NOT relative to files | |
| 2104 | - * | |
| 2105 | - * @throws \InvalidArgumentException if the given path was not under "files" | |
| 2106 | - * @since 8.1.0 | |
| 2107 | - */ | |
| 2108 | -	public function getPathRelativeToFiles($absolutePath) { | |
| 2109 | - $path = Filesystem::normalizePath($absolutePath); | |
| 2110 | -		$parts = explode('/', trim($path, '/'), 3); | |
| 2111 | - // "$user", "files", "path/to/dir" | |
| 2112 | -		if (!isset($parts[1]) || $parts[1] !== 'files') { | |
| 2113 | - $this->logger->error( | |
| 2114 | - '$absolutePath must be relative to "files", value is "%s"', | |
| 2115 | - [ | |
| 2116 | - $absolutePath | |
| 2117 | - ] | |
| 2118 | - ); | |
| 2119 | -			throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); | |
| 2120 | - } | |
| 2121 | -		if (isset($parts[2])) { | |
| 2122 | - return $parts[2]; | |
| 2123 | - } | |
| 2124 | - return ''; | |
| 2125 | - } | |
| 2126 | - | |
| 2127 | - /** | |
| 2128 | - * @param string $filename | |
| 2129 | - * @return array | |
| 2130 | - * @throws \OC\User\NoUserException | |
| 2131 | - * @throws NotFoundException | |
| 2132 | - */ | |
| 2133 | -	public function getUidAndFilename($filename) { | |
| 2134 | - $info = $this->getFileInfo($filename); | |
| 2135 | -		if (!$info instanceof \OCP\Files\FileInfo) { | |
| 2136 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); | |
| 2137 | - } | |
| 2138 | - $uid = $info->getOwner()->getUID(); | |
| 2139 | -		if ($uid != \OCP\User::getUser()) { | |
| 2140 | - Filesystem::initMountPoints($uid); | |
| 2141 | -			$ownerView = new View('/' . $uid . '/files'); | |
| 2142 | -			try { | |
| 2143 | - $filename = $ownerView->getPath($info['fileid']); | |
| 2144 | -			} catch (NotFoundException $e) { | |
| 2145 | -				throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); | |
| 2146 | - } | |
| 2147 | - } | |
| 2148 | - return [$uid, $filename]; | |
| 2149 | - } | |
| 2150 | - | |
| 2151 | - /** | |
| 2152 | - * Creates parent non-existing folders | |
| 2153 | - * | |
| 2154 | - * @param string $filePath | |
| 2155 | - * @return bool | |
| 2156 | - */ | |
| 2157 | -	private function createParentDirectories($filePath) { | |
| 2158 | -		$directoryParts = explode('/', $filePath); | |
| 2159 | - $directoryParts = array_filter($directoryParts); | |
| 2160 | -		foreach ($directoryParts as $key => $part) { | |
| 2161 | - $currentPathElements = array_slice($directoryParts, 0, $key); | |
| 2162 | -			$currentPath = '/' . implode('/', $currentPathElements); | |
| 2163 | -			if ($this->is_file($currentPath)) { | |
| 2164 | - return false; | |
| 2165 | - } | |
| 2166 | -			if (!$this->file_exists($currentPath)) { | |
| 2167 | - $this->mkdir($currentPath); | |
| 2168 | - } | |
| 2169 | - } | |
| 2170 | - | |
| 2171 | - return true; | |
| 2172 | - } | |
| 85 | + /** @var string */ | |
| 86 | + private $fakeRoot = ''; | |
| 87 | + | |
| 88 | + /** | |
| 89 | + * @var \OCP\Lock\ILockingProvider | |
| 90 | + */ | |
| 91 | + protected $lockingProvider; | |
| 92 | + | |
| 93 | + private $lockingEnabled; | |
| 94 | + | |
| 95 | + private $updaterEnabled = true; | |
| 96 | + | |
| 97 | + /** @var \OC\User\Manager */ | |
| 98 | + private $userManager; | |
| 99 | + | |
| 100 | + /** @var \OCP\ILogger */ | |
| 101 | + private $logger; | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * @param string $root | |
| 105 | + * @throws \Exception If $root contains an invalid path | |
| 106 | + */ | |
| 107 | +    public function __construct($root = '') { | |
| 108 | +        if (is_null($root)) { | |
| 109 | +            throw new \InvalidArgumentException('Root can\'t be null'); | |
| 110 | + } | |
| 111 | +        if (!Filesystem::isValidPath($root)) { | |
| 112 | + throw new \Exception(); | |
| 113 | + } | |
| 114 | + | |
| 115 | + $this->fakeRoot = $root; | |
| 116 | + $this->lockingProvider = \OC::$server->getLockingProvider(); | |
| 117 | + $this->lockingEnabled = !($this->lockingProvider instanceof \OC\Lock\NoopLockingProvider); | |
| 118 | + $this->userManager = \OC::$server->getUserManager(); | |
| 119 | + $this->logger = \OC::$server->getLogger(); | |
| 120 | + } | |
| 121 | + | |
| 122 | +    public function getAbsolutePath($path = '/') { | |
| 123 | +        if ($path === null) { | |
| 124 | + return null; | |
| 125 | + } | |
| 126 | + $this->assertPathLength($path); | |
| 127 | +        if ($path === '') { | |
| 128 | + $path = '/'; | |
| 129 | + } | |
| 130 | +        if ($path[0] !== '/') { | |
| 131 | + $path = '/' . $path; | |
| 132 | + } | |
| 133 | + return $this->fakeRoot . $path; | |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * change the root to a fake root | |
| 138 | + * | |
| 139 | + * @param string $fakeRoot | |
| 140 | + * @return boolean|null | |
| 141 | + */ | |
| 142 | +    public function chroot($fakeRoot) { | |
| 143 | +        if (!$fakeRoot == '') { | |
| 144 | +            if ($fakeRoot[0] !== '/') { | |
| 145 | + $fakeRoot = '/' . $fakeRoot; | |
| 146 | + } | |
| 147 | + } | |
| 148 | + $this->fakeRoot = $fakeRoot; | |
| 149 | + } | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * get the fake root | |
| 153 | + * | |
| 154 | + * @return string | |
| 155 | + */ | |
| 156 | +    public function getRoot() { | |
| 157 | + return $this->fakeRoot; | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * get path relative to the root of the view | |
| 162 | + * | |
| 163 | + * @param string $path | |
| 164 | + * @return string | |
| 165 | + */ | |
| 166 | +    public function getRelativePath($path) { | |
| 167 | + $this->assertPathLength($path); | |
| 168 | +        if ($this->fakeRoot == '') { | |
| 169 | + return $path; | |
| 170 | + } | |
| 171 | + | |
| 172 | +        if (rtrim($path, '/') === rtrim($this->fakeRoot, '/')) { | |
| 173 | + return '/'; | |
| 174 | + } | |
| 175 | + | |
| 176 | + // missing slashes can cause wrong matches! | |
| 177 | + $root = rtrim($this->fakeRoot, '/') . '/'; | |
| 178 | + | |
| 179 | +        if (strpos($path, $root) !== 0) { | |
| 180 | + return null; | |
| 181 | +        } else { | |
| 182 | + $path = substr($path, strlen($this->fakeRoot)); | |
| 183 | +            if (strlen($path) === 0) { | |
| 184 | + return '/'; | |
| 185 | +            } else { | |
| 186 | + return $path; | |
| 187 | + } | |
| 188 | + } | |
| 189 | + } | |
| 190 | + | |
| 191 | + /** | |
| 192 | + * get the mountpoint of the storage object for a path | |
| 193 | + * ( note: because a storage is not always mounted inside the fakeroot, the | |
| 194 | + * returned mountpoint is relative to the absolute root of the filesystem | |
| 195 | + * and does not take the chroot into account ) | |
| 196 | + * | |
| 197 | + * @param string $path | |
| 198 | + * @return string | |
| 199 | + */ | |
| 200 | +    public function getMountPoint($path) { | |
| 201 | + return Filesystem::getMountPoint($this->getAbsolutePath($path)); | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * get the mountpoint of the storage object for a path | |
| 206 | + * ( note: because a storage is not always mounted inside the fakeroot, the | |
| 207 | + * returned mountpoint is relative to the absolute root of the filesystem | |
| 208 | + * and does not take the chroot into account ) | |
| 209 | + * | |
| 210 | + * @param string $path | |
| 211 | + * @return \OCP\Files\Mount\IMountPoint | |
| 212 | + */ | |
| 213 | +    public function getMount($path) { | |
| 214 | + return Filesystem::getMountManager()->find($this->getAbsolutePath($path)); | |
| 215 | + } | |
| 216 | + | |
| 217 | + /** | |
| 218 | + * resolve a path to a storage and internal path | |
| 219 | + * | |
| 220 | + * @param string $path | |
| 221 | + * @return array an array consisting of the storage and the internal path | |
| 222 | + */ | |
| 223 | +    public function resolvePath($path) { | |
| 224 | + $a = $this->getAbsolutePath($path); | |
| 225 | + $p = Filesystem::normalizePath($a); | |
| 226 | + return Filesystem::resolvePath($p); | |
| 227 | + } | |
| 228 | + | |
| 229 | + /** | |
| 230 | + * return the path to a local version of the file | |
| 231 | + * we need this because we can't know if a file is stored local or not from | |
| 232 | + * outside the filestorage and for some purposes a local file is needed | |
| 233 | + * | |
| 234 | + * @param string $path | |
| 235 | + * @return string | |
| 236 | + */ | |
| 237 | +    public function getLocalFile($path) { | |
| 238 | + $parent = substr($path, 0, strrpos($path, '/')); | |
| 239 | + $path = $this->getAbsolutePath($path); | |
| 240 | + list($storage, $internalPath) = Filesystem::resolvePath($path); | |
| 241 | +        if (Filesystem::isValidPath($parent) and $storage) { | |
| 242 | + return $storage->getLocalFile($internalPath); | |
| 243 | +        } else { | |
| 244 | + return null; | |
| 245 | + } | |
| 246 | + } | |
| 247 | + | |
| 248 | + /** | |
| 249 | + * @param string $path | |
| 250 | + * @return string | |
| 251 | + */ | |
| 252 | +    public function getLocalFolder($path) { | |
| 253 | + $parent = substr($path, 0, strrpos($path, '/')); | |
| 254 | + $path = $this->getAbsolutePath($path); | |
| 255 | + list($storage, $internalPath) = Filesystem::resolvePath($path); | |
| 256 | +        if (Filesystem::isValidPath($parent) and $storage) { | |
| 257 | + return $storage->getLocalFolder($internalPath); | |
| 258 | +        } else { | |
| 259 | + return null; | |
| 260 | + } | |
| 261 | + } | |
| 262 | + | |
| 263 | + /** | |
| 264 | + * the following functions operate with arguments and return values identical | |
| 265 | + * to those of their PHP built-in equivalents. Mostly they are merely wrappers | |
| 266 | + * for \OC\Files\Storage\Storage via basicOperation(). | |
| 267 | + */ | |
| 268 | +    public function mkdir($path) { | |
| 269 | +        return $this->basicOperation('mkdir', $path, array('create', 'write')); | |
| 270 | + } | |
| 271 | + | |
| 272 | + /** | |
| 273 | + * remove mount point | |
| 274 | + * | |
| 275 | + * @param \OC\Files\Mount\MoveableMount $mount | |
| 276 | + * @param string $path relative to data/ | |
| 277 | + * @return boolean | |
| 278 | + */ | |
| 279 | +    protected function removeMount($mount, $path) { | |
| 280 | +        if ($mount instanceof MoveableMount) { | |
| 281 | + // cut of /user/files to get the relative path to data/user/files | |
| 282 | +            $pathParts = explode('/', $path, 4); | |
| 283 | + $relPath = '/' . $pathParts[3]; | |
| 284 | + $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); | |
| 285 | + \OC_Hook::emit( | |
| 286 | + Filesystem::CLASSNAME, "umount", | |
| 287 | + array(Filesystem::signal_param_path => $relPath) | |
| 288 | + ); | |
| 289 | + $this->changeLock($relPath, ILockingProvider::LOCK_EXCLUSIVE, true); | |
| 290 | + $result = $mount->removeMount(); | |
| 291 | + $this->changeLock($relPath, ILockingProvider::LOCK_SHARED, true); | |
| 292 | +            if ($result) { | |
| 293 | + \OC_Hook::emit( | |
| 294 | + Filesystem::CLASSNAME, "post_umount", | |
| 295 | + array(Filesystem::signal_param_path => $relPath) | |
| 296 | + ); | |
| 297 | + } | |
| 298 | + $this->unlockFile($relPath, ILockingProvider::LOCK_SHARED, true); | |
| 299 | + return $result; | |
| 300 | +        } else { | |
| 301 | + // do not allow deleting the storage's root / the mount point | |
| 302 | + // because for some storages it might delete the whole contents | |
| 303 | + // but isn't supposed to work that way | |
| 304 | + return false; | |
| 305 | + } | |
| 306 | + } | |
| 307 | + | |
| 308 | +    public function disableCacheUpdate() { | |
| 309 | + $this->updaterEnabled = false; | |
| 310 | + } | |
| 311 | + | |
| 312 | +    public function enableCacheUpdate() { | |
| 313 | + $this->updaterEnabled = true; | |
| 314 | + } | |
| 315 | + | |
| 316 | +    protected function writeUpdate(Storage $storage, $internalPath, $time = null) { | |
| 317 | +        if ($this->updaterEnabled) { | |
| 318 | +            if (is_null($time)) { | |
| 319 | + $time = time(); | |
| 320 | + } | |
| 321 | + $storage->getUpdater()->update($internalPath, $time); | |
| 322 | + } | |
| 323 | + } | |
| 324 | + | |
| 325 | +    protected function removeUpdate(Storage $storage, $internalPath) { | |
| 326 | +        if ($this->updaterEnabled) { | |
| 327 | + $storage->getUpdater()->remove($internalPath); | |
| 328 | + } | |
| 329 | + } | |
| 330 | + | |
| 331 | +    protected function renameUpdate(Storage $sourceStorage, Storage $targetStorage, $sourceInternalPath, $targetInternalPath) { | |
| 332 | +        if ($this->updaterEnabled) { | |
| 333 | + $targetStorage->getUpdater()->renameFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath); | |
| 334 | + } | |
| 335 | + } | |
| 336 | + | |
| 337 | + /** | |
| 338 | + * @param string $path | |
| 339 | + * @return bool|mixed | |
| 340 | + */ | |
| 341 | +    public function rmdir($path) { | |
| 342 | + $absolutePath = $this->getAbsolutePath($path); | |
| 343 | + $mount = Filesystem::getMountManager()->find($absolutePath); | |
| 344 | +        if ($mount->getInternalPath($absolutePath) === '') { | |
| 345 | + return $this->removeMount($mount, $absolutePath); | |
| 346 | + } | |
| 347 | +        if ($this->is_dir($path)) { | |
| 348 | +            $result = $this->basicOperation('rmdir', $path, array('delete')); | |
| 349 | +        } else { | |
| 350 | + $result = false; | |
| 351 | + } | |
| 352 | + | |
| 353 | +        if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete | |
| 354 | + $storage = $mount->getStorage(); | |
| 355 | + $internalPath = $mount->getInternalPath($absolutePath); | |
| 356 | + $storage->getUpdater()->remove($internalPath); | |
| 357 | + } | |
| 358 | + return $result; | |
| 359 | + } | |
| 360 | + | |
| 361 | + /** | |
| 362 | + * @param string $path | |
| 363 | + * @return resource | |
| 364 | + */ | |
| 365 | +    public function opendir($path) { | |
| 366 | +        return $this->basicOperation('opendir', $path, array('read')); | |
| 367 | + } | |
| 368 | + | |
| 369 | + /** | |
| 370 | + * @param $handle | |
| 371 | + * @return mixed | |
| 372 | + */ | |
| 373 | +    public function readdir($handle) { | |
| 374 | +        $fsLocal = new Storage\Local(array('datadir' => '/')); | |
| 375 | + return $fsLocal->readdir($handle); | |
| 376 | + } | |
| 377 | + | |
| 378 | + /** | |
| 379 | + * @param string $path | |
| 380 | + * @return bool|mixed | |
| 381 | + */ | |
| 382 | +    public function is_dir($path) { | |
| 383 | +        if ($path == '/') { | |
| 384 | + return true; | |
| 385 | + } | |
| 386 | +        return $this->basicOperation('is_dir', $path); | |
| 387 | + } | |
| 388 | + | |
| 389 | + /** | |
| 390 | + * @param string $path | |
| 391 | + * @return bool|mixed | |
| 392 | + */ | |
| 393 | +    public function is_file($path) { | |
| 394 | +        if ($path == '/') { | |
| 395 | + return false; | |
| 396 | + } | |
| 397 | +        return $this->basicOperation('is_file', $path); | |
| 398 | + } | |
| 399 | + | |
| 400 | + /** | |
| 401 | + * @param string $path | |
| 402 | + * @return mixed | |
| 403 | + */ | |
| 404 | +    public function stat($path) { | |
| 405 | +        return $this->basicOperation('stat', $path); | |
| 406 | + } | |
| 407 | + | |
| 408 | + /** | |
| 409 | + * @param string $path | |
| 410 | + * @return mixed | |
| 411 | + */ | |
| 412 | +    public function filetype($path) { | |
| 413 | +        return $this->basicOperation('filetype', $path); | |
| 414 | + } | |
| 415 | + | |
| 416 | + /** | |
| 417 | + * @param string $path | |
| 418 | + * @return mixed | |
| 419 | + */ | |
| 420 | +    public function filesize($path) { | |
| 421 | +        return $this->basicOperation('filesize', $path); | |
| 422 | + } | |
| 423 | + | |
| 424 | + /** | |
| 425 | + * @param string $path | |
| 426 | + * @return bool|mixed | |
| 427 | + * @throws \OCP\Files\InvalidPathException | |
| 428 | + */ | |
| 429 | +    public function readfile($path) { | |
| 430 | + $this->assertPathLength($path); | |
| 431 | + @ob_end_clean(); | |
| 432 | + $handle = $this->fopen($path, 'rb'); | |
| 433 | +        if ($handle) { | |
| 434 | + $chunkSize = 8192; // 8 kB chunks | |
| 435 | +            while (!feof($handle)) { | |
| 436 | + echo fread($handle, $chunkSize); | |
| 437 | + flush(); | |
| 438 | + } | |
| 439 | + fclose($handle); | |
| 440 | + $size = $this->filesize($path); | |
| 441 | + return $size; | |
| 442 | + } | |
| 443 | + return false; | |
| 444 | + } | |
| 445 | + | |
| 446 | + /** | |
| 447 | + * @param string $path | |
| 448 | + * @param int $from | |
| 449 | + * @param int $to | |
| 450 | + * @return bool|mixed | |
| 451 | + * @throws \OCP\Files\InvalidPathException | |
| 452 | + * @throws \OCP\Files\UnseekableException | |
| 453 | + */ | |
| 454 | +    public function readfilePart($path, $from, $to) { | |
| 455 | + $this->assertPathLength($path); | |
| 456 | + @ob_end_clean(); | |
| 457 | + $handle = $this->fopen($path, 'rb'); | |
| 458 | +        if ($handle) { | |
| 459 | + $chunkSize = 8192; // 8 kB chunks | |
| 460 | + $startReading = true; | |
| 461 | + | |
| 462 | +            if ($from !== 0 && $from !== '0' && fseek($handle, $from) !== 0) { | |
| 463 | + // forward file handle via chunked fread because fseek seem to have failed | |
| 464 | + | |
| 465 | + $end = $from + 1; | |
| 466 | +                while (!feof($handle) && ftell($handle) < $end) { | |
| 467 | + $len = $from - ftell($handle); | |
| 468 | +                    if ($len > $chunkSize) { | |
| 469 | + $len = $chunkSize; | |
| 470 | + } | |
| 471 | + $result = fread($handle, $len); | |
| 472 | + | |
| 473 | +                    if ($result === false) { | |
| 474 | + $startReading = false; | |
| 475 | + break; | |
| 476 | + } | |
| 477 | + } | |
| 478 | + } | |
| 479 | + | |
| 480 | +            if ($startReading) { | |
| 481 | + $end = $to + 1; | |
| 482 | +                while (!feof($handle) && ftell($handle) < $end) { | |
| 483 | + $len = $end - ftell($handle); | |
| 484 | +                    if ($len > $chunkSize) { | |
| 485 | + $len = $chunkSize; | |
| 486 | + } | |
| 487 | + echo fread($handle, $len); | |
| 488 | + flush(); | |
| 489 | + } | |
| 490 | + $size = ftell($handle) - $from; | |
| 491 | + return $size; | |
| 492 | + } | |
| 493 | + | |
| 494 | +            throw new \OCP\Files\UnseekableException('fseek error'); | |
| 495 | + } | |
| 496 | + return false; | |
| 497 | + } | |
| 498 | + | |
| 499 | + /** | |
| 500 | + * @param string $path | |
| 501 | + * @return mixed | |
| 502 | + */ | |
| 503 | +    public function isCreatable($path) { | |
| 504 | +        return $this->basicOperation('isCreatable', $path); | |
| 505 | + } | |
| 506 | + | |
| 507 | + /** | |
| 508 | + * @param string $path | |
| 509 | + * @return mixed | |
| 510 | + */ | |
| 511 | +    public function isReadable($path) { | |
| 512 | +        return $this->basicOperation('isReadable', $path); | |
| 513 | + } | |
| 514 | + | |
| 515 | + /** | |
| 516 | + * @param string $path | |
| 517 | + * @return mixed | |
| 518 | + */ | |
| 519 | +    public function isUpdatable($path) { | |
| 520 | +        return $this->basicOperation('isUpdatable', $path); | |
| 521 | + } | |
| 522 | + | |
| 523 | + /** | |
| 524 | + * @param string $path | |
| 525 | + * @return bool|mixed | |
| 526 | + */ | |
| 527 | +    public function isDeletable($path) { | |
| 528 | + $absolutePath = $this->getAbsolutePath($path); | |
| 529 | + $mount = Filesystem::getMountManager()->find($absolutePath); | |
| 530 | +        if ($mount->getInternalPath($absolutePath) === '') { | |
| 531 | + return $mount instanceof MoveableMount; | |
| 532 | + } | |
| 533 | +        return $this->basicOperation('isDeletable', $path); | |
| 534 | + } | |
| 535 | + | |
| 536 | + /** | |
| 537 | + * @param string $path | |
| 538 | + * @return mixed | |
| 539 | + */ | |
| 540 | +    public function isSharable($path) { | |
| 541 | +        return $this->basicOperation('isSharable', $path); | |
| 542 | + } | |
| 543 | + | |
| 544 | + /** | |
| 545 | + * @param string $path | |
| 546 | + * @return bool|mixed | |
| 547 | + */ | |
| 548 | +    public function file_exists($path) { | |
| 549 | +        if ($path == '/') { | |
| 550 | + return true; | |
| 551 | + } | |
| 552 | +        return $this->basicOperation('file_exists', $path); | |
| 553 | + } | |
| 554 | + | |
| 555 | + /** | |
| 556 | + * @param string $path | |
| 557 | + * @return mixed | |
| 558 | + */ | |
| 559 | +    public function filemtime($path) { | |
| 560 | +        return $this->basicOperation('filemtime', $path); | |
| 561 | + } | |
| 562 | + | |
| 563 | + /** | |
| 564 | + * @param string $path | |
| 565 | + * @param int|string $mtime | |
| 566 | + * @return bool | |
| 567 | + */ | |
| 568 | +    public function touch($path, $mtime = null) { | |
| 569 | +        if (!is_null($mtime) and !is_numeric($mtime)) { | |
| 570 | + $mtime = strtotime($mtime); | |
| 571 | + } | |
| 572 | + | |
| 573 | +        $hooks = array('touch'); | |
| 574 | + | |
| 575 | +        if (!$this->file_exists($path)) { | |
| 576 | + $hooks[] = 'create'; | |
| 577 | + $hooks[] = 'write'; | |
| 578 | + } | |
| 579 | +        $result = $this->basicOperation('touch', $path, $hooks, $mtime); | |
| 580 | +        if (!$result) { | |
| 581 | + // If create file fails because of permissions on external storage like SMB folders, | |
| 582 | + // check file exists and return false if not. | |
| 583 | +            if (!$this->file_exists($path)) { | |
| 584 | + return false; | |
| 585 | + } | |
| 586 | +            if (is_null($mtime)) { | |
| 587 | + $mtime = time(); | |
| 588 | + } | |
| 589 | + //if native touch fails, we emulate it by changing the mtime in the cache | |
| 590 | +            $this->putFileInfo($path, array('mtime' => floor($mtime))); | |
| 591 | + } | |
| 592 | + return true; | |
| 593 | + } | |
| 594 | + | |
| 595 | + /** | |
| 596 | + * @param string $path | |
| 597 | + * @return mixed | |
| 598 | + */ | |
| 599 | +    public function file_get_contents($path) { | |
| 600 | +        return $this->basicOperation('file_get_contents', $path, array('read')); | |
| 601 | + } | |
| 602 | + | |
| 603 | + /** | |
| 604 | + * @param bool $exists | |
| 605 | + * @param string $path | |
| 606 | + * @param bool $run | |
| 607 | + */ | |
| 608 | +    protected function emit_file_hooks_pre($exists, $path, &$run) { | |
| 609 | +        if (!$exists) { | |
| 610 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_create, array( | |
| 611 | + Filesystem::signal_param_path => $this->getHookPath($path), | |
| 612 | + Filesystem::signal_param_run => &$run, | |
| 613 | + )); | |
| 614 | +        } else { | |
| 615 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_update, array( | |
| 616 | + Filesystem::signal_param_path => $this->getHookPath($path), | |
| 617 | + Filesystem::signal_param_run => &$run, | |
| 618 | + )); | |
| 619 | + } | |
| 620 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_write, array( | |
| 621 | + Filesystem::signal_param_path => $this->getHookPath($path), | |
| 622 | + Filesystem::signal_param_run => &$run, | |
| 623 | + )); | |
| 624 | + } | |
| 625 | + | |
| 626 | + /** | |
| 627 | + * @param bool $exists | |
| 628 | + * @param string $path | |
| 629 | + */ | |
| 630 | +    protected function emit_file_hooks_post($exists, $path) { | |
| 631 | +        if (!$exists) { | |
| 632 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_create, array( | |
| 633 | + Filesystem::signal_param_path => $this->getHookPath($path), | |
| 634 | + )); | |
| 635 | +        } else { | |
| 636 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_update, array( | |
| 637 | + Filesystem::signal_param_path => $this->getHookPath($path), | |
| 638 | + )); | |
| 639 | + } | |
| 640 | + \OC_Hook::emit(Filesystem::CLASSNAME, Filesystem::signal_post_write, array( | |
| 641 | + Filesystem::signal_param_path => $this->getHookPath($path), | |
| 642 | + )); | |
| 643 | + } | |
| 644 | + | |
| 645 | + /** | |
| 646 | + * @param string $path | |
| 647 | + * @param mixed $data | |
| 648 | + * @return bool|mixed | |
| 649 | + * @throws \Exception | |
| 650 | + */ | |
| 651 | +    public function file_put_contents($path, $data) { | |
| 652 | +        if (is_resource($data)) { //not having to deal with streams in file_put_contents makes life easier | |
| 653 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); | |
| 654 | + if (Filesystem::isValidPath($path) | |
| 655 | + and !Filesystem::isFileBlacklisted($path) | |
| 656 | +            ) { | |
| 657 | + $path = $this->getRelativePath($absolutePath); | |
| 658 | + | |
| 659 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); | |
| 660 | + | |
| 661 | + $exists = $this->file_exists($path); | |
| 662 | + $run = true; | |
| 663 | +                if ($this->shouldEmitHooks($path)) { | |
| 664 | + $this->emit_file_hooks_pre($exists, $path, $run); | |
| 665 | + } | |
| 666 | +                if (!$run) { | |
| 667 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 668 | + return false; | |
| 669 | + } | |
| 670 | + | |
| 671 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 672 | + | |
| 673 | + /** @var \OC\Files\Storage\Storage $storage */ | |
| 674 | + list($storage, $internalPath) = $this->resolvePath($path); | |
| 675 | + $target = $storage->fopen($internalPath, 'w'); | |
| 676 | +                if ($target) { | |
| 677 | + list (, $result) = \OC_Helper::streamCopy($data, $target); | |
| 678 | + fclose($target); | |
| 679 | + fclose($data); | |
| 680 | + | |
| 681 | + $this->writeUpdate($storage, $internalPath); | |
| 682 | + | |
| 683 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); | |
| 684 | + | |
| 685 | +                    if ($this->shouldEmitHooks($path) && $result !== false) { | |
| 686 | + $this->emit_file_hooks_post($exists, $path); | |
| 687 | + } | |
| 688 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 689 | + return $result; | |
| 690 | +                } else { | |
| 691 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 692 | + return false; | |
| 693 | + } | |
| 694 | +            } else { | |
| 695 | + return false; | |
| 696 | + } | |
| 697 | +        } else { | |
| 698 | +            $hooks = ($this->file_exists($path)) ? array('update', 'write') : array('create', 'write'); | |
| 699 | +            return $this->basicOperation('file_put_contents', $path, $hooks, $data); | |
| 700 | + } | |
| 701 | + } | |
| 702 | + | |
| 703 | + /** | |
| 704 | + * @param string $path | |
| 705 | + * @return bool|mixed | |
| 706 | + */ | |
| 707 | +    public function unlink($path) { | |
| 708 | +        if ($path === '' || $path === '/') { | |
| 709 | + // do not allow deleting the root | |
| 710 | + return false; | |
| 711 | + } | |
| 712 | + $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; | |
| 713 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); | |
| 714 | + $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); | |
| 715 | +        if ($mount and $mount->getInternalPath($absolutePath) === '') { | |
| 716 | + return $this->removeMount($mount, $absolutePath); | |
| 717 | + } | |
| 718 | +        if ($this->is_dir($path)) { | |
| 719 | +            $result = $this->basicOperation('rmdir', $path, ['delete']); | |
| 720 | +        } else { | |
| 721 | +            $result = $this->basicOperation('unlink', $path, ['delete']); | |
| 722 | + } | |
| 723 | +        if (!$result && !$this->file_exists($path)) { //clear ghost files from the cache on delete | |
| 724 | + $storage = $mount->getStorage(); | |
| 725 | + $internalPath = $mount->getInternalPath($absolutePath); | |
| 726 | + $storage->getUpdater()->remove($internalPath); | |
| 727 | + return true; | |
| 728 | +        } else { | |
| 729 | + return $result; | |
| 730 | + } | |
| 731 | + } | |
| 732 | + | |
| 733 | + /** | |
| 734 | + * @param string $directory | |
| 735 | + * @return bool|mixed | |
| 736 | + */ | |
| 737 | +    public function deleteAll($directory) { | |
| 738 | + return $this->rmdir($directory); | |
| 739 | + } | |
| 740 | + | |
| 741 | + /** | |
| 742 | + * Rename/move a file or folder from the source path to target path. | |
| 743 | + * | |
| 744 | + * @param string $path1 source path | |
| 745 | + * @param string $path2 target path | |
| 746 | + * | |
| 747 | + * @return bool|mixed | |
| 748 | + */ | |
| 749 | +    public function rename($path1, $path2) { | |
| 750 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); | |
| 751 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); | |
| 752 | + $result = false; | |
| 753 | + if ( | |
| 754 | + Filesystem::isValidPath($path2) | |
| 755 | + and Filesystem::isValidPath($path1) | |
| 756 | + and !Filesystem::isFileBlacklisted($path2) | |
| 757 | +        ) { | |
| 758 | + $path1 = $this->getRelativePath($absolutePath1); | |
| 759 | + $path2 = $this->getRelativePath($absolutePath2); | |
| 760 | + $exists = $this->file_exists($path2); | |
| 761 | + | |
| 762 | +            if ($path1 == null or $path2 == null) { | |
| 763 | + return false; | |
| 764 | + } | |
| 765 | + | |
| 766 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED, true); | |
| 767 | +            try { | |
| 768 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED, true); | |
| 769 | + | |
| 770 | + $run = true; | |
| 771 | +                if ($this->shouldEmitHooks($path1) && (Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2))) { | |
| 772 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation | |
| 773 | + $this->emit_file_hooks_pre($exists, $path2, $run); | |
| 774 | +                } elseif ($this->shouldEmitHooks($path1)) { | |
| 775 | + \OC_Hook::emit( | |
| 776 | + Filesystem::CLASSNAME, Filesystem::signal_rename, | |
| 777 | + array( | |
| 778 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), | |
| 779 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), | |
| 780 | + Filesystem::signal_param_run => &$run | |
| 781 | + ) | |
| 782 | + ); | |
| 783 | + } | |
| 784 | +                if ($run) { | |
| 785 | + $this->verifyPath(dirname($path2), basename($path2)); | |
| 786 | + | |
| 787 | + $manager = Filesystem::getMountManager(); | |
| 788 | + $mount1 = $this->getMount($path1); | |
| 789 | + $mount2 = $this->getMount($path2); | |
| 790 | + $storage1 = $mount1->getStorage(); | |
| 791 | + $storage2 = $mount2->getStorage(); | |
| 792 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); | |
| 793 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); | |
| 794 | + | |
| 795 | + $this->changeLock($path1, ILockingProvider::LOCK_EXCLUSIVE, true); | |
| 796 | +                    try { | |
| 797 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE, true); | |
| 798 | + | |
| 799 | +                        if ($internalPath1 === '') { | |
| 800 | +                            if ($mount1 instanceof MoveableMount) { | |
| 801 | +                                if ($this->isTargetAllowed($absolutePath2)) { | |
| 802 | + /** | |
| 803 | + * @var \OC\Files\Mount\MountPoint | \OC\Files\Mount\MoveableMount $mount1 | |
| 804 | + */ | |
| 805 | + $sourceMountPoint = $mount1->getMountPoint(); | |
| 806 | + $result = $mount1->moveMount($absolutePath2); | |
| 807 | + $manager->moveMount($sourceMountPoint, $mount1->getMountPoint()); | |
| 808 | +                                } else { | |
| 809 | + $result = false; | |
| 810 | + } | |
| 811 | +                            } else { | |
| 812 | + $result = false; | |
| 813 | + } | |
| 814 | + // moving a file/folder within the same mount point | |
| 815 | +                        } elseif ($storage1 === $storage2) { | |
| 816 | +                            if ($storage1) { | |
| 817 | + $result = $storage1->rename($internalPath1, $internalPath2); | |
| 818 | +                            } else { | |
| 819 | + $result = false; | |
| 820 | + } | |
| 821 | + // moving a file/folder between storages (from $storage1 to $storage2) | |
| 822 | +                        } else { | |
| 823 | + $result = $storage2->moveFromStorage($storage1, $internalPath1, $internalPath2); | |
| 824 | + } | |
| 825 | + | |
| 826 | +                        if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { | |
| 827 | + // if it was a rename from a part file to a regular file it was a write and not a rename operation | |
| 828 | + $this->writeUpdate($storage2, $internalPath2); | |
| 829 | +                        } else if ($result) { | |
| 830 | +                            if ($internalPath1 !== '') { // don't do a cache update for moved mounts | |
| 831 | + $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); | |
| 832 | + } | |
| 833 | + } | |
| 834 | +                    } catch(\Exception $e) { | |
| 835 | + throw $e; | |
| 836 | +                    } finally { | |
| 837 | + $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); | |
| 838 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED, true); | |
| 839 | + } | |
| 840 | + | |
| 841 | +                    if ((Cache\Scanner::isPartialFile($path1) && !Cache\Scanner::isPartialFile($path2)) && $result !== false) { | |
| 842 | +                        if ($this->shouldEmitHooks()) { | |
| 843 | + $this->emit_file_hooks_post($exists, $path2); | |
| 844 | + } | |
| 845 | +                    } elseif ($result) { | |
| 846 | +                        if ($this->shouldEmitHooks($path1) and $this->shouldEmitHooks($path2)) { | |
| 847 | + \OC_Hook::emit( | |
| 848 | + Filesystem::CLASSNAME, | |
| 849 | + Filesystem::signal_post_rename, | |
| 850 | + array( | |
| 851 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), | |
| 852 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) | |
| 853 | + ) | |
| 854 | + ); | |
| 855 | + } | |
| 856 | + } | |
| 857 | + } | |
| 858 | +            } catch(\Exception $e) { | |
| 859 | + throw $e; | |
| 860 | +            } finally { | |
| 861 | + $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); | |
| 862 | + $this->unlockFile($path2, ILockingProvider::LOCK_SHARED, true); | |
| 863 | + } | |
| 864 | + } | |
| 865 | + return $result; | |
| 866 | + } | |
| 867 | + | |
| 868 | + /** | |
| 869 | + * Copy a file/folder from the source path to target path | |
| 870 | + * | |
| 871 | + * @param string $path1 source path | |
| 872 | + * @param string $path2 target path | |
| 873 | + * @param bool $preserveMtime whether to preserve mtime on the copy | |
| 874 | + * | |
| 875 | + * @return bool|mixed | |
| 876 | + */ | |
| 877 | +    public function copy($path1, $path2, $preserveMtime = false) { | |
| 878 | + $absolutePath1 = Filesystem::normalizePath($this->getAbsolutePath($path1)); | |
| 879 | + $absolutePath2 = Filesystem::normalizePath($this->getAbsolutePath($path2)); | |
| 880 | + $result = false; | |
| 881 | + if ( | |
| 882 | + Filesystem::isValidPath($path2) | |
| 883 | + and Filesystem::isValidPath($path1) | |
| 884 | + and !Filesystem::isFileBlacklisted($path2) | |
| 885 | +        ) { | |
| 886 | + $path1 = $this->getRelativePath($absolutePath1); | |
| 887 | + $path2 = $this->getRelativePath($absolutePath2); | |
| 888 | + | |
| 889 | +            if ($path1 == null or $path2 == null) { | |
| 890 | + return false; | |
| 891 | + } | |
| 892 | + $run = true; | |
| 893 | + | |
| 894 | + $this->lockFile($path2, ILockingProvider::LOCK_SHARED); | |
| 895 | + $this->lockFile($path1, ILockingProvider::LOCK_SHARED); | |
| 896 | + $lockTypePath1 = ILockingProvider::LOCK_SHARED; | |
| 897 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; | |
| 898 | + | |
| 899 | +            try { | |
| 900 | + | |
| 901 | + $exists = $this->file_exists($path2); | |
| 902 | +                if ($this->shouldEmitHooks()) { | |
| 903 | + \OC_Hook::emit( | |
| 904 | + Filesystem::CLASSNAME, | |
| 905 | + Filesystem::signal_copy, | |
| 906 | + array( | |
| 907 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), | |
| 908 | + Filesystem::signal_param_newpath => $this->getHookPath($path2), | |
| 909 | + Filesystem::signal_param_run => &$run | |
| 910 | + ) | |
| 911 | + ); | |
| 912 | + $this->emit_file_hooks_pre($exists, $path2, $run); | |
| 913 | + } | |
| 914 | +                if ($run) { | |
| 915 | + $mount1 = $this->getMount($path1); | |
| 916 | + $mount2 = $this->getMount($path2); | |
| 917 | + $storage1 = $mount1->getStorage(); | |
| 918 | + $internalPath1 = $mount1->getInternalPath($absolutePath1); | |
| 919 | + $storage2 = $mount2->getStorage(); | |
| 920 | + $internalPath2 = $mount2->getInternalPath($absolutePath2); | |
| 921 | + | |
| 922 | + $this->changeLock($path2, ILockingProvider::LOCK_EXCLUSIVE); | |
| 923 | + $lockTypePath2 = ILockingProvider::LOCK_EXCLUSIVE; | |
| 924 | + | |
| 925 | +                    if ($mount1->getMountPoint() == $mount2->getMountPoint()) { | |
| 926 | +                        if ($storage1) { | |
| 927 | + $result = $storage1->copy($internalPath1, $internalPath2); | |
| 928 | +                        } else { | |
| 929 | + $result = false; | |
| 930 | + } | |
| 931 | +                    } else { | |
| 932 | + $result = $storage2->copyFromStorage($storage1, $internalPath1, $internalPath2); | |
| 933 | + } | |
| 934 | + | |
| 935 | + $this->writeUpdate($storage2, $internalPath2); | |
| 936 | + | |
| 937 | + $this->changeLock($path2, ILockingProvider::LOCK_SHARED); | |
| 938 | + $lockTypePath2 = ILockingProvider::LOCK_SHARED; | |
| 939 | + | |
| 940 | +                    if ($this->shouldEmitHooks() && $result !== false) { | |
| 941 | + \OC_Hook::emit( | |
| 942 | + Filesystem::CLASSNAME, | |
| 943 | + Filesystem::signal_post_copy, | |
| 944 | + array( | |
| 945 | + Filesystem::signal_param_oldpath => $this->getHookPath($path1), | |
| 946 | + Filesystem::signal_param_newpath => $this->getHookPath($path2) | |
| 947 | + ) | |
| 948 | + ); | |
| 949 | + $this->emit_file_hooks_post($exists, $path2); | |
| 950 | + } | |
| 951 | + | |
| 952 | + } | |
| 953 | +            } catch (\Exception $e) { | |
| 954 | + $this->unlockFile($path2, $lockTypePath2); | |
| 955 | + $this->unlockFile($path1, $lockTypePath1); | |
| 956 | + throw $e; | |
| 957 | + } | |
| 958 | + | |
| 959 | + $this->unlockFile($path2, $lockTypePath2); | |
| 960 | + $this->unlockFile($path1, $lockTypePath1); | |
| 961 | + | |
| 962 | + } | |
| 963 | + return $result; | |
| 964 | + } | |
| 965 | + | |
| 966 | + /** | |
| 967 | + * @param string $path | |
| 968 | + * @param string $mode 'r' or 'w' | |
| 969 | + * @return resource | |
| 970 | + */ | |
| 971 | +    public function fopen($path, $mode) { | |
| 972 | +        $mode = str_replace('b', '', $mode); // the binary flag is a windows only feature which we do not support | |
| 973 | + $hooks = array(); | |
| 974 | +        switch ($mode) { | |
| 975 | + case 'r': | |
| 976 | + $hooks[] = 'read'; | |
| 977 | + break; | |
| 978 | + case 'r+': | |
| 979 | + case 'w+': | |
| 980 | + case 'x+': | |
| 981 | + case 'a+': | |
| 982 | + $hooks[] = 'read'; | |
| 983 | + $hooks[] = 'write'; | |
| 984 | + break; | |
| 985 | + case 'w': | |
| 986 | + case 'x': | |
| 987 | + case 'a': | |
| 988 | + $hooks[] = 'write'; | |
| 989 | + break; | |
| 990 | + default: | |
| 991 | +                \OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR); | |
| 992 | + } | |
| 993 | + | |
| 994 | +        if ($mode !== 'r' && $mode !== 'w') { | |
| 995 | +            \OC::$server->getLogger()->info('Trying to open a file with a mode other than "r" or "w" can cause severe performance issues with some backends'); | |
| 996 | + } | |
| 997 | + | |
| 998 | +        return $this->basicOperation('fopen', $path, $hooks, $mode); | |
| 999 | + } | |
| 1000 | + | |
| 1001 | + /** | |
| 1002 | + * @param string $path | |
| 1003 | + * @return bool|string | |
| 1004 | + * @throws \OCP\Files\InvalidPathException | |
| 1005 | + */ | |
| 1006 | +    public function toTmpFile($path) { | |
| 1007 | + $this->assertPathLength($path); | |
| 1008 | +        if (Filesystem::isValidPath($path)) { | |
| 1009 | + $source = $this->fopen($path, 'r'); | |
| 1010 | +            if ($source) { | |
| 1011 | + $extension = pathinfo($path, PATHINFO_EXTENSION); | |
| 1012 | + $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($extension); | |
| 1013 | + file_put_contents($tmpFile, $source); | |
| 1014 | + return $tmpFile; | |
| 1015 | +            } else { | |
| 1016 | + return false; | |
| 1017 | + } | |
| 1018 | +        } else { | |
| 1019 | + return false; | |
| 1020 | + } | |
| 1021 | + } | |
| 1022 | + | |
| 1023 | + /** | |
| 1024 | + * @param string $tmpFile | |
| 1025 | + * @param string $path | |
| 1026 | + * @return bool|mixed | |
| 1027 | + * @throws \OCP\Files\InvalidPathException | |
| 1028 | + */ | |
| 1029 | +    public function fromTmpFile($tmpFile, $path) { | |
| 1030 | + $this->assertPathLength($path); | |
| 1031 | +        if (Filesystem::isValidPath($path)) { | |
| 1032 | + | |
| 1033 | + // Get directory that the file is going into | |
| 1034 | + $filePath = dirname($path); | |
| 1035 | + | |
| 1036 | + // Create the directories if any | |
| 1037 | +            if (!$this->file_exists($filePath)) { | |
| 1038 | + $result = $this->createParentDirectories($filePath); | |
| 1039 | +                if ($result === false) { | |
| 1040 | + return false; | |
| 1041 | + } | |
| 1042 | + } | |
| 1043 | + | |
| 1044 | + $source = fopen($tmpFile, 'r'); | |
| 1045 | +            if ($source) { | |
| 1046 | + $result = $this->file_put_contents($path, $source); | |
| 1047 | + // $this->file_put_contents() might have already closed | |
| 1048 | + // the resource, so we check it, before trying to close it | |
| 1049 | + // to avoid messages in the error log. | |
| 1050 | +                if (is_resource($source)) { | |
| 1051 | + fclose($source); | |
| 1052 | + } | |
| 1053 | + unlink($tmpFile); | |
| 1054 | + return $result; | |
| 1055 | +            } else { | |
| 1056 | + return false; | |
| 1057 | + } | |
| 1058 | +        } else { | |
| 1059 | + return false; | |
| 1060 | + } | |
| 1061 | + } | |
| 1062 | + | |
| 1063 | + | |
| 1064 | + /** | |
| 1065 | + * @param string $path | |
| 1066 | + * @return mixed | |
| 1067 | + * @throws \OCP\Files\InvalidPathException | |
| 1068 | + */ | |
| 1069 | +    public function getMimeType($path) { | |
| 1070 | + $this->assertPathLength($path); | |
| 1071 | +        return $this->basicOperation('getMimeType', $path); | |
| 1072 | + } | |
| 1073 | + | |
| 1074 | + /** | |
| 1075 | + * @param string $type | |
| 1076 | + * @param string $path | |
| 1077 | + * @param bool $raw | |
| 1078 | + * @return bool|null|string | |
| 1079 | + */ | |
| 1080 | +    public function hash($type, $path, $raw = false) { | |
| 1081 | + $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; | |
| 1082 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); | |
| 1083 | +        if (Filesystem::isValidPath($path)) { | |
| 1084 | + $path = $this->getRelativePath($absolutePath); | |
| 1085 | +            if ($path == null) { | |
| 1086 | + return false; | |
| 1087 | + } | |
| 1088 | +            if ($this->shouldEmitHooks($path)) { | |
| 1089 | + \OC_Hook::emit( | |
| 1090 | + Filesystem::CLASSNAME, | |
| 1091 | + Filesystem::signal_read, | |
| 1092 | + array(Filesystem::signal_param_path => $this->getHookPath($path)) | |
| 1093 | + ); | |
| 1094 | + } | |
| 1095 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); | |
| 1096 | +            if ($storage) { | |
| 1097 | + $result = $storage->hash($type, $internalPath, $raw); | |
| 1098 | + return $result; | |
| 1099 | + } | |
| 1100 | + } | |
| 1101 | + return null; | |
| 1102 | + } | |
| 1103 | + | |
| 1104 | + /** | |
| 1105 | + * @param string $path | |
| 1106 | + * @return mixed | |
| 1107 | + * @throws \OCP\Files\InvalidPathException | |
| 1108 | + */ | |
| 1109 | +    public function free_space($path = '/') { | |
| 1110 | + $this->assertPathLength($path); | |
| 1111 | +        $result = $this->basicOperation('free_space', $path); | |
| 1112 | +        if ($result === null) { | |
| 1113 | + throw new InvalidPathException(); | |
| 1114 | + } | |
| 1115 | + return $result; | |
| 1116 | + } | |
| 1117 | + | |
| 1118 | + /** | |
| 1119 | + * abstraction layer for basic filesystem functions: wrapper for \OC\Files\Storage\Storage | |
| 1120 | + * | |
| 1121 | + * @param string $operation | |
| 1122 | + * @param string $path | |
| 1123 | + * @param array $hooks (optional) | |
| 1124 | + * @param mixed $extraParam (optional) | |
| 1125 | + * @return mixed | |
| 1126 | + * @throws \Exception | |
| 1127 | + * | |
| 1128 | + * This method takes requests for basic filesystem functions (e.g. reading & writing | |
| 1129 | + * files), processes hooks and proxies, sanitises paths, and finally passes them on to | |
| 1130 | + * \OC\Files\Storage\Storage for delegation to a storage backend for execution | |
| 1131 | + */ | |
| 1132 | +    private function basicOperation($operation, $path, $hooks = [], $extraParam = null) { | |
| 1133 | + $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; | |
| 1134 | + $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); | |
| 1135 | + if (Filesystem::isValidPath($path) | |
| 1136 | + and !Filesystem::isFileBlacklisted($path) | |
| 1137 | +        ) { | |
| 1138 | + $path = $this->getRelativePath($absolutePath); | |
| 1139 | +            if ($path == null) { | |
| 1140 | + return false; | |
| 1141 | + } | |
| 1142 | + | |
| 1143 | +            if (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) { | |
| 1144 | + // always a shared lock during pre-hooks so the hook can read the file | |
| 1145 | + $this->lockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1146 | + } | |
| 1147 | + | |
| 1148 | + $run = $this->runHooks($hooks, $path); | |
| 1149 | + /** @var \OC\Files\Storage\Storage $storage */ | |
| 1150 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); | |
| 1151 | +            if ($run and $storage) { | |
| 1152 | +                if (in_array('write', $hooks) || in_array('delete', $hooks)) { | |
| 1153 | + $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 1154 | + } | |
| 1155 | +                try { | |
| 1156 | +                    if (!is_null($extraParam)) { | |
| 1157 | + $result = $storage->$operation($internalPath, $extraParam); | |
| 1158 | +                    } else { | |
| 1159 | + $result = $storage->$operation($internalPath); | |
| 1160 | + } | |
| 1161 | +                } catch (\Exception $e) { | |
| 1162 | +                    if (in_array('write', $hooks) || in_array('delete', $hooks)) { | |
| 1163 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 1164 | +                    } else if (in_array('read', $hooks)) { | |
| 1165 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1166 | + } | |
| 1167 | + throw $e; | |
| 1168 | + } | |
| 1169 | + | |
| 1170 | +                if ($result && in_array('delete', $hooks) and $result) { | |
| 1171 | + $this->removeUpdate($storage, $internalPath); | |
| 1172 | + } | |
| 1173 | +                if ($result && in_array('write', $hooks) and $operation !== 'fopen') { | |
| 1174 | + $this->writeUpdate($storage, $internalPath); | |
| 1175 | + } | |
| 1176 | +                if ($result && in_array('touch', $hooks)) { | |
| 1177 | + $this->writeUpdate($storage, $internalPath, $extraParam); | |
| 1178 | + } | |
| 1179 | + | |
| 1180 | +                if ((in_array('write', $hooks) || in_array('delete', $hooks)) && ($operation !== 'fopen' || $result === false)) { | |
| 1181 | + $this->changeLock($path, ILockingProvider::LOCK_SHARED); | |
| 1182 | + } | |
| 1183 | + | |
| 1184 | + $unlockLater = false; | |
| 1185 | +                if ($this->lockingEnabled && $operation === 'fopen' && is_resource($result)) { | |
| 1186 | + $unlockLater = true; | |
| 1187 | + // make sure our unlocking callback will still be called if connection is aborted | |
| 1188 | + ignore_user_abort(true); | |
| 1189 | +                    $result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { | |
| 1190 | +                        if (in_array('write', $hooks)) { | |
| 1191 | + $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); | |
| 1192 | +                        } else if (in_array('read', $hooks)) { | |
| 1193 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1194 | + } | |
| 1195 | + }); | |
| 1196 | + } | |
| 1197 | + | |
| 1198 | +                if ($this->shouldEmitHooks($path) && $result !== false) { | |
| 1199 | +                    if ($operation != 'fopen') { //no post hooks for fopen, the file stream is still open | |
| 1200 | + $this->runHooks($hooks, $path, true); | |
| 1201 | + } | |
| 1202 | + } | |
| 1203 | + | |
| 1204 | + if (!$unlockLater | |
| 1205 | +                    && (in_array('write', $hooks) || in_array('delete', $hooks) || in_array('read', $hooks)) | |
| 1206 | +                ) { | |
| 1207 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1208 | + } | |
| 1209 | + return $result; | |
| 1210 | +            } else { | |
| 1211 | + $this->unlockFile($path, ILockingProvider::LOCK_SHARED); | |
| 1212 | + } | |
| 1213 | + } | |
| 1214 | + return null; | |
| 1215 | + } | |
| 1216 | + | |
| 1217 | + /** | |
| 1218 | + * get the path relative to the default root for hook usage | |
| 1219 | + * | |
| 1220 | + * @param string $path | |
| 1221 | + * @return string | |
| 1222 | + */ | |
| 1223 | +    private function getHookPath($path) { | |
| 1224 | +        if (!Filesystem::getView()) { | |
| 1225 | + return $path; | |
| 1226 | + } | |
| 1227 | + return Filesystem::getView()->getRelativePath($this->getAbsolutePath($path)); | |
| 1228 | + } | |
| 1229 | + | |
| 1230 | +    private function shouldEmitHooks($path = '') { | |
| 1231 | +        if ($path && Cache\Scanner::isPartialFile($path)) { | |
| 1232 | + return false; | |
| 1233 | + } | |
| 1234 | +        if (!Filesystem::$loaded) { | |
| 1235 | + return false; | |
| 1236 | + } | |
| 1237 | + $defaultRoot = Filesystem::getRoot(); | |
| 1238 | +        if ($defaultRoot === null) { | |
| 1239 | + return false; | |
| 1240 | + } | |
| 1241 | +        if ($this->fakeRoot === $defaultRoot) { | |
| 1242 | + return true; | |
| 1243 | + } | |
| 1244 | + $fullPath = $this->getAbsolutePath($path); | |
| 1245 | + | |
| 1246 | +        if ($fullPath === $defaultRoot) { | |
| 1247 | + return true; | |
| 1248 | + } | |
| 1249 | + | |
| 1250 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); | |
| 1251 | + } | |
| 1252 | + | |
| 1253 | + /** | |
| 1254 | + * @param string[] $hooks | |
| 1255 | + * @param string $path | |
| 1256 | + * @param bool $post | |
| 1257 | + * @return bool | |
| 1258 | + */ | |
| 1259 | +    private function runHooks($hooks, $path, $post = false) { | |
| 1260 | + $relativePath = $path; | |
| 1261 | + $path = $this->getHookPath($path); | |
| 1262 | + $prefix = ($post) ? 'post_' : ''; | |
| 1263 | + $run = true; | |
| 1264 | +        if ($this->shouldEmitHooks($relativePath)) { | |
| 1265 | +            foreach ($hooks as $hook) { | |
| 1266 | +                if ($hook != 'read') { | |
| 1267 | + \OC_Hook::emit( | |
| 1268 | + Filesystem::CLASSNAME, | |
| 1269 | + $prefix . $hook, | |
| 1270 | + array( | |
| 1271 | + Filesystem::signal_param_run => &$run, | |
| 1272 | + Filesystem::signal_param_path => $path | |
| 1273 | + ) | |
| 1274 | + ); | |
| 1275 | +                } elseif (!$post) { | |
| 1276 | + \OC_Hook::emit( | |
| 1277 | + Filesystem::CLASSNAME, | |
| 1278 | + $prefix . $hook, | |
| 1279 | + array( | |
| 1280 | + Filesystem::signal_param_path => $path | |
| 1281 | + ) | |
| 1282 | + ); | |
| 1283 | + } | |
| 1284 | + } | |
| 1285 | + } | |
| 1286 | + return $run; | |
| 1287 | + } | |
| 1288 | + | |
| 1289 | + /** | |
| 1290 | + * check if a file or folder has been updated since $time | |
| 1291 | + * | |
| 1292 | + * @param string $path | |
| 1293 | + * @param int $time | |
| 1294 | + * @return bool | |
| 1295 | + */ | |
| 1296 | +    public function hasUpdated($path, $time) { | |
| 1297 | +        return $this->basicOperation('hasUpdated', $path, array(), $time); | |
| 1298 | + } | |
| 1299 | + | |
| 1300 | + /** | |
| 1301 | + * @param string $ownerId | |
| 1302 | + * @return \OC\User\User | |
| 1303 | + */ | |
| 1304 | +    private function getUserObjectForOwner($ownerId) { | |
| 1305 | + $owner = $this->userManager->get($ownerId); | |
| 1306 | +        if ($owner instanceof IUser) { | |
| 1307 | + return $owner; | |
| 1308 | +        } else { | |
| 1309 | + return new User($ownerId, null); | |
| 1310 | + } | |
| 1311 | + } | |
| 1312 | + | |
| 1313 | + /** | |
| 1314 | + * Get file info from cache | |
| 1315 | + * | |
| 1316 | + * If the file is not in cached it will be scanned | |
| 1317 | + * If the file has changed on storage the cache will be updated | |
| 1318 | + * | |
| 1319 | + * @param \OC\Files\Storage\Storage $storage | |
| 1320 | + * @param string $internalPath | |
| 1321 | + * @param string $relativePath | |
| 1322 | + * @return array|bool | |
| 1323 | + */ | |
| 1324 | +    private function getCacheEntry($storage, $internalPath, $relativePath) { | |
| 1325 | + $cache = $storage->getCache($internalPath); | |
| 1326 | + $data = $cache->get($internalPath); | |
| 1327 | + $watcher = $storage->getWatcher($internalPath); | |
| 1328 | + | |
| 1329 | +        try { | |
| 1330 | + // if the file is not in the cache or needs to be updated, trigger the scanner and reload the data | |
| 1331 | +            if (!$data || $data['size'] === -1) { | |
| 1332 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1333 | +                if (!$storage->file_exists($internalPath)) { | |
| 1334 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1335 | + return false; | |
| 1336 | + } | |
| 1337 | + $scanner = $storage->getScanner($internalPath); | |
| 1338 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); | |
| 1339 | + $data = $cache->get($internalPath); | |
| 1340 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1341 | +            } else if (!Cache\Scanner::isPartialFile($internalPath) && $watcher->needsUpdate($internalPath, $data)) { | |
| 1342 | + $this->lockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1343 | + $watcher->update($internalPath, $data); | |
| 1344 | + $storage->getPropagator()->propagateChange($internalPath, time()); | |
| 1345 | + $data = $cache->get($internalPath); | |
| 1346 | + $this->unlockFile($relativePath, ILockingProvider::LOCK_SHARED); | |
| 1347 | + } | |
| 1348 | +        } catch (LockedException $e) { | |
| 1349 | + // if the file is locked we just use the old cache info | |
| 1350 | + } | |
| 1351 | + | |
| 1352 | + return $data; | |
| 1353 | + } | |
| 1354 | + | |
| 1355 | + /** | |
| 1356 | + * get the filesystem info | |
| 1357 | + * | |
| 1358 | + * @param string $path | |
| 1359 | + * @param boolean|string $includeMountPoints true to add mountpoint sizes, | |
| 1360 | + * 'ext' to add only ext storage mount point sizes. Defaults to true. | |
| 1361 | + * defaults to true | |
| 1362 | + * @return \OC\Files\FileInfo|false False if file does not exist | |
| 1363 | + */ | |
| 1364 | +    public function getFileInfo($path, $includeMountPoints = true) { | |
| 1365 | + $this->assertPathLength($path); | |
| 1366 | +        if (!Filesystem::isValidPath($path)) { | |
| 1367 | + return false; | |
| 1368 | + } | |
| 1369 | +        if (Cache\Scanner::isPartialFile($path)) { | |
| 1370 | + return $this->getPartFileInfo($path); | |
| 1371 | + } | |
| 1372 | + $relativePath = $path; | |
| 1373 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); | |
| 1374 | + | |
| 1375 | + $mount = Filesystem::getMountManager()->find($path); | |
| 1376 | + $storage = $mount->getStorage(); | |
| 1377 | + $internalPath = $mount->getInternalPath($path); | |
| 1378 | +        if ($storage) { | |
| 1379 | + $data = $this->getCacheEntry($storage, $internalPath, $relativePath); | |
| 1380 | + | |
| 1381 | +            if (!$data instanceof ICacheEntry) { | |
| 1382 | + return false; | |
| 1383 | + } | |
| 1384 | + | |
| 1385 | +            if ($mount instanceof MoveableMount && $internalPath === '') { | |
| 1386 | + $data['permissions'] |= \OCP\Constants::PERMISSION_DELETE; | |
| 1387 | + } | |
| 1388 | + | |
| 1389 | + $owner = $this->getUserObjectForOwner($storage->getOwner($internalPath)); | |
| 1390 | + $info = new FileInfo($path, $storage, $internalPath, $data, $mount, $owner); | |
| 1391 | + | |
| 1392 | +            if ($data and isset($data['fileid'])) { | |
| 1393 | +                if ($includeMountPoints and $data['mimetype'] === 'httpd/unix-directory') { | |
| 1394 | + //add the sizes of other mount points to the folder | |
| 1395 | + $extOnly = ($includeMountPoints === 'ext'); | |
| 1396 | + $mounts = Filesystem::getMountManager()->findIn($path); | |
| 1397 | +                    $info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { | |
| 1398 | + $subStorage = $mount->getStorage(); | |
| 1399 | + return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); | |
| 1400 | + })); | |
| 1401 | + } | |
| 1402 | + } | |
| 1403 | + | |
| 1404 | + return $info; | |
| 1405 | + } | |
| 1406 | + | |
| 1407 | + return false; | |
| 1408 | + } | |
| 1409 | + | |
| 1410 | + /** | |
| 1411 | + * get the content of a directory | |
| 1412 | + * | |
| 1413 | + * @param string $directory path under datadirectory | |
| 1414 | + * @param string $mimetype_filter limit returned content to this mimetype or mimepart | |
| 1415 | + * @return FileInfo[] | |
| 1416 | + */ | |
| 1417 | +    public function getDirectoryContent($directory, $mimetype_filter = '') { | |
| 1418 | + $this->assertPathLength($directory); | |
| 1419 | +        if (!Filesystem::isValidPath($directory)) { | |
| 1420 | + return []; | |
| 1421 | + } | |
| 1422 | + $path = $this->getAbsolutePath($directory); | |
| 1423 | + $path = Filesystem::normalizePath($path); | |
| 1424 | + $mount = $this->getMount($directory); | |
| 1425 | + $storage = $mount->getStorage(); | |
| 1426 | + $internalPath = $mount->getInternalPath($path); | |
| 1427 | +        if ($storage) { | |
| 1428 | + $cache = $storage->getCache($internalPath); | |
| 1429 | + $user = \OC_User::getUser(); | |
| 1430 | + | |
| 1431 | + $data = $this->getCacheEntry($storage, $internalPath, $directory); | |
| 1432 | + | |
| 1433 | +            if (!$data instanceof ICacheEntry || !isset($data['fileid']) || !($data->getPermissions() && Constants::PERMISSION_READ)) { | |
| 1434 | + return []; | |
| 1435 | + } | |
| 1436 | + | |
| 1437 | + $folderId = $data['fileid']; | |
| 1438 | + $contents = $cache->getFolderContentsById($folderId); //TODO: mimetype_filter | |
| 1439 | + | |
| 1440 | + $sharingDisabled = \OCP\Util::isSharingDisabledForUser(); | |
| 1441 | + | |
| 1442 | +            $fileNames = array_map(function(ICacheEntry $content) { | |
| 1443 | + return $content->getName(); | |
| 1444 | + }, $contents); | |
| 1445 | + /** | |
| 1446 | + * @var \OC\Files\FileInfo[] $fileInfos | |
| 1447 | + */ | |
| 1448 | +            $fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { | |
| 1449 | +                if ($sharingDisabled) { | |
| 1450 | + $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; | |
| 1451 | + } | |
| 1452 | + $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); | |
| 1453 | + return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); | |
| 1454 | + }, $contents); | |
| 1455 | + $files = array_combine($fileNames, $fileInfos); | |
| 1456 | + | |
| 1457 | + //add a folder for any mountpoint in this directory and add the sizes of other mountpoints to the folders | |
| 1458 | + $mounts = Filesystem::getMountManager()->findIn($path); | |
| 1459 | + $dirLength = strlen($path); | |
| 1460 | +            foreach ($mounts as $mount) { | |
| 1461 | + $mountPoint = $mount->getMountPoint(); | |
| 1462 | + $subStorage = $mount->getStorage(); | |
| 1463 | +                if ($subStorage) { | |
| 1464 | +                    $subCache = $subStorage->getCache(''); | |
| 1465 | + | |
| 1466 | +                    $rootEntry = $subCache->get(''); | |
| 1467 | +                    if (!$rootEntry) { | |
| 1468 | +                        $subScanner = $subStorage->getScanner(''); | |
| 1469 | +                        try { | |
| 1470 | +                            $subScanner->scanFile(''); | |
| 1471 | +                        } catch (\OCP\Files\StorageNotAvailableException $e) { | |
| 1472 | + continue; | |
| 1473 | +                        } catch (\OCP\Files\StorageInvalidException $e) { | |
| 1474 | + continue; | |
| 1475 | +                        } catch (\Exception $e) { | |
| 1476 | + // sometimes when the storage is not available it can be any exception | |
| 1477 | + \OCP\Util::writeLog( | |
| 1478 | + 'core', | |
| 1479 | + 'Exception while scanning storage "' . $subStorage->getId() . '": ' . | |
| 1480 | + get_class($e) . ': ' . $e->getMessage(), | |
| 1481 | + \OCP\Util::ERROR | |
| 1482 | + ); | |
| 1483 | + continue; | |
| 1484 | + } | |
| 1485 | +                        $rootEntry = $subCache->get(''); | |
| 1486 | + } | |
| 1487 | + | |
| 1488 | +                    if ($rootEntry && ($rootEntry->getPermissions() && Constants::PERMISSION_READ)) { | |
| 1489 | + $relativePath = trim(substr($mountPoint, $dirLength), '/'); | |
| 1490 | +                        if ($pos = strpos($relativePath, '/')) { | |
| 1491 | + //mountpoint inside subfolder add size to the correct folder | |
| 1492 | + $entryName = substr($relativePath, 0, $pos); | |
| 1493 | +                            foreach ($files as &$entry) { | |
| 1494 | +                                if ($entry->getName() === $entryName) { | |
| 1495 | + $entry->addSubEntry($rootEntry, $mountPoint); | |
| 1496 | + } | |
| 1497 | + } | |
| 1498 | +                        } else { //mountpoint in this folder, add an entry for it | |
| 1499 | + $rootEntry['name'] = $relativePath; | |
| 1500 | + $rootEntry['type'] = $rootEntry['mimetype'] === 'httpd/unix-directory' ? 'dir' : 'file'; | |
| 1501 | + $permissions = $rootEntry['permissions']; | |
| 1502 | + // do not allow renaming/deleting the mount point if they are not shared files/folders | |
| 1503 | + // for shared files/folders we use the permissions given by the owner | |
| 1504 | +                            if ($mount instanceof MoveableMount) { | |
| 1505 | + $rootEntry['permissions'] = $permissions | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE; | |
| 1506 | +                            } else { | |
| 1507 | + $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); | |
| 1508 | + } | |
| 1509 | + | |
| 1510 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ | |
| 1511 | + | |
| 1512 | + // if sharing was disabled for the user we remove the share permissions | |
| 1513 | +                            if (\OCP\Util::isSharingDisabledForUser()) { | |
| 1514 | + $rootEntry['permissions'] = $rootEntry['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; | |
| 1515 | + } | |
| 1516 | + | |
| 1517 | +                            $owner = $this->getUserObjectForOwner($subStorage->getOwner('')); | |
| 1518 | + $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); | |
| 1519 | + } | |
| 1520 | + } | |
| 1521 | + } | |
| 1522 | + } | |
| 1523 | + | |
| 1524 | +            if ($mimetype_filter) { | |
| 1525 | +                $files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { | |
| 1526 | +                    if (strpos($mimetype_filter, '/')) { | |
| 1527 | + return $file->getMimetype() === $mimetype_filter; | |
| 1528 | +                    } else { | |
| 1529 | + return $file->getMimePart() === $mimetype_filter; | |
| 1530 | + } | |
| 1531 | + }); | |
| 1532 | + } | |
| 1533 | + | |
| 1534 | + return array_values($files); | |
| 1535 | +        } else { | |
| 1536 | + return []; | |
| 1537 | + } | |
| 1538 | + } | |
| 1539 | + | |
| 1540 | + /** | |
| 1541 | + * change file metadata | |
| 1542 | + * | |
| 1543 | + * @param string $path | |
| 1544 | + * @param array|\OCP\Files\FileInfo $data | |
| 1545 | + * @return int | |
| 1546 | + * | |
| 1547 | + * returns the fileid of the updated file | |
| 1548 | + */ | |
| 1549 | +    public function putFileInfo($path, $data) { | |
| 1550 | + $this->assertPathLength($path); | |
| 1551 | +        if ($data instanceof FileInfo) { | |
| 1552 | + $data = $data->getData(); | |
| 1553 | + } | |
| 1554 | + $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); | |
| 1555 | + /** | |
| 1556 | + * @var \OC\Files\Storage\Storage $storage | |
| 1557 | + * @var string $internalPath | |
| 1558 | + */ | |
| 1559 | + list($storage, $internalPath) = Filesystem::resolvePath($path); | |
| 1560 | +        if ($storage) { | |
| 1561 | + $cache = $storage->getCache($path); | |
| 1562 | + | |
| 1563 | +            if (!$cache->inCache($internalPath)) { | |
| 1564 | + $scanner = $storage->getScanner($internalPath); | |
| 1565 | + $scanner->scan($internalPath, Cache\Scanner::SCAN_SHALLOW); | |
| 1566 | + } | |
| 1567 | + | |
| 1568 | + return $cache->put($internalPath, $data); | |
| 1569 | +        } else { | |
| 1570 | + return -1; | |
| 1571 | + } | |
| 1572 | + } | |
| 1573 | + | |
| 1574 | + /** | |
| 1575 | + * search for files with the name matching $query | |
| 1576 | + * | |
| 1577 | + * @param string $query | |
| 1578 | + * @return FileInfo[] | |
| 1579 | + */ | |
| 1580 | +    public function search($query) { | |
| 1581 | +        return $this->searchCommon('search', array('%' . $query . '%')); | |
| 1582 | + } | |
| 1583 | + | |
| 1584 | + /** | |
| 1585 | + * search for files with the name matching $query | |
| 1586 | + * | |
| 1587 | + * @param string $query | |
| 1588 | + * @return FileInfo[] | |
| 1589 | + */ | |
| 1590 | +    public function searchRaw($query) { | |
| 1591 | +        return $this->searchCommon('search', array($query)); | |
| 1592 | + } | |
| 1593 | + | |
| 1594 | + /** | |
| 1595 | + * search for files by mimetype | |
| 1596 | + * | |
| 1597 | + * @param string $mimetype | |
| 1598 | + * @return FileInfo[] | |
| 1599 | + */ | |
| 1600 | +    public function searchByMime($mimetype) { | |
| 1601 | +        return $this->searchCommon('searchByMime', array($mimetype)); | |
| 1602 | + } | |
| 1603 | + | |
| 1604 | + /** | |
| 1605 | + * search for files by tag | |
| 1606 | + * | |
| 1607 | + * @param string|int $tag name or tag id | |
| 1608 | + * @param string $userId owner of the tags | |
| 1609 | + * @return FileInfo[] | |
| 1610 | + */ | |
| 1611 | +    public function searchByTag($tag, $userId) { | |
| 1612 | +        return $this->searchCommon('searchByTag', array($tag, $userId)); | |
| 1613 | + } | |
| 1614 | + | |
| 1615 | + /** | |
| 1616 | + * @param string $method cache method | |
| 1617 | + * @param array $args | |
| 1618 | + * @return FileInfo[] | |
| 1619 | + */ | |
| 1620 | +    private function searchCommon($method, $args) { | |
| 1621 | + $files = array(); | |
| 1622 | + $rootLength = strlen($this->fakeRoot); | |
| 1623 | + | |
| 1624 | +        $mount = $this->getMount(''); | |
| 1625 | + $mountPoint = $mount->getMountPoint(); | |
| 1626 | + $storage = $mount->getStorage(); | |
| 1627 | +        if ($storage) { | |
| 1628 | +            $cache = $storage->getCache(''); | |
| 1629 | + | |
| 1630 | + $results = call_user_func_array(array($cache, $method), $args); | |
| 1631 | +            foreach ($results as $result) { | |
| 1632 | +                if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { | |
| 1633 | + $internalPath = $result['path']; | |
| 1634 | + $path = $mountPoint . $result['path']; | |
| 1635 | + $result['path'] = substr($mountPoint . $result['path'], $rootLength); | |
| 1636 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); | |
| 1637 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); | |
| 1638 | + } | |
| 1639 | + } | |
| 1640 | + | |
| 1641 | + $mounts = Filesystem::getMountManager()->findIn($this->fakeRoot); | |
| 1642 | +            foreach ($mounts as $mount) { | |
| 1643 | + $mountPoint = $mount->getMountPoint(); | |
| 1644 | + $storage = $mount->getStorage(); | |
| 1645 | +                if ($storage) { | |
| 1646 | +                    $cache = $storage->getCache(''); | |
| 1647 | + | |
| 1648 | + $relativeMountPoint = substr($mountPoint, $rootLength); | |
| 1649 | + $results = call_user_func_array(array($cache, $method), $args); | |
| 1650 | +                    if ($results) { | |
| 1651 | +                        foreach ($results as $result) { | |
| 1652 | + $internalPath = $result['path']; | |
| 1653 | + $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); | |
| 1654 | + $path = rtrim($mountPoint . $internalPath, '/'); | |
| 1655 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); | |
| 1656 | + $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); | |
| 1657 | + } | |
| 1658 | + } | |
| 1659 | + } | |
| 1660 | + } | |
| 1661 | + } | |
| 1662 | + return $files; | |
| 1663 | + } | |
| 1664 | + | |
| 1665 | + /** | |
| 1666 | + * Get the owner for a file or folder | |
| 1667 | + * | |
| 1668 | + * @param string $path | |
| 1669 | + * @return string the user id of the owner | |
| 1670 | + * @throws NotFoundException | |
| 1671 | + */ | |
| 1672 | +    public function getOwner($path) { | |
| 1673 | + $info = $this->getFileInfo($path); | |
| 1674 | +        if (!$info) { | |
| 1675 | + throw new NotFoundException($path . ' not found while trying to get owner'); | |
| 1676 | + } | |
| 1677 | + return $info->getOwner()->getUID(); | |
| 1678 | + } | |
| 1679 | + | |
| 1680 | + /** | |
| 1681 | + * get the ETag for a file or folder | |
| 1682 | + * | |
| 1683 | + * @param string $path | |
| 1684 | + * @return string | |
| 1685 | + */ | |
| 1686 | +    public function getETag($path) { | |
| 1687 | + /** | |
| 1688 | + * @var Storage\Storage $storage | |
| 1689 | + * @var string $internalPath | |
| 1690 | + */ | |
| 1691 | + list($storage, $internalPath) = $this->resolvePath($path); | |
| 1692 | +        if ($storage) { | |
| 1693 | + return $storage->getETag($internalPath); | |
| 1694 | +        } else { | |
| 1695 | + return null; | |
| 1696 | + } | |
| 1697 | + } | |
| 1698 | + | |
| 1699 | + /** | |
| 1700 | + * Get the path of a file by id, relative to the view | |
| 1701 | + * | |
| 1702 | + * Note that the resulting path is not guarantied to be unique for the id, multiple paths can point to the same file | |
| 1703 | + * | |
| 1704 | + * @param int $id | |
| 1705 | + * @throws NotFoundException | |
| 1706 | + * @return string | |
| 1707 | + */ | |
| 1708 | +    public function getPath($id) { | |
| 1709 | + $id = (int)$id; | |
| 1710 | + $manager = Filesystem::getMountManager(); | |
| 1711 | + $mounts = $manager->findIn($this->fakeRoot); | |
| 1712 | + $mounts[] = $manager->find($this->fakeRoot); | |
| 1713 | + // reverse the array so we start with the storage this view is in | |
| 1714 | + // which is the most likely to contain the file we're looking for | |
| 1715 | + $mounts = array_reverse($mounts); | |
| 1716 | +        foreach ($mounts as $mount) { | |
| 1717 | + /** | |
| 1718 | + * @var \OC\Files\Mount\MountPoint $mount | |
| 1719 | + */ | |
| 1720 | +            if ($mount->getStorage()) { | |
| 1721 | + $cache = $mount->getStorage()->getCache(); | |
| 1722 | +                if (!$cache) { | |
| 1723 | +                    throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); | |
| 1724 | + } | |
| 1725 | + $internalPath = $cache->getPathById($id); | |
| 1726 | +                if (is_string($internalPath)) { | |
| 1727 | + $fullPath = $mount->getMountPoint() . $internalPath; | |
| 1728 | +                    if (!is_null($path = $this->getRelativePath($fullPath))) { | |
| 1729 | + return $path; | |
| 1730 | + } | |
| 1731 | + } | |
| 1732 | + } | |
| 1733 | + } | |
| 1734 | +        throw new NotFoundException(sprintf('File with id "%s" has not been found.', $id)); | |
| 1735 | + } | |
| 1736 | + | |
| 1737 | + /** | |
| 1738 | + * @param string $path | |
| 1739 | + * @throws InvalidPathException | |
| 1740 | + */ | |
| 1741 | +    private function assertPathLength($path) { | |
| 1742 | + $maxLen = min(PHP_MAXPATHLEN, 4000); | |
| 1743 | + // Check for the string length - performed using isset() instead of strlen() | |
| 1744 | + // because isset() is about 5x-40x faster. | |
| 1745 | +        if (isset($path[$maxLen])) { | |
| 1746 | + $pathLen = strlen($path); | |
| 1747 | +            throw new \OCP\Files\InvalidPathException("Path length($pathLen) exceeds max path length($maxLen): $path"); | |
| 1748 | + } | |
| 1749 | + } | |
| 1750 | + | |
| 1751 | + /** | |
| 1752 | + * check if it is allowed to move a mount point to a given target. | |
| 1753 | + * It is not allowed to move a mount point into a different mount point or | |
| 1754 | + * into an already shared folder | |
| 1755 | + * | |
| 1756 | + * @param string $target path | |
| 1757 | + * @return boolean | |
| 1758 | + */ | |
| 1759 | +    private function isTargetAllowed($target) { | |
| 1760 | + | |
| 1761 | + list($targetStorage, $targetInternalPath) = \OC\Files\Filesystem::resolvePath($target); | |
| 1762 | +        if (!$targetStorage->instanceOfStorage('\OCP\Files\IHomeStorage')) { | |
| 1763 | +            \OCP\Util::writeLog('files', | |
| 1764 | + 'It is not allowed to move one mount point into another one', | |
| 1765 | + \OCP\Util::DEBUG); | |
| 1766 | + return false; | |
| 1767 | + } | |
| 1768 | + | |
| 1769 | + // note: cannot use the view because the target is already locked | |
| 1770 | + $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); | |
| 1771 | +        if ($fileId === -1) { | |
| 1772 | + // target might not exist, need to check parent instead | |
| 1773 | + $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); | |
| 1774 | + } | |
| 1775 | + | |
| 1776 | + // check if any of the parents were shared by the current owner (include collections) | |
| 1777 | + $shares = \OCP\Share::getItemShared( | |
| 1778 | + 'folder', | |
| 1779 | + $fileId, | |
| 1780 | + \OCP\Share::FORMAT_NONE, | |
| 1781 | + null, | |
| 1782 | + true | |
| 1783 | + ); | |
| 1784 | + | |
| 1785 | +        if (count($shares) > 0) { | |
| 1786 | +            \OCP\Util::writeLog('files', | |
| 1787 | + 'It is not allowed to move one mount point into a shared folder', | |
| 1788 | + \OCP\Util::DEBUG); | |
| 1789 | + return false; | |
| 1790 | + } | |
| 1791 | + | |
| 1792 | + return true; | |
| 1793 | + } | |
| 1794 | + | |
| 1795 | + /** | |
| 1796 | + * Get a fileinfo object for files that are ignored in the cache (part files) | |
| 1797 | + * | |
| 1798 | + * @param string $path | |
| 1799 | + * @return \OCP\Files\FileInfo | |
| 1800 | + */ | |
| 1801 | +    private function getPartFileInfo($path) { | |
| 1802 | + $mount = $this->getMount($path); | |
| 1803 | + $storage = $mount->getStorage(); | |
| 1804 | + $internalPath = $mount->getInternalPath($this->getAbsolutePath($path)); | |
| 1805 | + $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); | |
| 1806 | + return new FileInfo( | |
| 1807 | + $this->getAbsolutePath($path), | |
| 1808 | + $storage, | |
| 1809 | + $internalPath, | |
| 1810 | + [ | |
| 1811 | + 'fileid' => null, | |
| 1812 | + 'mimetype' => $storage->getMimeType($internalPath), | |
| 1813 | + 'name' => basename($path), | |
| 1814 | + 'etag' => null, | |
| 1815 | + 'size' => $storage->filesize($internalPath), | |
| 1816 | + 'mtime' => $storage->filemtime($internalPath), | |
| 1817 | + 'encrypted' => false, | |
| 1818 | + 'permissions' => \OCP\Constants::PERMISSION_ALL | |
| 1819 | + ], | |
| 1820 | + $mount, | |
| 1821 | + $owner | |
| 1822 | + ); | |
| 1823 | + } | |
| 1824 | + | |
| 1825 | + /** | |
| 1826 | + * @param string $path | |
| 1827 | + * @param string $fileName | |
| 1828 | + * @throws InvalidPathException | |
| 1829 | + */ | |
| 1830 | +    public function verifyPath($path, $fileName) { | |
| 1831 | +        try { | |
| 1832 | + /** @type \OCP\Files\Storage $storage */ | |
| 1833 | + list($storage, $internalPath) = $this->resolvePath($path); | |
| 1834 | + $storage->verifyPath($internalPath, $fileName); | |
| 1835 | +        } catch (ReservedWordException $ex) { | |
| 1836 | +            $l = \OC::$server->getL10N('lib'); | |
| 1837 | +            throw new InvalidPathException($l->t('File name is a reserved word')); | |
| 1838 | +        } catch (InvalidCharacterInPathException $ex) { | |
| 1839 | +            $l = \OC::$server->getL10N('lib'); | |
| 1840 | +            throw new InvalidPathException($l->t('File name contains at least one invalid character')); | |
| 1841 | +        } catch (FileNameTooLongException $ex) { | |
| 1842 | +            $l = \OC::$server->getL10N('lib'); | |
| 1843 | +            throw new InvalidPathException($l->t('File name is too long')); | |
| 1844 | +        } catch (InvalidDirectoryException $ex) { | |
| 1845 | +            $l = \OC::$server->getL10N('lib'); | |
| 1846 | +            throw new InvalidPathException($l->t('Dot files are not allowed')); | |
| 1847 | +        } catch (EmptyFileNameException $ex) { | |
| 1848 | +            $l = \OC::$server->getL10N('lib'); | |
| 1849 | +            throw new InvalidPathException($l->t('Empty filename is not allowed')); | |
| 1850 | + } | |
| 1851 | + } | |
| 1852 | + | |
| 1853 | + /** | |
| 1854 | + * get all parent folders of $path | |
| 1855 | + * | |
| 1856 | + * @param string $path | |
| 1857 | + * @return string[] | |
| 1858 | + */ | |
| 1859 | +    private function getParents($path) { | |
| 1860 | + $path = trim($path, '/'); | |
| 1861 | +        if (!$path) { | |
| 1862 | + return []; | |
| 1863 | + } | |
| 1864 | + | |
| 1865 | +        $parts = explode('/', $path); | |
| 1866 | + | |
| 1867 | + // remove the single file | |
| 1868 | + array_pop($parts); | |
| 1869 | +        $result = array('/'); | |
| 1870 | + $resultPath = ''; | |
| 1871 | +        foreach ($parts as $part) { | |
| 1872 | +            if ($part) { | |
| 1873 | + $resultPath .= '/' . $part; | |
| 1874 | + $result[] = $resultPath; | |
| 1875 | + } | |
| 1876 | + } | |
| 1877 | + return $result; | |
| 1878 | + } | |
| 1879 | + | |
| 1880 | + /** | |
| 1881 | + * Returns the mount point for which to lock | |
| 1882 | + * | |
| 1883 | + * @param string $absolutePath absolute path | |
| 1884 | + * @param bool $useParentMount true to return parent mount instead of whatever | |
| 1885 | + * is mounted directly on the given path, false otherwise | |
| 1886 | + * @return \OC\Files\Mount\MountPoint mount point for which to apply locks | |
| 1887 | + */ | |
| 1888 | +    private function getMountForLock($absolutePath, $useParentMount = false) { | |
| 1889 | + $results = []; | |
| 1890 | + $mount = Filesystem::getMountManager()->find($absolutePath); | |
| 1891 | +        if (!$mount) { | |
| 1892 | + return $results; | |
| 1893 | + } | |
| 1894 | + | |
| 1895 | +        if ($useParentMount) { | |
| 1896 | + // find out if something is mounted directly on the path | |
| 1897 | + $internalPath = $mount->getInternalPath($absolutePath); | |
| 1898 | +            if ($internalPath === '') { | |
| 1899 | + // resolve the parent mount instead | |
| 1900 | + $mount = Filesystem::getMountManager()->find(dirname($absolutePath)); | |
| 1901 | + } | |
| 1902 | + } | |
| 1903 | + | |
| 1904 | + return $mount; | |
| 1905 | + } | |
| 1906 | + | |
| 1907 | + /** | |
| 1908 | + * Lock the given path | |
| 1909 | + * | |
| 1910 | + * @param string $path the path of the file to lock, relative to the view | |
| 1911 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 1912 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 1913 | + * | |
| 1914 | + * @return bool False if the path is excluded from locking, true otherwise | |
| 1915 | + * @throws \OCP\Lock\LockedException if the path is already locked | |
| 1916 | + */ | |
| 1917 | +    private function lockPath($path, $type, $lockMountPoint = false) { | |
| 1918 | + $absolutePath = $this->getAbsolutePath($path); | |
| 1919 | + $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 1920 | +        if (!$this->shouldLockFile($absolutePath)) { | |
| 1921 | + return false; | |
| 1922 | + } | |
| 1923 | + | |
| 1924 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); | |
| 1925 | +        if ($mount) { | |
| 1926 | +            try { | |
| 1927 | + $storage = $mount->getStorage(); | |
| 1928 | +                if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { | |
| 1929 | + $storage->acquireLock( | |
| 1930 | + $mount->getInternalPath($absolutePath), | |
| 1931 | + $type, | |
| 1932 | + $this->lockingProvider | |
| 1933 | + ); | |
| 1934 | + } | |
| 1935 | +            } catch (\OCP\Lock\LockedException $e) { | |
| 1936 | + // rethrow with the a human-readable path | |
| 1937 | + throw new \OCP\Lock\LockedException( | |
| 1938 | + $this->getPathRelativeToFiles($absolutePath), | |
| 1939 | + $e | |
| 1940 | + ); | |
| 1941 | + } | |
| 1942 | + } | |
| 1943 | + | |
| 1944 | + return true; | |
| 1945 | + } | |
| 1946 | + | |
| 1947 | + /** | |
| 1948 | + * Change the lock type | |
| 1949 | + * | |
| 1950 | + * @param string $path the path of the file to lock, relative to the view | |
| 1951 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 1952 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 1953 | + * | |
| 1954 | + * @return bool False if the path is excluded from locking, true otherwise | |
| 1955 | + * @throws \OCP\Lock\LockedException if the path is already locked | |
| 1956 | + */ | |
| 1957 | +    public function changeLock($path, $type, $lockMountPoint = false) { | |
| 1958 | + $path = Filesystem::normalizePath($path); | |
| 1959 | + $absolutePath = $this->getAbsolutePath($path); | |
| 1960 | + $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 1961 | +        if (!$this->shouldLockFile($absolutePath)) { | |
| 1962 | + return false; | |
| 1963 | + } | |
| 1964 | + | |
| 1965 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); | |
| 1966 | +        if ($mount) { | |
| 1967 | +            try { | |
| 1968 | + $storage = $mount->getStorage(); | |
| 1969 | +                if ($storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { | |
| 1970 | + $storage->changeLock( | |
| 1971 | + $mount->getInternalPath($absolutePath), | |
| 1972 | + $type, | |
| 1973 | + $this->lockingProvider | |
| 1974 | + ); | |
| 1975 | + } | |
| 1976 | +            } catch (\OCP\Lock\LockedException $e) { | |
| 1977 | +                try { | |
| 1978 | + // rethrow with the a human-readable path | |
| 1979 | + throw new \OCP\Lock\LockedException( | |
| 1980 | + $this->getPathRelativeToFiles($absolutePath), | |
| 1981 | + $e | |
| 1982 | + ); | |
| 1983 | +                } catch (\InvalidArgumentException $e) { | |
| 1984 | + throw new \OCP\Lock\LockedException( | |
| 1985 | + $absolutePath, | |
| 1986 | + $e | |
| 1987 | + ); | |
| 1988 | + } | |
| 1989 | + } | |
| 1990 | + } | |
| 1991 | + | |
| 1992 | + return true; | |
| 1993 | + } | |
| 1994 | + | |
| 1995 | + /** | |
| 1996 | + * Unlock the given path | |
| 1997 | + * | |
| 1998 | + * @param string $path the path of the file to unlock, relative to the view | |
| 1999 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 2000 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 2001 | + * | |
| 2002 | + * @return bool False if the path is excluded from locking, true otherwise | |
| 2003 | + */ | |
| 2004 | +    private function unlockPath($path, $type, $lockMountPoint = false) { | |
| 2005 | + $absolutePath = $this->getAbsolutePath($path); | |
| 2006 | + $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 2007 | +        if (!$this->shouldLockFile($absolutePath)) { | |
| 2008 | + return false; | |
| 2009 | + } | |
| 2010 | + | |
| 2011 | + $mount = $this->getMountForLock($absolutePath, $lockMountPoint); | |
| 2012 | +        if ($mount) { | |
| 2013 | + $storage = $mount->getStorage(); | |
| 2014 | +            if ($storage && $storage->instanceOfStorage('\OCP\Files\Storage\ILockingStorage')) { | |
| 2015 | + $storage->releaseLock( | |
| 2016 | + $mount->getInternalPath($absolutePath), | |
| 2017 | + $type, | |
| 2018 | + $this->lockingProvider | |
| 2019 | + ); | |
| 2020 | + } | |
| 2021 | + } | |
| 2022 | + | |
| 2023 | + return true; | |
| 2024 | + } | |
| 2025 | + | |
| 2026 | + /** | |
| 2027 | + * Lock a path and all its parents up to the root of the view | |
| 2028 | + * | |
| 2029 | + * @param string $path the path of the file to lock relative to the view | |
| 2030 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 2031 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 2032 | + * | |
| 2033 | + * @return bool False if the path is excluded from locking, true otherwise | |
| 2034 | + */ | |
| 2035 | +    public function lockFile($path, $type, $lockMountPoint = false) { | |
| 2036 | + $absolutePath = $this->getAbsolutePath($path); | |
| 2037 | + $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 2038 | +        if (!$this->shouldLockFile($absolutePath)) { | |
| 2039 | + return false; | |
| 2040 | + } | |
| 2041 | + | |
| 2042 | + $this->lockPath($path, $type, $lockMountPoint); | |
| 2043 | + | |
| 2044 | + $parents = $this->getParents($path); | |
| 2045 | +        foreach ($parents as $parent) { | |
| 2046 | + $this->lockPath($parent, ILockingProvider::LOCK_SHARED); | |
| 2047 | + } | |
| 2048 | + | |
| 2049 | + return true; | |
| 2050 | + } | |
| 2051 | + | |
| 2052 | + /** | |
| 2053 | + * Unlock a path and all its parents up to the root of the view | |
| 2054 | + * | |
| 2055 | + * @param string $path the path of the file to lock relative to the view | |
| 2056 | + * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE | |
| 2057 | + * @param bool $lockMountPoint true to lock the mount point, false to lock the attached mount/storage | |
| 2058 | + * | |
| 2059 | + * @return bool False if the path is excluded from locking, true otherwise | |
| 2060 | + */ | |
| 2061 | +    public function unlockFile($path, $type, $lockMountPoint = false) { | |
| 2062 | + $absolutePath = $this->getAbsolutePath($path); | |
| 2063 | + $absolutePath = Filesystem::normalizePath($absolutePath); | |
| 2064 | +        if (!$this->shouldLockFile($absolutePath)) { | |
| 2065 | + return false; | |
| 2066 | + } | |
| 2067 | + | |
| 2068 | + $this->unlockPath($path, $type, $lockMountPoint); | |
| 2069 | + | |
| 2070 | + $parents = $this->getParents($path); | |
| 2071 | +        foreach ($parents as $parent) { | |
| 2072 | + $this->unlockPath($parent, ILockingProvider::LOCK_SHARED); | |
| 2073 | + } | |
| 2074 | + | |
| 2075 | + return true; | |
| 2076 | + } | |
| 2077 | + | |
| 2078 | + /** | |
| 2079 | + * Only lock files in data/user/files/ | |
| 2080 | + * | |
| 2081 | + * @param string $path Absolute path to the file/folder we try to (un)lock | |
| 2082 | + * @return bool | |
| 2083 | + */ | |
| 2084 | +    protected function shouldLockFile($path) { | |
| 2085 | + $path = Filesystem::normalizePath($path); | |
| 2086 | + | |
| 2087 | +        $pathSegments = explode('/', $path); | |
| 2088 | +        if (isset($pathSegments[2])) { | |
| 2089 | + // E.g.: /username/files/path-to-file | |
| 2090 | + return ($pathSegments[2] === 'files') && (count($pathSegments) > 3); | |
| 2091 | + } | |
| 2092 | + | |
| 2093 | + return strpos($path, '/appdata_') !== 0; | |
| 2094 | + } | |
| 2095 | + | |
| 2096 | + /** | |
| 2097 | + * Shortens the given absolute path to be relative to | |
| 2098 | + * "$user/files". | |
| 2099 | + * | |
| 2100 | + * @param string $absolutePath absolute path which is under "files" | |
| 2101 | + * | |
| 2102 | + * @return string path relative to "files" with trimmed slashes or null | |
| 2103 | + * if the path was NOT relative to files | |
| 2104 | + * | |
| 2105 | + * @throws \InvalidArgumentException if the given path was not under "files" | |
| 2106 | + * @since 8.1.0 | |
| 2107 | + */ | |
| 2108 | +    public function getPathRelativeToFiles($absolutePath) { | |
| 2109 | + $path = Filesystem::normalizePath($absolutePath); | |
| 2110 | +        $parts = explode('/', trim($path, '/'), 3); | |
| 2111 | + // "$user", "files", "path/to/dir" | |
| 2112 | +        if (!isset($parts[1]) || $parts[1] !== 'files') { | |
| 2113 | + $this->logger->error( | |
| 2114 | + '$absolutePath must be relative to "files", value is "%s"', | |
| 2115 | + [ | |
| 2116 | + $absolutePath | |
| 2117 | + ] | |
| 2118 | + ); | |
| 2119 | +            throw new \InvalidArgumentException('$absolutePath must be relative to "files"'); | |
| 2120 | + } | |
| 2121 | +        if (isset($parts[2])) { | |
| 2122 | + return $parts[2]; | |
| 2123 | + } | |
| 2124 | + return ''; | |
| 2125 | + } | |
| 2126 | + | |
| 2127 | + /** | |
| 2128 | + * @param string $filename | |
| 2129 | + * @return array | |
| 2130 | + * @throws \OC\User\NoUserException | |
| 2131 | + * @throws NotFoundException | |
| 2132 | + */ | |
| 2133 | +    public function getUidAndFilename($filename) { | |
| 2134 | + $info = $this->getFileInfo($filename); | |
| 2135 | +        if (!$info instanceof \OCP\Files\FileInfo) { | |
| 2136 | + throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); | |
| 2137 | + } | |
| 2138 | + $uid = $info->getOwner()->getUID(); | |
| 2139 | +        if ($uid != \OCP\User::getUser()) { | |
| 2140 | + Filesystem::initMountPoints($uid); | |
| 2141 | +            $ownerView = new View('/' . $uid . '/files'); | |
| 2142 | +            try { | |
| 2143 | + $filename = $ownerView->getPath($info['fileid']); | |
| 2144 | +            } catch (NotFoundException $e) { | |
| 2145 | +                throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); | |
| 2146 | + } | |
| 2147 | + } | |
| 2148 | + return [$uid, $filename]; | |
| 2149 | + } | |
| 2150 | + | |
| 2151 | + /** | |
| 2152 | + * Creates parent non-existing folders | |
| 2153 | + * | |
| 2154 | + * @param string $filePath | |
| 2155 | + * @return bool | |
| 2156 | + */ | |
| 2157 | +    private function createParentDirectories($filePath) { | |
| 2158 | +        $directoryParts = explode('/', $filePath); | |
| 2159 | + $directoryParts = array_filter($directoryParts); | |
| 2160 | +        foreach ($directoryParts as $key => $part) { | |
| 2161 | + $currentPathElements = array_slice($directoryParts, 0, $key); | |
| 2162 | +            $currentPath = '/' . implode('/', $currentPathElements); | |
| 2163 | +            if ($this->is_file($currentPath)) { | |
| 2164 | + return false; | |
| 2165 | + } | |
| 2166 | +            if (!$this->file_exists($currentPath)) { | |
| 2167 | + $this->mkdir($currentPath); | |
| 2168 | + } | |
| 2169 | + } | |
| 2170 | + | |
| 2171 | + return true; | |
| 2172 | + } | |
| 2173 | 2173 | } | 
| @@ -128,9 +128,9 @@ discard block | ||
| 128 | 128 | $path = '/'; | 
| 129 | 129 | } | 
| 130 | 130 |  		if ($path[0] !== '/') { | 
| 131 | - $path = '/' . $path; | |
| 131 | + $path = '/'.$path; | |
| 132 | 132 | } | 
| 133 | - return $this->fakeRoot . $path; | |
| 133 | + return $this->fakeRoot.$path; | |
| 134 | 134 | } | 
| 135 | 135 | |
| 136 | 136 | /** | 
| @@ -142,7 +142,7 @@ discard block | ||
| 142 | 142 |  	public function chroot($fakeRoot) { | 
| 143 | 143 |  		if (!$fakeRoot == '') { | 
| 144 | 144 |  			if ($fakeRoot[0] !== '/') { | 
| 145 | - $fakeRoot = '/' . $fakeRoot; | |
| 145 | + $fakeRoot = '/'.$fakeRoot; | |
| 146 | 146 | } | 
| 147 | 147 | } | 
| 148 | 148 | $this->fakeRoot = $fakeRoot; | 
| @@ -174,7 +174,7 @@ discard block | ||
| 174 | 174 | } | 
| 175 | 175 | |
| 176 | 176 | // missing slashes can cause wrong matches! | 
| 177 | - $root = rtrim($this->fakeRoot, '/') . '/'; | |
| 177 | + $root = rtrim($this->fakeRoot, '/').'/'; | |
| 178 | 178 | |
| 179 | 179 |  		if (strpos($path, $root) !== 0) { | 
| 180 | 180 | return null; | 
| @@ -280,7 +280,7 @@ discard block | ||
| 280 | 280 |  		if ($mount instanceof MoveableMount) { | 
| 281 | 281 | // cut of /user/files to get the relative path to data/user/files | 
| 282 | 282 |  			$pathParts = explode('/', $path, 4); | 
| 283 | - $relPath = '/' . $pathParts[3]; | |
| 283 | + $relPath = '/'.$pathParts[3]; | |
| 284 | 284 | $this->lockFile($relPath, ILockingProvider::LOCK_SHARED, true); | 
| 285 | 285 | \OC_Hook::emit( | 
| 286 | 286 | Filesystem::CLASSNAME, "umount", | 
| @@ -711,7 +711,7 @@ discard block | ||
| 711 | 711 | } | 
| 712 | 712 | $postFix = (substr($path, -1, 1) === '/') ? '/' : ''; | 
| 713 | 713 | $absolutePath = Filesystem::normalizePath($this->getAbsolutePath($path)); | 
| 714 | - $mount = Filesystem::getMountManager()->find($absolutePath . $postFix); | |
| 714 | + $mount = Filesystem::getMountManager()->find($absolutePath.$postFix); | |
| 715 | 715 |  		if ($mount and $mount->getInternalPath($absolutePath) === '') { | 
| 716 | 716 | return $this->removeMount($mount, $absolutePath); | 
| 717 | 717 | } | 
| @@ -831,7 +831,7 @@ discard block | ||
| 831 | 831 | $this->renameUpdate($storage1, $storage2, $internalPath1, $internalPath2); | 
| 832 | 832 | } | 
| 833 | 833 | } | 
| 834 | -					} catch(\Exception $e) { | |
| 834 | +					} catch (\Exception $e) { | |
| 835 | 835 | throw $e; | 
| 836 | 836 |  					} finally { | 
| 837 | 837 | $this->changeLock($path1, ILockingProvider::LOCK_SHARED, true); | 
| @@ -855,7 +855,7 @@ discard block | ||
| 855 | 855 | } | 
| 856 | 856 | } | 
| 857 | 857 | } | 
| 858 | -			} catch(\Exception $e) { | |
| 858 | +			} catch (\Exception $e) { | |
| 859 | 859 | throw $e; | 
| 860 | 860 |  			} finally { | 
| 861 | 861 | $this->unlockFile($path1, ILockingProvider::LOCK_SHARED, true); | 
| @@ -988,7 +988,7 @@ discard block | ||
| 988 | 988 | $hooks[] = 'write'; | 
| 989 | 989 | break; | 
| 990 | 990 | default: | 
| 991 | -				\OCP\Util::writeLog('core', 'invalid mode (' . $mode . ') for ' . $path, \OCP\Util::ERROR); | |
| 991 | +				\OCP\Util::writeLog('core', 'invalid mode ('.$mode.') for '.$path, \OCP\Util::ERROR); | |
| 992 | 992 | } | 
| 993 | 993 | |
| 994 | 994 |  		if ($mode !== 'r' && $mode !== 'w') { | 
| @@ -1092,7 +1092,7 @@ discard block | ||
| 1092 | 1092 | array(Filesystem::signal_param_path => $this->getHookPath($path)) | 
| 1093 | 1093 | ); | 
| 1094 | 1094 | } | 
| 1095 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); | |
| 1095 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); | |
| 1096 | 1096 |  			if ($storage) { | 
| 1097 | 1097 | $result = $storage->hash($type, $internalPath, $raw); | 
| 1098 | 1098 | return $result; | 
| @@ -1147,7 +1147,7 @@ discard block | ||
| 1147 | 1147 | |
| 1148 | 1148 | $run = $this->runHooks($hooks, $path); | 
| 1149 | 1149 | /** @var \OC\Files\Storage\Storage $storage */ | 
| 1150 | - list($storage, $internalPath) = Filesystem::resolvePath($absolutePath . $postFix); | |
| 1150 | + list($storage, $internalPath) = Filesystem::resolvePath($absolutePath.$postFix); | |
| 1151 | 1151 |  			if ($run and $storage) { | 
| 1152 | 1152 |  				if (in_array('write', $hooks) || in_array('delete', $hooks)) { | 
| 1153 | 1153 | $this->changeLock($path, ILockingProvider::LOCK_EXCLUSIVE); | 
| @@ -1186,7 +1186,7 @@ discard block | ||
| 1186 | 1186 | $unlockLater = true; | 
| 1187 | 1187 | // make sure our unlocking callback will still be called if connection is aborted | 
| 1188 | 1188 | ignore_user_abort(true); | 
| 1189 | -					$result = CallbackWrapper::wrap($result, null, null, function () use ($hooks, $path) { | |
| 1189 | +					$result = CallbackWrapper::wrap($result, null, null, function() use ($hooks, $path) { | |
| 1190 | 1190 |  						if (in_array('write', $hooks)) { | 
| 1191 | 1191 | $this->unlockFile($path, ILockingProvider::LOCK_EXCLUSIVE); | 
| 1192 | 1192 |  						} else if (in_array('read', $hooks)) { | 
| @@ -1247,7 +1247,7 @@ discard block | ||
| 1247 | 1247 | return true; | 
| 1248 | 1248 | } | 
| 1249 | 1249 | |
| 1250 | - return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot . '/'); | |
| 1250 | + return (strlen($fullPath) > strlen($defaultRoot)) && (substr($fullPath, 0, strlen($defaultRoot) + 1) === $defaultRoot.'/'); | |
| 1251 | 1251 | } | 
| 1252 | 1252 | |
| 1253 | 1253 | /** | 
| @@ -1266,7 +1266,7 @@ discard block | ||
| 1266 | 1266 |  				if ($hook != 'read') { | 
| 1267 | 1267 | \OC_Hook::emit( | 
| 1268 | 1268 | Filesystem::CLASSNAME, | 
| 1269 | - $prefix . $hook, | |
| 1269 | + $prefix.$hook, | |
| 1270 | 1270 | array( | 
| 1271 | 1271 | Filesystem::signal_param_run => &$run, | 
| 1272 | 1272 | Filesystem::signal_param_path => $path | 
| @@ -1275,7 +1275,7 @@ discard block | ||
| 1275 | 1275 |  				} elseif (!$post) { | 
| 1276 | 1276 | \OC_Hook::emit( | 
| 1277 | 1277 | Filesystem::CLASSNAME, | 
| 1278 | - $prefix . $hook, | |
| 1278 | + $prefix.$hook, | |
| 1279 | 1279 | array( | 
| 1280 | 1280 | Filesystem::signal_param_path => $path | 
| 1281 | 1281 | ) | 
| @@ -1370,7 +1370,7 @@ discard block | ||
| 1370 | 1370 | return $this->getPartFileInfo($path); | 
| 1371 | 1371 | } | 
| 1372 | 1372 | $relativePath = $path; | 
| 1373 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); | |
| 1373 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); | |
| 1374 | 1374 | |
| 1375 | 1375 | $mount = Filesystem::getMountManager()->find($path); | 
| 1376 | 1376 | $storage = $mount->getStorage(); | 
| @@ -1394,7 +1394,7 @@ discard block | ||
| 1394 | 1394 | //add the sizes of other mount points to the folder | 
| 1395 | 1395 | $extOnly = ($includeMountPoints === 'ext'); | 
| 1396 | 1396 | $mounts = Filesystem::getMountManager()->findIn($path); | 
| 1397 | -					$info->setSubMounts(array_filter($mounts, function (IMountPoint $mount) use ($extOnly) { | |
| 1397 | +					$info->setSubMounts(array_filter($mounts, function(IMountPoint $mount) use ($extOnly) { | |
| 1398 | 1398 | $subStorage = $mount->getStorage(); | 
| 1399 | 1399 | return !($extOnly && $subStorage instanceof \OCA\Files_Sharing\SharedStorage); | 
| 1400 | 1400 | })); | 
| @@ -1445,12 +1445,12 @@ discard block | ||
| 1445 | 1445 | /** | 
| 1446 | 1446 | * @var \OC\Files\FileInfo[] $fileInfos | 
| 1447 | 1447 | */ | 
| 1448 | -			$fileInfos = array_map(function (ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { | |
| 1448 | +			$fileInfos = array_map(function(ICacheEntry $content) use ($path, $storage, $mount, $sharingDisabled) { | |
| 1449 | 1449 |  				if ($sharingDisabled) { | 
| 1450 | 1450 | $content['permissions'] = $content['permissions'] & ~\OCP\Constants::PERMISSION_SHARE; | 
| 1451 | 1451 | } | 
| 1452 | 1452 | $owner = $this->getUserObjectForOwner($storage->getOwner($content['path'])); | 
| 1453 | - return new FileInfo($path . '/' . $content['name'], $storage, $content['path'], $content, $mount, $owner); | |
| 1453 | + return new FileInfo($path.'/'.$content['name'], $storage, $content['path'], $content, $mount, $owner); | |
| 1454 | 1454 | }, $contents); | 
| 1455 | 1455 | $files = array_combine($fileNames, $fileInfos); | 
| 1456 | 1456 | |
| @@ -1476,8 +1476,8 @@ discard block | ||
| 1476 | 1476 | // sometimes when the storage is not available it can be any exception | 
| 1477 | 1477 | \OCP\Util::writeLog( | 
| 1478 | 1478 | 'core', | 
| 1479 | - 'Exception while scanning storage "' . $subStorage->getId() . '": ' . | |
| 1480 | - get_class($e) . ': ' . $e->getMessage(), | |
| 1479 | + 'Exception while scanning storage "'.$subStorage->getId().'": '. | |
| 1480 | + get_class($e).': '.$e->getMessage(), | |
| 1481 | 1481 | \OCP\Util::ERROR | 
| 1482 | 1482 | ); | 
| 1483 | 1483 | continue; | 
| @@ -1507,7 +1507,7 @@ discard block | ||
| 1507 | 1507 | $rootEntry['permissions'] = $permissions & (\OCP\Constants::PERMISSION_ALL - (\OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE)); | 
| 1508 | 1508 | } | 
| 1509 | 1509 | |
| 1510 | - $rootEntry['path'] = substr(Filesystem::normalizePath($path . '/' . $rootEntry['name']), strlen($user) + 2); // full path without /$user/ | |
| 1510 | + $rootEntry['path'] = substr(Filesystem::normalizePath($path.'/'.$rootEntry['name']), strlen($user) + 2); // full path without /$user/ | |
| 1511 | 1511 | |
| 1512 | 1512 | // if sharing was disabled for the user we remove the share permissions | 
| 1513 | 1513 |  							if (\OCP\Util::isSharingDisabledForUser()) { | 
| @@ -1515,14 +1515,14 @@ discard block | ||
| 1515 | 1515 | } | 
| 1516 | 1516 | |
| 1517 | 1517 |  							$owner = $this->getUserObjectForOwner($subStorage->getOwner('')); | 
| 1518 | - $files[$rootEntry->getName()] = new FileInfo($path . '/' . $rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); | |
| 1518 | + $files[$rootEntry->getName()] = new FileInfo($path.'/'.$rootEntry['name'], $subStorage, '', $rootEntry, $mount, $owner); | |
| 1519 | 1519 | } | 
| 1520 | 1520 | } | 
| 1521 | 1521 | } | 
| 1522 | 1522 | } | 
| 1523 | 1523 | |
| 1524 | 1524 |  			if ($mimetype_filter) { | 
| 1525 | -				$files = array_filter($files, function (FileInfo $file) use ($mimetype_filter) { | |
| 1525 | +				$files = array_filter($files, function(FileInfo $file) use ($mimetype_filter) { | |
| 1526 | 1526 |  					if (strpos($mimetype_filter, '/')) { | 
| 1527 | 1527 | return $file->getMimetype() === $mimetype_filter; | 
| 1528 | 1528 |  					} else { | 
| @@ -1551,7 +1551,7 @@ discard block | ||
| 1551 | 1551 |  		if ($data instanceof FileInfo) { | 
| 1552 | 1552 | $data = $data->getData(); | 
| 1553 | 1553 | } | 
| 1554 | - $path = Filesystem::normalizePath($this->fakeRoot . '/' . $path); | |
| 1554 | + $path = Filesystem::normalizePath($this->fakeRoot.'/'.$path); | |
| 1555 | 1555 | /** | 
| 1556 | 1556 | * @var \OC\Files\Storage\Storage $storage | 
| 1557 | 1557 | * @var string $internalPath | 
| @@ -1578,7 +1578,7 @@ discard block | ||
| 1578 | 1578 | * @return FileInfo[] | 
| 1579 | 1579 | */ | 
| 1580 | 1580 |  	public function search($query) { | 
| 1581 | -		return $this->searchCommon('search', array('%' . $query . '%')); | |
| 1581 | +		return $this->searchCommon('search', array('%'.$query.'%')); | |
| 1582 | 1582 | } | 
| 1583 | 1583 | |
| 1584 | 1584 | /** | 
| @@ -1629,10 +1629,10 @@ discard block | ||
| 1629 | 1629 | |
| 1630 | 1630 | $results = call_user_func_array(array($cache, $method), $args); | 
| 1631 | 1631 |  			foreach ($results as $result) { | 
| 1632 | -				if (substr($mountPoint . $result['path'], 0, $rootLength + 1) === $this->fakeRoot . '/') { | |
| 1632 | +				if (substr($mountPoint.$result['path'], 0, $rootLength + 1) === $this->fakeRoot.'/') { | |
| 1633 | 1633 | $internalPath = $result['path']; | 
| 1634 | - $path = $mountPoint . $result['path']; | |
| 1635 | - $result['path'] = substr($mountPoint . $result['path'], $rootLength); | |
| 1634 | + $path = $mountPoint.$result['path']; | |
| 1635 | + $result['path'] = substr($mountPoint.$result['path'], $rootLength); | |
| 1636 | 1636 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); | 
| 1637 | 1637 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); | 
| 1638 | 1638 | } | 
| @@ -1650,8 +1650,8 @@ discard block | ||
| 1650 | 1650 |  					if ($results) { | 
| 1651 | 1651 |  						foreach ($results as $result) { | 
| 1652 | 1652 | $internalPath = $result['path']; | 
| 1653 | - $result['path'] = rtrim($relativeMountPoint . $result['path'], '/'); | |
| 1654 | - $path = rtrim($mountPoint . $internalPath, '/'); | |
| 1653 | + $result['path'] = rtrim($relativeMountPoint.$result['path'], '/'); | |
| 1654 | + $path = rtrim($mountPoint.$internalPath, '/'); | |
| 1655 | 1655 | $owner = \OC::$server->getUserManager()->get($storage->getOwner($internalPath)); | 
| 1656 | 1656 | $files[] = new FileInfo($path, $storage, $internalPath, $result, $mount, $owner); | 
| 1657 | 1657 | } | 
| @@ -1672,7 +1672,7 @@ discard block | ||
| 1672 | 1672 |  	public function getOwner($path) { | 
| 1673 | 1673 | $info = $this->getFileInfo($path); | 
| 1674 | 1674 |  		if (!$info) { | 
| 1675 | - throw new NotFoundException($path . ' not found while trying to get owner'); | |
| 1675 | + throw new NotFoundException($path.' not found while trying to get owner'); | |
| 1676 | 1676 | } | 
| 1677 | 1677 | return $info->getOwner()->getUID(); | 
| 1678 | 1678 | } | 
| @@ -1706,7 +1706,7 @@ discard block | ||
| 1706 | 1706 | * @return string | 
| 1707 | 1707 | */ | 
| 1708 | 1708 |  	public function getPath($id) { | 
| 1709 | - $id = (int)$id; | |
| 1709 | + $id = (int) $id; | |
| 1710 | 1710 | $manager = Filesystem::getMountManager(); | 
| 1711 | 1711 | $mounts = $manager->findIn($this->fakeRoot); | 
| 1712 | 1712 | $mounts[] = $manager->find($this->fakeRoot); | 
| @@ -1724,7 +1724,7 @@ discard block | ||
| 1724 | 1724 | } | 
| 1725 | 1725 | $internalPath = $cache->getPathById($id); | 
| 1726 | 1726 |  				if (is_string($internalPath)) { | 
| 1727 | - $fullPath = $mount->getMountPoint() . $internalPath; | |
| 1727 | + $fullPath = $mount->getMountPoint().$internalPath; | |
| 1728 | 1728 |  					if (!is_null($path = $this->getRelativePath($fullPath))) { | 
| 1729 | 1729 | return $path; | 
| 1730 | 1730 | } | 
| @@ -1767,10 +1767,10 @@ discard block | ||
| 1767 | 1767 | } | 
| 1768 | 1768 | |
| 1769 | 1769 | // note: cannot use the view because the target is already locked | 
| 1770 | - $fileId = (int)$targetStorage->getCache()->getId($targetInternalPath); | |
| 1770 | + $fileId = (int) $targetStorage->getCache()->getId($targetInternalPath); | |
| 1771 | 1771 |  		if ($fileId === -1) { | 
| 1772 | 1772 | // target might not exist, need to check parent instead | 
| 1773 | - $fileId = (int)$targetStorage->getCache()->getId(dirname($targetInternalPath)); | |
| 1773 | + $fileId = (int) $targetStorage->getCache()->getId(dirname($targetInternalPath)); | |
| 1774 | 1774 | } | 
| 1775 | 1775 | |
| 1776 | 1776 | // check if any of the parents were shared by the current owner (include collections) | 
| @@ -1870,7 +1870,7 @@ discard block | ||
| 1870 | 1870 | $resultPath = ''; | 
| 1871 | 1871 |  		foreach ($parts as $part) { | 
| 1872 | 1872 |  			if ($part) { | 
| 1873 | - $resultPath .= '/' . $part; | |
| 1873 | + $resultPath .= '/'.$part; | |
| 1874 | 1874 | $result[] = $resultPath; | 
| 1875 | 1875 | } | 
| 1876 | 1876 | } | 
| @@ -2133,16 +2133,16 @@ discard block | ||
| 2133 | 2133 |  	public function getUidAndFilename($filename) { | 
| 2134 | 2134 | $info = $this->getFileInfo($filename); | 
| 2135 | 2135 |  		if (!$info instanceof \OCP\Files\FileInfo) { | 
| 2136 | - throw new NotFoundException($this->getAbsolutePath($filename) . ' not found'); | |
| 2136 | + throw new NotFoundException($this->getAbsolutePath($filename).' not found'); | |
| 2137 | 2137 | } | 
| 2138 | 2138 | $uid = $info->getOwner()->getUID(); | 
| 2139 | 2139 |  		if ($uid != \OCP\User::getUser()) { | 
| 2140 | 2140 | Filesystem::initMountPoints($uid); | 
| 2141 | -			$ownerView = new View('/' . $uid . '/files'); | |
| 2141 | +			$ownerView = new View('/'.$uid.'/files'); | |
| 2142 | 2142 |  			try { | 
| 2143 | 2143 | $filename = $ownerView->getPath($info['fileid']); | 
| 2144 | 2144 |  			} catch (NotFoundException $e) { | 
| 2145 | -				throw new NotFoundException('File with id ' . $info['fileid'] . ' not found for user ' . $uid); | |
| 2145 | +				throw new NotFoundException('File with id '.$info['fileid'].' not found for user '.$uid); | |
| 2146 | 2146 | } | 
| 2147 | 2147 | } | 
| 2148 | 2148 | return [$uid, $filename]; | 
| @@ -2159,7 +2159,7 @@ discard block | ||
| 2159 | 2159 | $directoryParts = array_filter($directoryParts); | 
| 2160 | 2160 |  		foreach ($directoryParts as $key => $part) { | 
| 2161 | 2161 | $currentPathElements = array_slice($directoryParts, 0, $key); | 
| 2162 | -			$currentPath = '/' . implode('/', $currentPathElements); | |
| 2162 | +			$currentPath = '/'.implode('/', $currentPathElements); | |
| 2163 | 2163 |  			if ($this->is_file($currentPath)) { | 
| 2164 | 2164 | return false; | 
| 2165 | 2165 | } | 
| @@ -42,328 +42,328 @@ | ||
| 42 | 42 | * Cache mounts points per user in the cache so we can easilly look them up | 
| 43 | 43 | */ | 
| 44 | 44 |  class UserMountCache implements IUserMountCache { | 
| 45 | - /** | |
| 46 | - * @var IDBConnection | |
| 47 | - */ | |
| 48 | - private $connection; | |
| 49 | - | |
| 50 | - /** | |
| 51 | - * @var IUserManager | |
| 52 | - */ | |
| 53 | - private $userManager; | |
| 54 | - | |
| 55 | - /** | |
| 56 | - * Cached mount info. | |
| 57 | - * Map of $userId to ICachedMountInfo. | |
| 58 | - * | |
| 59 | - * @var ICache | |
| 60 | - **/ | |
| 61 | - private $mountsForUsers; | |
| 62 | - | |
| 63 | - /** | |
| 64 | - * @var ILogger | |
| 65 | - */ | |
| 66 | - private $logger; | |
| 67 | - | |
| 68 | - /** | |
| 69 | - * @var ICache | |
| 70 | - */ | |
| 71 | - private $cacheInfoCache; | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * UserMountCache constructor. | |
| 75 | - * | |
| 76 | - * @param IDBConnection $connection | |
| 77 | - * @param IUserManager $userManager | |
| 78 | - * @param ILogger $logger | |
| 79 | - */ | |
| 80 | -	public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { | |
| 81 | - $this->connection = $connection; | |
| 82 | - $this->userManager = $userManager; | |
| 83 | - $this->logger = $logger; | |
| 84 | - $this->cacheInfoCache = new CappedMemoryCache(); | |
| 85 | - $this->mountsForUsers = new CappedMemoryCache(); | |
| 86 | - } | |
| 87 | - | |
| 88 | -	public function registerMounts(IUser $user, array $mounts) { | |
| 89 | - // filter out non-proper storages coming from unit tests | |
| 90 | -		$mounts = array_filter($mounts, function (IMountPoint $mount) { | |
| 91 | - return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); | |
| 92 | - }); | |
| 93 | - /** @var ICachedMountInfo[] $newMounts */ | |
| 94 | -		$newMounts = array_map(function (IMountPoint $mount) use ($user) { | |
| 95 | - // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) | |
| 96 | -			if ($mount->getStorageRootId() === -1) { | |
| 97 | - return null; | |
| 98 | -			} else { | |
| 99 | - return new LazyStorageMountInfo($user, $mount); | |
| 100 | - } | |
| 101 | - }, $mounts); | |
| 102 | - $newMounts = array_values(array_filter($newMounts)); | |
| 103 | -		$newMountRootIds = array_map(function (ICachedMountInfo $mount) { | |
| 104 | - return $mount->getRootId(); | |
| 105 | - }, $newMounts); | |
| 106 | - $newMounts = array_combine($newMountRootIds, $newMounts); | |
| 107 | - | |
| 108 | - $cachedMounts = $this->getMountsForUser($user); | |
| 109 | -		$cachedMountRootIds = array_map(function (ICachedMountInfo $mount) { | |
| 110 | - return $mount->getRootId(); | |
| 111 | - }, $cachedMounts); | |
| 112 | - $cachedMounts = array_combine($cachedMountRootIds, $cachedMounts); | |
| 113 | - | |
| 114 | - $addedMounts = []; | |
| 115 | - $removedMounts = []; | |
| 116 | - | |
| 117 | -		foreach ($newMounts as $rootId => $newMount) { | |
| 118 | -			if (!isset($cachedMounts[$rootId])) { | |
| 119 | - $addedMounts[] = $newMount; | |
| 120 | - } | |
| 121 | - } | |
| 122 | - | |
| 123 | -		foreach ($cachedMounts as $rootId => $cachedMount) { | |
| 124 | -			if (!isset($newMounts[$rootId])) { | |
| 125 | - $removedMounts[] = $cachedMount; | |
| 126 | - } | |
| 127 | - } | |
| 128 | - | |
| 129 | - $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); | |
| 130 | - | |
| 131 | -		foreach ($addedMounts as $mount) { | |
| 132 | - $this->addToCache($mount); | |
| 133 | - $this->mountsForUsers[$user->getUID()][] = $mount; | |
| 134 | - } | |
| 135 | -		foreach ($removedMounts as $mount) { | |
| 136 | - $this->removeFromCache($mount); | |
| 137 | - $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); | |
| 138 | - unset($this->mountsForUsers[$user->getUID()][$index]); | |
| 139 | - } | |
| 140 | -		foreach ($changedMounts as $mount) { | |
| 141 | - $this->updateCachedMount($mount); | |
| 142 | - } | |
| 143 | - } | |
| 144 | - | |
| 145 | - /** | |
| 146 | - * @param ICachedMountInfo[] $newMounts | |
| 147 | - * @param ICachedMountInfo[] $cachedMounts | |
| 148 | - * @return ICachedMountInfo[] | |
| 149 | - */ | |
| 150 | -	private function findChangedMounts(array $newMounts, array $cachedMounts) { | |
| 151 | - $new = []; | |
| 152 | -		foreach ($newMounts as $mount) { | |
| 153 | - $new[$mount->getRootId()] = $mount; | |
| 154 | - } | |
| 155 | - $changed = []; | |
| 156 | -		foreach ($cachedMounts as $cachedMount) { | |
| 157 | - $rootId = $cachedMount->getRootId(); | |
| 158 | -			if (isset($new[$rootId])) { | |
| 159 | - $newMount = $new[$rootId]; | |
| 160 | - if ( | |
| 161 | - $newMount->getMountPoint() !== $cachedMount->getMountPoint() || | |
| 162 | - $newMount->getStorageId() !== $cachedMount->getStorageId() || | |
| 163 | - $newMount->getMountId() !== $cachedMount->getMountId() | |
| 164 | -				) { | |
| 165 | - $changed[] = $newMount; | |
| 166 | - } | |
| 167 | - } | |
| 168 | - } | |
| 169 | - return $changed; | |
| 170 | - } | |
| 171 | - | |
| 172 | -	private function addToCache(ICachedMountInfo $mount) { | |
| 173 | -		if ($mount->getStorageId() !== -1) { | |
| 174 | -			$this->connection->insertIfNotExist('*PREFIX*mounts', [ | |
| 175 | - 'storage_id' => $mount->getStorageId(), | |
| 176 | - 'root_id' => $mount->getRootId(), | |
| 177 | - 'user_id' => $mount->getUser()->getUID(), | |
| 178 | - 'mount_point' => $mount->getMountPoint(), | |
| 179 | - 'mount_id' => $mount->getMountId() | |
| 180 | - ], ['root_id', 'user_id']); | |
| 181 | -		} else { | |
| 182 | - // in some cases this is legitimate, like orphaned shares | |
| 183 | -			$this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); | |
| 184 | - } | |
| 185 | - } | |
| 186 | - | |
| 187 | -	private function updateCachedMount(ICachedMountInfo $mount) { | |
| 188 | - $builder = $this->connection->getQueryBuilder(); | |
| 189 | - | |
| 190 | -		$query = $builder->update('mounts') | |
| 191 | -			->set('storage_id', $builder->createNamedParameter($mount->getStorageId())) | |
| 192 | -			->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) | |
| 193 | -			->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT)) | |
| 194 | -			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) | |
| 195 | -			->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); | |
| 196 | - | |
| 197 | - $query->execute(); | |
| 198 | - } | |
| 199 | - | |
| 200 | -	private function removeFromCache(ICachedMountInfo $mount) { | |
| 201 | - $builder = $this->connection->getQueryBuilder(); | |
| 202 | - | |
| 203 | -		$query = $builder->delete('mounts') | |
| 204 | -			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) | |
| 205 | -			->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); | |
| 206 | - $query->execute(); | |
| 207 | - } | |
| 208 | - | |
| 209 | -	private function dbRowToMountInfo(array $row) { | |
| 210 | - $user = $this->userManager->get($row['user_id']); | |
| 211 | -		if (is_null($user)) { | |
| 212 | - return null; | |
| 213 | - } | |
| 214 | - $mount_id = $row['mount_id']; | |
| 215 | -		if (!is_null($mount_id)) { | |
| 216 | - $mount_id = (int)$mount_id; | |
| 217 | - } | |
| 218 | - return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $mount_id, isset($row['path'])? $row['path']:''); | |
| 219 | - } | |
| 220 | - | |
| 221 | - /** | |
| 222 | - * @param IUser $user | |
| 223 | - * @return ICachedMountInfo[] | |
| 224 | - */ | |
| 225 | -	public function getMountsForUser(IUser $user) { | |
| 226 | -		if (!isset($this->mountsForUsers[$user->getUID()])) { | |
| 227 | - $builder = $this->connection->getQueryBuilder(); | |
| 228 | -			$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') | |
| 229 | -				->from('mounts', 'm') | |
| 230 | -				->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) | |
| 231 | -				->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); | |
| 232 | - | |
| 233 | - $rows = $query->execute()->fetchAll(); | |
| 234 | - | |
| 235 | - $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); | |
| 236 | - } | |
| 237 | - return $this->mountsForUsers[$user->getUID()]; | |
| 238 | - } | |
| 239 | - | |
| 240 | - /** | |
| 241 | - * @param int $numericStorageId | |
| 242 | - * @param string|null $user limit the results to a single user | |
| 243 | - * @return CachedMountInfo[] | |
| 244 | - */ | |
| 245 | -	public function getMountsForStorageId($numericStorageId, $user = null) { | |
| 246 | - $builder = $this->connection->getQueryBuilder(); | |
| 247 | -		$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') | |
| 248 | -			->from('mounts', 'm') | |
| 249 | -			->innerJoin('m', 'filecache', 'f' , $builder->expr()->eq('m.root_id', 'f.fileid')) | |
| 250 | -			->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); | |
| 251 | - | |
| 252 | -		if ($user) { | |
| 253 | -			$query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user))); | |
| 254 | - } | |
| 255 | - | |
| 256 | - $rows = $query->execute()->fetchAll(); | |
| 257 | - | |
| 258 | - return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); | |
| 259 | - } | |
| 260 | - | |
| 261 | - /** | |
| 262 | - * @param int $rootFileId | |
| 263 | - * @return CachedMountInfo[] | |
| 264 | - */ | |
| 265 | -	public function getMountsForRootId($rootFileId) { | |
| 266 | - $builder = $this->connection->getQueryBuilder(); | |
| 267 | -		$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') | |
| 268 | -			->from('mounts', 'm') | |
| 269 | -			->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) | |
| 270 | -			->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); | |
| 271 | - | |
| 272 | - $rows = $query->execute()->fetchAll(); | |
| 273 | - | |
| 274 | - return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); | |
| 275 | - } | |
| 276 | - | |
| 277 | - /** | |
| 278 | - * @param $fileId | |
| 279 | - * @return array | |
| 280 | - * @throws \OCP\Files\NotFoundException | |
| 281 | - */ | |
| 282 | -	private function getCacheInfoFromFileId($fileId) { | |
| 283 | -		if (!isset($this->cacheInfoCache[$fileId])) { | |
| 284 | - $builder = $this->connection->getQueryBuilder(); | |
| 285 | -			$query = $builder->select('storage', 'path', 'mimetype') | |
| 286 | -				->from('filecache') | |
| 287 | -				->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); | |
| 288 | - | |
| 289 | - $row = $query->execute()->fetch(); | |
| 290 | -			if (is_array($row)) { | |
| 291 | - $this->cacheInfoCache[$fileId] = [ | |
| 292 | - (int)$row['storage'], | |
| 293 | - $row['path'], | |
| 294 | - (int)$row['mimetype'] | |
| 295 | - ]; | |
| 296 | -			} else { | |
| 297 | -				throw new NotFoundException('File with id "' . $fileId . '" not found'); | |
| 298 | - } | |
| 299 | - } | |
| 300 | - return $this->cacheInfoCache[$fileId]; | |
| 301 | - } | |
| 302 | - | |
| 303 | - /** | |
| 304 | - * @param int $fileId | |
| 305 | - * @param string|null $user optionally restrict the results to a single user | |
| 306 | - * @return ICachedMountFileInfo[] | |
| 307 | - * @since 9.0.0 | |
| 308 | - */ | |
| 309 | -	public function getMountsForFileId($fileId, $user = null) { | |
| 310 | -		try { | |
| 311 | - list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); | |
| 312 | -		} catch (NotFoundException $e) { | |
| 313 | - return []; | |
| 314 | - } | |
| 315 | - $mountsForStorage = $this->getMountsForStorageId($storageId, $user); | |
| 316 | - | |
| 317 | - // filter mounts that are from the same storage but a different directory | |
| 318 | -		$filteredMounts = array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { | |
| 319 | -			if ($fileId === $mount->getRootId()) { | |
| 320 | - return true; | |
| 321 | - } | |
| 322 | - $internalMountPath = $mount->getRootInternalPath(); | |
| 323 | - | |
| 324 | - return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; | |
| 325 | - }); | |
| 326 | - | |
| 327 | -		return array_map(function (ICachedMountInfo $mount) use ($internalPath) { | |
| 328 | - return new CachedMountFileInfo( | |
| 329 | - $mount->getUser(), | |
| 330 | - $mount->getStorageId(), | |
| 331 | - $mount->getRootId(), | |
| 332 | - $mount->getMountPoint(), | |
| 333 | - $mount->getMountId(), | |
| 334 | - $mount->getRootInternalPath(), | |
| 335 | - $internalPath | |
| 336 | - ); | |
| 337 | - }, $filteredMounts); | |
| 338 | - } | |
| 339 | - | |
| 340 | - /** | |
| 341 | - * Remove all cached mounts for a user | |
| 342 | - * | |
| 343 | - * @param IUser $user | |
| 344 | - */ | |
| 345 | -	public function removeUserMounts(IUser $user) { | |
| 346 | - $builder = $this->connection->getQueryBuilder(); | |
| 347 | - | |
| 348 | -		$query = $builder->delete('mounts') | |
| 349 | -			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); | |
| 350 | - $query->execute(); | |
| 351 | - } | |
| 352 | - | |
| 353 | -	public function removeUserStorageMount($storageId, $userId) { | |
| 354 | - $builder = $this->connection->getQueryBuilder(); | |
| 355 | - | |
| 356 | -		$query = $builder->delete('mounts') | |
| 357 | -			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId))) | |
| 358 | -			->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); | |
| 359 | - $query->execute(); | |
| 360 | - } | |
| 361 | - | |
| 362 | -	public function remoteStorageMounts($storageId) { | |
| 363 | - $builder = $this->connection->getQueryBuilder(); | |
| 364 | - | |
| 365 | -		$query = $builder->delete('mounts') | |
| 366 | -			->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); | |
| 367 | - $query->execute(); | |
| 368 | - } | |
| 45 | + /** | |
| 46 | + * @var IDBConnection | |
| 47 | + */ | |
| 48 | + private $connection; | |
| 49 | + | |
| 50 | + /** | |
| 51 | + * @var IUserManager | |
| 52 | + */ | |
| 53 | + private $userManager; | |
| 54 | + | |
| 55 | + /** | |
| 56 | + * Cached mount info. | |
| 57 | + * Map of $userId to ICachedMountInfo. | |
| 58 | + * | |
| 59 | + * @var ICache | |
| 60 | + **/ | |
| 61 | + private $mountsForUsers; | |
| 62 | + | |
| 63 | + /** | |
| 64 | + * @var ILogger | |
| 65 | + */ | |
| 66 | + private $logger; | |
| 67 | + | |
| 68 | + /** | |
| 69 | + * @var ICache | |
| 70 | + */ | |
| 71 | + private $cacheInfoCache; | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * UserMountCache constructor. | |
| 75 | + * | |
| 76 | + * @param IDBConnection $connection | |
| 77 | + * @param IUserManager $userManager | |
| 78 | + * @param ILogger $logger | |
| 79 | + */ | |
| 80 | +    public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) { | |
| 81 | + $this->connection = $connection; | |
| 82 | + $this->userManager = $userManager; | |
| 83 | + $this->logger = $logger; | |
| 84 | + $this->cacheInfoCache = new CappedMemoryCache(); | |
| 85 | + $this->mountsForUsers = new CappedMemoryCache(); | |
| 86 | + } | |
| 87 | + | |
| 88 | +    public function registerMounts(IUser $user, array $mounts) { | |
| 89 | + // filter out non-proper storages coming from unit tests | |
| 90 | +        $mounts = array_filter($mounts, function (IMountPoint $mount) { | |
| 91 | + return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); | |
| 92 | + }); | |
| 93 | + /** @var ICachedMountInfo[] $newMounts */ | |
| 94 | +        $newMounts = array_map(function (IMountPoint $mount) use ($user) { | |
| 95 | + // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) | |
| 96 | +            if ($mount->getStorageRootId() === -1) { | |
| 97 | + return null; | |
| 98 | +            } else { | |
| 99 | + return new LazyStorageMountInfo($user, $mount); | |
| 100 | + } | |
| 101 | + }, $mounts); | |
| 102 | + $newMounts = array_values(array_filter($newMounts)); | |
| 103 | +        $newMountRootIds = array_map(function (ICachedMountInfo $mount) { | |
| 104 | + return $mount->getRootId(); | |
| 105 | + }, $newMounts); | |
| 106 | + $newMounts = array_combine($newMountRootIds, $newMounts); | |
| 107 | + | |
| 108 | + $cachedMounts = $this->getMountsForUser($user); | |
| 109 | +        $cachedMountRootIds = array_map(function (ICachedMountInfo $mount) { | |
| 110 | + return $mount->getRootId(); | |
| 111 | + }, $cachedMounts); | |
| 112 | + $cachedMounts = array_combine($cachedMountRootIds, $cachedMounts); | |
| 113 | + | |
| 114 | + $addedMounts = []; | |
| 115 | + $removedMounts = []; | |
| 116 | + | |
| 117 | +        foreach ($newMounts as $rootId => $newMount) { | |
| 118 | +            if (!isset($cachedMounts[$rootId])) { | |
| 119 | + $addedMounts[] = $newMount; | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | +        foreach ($cachedMounts as $rootId => $cachedMount) { | |
| 124 | +            if (!isset($newMounts[$rootId])) { | |
| 125 | + $removedMounts[] = $cachedMount; | |
| 126 | + } | |
| 127 | + } | |
| 128 | + | |
| 129 | + $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts); | |
| 130 | + | |
| 131 | +        foreach ($addedMounts as $mount) { | |
| 132 | + $this->addToCache($mount); | |
| 133 | + $this->mountsForUsers[$user->getUID()][] = $mount; | |
| 134 | + } | |
| 135 | +        foreach ($removedMounts as $mount) { | |
| 136 | + $this->removeFromCache($mount); | |
| 137 | + $index = array_search($mount, $this->mountsForUsers[$user->getUID()]); | |
| 138 | + unset($this->mountsForUsers[$user->getUID()][$index]); | |
| 139 | + } | |
| 140 | +        foreach ($changedMounts as $mount) { | |
| 141 | + $this->updateCachedMount($mount); | |
| 142 | + } | |
| 143 | + } | |
| 144 | + | |
| 145 | + /** | |
| 146 | + * @param ICachedMountInfo[] $newMounts | |
| 147 | + * @param ICachedMountInfo[] $cachedMounts | |
| 148 | + * @return ICachedMountInfo[] | |
| 149 | + */ | |
| 150 | +    private function findChangedMounts(array $newMounts, array $cachedMounts) { | |
| 151 | + $new = []; | |
| 152 | +        foreach ($newMounts as $mount) { | |
| 153 | + $new[$mount->getRootId()] = $mount; | |
| 154 | + } | |
| 155 | + $changed = []; | |
| 156 | +        foreach ($cachedMounts as $cachedMount) { | |
| 157 | + $rootId = $cachedMount->getRootId(); | |
| 158 | +            if (isset($new[$rootId])) { | |
| 159 | + $newMount = $new[$rootId]; | |
| 160 | + if ( | |
| 161 | + $newMount->getMountPoint() !== $cachedMount->getMountPoint() || | |
| 162 | + $newMount->getStorageId() !== $cachedMount->getStorageId() || | |
| 163 | + $newMount->getMountId() !== $cachedMount->getMountId() | |
| 164 | +                ) { | |
| 165 | + $changed[] = $newMount; | |
| 166 | + } | |
| 167 | + } | |
| 168 | + } | |
| 169 | + return $changed; | |
| 170 | + } | |
| 171 | + | |
| 172 | +    private function addToCache(ICachedMountInfo $mount) { | |
| 173 | +        if ($mount->getStorageId() !== -1) { | |
| 174 | +            $this->connection->insertIfNotExist('*PREFIX*mounts', [ | |
| 175 | + 'storage_id' => $mount->getStorageId(), | |
| 176 | + 'root_id' => $mount->getRootId(), | |
| 177 | + 'user_id' => $mount->getUser()->getUID(), | |
| 178 | + 'mount_point' => $mount->getMountPoint(), | |
| 179 | + 'mount_id' => $mount->getMountId() | |
| 180 | + ], ['root_id', 'user_id']); | |
| 181 | +        } else { | |
| 182 | + // in some cases this is legitimate, like orphaned shares | |
| 183 | +            $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); | |
| 184 | + } | |
| 185 | + } | |
| 186 | + | |
| 187 | +    private function updateCachedMount(ICachedMountInfo $mount) { | |
| 188 | + $builder = $this->connection->getQueryBuilder(); | |
| 189 | + | |
| 190 | +        $query = $builder->update('mounts') | |
| 191 | +            ->set('storage_id', $builder->createNamedParameter($mount->getStorageId())) | |
| 192 | +            ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint())) | |
| 193 | +            ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT)) | |
| 194 | +            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) | |
| 195 | +            ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); | |
| 196 | + | |
| 197 | + $query->execute(); | |
| 198 | + } | |
| 199 | + | |
| 200 | +    private function removeFromCache(ICachedMountInfo $mount) { | |
| 201 | + $builder = $this->connection->getQueryBuilder(); | |
| 202 | + | |
| 203 | +        $query = $builder->delete('mounts') | |
| 204 | +            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID()))) | |
| 205 | +            ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT))); | |
| 206 | + $query->execute(); | |
| 207 | + } | |
| 208 | + | |
| 209 | +    private function dbRowToMountInfo(array $row) { | |
| 210 | + $user = $this->userManager->get($row['user_id']); | |
| 211 | +        if (is_null($user)) { | |
| 212 | + return null; | |
| 213 | + } | |
| 214 | + $mount_id = $row['mount_id']; | |
| 215 | +        if (!is_null($mount_id)) { | |
| 216 | + $mount_id = (int)$mount_id; | |
| 217 | + } | |
| 218 | + return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $mount_id, isset($row['path'])? $row['path']:''); | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * @param IUser $user | |
| 223 | + * @return ICachedMountInfo[] | |
| 224 | + */ | |
| 225 | +    public function getMountsForUser(IUser $user) { | |
| 226 | +        if (!isset($this->mountsForUsers[$user->getUID()])) { | |
| 227 | + $builder = $this->connection->getQueryBuilder(); | |
| 228 | +            $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') | |
| 229 | +                ->from('mounts', 'm') | |
| 230 | +                ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) | |
| 231 | +                ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID()))); | |
| 232 | + | |
| 233 | + $rows = $query->execute()->fetchAll(); | |
| 234 | + | |
| 235 | + $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); | |
| 236 | + } | |
| 237 | + return $this->mountsForUsers[$user->getUID()]; | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * @param int $numericStorageId | |
| 242 | + * @param string|null $user limit the results to a single user | |
| 243 | + * @return CachedMountInfo[] | |
| 244 | + */ | |
| 245 | +    public function getMountsForStorageId($numericStorageId, $user = null) { | |
| 246 | + $builder = $this->connection->getQueryBuilder(); | |
| 247 | +        $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') | |
| 248 | +            ->from('mounts', 'm') | |
| 249 | +            ->innerJoin('m', 'filecache', 'f' , $builder->expr()->eq('m.root_id', 'f.fileid')) | |
| 250 | +            ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); | |
| 251 | + | |
| 252 | +        if ($user) { | |
| 253 | +            $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user))); | |
| 254 | + } | |
| 255 | + | |
| 256 | + $rows = $query->execute()->fetchAll(); | |
| 257 | + | |
| 258 | + return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); | |
| 259 | + } | |
| 260 | + | |
| 261 | + /** | |
| 262 | + * @param int $rootFileId | |
| 263 | + * @return CachedMountInfo[] | |
| 264 | + */ | |
| 265 | +    public function getMountsForRootId($rootFileId) { | |
| 266 | + $builder = $this->connection->getQueryBuilder(); | |
| 267 | +        $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') | |
| 268 | +            ->from('mounts', 'm') | |
| 269 | +            ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) | |
| 270 | +            ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT))); | |
| 271 | + | |
| 272 | + $rows = $query->execute()->fetchAll(); | |
| 273 | + | |
| 274 | + return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows)); | |
| 275 | + } | |
| 276 | + | |
| 277 | + /** | |
| 278 | + * @param $fileId | |
| 279 | + * @return array | |
| 280 | + * @throws \OCP\Files\NotFoundException | |
| 281 | + */ | |
| 282 | +    private function getCacheInfoFromFileId($fileId) { | |
| 283 | +        if (!isset($this->cacheInfoCache[$fileId])) { | |
| 284 | + $builder = $this->connection->getQueryBuilder(); | |
| 285 | +            $query = $builder->select('storage', 'path', 'mimetype') | |
| 286 | +                ->from('filecache') | |
| 287 | +                ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT))); | |
| 288 | + | |
| 289 | + $row = $query->execute()->fetch(); | |
| 290 | +            if (is_array($row)) { | |
| 291 | + $this->cacheInfoCache[$fileId] = [ | |
| 292 | + (int)$row['storage'], | |
| 293 | + $row['path'], | |
| 294 | + (int)$row['mimetype'] | |
| 295 | + ]; | |
| 296 | +            } else { | |
| 297 | +                throw new NotFoundException('File with id "' . $fileId . '" not found'); | |
| 298 | + } | |
| 299 | + } | |
| 300 | + return $this->cacheInfoCache[$fileId]; | |
| 301 | + } | |
| 302 | + | |
| 303 | + /** | |
| 304 | + * @param int $fileId | |
| 305 | + * @param string|null $user optionally restrict the results to a single user | |
| 306 | + * @return ICachedMountFileInfo[] | |
| 307 | + * @since 9.0.0 | |
| 308 | + */ | |
| 309 | +    public function getMountsForFileId($fileId, $user = null) { | |
| 310 | +        try { | |
| 311 | + list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId); | |
| 312 | +        } catch (NotFoundException $e) { | |
| 313 | + return []; | |
| 314 | + } | |
| 315 | + $mountsForStorage = $this->getMountsForStorageId($storageId, $user); | |
| 316 | + | |
| 317 | + // filter mounts that are from the same storage but a different directory | |
| 318 | +        $filteredMounts = array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { | |
| 319 | +            if ($fileId === $mount->getRootId()) { | |
| 320 | + return true; | |
| 321 | + } | |
| 322 | + $internalMountPath = $mount->getRootInternalPath(); | |
| 323 | + | |
| 324 | + return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; | |
| 325 | + }); | |
| 326 | + | |
| 327 | +        return array_map(function (ICachedMountInfo $mount) use ($internalPath) { | |
| 328 | + return new CachedMountFileInfo( | |
| 329 | + $mount->getUser(), | |
| 330 | + $mount->getStorageId(), | |
| 331 | + $mount->getRootId(), | |
| 332 | + $mount->getMountPoint(), | |
| 333 | + $mount->getMountId(), | |
| 334 | + $mount->getRootInternalPath(), | |
| 335 | + $internalPath | |
| 336 | + ); | |
| 337 | + }, $filteredMounts); | |
| 338 | + } | |
| 339 | + | |
| 340 | + /** | |
| 341 | + * Remove all cached mounts for a user | |
| 342 | + * | |
| 343 | + * @param IUser $user | |
| 344 | + */ | |
| 345 | +    public function removeUserMounts(IUser $user) { | |
| 346 | + $builder = $this->connection->getQueryBuilder(); | |
| 347 | + | |
| 348 | +        $query = $builder->delete('mounts') | |
| 349 | +            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID()))); | |
| 350 | + $query->execute(); | |
| 351 | + } | |
| 352 | + | |
| 353 | +    public function removeUserStorageMount($storageId, $userId) { | |
| 354 | + $builder = $this->connection->getQueryBuilder(); | |
| 355 | + | |
| 356 | +        $query = $builder->delete('mounts') | |
| 357 | +            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId))) | |
| 358 | +            ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); | |
| 359 | + $query->execute(); | |
| 360 | + } | |
| 361 | + | |
| 362 | +    public function remoteStorageMounts($storageId) { | |
| 363 | + $builder = $this->connection->getQueryBuilder(); | |
| 364 | + | |
| 365 | +        $query = $builder->delete('mounts') | |
| 366 | +            ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT))); | |
| 367 | + $query->execute(); | |
| 368 | + } | |
| 369 | 369 | } | 
| @@ -87,11 +87,11 @@ discard block | ||
| 87 | 87 | |
| 88 | 88 |  	public function registerMounts(IUser $user, array $mounts) { | 
| 89 | 89 | // filter out non-proper storages coming from unit tests | 
| 90 | -		$mounts = array_filter($mounts, function (IMountPoint $mount) { | |
| 90 | +		$mounts = array_filter($mounts, function(IMountPoint $mount) { | |
| 91 | 91 | return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache(); | 
| 92 | 92 | }); | 
| 93 | 93 | /** @var ICachedMountInfo[] $newMounts */ | 
| 94 | -		$newMounts = array_map(function (IMountPoint $mount) use ($user) { | |
| 94 | +		$newMounts = array_map(function(IMountPoint $mount) use ($user) { | |
| 95 | 95 | // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet) | 
| 96 | 96 |  			if ($mount->getStorageRootId() === -1) { | 
| 97 | 97 | return null; | 
| @@ -100,13 +100,13 @@ discard block | ||
| 100 | 100 | } | 
| 101 | 101 | }, $mounts); | 
| 102 | 102 | $newMounts = array_values(array_filter($newMounts)); | 
| 103 | -		$newMountRootIds = array_map(function (ICachedMountInfo $mount) { | |
| 103 | +		$newMountRootIds = array_map(function(ICachedMountInfo $mount) { | |
| 104 | 104 | return $mount->getRootId(); | 
| 105 | 105 | }, $newMounts); | 
| 106 | 106 | $newMounts = array_combine($newMountRootIds, $newMounts); | 
| 107 | 107 | |
| 108 | 108 | $cachedMounts = $this->getMountsForUser($user); | 
| 109 | -		$cachedMountRootIds = array_map(function (ICachedMountInfo $mount) { | |
| 109 | +		$cachedMountRootIds = array_map(function(ICachedMountInfo $mount) { | |
| 110 | 110 | return $mount->getRootId(); | 
| 111 | 111 | }, $cachedMounts); | 
| 112 | 112 | $cachedMounts = array_combine($cachedMountRootIds, $cachedMounts); | 
| @@ -180,7 +180,7 @@ discard block | ||
| 180 | 180 | ], ['root_id', 'user_id']); | 
| 181 | 181 |  		} else { | 
| 182 | 182 | // in some cases this is legitimate, like orphaned shares | 
| 183 | -			$this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint()); | |
| 183 | +			$this->logger->debug('Could not get storage info for mount at '.$mount->getMountPoint()); | |
| 184 | 184 | } | 
| 185 | 185 | } | 
| 186 | 186 | |
| @@ -213,9 +213,9 @@ discard block | ||
| 213 | 213 | } | 
| 214 | 214 | $mount_id = $row['mount_id']; | 
| 215 | 215 |  		if (!is_null($mount_id)) { | 
| 216 | - $mount_id = (int)$mount_id; | |
| 216 | + $mount_id = (int) $mount_id; | |
| 217 | 217 | } | 
| 218 | - return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $mount_id, isset($row['path'])? $row['path']:''); | |
| 218 | + return new CachedMountInfo($user, (int) $row['storage_id'], (int) $row['root_id'], $row['mount_point'], $mount_id, isset($row['path']) ? $row['path'] : ''); | |
| 219 | 219 | } | 
| 220 | 220 | |
| 221 | 221 | /** | 
| @@ -246,7 +246,7 @@ discard block | ||
| 246 | 246 | $builder = $this->connection->getQueryBuilder(); | 
| 247 | 247 |  		$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path') | 
| 248 | 248 |  			->from('mounts', 'm') | 
| 249 | -			->innerJoin('m', 'filecache', 'f' , $builder->expr()->eq('m.root_id', 'f.fileid')) | |
| 249 | +			->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid')) | |
| 250 | 250 |  			->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT))); | 
| 251 | 251 | |
| 252 | 252 |  		if ($user) { | 
| @@ -289,12 +289,12 @@ discard block | ||
| 289 | 289 | $row = $query->execute()->fetch(); | 
| 290 | 290 |  			if (is_array($row)) { | 
| 291 | 291 | $this->cacheInfoCache[$fileId] = [ | 
| 292 | - (int)$row['storage'], | |
| 292 | + (int) $row['storage'], | |
| 293 | 293 | $row['path'], | 
| 294 | - (int)$row['mimetype'] | |
| 294 | + (int) $row['mimetype'] | |
| 295 | 295 | ]; | 
| 296 | 296 |  			} else { | 
| 297 | -				throw new NotFoundException('File with id "' . $fileId . '" not found'); | |
| 297 | +				throw new NotFoundException('File with id "'.$fileId.'" not found'); | |
| 298 | 298 | } | 
| 299 | 299 | } | 
| 300 | 300 | return $this->cacheInfoCache[$fileId]; | 
| @@ -315,16 +315,16 @@ discard block | ||
| 315 | 315 | $mountsForStorage = $this->getMountsForStorageId($storageId, $user); | 
| 316 | 316 | |
| 317 | 317 | // filter mounts that are from the same storage but a different directory | 
| 318 | -		$filteredMounts = array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) { | |
| 318 | +		$filteredMounts = array_filter($mountsForStorage, function(ICachedMountInfo $mount) use ($internalPath, $fileId) { | |
| 319 | 319 |  			if ($fileId === $mount->getRootId()) { | 
| 320 | 320 | return true; | 
| 321 | 321 | } | 
| 322 | 322 | $internalMountPath = $mount->getRootInternalPath(); | 
| 323 | 323 | |
| 324 | - return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/'; | |
| 324 | + return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath.'/'; | |
| 325 | 325 | }); | 
| 326 | 326 | |
| 327 | -		return array_map(function (ICachedMountInfo $mount) use ($internalPath) { | |
| 327 | +		return array_map(function(ICachedMountInfo $mount) use ($internalPath) { | |
| 328 | 328 | return new CachedMountFileInfo( | 
| 329 | 329 | $mount->getUser(), | 
| 330 | 330 | $mount->getStorageId(), | 
| @@ -31,153 +31,153 @@ | ||
| 31 | 31 | use OCP\Files\Mount\IMountPoint; | 
| 32 | 32 | |
| 33 | 33 |  class Manager implements IMountManager { | 
| 34 | - /** @var MountPoint[] */ | |
| 35 | - private $mounts = []; | |
| 36 | - | |
| 37 | - /** @var CappedMemoryCache */ | |
| 38 | - private $inPathCache; | |
| 39 | - | |
| 40 | -	public function __construct() { | |
| 41 | - $this->inPathCache = new CappedMemoryCache(); | |
| 42 | - } | |
| 43 | - | |
| 44 | - /** | |
| 45 | - * @param IMountPoint $mount | |
| 46 | - */ | |
| 47 | -	public function addMount(IMountPoint $mount) { | |
| 48 | - $this->mounts[$mount->getMountPoint()] = $mount; | |
| 49 | - $this->inPathCache->clear(); | |
| 50 | - } | |
| 51 | - | |
| 52 | - /** | |
| 53 | - * @param string $mountPoint | |
| 54 | - */ | |
| 55 | -	public function removeMount($mountPoint) { | |
| 56 | - $mountPoint = Filesystem::normalizePath($mountPoint); | |
| 57 | -		if (strlen($mountPoint) > 1) { | |
| 58 | - $mountPoint .= '/'; | |
| 59 | - } | |
| 60 | - unset($this->mounts[$mountPoint]); | |
| 61 | - $this->inPathCache->clear(); | |
| 62 | - } | |
| 63 | - | |
| 64 | - /** | |
| 65 | - * @param string $mountPoint | |
| 66 | - * @param string $target | |
| 67 | - */ | |
| 68 | -	public function moveMount($mountPoint, $target) { | |
| 69 | - $this->mounts[$target] = $this->mounts[$mountPoint]; | |
| 70 | - unset($this->mounts[$mountPoint]); | |
| 71 | - $this->inPathCache->clear(); | |
| 72 | - } | |
| 73 | - | |
| 74 | - /** | |
| 75 | - * Find the mount for $path | |
| 76 | - * | |
| 77 | - * @param string $path | |
| 78 | - * @return MountPoint | |
| 79 | - */ | |
| 80 | -	public function find($path) { | |
| 81 | - \OC_Util::setupFS(); | |
| 82 | - $path = Filesystem::normalizePath($path); | |
| 83 | - | |
| 84 | - $current = $path; | |
| 85 | -		while (true) { | |
| 86 | - $mountPoint = $current . '/'; | |
| 87 | -			if (isset($this->mounts[$mountPoint])) { | |
| 88 | - return $this->mounts[$mountPoint]; | |
| 89 | - } | |
| 90 | - | |
| 91 | -			if ($current === '') { | |
| 92 | - return null; | |
| 93 | - } | |
| 94 | - | |
| 95 | - $current = dirname($current); | |
| 96 | -			if ($current === '.' || $current === '/') { | |
| 97 | - $current = ''; | |
| 98 | - } | |
| 99 | - } | |
| 100 | - } | |
| 101 | - | |
| 102 | - /** | |
| 103 | - * Find all mounts in $path | |
| 104 | - * | |
| 105 | - * @param string $path | |
| 106 | - * @return MountPoint[] | |
| 107 | - */ | |
| 108 | -	public function findIn($path) { | |
| 109 | - \OC_Util::setupFS(); | |
| 110 | - $path = $this->formatPath($path); | |
| 111 | - | |
| 112 | -		if (isset($this->inPathCache[$path])) { | |
| 113 | - return $this->inPathCache[$path]; | |
| 114 | - } | |
| 115 | - | |
| 116 | - $result = []; | |
| 117 | - $pathLength = strlen($path); | |
| 118 | - $mountPoints = array_keys($this->mounts); | |
| 119 | -		foreach ($mountPoints as $mountPoint) { | |
| 120 | -			if (substr($mountPoint, 0, $pathLength) === $path and strlen($mountPoint) > $pathLength) { | |
| 121 | - $result[] = $this->mounts[$mountPoint]; | |
| 122 | - } | |
| 123 | - } | |
| 124 | - | |
| 125 | - $this->inPathCache[$path] = $result; | |
| 126 | - return $result; | |
| 127 | - } | |
| 128 | - | |
| 129 | -	public function clear() { | |
| 130 | - $this->mounts = []; | |
| 131 | - $this->inPathCache->clear(); | |
| 132 | - } | |
| 133 | - | |
| 134 | - /** | |
| 135 | - * Find mounts by storage id | |
| 136 | - * | |
| 137 | - * @param string $id | |
| 138 | - * @return MountPoint[] | |
| 139 | - */ | |
| 140 | -	public function findByStorageId($id) { | |
| 141 | - \OC_Util::setupFS(); | |
| 142 | -		if (strlen($id) > 64) { | |
| 143 | - $id = md5($id); | |
| 144 | - } | |
| 145 | - $result = array(); | |
| 146 | -		foreach ($this->mounts as $mount) { | |
| 147 | -			if ($mount->getStorageId() === $id) { | |
| 148 | - $result[] = $mount; | |
| 149 | - } | |
| 150 | - } | |
| 151 | - return $result; | |
| 152 | - } | |
| 153 | - | |
| 154 | - /** | |
| 155 | - * @return MountPoint[] | |
| 156 | - */ | |
| 157 | -	public function getAll() { | |
| 158 | - return $this->mounts; | |
| 159 | - } | |
| 160 | - | |
| 161 | - /** | |
| 162 | - * Find mounts by numeric storage id | |
| 163 | - * | |
| 164 | - * @param int $id | |
| 165 | - * @return MountPoint[] | |
| 166 | - */ | |
| 167 | -	public function findByNumericId($id) { | |
| 168 | - $storageId = \OC\Files\Cache\Storage::getStorageId($id); | |
| 169 | - return $this->findByStorageId($storageId); | |
| 170 | - } | |
| 171 | - | |
| 172 | - /** | |
| 173 | - * @param string $path | |
| 174 | - * @return string | |
| 175 | - */ | |
| 176 | -	private function formatPath($path) { | |
| 177 | - $path = Filesystem::normalizePath($path); | |
| 178 | -		if (strlen($path) > 1) { | |
| 179 | - $path .= '/'; | |
| 180 | - } | |
| 181 | - return $path; | |
| 182 | - } | |
| 34 | + /** @var MountPoint[] */ | |
| 35 | + private $mounts = []; | |
| 36 | + | |
| 37 | + /** @var CappedMemoryCache */ | |
| 38 | + private $inPathCache; | |
| 39 | + | |
| 40 | +    public function __construct() { | |
| 41 | + $this->inPathCache = new CappedMemoryCache(); | |
| 42 | + } | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * @param IMountPoint $mount | |
| 46 | + */ | |
| 47 | +    public function addMount(IMountPoint $mount) { | |
| 48 | + $this->mounts[$mount->getMountPoint()] = $mount; | |
| 49 | + $this->inPathCache->clear(); | |
| 50 | + } | |
| 51 | + | |
| 52 | + /** | |
| 53 | + * @param string $mountPoint | |
| 54 | + */ | |
| 55 | +    public function removeMount($mountPoint) { | |
| 56 | + $mountPoint = Filesystem::normalizePath($mountPoint); | |
| 57 | +        if (strlen($mountPoint) > 1) { | |
| 58 | + $mountPoint .= '/'; | |
| 59 | + } | |
| 60 | + unset($this->mounts[$mountPoint]); | |
| 61 | + $this->inPathCache->clear(); | |
| 62 | + } | |
| 63 | + | |
| 64 | + /** | |
| 65 | + * @param string $mountPoint | |
| 66 | + * @param string $target | |
| 67 | + */ | |
| 68 | +    public function moveMount($mountPoint, $target) { | |
| 69 | + $this->mounts[$target] = $this->mounts[$mountPoint]; | |
| 70 | + unset($this->mounts[$mountPoint]); | |
| 71 | + $this->inPathCache->clear(); | |
| 72 | + } | |
| 73 | + | |
| 74 | + /** | |
| 75 | + * Find the mount for $path | |
| 76 | + * | |
| 77 | + * @param string $path | |
| 78 | + * @return MountPoint | |
| 79 | + */ | |
| 80 | +    public function find($path) { | |
| 81 | + \OC_Util::setupFS(); | |
| 82 | + $path = Filesystem::normalizePath($path); | |
| 83 | + | |
| 84 | + $current = $path; | |
| 85 | +        while (true) { | |
| 86 | + $mountPoint = $current . '/'; | |
| 87 | +            if (isset($this->mounts[$mountPoint])) { | |
| 88 | + return $this->mounts[$mountPoint]; | |
| 89 | + } | |
| 90 | + | |
| 91 | +            if ($current === '') { | |
| 92 | + return null; | |
| 93 | + } | |
| 94 | + | |
| 95 | + $current = dirname($current); | |
| 96 | +            if ($current === '.' || $current === '/') { | |
| 97 | + $current = ''; | |
| 98 | + } | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * Find all mounts in $path | |
| 104 | + * | |
| 105 | + * @param string $path | |
| 106 | + * @return MountPoint[] | |
| 107 | + */ | |
| 108 | +    public function findIn($path) { | |
| 109 | + \OC_Util::setupFS(); | |
| 110 | + $path = $this->formatPath($path); | |
| 111 | + | |
| 112 | +        if (isset($this->inPathCache[$path])) { | |
| 113 | + return $this->inPathCache[$path]; | |
| 114 | + } | |
| 115 | + | |
| 116 | + $result = []; | |
| 117 | + $pathLength = strlen($path); | |
| 118 | + $mountPoints = array_keys($this->mounts); | |
| 119 | +        foreach ($mountPoints as $mountPoint) { | |
| 120 | +            if (substr($mountPoint, 0, $pathLength) === $path and strlen($mountPoint) > $pathLength) { | |
| 121 | + $result[] = $this->mounts[$mountPoint]; | |
| 122 | + } | |
| 123 | + } | |
| 124 | + | |
| 125 | + $this->inPathCache[$path] = $result; | |
| 126 | + return $result; | |
| 127 | + } | |
| 128 | + | |
| 129 | +    public function clear() { | |
| 130 | + $this->mounts = []; | |
| 131 | + $this->inPathCache->clear(); | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Find mounts by storage id | |
| 136 | + * | |
| 137 | + * @param string $id | |
| 138 | + * @return MountPoint[] | |
| 139 | + */ | |
| 140 | +    public function findByStorageId($id) { | |
| 141 | + \OC_Util::setupFS(); | |
| 142 | +        if (strlen($id) > 64) { | |
| 143 | + $id = md5($id); | |
| 144 | + } | |
| 145 | + $result = array(); | |
| 146 | +        foreach ($this->mounts as $mount) { | |
| 147 | +            if ($mount->getStorageId() === $id) { | |
| 148 | + $result[] = $mount; | |
| 149 | + } | |
| 150 | + } | |
| 151 | + return $result; | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 155 | + * @return MountPoint[] | |
| 156 | + */ | |
| 157 | +    public function getAll() { | |
| 158 | + return $this->mounts; | |
| 159 | + } | |
| 160 | + | |
| 161 | + /** | |
| 162 | + * Find mounts by numeric storage id | |
| 163 | + * | |
| 164 | + * @param int $id | |
| 165 | + * @return MountPoint[] | |
| 166 | + */ | |
| 167 | +    public function findByNumericId($id) { | |
| 168 | + $storageId = \OC\Files\Cache\Storage::getStorageId($id); | |
| 169 | + return $this->findByStorageId($storageId); | |
| 170 | + } | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * @param string $path | |
| 174 | + * @return string | |
| 175 | + */ | |
| 176 | +    private function formatPath($path) { | |
| 177 | + $path = Filesystem::normalizePath($path); | |
| 178 | +        if (strlen($path) > 1) { | |
| 179 | + $path .= '/'; | |
| 180 | + } | |
| 181 | + return $path; | |
| 182 | + } | |
| 183 | 183 | } | 
| @@ -83,7 +83,7 @@ | ||
| 83 | 83 | |
| 84 | 84 | $current = $path; | 
| 85 | 85 |  		while (true) { | 
| 86 | - $mountPoint = $current . '/'; | |
| 86 | + $mountPoint = $current.'/'; | |
| 87 | 87 |  			if (isset($this->mounts[$mountPoint])) { | 
| 88 | 88 | return $this->mounts[$mountPoint]; | 
| 89 | 89 | } | 
| @@ -60,1524 +60,1524 @@ | ||
| 60 | 60 | */ | 
| 61 | 61 |  class Manager implements IManager { | 
| 62 | 62 | |
| 63 | - /** @var IProviderFactory */ | |
| 64 | - private $factory; | |
| 65 | - /** @var ILogger */ | |
| 66 | - private $logger; | |
| 67 | - /** @var IConfig */ | |
| 68 | - private $config; | |
| 69 | - /** @var ISecureRandom */ | |
| 70 | - private $secureRandom; | |
| 71 | - /** @var IHasher */ | |
| 72 | - private $hasher; | |
| 73 | - /** @var IMountManager */ | |
| 74 | - private $mountManager; | |
| 75 | - /** @var IGroupManager */ | |
| 76 | - private $groupManager; | |
| 77 | - /** @var IL10N */ | |
| 78 | - private $l; | |
| 79 | - /** @var IFactory */ | |
| 80 | - private $l10nFactory; | |
| 81 | - /** @var IUserManager */ | |
| 82 | - private $userManager; | |
| 83 | - /** @var IRootFolder */ | |
| 84 | - private $rootFolder; | |
| 85 | - /** @var CappedMemoryCache */ | |
| 86 | - private $sharingDisabledForUsersCache; | |
| 87 | - /** @var EventDispatcher */ | |
| 88 | - private $eventDispatcher; | |
| 89 | - /** @var LegacyHooks */ | |
| 90 | - private $legacyHooks; | |
| 91 | - /** @var IMailer */ | |
| 92 | - private $mailer; | |
| 93 | - /** @var IURLGenerator */ | |
| 94 | - private $urlGenerator; | |
| 95 | - /** @var \OC_Defaults */ | |
| 96 | - private $defaults; | |
| 97 | - | |
| 98 | - | |
| 99 | - /** | |
| 100 | - * Manager constructor. | |
| 101 | - * | |
| 102 | - * @param ILogger $logger | |
| 103 | - * @param IConfig $config | |
| 104 | - * @param ISecureRandom $secureRandom | |
| 105 | - * @param IHasher $hasher | |
| 106 | - * @param IMountManager $mountManager | |
| 107 | - * @param IGroupManager $groupManager | |
| 108 | - * @param IL10N $l | |
| 109 | - * @param IFactory $l10nFactory | |
| 110 | - * @param IProviderFactory $factory | |
| 111 | - * @param IUserManager $userManager | |
| 112 | - * @param IRootFolder $rootFolder | |
| 113 | - * @param EventDispatcher $eventDispatcher | |
| 114 | - * @param IMailer $mailer | |
| 115 | - * @param IURLGenerator $urlGenerator | |
| 116 | - * @param \OC_Defaults $defaults | |
| 117 | - */ | |
| 118 | - public function __construct( | |
| 119 | - ILogger $logger, | |
| 120 | - IConfig $config, | |
| 121 | - ISecureRandom $secureRandom, | |
| 122 | - IHasher $hasher, | |
| 123 | - IMountManager $mountManager, | |
| 124 | - IGroupManager $groupManager, | |
| 125 | - IL10N $l, | |
| 126 | - IFactory $l10nFactory, | |
| 127 | - IProviderFactory $factory, | |
| 128 | - IUserManager $userManager, | |
| 129 | - IRootFolder $rootFolder, | |
| 130 | - EventDispatcher $eventDispatcher, | |
| 131 | - IMailer $mailer, | |
| 132 | - IURLGenerator $urlGenerator, | |
| 133 | - \OC_Defaults $defaults | |
| 134 | -	) { | |
| 135 | - $this->logger = $logger; | |
| 136 | - $this->config = $config; | |
| 137 | - $this->secureRandom = $secureRandom; | |
| 138 | - $this->hasher = $hasher; | |
| 139 | - $this->mountManager = $mountManager; | |
| 140 | - $this->groupManager = $groupManager; | |
| 141 | - $this->l = $l; | |
| 142 | - $this->l10nFactory = $l10nFactory; | |
| 143 | - $this->factory = $factory; | |
| 144 | - $this->userManager = $userManager; | |
| 145 | - $this->rootFolder = $rootFolder; | |
| 146 | - $this->eventDispatcher = $eventDispatcher; | |
| 147 | - $this->sharingDisabledForUsersCache = new CappedMemoryCache(); | |
| 148 | - $this->legacyHooks = new LegacyHooks($this->eventDispatcher); | |
| 149 | - $this->mailer = $mailer; | |
| 150 | - $this->urlGenerator = $urlGenerator; | |
| 151 | - $this->defaults = $defaults; | |
| 152 | - } | |
| 153 | - | |
| 154 | - /** | |
| 155 | - * Convert from a full share id to a tuple (providerId, shareId) | |
| 156 | - * | |
| 157 | - * @param string $id | |
| 158 | - * @return string[] | |
| 159 | - */ | |
| 160 | -	private function splitFullId($id) { | |
| 161 | -		return explode(':', $id, 2); | |
| 162 | - } | |
| 163 | - | |
| 164 | - /** | |
| 165 | - * Verify if a password meets all requirements | |
| 166 | - * | |
| 167 | - * @param string $password | |
| 168 | - * @throws \Exception | |
| 169 | - */ | |
| 170 | -	protected function verifyPassword($password) { | |
| 171 | -		if ($password === null) { | |
| 172 | - // No password is set, check if this is allowed. | |
| 173 | -			if ($this->shareApiLinkEnforcePassword()) { | |
| 174 | -				throw new \InvalidArgumentException('Passwords are enforced for link shares'); | |
| 175 | - } | |
| 176 | - | |
| 177 | - return; | |
| 178 | - } | |
| 179 | - | |
| 180 | - // Let others verify the password | |
| 181 | -		try { | |
| 182 | - $event = new GenericEvent($password); | |
| 183 | -			$this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); | |
| 184 | -		} catch (HintException $e) { | |
| 185 | - throw new \Exception($e->getHint()); | |
| 186 | - } | |
| 187 | - } | |
| 188 | - | |
| 189 | - /** | |
| 190 | - * Check for generic requirements before creating a share | |
| 191 | - * | |
| 192 | - * @param \OCP\Share\IShare $share | |
| 193 | - * @throws \InvalidArgumentException | |
| 194 | - * @throws GenericShareException | |
| 195 | - */ | |
| 196 | -	protected function generalCreateChecks(\OCP\Share\IShare $share) { | |
| 197 | -		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { | |
| 198 | - // We expect a valid user as sharedWith for user shares | |
| 199 | -			if (!$this->userManager->userExists($share->getSharedWith())) { | |
| 200 | -				throw new \InvalidArgumentException('SharedWith is not a valid user'); | |
| 201 | - } | |
| 202 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 203 | - // We expect a valid group as sharedWith for group shares | |
| 204 | -			if (!$this->groupManager->groupExists($share->getSharedWith())) { | |
| 205 | -				throw new \InvalidArgumentException('SharedWith is not a valid group'); | |
| 206 | - } | |
| 207 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { | |
| 208 | -			if ($share->getSharedWith() !== null) { | |
| 209 | -				throw new \InvalidArgumentException('SharedWith should be empty'); | |
| 210 | - } | |
| 211 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { | |
| 212 | -			if ($share->getSharedWith() === null) { | |
| 213 | -				throw new \InvalidArgumentException('SharedWith should not be empty'); | |
| 214 | - } | |
| 215 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { | |
| 216 | -			if ($share->getSharedWith() === null) { | |
| 217 | -				throw new \InvalidArgumentException('SharedWith should not be empty'); | |
| 218 | - } | |
| 219 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) { | |
| 220 | - $circle = \OCA\Circles\Api\Circles::detailsCircle($share->getSharedWith()); | |
| 221 | -			if ($circle === null) { | |
| 222 | -				throw new \InvalidArgumentException('SharedWith is not a valid circle'); | |
| 223 | - } | |
| 224 | -		} else { | |
| 225 | - // We can't handle other types yet | |
| 226 | -			throw new \InvalidArgumentException('unknown share type'); | |
| 227 | - } | |
| 228 | - | |
| 229 | - // Verify the initiator of the share is set | |
| 230 | -		if ($share->getSharedBy() === null) { | |
| 231 | -			throw new \InvalidArgumentException('SharedBy should be set'); | |
| 232 | - } | |
| 233 | - | |
| 234 | - // Cannot share with yourself | |
| 235 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && | |
| 236 | -			$share->getSharedWith() === $share->getSharedBy()) { | |
| 237 | -			throw new \InvalidArgumentException('Can\'t share with yourself'); | |
| 238 | - } | |
| 239 | - | |
| 240 | - // The path should be set | |
| 241 | -		if ($share->getNode() === null) { | |
| 242 | -			throw new \InvalidArgumentException('Path should be set'); | |
| 243 | - } | |
| 244 | - | |
| 245 | - // And it should be a file or a folder | |
| 246 | - if (!($share->getNode() instanceof \OCP\Files\File) && | |
| 247 | -				!($share->getNode() instanceof \OCP\Files\Folder)) { | |
| 248 | -			throw new \InvalidArgumentException('Path should be either a file or a folder'); | |
| 249 | - } | |
| 250 | - | |
| 251 | - // And you can't share your rootfolder | |
| 252 | -		if ($this->userManager->userExists($share->getSharedBy())) { | |
| 253 | - $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath(); | |
| 254 | -		} else { | |
| 255 | - $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath(); | |
| 256 | - } | |
| 257 | -		if ($sharedPath === $share->getNode()->getPath()) { | |
| 258 | -			throw new \InvalidArgumentException('You can\'t share your root folder'); | |
| 259 | - } | |
| 260 | - | |
| 261 | - // Check if we actually have share permissions | |
| 262 | -		if (!$share->getNode()->isShareable()) { | |
| 263 | -			$message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]); | |
| 264 | - throw new GenericShareException($message_t, $message_t, 404); | |
| 265 | - } | |
| 266 | - | |
| 267 | - // Permissions should be set | |
| 268 | -		if ($share->getPermissions() === null) { | |
| 269 | -			throw new \InvalidArgumentException('A share requires permissions'); | |
| 270 | - } | |
| 271 | - | |
| 272 | - /* | |
| 63 | + /** @var IProviderFactory */ | |
| 64 | + private $factory; | |
| 65 | + /** @var ILogger */ | |
| 66 | + private $logger; | |
| 67 | + /** @var IConfig */ | |
| 68 | + private $config; | |
| 69 | + /** @var ISecureRandom */ | |
| 70 | + private $secureRandom; | |
| 71 | + /** @var IHasher */ | |
| 72 | + private $hasher; | |
| 73 | + /** @var IMountManager */ | |
| 74 | + private $mountManager; | |
| 75 | + /** @var IGroupManager */ | |
| 76 | + private $groupManager; | |
| 77 | + /** @var IL10N */ | |
| 78 | + private $l; | |
| 79 | + /** @var IFactory */ | |
| 80 | + private $l10nFactory; | |
| 81 | + /** @var IUserManager */ | |
| 82 | + private $userManager; | |
| 83 | + /** @var IRootFolder */ | |
| 84 | + private $rootFolder; | |
| 85 | + /** @var CappedMemoryCache */ | |
| 86 | + private $sharingDisabledForUsersCache; | |
| 87 | + /** @var EventDispatcher */ | |
| 88 | + private $eventDispatcher; | |
| 89 | + /** @var LegacyHooks */ | |
| 90 | + private $legacyHooks; | |
| 91 | + /** @var IMailer */ | |
| 92 | + private $mailer; | |
| 93 | + /** @var IURLGenerator */ | |
| 94 | + private $urlGenerator; | |
| 95 | + /** @var \OC_Defaults */ | |
| 96 | + private $defaults; | |
| 97 | + | |
| 98 | + | |
| 99 | + /** | |
| 100 | + * Manager constructor. | |
| 101 | + * | |
| 102 | + * @param ILogger $logger | |
| 103 | + * @param IConfig $config | |
| 104 | + * @param ISecureRandom $secureRandom | |
| 105 | + * @param IHasher $hasher | |
| 106 | + * @param IMountManager $mountManager | |
| 107 | + * @param IGroupManager $groupManager | |
| 108 | + * @param IL10N $l | |
| 109 | + * @param IFactory $l10nFactory | |
| 110 | + * @param IProviderFactory $factory | |
| 111 | + * @param IUserManager $userManager | |
| 112 | + * @param IRootFolder $rootFolder | |
| 113 | + * @param EventDispatcher $eventDispatcher | |
| 114 | + * @param IMailer $mailer | |
| 115 | + * @param IURLGenerator $urlGenerator | |
| 116 | + * @param \OC_Defaults $defaults | |
| 117 | + */ | |
| 118 | + public function __construct( | |
| 119 | + ILogger $logger, | |
| 120 | + IConfig $config, | |
| 121 | + ISecureRandom $secureRandom, | |
| 122 | + IHasher $hasher, | |
| 123 | + IMountManager $mountManager, | |
| 124 | + IGroupManager $groupManager, | |
| 125 | + IL10N $l, | |
| 126 | + IFactory $l10nFactory, | |
| 127 | + IProviderFactory $factory, | |
| 128 | + IUserManager $userManager, | |
| 129 | + IRootFolder $rootFolder, | |
| 130 | + EventDispatcher $eventDispatcher, | |
| 131 | + IMailer $mailer, | |
| 132 | + IURLGenerator $urlGenerator, | |
| 133 | + \OC_Defaults $defaults | |
| 134 | +    ) { | |
| 135 | + $this->logger = $logger; | |
| 136 | + $this->config = $config; | |
| 137 | + $this->secureRandom = $secureRandom; | |
| 138 | + $this->hasher = $hasher; | |
| 139 | + $this->mountManager = $mountManager; | |
| 140 | + $this->groupManager = $groupManager; | |
| 141 | + $this->l = $l; | |
| 142 | + $this->l10nFactory = $l10nFactory; | |
| 143 | + $this->factory = $factory; | |
| 144 | + $this->userManager = $userManager; | |
| 145 | + $this->rootFolder = $rootFolder; | |
| 146 | + $this->eventDispatcher = $eventDispatcher; | |
| 147 | + $this->sharingDisabledForUsersCache = new CappedMemoryCache(); | |
| 148 | + $this->legacyHooks = new LegacyHooks($this->eventDispatcher); | |
| 149 | + $this->mailer = $mailer; | |
| 150 | + $this->urlGenerator = $urlGenerator; | |
| 151 | + $this->defaults = $defaults; | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 155 | + * Convert from a full share id to a tuple (providerId, shareId) | |
| 156 | + * | |
| 157 | + * @param string $id | |
| 158 | + * @return string[] | |
| 159 | + */ | |
| 160 | +    private function splitFullId($id) { | |
| 161 | +        return explode(':', $id, 2); | |
| 162 | + } | |
| 163 | + | |
| 164 | + /** | |
| 165 | + * Verify if a password meets all requirements | |
| 166 | + * | |
| 167 | + * @param string $password | |
| 168 | + * @throws \Exception | |
| 169 | + */ | |
| 170 | +    protected function verifyPassword($password) { | |
| 171 | +        if ($password === null) { | |
| 172 | + // No password is set, check if this is allowed. | |
| 173 | +            if ($this->shareApiLinkEnforcePassword()) { | |
| 174 | +                throw new \InvalidArgumentException('Passwords are enforced for link shares'); | |
| 175 | + } | |
| 176 | + | |
| 177 | + return; | |
| 178 | + } | |
| 179 | + | |
| 180 | + // Let others verify the password | |
| 181 | +        try { | |
| 182 | + $event = new GenericEvent($password); | |
| 183 | +            $this->eventDispatcher->dispatch('OCP\PasswordPolicy::validate', $event); | |
| 184 | +        } catch (HintException $e) { | |
| 185 | + throw new \Exception($e->getHint()); | |
| 186 | + } | |
| 187 | + } | |
| 188 | + | |
| 189 | + /** | |
| 190 | + * Check for generic requirements before creating a share | |
| 191 | + * | |
| 192 | + * @param \OCP\Share\IShare $share | |
| 193 | + * @throws \InvalidArgumentException | |
| 194 | + * @throws GenericShareException | |
| 195 | + */ | |
| 196 | +    protected function generalCreateChecks(\OCP\Share\IShare $share) { | |
| 197 | +        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { | |
| 198 | + // We expect a valid user as sharedWith for user shares | |
| 199 | +            if (!$this->userManager->userExists($share->getSharedWith())) { | |
| 200 | +                throw new \InvalidArgumentException('SharedWith is not a valid user'); | |
| 201 | + } | |
| 202 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 203 | + // We expect a valid group as sharedWith for group shares | |
| 204 | +            if (!$this->groupManager->groupExists($share->getSharedWith())) { | |
| 205 | +                throw new \InvalidArgumentException('SharedWith is not a valid group'); | |
| 206 | + } | |
| 207 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { | |
| 208 | +            if ($share->getSharedWith() !== null) { | |
| 209 | +                throw new \InvalidArgumentException('SharedWith should be empty'); | |
| 210 | + } | |
| 211 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_REMOTE) { | |
| 212 | +            if ($share->getSharedWith() === null) { | |
| 213 | +                throw new \InvalidArgumentException('SharedWith should not be empty'); | |
| 214 | + } | |
| 215 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { | |
| 216 | +            if ($share->getSharedWith() === null) { | |
| 217 | +                throw new \InvalidArgumentException('SharedWith should not be empty'); | |
| 218 | + } | |
| 219 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_CIRCLE) { | |
| 220 | + $circle = \OCA\Circles\Api\Circles::detailsCircle($share->getSharedWith()); | |
| 221 | +            if ($circle === null) { | |
| 222 | +                throw new \InvalidArgumentException('SharedWith is not a valid circle'); | |
| 223 | + } | |
| 224 | +        } else { | |
| 225 | + // We can't handle other types yet | |
| 226 | +            throw new \InvalidArgumentException('unknown share type'); | |
| 227 | + } | |
| 228 | + | |
| 229 | + // Verify the initiator of the share is set | |
| 230 | +        if ($share->getSharedBy() === null) { | |
| 231 | +            throw new \InvalidArgumentException('SharedBy should be set'); | |
| 232 | + } | |
| 233 | + | |
| 234 | + // Cannot share with yourself | |
| 235 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && | |
| 236 | +            $share->getSharedWith() === $share->getSharedBy()) { | |
| 237 | +            throw new \InvalidArgumentException('Can\'t share with yourself'); | |
| 238 | + } | |
| 239 | + | |
| 240 | + // The path should be set | |
| 241 | +        if ($share->getNode() === null) { | |
| 242 | +            throw new \InvalidArgumentException('Path should be set'); | |
| 243 | + } | |
| 244 | + | |
| 245 | + // And it should be a file or a folder | |
| 246 | + if (!($share->getNode() instanceof \OCP\Files\File) && | |
| 247 | +                !($share->getNode() instanceof \OCP\Files\Folder)) { | |
| 248 | +            throw new \InvalidArgumentException('Path should be either a file or a folder'); | |
| 249 | + } | |
| 250 | + | |
| 251 | + // And you can't share your rootfolder | |
| 252 | +        if ($this->userManager->userExists($share->getSharedBy())) { | |
| 253 | + $sharedPath = $this->rootFolder->getUserFolder($share->getSharedBy())->getPath(); | |
| 254 | +        } else { | |
| 255 | + $sharedPath = $this->rootFolder->getUserFolder($share->getShareOwner())->getPath(); | |
| 256 | + } | |
| 257 | +        if ($sharedPath === $share->getNode()->getPath()) { | |
| 258 | +            throw new \InvalidArgumentException('You can\'t share your root folder'); | |
| 259 | + } | |
| 260 | + | |
| 261 | + // Check if we actually have share permissions | |
| 262 | +        if (!$share->getNode()->isShareable()) { | |
| 263 | +            $message_t = $this->l->t('You are not allowed to share %s', [$share->getNode()->getPath()]); | |
| 264 | + throw new GenericShareException($message_t, $message_t, 404); | |
| 265 | + } | |
| 266 | + | |
| 267 | + // Permissions should be set | |
| 268 | +        if ($share->getPermissions() === null) { | |
| 269 | +            throw new \InvalidArgumentException('A share requires permissions'); | |
| 270 | + } | |
| 271 | + | |
| 272 | + /* | |
| 273 | 273 | * Quick fix for #23536 | 
| 274 | 274 | * Non moveable mount points do not have update and delete permissions | 
| 275 | 275 | * while we 'most likely' do have that on the storage. | 
| 276 | 276 | */ | 
| 277 | - $permissions = $share->getNode()->getPermissions(); | |
| 278 | - $mount = $share->getNode()->getMountPoint(); | |
| 279 | -		if (!($mount instanceof MoveableMount)) { | |
| 280 | - $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; | |
| 281 | - } | |
| 282 | - | |
| 283 | - // Check that we do not share with more permissions than we have | |
| 284 | -		if ($share->getPermissions() & ~$permissions) { | |
| 285 | -			$message_t = $this->l->t('Cannot increase permissions of %s', [$share->getNode()->getPath()]); | |
| 286 | - throw new GenericShareException($message_t, $message_t, 404); | |
| 287 | - } | |
| 288 | - | |
| 289 | - | |
| 290 | - // Check that read permissions are always set | |
| 291 | - // Link shares are allowed to have no read permissions to allow upload to hidden folders | |
| 292 | - $noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK | |
| 293 | - || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL; | |
| 294 | - if (!$noReadPermissionRequired && | |
| 295 | -			($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) { | |
| 296 | -			throw new \InvalidArgumentException('Shares need at least read permissions'); | |
| 297 | - } | |
| 298 | - | |
| 299 | -		if ($share->getNode() instanceof \OCP\Files\File) { | |
| 300 | -			if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) { | |
| 301 | -				$message_t = $this->l->t('Files can\'t be shared with delete permissions'); | |
| 302 | - throw new GenericShareException($message_t); | |
| 303 | - } | |
| 304 | -			if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) { | |
| 305 | -				$message_t = $this->l->t('Files can\'t be shared with create permissions'); | |
| 306 | - throw new GenericShareException($message_t); | |
| 307 | - } | |
| 308 | - } | |
| 309 | - } | |
| 310 | - | |
| 311 | - /** | |
| 312 | - * Validate if the expiration date fits the system settings | |
| 313 | - * | |
| 314 | - * @param \OCP\Share\IShare $share The share to validate the expiration date of | |
| 315 | - * @return \OCP\Share\IShare The modified share object | |
| 316 | - * @throws GenericShareException | |
| 317 | - * @throws \InvalidArgumentException | |
| 318 | - * @throws \Exception | |
| 319 | - */ | |
| 320 | -	protected function validateExpirationDate(\OCP\Share\IShare $share) { | |
| 321 | - | |
| 322 | - $expirationDate = $share->getExpirationDate(); | |
| 323 | - | |
| 324 | -		if ($expirationDate !== null) { | |
| 325 | - //Make sure the expiration date is a date | |
| 326 | - $expirationDate->setTime(0, 0, 0); | |
| 327 | - | |
| 328 | - $date = new \DateTime(); | |
| 329 | - $date->setTime(0, 0, 0); | |
| 330 | -			if ($date >= $expirationDate) { | |
| 331 | -				$message = $this->l->t('Expiration date is in the past'); | |
| 332 | - throw new GenericShareException($message, $message, 404); | |
| 333 | - } | |
| 334 | - } | |
| 335 | - | |
| 336 | - // If expiredate is empty set a default one if there is a default | |
| 337 | - $fullId = null; | |
| 338 | -		try { | |
| 339 | - $fullId = $share->getFullId(); | |
| 340 | -		} catch (\UnexpectedValueException $e) { | |
| 341 | - // This is a new share | |
| 342 | - } | |
| 343 | - | |
| 344 | -		if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { | |
| 345 | - $expirationDate = new \DateTime(); | |
| 346 | - $expirationDate->setTime(0,0,0); | |
| 347 | -			$expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); | |
| 348 | - } | |
| 349 | - | |
| 350 | - // If we enforce the expiration date check that is does not exceed | |
| 351 | -		if ($this->shareApiLinkDefaultExpireDateEnforced()) { | |
| 352 | -			if ($expirationDate === null) { | |
| 353 | -				throw new \InvalidArgumentException('Expiration date is enforced'); | |
| 354 | - } | |
| 355 | - | |
| 356 | - $date = new \DateTime(); | |
| 357 | - $date->setTime(0, 0, 0); | |
| 358 | -			$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); | |
| 359 | -			if ($date < $expirationDate) { | |
| 360 | -				$message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); | |
| 361 | - throw new GenericShareException($message, $message, 404); | |
| 362 | - } | |
| 363 | - } | |
| 364 | - | |
| 365 | - $accepted = true; | |
| 366 | - $message = ''; | |
| 367 | -		\OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ | |
| 368 | - 'expirationDate' => &$expirationDate, | |
| 369 | - 'accepted' => &$accepted, | |
| 370 | - 'message' => &$message, | |
| 371 | - 'passwordSet' => $share->getPassword() !== null, | |
| 372 | - ]); | |
| 373 | - | |
| 374 | -		if (!$accepted) { | |
| 375 | - throw new \Exception($message); | |
| 376 | - } | |
| 377 | - | |
| 378 | - $share->setExpirationDate($expirationDate); | |
| 379 | - | |
| 380 | - return $share; | |
| 381 | - } | |
| 382 | - | |
| 383 | - /** | |
| 384 | - * Check for pre share requirements for user shares | |
| 385 | - * | |
| 386 | - * @param \OCP\Share\IShare $share | |
| 387 | - * @throws \Exception | |
| 388 | - */ | |
| 389 | -	protected function userCreateChecks(\OCP\Share\IShare $share) { | |
| 390 | - // Check if we can share with group members only | |
| 391 | -		if ($this->shareWithGroupMembersOnly()) { | |
| 392 | - $sharedBy = $this->userManager->get($share->getSharedBy()); | |
| 393 | - $sharedWith = $this->userManager->get($share->getSharedWith()); | |
| 394 | - // Verify we can share with this user | |
| 395 | - $groups = array_intersect( | |
| 396 | - $this->groupManager->getUserGroupIds($sharedBy), | |
| 397 | - $this->groupManager->getUserGroupIds($sharedWith) | |
| 398 | - ); | |
| 399 | -			if (empty($groups)) { | |
| 400 | -				throw new \Exception('Only sharing with group members is allowed'); | |
| 401 | - } | |
| 402 | - } | |
| 403 | - | |
| 404 | - /* | |
| 277 | + $permissions = $share->getNode()->getPermissions(); | |
| 278 | + $mount = $share->getNode()->getMountPoint(); | |
| 279 | +        if (!($mount instanceof MoveableMount)) { | |
| 280 | + $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; | |
| 281 | + } | |
| 282 | + | |
| 283 | + // Check that we do not share with more permissions than we have | |
| 284 | +        if ($share->getPermissions() & ~$permissions) { | |
| 285 | +            $message_t = $this->l->t('Cannot increase permissions of %s', [$share->getNode()->getPath()]); | |
| 286 | + throw new GenericShareException($message_t, $message_t, 404); | |
| 287 | + } | |
| 288 | + | |
| 289 | + | |
| 290 | + // Check that read permissions are always set | |
| 291 | + // Link shares are allowed to have no read permissions to allow upload to hidden folders | |
| 292 | + $noReadPermissionRequired = $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK | |
| 293 | + || $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL; | |
| 294 | + if (!$noReadPermissionRequired && | |
| 295 | +            ($share->getPermissions() & \OCP\Constants::PERMISSION_READ) === 0) { | |
| 296 | +            throw new \InvalidArgumentException('Shares need at least read permissions'); | |
| 297 | + } | |
| 298 | + | |
| 299 | +        if ($share->getNode() instanceof \OCP\Files\File) { | |
| 300 | +            if ($share->getPermissions() & \OCP\Constants::PERMISSION_DELETE) { | |
| 301 | +                $message_t = $this->l->t('Files can\'t be shared with delete permissions'); | |
| 302 | + throw new GenericShareException($message_t); | |
| 303 | + } | |
| 304 | +            if ($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE) { | |
| 305 | +                $message_t = $this->l->t('Files can\'t be shared with create permissions'); | |
| 306 | + throw new GenericShareException($message_t); | |
| 307 | + } | |
| 308 | + } | |
| 309 | + } | |
| 310 | + | |
| 311 | + /** | |
| 312 | + * Validate if the expiration date fits the system settings | |
| 313 | + * | |
| 314 | + * @param \OCP\Share\IShare $share The share to validate the expiration date of | |
| 315 | + * @return \OCP\Share\IShare The modified share object | |
| 316 | + * @throws GenericShareException | |
| 317 | + * @throws \InvalidArgumentException | |
| 318 | + * @throws \Exception | |
| 319 | + */ | |
| 320 | +    protected function validateExpirationDate(\OCP\Share\IShare $share) { | |
| 321 | + | |
| 322 | + $expirationDate = $share->getExpirationDate(); | |
| 323 | + | |
| 324 | +        if ($expirationDate !== null) { | |
| 325 | + //Make sure the expiration date is a date | |
| 326 | + $expirationDate->setTime(0, 0, 0); | |
| 327 | + | |
| 328 | + $date = new \DateTime(); | |
| 329 | + $date->setTime(0, 0, 0); | |
| 330 | +            if ($date >= $expirationDate) { | |
| 331 | +                $message = $this->l->t('Expiration date is in the past'); | |
| 332 | + throw new GenericShareException($message, $message, 404); | |
| 333 | + } | |
| 334 | + } | |
| 335 | + | |
| 336 | + // If expiredate is empty set a default one if there is a default | |
| 337 | + $fullId = null; | |
| 338 | +        try { | |
| 339 | + $fullId = $share->getFullId(); | |
| 340 | +        } catch (\UnexpectedValueException $e) { | |
| 341 | + // This is a new share | |
| 342 | + } | |
| 343 | + | |
| 344 | +        if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { | |
| 345 | + $expirationDate = new \DateTime(); | |
| 346 | + $expirationDate->setTime(0,0,0); | |
| 347 | +            $expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); | |
| 348 | + } | |
| 349 | + | |
| 350 | + // If we enforce the expiration date check that is does not exceed | |
| 351 | +        if ($this->shareApiLinkDefaultExpireDateEnforced()) { | |
| 352 | +            if ($expirationDate === null) { | |
| 353 | +                throw new \InvalidArgumentException('Expiration date is enforced'); | |
| 354 | + } | |
| 355 | + | |
| 356 | + $date = new \DateTime(); | |
| 357 | + $date->setTime(0, 0, 0); | |
| 358 | +            $date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); | |
| 359 | +            if ($date < $expirationDate) { | |
| 360 | +                $message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); | |
| 361 | + throw new GenericShareException($message, $message, 404); | |
| 362 | + } | |
| 363 | + } | |
| 364 | + | |
| 365 | + $accepted = true; | |
| 366 | + $message = ''; | |
| 367 | +        \OCP\Util::emitHook('\OC\Share', 'verifyExpirationDate', [ | |
| 368 | + 'expirationDate' => &$expirationDate, | |
| 369 | + 'accepted' => &$accepted, | |
| 370 | + 'message' => &$message, | |
| 371 | + 'passwordSet' => $share->getPassword() !== null, | |
| 372 | + ]); | |
| 373 | + | |
| 374 | +        if (!$accepted) { | |
| 375 | + throw new \Exception($message); | |
| 376 | + } | |
| 377 | + | |
| 378 | + $share->setExpirationDate($expirationDate); | |
| 379 | + | |
| 380 | + return $share; | |
| 381 | + } | |
| 382 | + | |
| 383 | + /** | |
| 384 | + * Check for pre share requirements for user shares | |
| 385 | + * | |
| 386 | + * @param \OCP\Share\IShare $share | |
| 387 | + * @throws \Exception | |
| 388 | + */ | |
| 389 | +    protected function userCreateChecks(\OCP\Share\IShare $share) { | |
| 390 | + // Check if we can share with group members only | |
| 391 | +        if ($this->shareWithGroupMembersOnly()) { | |
| 392 | + $sharedBy = $this->userManager->get($share->getSharedBy()); | |
| 393 | + $sharedWith = $this->userManager->get($share->getSharedWith()); | |
| 394 | + // Verify we can share with this user | |
| 395 | + $groups = array_intersect( | |
| 396 | + $this->groupManager->getUserGroupIds($sharedBy), | |
| 397 | + $this->groupManager->getUserGroupIds($sharedWith) | |
| 398 | + ); | |
| 399 | +            if (empty($groups)) { | |
| 400 | +                throw new \Exception('Only sharing with group members is allowed'); | |
| 401 | + } | |
| 402 | + } | |
| 403 | + | |
| 404 | + /* | |
| 405 | 405 | * TODO: Could be costly, fix | 
| 406 | 406 | * | 
| 407 | 407 | * Also this is not what we want in the future.. then we want to squash identical shares. | 
| 408 | 408 | */ | 
| 409 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); | |
| 410 | - $existingShares = $provider->getSharesByPath($share->getNode()); | |
| 411 | -		foreach($existingShares as $existingShare) { | |
| 412 | - // Ignore if it is the same share | |
| 413 | -			try { | |
| 414 | -				if ($existingShare->getFullId() === $share->getFullId()) { | |
| 415 | - continue; | |
| 416 | - } | |
| 417 | -			} catch (\UnexpectedValueException $e) { | |
| 418 | - //Shares are not identical | |
| 419 | - } | |
| 420 | - | |
| 421 | - // Identical share already existst | |
| 422 | -			if ($existingShare->getSharedWith() === $share->getSharedWith()) { | |
| 423 | -				throw new \Exception('Path already shared with this user'); | |
| 424 | - } | |
| 425 | - | |
| 426 | - // The share is already shared with this user via a group share | |
| 427 | -			if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 428 | - $group = $this->groupManager->get($existingShare->getSharedWith()); | |
| 429 | -				if (!is_null($group)) { | |
| 430 | - $user = $this->userManager->get($share->getSharedWith()); | |
| 431 | - | |
| 432 | -					if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) { | |
| 433 | -						throw new \Exception('Path already shared with this user'); | |
| 434 | - } | |
| 435 | - } | |
| 436 | - } | |
| 437 | - } | |
| 438 | - } | |
| 439 | - | |
| 440 | - /** | |
| 441 | - * Check for pre share requirements for group shares | |
| 442 | - * | |
| 443 | - * @param \OCP\Share\IShare $share | |
| 444 | - * @throws \Exception | |
| 445 | - */ | |
| 446 | -	protected function groupCreateChecks(\OCP\Share\IShare $share) { | |
| 447 | - // Verify group shares are allowed | |
| 448 | -		if (!$this->allowGroupSharing()) { | |
| 449 | -			throw new \Exception('Group sharing is now allowed'); | |
| 450 | - } | |
| 451 | - | |
| 452 | - // Verify if the user can share with this group | |
| 453 | -		if ($this->shareWithGroupMembersOnly()) { | |
| 454 | - $sharedBy = $this->userManager->get($share->getSharedBy()); | |
| 455 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); | |
| 456 | -			if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) { | |
| 457 | -				throw new \Exception('Only sharing within your own groups is allowed'); | |
| 458 | - } | |
| 459 | - } | |
| 460 | - | |
| 461 | - /* | |
| 409 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); | |
| 410 | + $existingShares = $provider->getSharesByPath($share->getNode()); | |
| 411 | +        foreach($existingShares as $existingShare) { | |
| 412 | + // Ignore if it is the same share | |
| 413 | +            try { | |
| 414 | +                if ($existingShare->getFullId() === $share->getFullId()) { | |
| 415 | + continue; | |
| 416 | + } | |
| 417 | +            } catch (\UnexpectedValueException $e) { | |
| 418 | + //Shares are not identical | |
| 419 | + } | |
| 420 | + | |
| 421 | + // Identical share already existst | |
| 422 | +            if ($existingShare->getSharedWith() === $share->getSharedWith()) { | |
| 423 | +                throw new \Exception('Path already shared with this user'); | |
| 424 | + } | |
| 425 | + | |
| 426 | + // The share is already shared with this user via a group share | |
| 427 | +            if ($existingShare->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 428 | + $group = $this->groupManager->get($existingShare->getSharedWith()); | |
| 429 | +                if (!is_null($group)) { | |
| 430 | + $user = $this->userManager->get($share->getSharedWith()); | |
| 431 | + | |
| 432 | +                    if ($group->inGroup($user) && $existingShare->getShareOwner() !== $share->getShareOwner()) { | |
| 433 | +                        throw new \Exception('Path already shared with this user'); | |
| 434 | + } | |
| 435 | + } | |
| 436 | + } | |
| 437 | + } | |
| 438 | + } | |
| 439 | + | |
| 440 | + /** | |
| 441 | + * Check for pre share requirements for group shares | |
| 442 | + * | |
| 443 | + * @param \OCP\Share\IShare $share | |
| 444 | + * @throws \Exception | |
| 445 | + */ | |
| 446 | +    protected function groupCreateChecks(\OCP\Share\IShare $share) { | |
| 447 | + // Verify group shares are allowed | |
| 448 | +        if (!$this->allowGroupSharing()) { | |
| 449 | +            throw new \Exception('Group sharing is now allowed'); | |
| 450 | + } | |
| 451 | + | |
| 452 | + // Verify if the user can share with this group | |
| 453 | +        if ($this->shareWithGroupMembersOnly()) { | |
| 454 | + $sharedBy = $this->userManager->get($share->getSharedBy()); | |
| 455 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); | |
| 456 | +            if (is_null($sharedWith) || !$sharedWith->inGroup($sharedBy)) { | |
| 457 | +                throw new \Exception('Only sharing within your own groups is allowed'); | |
| 458 | + } | |
| 459 | + } | |
| 460 | + | |
| 461 | + /* | |
| 462 | 462 | * TODO: Could be costly, fix | 
| 463 | 463 | * | 
| 464 | 464 | * Also this is not what we want in the future.. then we want to squash identical shares. | 
| 465 | 465 | */ | 
| 466 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); | |
| 467 | - $existingShares = $provider->getSharesByPath($share->getNode()); | |
| 468 | -		foreach($existingShares as $existingShare) { | |
| 469 | -			try { | |
| 470 | -				if ($existingShare->getFullId() === $share->getFullId()) { | |
| 471 | - continue; | |
| 472 | - } | |
| 473 | -			} catch (\UnexpectedValueException $e) { | |
| 474 | - //It is a new share so just continue | |
| 475 | - } | |
| 476 | - | |
| 477 | -			if ($existingShare->getSharedWith() === $share->getSharedWith()) { | |
| 478 | -				throw new \Exception('Path already shared with this group'); | |
| 479 | - } | |
| 480 | - } | |
| 481 | - } | |
| 482 | - | |
| 483 | - /** | |
| 484 | - * Check for pre share requirements for link shares | |
| 485 | - * | |
| 486 | - * @param \OCP\Share\IShare $share | |
| 487 | - * @throws \Exception | |
| 488 | - */ | |
| 489 | -	protected function linkCreateChecks(\OCP\Share\IShare $share) { | |
| 490 | - // Are link shares allowed? | |
| 491 | -		if (!$this->shareApiAllowLinks()) { | |
| 492 | -			throw new \Exception('Link sharing not allowed'); | |
| 493 | - } | |
| 494 | - | |
| 495 | - // Link shares by definition can't have share permissions | |
| 496 | -		if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) { | |
| 497 | -			throw new \InvalidArgumentException('Link shares can\'t have reshare permissions'); | |
| 498 | - } | |
| 499 | - | |
| 500 | - // Check if public upload is allowed | |
| 501 | - if (!$this->shareApiLinkAllowPublicUpload() && | |
| 502 | -			($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) { | |
| 503 | -			throw new \InvalidArgumentException('Public upload not allowed'); | |
| 504 | - } | |
| 505 | - } | |
| 506 | - | |
| 507 | - /** | |
| 508 | - * To make sure we don't get invisible link shares we set the parent | |
| 509 | - * of a link if it is a reshare. This is a quick word around | |
| 510 | - * until we can properly display multiple link shares in the UI | |
| 511 | - * | |
| 512 | - * See: https://github.com/owncloud/core/issues/22295 | |
| 513 | - * | |
| 514 | - * FIXME: Remove once multiple link shares can be properly displayed | |
| 515 | - * | |
| 516 | - * @param \OCP\Share\IShare $share | |
| 517 | - */ | |
| 518 | -	protected function setLinkParent(\OCP\Share\IShare $share) { | |
| 519 | - | |
| 520 | - // No sense in checking if the method is not there. | |
| 521 | -		if (method_exists($share, 'setParent')) { | |
| 522 | - $storage = $share->getNode()->getStorage(); | |
| 523 | -			if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { | |
| 524 | - /** @var \OCA\Files_Sharing\SharedStorage $storage */ | |
| 525 | - $share->setParent($storage->getShareId()); | |
| 526 | - } | |
| 527 | - }; | |
| 528 | - } | |
| 529 | - | |
| 530 | - /** | |
| 531 | - * @param File|Folder $path | |
| 532 | - */ | |
| 533 | -	protected function pathCreateChecks($path) { | |
| 534 | - // Make sure that we do not share a path that contains a shared mountpoint | |
| 535 | -		if ($path instanceof \OCP\Files\Folder) { | |
| 536 | - $mounts = $this->mountManager->findIn($path->getPath()); | |
| 537 | -			foreach($mounts as $mount) { | |
| 538 | -				if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { | |
| 539 | -					throw new \InvalidArgumentException('Path contains files shared with you'); | |
| 540 | - } | |
| 541 | - } | |
| 542 | - } | |
| 543 | - } | |
| 544 | - | |
| 545 | - /** | |
| 546 | - * Check if the user that is sharing can actually share | |
| 547 | - * | |
| 548 | - * @param \OCP\Share\IShare $share | |
| 549 | - * @throws \Exception | |
| 550 | - */ | |
| 551 | -	protected function canShare(\OCP\Share\IShare $share) { | |
| 552 | -		if (!$this->shareApiEnabled()) { | |
| 553 | -			throw new \Exception('The share API is disabled'); | |
| 554 | - } | |
| 555 | - | |
| 556 | -		if ($this->sharingDisabledForUser($share->getSharedBy())) { | |
| 557 | -			throw new \Exception('You are not allowed to share'); | |
| 558 | - } | |
| 559 | - } | |
| 560 | - | |
| 561 | - /** | |
| 562 | - * Share a path | |
| 563 | - * | |
| 564 | - * @param \OCP\Share\IShare $share | |
| 565 | - * @return Share The share object | |
| 566 | - * @throws \Exception | |
| 567 | - * | |
| 568 | - * TODO: handle link share permissions or check them | |
| 569 | - */ | |
| 570 | -	public function createShare(\OCP\Share\IShare $share) { | |
| 571 | - $this->canShare($share); | |
| 572 | - | |
| 573 | - $this->generalCreateChecks($share); | |
| 574 | - | |
| 575 | - // Verify if there are any issues with the path | |
| 576 | - $this->pathCreateChecks($share->getNode()); | |
| 577 | - | |
| 578 | - /* | |
| 466 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); | |
| 467 | + $existingShares = $provider->getSharesByPath($share->getNode()); | |
| 468 | +        foreach($existingShares as $existingShare) { | |
| 469 | +            try { | |
| 470 | +                if ($existingShare->getFullId() === $share->getFullId()) { | |
| 471 | + continue; | |
| 472 | + } | |
| 473 | +            } catch (\UnexpectedValueException $e) { | |
| 474 | + //It is a new share so just continue | |
| 475 | + } | |
| 476 | + | |
| 477 | +            if ($existingShare->getSharedWith() === $share->getSharedWith()) { | |
| 478 | +                throw new \Exception('Path already shared with this group'); | |
| 479 | + } | |
| 480 | + } | |
| 481 | + } | |
| 482 | + | |
| 483 | + /** | |
| 484 | + * Check for pre share requirements for link shares | |
| 485 | + * | |
| 486 | + * @param \OCP\Share\IShare $share | |
| 487 | + * @throws \Exception | |
| 488 | + */ | |
| 489 | +    protected function linkCreateChecks(\OCP\Share\IShare $share) { | |
| 490 | + // Are link shares allowed? | |
| 491 | +        if (!$this->shareApiAllowLinks()) { | |
| 492 | +            throw new \Exception('Link sharing not allowed'); | |
| 493 | + } | |
| 494 | + | |
| 495 | + // Link shares by definition can't have share permissions | |
| 496 | +        if ($share->getPermissions() & \OCP\Constants::PERMISSION_SHARE) { | |
| 497 | +            throw new \InvalidArgumentException('Link shares can\'t have reshare permissions'); | |
| 498 | + } | |
| 499 | + | |
| 500 | + // Check if public upload is allowed | |
| 501 | + if (!$this->shareApiLinkAllowPublicUpload() && | |
| 502 | +            ($share->getPermissions() & (\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE | \OCP\Constants::PERMISSION_DELETE))) { | |
| 503 | +            throw new \InvalidArgumentException('Public upload not allowed'); | |
| 504 | + } | |
| 505 | + } | |
| 506 | + | |
| 507 | + /** | |
| 508 | + * To make sure we don't get invisible link shares we set the parent | |
| 509 | + * of a link if it is a reshare. This is a quick word around | |
| 510 | + * until we can properly display multiple link shares in the UI | |
| 511 | + * | |
| 512 | + * See: https://github.com/owncloud/core/issues/22295 | |
| 513 | + * | |
| 514 | + * FIXME: Remove once multiple link shares can be properly displayed | |
| 515 | + * | |
| 516 | + * @param \OCP\Share\IShare $share | |
| 517 | + */ | |
| 518 | +    protected function setLinkParent(\OCP\Share\IShare $share) { | |
| 519 | + | |
| 520 | + // No sense in checking if the method is not there. | |
| 521 | +        if (method_exists($share, 'setParent')) { | |
| 522 | + $storage = $share->getNode()->getStorage(); | |
| 523 | +            if ($storage->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { | |
| 524 | + /** @var \OCA\Files_Sharing\SharedStorage $storage */ | |
| 525 | + $share->setParent($storage->getShareId()); | |
| 526 | + } | |
| 527 | + }; | |
| 528 | + } | |
| 529 | + | |
| 530 | + /** | |
| 531 | + * @param File|Folder $path | |
| 532 | + */ | |
| 533 | +    protected function pathCreateChecks($path) { | |
| 534 | + // Make sure that we do not share a path that contains a shared mountpoint | |
| 535 | +        if ($path instanceof \OCP\Files\Folder) { | |
| 536 | + $mounts = $this->mountManager->findIn($path->getPath()); | |
| 537 | +            foreach($mounts as $mount) { | |
| 538 | +                if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { | |
| 539 | +                    throw new \InvalidArgumentException('Path contains files shared with you'); | |
| 540 | + } | |
| 541 | + } | |
| 542 | + } | |
| 543 | + } | |
| 544 | + | |
| 545 | + /** | |
| 546 | + * Check if the user that is sharing can actually share | |
| 547 | + * | |
| 548 | + * @param \OCP\Share\IShare $share | |
| 549 | + * @throws \Exception | |
| 550 | + */ | |
| 551 | +    protected function canShare(\OCP\Share\IShare $share) { | |
| 552 | +        if (!$this->shareApiEnabled()) { | |
| 553 | +            throw new \Exception('The share API is disabled'); | |
| 554 | + } | |
| 555 | + | |
| 556 | +        if ($this->sharingDisabledForUser($share->getSharedBy())) { | |
| 557 | +            throw new \Exception('You are not allowed to share'); | |
| 558 | + } | |
| 559 | + } | |
| 560 | + | |
| 561 | + /** | |
| 562 | + * Share a path | |
| 563 | + * | |
| 564 | + * @param \OCP\Share\IShare $share | |
| 565 | + * @return Share The share object | |
| 566 | + * @throws \Exception | |
| 567 | + * | |
| 568 | + * TODO: handle link share permissions or check them | |
| 569 | + */ | |
| 570 | +    public function createShare(\OCP\Share\IShare $share) { | |
| 571 | + $this->canShare($share); | |
| 572 | + | |
| 573 | + $this->generalCreateChecks($share); | |
| 574 | + | |
| 575 | + // Verify if there are any issues with the path | |
| 576 | + $this->pathCreateChecks($share->getNode()); | |
| 577 | + | |
| 578 | + /* | |
| 579 | 579 | * On creation of a share the owner is always the owner of the path | 
| 580 | 580 | * Except for mounted federated shares. | 
| 581 | 581 | */ | 
| 582 | - $storage = $share->getNode()->getStorage(); | |
| 583 | -		if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { | |
| 584 | - $parent = $share->getNode()->getParent(); | |
| 585 | -			while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { | |
| 586 | - $parent = $parent->getParent(); | |
| 587 | - } | |
| 588 | - $share->setShareOwner($parent->getOwner()->getUID()); | |
| 589 | -		} else { | |
| 590 | - $share->setShareOwner($share->getNode()->getOwner()->getUID()); | |
| 591 | - } | |
| 592 | - | |
| 593 | - //Verify share type | |
| 594 | -		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { | |
| 595 | - $this->userCreateChecks($share); | |
| 596 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 597 | - $this->groupCreateChecks($share); | |
| 598 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { | |
| 599 | - $this->linkCreateChecks($share); | |
| 600 | - $this->setLinkParent($share); | |
| 601 | - | |
| 602 | - /* | |
| 582 | + $storage = $share->getNode()->getStorage(); | |
| 583 | +        if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { | |
| 584 | + $parent = $share->getNode()->getParent(); | |
| 585 | +            while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { | |
| 586 | + $parent = $parent->getParent(); | |
| 587 | + } | |
| 588 | + $share->setShareOwner($parent->getOwner()->getUID()); | |
| 589 | +        } else { | |
| 590 | + $share->setShareOwner($share->getNode()->getOwner()->getUID()); | |
| 591 | + } | |
| 592 | + | |
| 593 | + //Verify share type | |
| 594 | +        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { | |
| 595 | + $this->userCreateChecks($share); | |
| 596 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 597 | + $this->groupCreateChecks($share); | |
| 598 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { | |
| 599 | + $this->linkCreateChecks($share); | |
| 600 | + $this->setLinkParent($share); | |
| 601 | + | |
| 602 | + /* | |
| 603 | 603 | * For now ignore a set token. | 
| 604 | 604 | */ | 
| 605 | - $share->setToken( | |
| 606 | - $this->secureRandom->generate( | |
| 607 | - \OC\Share\Constants::TOKEN_LENGTH, | |
| 608 | - \OCP\Security\ISecureRandom::CHAR_LOWER. | |
| 609 | - \OCP\Security\ISecureRandom::CHAR_UPPER. | |
| 610 | - \OCP\Security\ISecureRandom::CHAR_DIGITS | |
| 611 | - ) | |
| 612 | - ); | |
| 613 | - | |
| 614 | - //Verify the expiration date | |
| 615 | - $this->validateExpirationDate($share); | |
| 616 | - | |
| 617 | - //Verify the password | |
| 618 | - $this->verifyPassword($share->getPassword()); | |
| 619 | - | |
| 620 | - // If a password is set. Hash it! | |
| 621 | -			if ($share->getPassword() !== null) { | |
| 622 | - $share->setPassword($this->hasher->hash($share->getPassword())); | |
| 623 | - } | |
| 624 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { | |
| 625 | - $share->setToken( | |
| 626 | - $this->secureRandom->generate( | |
| 627 | - \OC\Share\Constants::TOKEN_LENGTH, | |
| 628 | - \OCP\Security\ISecureRandom::CHAR_LOWER. | |
| 629 | - \OCP\Security\ISecureRandom::CHAR_UPPER. | |
| 630 | - \OCP\Security\ISecureRandom::CHAR_DIGITS | |
| 631 | - ) | |
| 632 | - ); | |
| 633 | - } | |
| 634 | - | |
| 635 | - // Cannot share with the owner | |
| 636 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && | |
| 637 | -			$share->getSharedWith() === $share->getShareOwner()) { | |
| 638 | -			throw new \InvalidArgumentException('Can\'t share with the share owner'); | |
| 639 | - } | |
| 640 | - | |
| 641 | - // Generate the target | |
| 642 | -		$target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName(); | |
| 643 | - $target = \OC\Files\Filesystem::normalizePath($target); | |
| 644 | - $share->setTarget($target); | |
| 645 | - | |
| 646 | - // Pre share hook | |
| 647 | - $run = true; | |
| 648 | - $error = ''; | |
| 649 | - $preHookData = [ | |
| 650 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 651 | - 'itemSource' => $share->getNode()->getId(), | |
| 652 | - 'shareType' => $share->getShareType(), | |
| 653 | - 'uidOwner' => $share->getSharedBy(), | |
| 654 | - 'permissions' => $share->getPermissions(), | |
| 655 | - 'fileSource' => $share->getNode()->getId(), | |
| 656 | - 'expiration' => $share->getExpirationDate(), | |
| 657 | - 'token' => $share->getToken(), | |
| 658 | - 'itemTarget' => $share->getTarget(), | |
| 659 | - 'shareWith' => $share->getSharedWith(), | |
| 660 | - 'run' => &$run, | |
| 661 | - 'error' => &$error, | |
| 662 | - ]; | |
| 663 | -		\OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData); | |
| 664 | - | |
| 665 | -		if ($run === false) { | |
| 666 | - throw new \Exception($error); | |
| 667 | - } | |
| 668 | - | |
| 669 | - $oldShare = $share; | |
| 670 | - $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 671 | - $share = $provider->create($share); | |
| 672 | - //reuse the node we already have | |
| 673 | - $share->setNode($oldShare->getNode()); | |
| 674 | - | |
| 675 | - // Post share hook | |
| 676 | - $postHookData = [ | |
| 677 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 678 | - 'itemSource' => $share->getNode()->getId(), | |
| 679 | - 'shareType' => $share->getShareType(), | |
| 680 | - 'uidOwner' => $share->getSharedBy(), | |
| 681 | - 'permissions' => $share->getPermissions(), | |
| 682 | - 'fileSource' => $share->getNode()->getId(), | |
| 683 | - 'expiration' => $share->getExpirationDate(), | |
| 684 | - 'token' => $share->getToken(), | |
| 685 | - 'id' => $share->getId(), | |
| 686 | - 'shareWith' => $share->getSharedWith(), | |
| 687 | - 'itemTarget' => $share->getTarget(), | |
| 688 | - 'fileTarget' => $share->getTarget(), | |
| 689 | - ]; | |
| 690 | - | |
| 691 | -		\OC_Hook::emit('OCP\Share', 'post_shared', $postHookData); | |
| 692 | - | |
| 693 | -		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { | |
| 694 | - $user = $this->userManager->get($share->getSharedWith()); | |
| 695 | -			if ($user !== null) { | |
| 696 | - $emailAddress = $user->getEMailAddress(); | |
| 697 | -				if ($emailAddress !== null && $emailAddress !== '') { | |
| 698 | - $userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null); | |
| 699 | -					$l = $this->l10nFactory->get('lib', $userLang); | |
| 700 | - $this->sendMailNotification( | |
| 701 | - $l, | |
| 702 | - $share->getNode()->getName(), | |
| 703 | -						$this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', [ 'fileid' => $share->getNode()->getId() ]), | |
| 704 | - $share->getSharedBy(), | |
| 705 | - $emailAddress, | |
| 706 | - $share->getExpirationDate() | |
| 707 | - ); | |
| 708 | -					$this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']); | |
| 709 | -				} else { | |
| 710 | -					$this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']); | |
| 711 | - } | |
| 712 | -			} else { | |
| 713 | -				$this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']); | |
| 714 | - } | |
| 715 | - } | |
| 716 | - | |
| 717 | - return $share; | |
| 718 | - } | |
| 719 | - | |
| 720 | - /** | |
| 721 | - * @param IL10N $l Language of the recipient | |
| 722 | - * @param string $filename file/folder name | |
| 723 | - * @param string $link link to the file/folder | |
| 724 | - * @param string $initiator user ID of share sender | |
| 725 | - * @param string $shareWith email address of share receiver | |
| 726 | - * @param \DateTime|null $expiration | |
| 727 | - * @throws \Exception If mail couldn't be sent | |
| 728 | - */ | |
| 729 | - protected function sendMailNotification(IL10N $l, | |
| 730 | - $filename, | |
| 731 | - $link, | |
| 732 | - $initiator, | |
| 733 | - $shareWith, | |
| 734 | -											\DateTime $expiration = null) { | |
| 735 | - $initiatorUser = $this->userManager->get($initiator); | |
| 736 | - $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; | |
| 737 | -		$subject = $l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)); | |
| 738 | - | |
| 739 | - $message = $this->mailer->createMessage(); | |
| 740 | - | |
| 741 | -		$emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [ | |
| 742 | - 'filename' => $filename, | |
| 743 | - 'link' => $link, | |
| 744 | - 'initiator' => $initiatorDisplayName, | |
| 745 | - 'expiration' => $expiration, | |
| 746 | - 'shareWith' => $shareWith, | |
| 747 | - ]); | |
| 748 | - | |
| 749 | - $emailTemplate->addHeader(); | |
| 750 | -		$emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false); | |
| 751 | -		$text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); | |
| 752 | - | |
| 753 | - $emailTemplate->addBodyText( | |
| 754 | -			$text . ' ' . $l->t('Click the button below to open it.'), | |
| 755 | - $text | |
| 756 | - ); | |
| 757 | - $emailTemplate->addBodyButton( | |
| 758 | -			$l->t('Open »%s«', [$filename]), | |
| 759 | - $link | |
| 760 | - ); | |
| 761 | - | |
| 762 | - $message->setTo([$shareWith]); | |
| 763 | - | |
| 764 | - // The "From" contains the sharers name | |
| 765 | - $instanceName = $this->defaults->getName(); | |
| 766 | - $senderName = $l->t( | |
| 767 | - '%s via %s', | |
| 768 | - [ | |
| 769 | - $initiatorDisplayName, | |
| 770 | - $instanceName | |
| 771 | - ] | |
| 772 | - ); | |
| 773 | - $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); | |
| 774 | - | |
| 775 | - // The "Reply-To" is set to the sharer if an mail address is configured | |
| 776 | - // also the default footer contains a "Do not reply" which needs to be adjusted. | |
| 777 | - $initiatorEmail = $initiatorUser->getEMailAddress(); | |
| 778 | -		if($initiatorEmail !== null) { | |
| 779 | - $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); | |
| 780 | - $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); | |
| 781 | -		} else { | |
| 782 | - $emailTemplate->addFooter(); | |
| 783 | - } | |
| 784 | - | |
| 785 | - $message->setSubject($subject); | |
| 786 | - $message->setPlainBody($emailTemplate->renderText()); | |
| 787 | - $message->setHtmlBody($emailTemplate->renderHtml()); | |
| 788 | - $this->mailer->send($message); | |
| 789 | - } | |
| 790 | - | |
| 791 | - /** | |
| 792 | - * Update a share | |
| 793 | - * | |
| 794 | - * @param \OCP\Share\IShare $share | |
| 795 | - * @return \OCP\Share\IShare The share object | |
| 796 | - * @throws \InvalidArgumentException | |
| 797 | - */ | |
| 798 | -	public function updateShare(\OCP\Share\IShare $share) { | |
| 799 | - $expirationDateUpdated = false; | |
| 800 | - | |
| 801 | - $this->canShare($share); | |
| 802 | - | |
| 803 | -		try { | |
| 804 | - $originalShare = $this->getShareById($share->getFullId()); | |
| 805 | -		} catch (\UnexpectedValueException $e) { | |
| 806 | -			throw new \InvalidArgumentException('Share does not have a full id'); | |
| 807 | - } | |
| 808 | - | |
| 809 | - // We can't change the share type! | |
| 810 | -		if ($share->getShareType() !== $originalShare->getShareType()) { | |
| 811 | -			throw new \InvalidArgumentException('Can\'t change share type'); | |
| 812 | - } | |
| 813 | - | |
| 814 | - // We can only change the recipient on user shares | |
| 815 | - if ($share->getSharedWith() !== $originalShare->getSharedWith() && | |
| 816 | -		    $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) { | |
| 817 | -			throw new \InvalidArgumentException('Can only update recipient on user shares'); | |
| 818 | - } | |
| 819 | - | |
| 820 | - // Cannot share with the owner | |
| 821 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && | |
| 822 | -			$share->getSharedWith() === $share->getShareOwner()) { | |
| 823 | -			throw new \InvalidArgumentException('Can\'t share with the share owner'); | |
| 824 | - } | |
| 825 | - | |
| 826 | - $this->generalCreateChecks($share); | |
| 827 | - | |
| 828 | -		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { | |
| 829 | - $this->userCreateChecks($share); | |
| 830 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 831 | - $this->groupCreateChecks($share); | |
| 832 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { | |
| 833 | - $this->linkCreateChecks($share); | |
| 834 | - | |
| 835 | - $this->updateSharePasswordIfNeeded($share, $originalShare); | |
| 836 | - | |
| 837 | -			if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { | |
| 838 | - //Verify the expiration date | |
| 839 | - $this->validateExpirationDate($share); | |
| 840 | - $expirationDateUpdated = true; | |
| 841 | - } | |
| 842 | -		} else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { | |
| 843 | - $plainTextPassword = $share->getPassword(); | |
| 844 | -			if (!$this->updateSharePasswordIfNeeded($share, $originalShare)) { | |
| 845 | - $plainTextPassword = null; | |
| 846 | - } | |
| 847 | - } | |
| 848 | - | |
| 849 | - $this->pathCreateChecks($share->getNode()); | |
| 850 | - | |
| 851 | - // Now update the share! | |
| 852 | - $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 853 | -		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { | |
| 854 | - $share = $provider->update($share, $plainTextPassword); | |
| 855 | -		} else { | |
| 856 | - $share = $provider->update($share); | |
| 857 | - } | |
| 858 | - | |
| 859 | -		if ($expirationDateUpdated === true) { | |
| 860 | -			\OC_Hook::emit('OCP\Share', 'post_set_expiration_date', [ | |
| 861 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 862 | - 'itemSource' => $share->getNode()->getId(), | |
| 863 | - 'date' => $share->getExpirationDate(), | |
| 864 | - 'uidOwner' => $share->getSharedBy(), | |
| 865 | - ]); | |
| 866 | - } | |
| 867 | - | |
| 868 | -		if ($share->getPassword() !== $originalShare->getPassword()) { | |
| 869 | -			\OC_Hook::emit('OCP\Share', 'post_update_password', [ | |
| 870 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 871 | - 'itemSource' => $share->getNode()->getId(), | |
| 872 | - 'uidOwner' => $share->getSharedBy(), | |
| 873 | - 'token' => $share->getToken(), | |
| 874 | - 'disabled' => is_null($share->getPassword()), | |
| 875 | - ]); | |
| 876 | - } | |
| 877 | - | |
| 878 | -		if ($share->getPermissions() !== $originalShare->getPermissions()) { | |
| 879 | -			if ($this->userManager->userExists($share->getShareOwner())) { | |
| 880 | - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); | |
| 881 | -			} else { | |
| 882 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); | |
| 883 | - } | |
| 884 | -			\OC_Hook::emit('OCP\Share', 'post_update_permissions', array( | |
| 885 | - 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 886 | - 'itemSource' => $share->getNode()->getId(), | |
| 887 | - 'shareType' => $share->getShareType(), | |
| 888 | - 'shareWith' => $share->getSharedWith(), | |
| 889 | - 'uidOwner' => $share->getSharedBy(), | |
| 890 | - 'permissions' => $share->getPermissions(), | |
| 891 | - 'path' => $userFolder->getRelativePath($share->getNode()->getPath()), | |
| 892 | - )); | |
| 893 | - } | |
| 894 | - | |
| 895 | - return $share; | |
| 896 | - } | |
| 897 | - | |
| 898 | - /** | |
| 899 | - * Updates the password of the given share if it is not the same as the | |
| 900 | - * password of the original share. | |
| 901 | - * | |
| 902 | - * @param \OCP\Share\IShare $share the share to update its password. | |
| 903 | - * @param \OCP\Share\IShare $originalShare the original share to compare its | |
| 904 | - * password with. | |
| 905 | - * @return boolean whether the password was updated or not. | |
| 906 | - */ | |
| 907 | -	private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) { | |
| 908 | - // Password updated. | |
| 909 | -		if ($share->getPassword() !== $originalShare->getPassword()) { | |
| 910 | - //Verify the password | |
| 911 | - $this->verifyPassword($share->getPassword()); | |
| 912 | - | |
| 913 | - // If a password is set. Hash it! | |
| 914 | -			if ($share->getPassword() !== null) { | |
| 915 | - $share->setPassword($this->hasher->hash($share->getPassword())); | |
| 916 | - | |
| 917 | - return true; | |
| 918 | - } | |
| 919 | - } | |
| 920 | - | |
| 921 | - return false; | |
| 922 | - } | |
| 923 | - | |
| 924 | - /** | |
| 925 | - * Delete all the children of this share | |
| 926 | - * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in | |
| 927 | - * | |
| 928 | - * @param \OCP\Share\IShare $share | |
| 929 | - * @return \OCP\Share\IShare[] List of deleted shares | |
| 930 | - */ | |
| 931 | -	protected function deleteChildren(\OCP\Share\IShare $share) { | |
| 932 | - $deletedShares = []; | |
| 933 | - | |
| 934 | - $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 935 | - | |
| 936 | -		foreach ($provider->getChildren($share) as $child) { | |
| 937 | - $deletedChildren = $this->deleteChildren($child); | |
| 938 | - $deletedShares = array_merge($deletedShares, $deletedChildren); | |
| 939 | - | |
| 940 | - $provider->delete($child); | |
| 941 | - $deletedShares[] = $child; | |
| 942 | - } | |
| 943 | - | |
| 944 | - return $deletedShares; | |
| 945 | - } | |
| 946 | - | |
| 947 | - /** | |
| 948 | - * Delete a share | |
| 949 | - * | |
| 950 | - * @param \OCP\Share\IShare $share | |
| 951 | - * @throws ShareNotFound | |
| 952 | - * @throws \InvalidArgumentException | |
| 953 | - */ | |
| 954 | -	public function deleteShare(\OCP\Share\IShare $share) { | |
| 955 | - | |
| 956 | -		try { | |
| 957 | - $share->getFullId(); | |
| 958 | -		} catch (\UnexpectedValueException $e) { | |
| 959 | -			throw new \InvalidArgumentException('Share does not have a full id'); | |
| 960 | - } | |
| 961 | - | |
| 962 | - $event = new GenericEvent($share); | |
| 963 | -		$this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event); | |
| 964 | - | |
| 965 | - // Get all children and delete them as well | |
| 966 | - $deletedShares = $this->deleteChildren($share); | |
| 967 | - | |
| 968 | - // Do the actual delete | |
| 969 | - $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 970 | - $provider->delete($share); | |
| 971 | - | |
| 972 | - // All the deleted shares caused by this delete | |
| 973 | - $deletedShares[] = $share; | |
| 974 | - | |
| 975 | - // Emit post hook | |
| 976 | -		$event->setArgument('deletedShares', $deletedShares); | |
| 977 | -		$this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event); | |
| 978 | - } | |
| 979 | - | |
| 980 | - | |
| 981 | - /** | |
| 982 | - * Unshare a file as the recipient. | |
| 983 | - * This can be different from a regular delete for example when one of | |
| 984 | - * the users in a groups deletes that share. But the provider should | |
| 985 | - * handle this. | |
| 986 | - * | |
| 987 | - * @param \OCP\Share\IShare $share | |
| 988 | - * @param string $recipientId | |
| 989 | - */ | |
| 990 | -	public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { | |
| 991 | - list($providerId, ) = $this->splitFullId($share->getFullId()); | |
| 992 | - $provider = $this->factory->getProvider($providerId); | |
| 993 | - | |
| 994 | - $provider->deleteFromSelf($share, $recipientId); | |
| 995 | - } | |
| 996 | - | |
| 997 | - /** | |
| 998 | - * @inheritdoc | |
| 999 | - */ | |
| 1000 | -	public function moveShare(\OCP\Share\IShare $share, $recipientId) { | |
| 1001 | -		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { | |
| 1002 | -			throw new \InvalidArgumentException('Can\'t change target of link share'); | |
| 1003 | - } | |
| 1004 | - | |
| 1005 | -		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) { | |
| 1006 | -			throw new \InvalidArgumentException('Invalid recipient'); | |
| 1007 | - } | |
| 1008 | - | |
| 1009 | -		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 1010 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); | |
| 1011 | -			if (is_null($sharedWith)) { | |
| 1012 | -				throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); | |
| 1013 | - } | |
| 1014 | - $recipient = $this->userManager->get($recipientId); | |
| 1015 | -			if (!$sharedWith->inGroup($recipient)) { | |
| 1016 | -				throw new \InvalidArgumentException('Invalid recipient'); | |
| 1017 | - } | |
| 1018 | - } | |
| 1019 | - | |
| 1020 | - list($providerId, ) = $this->splitFullId($share->getFullId()); | |
| 1021 | - $provider = $this->factory->getProvider($providerId); | |
| 1022 | - | |
| 1023 | - $provider->move($share, $recipientId); | |
| 1024 | - } | |
| 1025 | - | |
| 1026 | -	public function getSharesInFolder($userId, Folder $node, $reshares = false) { | |
| 1027 | - $providers = $this->factory->getAllProviders(); | |
| 1028 | - | |
| 1029 | -		return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) { | |
| 1030 | - $newShares = $provider->getSharesInFolder($userId, $node, $reshares); | |
| 1031 | -			foreach ($newShares as $fid => $data) { | |
| 1032 | -				if (!isset($shares[$fid])) { | |
| 1033 | - $shares[$fid] = []; | |
| 1034 | - } | |
| 1035 | - | |
| 1036 | - $shares[$fid] = array_merge($shares[$fid], $data); | |
| 1037 | - } | |
| 1038 | - return $shares; | |
| 1039 | - }, []); | |
| 1040 | - } | |
| 1041 | - | |
| 1042 | - /** | |
| 1043 | - * @inheritdoc | |
| 1044 | - */ | |
| 1045 | -	public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { | |
| 1046 | - if ($path !== null && | |
| 1047 | - !($path instanceof \OCP\Files\File) && | |
| 1048 | -				!($path instanceof \OCP\Files\Folder)) { | |
| 1049 | -			throw new \InvalidArgumentException('invalid path'); | |
| 1050 | - } | |
| 1051 | - | |
| 1052 | -		try { | |
| 1053 | - $provider = $this->factory->getProviderForType($shareType); | |
| 1054 | -		} catch (ProviderException $e) { | |
| 1055 | - return []; | |
| 1056 | - } | |
| 1057 | - | |
| 1058 | - $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); | |
| 1059 | - | |
| 1060 | - /* | |
| 605 | + $share->setToken( | |
| 606 | + $this->secureRandom->generate( | |
| 607 | + \OC\Share\Constants::TOKEN_LENGTH, | |
| 608 | + \OCP\Security\ISecureRandom::CHAR_LOWER. | |
| 609 | + \OCP\Security\ISecureRandom::CHAR_UPPER. | |
| 610 | + \OCP\Security\ISecureRandom::CHAR_DIGITS | |
| 611 | + ) | |
| 612 | + ); | |
| 613 | + | |
| 614 | + //Verify the expiration date | |
| 615 | + $this->validateExpirationDate($share); | |
| 616 | + | |
| 617 | + //Verify the password | |
| 618 | + $this->verifyPassword($share->getPassword()); | |
| 619 | + | |
| 620 | + // If a password is set. Hash it! | |
| 621 | +            if ($share->getPassword() !== null) { | |
| 622 | + $share->setPassword($this->hasher->hash($share->getPassword())); | |
| 623 | + } | |
| 624 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { | |
| 625 | + $share->setToken( | |
| 626 | + $this->secureRandom->generate( | |
| 627 | + \OC\Share\Constants::TOKEN_LENGTH, | |
| 628 | + \OCP\Security\ISecureRandom::CHAR_LOWER. | |
| 629 | + \OCP\Security\ISecureRandom::CHAR_UPPER. | |
| 630 | + \OCP\Security\ISecureRandom::CHAR_DIGITS | |
| 631 | + ) | |
| 632 | + ); | |
| 633 | + } | |
| 634 | + | |
| 635 | + // Cannot share with the owner | |
| 636 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && | |
| 637 | +            $share->getSharedWith() === $share->getShareOwner()) { | |
| 638 | +            throw new \InvalidArgumentException('Can\'t share with the share owner'); | |
| 639 | + } | |
| 640 | + | |
| 641 | + // Generate the target | |
| 642 | +        $target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName(); | |
| 643 | + $target = \OC\Files\Filesystem::normalizePath($target); | |
| 644 | + $share->setTarget($target); | |
| 645 | + | |
| 646 | + // Pre share hook | |
| 647 | + $run = true; | |
| 648 | + $error = ''; | |
| 649 | + $preHookData = [ | |
| 650 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 651 | + 'itemSource' => $share->getNode()->getId(), | |
| 652 | + 'shareType' => $share->getShareType(), | |
| 653 | + 'uidOwner' => $share->getSharedBy(), | |
| 654 | + 'permissions' => $share->getPermissions(), | |
| 655 | + 'fileSource' => $share->getNode()->getId(), | |
| 656 | + 'expiration' => $share->getExpirationDate(), | |
| 657 | + 'token' => $share->getToken(), | |
| 658 | + 'itemTarget' => $share->getTarget(), | |
| 659 | + 'shareWith' => $share->getSharedWith(), | |
| 660 | + 'run' => &$run, | |
| 661 | + 'error' => &$error, | |
| 662 | + ]; | |
| 663 | +        \OC_Hook::emit('OCP\Share', 'pre_shared', $preHookData); | |
| 664 | + | |
| 665 | +        if ($run === false) { | |
| 666 | + throw new \Exception($error); | |
| 667 | + } | |
| 668 | + | |
| 669 | + $oldShare = $share; | |
| 670 | + $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 671 | + $share = $provider->create($share); | |
| 672 | + //reuse the node we already have | |
| 673 | + $share->setNode($oldShare->getNode()); | |
| 674 | + | |
| 675 | + // Post share hook | |
| 676 | + $postHookData = [ | |
| 677 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 678 | + 'itemSource' => $share->getNode()->getId(), | |
| 679 | + 'shareType' => $share->getShareType(), | |
| 680 | + 'uidOwner' => $share->getSharedBy(), | |
| 681 | + 'permissions' => $share->getPermissions(), | |
| 682 | + 'fileSource' => $share->getNode()->getId(), | |
| 683 | + 'expiration' => $share->getExpirationDate(), | |
| 684 | + 'token' => $share->getToken(), | |
| 685 | + 'id' => $share->getId(), | |
| 686 | + 'shareWith' => $share->getSharedWith(), | |
| 687 | + 'itemTarget' => $share->getTarget(), | |
| 688 | + 'fileTarget' => $share->getTarget(), | |
| 689 | + ]; | |
| 690 | + | |
| 691 | +        \OC_Hook::emit('OCP\Share', 'post_shared', $postHookData); | |
| 692 | + | |
| 693 | +        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { | |
| 694 | + $user = $this->userManager->get($share->getSharedWith()); | |
| 695 | +            if ($user !== null) { | |
| 696 | + $emailAddress = $user->getEMailAddress(); | |
| 697 | +                if ($emailAddress !== null && $emailAddress !== '') { | |
| 698 | + $userLang = $this->config->getUserValue($share->getSharedWith(), 'core', 'lang', null); | |
| 699 | +                    $l = $this->l10nFactory->get('lib', $userLang); | |
| 700 | + $this->sendMailNotification( | |
| 701 | + $l, | |
| 702 | + $share->getNode()->getName(), | |
| 703 | +                        $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', [ 'fileid' => $share->getNode()->getId() ]), | |
| 704 | + $share->getSharedBy(), | |
| 705 | + $emailAddress, | |
| 706 | + $share->getExpirationDate() | |
| 707 | + ); | |
| 708 | +                    $this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']); | |
| 709 | +                } else { | |
| 710 | +                    $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']); | |
| 711 | + } | |
| 712 | +            } else { | |
| 713 | +                $this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']); | |
| 714 | + } | |
| 715 | + } | |
| 716 | + | |
| 717 | + return $share; | |
| 718 | + } | |
| 719 | + | |
| 720 | + /** | |
| 721 | + * @param IL10N $l Language of the recipient | |
| 722 | + * @param string $filename file/folder name | |
| 723 | + * @param string $link link to the file/folder | |
| 724 | + * @param string $initiator user ID of share sender | |
| 725 | + * @param string $shareWith email address of share receiver | |
| 726 | + * @param \DateTime|null $expiration | |
| 727 | + * @throws \Exception If mail couldn't be sent | |
| 728 | + */ | |
| 729 | + protected function sendMailNotification(IL10N $l, | |
| 730 | + $filename, | |
| 731 | + $link, | |
| 732 | + $initiator, | |
| 733 | + $shareWith, | |
| 734 | +                                            \DateTime $expiration = null) { | |
| 735 | + $initiatorUser = $this->userManager->get($initiator); | |
| 736 | + $initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator; | |
| 737 | +        $subject = $l->t('%s shared »%s« with you', array($initiatorDisplayName, $filename)); | |
| 738 | + | |
| 739 | + $message = $this->mailer->createMessage(); | |
| 740 | + | |
| 741 | +        $emailTemplate = $this->mailer->createEMailTemplate('files_sharing.RecipientNotification', [ | |
| 742 | + 'filename' => $filename, | |
| 743 | + 'link' => $link, | |
| 744 | + 'initiator' => $initiatorDisplayName, | |
| 745 | + 'expiration' => $expiration, | |
| 746 | + 'shareWith' => $shareWith, | |
| 747 | + ]); | |
| 748 | + | |
| 749 | + $emailTemplate->addHeader(); | |
| 750 | +        $emailTemplate->addHeading($l->t('%s shared »%s« with you', [$initiatorDisplayName, $filename]), false); | |
| 751 | +        $text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); | |
| 752 | + | |
| 753 | + $emailTemplate->addBodyText( | |
| 754 | +            $text . ' ' . $l->t('Click the button below to open it.'), | |
| 755 | + $text | |
| 756 | + ); | |
| 757 | + $emailTemplate->addBodyButton( | |
| 758 | +            $l->t('Open »%s«', [$filename]), | |
| 759 | + $link | |
| 760 | + ); | |
| 761 | + | |
| 762 | + $message->setTo([$shareWith]); | |
| 763 | + | |
| 764 | + // The "From" contains the sharers name | |
| 765 | + $instanceName = $this->defaults->getName(); | |
| 766 | + $senderName = $l->t( | |
| 767 | + '%s via %s', | |
| 768 | + [ | |
| 769 | + $initiatorDisplayName, | |
| 770 | + $instanceName | |
| 771 | + ] | |
| 772 | + ); | |
| 773 | + $message->setFrom([\OCP\Util::getDefaultEmailAddress($instanceName) => $senderName]); | |
| 774 | + | |
| 775 | + // The "Reply-To" is set to the sharer if an mail address is configured | |
| 776 | + // also the default footer contains a "Do not reply" which needs to be adjusted. | |
| 777 | + $initiatorEmail = $initiatorUser->getEMailAddress(); | |
| 778 | +        if($initiatorEmail !== null) { | |
| 779 | + $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); | |
| 780 | + $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); | |
| 781 | +        } else { | |
| 782 | + $emailTemplate->addFooter(); | |
| 783 | + } | |
| 784 | + | |
| 785 | + $message->setSubject($subject); | |
| 786 | + $message->setPlainBody($emailTemplate->renderText()); | |
| 787 | + $message->setHtmlBody($emailTemplate->renderHtml()); | |
| 788 | + $this->mailer->send($message); | |
| 789 | + } | |
| 790 | + | |
| 791 | + /** | |
| 792 | + * Update a share | |
| 793 | + * | |
| 794 | + * @param \OCP\Share\IShare $share | |
| 795 | + * @return \OCP\Share\IShare The share object | |
| 796 | + * @throws \InvalidArgumentException | |
| 797 | + */ | |
| 798 | +    public function updateShare(\OCP\Share\IShare $share) { | |
| 799 | + $expirationDateUpdated = false; | |
| 800 | + | |
| 801 | + $this->canShare($share); | |
| 802 | + | |
| 803 | +        try { | |
| 804 | + $originalShare = $this->getShareById($share->getFullId()); | |
| 805 | +        } catch (\UnexpectedValueException $e) { | |
| 806 | +            throw new \InvalidArgumentException('Share does not have a full id'); | |
| 807 | + } | |
| 808 | + | |
| 809 | + // We can't change the share type! | |
| 810 | +        if ($share->getShareType() !== $originalShare->getShareType()) { | |
| 811 | +            throw new \InvalidArgumentException('Can\'t change share type'); | |
| 812 | + } | |
| 813 | + | |
| 814 | + // We can only change the recipient on user shares | |
| 815 | + if ($share->getSharedWith() !== $originalShare->getSharedWith() && | |
| 816 | +            $share->getShareType() !== \OCP\Share::SHARE_TYPE_USER) { | |
| 817 | +            throw new \InvalidArgumentException('Can only update recipient on user shares'); | |
| 818 | + } | |
| 819 | + | |
| 820 | + // Cannot share with the owner | |
| 821 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && | |
| 822 | +            $share->getSharedWith() === $share->getShareOwner()) { | |
| 823 | +            throw new \InvalidArgumentException('Can\'t share with the share owner'); | |
| 824 | + } | |
| 825 | + | |
| 826 | + $this->generalCreateChecks($share); | |
| 827 | + | |
| 828 | +        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER) { | |
| 829 | + $this->userCreateChecks($share); | |
| 830 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 831 | + $this->groupCreateChecks($share); | |
| 832 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { | |
| 833 | + $this->linkCreateChecks($share); | |
| 834 | + | |
| 835 | + $this->updateSharePasswordIfNeeded($share, $originalShare); | |
| 836 | + | |
| 837 | +            if ($share->getExpirationDate() != $originalShare->getExpirationDate()) { | |
| 838 | + //Verify the expiration date | |
| 839 | + $this->validateExpirationDate($share); | |
| 840 | + $expirationDateUpdated = true; | |
| 841 | + } | |
| 842 | +        } else if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { | |
| 843 | + $plainTextPassword = $share->getPassword(); | |
| 844 | +            if (!$this->updateSharePasswordIfNeeded($share, $originalShare)) { | |
| 845 | + $plainTextPassword = null; | |
| 846 | + } | |
| 847 | + } | |
| 848 | + | |
| 849 | + $this->pathCreateChecks($share->getNode()); | |
| 850 | + | |
| 851 | + // Now update the share! | |
| 852 | + $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 853 | +        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { | |
| 854 | + $share = $provider->update($share, $plainTextPassword); | |
| 855 | +        } else { | |
| 856 | + $share = $provider->update($share); | |
| 857 | + } | |
| 858 | + | |
| 859 | +        if ($expirationDateUpdated === true) { | |
| 860 | +            \OC_Hook::emit('OCP\Share', 'post_set_expiration_date', [ | |
| 861 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 862 | + 'itemSource' => $share->getNode()->getId(), | |
| 863 | + 'date' => $share->getExpirationDate(), | |
| 864 | + 'uidOwner' => $share->getSharedBy(), | |
| 865 | + ]); | |
| 866 | + } | |
| 867 | + | |
| 868 | +        if ($share->getPassword() !== $originalShare->getPassword()) { | |
| 869 | +            \OC_Hook::emit('OCP\Share', 'post_update_password', [ | |
| 870 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 871 | + 'itemSource' => $share->getNode()->getId(), | |
| 872 | + 'uidOwner' => $share->getSharedBy(), | |
| 873 | + 'token' => $share->getToken(), | |
| 874 | + 'disabled' => is_null($share->getPassword()), | |
| 875 | + ]); | |
| 876 | + } | |
| 877 | + | |
| 878 | +        if ($share->getPermissions() !== $originalShare->getPermissions()) { | |
| 879 | +            if ($this->userManager->userExists($share->getShareOwner())) { | |
| 880 | + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); | |
| 881 | +            } else { | |
| 882 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); | |
| 883 | + } | |
| 884 | +            \OC_Hook::emit('OCP\Share', 'post_update_permissions', array( | |
| 885 | + 'itemType' => $share->getNode() instanceof \OCP\Files\File ? 'file' : 'folder', | |
| 886 | + 'itemSource' => $share->getNode()->getId(), | |
| 887 | + 'shareType' => $share->getShareType(), | |
| 888 | + 'shareWith' => $share->getSharedWith(), | |
| 889 | + 'uidOwner' => $share->getSharedBy(), | |
| 890 | + 'permissions' => $share->getPermissions(), | |
| 891 | + 'path' => $userFolder->getRelativePath($share->getNode()->getPath()), | |
| 892 | + )); | |
| 893 | + } | |
| 894 | + | |
| 895 | + return $share; | |
| 896 | + } | |
| 897 | + | |
| 898 | + /** | |
| 899 | + * Updates the password of the given share if it is not the same as the | |
| 900 | + * password of the original share. | |
| 901 | + * | |
| 902 | + * @param \OCP\Share\IShare $share the share to update its password. | |
| 903 | + * @param \OCP\Share\IShare $originalShare the original share to compare its | |
| 904 | + * password with. | |
| 905 | + * @return boolean whether the password was updated or not. | |
| 906 | + */ | |
| 907 | +    private function updateSharePasswordIfNeeded(\OCP\Share\IShare $share, \OCP\Share\IShare $originalShare) { | |
| 908 | + // Password updated. | |
| 909 | +        if ($share->getPassword() !== $originalShare->getPassword()) { | |
| 910 | + //Verify the password | |
| 911 | + $this->verifyPassword($share->getPassword()); | |
| 912 | + | |
| 913 | + // If a password is set. Hash it! | |
| 914 | +            if ($share->getPassword() !== null) { | |
| 915 | + $share->setPassword($this->hasher->hash($share->getPassword())); | |
| 916 | + | |
| 917 | + return true; | |
| 918 | + } | |
| 919 | + } | |
| 920 | + | |
| 921 | + return false; | |
| 922 | + } | |
| 923 | + | |
| 924 | + /** | |
| 925 | + * Delete all the children of this share | |
| 926 | + * FIXME: remove once https://github.com/owncloud/core/pull/21660 is in | |
| 927 | + * | |
| 928 | + * @param \OCP\Share\IShare $share | |
| 929 | + * @return \OCP\Share\IShare[] List of deleted shares | |
| 930 | + */ | |
| 931 | +    protected function deleteChildren(\OCP\Share\IShare $share) { | |
| 932 | + $deletedShares = []; | |
| 933 | + | |
| 934 | + $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 935 | + | |
| 936 | +        foreach ($provider->getChildren($share) as $child) { | |
| 937 | + $deletedChildren = $this->deleteChildren($child); | |
| 938 | + $deletedShares = array_merge($deletedShares, $deletedChildren); | |
| 939 | + | |
| 940 | + $provider->delete($child); | |
| 941 | + $deletedShares[] = $child; | |
| 942 | + } | |
| 943 | + | |
| 944 | + return $deletedShares; | |
| 945 | + } | |
| 946 | + | |
| 947 | + /** | |
| 948 | + * Delete a share | |
| 949 | + * | |
| 950 | + * @param \OCP\Share\IShare $share | |
| 951 | + * @throws ShareNotFound | |
| 952 | + * @throws \InvalidArgumentException | |
| 953 | + */ | |
| 954 | +    public function deleteShare(\OCP\Share\IShare $share) { | |
| 955 | + | |
| 956 | +        try { | |
| 957 | + $share->getFullId(); | |
| 958 | +        } catch (\UnexpectedValueException $e) { | |
| 959 | +            throw new \InvalidArgumentException('Share does not have a full id'); | |
| 960 | + } | |
| 961 | + | |
| 962 | + $event = new GenericEvent($share); | |
| 963 | +        $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event); | |
| 964 | + | |
| 965 | + // Get all children and delete them as well | |
| 966 | + $deletedShares = $this->deleteChildren($share); | |
| 967 | + | |
| 968 | + // Do the actual delete | |
| 969 | + $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 970 | + $provider->delete($share); | |
| 971 | + | |
| 972 | + // All the deleted shares caused by this delete | |
| 973 | + $deletedShares[] = $share; | |
| 974 | + | |
| 975 | + // Emit post hook | |
| 976 | +        $event->setArgument('deletedShares', $deletedShares); | |
| 977 | +        $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event); | |
| 978 | + } | |
| 979 | + | |
| 980 | + | |
| 981 | + /** | |
| 982 | + * Unshare a file as the recipient. | |
| 983 | + * This can be different from a regular delete for example when one of | |
| 984 | + * the users in a groups deletes that share. But the provider should | |
| 985 | + * handle this. | |
| 986 | + * | |
| 987 | + * @param \OCP\Share\IShare $share | |
| 988 | + * @param string $recipientId | |
| 989 | + */ | |
| 990 | +    public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { | |
| 991 | + list($providerId, ) = $this->splitFullId($share->getFullId()); | |
| 992 | + $provider = $this->factory->getProvider($providerId); | |
| 993 | + | |
| 994 | + $provider->deleteFromSelf($share, $recipientId); | |
| 995 | + } | |
| 996 | + | |
| 997 | + /** | |
| 998 | + * @inheritdoc | |
| 999 | + */ | |
| 1000 | +    public function moveShare(\OCP\Share\IShare $share, $recipientId) { | |
| 1001 | +        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK) { | |
| 1002 | +            throw new \InvalidArgumentException('Can\'t change target of link share'); | |
| 1003 | + } | |
| 1004 | + | |
| 1005 | +        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_USER && $share->getSharedWith() !== $recipientId) { | |
| 1006 | +            throw new \InvalidArgumentException('Invalid recipient'); | |
| 1007 | + } | |
| 1008 | + | |
| 1009 | +        if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | |
| 1010 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); | |
| 1011 | +            if (is_null($sharedWith)) { | |
| 1012 | +                throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); | |
| 1013 | + } | |
| 1014 | + $recipient = $this->userManager->get($recipientId); | |
| 1015 | +            if (!$sharedWith->inGroup($recipient)) { | |
| 1016 | +                throw new \InvalidArgumentException('Invalid recipient'); | |
| 1017 | + } | |
| 1018 | + } | |
| 1019 | + | |
| 1020 | + list($providerId, ) = $this->splitFullId($share->getFullId()); | |
| 1021 | + $provider = $this->factory->getProvider($providerId); | |
| 1022 | + | |
| 1023 | + $provider->move($share, $recipientId); | |
| 1024 | + } | |
| 1025 | + | |
| 1026 | +    public function getSharesInFolder($userId, Folder $node, $reshares = false) { | |
| 1027 | + $providers = $this->factory->getAllProviders(); | |
| 1028 | + | |
| 1029 | +        return array_reduce($providers, function($shares, IShareProvider $provider) use ($userId, $node, $reshares) { | |
| 1030 | + $newShares = $provider->getSharesInFolder($userId, $node, $reshares); | |
| 1031 | +            foreach ($newShares as $fid => $data) { | |
| 1032 | +                if (!isset($shares[$fid])) { | |
| 1033 | + $shares[$fid] = []; | |
| 1034 | + } | |
| 1035 | + | |
| 1036 | + $shares[$fid] = array_merge($shares[$fid], $data); | |
| 1037 | + } | |
| 1038 | + return $shares; | |
| 1039 | + }, []); | |
| 1040 | + } | |
| 1041 | + | |
| 1042 | + /** | |
| 1043 | + * @inheritdoc | |
| 1044 | + */ | |
| 1045 | +    public function getSharesBy($userId, $shareType, $path = null, $reshares = false, $limit = 50, $offset = 0) { | |
| 1046 | + if ($path !== null && | |
| 1047 | + !($path instanceof \OCP\Files\File) && | |
| 1048 | +                !($path instanceof \OCP\Files\Folder)) { | |
| 1049 | +            throw new \InvalidArgumentException('invalid path'); | |
| 1050 | + } | |
| 1051 | + | |
| 1052 | +        try { | |
| 1053 | + $provider = $this->factory->getProviderForType($shareType); | |
| 1054 | +        } catch (ProviderException $e) { | |
| 1055 | + return []; | |
| 1056 | + } | |
| 1057 | + | |
| 1058 | + $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); | |
| 1059 | + | |
| 1060 | + /* | |
| 1061 | 1061 | * Work around so we don't return expired shares but still follow | 
| 1062 | 1062 | * proper pagination. | 
| 1063 | 1063 | */ | 
| 1064 | 1064 | |
| 1065 | - $shares2 = []; | |
| 1066 | - | |
| 1067 | -		while(true) { | |
| 1068 | - $added = 0; | |
| 1069 | -			foreach ($shares as $share) { | |
| 1070 | - | |
| 1071 | -				try { | |
| 1072 | - $this->checkExpireDate($share); | |
| 1073 | -				} catch (ShareNotFound $e) { | |
| 1074 | - //Ignore since this basically means the share is deleted | |
| 1075 | - continue; | |
| 1076 | - } | |
| 1077 | - | |
| 1078 | - $added++; | |
| 1079 | - $shares2[] = $share; | |
| 1080 | - | |
| 1081 | -				if (count($shares2) === $limit) { | |
| 1082 | - break; | |
| 1083 | - } | |
| 1084 | - } | |
| 1085 | - | |
| 1086 | -			if (count($shares2) === $limit) { | |
| 1087 | - break; | |
| 1088 | - } | |
| 1089 | - | |
| 1090 | - // If there was no limit on the select we are done | |
| 1091 | -			if ($limit === -1) { | |
| 1092 | - break; | |
| 1093 | - } | |
| 1094 | - | |
| 1095 | - $offset += $added; | |
| 1096 | - | |
| 1097 | - // Fetch again $limit shares | |
| 1098 | - $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); | |
| 1099 | - | |
| 1100 | - // No more shares means we are done | |
| 1101 | -			if (empty($shares)) { | |
| 1102 | - break; | |
| 1103 | - } | |
| 1104 | - } | |
| 1105 | - | |
| 1106 | - $shares = $shares2; | |
| 1107 | - | |
| 1108 | - return $shares; | |
| 1109 | - } | |
| 1110 | - | |
| 1111 | - /** | |
| 1112 | - * @inheritdoc | |
| 1113 | - */ | |
| 1114 | -	public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { | |
| 1115 | -		try { | |
| 1116 | - $provider = $this->factory->getProviderForType($shareType); | |
| 1117 | -		} catch (ProviderException $e) { | |
| 1118 | - return []; | |
| 1119 | - } | |
| 1120 | - | |
| 1121 | - $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset); | |
| 1122 | - | |
| 1123 | - // remove all shares which are already expired | |
| 1124 | -		foreach ($shares as $key => $share) { | |
| 1125 | -			try { | |
| 1126 | - $this->checkExpireDate($share); | |
| 1127 | -			} catch (ShareNotFound $e) { | |
| 1128 | - unset($shares[$key]); | |
| 1129 | - } | |
| 1130 | - } | |
| 1131 | - | |
| 1132 | - return $shares; | |
| 1133 | - } | |
| 1134 | - | |
| 1135 | - /** | |
| 1136 | - * @inheritdoc | |
| 1137 | - */ | |
| 1138 | -	public function getShareById($id, $recipient = null) { | |
| 1139 | -		if ($id === null) { | |
| 1140 | - throw new ShareNotFound(); | |
| 1141 | - } | |
| 1142 | - | |
| 1143 | - list($providerId, $id) = $this->splitFullId($id); | |
| 1144 | - | |
| 1145 | -		try { | |
| 1146 | - $provider = $this->factory->getProvider($providerId); | |
| 1147 | -		} catch (ProviderException $e) { | |
| 1148 | - throw new ShareNotFound(); | |
| 1149 | - } | |
| 1150 | - | |
| 1151 | - $share = $provider->getShareById($id, $recipient); | |
| 1152 | - | |
| 1153 | - $this->checkExpireDate($share); | |
| 1154 | - | |
| 1155 | - return $share; | |
| 1156 | - } | |
| 1157 | - | |
| 1158 | - /** | |
| 1159 | - * Get all the shares for a given path | |
| 1160 | - * | |
| 1161 | - * @param \OCP\Files\Node $path | |
| 1162 | - * @param int $page | |
| 1163 | - * @param int $perPage | |
| 1164 | - * | |
| 1165 | - * @return Share[] | |
| 1166 | - */ | |
| 1167 | -	public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { | |
| 1168 | - return []; | |
| 1169 | - } | |
| 1170 | - | |
| 1171 | - /** | |
| 1172 | - * Get the share by token possible with password | |
| 1173 | - * | |
| 1174 | - * @param string $token | |
| 1175 | - * @return Share | |
| 1176 | - * | |
| 1177 | - * @throws ShareNotFound | |
| 1178 | - */ | |
| 1179 | -	public function getShareByToken($token) { | |
| 1180 | - // tokens can't be valid local user names | |
| 1181 | -		if ($this->userManager->userExists($token)) { | |
| 1182 | - throw new ShareNotFound(); | |
| 1183 | - } | |
| 1184 | - $share = null; | |
| 1185 | -		try { | |
| 1186 | -			if($this->shareApiAllowLinks()) { | |
| 1187 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); | |
| 1188 | - $share = $provider->getShareByToken($token); | |
| 1189 | - } | |
| 1190 | -		} catch (ProviderException $e) { | |
| 1191 | -		} catch (ShareNotFound $e) { | |
| 1192 | - } | |
| 1193 | - | |
| 1194 | - | |
| 1195 | - // If it is not a link share try to fetch a federated share by token | |
| 1196 | -		if ($share === null) { | |
| 1197 | -			try { | |
| 1198 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE); | |
| 1199 | - $share = $provider->getShareByToken($token); | |
| 1200 | -			} catch (ProviderException $e) { | |
| 1201 | -			} catch (ShareNotFound $e) { | |
| 1202 | - } | |
| 1203 | - } | |
| 1204 | - | |
| 1205 | - // If it is not a link share try to fetch a mail share by token | |
| 1206 | -		if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) { | |
| 1207 | -			try { | |
| 1208 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL); | |
| 1209 | - $share = $provider->getShareByToken($token); | |
| 1210 | -			} catch (ProviderException $e) { | |
| 1211 | -			} catch (ShareNotFound $e) { | |
| 1212 | - } | |
| 1213 | - } | |
| 1214 | - | |
| 1215 | -		if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) { | |
| 1216 | -			try { | |
| 1217 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE); | |
| 1218 | - $share = $provider->getShareByToken($token); | |
| 1219 | -			} catch (ProviderException $e) { | |
| 1220 | -			} catch (ShareNotFound $e) { | |
| 1221 | - } | |
| 1222 | - } | |
| 1223 | - | |
| 1224 | -		if ($share === null) { | |
| 1225 | -			throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); | |
| 1226 | - } | |
| 1227 | - | |
| 1228 | - $this->checkExpireDate($share); | |
| 1229 | - | |
| 1230 | - /* | |
| 1065 | + $shares2 = []; | |
| 1066 | + | |
| 1067 | +        while(true) { | |
| 1068 | + $added = 0; | |
| 1069 | +            foreach ($shares as $share) { | |
| 1070 | + | |
| 1071 | +                try { | |
| 1072 | + $this->checkExpireDate($share); | |
| 1073 | +                } catch (ShareNotFound $e) { | |
| 1074 | + //Ignore since this basically means the share is deleted | |
| 1075 | + continue; | |
| 1076 | + } | |
| 1077 | + | |
| 1078 | + $added++; | |
| 1079 | + $shares2[] = $share; | |
| 1080 | + | |
| 1081 | +                if (count($shares2) === $limit) { | |
| 1082 | + break; | |
| 1083 | + } | |
| 1084 | + } | |
| 1085 | + | |
| 1086 | +            if (count($shares2) === $limit) { | |
| 1087 | + break; | |
| 1088 | + } | |
| 1089 | + | |
| 1090 | + // If there was no limit on the select we are done | |
| 1091 | +            if ($limit === -1) { | |
| 1092 | + break; | |
| 1093 | + } | |
| 1094 | + | |
| 1095 | + $offset += $added; | |
| 1096 | + | |
| 1097 | + // Fetch again $limit shares | |
| 1098 | + $shares = $provider->getSharesBy($userId, $shareType, $path, $reshares, $limit, $offset); | |
| 1099 | + | |
| 1100 | + // No more shares means we are done | |
| 1101 | +            if (empty($shares)) { | |
| 1102 | + break; | |
| 1103 | + } | |
| 1104 | + } | |
| 1105 | + | |
| 1106 | + $shares = $shares2; | |
| 1107 | + | |
| 1108 | + return $shares; | |
| 1109 | + } | |
| 1110 | + | |
| 1111 | + /** | |
| 1112 | + * @inheritdoc | |
| 1113 | + */ | |
| 1114 | +    public function getSharedWith($userId, $shareType, $node = null, $limit = 50, $offset = 0) { | |
| 1115 | +        try { | |
| 1116 | + $provider = $this->factory->getProviderForType($shareType); | |
| 1117 | +        } catch (ProviderException $e) { | |
| 1118 | + return []; | |
| 1119 | + } | |
| 1120 | + | |
| 1121 | + $shares = $provider->getSharedWith($userId, $shareType, $node, $limit, $offset); | |
| 1122 | + | |
| 1123 | + // remove all shares which are already expired | |
| 1124 | +        foreach ($shares as $key => $share) { | |
| 1125 | +            try { | |
| 1126 | + $this->checkExpireDate($share); | |
| 1127 | +            } catch (ShareNotFound $e) { | |
| 1128 | + unset($shares[$key]); | |
| 1129 | + } | |
| 1130 | + } | |
| 1131 | + | |
| 1132 | + return $shares; | |
| 1133 | + } | |
| 1134 | + | |
| 1135 | + /** | |
| 1136 | + * @inheritdoc | |
| 1137 | + */ | |
| 1138 | +    public function getShareById($id, $recipient = null) { | |
| 1139 | +        if ($id === null) { | |
| 1140 | + throw new ShareNotFound(); | |
| 1141 | + } | |
| 1142 | + | |
| 1143 | + list($providerId, $id) = $this->splitFullId($id); | |
| 1144 | + | |
| 1145 | +        try { | |
| 1146 | + $provider = $this->factory->getProvider($providerId); | |
| 1147 | +        } catch (ProviderException $e) { | |
| 1148 | + throw new ShareNotFound(); | |
| 1149 | + } | |
| 1150 | + | |
| 1151 | + $share = $provider->getShareById($id, $recipient); | |
| 1152 | + | |
| 1153 | + $this->checkExpireDate($share); | |
| 1154 | + | |
| 1155 | + return $share; | |
| 1156 | + } | |
| 1157 | + | |
| 1158 | + /** | |
| 1159 | + * Get all the shares for a given path | |
| 1160 | + * | |
| 1161 | + * @param \OCP\Files\Node $path | |
| 1162 | + * @param int $page | |
| 1163 | + * @param int $perPage | |
| 1164 | + * | |
| 1165 | + * @return Share[] | |
| 1166 | + */ | |
| 1167 | +    public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { | |
| 1168 | + return []; | |
| 1169 | + } | |
| 1170 | + | |
| 1171 | + /** | |
| 1172 | + * Get the share by token possible with password | |
| 1173 | + * | |
| 1174 | + * @param string $token | |
| 1175 | + * @return Share | |
| 1176 | + * | |
| 1177 | + * @throws ShareNotFound | |
| 1178 | + */ | |
| 1179 | +    public function getShareByToken($token) { | |
| 1180 | + // tokens can't be valid local user names | |
| 1181 | +        if ($this->userManager->userExists($token)) { | |
| 1182 | + throw new ShareNotFound(); | |
| 1183 | + } | |
| 1184 | + $share = null; | |
| 1185 | +        try { | |
| 1186 | +            if($this->shareApiAllowLinks()) { | |
| 1187 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); | |
| 1188 | + $share = $provider->getShareByToken($token); | |
| 1189 | + } | |
| 1190 | +        } catch (ProviderException $e) { | |
| 1191 | +        } catch (ShareNotFound $e) { | |
| 1192 | + } | |
| 1193 | + | |
| 1194 | + | |
| 1195 | + // If it is not a link share try to fetch a federated share by token | |
| 1196 | +        if ($share === null) { | |
| 1197 | +            try { | |
| 1198 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_REMOTE); | |
| 1199 | + $share = $provider->getShareByToken($token); | |
| 1200 | +            } catch (ProviderException $e) { | |
| 1201 | +            } catch (ShareNotFound $e) { | |
| 1202 | + } | |
| 1203 | + } | |
| 1204 | + | |
| 1205 | + // If it is not a link share try to fetch a mail share by token | |
| 1206 | +        if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_EMAIL)) { | |
| 1207 | +            try { | |
| 1208 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_EMAIL); | |
| 1209 | + $share = $provider->getShareByToken($token); | |
| 1210 | +            } catch (ProviderException $e) { | |
| 1211 | +            } catch (ShareNotFound $e) { | |
| 1212 | + } | |
| 1213 | + } | |
| 1214 | + | |
| 1215 | +        if ($share === null && $this->shareProviderExists(\OCP\Share::SHARE_TYPE_CIRCLE)) { | |
| 1216 | +            try { | |
| 1217 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_CIRCLE); | |
| 1218 | + $share = $provider->getShareByToken($token); | |
| 1219 | +            } catch (ProviderException $e) { | |
| 1220 | +            } catch (ShareNotFound $e) { | |
| 1221 | + } | |
| 1222 | + } | |
| 1223 | + | |
| 1224 | +        if ($share === null) { | |
| 1225 | +            throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); | |
| 1226 | + } | |
| 1227 | + | |
| 1228 | + $this->checkExpireDate($share); | |
| 1229 | + | |
| 1230 | + /* | |
| 1231 | 1231 | * Reduce the permissions for link shares if public upload is not enabled | 
| 1232 | 1232 | */ | 
| 1233 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && | |
| 1234 | -			!$this->shareApiLinkAllowPublicUpload()) { | |
| 1235 | - $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)); | |
| 1236 | - } | |
| 1237 | - | |
| 1238 | - return $share; | |
| 1239 | - } | |
| 1240 | - | |
| 1241 | -	protected function checkExpireDate($share) { | |
| 1242 | - if ($share->getExpirationDate() !== null && | |
| 1243 | -			$share->getExpirationDate() <= new \DateTime()) { | |
| 1244 | - $this->deleteShare($share); | |
| 1245 | -			throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); | |
| 1246 | - } | |
| 1247 | - | |
| 1248 | - } | |
| 1249 | - | |
| 1250 | - /** | |
| 1251 | - * Verify the password of a public share | |
| 1252 | - * | |
| 1253 | - * @param \OCP\Share\IShare $share | |
| 1254 | - * @param string $password | |
| 1255 | - * @return bool | |
| 1256 | - */ | |
| 1257 | -	public function checkPassword(\OCP\Share\IShare $share, $password) { | |
| 1258 | - $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK | |
| 1259 | - || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL; | |
| 1260 | -		if (!$passwordProtected) { | |
| 1261 | - //TODO maybe exception? | |
| 1262 | - return false; | |
| 1263 | - } | |
| 1264 | - | |
| 1265 | -		if ($password === null || $share->getPassword() === null) { | |
| 1266 | - return false; | |
| 1267 | - } | |
| 1268 | - | |
| 1269 | - $newHash = ''; | |
| 1270 | -		if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { | |
| 1271 | - return false; | |
| 1272 | - } | |
| 1273 | - | |
| 1274 | -		if (!empty($newHash)) { | |
| 1275 | - $share->setPassword($newHash); | |
| 1276 | - $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 1277 | - $provider->update($share); | |
| 1278 | - } | |
| 1279 | - | |
| 1280 | - return true; | |
| 1281 | - } | |
| 1282 | - | |
| 1283 | - /** | |
| 1284 | - * @inheritdoc | |
| 1285 | - */ | |
| 1286 | -	public function userDeleted($uid) { | |
| 1287 | - $types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL]; | |
| 1288 | - | |
| 1289 | -		foreach ($types as $type) { | |
| 1290 | -			try { | |
| 1291 | - $provider = $this->factory->getProviderForType($type); | |
| 1292 | -			} catch (ProviderException $e) { | |
| 1293 | - continue; | |
| 1294 | - } | |
| 1295 | - $provider->userDeleted($uid, $type); | |
| 1296 | - } | |
| 1297 | - } | |
| 1298 | - | |
| 1299 | - /** | |
| 1300 | - * @inheritdoc | |
| 1301 | - */ | |
| 1302 | -	public function groupDeleted($gid) { | |
| 1303 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); | |
| 1304 | - $provider->groupDeleted($gid); | |
| 1305 | - } | |
| 1306 | - | |
| 1307 | - /** | |
| 1308 | - * @inheritdoc | |
| 1309 | - */ | |
| 1310 | -	public function userDeletedFromGroup($uid, $gid) { | |
| 1311 | - $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); | |
| 1312 | - $provider->userDeletedFromGroup($uid, $gid); | |
| 1313 | - } | |
| 1314 | - | |
| 1315 | - /** | |
| 1316 | - * Get access list to a path. This means | |
| 1317 | - * all the users that can access a given path. | |
| 1318 | - * | |
| 1319 | - * Consider: | |
| 1320 | - * -root | |
| 1321 | - * |-folder1 (23) | |
| 1322 | - * |-folder2 (32) | |
| 1323 | - * |-fileA (42) | |
| 1324 | - * | |
| 1325 | - * fileA is shared with user1 and user1@server1 | |
| 1326 | - * folder2 is shared with group2 (user4 is a member of group2) | |
| 1327 | - * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2 | |
| 1328 | - * | |
| 1329 | - * Then the access list to '/folder1/folder2/fileA' with $currentAccess is: | |
| 1330 | - * [ | |
| 1331 | - * users => [ | |
| 1332 | - * 'user1' => ['node_id' => 42, 'node_path' => '/fileA'], | |
| 1333 | - * 'user4' => ['node_id' => 32, 'node_path' => '/folder2'], | |
| 1334 | - * 'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'], | |
| 1335 | - * ], | |
| 1336 | - * remote => [ | |
| 1337 | - * 'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'], | |
| 1338 | - * 'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'], | |
| 1339 | - * ], | |
| 1340 | - * public => bool | |
| 1341 | - * mail => bool | |
| 1342 | - * ] | |
| 1343 | - * | |
| 1344 | - * The access list to '/folder1/folder2/fileA' **without** $currentAccess is: | |
| 1345 | - * [ | |
| 1346 | - * users => ['user1', 'user2', 'user4'], | |
| 1347 | - * remote => bool, | |
| 1348 | - * public => bool | |
| 1349 | - * mail => bool | |
| 1350 | - * ] | |
| 1351 | - * | |
| 1352 | - * This is required for encryption/activity | |
| 1353 | - * | |
| 1354 | - * @param \OCP\Files\Node $path | |
| 1355 | - * @param bool $recursive Should we check all parent folders as well | |
| 1356 | - * @param bool $currentAccess Should the user have currently access to the file | |
| 1357 | - * @return array | |
| 1358 | - */ | |
| 1359 | -	public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) { | |
| 1360 | - $owner = $path->getOwner()->getUID(); | |
| 1361 | - | |
| 1362 | -		if ($currentAccess) { | |
| 1363 | - $al = ['users' => [], 'remote' => [], 'public' => false]; | |
| 1364 | -		} else { | |
| 1365 | - $al = ['users' => [], 'remote' => false, 'public' => false]; | |
| 1366 | - } | |
| 1367 | -		if (!$this->userManager->userExists($owner)) { | |
| 1368 | - return $al; | |
| 1369 | - } | |
| 1370 | - | |
| 1371 | - //Get node for the owner | |
| 1372 | - $userFolder = $this->rootFolder->getUserFolder($owner); | |
| 1373 | -		if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { | |
| 1374 | - $path = $userFolder->getById($path->getId())[0]; | |
| 1375 | - } | |
| 1376 | - | |
| 1377 | - $providers = $this->factory->getAllProviders(); | |
| 1378 | - | |
| 1379 | - /** @var Node[] $nodes */ | |
| 1380 | - $nodes = []; | |
| 1381 | - | |
| 1382 | - | |
| 1383 | -		if ($currentAccess) { | |
| 1384 | - $ownerPath = $path->getPath(); | |
| 1385 | -			$ownerPath = explode('/', $ownerPath, 4); | |
| 1386 | -			if (count($ownerPath) < 4) { | |
| 1387 | - $ownerPath = ''; | |
| 1388 | -			} else { | |
| 1389 | - $ownerPath = $ownerPath[3]; | |
| 1390 | - } | |
| 1391 | - $al['users'][$owner] = [ | |
| 1392 | - 'node_id' => $path->getId(), | |
| 1393 | - 'node_path' => '/' . $ownerPath, | |
| 1394 | - ]; | |
| 1395 | -		} else { | |
| 1396 | - $al['users'][] = $owner; | |
| 1397 | - } | |
| 1398 | - | |
| 1399 | - // Collect all the shares | |
| 1400 | -		while ($path->getPath() !== $userFolder->getPath()) { | |
| 1401 | - $nodes[] = $path; | |
| 1402 | -			if (!$recursive) { | |
| 1403 | - break; | |
| 1404 | - } | |
| 1405 | - $path = $path->getParent(); | |
| 1406 | - } | |
| 1407 | - | |
| 1408 | -		foreach ($providers as $provider) { | |
| 1409 | - $tmp = $provider->getAccessList($nodes, $currentAccess); | |
| 1410 | - | |
| 1411 | -			foreach ($tmp as $k => $v) { | |
| 1412 | -				if (isset($al[$k])) { | |
| 1413 | -					if (is_array($al[$k])) { | |
| 1414 | -						if ($currentAccess) { | |
| 1415 | - $al[$k] += $v; | |
| 1416 | -						} else { | |
| 1417 | - $al[$k] = array_merge($al[$k], $v); | |
| 1418 | - $al[$k] = array_unique($al[$k]); | |
| 1419 | - $al[$k] = array_values($al[$k]); | |
| 1420 | - } | |
| 1421 | -					} else { | |
| 1422 | - $al[$k] = $al[$k] || $v; | |
| 1423 | - } | |
| 1424 | -				} else { | |
| 1425 | - $al[$k] = $v; | |
| 1426 | - } | |
| 1427 | - } | |
| 1428 | - } | |
| 1429 | - | |
| 1430 | - return $al; | |
| 1431 | - } | |
| 1432 | - | |
| 1433 | - /** | |
| 1434 | - * Create a new share | |
| 1435 | - * @return \OCP\Share\IShare; | |
| 1436 | - */ | |
| 1437 | -	public function newShare() { | |
| 1438 | - return new \OC\Share20\Share($this->rootFolder, $this->userManager); | |
| 1439 | - } | |
| 1440 | - | |
| 1441 | - /** | |
| 1442 | - * Is the share API enabled | |
| 1443 | - * | |
| 1444 | - * @return bool | |
| 1445 | - */ | |
| 1446 | -	public function shareApiEnabled() { | |
| 1447 | -		return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes'; | |
| 1448 | - } | |
| 1449 | - | |
| 1450 | - /** | |
| 1451 | - * Is public link sharing enabled | |
| 1452 | - * | |
| 1453 | - * @return bool | |
| 1454 | - */ | |
| 1455 | -	public function shareApiAllowLinks() { | |
| 1456 | -		return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes'; | |
| 1457 | - } | |
| 1458 | - | |
| 1459 | - /** | |
| 1460 | - * Is password on public link requires | |
| 1461 | - * | |
| 1462 | - * @return bool | |
| 1463 | - */ | |
| 1464 | -	public function shareApiLinkEnforcePassword() { | |
| 1465 | -		return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes'; | |
| 1466 | - } | |
| 1467 | - | |
| 1468 | - /** | |
| 1469 | - * Is default expire date enabled | |
| 1470 | - * | |
| 1471 | - * @return bool | |
| 1472 | - */ | |
| 1473 | -	public function shareApiLinkDefaultExpireDate() { | |
| 1474 | -		return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes'; | |
| 1475 | - } | |
| 1476 | - | |
| 1477 | - /** | |
| 1478 | - * Is default expire date enforced | |
| 1479 | - *` | |
| 1480 | - * @return bool | |
| 1481 | - */ | |
| 1482 | -	public function shareApiLinkDefaultExpireDateEnforced() { | |
| 1483 | - return $this->shareApiLinkDefaultExpireDate() && | |
| 1484 | -			$this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes'; | |
| 1485 | - } | |
| 1486 | - | |
| 1487 | - /** | |
| 1488 | - * Number of default expire days | |
| 1489 | - *shareApiLinkAllowPublicUpload | |
| 1490 | - * @return int | |
| 1491 | - */ | |
| 1492 | -	public function shareApiLinkDefaultExpireDays() { | |
| 1493 | -		return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); | |
| 1494 | - } | |
| 1495 | - | |
| 1496 | - /** | |
| 1497 | - * Allow public upload on link shares | |
| 1498 | - * | |
| 1499 | - * @return bool | |
| 1500 | - */ | |
| 1501 | -	public function shareApiLinkAllowPublicUpload() { | |
| 1502 | -		return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes'; | |
| 1503 | - } | |
| 1504 | - | |
| 1505 | - /** | |
| 1506 | - * check if user can only share with group members | |
| 1507 | - * @return bool | |
| 1508 | - */ | |
| 1509 | -	public function shareWithGroupMembersOnly() { | |
| 1510 | -		return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; | |
| 1511 | - } | |
| 1512 | - | |
| 1513 | - /** | |
| 1514 | - * Check if users can share with groups | |
| 1515 | - * @return bool | |
| 1516 | - */ | |
| 1517 | -	public function allowGroupSharing() { | |
| 1518 | -		return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; | |
| 1519 | - } | |
| 1520 | - | |
| 1521 | - /** | |
| 1522 | - * Copied from \OC_Util::isSharingDisabledForUser | |
| 1523 | - * | |
| 1524 | - * TODO: Deprecate fuction from OC_Util | |
| 1525 | - * | |
| 1526 | - * @param string $userId | |
| 1527 | - * @return bool | |
| 1528 | - */ | |
| 1529 | -	public function sharingDisabledForUser($userId) { | |
| 1530 | -		if ($userId === null) { | |
| 1531 | - return false; | |
| 1532 | - } | |
| 1533 | - | |
| 1534 | -		if (isset($this->sharingDisabledForUsersCache[$userId])) { | |
| 1535 | - return $this->sharingDisabledForUsersCache[$userId]; | |
| 1536 | - } | |
| 1537 | - | |
| 1538 | -		if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { | |
| 1539 | -			$groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); | |
| 1540 | - $excludedGroups = json_decode($groupsList); | |
| 1541 | -			if (is_null($excludedGroups)) { | |
| 1542 | -				$excludedGroups = explode(',', $groupsList); | |
| 1543 | - $newValue = json_encode($excludedGroups); | |
| 1544 | -				$this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); | |
| 1545 | - } | |
| 1546 | - $user = $this->userManager->get($userId); | |
| 1547 | - $usersGroups = $this->groupManager->getUserGroupIds($user); | |
| 1548 | -			if (!empty($usersGroups)) { | |
| 1549 | - $remainingGroups = array_diff($usersGroups, $excludedGroups); | |
| 1550 | - // if the user is only in groups which are disabled for sharing then | |
| 1551 | - // sharing is also disabled for the user | |
| 1552 | -				if (empty($remainingGroups)) { | |
| 1553 | - $this->sharingDisabledForUsersCache[$userId] = true; | |
| 1554 | - return true; | |
| 1555 | - } | |
| 1556 | - } | |
| 1557 | - } | |
| 1558 | - | |
| 1559 | - $this->sharingDisabledForUsersCache[$userId] = false; | |
| 1560 | - return false; | |
| 1561 | - } | |
| 1562 | - | |
| 1563 | - /** | |
| 1564 | - * @inheritdoc | |
| 1565 | - */ | |
| 1566 | -	public function outgoingServer2ServerSharesAllowed() { | |
| 1567 | -		return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes'; | |
| 1568 | - } | |
| 1569 | - | |
| 1570 | - /** | |
| 1571 | - * @inheritdoc | |
| 1572 | - */ | |
| 1573 | -	public function shareProviderExists($shareType) { | |
| 1574 | -		try { | |
| 1575 | - $this->factory->getProviderForType($shareType); | |
| 1576 | -		} catch (ProviderException $e) { | |
| 1577 | - return false; | |
| 1578 | - } | |
| 1579 | - | |
| 1580 | - return true; | |
| 1581 | - } | |
| 1233 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && | |
| 1234 | +            !$this->shareApiLinkAllowPublicUpload()) { | |
| 1235 | + $share->setPermissions($share->getPermissions() & ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_UPDATE)); | |
| 1236 | + } | |
| 1237 | + | |
| 1238 | + return $share; | |
| 1239 | + } | |
| 1240 | + | |
| 1241 | +    protected function checkExpireDate($share) { | |
| 1242 | + if ($share->getExpirationDate() !== null && | |
| 1243 | +            $share->getExpirationDate() <= new \DateTime()) { | |
| 1244 | + $this->deleteShare($share); | |
| 1245 | +            throw new ShareNotFound($this->l->t('The requested share does not exist anymore')); | |
| 1246 | + } | |
| 1247 | + | |
| 1248 | + } | |
| 1249 | + | |
| 1250 | + /** | |
| 1251 | + * Verify the password of a public share | |
| 1252 | + * | |
| 1253 | + * @param \OCP\Share\IShare $share | |
| 1254 | + * @param string $password | |
| 1255 | + * @return bool | |
| 1256 | + */ | |
| 1257 | +    public function checkPassword(\OCP\Share\IShare $share, $password) { | |
| 1258 | + $passwordProtected = $share->getShareType() !== \OCP\Share::SHARE_TYPE_LINK | |
| 1259 | + || $share->getShareType() !== \OCP\Share::SHARE_TYPE_EMAIL; | |
| 1260 | +        if (!$passwordProtected) { | |
| 1261 | + //TODO maybe exception? | |
| 1262 | + return false; | |
| 1263 | + } | |
| 1264 | + | |
| 1265 | +        if ($password === null || $share->getPassword() === null) { | |
| 1266 | + return false; | |
| 1267 | + } | |
| 1268 | + | |
| 1269 | + $newHash = ''; | |
| 1270 | +        if (!$this->hasher->verify($password, $share->getPassword(), $newHash)) { | |
| 1271 | + return false; | |
| 1272 | + } | |
| 1273 | + | |
| 1274 | +        if (!empty($newHash)) { | |
| 1275 | + $share->setPassword($newHash); | |
| 1276 | + $provider = $this->factory->getProviderForType($share->getShareType()); | |
| 1277 | + $provider->update($share); | |
| 1278 | + } | |
| 1279 | + | |
| 1280 | + return true; | |
| 1281 | + } | |
| 1282 | + | |
| 1283 | + /** | |
| 1284 | + * @inheritdoc | |
| 1285 | + */ | |
| 1286 | +    public function userDeleted($uid) { | |
| 1287 | + $types = [\OCP\Share::SHARE_TYPE_USER, \OCP\Share::SHARE_TYPE_GROUP, \OCP\Share::SHARE_TYPE_LINK, \OCP\Share::SHARE_TYPE_REMOTE, \OCP\Share::SHARE_TYPE_EMAIL]; | |
| 1288 | + | |
| 1289 | +        foreach ($types as $type) { | |
| 1290 | +            try { | |
| 1291 | + $provider = $this->factory->getProviderForType($type); | |
| 1292 | +            } catch (ProviderException $e) { | |
| 1293 | + continue; | |
| 1294 | + } | |
| 1295 | + $provider->userDeleted($uid, $type); | |
| 1296 | + } | |
| 1297 | + } | |
| 1298 | + | |
| 1299 | + /** | |
| 1300 | + * @inheritdoc | |
| 1301 | + */ | |
| 1302 | +    public function groupDeleted($gid) { | |
| 1303 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); | |
| 1304 | + $provider->groupDeleted($gid); | |
| 1305 | + } | |
| 1306 | + | |
| 1307 | + /** | |
| 1308 | + * @inheritdoc | |
| 1309 | + */ | |
| 1310 | +    public function userDeletedFromGroup($uid, $gid) { | |
| 1311 | + $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); | |
| 1312 | + $provider->userDeletedFromGroup($uid, $gid); | |
| 1313 | + } | |
| 1314 | + | |
| 1315 | + /** | |
| 1316 | + * Get access list to a path. This means | |
| 1317 | + * all the users that can access a given path. | |
| 1318 | + * | |
| 1319 | + * Consider: | |
| 1320 | + * -root | |
| 1321 | + * |-folder1 (23) | |
| 1322 | + * |-folder2 (32) | |
| 1323 | + * |-fileA (42) | |
| 1324 | + * | |
| 1325 | + * fileA is shared with user1 and user1@server1 | |
| 1326 | + * folder2 is shared with group2 (user4 is a member of group2) | |
| 1327 | + * folder1 is shared with user2 (renamed to "folder (1)") and user2@server2 | |
| 1328 | + * | |
| 1329 | + * Then the access list to '/folder1/folder2/fileA' with $currentAccess is: | |
| 1330 | + * [ | |
| 1331 | + * users => [ | |
| 1332 | + * 'user1' => ['node_id' => 42, 'node_path' => '/fileA'], | |
| 1333 | + * 'user4' => ['node_id' => 32, 'node_path' => '/folder2'], | |
| 1334 | + * 'user2' => ['node_id' => 23, 'node_path' => '/folder (1)'], | |
| 1335 | + * ], | |
| 1336 | + * remote => [ | |
| 1337 | + * 'user1@server1' => ['node_id' => 42, 'token' => 'SeCr3t'], | |
| 1338 | + * 'user2@server2' => ['node_id' => 23, 'token' => 'FooBaR'], | |
| 1339 | + * ], | |
| 1340 | + * public => bool | |
| 1341 | + * mail => bool | |
| 1342 | + * ] | |
| 1343 | + * | |
| 1344 | + * The access list to '/folder1/folder2/fileA' **without** $currentAccess is: | |
| 1345 | + * [ | |
| 1346 | + * users => ['user1', 'user2', 'user4'], | |
| 1347 | + * remote => bool, | |
| 1348 | + * public => bool | |
| 1349 | + * mail => bool | |
| 1350 | + * ] | |
| 1351 | + * | |
| 1352 | + * This is required for encryption/activity | |
| 1353 | + * | |
| 1354 | + * @param \OCP\Files\Node $path | |
| 1355 | + * @param bool $recursive Should we check all parent folders as well | |
| 1356 | + * @param bool $currentAccess Should the user have currently access to the file | |
| 1357 | + * @return array | |
| 1358 | + */ | |
| 1359 | +    public function getAccessList(\OCP\Files\Node $path, $recursive = true, $currentAccess = false) { | |
| 1360 | + $owner = $path->getOwner()->getUID(); | |
| 1361 | + | |
| 1362 | +        if ($currentAccess) { | |
| 1363 | + $al = ['users' => [], 'remote' => [], 'public' => false]; | |
| 1364 | +        } else { | |
| 1365 | + $al = ['users' => [], 'remote' => false, 'public' => false]; | |
| 1366 | + } | |
| 1367 | +        if (!$this->userManager->userExists($owner)) { | |
| 1368 | + return $al; | |
| 1369 | + } | |
| 1370 | + | |
| 1371 | + //Get node for the owner | |
| 1372 | + $userFolder = $this->rootFolder->getUserFolder($owner); | |
| 1373 | +        if ($path->getId() !== $userFolder->getId() && !$userFolder->isSubNode($path)) { | |
| 1374 | + $path = $userFolder->getById($path->getId())[0]; | |
| 1375 | + } | |
| 1376 | + | |
| 1377 | + $providers = $this->factory->getAllProviders(); | |
| 1378 | + | |
| 1379 | + /** @var Node[] $nodes */ | |
| 1380 | + $nodes = []; | |
| 1381 | + | |
| 1382 | + | |
| 1383 | +        if ($currentAccess) { | |
| 1384 | + $ownerPath = $path->getPath(); | |
| 1385 | +            $ownerPath = explode('/', $ownerPath, 4); | |
| 1386 | +            if (count($ownerPath) < 4) { | |
| 1387 | + $ownerPath = ''; | |
| 1388 | +            } else { | |
| 1389 | + $ownerPath = $ownerPath[3]; | |
| 1390 | + } | |
| 1391 | + $al['users'][$owner] = [ | |
| 1392 | + 'node_id' => $path->getId(), | |
| 1393 | + 'node_path' => '/' . $ownerPath, | |
| 1394 | + ]; | |
| 1395 | +        } else { | |
| 1396 | + $al['users'][] = $owner; | |
| 1397 | + } | |
| 1398 | + | |
| 1399 | + // Collect all the shares | |
| 1400 | +        while ($path->getPath() !== $userFolder->getPath()) { | |
| 1401 | + $nodes[] = $path; | |
| 1402 | +            if (!$recursive) { | |
| 1403 | + break; | |
| 1404 | + } | |
| 1405 | + $path = $path->getParent(); | |
| 1406 | + } | |
| 1407 | + | |
| 1408 | +        foreach ($providers as $provider) { | |
| 1409 | + $tmp = $provider->getAccessList($nodes, $currentAccess); | |
| 1410 | + | |
| 1411 | +            foreach ($tmp as $k => $v) { | |
| 1412 | +                if (isset($al[$k])) { | |
| 1413 | +                    if (is_array($al[$k])) { | |
| 1414 | +                        if ($currentAccess) { | |
| 1415 | + $al[$k] += $v; | |
| 1416 | +                        } else { | |
| 1417 | + $al[$k] = array_merge($al[$k], $v); | |
| 1418 | + $al[$k] = array_unique($al[$k]); | |
| 1419 | + $al[$k] = array_values($al[$k]); | |
| 1420 | + } | |
| 1421 | +                    } else { | |
| 1422 | + $al[$k] = $al[$k] || $v; | |
| 1423 | + } | |
| 1424 | +                } else { | |
| 1425 | + $al[$k] = $v; | |
| 1426 | + } | |
| 1427 | + } | |
| 1428 | + } | |
| 1429 | + | |
| 1430 | + return $al; | |
| 1431 | + } | |
| 1432 | + | |
| 1433 | + /** | |
| 1434 | + * Create a new share | |
| 1435 | + * @return \OCP\Share\IShare; | |
| 1436 | + */ | |
| 1437 | +    public function newShare() { | |
| 1438 | + return new \OC\Share20\Share($this->rootFolder, $this->userManager); | |
| 1439 | + } | |
| 1440 | + | |
| 1441 | + /** | |
| 1442 | + * Is the share API enabled | |
| 1443 | + * | |
| 1444 | + * @return bool | |
| 1445 | + */ | |
| 1446 | +    public function shareApiEnabled() { | |
| 1447 | +        return $this->config->getAppValue('core', 'shareapi_enabled', 'yes') === 'yes'; | |
| 1448 | + } | |
| 1449 | + | |
| 1450 | + /** | |
| 1451 | + * Is public link sharing enabled | |
| 1452 | + * | |
| 1453 | + * @return bool | |
| 1454 | + */ | |
| 1455 | +    public function shareApiAllowLinks() { | |
| 1456 | +        return $this->config->getAppValue('core', 'shareapi_allow_links', 'yes') === 'yes'; | |
| 1457 | + } | |
| 1458 | + | |
| 1459 | + /** | |
| 1460 | + * Is password on public link requires | |
| 1461 | + * | |
| 1462 | + * @return bool | |
| 1463 | + */ | |
| 1464 | +    public function shareApiLinkEnforcePassword() { | |
| 1465 | +        return $this->config->getAppValue('core', 'shareapi_enforce_links_password', 'no') === 'yes'; | |
| 1466 | + } | |
| 1467 | + | |
| 1468 | + /** | |
| 1469 | + * Is default expire date enabled | |
| 1470 | + * | |
| 1471 | + * @return bool | |
| 1472 | + */ | |
| 1473 | +    public function shareApiLinkDefaultExpireDate() { | |
| 1474 | +        return $this->config->getAppValue('core', 'shareapi_default_expire_date', 'no') === 'yes'; | |
| 1475 | + } | |
| 1476 | + | |
| 1477 | + /** | |
| 1478 | + * Is default expire date enforced | |
| 1479 | + *` | |
| 1480 | + * @return bool | |
| 1481 | + */ | |
| 1482 | +    public function shareApiLinkDefaultExpireDateEnforced() { | |
| 1483 | + return $this->shareApiLinkDefaultExpireDate() && | |
| 1484 | +            $this->config->getAppValue('core', 'shareapi_enforce_expire_date', 'no') === 'yes'; | |
| 1485 | + } | |
| 1486 | + | |
| 1487 | + /** | |
| 1488 | + * Number of default expire days | |
| 1489 | + *shareApiLinkAllowPublicUpload | |
| 1490 | + * @return int | |
| 1491 | + */ | |
| 1492 | +    public function shareApiLinkDefaultExpireDays() { | |
| 1493 | +        return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); | |
| 1494 | + } | |
| 1495 | + | |
| 1496 | + /** | |
| 1497 | + * Allow public upload on link shares | |
| 1498 | + * | |
| 1499 | + * @return bool | |
| 1500 | + */ | |
| 1501 | +    public function shareApiLinkAllowPublicUpload() { | |
| 1502 | +        return $this->config->getAppValue('core', 'shareapi_allow_public_upload', 'yes') === 'yes'; | |
| 1503 | + } | |
| 1504 | + | |
| 1505 | + /** | |
| 1506 | + * check if user can only share with group members | |
| 1507 | + * @return bool | |
| 1508 | + */ | |
| 1509 | +    public function shareWithGroupMembersOnly() { | |
| 1510 | +        return $this->config->getAppValue('core', 'shareapi_only_share_with_group_members', 'no') === 'yes'; | |
| 1511 | + } | |
| 1512 | + | |
| 1513 | + /** | |
| 1514 | + * Check if users can share with groups | |
| 1515 | + * @return bool | |
| 1516 | + */ | |
| 1517 | +    public function allowGroupSharing() { | |
| 1518 | +        return $this->config->getAppValue('core', 'shareapi_allow_group_sharing', 'yes') === 'yes'; | |
| 1519 | + } | |
| 1520 | + | |
| 1521 | + /** | |
| 1522 | + * Copied from \OC_Util::isSharingDisabledForUser | |
| 1523 | + * | |
| 1524 | + * TODO: Deprecate fuction from OC_Util | |
| 1525 | + * | |
| 1526 | + * @param string $userId | |
| 1527 | + * @return bool | |
| 1528 | + */ | |
| 1529 | +    public function sharingDisabledForUser($userId) { | |
| 1530 | +        if ($userId === null) { | |
| 1531 | + return false; | |
| 1532 | + } | |
| 1533 | + | |
| 1534 | +        if (isset($this->sharingDisabledForUsersCache[$userId])) { | |
| 1535 | + return $this->sharingDisabledForUsersCache[$userId]; | |
| 1536 | + } | |
| 1537 | + | |
| 1538 | +        if ($this->config->getAppValue('core', 'shareapi_exclude_groups', 'no') === 'yes') { | |
| 1539 | +            $groupsList = $this->config->getAppValue('core', 'shareapi_exclude_groups_list', ''); | |
| 1540 | + $excludedGroups = json_decode($groupsList); | |
| 1541 | +            if (is_null($excludedGroups)) { | |
| 1542 | +                $excludedGroups = explode(',', $groupsList); | |
| 1543 | + $newValue = json_encode($excludedGroups); | |
| 1544 | +                $this->config->setAppValue('core', 'shareapi_exclude_groups_list', $newValue); | |
| 1545 | + } | |
| 1546 | + $user = $this->userManager->get($userId); | |
| 1547 | + $usersGroups = $this->groupManager->getUserGroupIds($user); | |
| 1548 | +            if (!empty($usersGroups)) { | |
| 1549 | + $remainingGroups = array_diff($usersGroups, $excludedGroups); | |
| 1550 | + // if the user is only in groups which are disabled for sharing then | |
| 1551 | + // sharing is also disabled for the user | |
| 1552 | +                if (empty($remainingGroups)) { | |
| 1553 | + $this->sharingDisabledForUsersCache[$userId] = true; | |
| 1554 | + return true; | |
| 1555 | + } | |
| 1556 | + } | |
| 1557 | + } | |
| 1558 | + | |
| 1559 | + $this->sharingDisabledForUsersCache[$userId] = false; | |
| 1560 | + return false; | |
| 1561 | + } | |
| 1562 | + | |
| 1563 | + /** | |
| 1564 | + * @inheritdoc | |
| 1565 | + */ | |
| 1566 | +    public function outgoingServer2ServerSharesAllowed() { | |
| 1567 | +        return $this->config->getAppValue('files_sharing', 'outgoing_server2server_share_enabled', 'yes') === 'yes'; | |
| 1568 | + } | |
| 1569 | + | |
| 1570 | + /** | |
| 1571 | + * @inheritdoc | |
| 1572 | + */ | |
| 1573 | +    public function shareProviderExists($shareType) { | |
| 1574 | +        try { | |
| 1575 | + $this->factory->getProviderForType($shareType); | |
| 1576 | +        } catch (ProviderException $e) { | |
| 1577 | + return false; | |
| 1578 | + } | |
| 1579 | + | |
| 1580 | + return true; | |
| 1581 | + } | |
| 1582 | 1582 | |
| 1583 | 1583 | } | 
| @@ -343,7 +343,7 @@ discard block | ||
| 343 | 343 | |
| 344 | 344 |  		if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) { | 
| 345 | 345 | $expirationDate = new \DateTime(); | 
| 346 | - $expirationDate->setTime(0,0,0); | |
| 346 | + $expirationDate->setTime(0, 0, 0); | |
| 347 | 347 |  			$expirationDate->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); | 
| 348 | 348 | } | 
| 349 | 349 | |
| @@ -355,7 +355,7 @@ discard block | ||
| 355 | 355 | |
| 356 | 356 | $date = new \DateTime(); | 
| 357 | 357 | $date->setTime(0, 0, 0); | 
| 358 | -			$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D')); | |
| 358 | +			$date->add(new \DateInterval('P'.$this->shareApiLinkDefaultExpireDays().'D')); | |
| 359 | 359 |  			if ($date < $expirationDate) { | 
| 360 | 360 |  				$message = $this->l->t('Cannot set expiration date more than %s days in the future', [$this->shareApiLinkDefaultExpireDays()]); | 
| 361 | 361 | throw new GenericShareException($message, $message, 404); | 
| @@ -408,7 +408,7 @@ discard block | ||
| 408 | 408 | */ | 
| 409 | 409 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_USER); | 
| 410 | 410 | $existingShares = $provider->getSharesByPath($share->getNode()); | 
| 411 | -		foreach($existingShares as $existingShare) { | |
| 411 | +		foreach ($existingShares as $existingShare) { | |
| 412 | 412 | // Ignore if it is the same share | 
| 413 | 413 |  			try { | 
| 414 | 414 |  				if ($existingShare->getFullId() === $share->getFullId()) { | 
| @@ -465,7 +465,7 @@ discard block | ||
| 465 | 465 | */ | 
| 466 | 466 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_GROUP); | 
| 467 | 467 | $existingShares = $provider->getSharesByPath($share->getNode()); | 
| 468 | -		foreach($existingShares as $existingShare) { | |
| 468 | +		foreach ($existingShares as $existingShare) { | |
| 469 | 469 |  			try { | 
| 470 | 470 |  				if ($existingShare->getFullId() === $share->getFullId()) { | 
| 471 | 471 | continue; | 
| @@ -534,7 +534,7 @@ discard block | ||
| 534 | 534 | // Make sure that we do not share a path that contains a shared mountpoint | 
| 535 | 535 |  		if ($path instanceof \OCP\Files\Folder) { | 
| 536 | 536 | $mounts = $this->mountManager->findIn($path->getPath()); | 
| 537 | -			foreach($mounts as $mount) { | |
| 537 | +			foreach ($mounts as $mount) { | |
| 538 | 538 |  				if ($mount->getStorage()->instanceOfStorage('\OCA\Files_Sharing\ISharedStorage')) { | 
| 539 | 539 |  					throw new \InvalidArgumentException('Path contains files shared with you'); | 
| 540 | 540 | } | 
| @@ -582,7 +582,7 @@ discard block | ||
| 582 | 582 | $storage = $share->getNode()->getStorage(); | 
| 583 | 583 |  		if ($storage->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { | 
| 584 | 584 | $parent = $share->getNode()->getParent(); | 
| 585 | -			while($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { | |
| 585 | +			while ($parent->getStorage()->instanceOfStorage('OCA\Files_Sharing\External\Storage')) { | |
| 586 | 586 | $parent = $parent->getParent(); | 
| 587 | 587 | } | 
| 588 | 588 | $share->setShareOwner($parent->getOwner()->getUID()); | 
| @@ -639,7 +639,7 @@ discard block | ||
| 639 | 639 | } | 
| 640 | 640 | |
| 641 | 641 | // Generate the target | 
| 642 | -		$target = $this->config->getSystemValue('share_folder', '/') .'/'. $share->getNode()->getName(); | |
| 642 | +		$target = $this->config->getSystemValue('share_folder', '/').'/'.$share->getNode()->getName(); | |
| 643 | 643 | $target = \OC\Files\Filesystem::normalizePath($target); | 
| 644 | 644 | $share->setTarget($target); | 
| 645 | 645 | |
| @@ -700,17 +700,17 @@ discard block | ||
| 700 | 700 | $this->sendMailNotification( | 
| 701 | 701 | $l, | 
| 702 | 702 | $share->getNode()->getName(), | 
| 703 | -						$this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', [ 'fileid' => $share->getNode()->getId() ]), | |
| 703 | +						$this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $share->getNode()->getId()]), | |
| 704 | 704 | $share->getSharedBy(), | 
| 705 | 705 | $emailAddress, | 
| 706 | 706 | $share->getExpirationDate() | 
| 707 | 707 | ); | 
| 708 | -					$this->logger->debug('Send share notification to ' . $emailAddress . ' for share with ID ' . $share->getId(), ['app' => 'share']); | |
| 708 | +					$this->logger->debug('Send share notification to '.$emailAddress.' for share with ID '.$share->getId(), ['app' => 'share']); | |
| 709 | 709 |  				} else { | 
| 710 | -					$this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because email address is not set.', ['app' => 'share']); | |
| 710 | +					$this->logger->debug('Share notification not send to '.$share->getSharedWith().' because email address is not set.', ['app' => 'share']); | |
| 711 | 711 | } | 
| 712 | 712 |  			} else { | 
| 713 | -				$this->logger->debug('Share notification not send to ' . $share->getSharedWith() . ' because user could not be found.', ['app' => 'share']); | |
| 713 | +				$this->logger->debug('Share notification not send to '.$share->getSharedWith().' because user could not be found.', ['app' => 'share']); | |
| 714 | 714 | } | 
| 715 | 715 | } | 
| 716 | 716 | |
| @@ -751,7 +751,7 @@ discard block | ||
| 751 | 751 |  		$text = $l->t('%s shared »%s« with you.', [$initiatorDisplayName, $filename]); | 
| 752 | 752 | |
| 753 | 753 | $emailTemplate->addBodyText( | 
| 754 | -			$text . ' ' . $l->t('Click the button below to open it.'), | |
| 754 | +			$text.' '.$l->t('Click the button below to open it.'), | |
| 755 | 755 | $text | 
| 756 | 756 | ); | 
| 757 | 757 | $emailTemplate->addBodyButton( | 
| @@ -775,9 +775,9 @@ discard block | ||
| 775 | 775 | // The "Reply-To" is set to the sharer if an mail address is configured | 
| 776 | 776 | // also the default footer contains a "Do not reply" which needs to be adjusted. | 
| 777 | 777 | $initiatorEmail = $initiatorUser->getEMailAddress(); | 
| 778 | -		if($initiatorEmail !== null) { | |
| 778 | +		if ($initiatorEmail !== null) { | |
| 779 | 779 | $message->setReplyTo([$initiatorEmail => $initiatorDisplayName]); | 
| 780 | - $emailTemplate->addFooter($instanceName . ' - ' . $this->defaults->getSlogan()); | |
| 780 | + $emailTemplate->addFooter($instanceName.' - '.$this->defaults->getSlogan()); | |
| 781 | 781 |  		} else { | 
| 782 | 782 | $emailTemplate->addFooter(); | 
| 783 | 783 | } | 
| @@ -988,7 +988,7 @@ discard block | ||
| 988 | 988 | * @param string $recipientId | 
| 989 | 989 | */ | 
| 990 | 990 |  	public function deleteFromSelf(\OCP\Share\IShare $share, $recipientId) { | 
| 991 | - list($providerId, ) = $this->splitFullId($share->getFullId()); | |
| 991 | + list($providerId,) = $this->splitFullId($share->getFullId()); | |
| 992 | 992 | $provider = $this->factory->getProvider($providerId); | 
| 993 | 993 | |
| 994 | 994 | $provider->deleteFromSelf($share, $recipientId); | 
| @@ -1009,7 +1009,7 @@ discard block | ||
| 1009 | 1009 |  		if ($share->getShareType() === \OCP\Share::SHARE_TYPE_GROUP) { | 
| 1010 | 1010 | $sharedWith = $this->groupManager->get($share->getSharedWith()); | 
| 1011 | 1011 |  			if (is_null($sharedWith)) { | 
| 1012 | -				throw new \InvalidArgumentException('Group "' . $share->getSharedWith() . '" does not exist'); | |
| 1012 | +				throw new \InvalidArgumentException('Group "'.$share->getSharedWith().'" does not exist'); | |
| 1013 | 1013 | } | 
| 1014 | 1014 | $recipient = $this->userManager->get($recipientId); | 
| 1015 | 1015 |  			if (!$sharedWith->inGroup($recipient)) { | 
| @@ -1017,7 +1017,7 @@ discard block | ||
| 1017 | 1017 | } | 
| 1018 | 1018 | } | 
| 1019 | 1019 | |
| 1020 | - list($providerId, ) = $this->splitFullId($share->getFullId()); | |
| 1020 | + list($providerId,) = $this->splitFullId($share->getFullId()); | |
| 1021 | 1021 | $provider = $this->factory->getProvider($providerId); | 
| 1022 | 1022 | |
| 1023 | 1023 | $provider->move($share, $recipientId); | 
| @@ -1064,7 +1064,7 @@ discard block | ||
| 1064 | 1064 | |
| 1065 | 1065 | $shares2 = []; | 
| 1066 | 1066 | |
| 1067 | -		while(true) { | |
| 1067 | +		while (true) { | |
| 1068 | 1068 | $added = 0; | 
| 1069 | 1069 |  			foreach ($shares as $share) { | 
| 1070 | 1070 | |
| @@ -1164,7 +1164,7 @@ discard block | ||
| 1164 | 1164 | * | 
| 1165 | 1165 | * @return Share[] | 
| 1166 | 1166 | */ | 
| 1167 | -	public function getSharesByPath(\OCP\Files\Node $path, $page=0, $perPage=50) { | |
| 1167 | +	public function getSharesByPath(\OCP\Files\Node $path, $page = 0, $perPage = 50) { | |
| 1168 | 1168 | return []; | 
| 1169 | 1169 | } | 
| 1170 | 1170 | |
| @@ -1183,7 +1183,7 @@ discard block | ||
| 1183 | 1183 | } | 
| 1184 | 1184 | $share = null; | 
| 1185 | 1185 |  		try { | 
| 1186 | -			if($this->shareApiAllowLinks()) { | |
| 1186 | +			if ($this->shareApiAllowLinks()) { | |
| 1187 | 1187 | $provider = $this->factory->getProviderForType(\OCP\Share::SHARE_TYPE_LINK); | 
| 1188 | 1188 | $share = $provider->getShareByToken($token); | 
| 1189 | 1189 | } | 
| @@ -1390,7 +1390,7 @@ discard block | ||
| 1390 | 1390 | } | 
| 1391 | 1391 | $al['users'][$owner] = [ | 
| 1392 | 1392 | 'node_id' => $path->getId(), | 
| 1393 | - 'node_path' => '/' . $ownerPath, | |
| 1393 | + 'node_path' => '/'.$ownerPath, | |
| 1394 | 1394 | ]; | 
| 1395 | 1395 |  		} else { | 
| 1396 | 1396 | $al['users'][] = $owner; | 
| @@ -1490,7 +1490,7 @@ discard block | ||
| 1490 | 1490 | * @return int | 
| 1491 | 1491 | */ | 
| 1492 | 1492 |  	public function shareApiLinkDefaultExpireDays() { | 
| 1493 | -		return (int)$this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); | |
| 1493 | +		return (int) $this->config->getAppValue('core', 'shareapi_expire_after_n_days', '7'); | |
| 1494 | 1494 | } | 
| 1495 | 1495 | |
| 1496 | 1496 | /** |