DefaultPublicShareTemplateProvider.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Files_Sharing;
  8. use OCA\FederatedFileSharing\FederatedShareProvider;
  9. use OCA\Files_Sharing\AppInfo\Application;
  10. use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
  11. use OCA\Viewer\Event\LoadViewer;
  12. use OCP\Accounts\IAccountManager;
  13. use OCP\AppFramework\Http\ContentSecurityPolicy;
  14. use OCP\AppFramework\Http\Template\ExternalShareMenuAction;
  15. use OCP\AppFramework\Http\Template\LinkMenuAction;
  16. use OCP\AppFramework\Http\Template\PublicTemplateResponse;
  17. use OCP\AppFramework\Http\Template\SimpleMenuAction;
  18. use OCP\AppFramework\Http\TemplateResponse;
  19. use OCP\AppFramework\Services\IInitialState;
  20. use OCP\Defaults;
  21. use OCP\EventDispatcher\IEventDispatcher;
  22. use OCP\Files\File;
  23. use OCP\IAppConfig;
  24. use OCP\IConfig;
  25. use OCP\IL10N;
  26. use OCP\IPreview;
  27. use OCP\IRequest;
  28. use OCP\IURLGenerator;
  29. use OCP\IUser;
  30. use OCP\IUserManager;
  31. use OCP\Share\IPublicShareTemplateProvider;
  32. use OCP\Share\IShare;
  33. use OCP\Util;
  34. class DefaultPublicShareTemplateProvider implements IPublicShareTemplateProvider {
  35. public function __construct(
  36. private IUserManager $userManager,
  37. private IAccountManager $accountManager,
  38. private IPreview $previewManager,
  39. protected FederatedShareProvider $federatedShareProvider,
  40. private IUrlGenerator $urlGenerator,
  41. private IEventDispatcher $eventDispatcher,
  42. private IL10N $l10n,
  43. private Defaults $defaults,
  44. private IConfig $config,
  45. private IRequest $request,
  46. private IInitialState $initialState,
  47. private IAppConfig $appConfig,
  48. ) {
  49. }
  50. public function shouldRespond(IShare $share): bool {
  51. return true;
  52. }
  53. public function renderPage(IShare $share, string $token, string $path): TemplateResponse {
  54. $shareNode = $share->getNode();
  55. $ownerName = '';
  56. $ownerId = '';
  57. // Only make the share owner public if they allowed to show their name
  58. $owner = $this->userManager->get($share->getShareOwner());
  59. if ($owner instanceof IUser) {
  60. $ownerAccount = $this->accountManager->getAccount($owner);
  61. $ownerNameProperty = $ownerAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
  62. if ($ownerNameProperty->getScope() === IAccountManager::SCOPE_PUBLISHED) {
  63. $ownerName = $owner->getDisplayName();
  64. $ownerId = $owner->getUID();
  65. }
  66. }
  67. $view = 'public-share';
  68. if ($shareNode instanceof File) {
  69. $view = 'public-file-share';
  70. $this->initialState->provideInitialState('fileId', $shareNode->getId());
  71. } elseif (($share->getPermissions() & \OCP\Constants::PERMISSION_CREATE)
  72. && !($share->getPermissions() & \OCP\Constants::PERMISSION_READ)
  73. ) {
  74. // share is a folder with create but no read permissions -> file drop only
  75. $view = 'public-file-drop';
  76. // Only needed for file drops
  77. $this->initialState->provideInitialState(
  78. 'disclaimer',
  79. $this->appConfig->getValueString('core', 'shareapi_public_link_disclaimertext'),
  80. );
  81. }
  82. // Set up initial state
  83. $this->initialState->provideInitialState('isPublic', true);
  84. $this->initialState->provideInitialState('sharingToken', $token);
  85. $this->initialState->provideInitialState('sharePermissions', $share->getPermissions());
  86. $this->initialState->provideInitialState('filename', $shareNode->getName());
  87. $this->initialState->provideInitialState('view', $view);
  88. // Load scripts and styles for UI
  89. \OCP\Util::addInitScript('files', 'init');
  90. \OCP\Util::addInitScript(Application::APP_ID, 'init');
  91. \OCP\Util::addInitScript(Application::APP_ID, 'init-public');
  92. \OCP\Util::addScript('files', 'main');
  93. // Add file-request script if needed
  94. $attributes = $share->getAttributes();
  95. $isFileRequest = $attributes?->getAttribute('fileRequest', 'enabled') === true;
  96. if ($isFileRequest) {
  97. Util::addScript(Application::APP_ID, 'public-file-request');
  98. }
  99. // Load Viewer scripts
  100. if (class_exists(LoadViewer::class)) {
  101. $this->eventDispatcher->dispatchTyped(new LoadViewer());
  102. }
  103. // Allow external apps to register their scripts
  104. $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($share));
  105. // OpenGraph Support: http://ogp.me/
  106. $this->addOpenGraphHeaders($share);
  107. // CSP to allow office
  108. $csp = new ContentSecurityPolicy();
  109. $csp->addAllowedFrameDomain('\'self\'');
  110. $response = new PublicTemplateResponse(
  111. 'files',
  112. 'index',
  113. );
  114. $response->setContentSecurityPolicy($csp);
  115. // If the share has a label, use it as the title
  116. if ($share->getLabel() !== '') {
  117. $response->setHeaderTitle($share->getLabel());
  118. } else {
  119. $response->setHeaderTitle($shareNode->getName());
  120. }
  121. if ($ownerName !== '') {
  122. $response->setHeaderDetails($this->l10n->t('shared by %s', [$ownerName]));
  123. }
  124. // Create the header action menu
  125. $headerActions = [];
  126. if ($view !== 'public-file-drop') {
  127. // The download URL is used for the "download" header action as well as in some cases for the direct link
  128. $downloadUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', [
  129. 'token' => $token,
  130. 'filename' => ($shareNode instanceof File) ? $shareNode->getName() : null,
  131. ]);
  132. // If not a file drop, then add the download header action
  133. $headerActions[] = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $downloadUrl, 0, (string)$shareNode->getSize());
  134. // If remote sharing is enabled also add the remote share action to the menu
  135. if ($this->federatedShareProvider->isOutgoingServer2serverShareEnabled() && !$share->getHideDownload()) {
  136. $headerActions[] = new ExternalShareMenuAction(
  137. // TRANSLATORS The placeholder refers to the software product name as in 'Add to your Nextcloud'
  138. $this->l10n->t('Add to your %s', [$this->defaults->getProductName()]),
  139. 'icon-external',
  140. $ownerId,
  141. $ownerName,
  142. $shareNode->getName(),
  143. );
  144. }
  145. }
  146. $shareUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]);
  147. // By default use the share link as the direct link
  148. $directLink = $shareUrl;
  149. // Add the direct link header actions
  150. if ($shareNode->getMimePart() === 'image') {
  151. // If this is a file and especially an image directly point to the image preview
  152. $directLink = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]);
  153. } elseif (($share->getPermissions() & \OCP\Constants::PERMISSION_READ) && !$share->getHideDownload()) {
  154. // Can read and no download restriction, so just download it
  155. $directLink = $downloadUrl ?? $shareUrl;
  156. }
  157. $headerActions[] = new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $directLink);
  158. $response->setHeaderActions($headerActions);
  159. return $response;
  160. }
  161. /**
  162. * Add OpenGraph headers to response for preview
  163. * @param IShare $share The share for which to add the headers
  164. */
  165. protected function addOpenGraphHeaders(IShare $share): void {
  166. $shareNode = $share->getNode();
  167. $token = $share->getToken();
  168. $shareUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]);
  169. // Handle preview generation for OpenGraph
  170. if ($this->previewManager->isMimeSupported($shareNode->getMimetype())) {
  171. // For images we can use direct links
  172. if ($shareNode->getMimePart() === 'image') {
  173. $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]);
  174. // Whatsapp is kind of picky about their size requirements
  175. if ($this->request->isUserAgent(['/^WhatsApp/'])) {
  176. $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [
  177. 'token' => $token,
  178. 'x' => 256,
  179. 'y' => 256,
  180. 'a' => true,
  181. ]);
  182. }
  183. } else {
  184. // For normal files use preview API
  185. $ogPreview = $this->urlGenerator->linkToRouteAbsolute(
  186. 'files_sharing.PublicPreview.getPreview',
  187. [
  188. 'x' => 256,
  189. 'y' => 256,
  190. 'file' => $share->getTarget(),
  191. 'token' => $token,
  192. ],
  193. );
  194. }
  195. } else {
  196. // No preview supported, so we just add the favicon
  197. $ogPreview = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png'));
  198. }
  199. Util::addHeader('meta', ['property' => 'og:title', 'content' => $shareNode->getName()]);
  200. Util::addHeader('meta', ['property' => 'og:description', 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]);
  201. Util::addHeader('meta', ['property' => 'og:site_name', 'content' => $this->defaults->getName()]);
  202. Util::addHeader('meta', ['property' => 'og:url', 'content' => $shareUrl]);
  203. Util::addHeader('meta', ['property' => 'og:type', 'content' => 'object']);
  204. Util::addHeader('meta', ['property' => 'og:image', 'content' => $ogPreview]);
  205. }
  206. }