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