DefaultPublicShareTemplateProvider.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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\Constants;
  21. use OCP\Defaults;
  22. use OCP\EventDispatcher\IEventDispatcher;
  23. use OCP\Files\FileInfo;
  24. use OCP\Files\Folder;
  25. use OCP\IConfig;
  26. use OCP\IL10N;
  27. use OCP\IPreview;
  28. use OCP\IRequest;
  29. use OCP\IURLGenerator;
  30. use OCP\IUser;
  31. use OCP\IUserManager;
  32. use OCP\Share\IPublicShareTemplateProvider;
  33. use OCP\Share\IShare;
  34. use OCP\Template;
  35. use OCP\Util;
  36. class DefaultPublicShareTemplateProvider implements IPublicShareTemplateProvider {
  37. public function __construct(
  38. private IUserManager $userManager,
  39. private IAccountManager $accountManager,
  40. private IPreview $previewManager,
  41. protected FederatedShareProvider $federatedShareProvider,
  42. private IUrlGenerator $urlGenerator,
  43. private IEventDispatcher $eventDispatcher,
  44. private IL10N $l10n,
  45. private Defaults $defaults,
  46. private IConfig $config,
  47. private IRequest $request,
  48. private IInitialState $initialState,
  49. ) {
  50. }
  51. public function shouldRespond(IShare $share): bool {
  52. return true;
  53. }
  54. public function renderPage(IShare $share, string $token, string $path): TemplateResponse {
  55. $shareNode = $share->getNode();
  56. $shareTmpl = [];
  57. $shareTmpl['owner'] = '';
  58. $shareTmpl['shareOwner'] = '';
  59. $owner = $this->userManager->get($share->getShareOwner());
  60. if ($owner instanceof IUser) {
  61. $ownerAccount = $this->accountManager->getAccount($owner);
  62. $ownerName = $ownerAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
  63. if ($ownerName->getScope() === IAccountManager::SCOPE_PUBLISHED) {
  64. $shareTmpl['owner'] = $owner->getUID();
  65. $shareTmpl['shareOwner'] = $owner->getDisplayName();
  66. $this->initialState->provideInitialState('owner', $shareTmpl['owner']);
  67. $this->initialState->provideInitialState('ownerDisplayName', $shareTmpl['shareOwner']);
  68. }
  69. }
  70. // Provide initial state
  71. $this->initialState->provideInitialState('label', $share->getLabel());
  72. $this->initialState->provideInitialState('note', $share->getNote());
  73. $this->initialState->provideInitialState('filename', $shareNode->getName());
  74. $shareTmpl['filename'] = $shareNode->getName();
  75. $shareTmpl['directory_path'] = $share->getTarget();
  76. $shareTmpl['label'] = $share->getLabel();
  77. $shareTmpl['note'] = $share->getNote();
  78. $shareTmpl['mimetype'] = $shareNode->getMimetype();
  79. $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($shareNode->getMimetype());
  80. $shareTmpl['dirToken'] = $token;
  81. $shareTmpl['sharingToken'] = $token;
  82. $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
  83. $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false';
  84. $shareTmpl['dir'] = '';
  85. $shareTmpl['nonHumanFileSize'] = $shareNode->getSize();
  86. $shareTmpl['fileSize'] = Util::humanFileSize($shareNode->getSize());
  87. $shareTmpl['hideDownload'] = $share->getHideDownload();
  88. $hideFileList = false;
  89. if ($shareNode instanceof Folder) {
  90. $shareIsFolder = true;
  91. $folderNode = $shareNode->get($path);
  92. $shareTmpl['dir'] = $shareNode->getRelativePath($folderNode->getPath());
  93. /*
  94. * The OC_Util methods require a view. This just uses the node API
  95. */
  96. $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath());
  97. if ($freeSpace < FileInfo::SPACE_UNLIMITED) {
  98. $freeSpace = (int)max($freeSpace, 0);
  99. } else {
  100. $freeSpace = (INF > 0) ? INF: PHP_INT_MAX; // work around https://bugs.php.net/bug.php?id=69188
  101. }
  102. $hideFileList = !($share->getPermissions() & Constants::PERMISSION_READ);
  103. $maxUploadFilesize = $freeSpace;
  104. $folder = new Template('files', 'list', '');
  105. $folder->assign('dir', $shareNode->getRelativePath($folderNode->getPath()));
  106. $folder->assign('dirToken', $token);
  107. $folder->assign('permissions', Constants::PERMISSION_READ);
  108. $folder->assign('isPublic', true);
  109. $folder->assign('hideFileList', $hideFileList);
  110. $folder->assign('publicUploadEnabled', 'no');
  111. // default to list view
  112. $folder->assign('showgridview', false);
  113. $folder->assign('uploadMaxFilesize', $maxUploadFilesize);
  114. $folder->assign('uploadMaxHumanFilesize', Util::humanFileSize($maxUploadFilesize));
  115. $folder->assign('freeSpace', $freeSpace);
  116. $folder->assign('usedSpacePercent', 0);
  117. $folder->assign('trash', false);
  118. $shareTmpl['folder'] = $folder->fetchPage();
  119. } else {
  120. $shareIsFolder = false;
  121. }
  122. // default to list view
  123. $shareTmpl['showgridview'] = false;
  124. $shareTmpl['hideFileList'] = $hideFileList;
  125. $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', [
  126. 'token' => $token,
  127. 'filename' => $shareIsFolder ? null : $shareNode->getName()
  128. ]);
  129. $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]);
  130. $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
  131. $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
  132. $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024);
  133. $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024);
  134. $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', '');
  135. $shareTmpl['previewURL'] = $shareTmpl['downloadURL'];
  136. if ($shareTmpl['previewSupported']) {
  137. $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview',
  138. ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 'token' => $shareTmpl['dirToken']]);
  139. $ogPreview = $shareTmpl['previewImage'];
  140. // We just have direct previews for image files
  141. if ($shareNode->getMimePart() === 'image') {
  142. $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]);
  143. $ogPreview = $shareTmpl['previewURL'];
  144. //Whatapp is kind of picky about their size requirements
  145. if ($this->request->isUserAgent(['/^WhatsApp/'])) {
  146. $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [
  147. 'token' => $token,
  148. 'x' => 256,
  149. 'y' => 256,
  150. 'a' => true,
  151. ]);
  152. }
  153. }
  154. } else {
  155. $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png'));
  156. $ogPreview = $shareTmpl['previewImage'];
  157. }
  158. // Load files we need
  159. Util::addScript('files', 'semaphore');
  160. Util::addScript('files', 'file-upload');
  161. Util::addStyle('files_sharing', 'publicView');
  162. Util::addScript('files_sharing', 'public');
  163. Util::addScript('files_sharing', 'templates');
  164. Util::addScript('files', 'fileactions');
  165. Util::addScript('files', 'fileactionsmenu');
  166. Util::addScript('files', 'jquery.fileupload');
  167. Util::addScript('files_sharing', 'files_drop');
  168. if (isset($shareTmpl['folder'])) {
  169. // JS required for folders
  170. Util::addStyle('files', 'merged');
  171. Util::addScript('files', 'filesummary');
  172. Util::addScript('files', 'templates');
  173. Util::addScript('files', 'breadcrumb');
  174. Util::addScript('files', 'fileinfomodel');
  175. Util::addScript('files', 'newfilemenu');
  176. Util::addScript('files', 'files');
  177. Util::addScript('files', 'filemultiselectmenu');
  178. Util::addScript('files', 'filelist');
  179. Util::addScript('files', 'keyboardshortcuts');
  180. Util::addScript('files', 'operationprogressbar');
  181. }
  182. // Load Viewer scripts
  183. if (class_exists(LoadViewer::class)) {
  184. $this->eventDispatcher->dispatchTyped(new LoadViewer());
  185. }
  186. // OpenGraph Support: http://ogp.me/
  187. Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]);
  188. Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]);
  189. Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]);
  190. Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]);
  191. Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]);
  192. Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]);
  193. $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($share));
  194. $csp = new ContentSecurityPolicy();
  195. $csp->addAllowedFrameDomain('\'self\'');
  196. $response = new PublicTemplateResponse(Application::APP_ID, 'public', $shareTmpl);
  197. $response->setHeaderTitle($shareTmpl['filename']);
  198. if ($shareTmpl['shareOwner'] !== '') {
  199. $response->setHeaderDetails($this->l10n->t('shared by %s', [$shareTmpl['shareOwner']]));
  200. }
  201. // If the share has a label, use it as the title
  202. if ($shareTmpl['label'] !== '') {
  203. $response->setHeaderTitle($shareTmpl['label']);
  204. }
  205. $isNoneFileDropFolder = $shareIsFolder === false || $share->getPermissions() !== Constants::PERMISSION_CREATE;
  206. if ($isNoneFileDropFolder && !$share->getHideDownload()) {
  207. Util::addScript('files_sharing', 'public_note');
  208. $downloadWhite = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $shareTmpl['downloadURL'], 0);
  209. $downloadAllWhite = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download-white', $shareTmpl['downloadURL'], 0);
  210. $download = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']);
  211. $downloadAll = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']);
  212. $directLink = new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $shareTmpl['previewURL']);
  213. // TRANSLATORS The placeholder refers to the software product name as in 'Add to your Nextcloud'
  214. $externalShare = new ExternalShareMenuAction($this->l10n->t('Add to your %s', [$this->defaults->getProductName()]), 'icon-external', $shareTmpl['owner'], $shareTmpl['shareOwner'], $shareTmpl['filename']);
  215. $responseComposer = [];
  216. if ($shareIsFolder) {
  217. $responseComposer[] = $downloadAllWhite;
  218. $responseComposer[] = $downloadAll;
  219. } else {
  220. $responseComposer[] = $downloadWhite;
  221. $responseComposer[] = $download;
  222. }
  223. $responseComposer[] = $directLink;
  224. if ($this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) {
  225. $responseComposer[] = $externalShare;
  226. }
  227. $response->setHeaderActions($responseComposer);
  228. }
  229. $response->setContentSecurityPolicy($csp);
  230. return $response;
  231. }
  232. }