1
0

DefaultPublicShareTemplateProvider.php 11 KB

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