DefaultPublicShareTemplateProvider.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Louis Chemineau <louis@chmn.me>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace OCA\Files_Sharing;
  23. use OCA\FederatedFileSharing\FederatedShareProvider;
  24. use OCA\Files_Sharing\AppInfo\Application;
  25. use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent;
  26. use OCA\Viewer\Event\LoadViewer;
  27. use OCP\Accounts\IAccountManager;
  28. use OCP\AppFramework\Http\ContentSecurityPolicy;
  29. use OCP\AppFramework\Http\Template\ExternalShareMenuAction;
  30. use OCP\AppFramework\Http\Template\LinkMenuAction;
  31. use OCP\AppFramework\Http\Template\PublicTemplateResponse;
  32. use OCP\AppFramework\Http\Template\SimpleMenuAction;
  33. use OCP\AppFramework\Http\TemplateResponse;
  34. use OCP\Constants;
  35. use OCP\Defaults;
  36. use OCP\EventDispatcher\IEventDispatcher;
  37. use OCP\Files\FileInfo;
  38. use OCP\Files\Folder;
  39. use OCP\Files\NotFoundException;
  40. use OCP\IConfig;
  41. use OCP\IL10N;
  42. use OCP\IPreview;
  43. use OCP\IRequest;
  44. use OCP\IURLGenerator;
  45. use OCP\IUser;
  46. use OCP\IUserManager;
  47. use OCP\Share\IShare;
  48. use OCP\Share\IPublicShareTemplateProvider;
  49. use OCP\Template;
  50. use OCP\Util;
  51. class DefaultPublicShareTemplateProvider implements IPublicShareTemplateProvider {
  52. private IUserManager $userManager;
  53. private IAccountManager $accountManager;
  54. private IPreview $previewManager;
  55. protected FederatedShareProvider $federatedShareProvider;
  56. private IURLGenerator $urlGenerator;
  57. private IEventDispatcher $eventDispatcher;
  58. private IL10N $l10n;
  59. private Defaults $defaults;
  60. private IConfig $config;
  61. private IRequest $request;
  62. public function __construct(
  63. IUserManager $userManager,
  64. IAccountManager $accountManager,
  65. IPreview $previewManager,
  66. FederatedShareProvider $federatedShareProvider,
  67. IUrlGenerator $urlGenerator,
  68. IEventDispatcher $eventDispatcher,
  69. IL10N $l10n,
  70. Defaults $defaults,
  71. IConfig $config,
  72. IRequest $request
  73. ) {
  74. $this->userManager = $userManager;
  75. $this->accountManager = $accountManager;
  76. $this->previewManager = $previewManager;
  77. $this->federatedShareProvider = $federatedShareProvider;
  78. $this->urlGenerator = $urlGenerator;
  79. $this->eventDispatcher = $eventDispatcher;
  80. $this->l10n = $l10n;
  81. $this->defaults = $defaults;
  82. $this->config = $config;
  83. $this->request = $request;
  84. }
  85. public function shouldRespond(IShare $share): bool {
  86. return true;
  87. }
  88. public function renderPage(IShare $share, string $token, string $path): TemplateResponse {
  89. $shareNode = $share->getNode();
  90. $shareTmpl = [];
  91. $shareTmpl['owner'] = '';
  92. $shareTmpl['shareOwner'] = '';
  93. $owner = $this->userManager->get($share->getShareOwner());
  94. if ($owner instanceof IUser) {
  95. $ownerAccount = $this->accountManager->getAccount($owner);
  96. $ownerName = $ownerAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME);
  97. if ($ownerName->getScope() === IAccountManager::SCOPE_PUBLISHED) {
  98. $shareTmpl['owner'] = $owner->getUID();
  99. $shareTmpl['shareOwner'] = $owner->getDisplayName();
  100. }
  101. }
  102. $shareTmpl['filename'] = $shareNode->getName();
  103. $shareTmpl['directory_path'] = $share->getTarget();
  104. $shareTmpl['note'] = $share->getNote();
  105. $shareTmpl['mimetype'] = $shareNode->getMimetype();
  106. $shareTmpl['previewSupported'] = $this->previewManager->isMimeSupported($shareNode->getMimetype());
  107. $shareTmpl['dirToken'] = $token;
  108. $shareTmpl['sharingToken'] = $token;
  109. $shareTmpl['server2serversharing'] = $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
  110. $shareTmpl['protected'] = $share->getPassword() !== null ? 'true' : 'false';
  111. $shareTmpl['dir'] = '';
  112. $shareTmpl['nonHumanFileSize'] = $shareNode->getSize();
  113. $shareTmpl['fileSize'] = Util::humanFileSize($shareNode->getSize());
  114. $shareTmpl['hideDownload'] = $share->getHideDownload();
  115. $hideFileList = false;
  116. if ($shareNode instanceof Folder) {
  117. $shareIsFolder = true;
  118. $folderNode = $shareNode->get($path);
  119. $shareTmpl['dir'] = $shareNode->getRelativePath($folderNode->getPath());
  120. /*
  121. * The OC_Util methods require a view. This just uses the node API
  122. */
  123. $freeSpace = $share->getNode()->getStorage()->free_space($share->getNode()->getInternalPath());
  124. if ($freeSpace < FileInfo::SPACE_UNLIMITED) {
  125. $freeSpace = (int)max($freeSpace, 0);
  126. } else {
  127. $freeSpace = (int)((INF > 0) ? INF: PHP_INT_MAX); // work around https://bugs.php.net/bug.php?id=69188
  128. }
  129. $hideFileList = !($share->getPermissions() & Constants::PERMISSION_READ);
  130. $maxUploadFilesize = $freeSpace;
  131. $folder = new Template('files', 'list', '');
  132. $folder->assign('dir', $shareNode->getRelativePath($folderNode->getPath()));
  133. $folder->assign('dirToken', $token);
  134. $folder->assign('permissions', Constants::PERMISSION_READ);
  135. $folder->assign('isPublic', true);
  136. $folder->assign('hideFileList', $hideFileList);
  137. $folder->assign('publicUploadEnabled', 'no');
  138. // default to list view
  139. $folder->assign('showgridview', false);
  140. $folder->assign('uploadMaxFilesize', $maxUploadFilesize);
  141. $folder->assign('uploadMaxHumanFilesize', Util::humanFileSize($maxUploadFilesize));
  142. $folder->assign('freeSpace', $freeSpace);
  143. $folder->assign('usedSpacePercent', 0);
  144. $folder->assign('trash', false);
  145. $shareTmpl['folder'] = $folder->fetchPage();
  146. } else {
  147. $shareIsFolder = false;
  148. }
  149. // default to list view
  150. $shareTmpl['showgridview'] = false;
  151. $shareTmpl['hideFileList'] = $hideFileList;
  152. $shareTmpl['downloadURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', [
  153. 'token' => $token,
  154. 'filename' => $shareIsFolder ? null : $shareNode->getName()
  155. ]);
  156. $shareTmpl['shareUrl'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]);
  157. $shareTmpl['maxSizeAnimateGif'] = $this->config->getSystemValue('max_filesize_animated_gifs_public_sharing', 10);
  158. $shareTmpl['previewEnabled'] = $this->config->getSystemValue('enable_previews', true);
  159. $shareTmpl['previewMaxX'] = $this->config->getSystemValue('preview_max_x', 1024);
  160. $shareTmpl['previewMaxY'] = $this->config->getSystemValue('preview_max_y', 1024);
  161. $shareTmpl['disclaimer'] = $this->config->getAppValue('core', 'shareapi_public_link_disclaimertext', '');
  162. $shareTmpl['previewURL'] = $shareTmpl['downloadURL'];
  163. if ($shareTmpl['previewSupported']) {
  164. $shareTmpl['previewImage'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview',
  165. ['x' => 200, 'y' => 200, 'file' => $shareTmpl['directory_path'], 'token' => $shareTmpl['dirToken']]);
  166. $ogPreview = $shareTmpl['previewImage'];
  167. // We just have direct previews for image files
  168. if ($shareNode->getMimePart() === 'image') {
  169. $shareTmpl['previewURL'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]);
  170. $ogPreview = $shareTmpl['previewURL'];
  171. //Whatapp is kind of picky about their size requirements
  172. if ($this->request->isUserAgent(['/^WhatsApp/'])) {
  173. $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [
  174. 'token' => $token,
  175. 'x' => 256,
  176. 'y' => 256,
  177. 'a' => true,
  178. ]);
  179. }
  180. }
  181. } else {
  182. $shareTmpl['previewImage'] = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png'));
  183. $ogPreview = $shareTmpl['previewImage'];
  184. }
  185. // Load files we need
  186. Util::addScript('files', 'semaphore');
  187. Util::addScript('files', 'file-upload');
  188. Util::addStyle('files_sharing', 'publicView');
  189. Util::addScript('files_sharing', 'public');
  190. Util::addScript('files_sharing', 'templates');
  191. Util::addScript('files', 'fileactions');
  192. Util::addScript('files', 'fileactionsmenu');
  193. Util::addScript('files', 'jquery.fileupload');
  194. Util::addScript('files_sharing', 'files_drop');
  195. if (isset($shareTmpl['folder'])) {
  196. // JS required for folders
  197. Util::addStyle('files', 'merged');
  198. Util::addScript('files', 'filesummary');
  199. Util::addScript('files', 'templates');
  200. Util::addScript('files', 'breadcrumb');
  201. Util::addScript('files', 'fileinfomodel');
  202. Util::addScript('files', 'newfilemenu');
  203. Util::addScript('files', 'files');
  204. Util::addScript('files', 'filemultiselectmenu');
  205. Util::addScript('files', 'filelist');
  206. Util::addScript('files', 'keyboardshortcuts');
  207. Util::addScript('files', 'operationprogressbar');
  208. }
  209. // Load Viewer scripts
  210. if (class_exists(LoadViewer::class)) {
  211. $this->eventDispatcher->dispatchTyped(new LoadViewer());
  212. }
  213. // OpenGraph Support: http://ogp.me/
  214. Util::addHeader('meta', ['property' => "og:title", 'content' => $shareTmpl['filename']]);
  215. Util::addHeader('meta', ['property' => "og:description", 'content' => $this->defaults->getName() . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : '')]);
  216. Util::addHeader('meta', ['property' => "og:site_name", 'content' => $this->defaults->getName()]);
  217. Util::addHeader('meta', ['property' => "og:url", 'content' => $shareTmpl['shareUrl']]);
  218. Util::addHeader('meta', ['property' => "og:type", 'content' => "object"]);
  219. Util::addHeader('meta', ['property' => "og:image", 'content' => $ogPreview]);
  220. $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($share));
  221. $csp = new ContentSecurityPolicy();
  222. $csp->addAllowedFrameDomain('\'self\'');
  223. $response = new PublicTemplateResponse(Application::APP_ID, 'public', $shareTmpl);
  224. $response->setHeaderTitle($shareTmpl['filename']);
  225. if ($shareTmpl['shareOwner'] !== '') {
  226. $response->setHeaderDetails($this->l10n->t('shared by %s', [$shareTmpl['shareOwner']]));
  227. }
  228. $isNoneFileDropFolder = $shareIsFolder === false || $share->getPermissions() !== Constants::PERMISSION_CREATE;
  229. if ($isNoneFileDropFolder && !$share->getHideDownload()) {
  230. Util::addScript('files_sharing', 'public_note');
  231. $downloadWhite = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download-white', $shareTmpl['downloadURL'], 0);
  232. $downloadAllWhite = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download-white', $shareTmpl['downloadURL'], 0);
  233. $download = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']);
  234. $downloadAll = new SimpleMenuAction('download', $this->l10n->t('Download all files'), 'icon-download', $shareTmpl['downloadURL'], 10, $shareTmpl['fileSize']);
  235. $directLink = new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $shareTmpl['previewURL']);
  236. // TRANSLATORS The placeholder refers to the software product name as in 'Add to your Nextcloud'
  237. $externalShare = new ExternalShareMenuAction($this->l10n->t('Add to your %s', [$this->defaults->getProductName()]), 'icon-external', $shareTmpl['owner'], $shareTmpl['shareOwner'], $shareTmpl['filename']);
  238. $responseComposer = [];
  239. if ($shareIsFolder) {
  240. $responseComposer[] = $downloadAllWhite;
  241. $responseComposer[] = $downloadAll;
  242. } else {
  243. $responseComposer[] = $downloadWhite;
  244. $responseComposer[] = $download;
  245. }
  246. $responseComposer[] = $directLink;
  247. if ($this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) {
  248. $responseComposer[] = $externalShare;
  249. }
  250. $response->setHeaderActions($responseComposer);
  251. }
  252. $response->setContentSecurityPolicy($csp);
  253. return $response;
  254. }
  255. }