@@ -73,618 +73,617 @@ |
||
| 73 | 73 | */ |
| 74 | 74 | class ShareController extends AuthPublicShareController { |
| 75 | 75 | |
| 76 | - /** @var IConfig */ |
|
| 77 | - protected $config; |
|
| 78 | - /** @var IUserManager */ |
|
| 79 | - protected $userManager; |
|
| 80 | - /** @var ILogger */ |
|
| 81 | - protected $logger; |
|
| 82 | - /** @var \OCP\Activity\IManager */ |
|
| 83 | - protected $activityManager; |
|
| 84 | - /** @var IPreview */ |
|
| 85 | - protected $previewManager; |
|
| 86 | - /** @var IRootFolder */ |
|
| 87 | - protected $rootFolder; |
|
| 88 | - /** @var FederatedShareProvider */ |
|
| 89 | - protected $federatedShareProvider; |
|
| 90 | - /** @var EventDispatcherInterface */ |
|
| 91 | - protected $eventDispatcher; |
|
| 92 | - /** @var IL10N */ |
|
| 93 | - protected $l10n; |
|
| 94 | - /** @var Defaults */ |
|
| 95 | - protected $defaults; |
|
| 96 | - /** @var ShareManager */ |
|
| 97 | - protected $shareManager; |
|
| 98 | - |
|
| 99 | - /** @var Share\IShare */ |
|
| 100 | - protected $share; |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * @param string $appName |
|
| 104 | - * @param IRequest $request |
|
| 105 | - * @param IConfig $config |
|
| 106 | - * @param IURLGenerator $urlGenerator |
|
| 107 | - * @param IUserManager $userManager |
|
| 108 | - * @param ILogger $logger |
|
| 109 | - * @param \OCP\Activity\IManager $activityManager |
|
| 110 | - * @param \OCP\Share\IManager $shareManager |
|
| 111 | - * @param ISession $session |
|
| 112 | - * @param IPreview $previewManager |
|
| 113 | - * @param IRootFolder $rootFolder |
|
| 114 | - * @param FederatedShareProvider $federatedShareProvider |
|
| 115 | - * @param EventDispatcherInterface $eventDispatcher |
|
| 116 | - * @param IL10N $l10n |
|
| 117 | - * @param Defaults $defaults |
|
| 118 | - */ |
|
| 119 | - public function __construct(string $appName, |
|
| 120 | - IRequest $request, |
|
| 121 | - IConfig $config, |
|
| 122 | - IURLGenerator $urlGenerator, |
|
| 123 | - IUserManager $userManager, |
|
| 124 | - ILogger $logger, |
|
| 125 | - \OCP\Activity\IManager $activityManager, |
|
| 126 | - ShareManager $shareManager, |
|
| 127 | - ISession $session, |
|
| 128 | - IPreview $previewManager, |
|
| 129 | - IRootFolder $rootFolder, |
|
| 130 | - FederatedShareProvider $federatedShareProvider, |
|
| 131 | - EventDispatcherInterface $eventDispatcher, |
|
| 132 | - IL10N $l10n, |
|
| 133 | - Defaults $defaults) { |
|
| 134 | - parent::__construct($appName, $request, $session, $urlGenerator); |
|
| 135 | - |
|
| 136 | - $this->config = $config; |
|
| 137 | - $this->userManager = $userManager; |
|
| 138 | - $this->logger = $logger; |
|
| 139 | - $this->activityManager = $activityManager; |
|
| 140 | - $this->previewManager = $previewManager; |
|
| 141 | - $this->rootFolder = $rootFolder; |
|
| 142 | - $this->federatedShareProvider = $federatedShareProvider; |
|
| 143 | - $this->eventDispatcher = $eventDispatcher; |
|
| 144 | - $this->l10n = $l10n; |
|
| 145 | - $this->defaults = $defaults; |
|
| 146 | - $this->shareManager = $shareManager; |
|
| 147 | - } |
|
| 148 | - |
|
| 149 | - /** |
|
| 150 | - * @PublicPage |
|
| 151 | - * @NoCSRFRequired |
|
| 152 | - * |
|
| 153 | - * Show the authentication page |
|
| 154 | - * The form has to submit to the authenticate method route |
|
| 155 | - */ |
|
| 156 | - public function showAuthenticate(): TemplateResponse { |
|
| 157 | - $templateParameters = ['share' => $this->share]; |
|
| 158 | - |
|
| 159 | - $event = new GenericEvent(null, $templateParameters); |
|
| 160 | - $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event); |
|
| 161 | - |
|
| 162 | - $response = new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest'); |
|
| 163 | - if ($this->share->getSendPasswordByTalk()) { |
|
| 164 | - $csp = new ContentSecurityPolicy(); |
|
| 165 | - $csp->addAllowedConnectDomain('*'); |
|
| 166 | - $csp->addAllowedMediaDomain('blob:'); |
|
| 167 | - $response->setContentSecurityPolicy($csp); |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - return $response; |
|
| 171 | - } |
|
| 172 | - |
|
| 173 | - /** |
|
| 174 | - * The template to show when authentication failed |
|
| 175 | - */ |
|
| 176 | - protected function showAuthFailed(): TemplateResponse { |
|
| 177 | - $templateParameters = ['share' => $this->share, 'wrongpw' => true]; |
|
| 178 | - |
|
| 179 | - $event = new GenericEvent(null, $templateParameters); |
|
| 180 | - $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event); |
|
| 181 | - |
|
| 182 | - $response = new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest'); |
|
| 183 | - if ($this->share->getSendPasswordByTalk()) { |
|
| 184 | - $csp = new ContentSecurityPolicy(); |
|
| 185 | - $csp->addAllowedConnectDomain('*'); |
|
| 186 | - $csp->addAllowedMediaDomain('blob:'); |
|
| 187 | - $response->setContentSecurityPolicy($csp); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - return $response; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - protected function verifyPassword(string $password): bool { |
|
| 194 | - return $this->shareManager->checkPassword($this->share, $password); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - protected function getPasswordHash(): string { |
|
| 198 | - return $this->share->getPassword(); |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - public function isValidToken(): bool { |
|
| 202 | - try { |
|
| 203 | - $this->share = $this->shareManager->getShareByToken($this->getToken()); |
|
| 204 | - } catch (ShareNotFound $e) { |
|
| 205 | - return false; |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - return true; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - protected function isPasswordProtected(): bool { |
|
| 212 | - return $this->share->getPassword() !== null; |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - protected function authSucceeded() { |
|
| 216 | - // For share this was always set so it is still used in other apps |
|
| 217 | - $this->session->set('public_link_authenticated', (string)$this->share->getId()); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - protected function authFailed() { |
|
| 221 | - $this->emitAccessShareHook($this->share, 403, 'Wrong password'); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - /** |
|
| 225 | - * throws hooks when a share is attempted to be accessed |
|
| 226 | - * |
|
| 227 | - * @param \OCP\Share\IShare|string $share the Share instance if available, |
|
| 228 | - * otherwise token |
|
| 229 | - * @param int $errorCode |
|
| 230 | - * @param string $errorMessage |
|
| 231 | - * @throws \OC\HintException |
|
| 232 | - * @throws \OC\ServerNotAvailableException |
|
| 233 | - */ |
|
| 234 | - protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') { |
|
| 235 | - $itemType = $itemSource = $uidOwner = ''; |
|
| 236 | - $token = $share; |
|
| 237 | - $exception = null; |
|
| 238 | - if($share instanceof \OCP\Share\IShare) { |
|
| 239 | - try { |
|
| 240 | - $token = $share->getToken(); |
|
| 241 | - $uidOwner = $share->getSharedBy(); |
|
| 242 | - $itemType = $share->getNodeType(); |
|
| 243 | - $itemSource = $share->getNodeId(); |
|
| 244 | - } catch (\Exception $e) { |
|
| 245 | - // we log what we know and pass on the exception afterwards |
|
| 246 | - $exception = $e; |
|
| 247 | - } |
|
| 248 | - } |
|
| 249 | - \OC_Hook::emit(Share::class, 'share_link_access', [ |
|
| 250 | - 'itemType' => $itemType, |
|
| 251 | - 'itemSource' => $itemSource, |
|
| 252 | - 'uidOwner' => $uidOwner, |
|
| 253 | - 'token' => $token, |
|
| 254 | - 'errorCode' => $errorCode, |
|
| 255 | - 'errorMessage' => $errorMessage, |
|
| 256 | - ]); |
|
| 257 | - if(!is_null($exception)) { |
|
| 258 | - throw $exception; |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Validate the permissions of the share |
|
| 264 | - * |
|
| 265 | - * @param Share\IShare $share |
|
| 266 | - * @return bool |
|
| 267 | - */ |
|
| 268 | - private function validateShare(\OCP\Share\IShare $share) { |
|
| 269 | - return $share->getNode()->isReadable() && $share->getNode()->isShareable(); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - /** |
|
| 273 | - * @PublicPage |
|
| 274 | - * @NoCSRFRequired |
|
| 275 | - * |
|
| 276 | - |
|
| 277 | - * @param string $path |
|
| 278 | - * @return TemplateResponse |
|
| 279 | - * @throws NotFoundException |
|
| 280 | - * @throws \Exception |
|
| 281 | - */ |
|
| 282 | - public function showShare($path = ''): TemplateResponse { |
|
| 283 | - \OC_User::setIncognitoMode(true); |
|
| 284 | - |
|
| 285 | - // Check whether share exists |
|
| 286 | - try { |
|
| 287 | - $share = $this->shareManager->getShareByToken($this->getToken()); |
|
| 288 | - } catch (ShareNotFound $e) { |
|
| 289 | - $this->emitAccessShareHook($this->getToken(), 404, 'Share not found'); |
|
| 290 | - throw new NotFoundException(); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - if (!$this->validateShare($share)) { |
|
| 294 | - throw new NotFoundException(); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - $shareNode = $share->getNode(); |
|
| 298 | - |
|
| 299 | - // We can't get the path of a file share |
|
| 300 | - try { |
|
| 301 | - if ($shareNode instanceof \OCP\Files\File && $path !== '') { |
|
| 302 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 303 | - throw new NotFoundException(); |
|
| 304 | - } |
|
| 305 | - } catch (\Exception $e) { |
|
| 306 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 307 | - throw $e; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - $shareTmpl = []; |
|
| 311 | - $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 312 | - $shareTmpl['owner'] = $share->getShareOwner(); |
|
| 313 | - $shareTmpl['filename'] = $shareNode->getName(); |
|
| 314 | - $shareTmpl['directory_path'] = $share->getTarget(); |
|
| 315 | - $shareTmpl['note'] = $share->getNote(); |
|
| 316 | - $shareTmpl['mimetype'] = $shareNode->getMimetype(); |
|
| 317 | - $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($shareNode->getMimetype()); |
|
| 318 | - $shareTmpl['dirToken'] = $this->getToken(); |
|
| 319 | - $shareTmpl['sharingToken'] = $this->getToken(); |
|
| 320 | - $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
| 321 | - $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false'; |
|
| 322 | - $shareTmpl['dir'] = ''; |
|
| 323 | - $shareTmpl['nonHumanFileSize'] = $shareNode->getSize(); |
|
| 324 | - $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($shareNode->getSize()); |
|
| 325 | - $shareTmpl['hideDownload'] = $share->getHideDownload(); |
|
| 326 | - |
|
| 327 | - $hideFileList = false; |
|
| 328 | - |
|
| 329 | - if ($shareNode instanceof \OCP\Files\Folder) { |
|
| 330 | - |
|
| 331 | - $shareIsFolder = true; |
|
| 332 | - |
|
| 333 | - try { |
|
| 334 | - $folderNode = $shareNode->get($path); |
|
| 335 | - } catch (\OCP\Files\NotFoundException $e) { |
|
| 336 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 337 | - throw new NotFoundException(); |
|
| 338 | - } |
|
| 339 | - |
|
| 340 | - $shareTmpl['dir'] = $shareNode->getRelativePath($folderNode->getPath()); |
|
| 341 | - |
|
| 342 | - /* |
|
| 76 | + /** @var IConfig */ |
|
| 77 | + protected $config; |
|
| 78 | + /** @var IUserManager */ |
|
| 79 | + protected $userManager; |
|
| 80 | + /** @var ILogger */ |
|
| 81 | + protected $logger; |
|
| 82 | + /** @var \OCP\Activity\IManager */ |
|
| 83 | + protected $activityManager; |
|
| 84 | + /** @var IPreview */ |
|
| 85 | + protected $previewManager; |
|
| 86 | + /** @var IRootFolder */ |
|
| 87 | + protected $rootFolder; |
|
| 88 | + /** @var FederatedShareProvider */ |
|
| 89 | + protected $federatedShareProvider; |
|
| 90 | + /** @var EventDispatcherInterface */ |
|
| 91 | + protected $eventDispatcher; |
|
| 92 | + /** @var IL10N */ |
|
| 93 | + protected $l10n; |
|
| 94 | + /** @var Defaults */ |
|
| 95 | + protected $defaults; |
|
| 96 | + /** @var ShareManager */ |
|
| 97 | + protected $shareManager; |
|
| 98 | + |
|
| 99 | + /** @var Share\IShare */ |
|
| 100 | + protected $share; |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * @param string $appName |
|
| 104 | + * @param IRequest $request |
|
| 105 | + * @param IConfig $config |
|
| 106 | + * @param IURLGenerator $urlGenerator |
|
| 107 | + * @param IUserManager $userManager |
|
| 108 | + * @param ILogger $logger |
|
| 109 | + * @param \OCP\Activity\IManager $activityManager |
|
| 110 | + * @param \OCP\Share\IManager $shareManager |
|
| 111 | + * @param ISession $session |
|
| 112 | + * @param IPreview $previewManager |
|
| 113 | + * @param IRootFolder $rootFolder |
|
| 114 | + * @param FederatedShareProvider $federatedShareProvider |
|
| 115 | + * @param EventDispatcherInterface $eventDispatcher |
|
| 116 | + * @param IL10N $l10n |
|
| 117 | + * @param Defaults $defaults |
|
| 118 | + */ |
|
| 119 | + public function __construct(string $appName, |
|
| 120 | + IRequest $request, |
|
| 121 | + IConfig $config, |
|
| 122 | + IURLGenerator $urlGenerator, |
|
| 123 | + IUserManager $userManager, |
|
| 124 | + ILogger $logger, |
|
| 125 | + \OCP\Activity\IManager $activityManager, |
|
| 126 | + ShareManager $shareManager, |
|
| 127 | + ISession $session, |
|
| 128 | + IPreview $previewManager, |
|
| 129 | + IRootFolder $rootFolder, |
|
| 130 | + FederatedShareProvider $federatedShareProvider, |
|
| 131 | + EventDispatcherInterface $eventDispatcher, |
|
| 132 | + IL10N $l10n, |
|
| 133 | + Defaults $defaults) { |
|
| 134 | + parent::__construct($appName, $request, $session, $urlGenerator); |
|
| 135 | + |
|
| 136 | + $this->config = $config; |
|
| 137 | + $this->userManager = $userManager; |
|
| 138 | + $this->logger = $logger; |
|
| 139 | + $this->activityManager = $activityManager; |
|
| 140 | + $this->previewManager = $previewManager; |
|
| 141 | + $this->rootFolder = $rootFolder; |
|
| 142 | + $this->federatedShareProvider = $federatedShareProvider; |
|
| 143 | + $this->eventDispatcher = $eventDispatcher; |
|
| 144 | + $this->l10n = $l10n; |
|
| 145 | + $this->defaults = $defaults; |
|
| 146 | + $this->shareManager = $shareManager; |
|
| 147 | + } |
|
| 148 | + |
|
| 149 | + /** |
|
| 150 | + * @PublicPage |
|
| 151 | + * @NoCSRFRequired |
|
| 152 | + * |
|
| 153 | + * Show the authentication page |
|
| 154 | + * The form has to submit to the authenticate method route |
|
| 155 | + */ |
|
| 156 | + public function showAuthenticate(): TemplateResponse { |
|
| 157 | + $templateParameters = ['share' => $this->share]; |
|
| 158 | + |
|
| 159 | + $event = new GenericEvent(null, $templateParameters); |
|
| 160 | + $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event); |
|
| 161 | + |
|
| 162 | + $response = new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest'); |
|
| 163 | + if ($this->share->getSendPasswordByTalk()) { |
|
| 164 | + $csp = new ContentSecurityPolicy(); |
|
| 165 | + $csp->addAllowedConnectDomain('*'); |
|
| 166 | + $csp->addAllowedMediaDomain('blob:'); |
|
| 167 | + $response->setContentSecurityPolicy($csp); |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + return $response; |
|
| 171 | + } |
|
| 172 | + |
|
| 173 | + /** |
|
| 174 | + * The template to show when authentication failed |
|
| 175 | + */ |
|
| 176 | + protected function showAuthFailed(): TemplateResponse { |
|
| 177 | + $templateParameters = ['share' => $this->share, 'wrongpw' => true]; |
|
| 178 | + |
|
| 179 | + $event = new GenericEvent(null, $templateParameters); |
|
| 180 | + $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts::publicShareAuth', $event); |
|
| 181 | + |
|
| 182 | + $response = new TemplateResponse('core', 'publicshareauth', $templateParameters, 'guest'); |
|
| 183 | + if ($this->share->getSendPasswordByTalk()) { |
|
| 184 | + $csp = new ContentSecurityPolicy(); |
|
| 185 | + $csp->addAllowedConnectDomain('*'); |
|
| 186 | + $csp->addAllowedMediaDomain('blob:'); |
|
| 187 | + $response->setContentSecurityPolicy($csp); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + return $response; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + protected function verifyPassword(string $password): bool { |
|
| 194 | + return $this->shareManager->checkPassword($this->share, $password); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + protected function getPasswordHash(): string { |
|
| 198 | + return $this->share->getPassword(); |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + public function isValidToken(): bool { |
|
| 202 | + try { |
|
| 203 | + $this->share = $this->shareManager->getShareByToken($this->getToken()); |
|
| 204 | + } catch (ShareNotFound $e) { |
|
| 205 | + return false; |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + return true; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + protected function isPasswordProtected(): bool { |
|
| 212 | + return $this->share->getPassword() !== null; |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + protected function authSucceeded() { |
|
| 216 | + // For share this was always set so it is still used in other apps |
|
| 217 | + $this->session->set('public_link_authenticated', (string)$this->share->getId()); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + protected function authFailed() { |
|
| 221 | + $this->emitAccessShareHook($this->share, 403, 'Wrong password'); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + /** |
|
| 225 | + * throws hooks when a share is attempted to be accessed |
|
| 226 | + * |
|
| 227 | + * @param \OCP\Share\IShare|string $share the Share instance if available, |
|
| 228 | + * otherwise token |
|
| 229 | + * @param int $errorCode |
|
| 230 | + * @param string $errorMessage |
|
| 231 | + * @throws \OC\HintException |
|
| 232 | + * @throws \OC\ServerNotAvailableException |
|
| 233 | + */ |
|
| 234 | + protected function emitAccessShareHook($share, $errorCode = 200, $errorMessage = '') { |
|
| 235 | + $itemType = $itemSource = $uidOwner = ''; |
|
| 236 | + $token = $share; |
|
| 237 | + $exception = null; |
|
| 238 | + if($share instanceof \OCP\Share\IShare) { |
|
| 239 | + try { |
|
| 240 | + $token = $share->getToken(); |
|
| 241 | + $uidOwner = $share->getSharedBy(); |
|
| 242 | + $itemType = $share->getNodeType(); |
|
| 243 | + $itemSource = $share->getNodeId(); |
|
| 244 | + } catch (\Exception $e) { |
|
| 245 | + // we log what we know and pass on the exception afterwards |
|
| 246 | + $exception = $e; |
|
| 247 | + } |
|
| 248 | + } |
|
| 249 | + \OC_Hook::emit(Share::class, 'share_link_access', [ |
|
| 250 | + 'itemType' => $itemType, |
|
| 251 | + 'itemSource' => $itemSource, |
|
| 252 | + 'uidOwner' => $uidOwner, |
|
| 253 | + 'token' => $token, |
|
| 254 | + 'errorCode' => $errorCode, |
|
| 255 | + 'errorMessage' => $errorMessage, |
|
| 256 | + ]); |
|
| 257 | + if(!is_null($exception)) { |
|
| 258 | + throw $exception; |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Validate the permissions of the share |
|
| 264 | + * |
|
| 265 | + * @param Share\IShare $share |
|
| 266 | + * @return bool |
|
| 267 | + */ |
|
| 268 | + private function validateShare(\OCP\Share\IShare $share) { |
|
| 269 | + return $share->getNode()->isReadable() && $share->getNode()->isShareable(); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + /** |
|
| 273 | + * @PublicPage |
|
| 274 | + * @NoCSRFRequired |
|
| 275 | + * |
|
| 276 | + * @param string $path |
|
| 277 | + * @return TemplateResponse |
|
| 278 | + * @throws NotFoundException |
|
| 279 | + * @throws \Exception |
|
| 280 | + */ |
|
| 281 | + public function showShare($path = ''): TemplateResponse { |
|
| 282 | + \OC_User::setIncognitoMode(true); |
|
| 283 | + |
|
| 284 | + // Check whether share exists |
|
| 285 | + try { |
|
| 286 | + $share = $this->shareManager->getShareByToken($this->getToken()); |
|
| 287 | + } catch (ShareNotFound $e) { |
|
| 288 | + $this->emitAccessShareHook($this->getToken(), 404, 'Share not found'); |
|
| 289 | + throw new NotFoundException(); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + if (!$this->validateShare($share)) { |
|
| 293 | + throw new NotFoundException(); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + $shareNode = $share->getNode(); |
|
| 297 | + |
|
| 298 | + // We can't get the path of a file share |
|
| 299 | + try { |
|
| 300 | + if ($shareNode instanceof \OCP\Files\File && $path !== '') { |
|
| 301 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 302 | + throw new NotFoundException(); |
|
| 303 | + } |
|
| 304 | + } catch (\Exception $e) { |
|
| 305 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 306 | + throw $e; |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + $shareTmpl = []; |
|
| 310 | + $shareTmpl['displayName'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 311 | + $shareTmpl['owner'] = $share->getShareOwner(); |
|
| 312 | + $shareTmpl['filename'] = $shareNode->getName(); |
|
| 313 | + $shareTmpl['directory_path'] = $share->getTarget(); |
|
| 314 | + $shareTmpl['note'] = $share->getNote(); |
|
| 315 | + $shareTmpl['mimetype'] = $shareNode->getMimetype(); |
|
| 316 | + $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($shareNode->getMimetype()); |
|
| 317 | + $shareTmpl['dirToken'] = $this->getToken(); |
|
| 318 | + $shareTmpl['sharingToken'] = $this->getToken(); |
|
| 319 | + $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled(); |
|
| 320 | + $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false'; |
|
| 321 | + $shareTmpl['dir'] = ''; |
|
| 322 | + $shareTmpl['nonHumanFileSize'] = $shareNode->getSize(); |
|
| 323 | + $shareTmpl['fileSize'] = \OCP\Util::humanFileSize($shareNode->getSize()); |
|
| 324 | + $shareTmpl['hideDownload'] = $share->getHideDownload(); |
|
| 325 | + |
|
| 326 | + $hideFileList = false; |
|
| 327 | + |
|
| 328 | + if ($shareNode instanceof \OCP\Files\Folder) { |
|
| 329 | + |
|
| 330 | + $shareIsFolder = true; |
|
| 331 | + |
|
| 332 | + try { |
|
| 333 | + $folderNode = $shareNode->get($path); |
|
| 334 | + } catch (\OCP\Files\NotFoundException $e) { |
|
| 335 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 336 | + throw new NotFoundException(); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + $shareTmpl['dir'] = $shareNode->getRelativePath($folderNode->getPath()); |
|
| 340 | + |
|
| 341 | + /* |
|
| 343 | 342 | * The OC_Util methods require a view. This just uses the node API |
| 344 | 343 | */ |
| 345 | - $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath()); |
|
| 346 | - if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 347 | - $freeSpace = max($freeSpace, 0); |
|
| 348 | - } else { |
|
| 349 | - $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 350 | - } |
|
| 351 | - |
|
| 352 | - $hideFileList = !($share->getPermissions() & \OCP\Constants::PERMISSION_READ); |
|
| 353 | - $maxUploadFilesize = $freeSpace; |
|
| 354 | - |
|
| 355 | - $folder = new Template('files', 'list', ''); |
|
| 356 | - $folder->assign('dir', $shareNode->getRelativePath($folderNode->getPath())); |
|
| 357 | - $folder->assign('dirToken', $this->getToken()); |
|
| 358 | - $folder->assign('permissions', \OCP\Constants::PERMISSION_READ); |
|
| 359 | - $folder->assign('isPublic', true); |
|
| 360 | - $folder->assign('hideFileList', $hideFileList); |
|
| 361 | - $folder->assign('publicUploadEnabled', 'no'); |
|
| 362 | - // default to list view |
|
| 363 | - $folder->assign('showgridview', false); |
|
| 364 | - $folder->assign('uploadMaxFilesize', $maxUploadFilesize); |
|
| 365 | - $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize)); |
|
| 366 | - $folder->assign('freeSpace', $freeSpace); |
|
| 367 | - $folder->assign('usedSpacePercent', 0); |
|
| 368 | - $folder->assign('trash', false); |
|
| 369 | - $shareTmpl['folder'] = $folder->fetchPage(); |
|
| 370 | - } else { |
|
| 371 | - $shareIsFolder = false; |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - // default to list view |
|
| 375 | - $shareTmpl['showgridview'] = false; |
|
| 376 | - |
|
| 377 | - $shareTmpl['hideFileList'] = $hideFileList; |
|
| 378 | - $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 379 | - $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $this->getToken()]); |
|
| 380 | - $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $this->getToken()]); |
|
| 381 | - $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10); |
|
| 382 | - $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true); |
|
| 383 | - $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); |
|
| 384 | - $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); |
|
| 385 | - $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
|
| 386 | - $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; |
|
| 387 | - |
|
| 388 | - if ($shareTmpl['previewSupported']) { |
|
| 389 | - $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 390 | - ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 'token' => $shareTmpl['dirToken']]); |
|
| 391 | - $ogPreview = $shareTmpl['previewImage']; |
|
| 392 | - |
|
| 393 | - // We just have direct previews for image files |
|
| 394 | - if ($shareNode->getMimePart() === 'image') { |
|
| 395 | - $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $this->getToken()]); |
|
| 396 | - |
|
| 397 | - $ogPreview = $shareTmpl['previewURL']; |
|
| 398 | - |
|
| 399 | - //Whatapp is kind of picky about their size requirements |
|
| 400 | - if ($this->request->isUserAgent(['/^WhatsApp/'])) { |
|
| 401 | - $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [ |
|
| 402 | - 'token' => $this->getToken(), |
|
| 403 | - 'x' => 256, |
|
| 404 | - 'y' => 256, |
|
| 405 | - 'a' => true, |
|
| 406 | - ]); |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - } else { |
|
| 410 | - $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 411 | - $ogPreview = $shareTmpl['previewImage']; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - // Load files we need |
|
| 415 | - \OCP\Util::addScript('files', 'file-upload'); |
|
| 416 | - \OCP\Util::addStyle('files_sharing', 'publicView'); |
|
| 417 | - \OCP\Util::addScript('files_sharing', 'public'); |
|
| 418 | - \OCP\Util::addScript('files_sharing', 'templates'); |
|
| 419 | - \OCP\Util::addScript('files', 'fileactions'); |
|
| 420 | - \OCP\Util::addScript('files', 'fileactionsmenu'); |
|
| 421 | - \OCP\Util::addScript('files', 'jquery.fileupload'); |
|
| 422 | - \OCP\Util::addScript('files_sharing', 'files_drop'); |
|
| 423 | - |
|
| 424 | - if (isset($shareTmpl['folder'])) { |
|
| 425 | - // JS required for folders |
|
| 426 | - \OCP\Util::addStyle('files', 'merged'); |
|
| 427 | - \OCP\Util::addScript('files', 'filesummary'); |
|
| 428 | - \OCP\Util::addScript('files', 'templates'); |
|
| 429 | - \OCP\Util::addScript('files', 'breadcrumb'); |
|
| 430 | - \OCP\Util::addScript('files', 'fileinfomodel'); |
|
| 431 | - \OCP\Util::addScript('files', 'newfilemenu'); |
|
| 432 | - \OCP\Util::addScript('files', 'files'); |
|
| 433 | - \OCP\Util::addScript('files', 'filemultiselectmenu'); |
|
| 434 | - \OCP\Util::addScript('files', 'filelist'); |
|
| 435 | - \OCP\Util::addScript('files', 'keyboardshortcuts'); |
|
| 436 | - \OCP\Util::addScript('files', 'operationprogressbar'); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - // OpenGraph Support: http://ogp.me/ |
|
| 440 | - \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]); |
|
| 441 | - \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]); |
|
| 442 | - \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
|
| 443 | - \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
|
| 444 | - \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
|
| 445 | - \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]); |
|
| 446 | - |
|
| 447 | - $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts'); |
|
| 448 | - |
|
| 449 | - $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
|
| 450 | - $csp->addAllowedFrameDomain('\'self\''); |
|
| 451 | - |
|
| 452 | - $response = new PublicTemplateResponse($this->appName, 'public', $shareTmpl); |
|
| 453 | - $response->setHeaderTitle($shareTmpl['filename']); |
|
| 454 | - $response->setHeaderDetails($this->l10n->t('shared by %s', [$shareTmpl['displayName']])); |
|
| 455 | - |
|
| 456 | - $isNoneFileDropFolder = $shareIsFolder === false || $share->getPermissions() !== \OCP\Constants::PERMISSION_CREATE; |
|
| 457 | - |
|
| 458 | - if ($isNoneFileDropFolder && !$share->getHideDownload()) { |
|
| 459 | - \OCP\Util::addScript('files_sharing', 'public_note'); |
|
| 460 | - |
|
| 461 | - $downloadWhite = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $shareTmpl['downloadURL'], 0); |
|
| 462 | - $downloadAllWhite = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download-white', $shareTmpl['downloadURL'], 0); |
|
| 463 | - $download = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']); |
|
| 464 | - $downloadAll = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']); |
|
| 465 | - $directLink = new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $shareTmpl['previewURL']); |
|
| 466 | - $externalShare = new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', $shareTmpl['owner'], $shareTmpl['displayName'], $shareTmpl['filename']); |
|
| 467 | - |
|
| 468 | - $responseComposer = []; |
|
| 469 | - |
|
| 470 | - if ($shareIsFolder) { |
|
| 471 | - $responseComposer[] = $downloadAllWhite; |
|
| 472 | - $responseComposer[] = $downloadAll; |
|
| 473 | - } else { |
|
| 474 | - $responseComposer[] = $downloadWhite; |
|
| 475 | - $responseComposer[] = $download; |
|
| 476 | - } |
|
| 477 | - $responseComposer[] = $directLink; |
|
| 478 | - if ($this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) { |
|
| 479 | - $responseComposer[] = $externalShare; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - $response->setHeaderActions($responseComposer); |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - $response->setContentSecurityPolicy($csp); |
|
| 486 | - |
|
| 487 | - $this->emitAccessShareHook($share); |
|
| 488 | - |
|
| 489 | - return $response; |
|
| 490 | - } |
|
| 491 | - |
|
| 492 | - /** |
|
| 493 | - * @PublicPage |
|
| 494 | - * @NoCSRFRequired |
|
| 495 | - * |
|
| 496 | - * @param string $token |
|
| 497 | - * @param string $files |
|
| 498 | - * @param string $path |
|
| 499 | - * @param string $downloadStartSecret |
|
| 500 | - * @return void|\OCP\AppFramework\Http\Response |
|
| 501 | - * @throws NotFoundException |
|
| 502 | - */ |
|
| 503 | - public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { |
|
| 504 | - \OC_User::setIncognitoMode(true); |
|
| 505 | - |
|
| 506 | - $share = $this->shareManager->getShareByToken($token); |
|
| 507 | - |
|
| 508 | - if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 509 | - return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
|
| 510 | - } |
|
| 511 | - |
|
| 512 | - $files_list = null; |
|
| 513 | - if (!is_null($files)) { // download selected files |
|
| 514 | - $files_list = json_decode($files); |
|
| 515 | - // in case we get only a single file |
|
| 516 | - if ($files_list === null) { |
|
| 517 | - $files_list = [$files]; |
|
| 518 | - } |
|
| 519 | - // Just in case $files is a single int like '1234' |
|
| 520 | - if (!is_array($files_list)) { |
|
| 521 | - $files_list = [$files_list]; |
|
| 522 | - } |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - |
|
| 526 | - if (!$this->validateShare($share)) { |
|
| 527 | - throw new NotFoundException(); |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 531 | - $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
| 532 | - |
|
| 533 | - |
|
| 534 | - // Single file share |
|
| 535 | - if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 536 | - // Single file download |
|
| 537 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 538 | - } |
|
| 539 | - // Directory share |
|
| 540 | - else { |
|
| 541 | - /** @var \OCP\Files\Folder $node */ |
|
| 542 | - $node = $share->getNode(); |
|
| 543 | - |
|
| 544 | - // Try to get the path |
|
| 545 | - if ($path !== '') { |
|
| 546 | - try { |
|
| 547 | - $node = $node->get($path); |
|
| 548 | - } catch (NotFoundException $e) { |
|
| 549 | - $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 550 | - return new NotFoundResponse(); |
|
| 551 | - } |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - $originalSharePath = $userFolder->getRelativePath($node->getPath()); |
|
| 555 | - |
|
| 556 | - if ($node instanceof \OCP\Files\File) { |
|
| 557 | - // Single file download |
|
| 558 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 559 | - } else if (!empty($files_list)) { |
|
| 560 | - $this->fileListDownloaded($share, $files_list, $node); |
|
| 561 | - } else { |
|
| 562 | - // The folder is downloaded |
|
| 563 | - $this->singleFileDownloaded($share, $share->getNode()); |
|
| 564 | - } |
|
| 565 | - } |
|
| 566 | - |
|
| 567 | - /* FIXME: We should do this all nicely in OCP */ |
|
| 568 | - OC_Util::tearDownFS(); |
|
| 569 | - OC_Util::setupFS($share->getShareOwner()); |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * this sets a cookie to be able to recognize the start of the download |
|
| 573 | - * the content must not be longer than 32 characters and must only contain |
|
| 574 | - * alphanumeric characters |
|
| 575 | - */ |
|
| 576 | - if (!empty($downloadStartSecret) |
|
| 577 | - && !isset($downloadStartSecret[32]) |
|
| 578 | - && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) { |
|
| 579 | - |
|
| 580 | - // FIXME: set on the response once we use an actual app framework response |
|
| 581 | - setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/'); |
|
| 582 | - } |
|
| 583 | - |
|
| 584 | - $this->emitAccessShareHook($share); |
|
| 585 | - |
|
| 586 | - $server_params = array( 'head' => $this->request->getMethod() === 'HEAD' ); |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * Http range requests support |
|
| 590 | - */ |
|
| 591 | - if (isset($_SERVER['HTTP_RANGE'])) { |
|
| 592 | - $server_params['range'] = $this->request->getHeader('Range'); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - // download selected files |
|
| 596 | - if (!is_null($files) && $files !== '') { |
|
| 597 | - // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 598 | - // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 599 | - OC_Files::get($originalSharePath, $files_list, $server_params); |
|
| 600 | - exit(); |
|
| 601 | - } else { |
|
| 602 | - // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 603 | - // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 604 | - OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params); |
|
| 605 | - exit(); |
|
| 606 | - } |
|
| 607 | - } |
|
| 608 | - |
|
| 609 | - /** |
|
| 610 | - * create activity for every downloaded file |
|
| 611 | - * |
|
| 612 | - * @param Share\IShare $share |
|
| 613 | - * @param array $files_list |
|
| 614 | - * @param \OCP\Files\Folder $node |
|
| 615 | - */ |
|
| 616 | - protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) { |
|
| 617 | - foreach ($files_list as $file) { |
|
| 618 | - $subNode = $node->get($file); |
|
| 619 | - $this->singleFileDownloaded($share, $subNode); |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * create activity if a single file was downloaded from a link share |
|
| 626 | - * |
|
| 627 | - * @param Share\IShare $share |
|
| 628 | - */ |
|
| 629 | - protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) { |
|
| 630 | - |
|
| 631 | - $fileId = $node->getId(); |
|
| 632 | - |
|
| 633 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 634 | - $userNodeList = $userFolder->getById($fileId); |
|
| 635 | - $userNode = $userNodeList[0]; |
|
| 636 | - $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 637 | - $userPath = $userFolder->getRelativePath($userNode->getPath()); |
|
| 638 | - $ownerPath = $ownerFolder->getRelativePath($node->getPath()); |
|
| 639 | - |
|
| 640 | - $parameters = [$userPath]; |
|
| 641 | - |
|
| 642 | - if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 643 | - if ($node instanceof \OCP\Files\File) { |
|
| 644 | - $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED; |
|
| 645 | - } else { |
|
| 646 | - $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED; |
|
| 647 | - } |
|
| 648 | - $parameters[] = $share->getSharedWith(); |
|
| 649 | - } else { |
|
| 650 | - if ($node instanceof \OCP\Files\File) { |
|
| 651 | - $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; |
|
| 652 | - } else { |
|
| 653 | - $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED; |
|
| 654 | - } |
|
| 655 | - } |
|
| 656 | - |
|
| 657 | - $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath); |
|
| 658 | - |
|
| 659 | - if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 660 | - $parameters[0] = $ownerPath; |
|
| 661 | - $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath); |
|
| 662 | - } |
|
| 663 | - } |
|
| 664 | - |
|
| 665 | - /** |
|
| 666 | - * publish activity |
|
| 667 | - * |
|
| 668 | - * @param string $subject |
|
| 669 | - * @param array $parameters |
|
| 670 | - * @param string $affectedUser |
|
| 671 | - * @param int $fileId |
|
| 672 | - * @param string $filePath |
|
| 673 | - */ |
|
| 674 | - protected function publishActivity($subject, |
|
| 675 | - array $parameters, |
|
| 676 | - $affectedUser, |
|
| 677 | - $fileId, |
|
| 678 | - $filePath) { |
|
| 679 | - |
|
| 680 | - $event = $this->activityManager->generateEvent(); |
|
| 681 | - $event->setApp('files_sharing') |
|
| 682 | - ->setType('public_links') |
|
| 683 | - ->setSubject($subject, $parameters) |
|
| 684 | - ->setAffectedUser($affectedUser) |
|
| 685 | - ->setObject('files', $fileId, $filePath); |
|
| 686 | - $this->activityManager->publish($event); |
|
| 687 | - } |
|
| 344 | + $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath()); |
|
| 345 | + if ($freeSpace < \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 346 | + $freeSpace = max($freeSpace, 0); |
|
| 347 | + } else { |
|
| 348 | + $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188 |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + $hideFileList = !($share->getPermissions() & \OCP\Constants::PERMISSION_READ); |
|
| 352 | + $maxUploadFilesize = $freeSpace; |
|
| 353 | + |
|
| 354 | + $folder = new Template('files', 'list', ''); |
|
| 355 | + $folder->assign('dir', $shareNode->getRelativePath($folderNode->getPath())); |
|
| 356 | + $folder->assign('dirToken', $this->getToken()); |
|
| 357 | + $folder->assign('permissions', \OCP\Constants::PERMISSION_READ); |
|
| 358 | + $folder->assign('isPublic', true); |
|
| 359 | + $folder->assign('hideFileList', $hideFileList); |
|
| 360 | + $folder->assign('publicUploadEnabled', 'no'); |
|
| 361 | + // default to list view |
|
| 362 | + $folder->assign('showgridview', false); |
|
| 363 | + $folder->assign('uploadMaxFilesize', $maxUploadFilesize); |
|
| 364 | + $folder->assign('uploadMaxHumanFilesize', \OCP\Util::humanFileSize($maxUploadFilesize)); |
|
| 365 | + $folder->assign('freeSpace', $freeSpace); |
|
| 366 | + $folder->assign('usedSpacePercent', 0); |
|
| 367 | + $folder->assign('trash', false); |
|
| 368 | + $shareTmpl['folder'] = $folder->fetchPage(); |
|
| 369 | + } else { |
|
| 370 | + $shareIsFolder = false; |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + // default to list view |
|
| 374 | + $shareTmpl['showgridview'] = false; |
|
| 375 | + |
|
| 376 | + $shareTmpl['hideFileList'] = $hideFileList; |
|
| 377 | + $shareTmpl['shareOwner'] = $this->userManager->get($share->getShareOwner())->getDisplayName(); |
|
| 378 | + $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', ['token' => $this->getToken()]); |
|
| 379 | + $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $this->getToken()]); |
|
| 380 | + $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10); |
|
| 381 | + $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true); |
|
| 382 | + $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024); |
|
| 383 | + $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024); |
|
| 384 | + $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', null); |
|
| 385 | + $shareTmpl['previewURL'] = $shareTmpl['downloadURL']; |
|
| 386 | + |
|
| 387 | + if ($shareTmpl['previewSupported']) { |
|
| 388 | + $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute( 'files_sharing.PublicPreview.getPreview', |
|
| 389 | + ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 'token' => $shareTmpl['dirToken']]); |
|
| 390 | + $ogPreview = $shareTmpl['previewImage']; |
|
| 391 | + |
|
| 392 | + // We just have direct previews for image files |
|
| 393 | + if ($shareNode->getMimePart() === 'image') { |
|
| 394 | + $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $this->getToken()]); |
|
| 395 | + |
|
| 396 | + $ogPreview = $shareTmpl['previewURL']; |
|
| 397 | + |
|
| 398 | + //Whatapp is kind of picky about their size requirements |
|
| 399 | + if ($this->request->isUserAgent(['/^WhatsApp/'])) { |
|
| 400 | + $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [ |
|
| 401 | + 'token' => $this->getToken(), |
|
| 402 | + 'x' => 256, |
|
| 403 | + 'y' => 256, |
|
| 404 | + 'a' => true, |
|
| 405 | + ]); |
|
| 406 | + } |
|
| 407 | + } |
|
| 408 | + } else { |
|
| 409 | + $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 410 | + $ogPreview = $shareTmpl['previewImage']; |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + // Load files we need |
|
| 414 | + \OCP\Util::addScript('files', 'file-upload'); |
|
| 415 | + \OCP\Util::addStyle('files_sharing', 'publicView'); |
|
| 416 | + \OCP\Util::addScript('files_sharing', 'public'); |
|
| 417 | + \OCP\Util::addScript('files_sharing', 'templates'); |
|
| 418 | + \OCP\Util::addScript('files', 'fileactions'); |
|
| 419 | + \OCP\Util::addScript('files', 'fileactionsmenu'); |
|
| 420 | + \OCP\Util::addScript('files', 'jquery.fileupload'); |
|
| 421 | + \OCP\Util::addScript('files_sharing', 'files_drop'); |
|
| 422 | + |
|
| 423 | + if (isset($shareTmpl['folder'])) { |
|
| 424 | + // JS required for folders |
|
| 425 | + \OCP\Util::addStyle('files', 'merged'); |
|
| 426 | + \OCP\Util::addScript('files', 'filesummary'); |
|
| 427 | + \OCP\Util::addScript('files', 'templates'); |
|
| 428 | + \OCP\Util::addScript('files', 'breadcrumb'); |
|
| 429 | + \OCP\Util::addScript('files', 'fileinfomodel'); |
|
| 430 | + \OCP\Util::addScript('files', 'newfilemenu'); |
|
| 431 | + \OCP\Util::addScript('files', 'files'); |
|
| 432 | + \OCP\Util::addScript('files', 'filemultiselectmenu'); |
|
| 433 | + \OCP\Util::addScript('files', 'filelist'); |
|
| 434 | + \OCP\Util::addScript('files', 'keyboardshortcuts'); |
|
| 435 | + \OCP\Util::addScript('files', 'operationprogressbar'); |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + // OpenGraph Support: http://ogp.me/ |
|
| 439 | + \OCP\Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]); |
|
| 440 | + \OCP\Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]); |
|
| 441 | + \OCP\Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]); |
|
| 442 | + \OCP\Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]); |
|
| 443 | + \OCP\Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]); |
|
| 444 | + \OCP\Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]); |
|
| 445 | + |
|
| 446 | + $this->eventDispatcher->dispatch('OCA\Files_Sharing::loadAdditionalScripts'); |
|
| 447 | + |
|
| 448 | + $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); |
|
| 449 | + $csp->addAllowedFrameDomain('\'self\''); |
|
| 450 | + |
|
| 451 | + $response = new PublicTemplateResponse($this->appName, 'public', $shareTmpl); |
|
| 452 | + $response->setHeaderTitle($shareTmpl['filename']); |
|
| 453 | + $response->setHeaderDetails($this->l10n->t('shared by %s', [$shareTmpl['displayName']])); |
|
| 454 | + |
|
| 455 | + $isNoneFileDropFolder = $shareIsFolder === false || $share->getPermissions() !== \OCP\Constants::PERMISSION_CREATE; |
|
| 456 | + |
|
| 457 | + if ($isNoneFileDropFolder && !$share->getHideDownload()) { |
|
| 458 | + \OCP\Util::addScript('files_sharing', 'public_note'); |
|
| 459 | + |
|
| 460 | + $downloadWhite = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $shareTmpl['downloadURL'], 0); |
|
| 461 | + $downloadAllWhite = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download-white', $shareTmpl['downloadURL'], 0); |
|
| 462 | + $download = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']); |
|
| 463 | + $downloadAll = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']); |
|
| 464 | + $directLink = new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $shareTmpl['previewURL']); |
|
| 465 | + $externalShare = new ExternalShareMenuAction($this->l10n->t('Add to your Nextcloud'), 'icon-external', $shareTmpl['owner'], $shareTmpl['displayName'], $shareTmpl['filename']); |
|
| 466 | + |
|
| 467 | + $responseComposer = []; |
|
| 468 | + |
|
| 469 | + if ($shareIsFolder) { |
|
| 470 | + $responseComposer[] = $downloadAllWhite; |
|
| 471 | + $responseComposer[] = $downloadAll; |
|
| 472 | + } else { |
|
| 473 | + $responseComposer[] = $downloadWhite; |
|
| 474 | + $responseComposer[] = $download; |
|
| 475 | + } |
|
| 476 | + $responseComposer[] = $directLink; |
|
| 477 | + if ($this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) { |
|
| 478 | + $responseComposer[] = $externalShare; |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + $response->setHeaderActions($responseComposer); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + $response->setContentSecurityPolicy($csp); |
|
| 485 | + |
|
| 486 | + $this->emitAccessShareHook($share); |
|
| 487 | + |
|
| 488 | + return $response; |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + /** |
|
| 492 | + * @PublicPage |
|
| 493 | + * @NoCSRFRequired |
|
| 494 | + * |
|
| 495 | + * @param string $token |
|
| 496 | + * @param string $files |
|
| 497 | + * @param string $path |
|
| 498 | + * @param string $downloadStartSecret |
|
| 499 | + * @return void|\OCP\AppFramework\Http\Response |
|
| 500 | + * @throws NotFoundException |
|
| 501 | + */ |
|
| 502 | + public function downloadShare($token, $files = null, $path = '', $downloadStartSecret = '') { |
|
| 503 | + \OC_User::setIncognitoMode(true); |
|
| 504 | + |
|
| 505 | + $share = $this->shareManager->getShareByToken($token); |
|
| 506 | + |
|
| 507 | + if(!($share->getPermissions() & \OCP\Constants::PERMISSION_READ)) { |
|
| 508 | + return new \OCP\AppFramework\Http\DataResponse('Share is read-only'); |
|
| 509 | + } |
|
| 510 | + |
|
| 511 | + $files_list = null; |
|
| 512 | + if (!is_null($files)) { // download selected files |
|
| 513 | + $files_list = json_decode($files); |
|
| 514 | + // in case we get only a single file |
|
| 515 | + if ($files_list === null) { |
|
| 516 | + $files_list = [$files]; |
|
| 517 | + } |
|
| 518 | + // Just in case $files is a single int like '1234' |
|
| 519 | + if (!is_array($files_list)) { |
|
| 520 | + $files_list = [$files_list]; |
|
| 521 | + } |
|
| 522 | + } |
|
| 523 | + |
|
| 524 | + |
|
| 525 | + if (!$this->validateShare($share)) { |
|
| 526 | + throw new NotFoundException(); |
|
| 527 | + } |
|
| 528 | + |
|
| 529 | + $userFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 530 | + $originalSharePath = $userFolder->getRelativePath($share->getNode()->getPath()); |
|
| 531 | + |
|
| 532 | + |
|
| 533 | + // Single file share |
|
| 534 | + if ($share->getNode() instanceof \OCP\Files\File) { |
|
| 535 | + // Single file download |
|
| 536 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 537 | + } |
|
| 538 | + // Directory share |
|
| 539 | + else { |
|
| 540 | + /** @var \OCP\Files\Folder $node */ |
|
| 541 | + $node = $share->getNode(); |
|
| 542 | + |
|
| 543 | + // Try to get the path |
|
| 544 | + if ($path !== '') { |
|
| 545 | + try { |
|
| 546 | + $node = $node->get($path); |
|
| 547 | + } catch (NotFoundException $e) { |
|
| 548 | + $this->emitAccessShareHook($share, 404, 'Share not found'); |
|
| 549 | + return new NotFoundResponse(); |
|
| 550 | + } |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + $originalSharePath = $userFolder->getRelativePath($node->getPath()); |
|
| 554 | + |
|
| 555 | + if ($node instanceof \OCP\Files\File) { |
|
| 556 | + // Single file download |
|
| 557 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 558 | + } else if (!empty($files_list)) { |
|
| 559 | + $this->fileListDownloaded($share, $files_list, $node); |
|
| 560 | + } else { |
|
| 561 | + // The folder is downloaded |
|
| 562 | + $this->singleFileDownloaded($share, $share->getNode()); |
|
| 563 | + } |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /* FIXME: We should do this all nicely in OCP */ |
|
| 567 | + OC_Util::tearDownFS(); |
|
| 568 | + OC_Util::setupFS($share->getShareOwner()); |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * this sets a cookie to be able to recognize the start of the download |
|
| 572 | + * the content must not be longer than 32 characters and must only contain |
|
| 573 | + * alphanumeric characters |
|
| 574 | + */ |
|
| 575 | + if (!empty($downloadStartSecret) |
|
| 576 | + && !isset($downloadStartSecret[32]) |
|
| 577 | + && preg_match('!^[a-zA-Z0-9]+$!', $downloadStartSecret) === 1) { |
|
| 578 | + |
|
| 579 | + // FIXME: set on the response once we use an actual app framework response |
|
| 580 | + setcookie('ocDownloadStarted', $downloadStartSecret, time() + 20, '/'); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + $this->emitAccessShareHook($share); |
|
| 584 | + |
|
| 585 | + $server_params = array( 'head' => $this->request->getMethod() === 'HEAD' ); |
|
| 586 | + |
|
| 587 | + /** |
|
| 588 | + * Http range requests support |
|
| 589 | + */ |
|
| 590 | + if (isset($_SERVER['HTTP_RANGE'])) { |
|
| 591 | + $server_params['range'] = $this->request->getHeader('Range'); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + // download selected files |
|
| 595 | + if (!is_null($files) && $files !== '') { |
|
| 596 | + // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 597 | + // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 598 | + OC_Files::get($originalSharePath, $files_list, $server_params); |
|
| 599 | + exit(); |
|
| 600 | + } else { |
|
| 601 | + // FIXME: The exit is required here because otherwise the AppFramework is trying to add headers as well |
|
| 602 | + // after dispatching the request which results in a "Cannot modify header information" notice. |
|
| 603 | + OC_Files::get(dirname($originalSharePath), basename($originalSharePath), $server_params); |
|
| 604 | + exit(); |
|
| 605 | + } |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + /** |
|
| 609 | + * create activity for every downloaded file |
|
| 610 | + * |
|
| 611 | + * @param Share\IShare $share |
|
| 612 | + * @param array $files_list |
|
| 613 | + * @param \OCP\Files\Folder $node |
|
| 614 | + */ |
|
| 615 | + protected function fileListDownloaded(Share\IShare $share, array $files_list, \OCP\Files\Folder $node) { |
|
| 616 | + foreach ($files_list as $file) { |
|
| 617 | + $subNode = $node->get($file); |
|
| 618 | + $this->singleFileDownloaded($share, $subNode); |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + /** |
|
| 624 | + * create activity if a single file was downloaded from a link share |
|
| 625 | + * |
|
| 626 | + * @param Share\IShare $share |
|
| 627 | + */ |
|
| 628 | + protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $node) { |
|
| 629 | + |
|
| 630 | + $fileId = $node->getId(); |
|
| 631 | + |
|
| 632 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 633 | + $userNodeList = $userFolder->getById($fileId); |
|
| 634 | + $userNode = $userNodeList[0]; |
|
| 635 | + $ownerFolder = $this->rootFolder->getUserFolder($share->getShareOwner()); |
|
| 636 | + $userPath = $userFolder->getRelativePath($userNode->getPath()); |
|
| 637 | + $ownerPath = $ownerFolder->getRelativePath($node->getPath()); |
|
| 638 | + |
|
| 639 | + $parameters = [$userPath]; |
|
| 640 | + |
|
| 641 | + if ($share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL) { |
|
| 642 | + if ($node instanceof \OCP\Files\File) { |
|
| 643 | + $subject = Downloads::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED; |
|
| 644 | + } else { |
|
| 645 | + $subject = Downloads::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED; |
|
| 646 | + } |
|
| 647 | + $parameters[] = $share->getSharedWith(); |
|
| 648 | + } else { |
|
| 649 | + if ($node instanceof \OCP\Files\File) { |
|
| 650 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED; |
|
| 651 | + } else { |
|
| 652 | + $subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED; |
|
| 653 | + } |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + $this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath); |
|
| 657 | + |
|
| 658 | + if ($share->getShareOwner() !== $share->getSharedBy()) { |
|
| 659 | + $parameters[0] = $ownerPath; |
|
| 660 | + $this->publishActivity($subject, $parameters, $share->getShareOwner(), $fileId, $ownerPath); |
|
| 661 | + } |
|
| 662 | + } |
|
| 663 | + |
|
| 664 | + /** |
|
| 665 | + * publish activity |
|
| 666 | + * |
|
| 667 | + * @param string $subject |
|
| 668 | + * @param array $parameters |
|
| 669 | + * @param string $affectedUser |
|
| 670 | + * @param int $fileId |
|
| 671 | + * @param string $filePath |
|
| 672 | + */ |
|
| 673 | + protected function publishActivity($subject, |
|
| 674 | + array $parameters, |
|
| 675 | + $affectedUser, |
|
| 676 | + $fileId, |
|
| 677 | + $filePath) { |
|
| 678 | + |
|
| 679 | + $event = $this->activityManager->generateEvent(); |
|
| 680 | + $event->setApp('files_sharing') |
|
| 681 | + ->setType('public_links') |
|
| 682 | + ->setSubject($subject, $parameters) |
|
| 683 | + ->setAffectedUser($affectedUser) |
|
| 684 | + ->setObject('files', $fileId, $filePath); |
|
| 685 | + $this->activityManager->publish($event); |
|
| 686 | + } |
|
| 688 | 687 | |
| 689 | 688 | |
| 690 | 689 | } |