diff options
Diffstat (limited to 'apps/files/lib/Controller/ViewController.php')
-rw-r--r-- | apps/files/lib/Controller/ViewController.php | 494 |
1 files changed, 185 insertions, 309 deletions
diff --git a/apps/files/lib/Controller/ViewController.php b/apps/files/lib/Controller/ViewController.php index 587f75f3f02..ecf21cef313 100644 --- a/apps/files/lib/Controller/ViewController.php +++ b/apps/files/lib/Controller/ViewController.php @@ -1,51 +1,33 @@ <?php + /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * @author fnuesse <felix.nuesse@t-online.de> - * @author fnuesse <fnuesse@techfak.uni-bielefeld.de> - * @author Joas Schilling <coding@schilljs.com> - * @author John Molakvoæ <skjnldsv@protonmail.com> - * @author Julius Härtl <jus@bitgrid.net> - * @author Lukas Reschke <lukas@statuscode.ch> - * @author Max Kovalenko <mxss1998@yandex.ru> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Nina Pypchenko <22447785+nina-py@users.noreply.github.com> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <vincent@nextcloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ namespace OCA\Files\Controller; -use OCA\Files\Activity\Helper; +use OC\Files\FilenameValidator; +use OC\Files\Filesystem; +use OCA\Files\AppInfo\Application; use OCA\Files\Event\LoadAdditionalScriptsEvent; +use OCA\Files\Event\LoadSearchPlugins; use OCA\Files\Event\LoadSidebar; +use OCA\Files\Service\UserConfig; +use OCA\Files\Service\ViewConfig; use OCA\Viewer\Event\LoadViewer; use OCP\App\IAppManager; use OCP\AppFramework\Controller; +use OCP\AppFramework\Http\Attribute\NoAdminRequired; +use OCP\AppFramework\Http\Attribute\NoCSRFRequired; +use OCP\AppFramework\Http\Attribute\OpenAPI; use OCP\AppFramework\Http\ContentSecurityPolicy; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Services\IInitialState; +use OCP\Authentication\TwoFactorAuth\IRegistry; +use OCP\Collaboration\Resources\LoadAdditionalScriptsEvent as ResourcesLoadAdditionalScriptsEvent; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Folder; use OCP\Files\IRootFolder; @@ -56,375 +38,269 @@ use OCP\IL10N; use OCP\IRequest; use OCP\IURLGenerator; use OCP\IUserSession; -use OCP\Share\IManager; +use OCP\Util; /** - * Class ViewController - * * @package OCA\Files\Controller */ +#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)] class ViewController extends Controller { - /** @var string */ - protected $appName; - /** @var IRequest */ - protected $request; - /** @var IURLGenerator */ - protected $urlGenerator; - /** @var IL10N */ - protected $l10n; - /** @var IConfig */ - protected $config; - /** @var IEventDispatcher */ - protected $eventDispatcher; - /** @var IUserSession */ - protected $userSession; - /** @var IAppManager */ - protected $appManager; - /** @var IRootFolder */ - protected $rootFolder; - /** @var Helper */ - protected $activityHelper; - /** @var IInitialState */ - private $initialState; - /** @var ITemplateManager */ - private $templateManager; - /** @var IManager */ - private $shareManager; - - public function __construct(string $appName, + + public function __construct( + string $appName, IRequest $request, - IURLGenerator $urlGenerator, - IL10N $l10n, - IConfig $config, - IEventDispatcher $eventDispatcher, - IUserSession $userSession, - IAppManager $appManager, - IRootFolder $rootFolder, - Helper $activityHelper, - IInitialState $initialState, - ITemplateManager $templateManager, - IManager $shareManager + private IURLGenerator $urlGenerator, + private IL10N $l10n, + private IConfig $config, + private IEventDispatcher $eventDispatcher, + private IUserSession $userSession, + private IAppManager $appManager, + private IRootFolder $rootFolder, + private IInitialState $initialState, + private ITemplateManager $templateManager, + private UserConfig $userConfig, + private ViewConfig $viewConfig, + private FilenameValidator $filenameValidator, + private IRegistry $twoFactorRegistry, ) { parent::__construct($appName, $request); - $this->appName = $appName; - $this->request = $request; - $this->urlGenerator = $urlGenerator; - $this->l10n = $l10n; - $this->config = $config; - $this->eventDispatcher = $eventDispatcher; - $this->userSession = $userSession; - $this->appManager = $appManager; - $this->rootFolder = $rootFolder; - $this->activityHelper = $activityHelper; - $this->initialState = $initialState; - $this->templateManager = $templateManager; - $this->shareManager = $shareManager; - } - - /** - * @param string $appName - * @param string $scriptName - * @return string - */ - protected function renderScript($appName, $scriptName) { - $content = ''; - $appPath = \OC_App::getAppPath($appName); - $scriptPath = $appPath . '/' . $scriptName; - if (file_exists($scriptPath)) { - // TODO: sanitize path / script name ? - ob_start(); - include $scriptPath; - $content = ob_get_contents(); - @ob_end_clean(); - } - - return $content; } /** * FIXME: Replace with non static code * * @return array - * @throws \OCP\Files\NotFoundException + * @throws NotFoundException */ - protected function getStorageInfo() { - \OC_Util::setupFS(); - $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false); + protected function getStorageInfo(string $dir = '/') { + $rootInfo = Filesystem::getFileInfo('/', false); - return \OC_Helper::getStorageInfo('/', $dirInfo); + return \OC_Helper::getStorageInfo($dir, $rootInfo ?: null); } /** - * @NoCSRFRequired - * @NoAdminRequired - * * @param string $fileid * @return TemplateResponse|RedirectResponse - * @throws NotFoundException */ - public function showFile(string $fileid = null, int $openfile = 1): Response { + #[NoAdminRequired] + #[NoCSRFRequired] + public function showFile(?string $fileid = null, ?string $opendetails = null, ?string $openfile = null): Response { + if (!$fileid) { + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index')); + } + // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server. try { - return $this->redirectToFile($fileid, $openfile !== 0); + return $this->redirectToFile((int)$fileid, $opendetails, $openfile); } catch (NotFoundException $e) { - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); + // Keep the fileid even if not found, it will be used + // to detect the file could not be found and warn the user + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.indexViewFileid', ['fileid' => $fileid, 'view' => 'files'])); } } + /** - * @NoCSRFRequired - * @NoAdminRequired - * * @param string $dir * @param string $view * @param string $fileid - * @param bool $fileNotFound - * @param string $openfile - the openfile URL parameter if it was present in the initial request * @return TemplateResponse|RedirectResponse - * @throws NotFoundException */ - public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false, $openfile = null) { - if ($fileid !== null && $dir === '') { + #[NoAdminRequired] + #[NoCSRFRequired] + public function indexView($dir = '', $view = '', $fileid = null) { + return $this->index($dir, $view, $fileid); + } + + /** + * @param string $dir + * @param string $view + * @param string $fileid + * @return TemplateResponse|RedirectResponse + */ + #[NoAdminRequired] + #[NoCSRFRequired] + public function indexViewFileid($dir = '', $view = '', $fileid = null) { + return $this->index($dir, $view, $fileid); + } + + /** + * @param string $dir + * @param string $view + * @param string $fileid + * @return TemplateResponse|RedirectResponse + */ + #[NoAdminRequired] + #[NoCSRFRequired] + public function index($dir = '', $view = '', $fileid = null) { + if ($fileid !== null && $view !== 'trashbin') { try { - return $this->redirectToFile($fileid); + return $this->redirectToFileIfInTrashbin((int)$fileid); } catch (NotFoundException $e) { - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); } } - $nav = new \OCP\Template('files', 'appnavigation', ''); - // Load the files we need - \OCP\Util::addStyle('files', 'merged'); - \OCP\Util::addScript('files', 'merged-index', 'files'); - \OCP\Util::addScript('files', 'main'); - - // mostly for the home storage's free space - // FIXME: Make non static - $storageInfo = $this->getStorageInfo(); + Util::addInitScript('files', 'init'); + Util::addScript('files', 'main'); - $user = $this->userSession->getUser()->getUID(); - - // Get all the user favorites to create a submenu - try { - $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID()); - } catch (\RuntimeException $e) { - $favElements['folders'] = []; - } - - $collapseClasses = ''; - if (count($favElements['folders']) > 0) { - $collapseClasses = 'collapsible'; - } - - $favoritesSublistArray = []; - - $navBarPositionPosition = 6; - $currentCount = 0; - foreach ($favElements['folders'] as $favElement) { - $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $favElement, 'view' => 'files']); - $sortingValue = ++$currentCount; - $element = [ - 'id' => str_replace('/', '-', $favElement), - 'view' => 'files', - 'href' => $link, - 'dir' => $favElement, - 'order' => $navBarPositionPosition, - 'folderPosition' => $sortingValue, - 'name' => basename($favElement), - 'icon' => 'files', - 'quickaccesselement' => 'true' - ]; - - array_push($favoritesSublistArray, $element); - $navBarPositionPosition++; - } - - $navItems = \OCA\Files\App::getNavigationManager()->getAll(); - - // add the favorites entry in menu - $navItems['favorites']['sublist'] = $favoritesSublistArray; - $navItems['favorites']['classes'] = $collapseClasses; - - // parse every menu and add the expandedState user value - foreach ($navItems as $key => $item) { - if (isset($item['expandedState'])) { - $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1'; + $user = $this->userSession->getUser(); + $userId = $user->getUID(); + + // If the file doesn't exists in the folder and + // exists in only one occurrence, redirect to that file + // in the correct folder + if ($fileid && $dir !== '') { + $baseFolder = $this->rootFolder->getUserFolder($userId); + $nodes = $baseFolder->getById((int)$fileid); + if (!empty($nodes)) { + $nodePath = $baseFolder->getRelativePath($nodes[0]->getPath()); + $relativePath = $nodePath ? dirname($nodePath) : ''; + // If the requested path does not contain the file id + // or if the requested path is not the file id itself + if (count($nodes) === 1 && $relativePath !== $dir && $nodePath !== $dir) { + return $this->redirectToFile((int)$fileid); + } } } - $nav->assign('navigationItems', $navItems); - - $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used'])); - if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { - $totalSpace = $this->l10n->t('Unlimited'); - } else { - $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); + try { + // If view is files, we use the directory, otherwise we use the root storage + $storageInfo = $this->getStorageInfo(($view === 'files' && $dir) ? $dir : '/'); + } catch (\Exception $e) { + $storageInfo = $this->getStorageInfo(); } - $nav->assign('total_space', $totalSpace); - $nav->assign('quota', $storageInfo['quota']); - $nav->assign('usage_relative', $storageInfo['relative']); - $nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . $user)); + $this->initialState->provideInitialState('storageStats', $storageInfo); + $this->initialState->provideInitialState('config', $this->userConfig->getConfigs()); + $this->initialState->provideInitialState('viewConfigs', $this->viewConfig->getConfigs()); - $contentItems = []; + // File sorting user config + $filesSortingConfig = json_decode($this->config->getUserValue($userId, 'files', 'files_sorting_configs', '{}'), true); + $this->initialState->provideInitialState('filesSortingConfig', $filesSortingConfig); - // render the container content for every navigation item - foreach ($navItems as $item) { - $content = ''; - if (isset($item['script'])) { - $content = $this->renderScript($item['appname'], $item['script']); - } - // parse submenus - if (isset($item['sublist'])) { - foreach ($item['sublist'] as $subitem) { - $subcontent = ''; - if (isset($subitem['script'])) { - $subcontent = $this->renderScript($subitem['appname'], $subitem['script']); - } - $contentItems[$subitem['id']] = [ - 'id' => $subitem['id'], - 'content' => $subcontent - ]; - } - } - $contentItems[$item['id']] = [ - 'id' => $item['id'], - 'content' => $content - ]; - } + // Forbidden file characters (deprecated use capabilities) + // TODO: Remove with next release of `@nextcloud/files` + $forbiddenCharacters = $this->filenameValidator->getForbiddenCharacters(); + $this->initialState->provideInitialState('forbiddenCharacters', $forbiddenCharacters); $event = new LoadAdditionalScriptsEvent(); $this->eventDispatcher->dispatchTyped($event); + $this->eventDispatcher->dispatchTyped(new ResourcesLoadAdditionalScriptsEvent()); $this->eventDispatcher->dispatchTyped(new LoadSidebar()); + $this->eventDispatcher->dispatchTyped(new LoadSearchPlugins()); // Load Viewer scripts if (class_exists(LoadViewer::class)) { $this->eventDispatcher->dispatchTyped(new LoadViewer()); } + + $this->initialState->provideInitialState('templates_enabled', ($this->config->getSystemValueString('skeletondirectory', \OC::$SERVERROOT . '/core/skeleton') !== '') || ($this->config->getSystemValueString('templatedirectory', \OC::$SERVERROOT . '/core/skeleton/Templates') !== '')); $this->initialState->provideInitialState('templates_path', $this->templateManager->hasTemplateDirectory() ? $this->templateManager->getTemplatePath() : false); $this->initialState->provideInitialState('templates', $this->templateManager->listCreators()); - $params = []; - $params['usedSpacePercent'] = (int) $storageInfo['relative']; - $params['owner'] = $storageInfo['owner'] ?? ''; - $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? ''; - $params['isPublic'] = false; - $params['allowShareWithLink'] = $this->shareManager->shareApiAllowLinks() ? 'yes' : 'no'; - $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); - $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); - $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false); - $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); - $params['showHiddenFiles'] = $showHidden ? 1 : 0; - $cropImagePreviews = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'crop_image_previews', true); - $params['cropImagePreviews'] = $cropImagePreviews ? 1 : 0; - $params['fileNotFound'] = $fileNotFound ? 1 : 0; - $params['appNavigation'] = $nav; - $params['appContents'] = $contentItems; - $params['hiddenFields'] = $event->getHiddenFields(); + $isTwoFactorEnabled = false; + foreach ($this->twoFactorRegistry->getProviderStates($user) as $providerId => $providerState) { + if ($providerId !== 'backup_codes' && $providerState === true) { + $isTwoFactorEnabled = true; + } + } + + $this->initialState->provideInitialState('isTwoFactorEnabled', $isTwoFactorEnabled); $response = new TemplateResponse( - $this->appName, + Application::APP_ID, 'index', - $params ); $policy = new ContentSecurityPolicy(); $policy->addAllowedFrameDomain('\'self\''); + // Allow preview service worker + $policy->addAllowedWorkerSrcDomain('\'self\''); $response->setContentSecurityPolicy($policy); - $this->provideInitialState($dir, $openfile); - return $response; } /** - * Add openFileInfo in initialState if $openfile is set. - * @param string $dir - the ?dir= URL param - * @param string $openfile - the ?openfile= URL param - * @return void + * Redirects to the trashbin file list and highlight the given file id + * + * @param int $fileId file id to show + * @return RedirectResponse redirect response or not found response + * @throws NotFoundException */ - private function provideInitialState(string $dir, ?string $openfile): void { - if ($openfile === null) { - return; - } - - $user = $this->userSession->getUser(); - - if ($user === null) { - return; - } - - $uid = $user->getUID(); - $userFolder = $this->rootFolder->getUserFolder($uid); - $nodes = $userFolder->getById((int) $openfile); - $node = array_shift($nodes); - - if ($node === null) { - return; - } + private function redirectToFileIfInTrashbin($fileId): RedirectResponse { + $uid = $this->userSession->getUser()->getUID(); + $baseFolder = $this->rootFolder->getUserFolder($uid); + $node = $baseFolder->getFirstNodeById($fileId); + $params = []; - // properly format full path and make sure - // we're relative to the user home folder - $isRoot = $node === $userFolder; - $path = $userFolder->getRelativePath($node->getPath()); - $directory = $userFolder->getRelativePath($node->getParent()->getPath()); + if (!$node && $this->appManager->isEnabledForUser('files_trashbin')) { + /** @var Folder */ + $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); + $node = $baseFolder->getFirstNodeById($fileId); + $params['view'] = 'trashbin'; - // Prevent opening a file from another folder. - if ($dir !== $directory) { - return; + if ($node) { + $params['fileid'] = $fileId; + if ($node instanceof Folder) { + // set the full path to enter the folder + $params['dir'] = $baseFolder->getRelativePath($node->getPath()); + } else { + // set parent path as dir + $params['dir'] = $baseFolder->getRelativePath($node->getParent()->getPath()); + } + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.indexViewFileid', $params)); + } } - - $this->initialState->provideInitialState( - 'openFileInfo', [ - 'id' => $node->getId(), - 'name' => $isRoot ? '' : $node->getName(), - 'path' => $path, - 'directory' => $directory, - 'mime' => $node->getMimetype(), - 'type' => $node->getType(), - 'permissions' => $node->getPermissions(), - ] - ); + throw new NotFoundException(); } /** * Redirects to the file list and highlight the given file id * - * @param string $fileId file id to show - * @param bool $setOpenfile - whether or not to set the openfile URL parameter + * @param int $fileId file id to show + * @param string|null $openDetails open details parameter + * @param string|null $openFile open file parameter * @return RedirectResponse redirect response or not found response - * @throws \OCP\Files\NotFoundException + * @throws NotFoundException */ - private function redirectToFile($fileId, bool $setOpenfile = false) { + private function redirectToFile(int $fileId, ?string $openDetails = null, ?string $openFile = null): RedirectResponse { $uid = $this->userSession->getUser()->getUID(); $baseFolder = $this->rootFolder->getUserFolder($uid); - $files = $baseFolder->getById($fileId); - $params = []; + $node = $baseFolder->getFirstNodeById($fileId); + $params = ['view' => 'files']; - if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) { - $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/'); - $files = $baseFolder->getById($fileId); - $params['view'] = 'trashbin'; + try { + $this->redirectToFileIfInTrashbin($fileId); + } catch (NotFoundException $e) { } - if (!empty($files)) { - $file = current($files); - if ($file instanceof Folder) { + if ($node) { + $params['fileid'] = $fileId; + if ($node instanceof Folder) { // set the full path to enter the folder - $params['dir'] = $baseFolder->getRelativePath($file->getPath()); + $params['dir'] = $baseFolder->getRelativePath($node->getPath()); } else { // set parent path as dir - $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath()); - // and scroll to the entry - $params['scrollto'] = $file->getName(); + $params['dir'] = $baseFolder->getRelativePath($node->getParent()->getPath()); + // open the file by default (opening the viewer) + $params['openfile'] = 'true'; + } - if ($setOpenfile) { - // forward the openfile URL parameter. - $params['openfile'] = $fileId; - } + // Forward open parameters if any. + // - openfile is true by default + // - opendetails is undefined by default + // - both will be evaluated as truthy + if ($openDetails !== null) { + $params['opendetails'] = $openDetails !== 'false' ? 'true' : 'false'; } - return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params)); + if ($openFile !== null) { + $params['openfile'] = $openFile !== 'false' ? 'true' : 'false'; + } + + return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.indexViewFileid', $params)); } - throw new \OCP\Files\NotFoundException(); + + throw new NotFoundException(); } } |