You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ViewController.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author fnuesse <felix.nuesse@t-online.de>
  8. * @author fnuesse <fnuesse@techfak.uni-bielefeld.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Max Kovalenko <mxss1998@yandex.ru>
  14. * @author Michael Weimann <mail@michael-weimann.eu>
  15. * @author Morris Jobke <hey@morrisjobke.de>
  16. * @author Robin Appelman <robin@icewind.nl>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Thomas Müller <thomas.mueller@tmit.eu>
  19. * @author Vincent Petry <pvince81@owncloud.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OCA\Files\Controller;
  37. use OCA\Files\Activity\Helper;
  38. use OCA\Files\Event\LoadAdditionalScriptsEvent;
  39. use OCA\Files\Event\LoadSidebar;
  40. use OCA\Viewer\Event\LoadViewer;
  41. use OCP\App\IAppManager;
  42. use OCP\AppFramework\Controller;
  43. use OCP\AppFramework\Http\ContentSecurityPolicy;
  44. use OCP\AppFramework\Http\RedirectResponse;
  45. use OCP\AppFramework\Http\Response;
  46. use OCP\AppFramework\Http\TemplateResponse;
  47. use OCP\EventDispatcher\IEventDispatcher;
  48. use OCP\Files\Folder;
  49. use OCP\Files\IRootFolder;
  50. use OCP\Files\NotFoundException;
  51. use OCP\IConfig;
  52. use OCP\IL10N;
  53. use OCP\IRequest;
  54. use OCP\IURLGenerator;
  55. use OCP\IUserSession;
  56. /**
  57. * Class ViewController
  58. *
  59. * @package OCA\Files\Controller
  60. */
  61. class ViewController extends Controller {
  62. /** @var string */
  63. protected $appName;
  64. /** @var IRequest */
  65. protected $request;
  66. /** @var IURLGenerator */
  67. protected $urlGenerator;
  68. /** @var IL10N */
  69. protected $l10n;
  70. /** @var IConfig */
  71. protected $config;
  72. /** @var IEventDispatcher */
  73. protected $eventDispatcher;
  74. /** @var IUserSession */
  75. protected $userSession;
  76. /** @var IAppManager */
  77. protected $appManager;
  78. /** @var IRootFolder */
  79. protected $rootFolder;
  80. /** @var Helper */
  81. protected $activityHelper;
  82. public function __construct(string $appName,
  83. IRequest $request,
  84. IURLGenerator $urlGenerator,
  85. IL10N $l10n,
  86. IConfig $config,
  87. IEventDispatcher $eventDispatcher,
  88. IUserSession $userSession,
  89. IAppManager $appManager,
  90. IRootFolder $rootFolder,
  91. Helper $activityHelper
  92. ) {
  93. parent::__construct($appName, $request);
  94. $this->appName = $appName;
  95. $this->request = $request;
  96. $this->urlGenerator = $urlGenerator;
  97. $this->l10n = $l10n;
  98. $this->config = $config;
  99. $this->eventDispatcher = $eventDispatcher;
  100. $this->userSession = $userSession;
  101. $this->appManager = $appManager;
  102. $this->rootFolder = $rootFolder;
  103. $this->activityHelper = $activityHelper;
  104. }
  105. /**
  106. * @param string $appName
  107. * @param string $scriptName
  108. * @return string
  109. */
  110. protected function renderScript($appName, $scriptName) {
  111. $content = '';
  112. $appPath = \OC_App::getAppPath($appName);
  113. $scriptPath = $appPath . '/' . $scriptName;
  114. if (file_exists($scriptPath)) {
  115. // TODO: sanitize path / script name ?
  116. ob_start();
  117. include $scriptPath;
  118. $content = ob_get_contents();
  119. @ob_end_clean();
  120. }
  121. return $content;
  122. }
  123. /**
  124. * FIXME: Replace with non static code
  125. *
  126. * @return array
  127. * @throws \OCP\Files\NotFoundException
  128. */
  129. protected function getStorageInfo() {
  130. $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false);
  131. return \OC_Helper::getStorageInfo('/', $dirInfo);
  132. }
  133. /**
  134. * @NoCSRFRequired
  135. * @NoAdminRequired
  136. *
  137. * @param string $fileid
  138. * @return TemplateResponse|RedirectResponse
  139. * @throws NotFoundException
  140. */
  141. public function showFile(string $fileid = null): Response {
  142. // This is the entry point from the `/f/{fileid}` URL which is hardcoded in the server.
  143. try {
  144. return $this->redirectToFile($fileid);
  145. } catch (NotFoundException $e) {
  146. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true]));
  147. }
  148. }
  149. /**
  150. * @NoCSRFRequired
  151. * @NoAdminRequired
  152. *
  153. * @param string $dir
  154. * @param string $view
  155. * @param string $fileid
  156. * @param bool $fileNotFound
  157. * @return TemplateResponse|RedirectResponse
  158. * @throws NotFoundException
  159. */
  160. public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false) {
  161. if ($fileid !== null) {
  162. try {
  163. return $this->redirectToFile($fileid);
  164. } catch (NotFoundException $e) {
  165. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true]));
  166. }
  167. }
  168. $nav = new \OCP\Template('files', 'appnavigation', '');
  169. // Load the files we need
  170. \OCP\Util::addStyle('files', 'merged');
  171. \OCP\Util::addScript('files', 'merged-index');
  172. // mostly for the home storage's free space
  173. // FIXME: Make non static
  174. $storageInfo = $this->getStorageInfo();
  175. $user = $this->userSession->getUser()->getUID();
  176. // Get all the user favorites to create a submenu
  177. try {
  178. $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID());
  179. } catch (\RuntimeException $e) {
  180. $favElements['folders'] = [];
  181. }
  182. $collapseClasses = '';
  183. if (count($favElements['folders']) > 0) {
  184. $collapseClasses = 'collapsible';
  185. }
  186. $favoritesSublistArray = [];
  187. $navBarPositionPosition = 6;
  188. $currentCount = 0;
  189. foreach ($favElements['folders'] as $dir) {
  190. $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $dir, 'view' => 'files']);
  191. $sortingValue = ++$currentCount;
  192. $element = [
  193. 'id' => str_replace('/', '-', $dir),
  194. 'view' => 'files',
  195. 'href' => $link,
  196. 'dir' => $dir,
  197. 'order' => $navBarPositionPosition,
  198. 'folderPosition' => $sortingValue,
  199. 'name' => basename($dir),
  200. 'icon' => 'files',
  201. 'quickaccesselement' => 'true'
  202. ];
  203. array_push($favoritesSublistArray, $element);
  204. $navBarPositionPosition++;
  205. }
  206. $navItems = \OCA\Files\App::getNavigationManager()->getAll();
  207. // add the favorites entry in menu
  208. $navItems['favorites']['sublist'] = $favoritesSublistArray;
  209. $navItems['favorites']['classes'] = $collapseClasses;
  210. // parse every menu and add the expandedState user value
  211. foreach ($navItems as $key => $item) {
  212. if (isset($item['expandedState'])) {
  213. $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1';
  214. }
  215. }
  216. $nav->assign('navigationItems', $navItems);
  217. $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used']));
  218. if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) {
  219. $totalSpace = $this->l10n->t('Unlimited');
  220. } else {
  221. $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']);
  222. }
  223. $nav->assign('total_space', $totalSpace);
  224. $nav->assign('quota', $storageInfo['quota']);
  225. $nav->assign('usage_relative', $storageInfo['relative']);
  226. $nav->assign('webdav_url', \OCP\Util::linkToRemote('dav/files/' . $user));
  227. $contentItems = [];
  228. // render the container content for every navigation item
  229. foreach ($navItems as $item) {
  230. $content = '';
  231. if (isset($item['script'])) {
  232. $content = $this->renderScript($item['appname'], $item['script']);
  233. }
  234. // parse submenus
  235. if (isset($item['sublist'])) {
  236. foreach ($item['sublist'] as $subitem) {
  237. $subcontent = '';
  238. if (isset($subitem['script'])) {
  239. $subcontent = $this->renderScript($subitem['appname'], $subitem['script']);
  240. }
  241. $contentItems[$subitem['id']] = [
  242. 'id' => $subitem['id'],
  243. 'content' => $subcontent
  244. ];
  245. }
  246. }
  247. $contentItems[$item['id']] = [
  248. 'id' => $item['id'],
  249. 'content' => $content
  250. ];
  251. }
  252. $event = new LoadAdditionalScriptsEvent();
  253. $this->eventDispatcher->dispatchTyped($event);
  254. $this->eventDispatcher->dispatchTyped(new LoadSidebar());
  255. // Load Viewer scripts
  256. if (class_exists(LoadViewer::class)) {
  257. $this->eventDispatcher->dispatchTyped(new LoadViewer());
  258. }
  259. $params = [];
  260. $params['usedSpacePercent'] = (int) $storageInfo['relative'];
  261. $params['owner'] = $storageInfo['owner'] ?? '';
  262. $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'] ?? '';
  263. $params['isPublic'] = false;
  264. $params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
  265. $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name');
  266. $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc');
  267. $params['showgridview'] = $this->config->getUserValue($user, 'files', 'show_grid', false);
  268. $params['isIE'] = \OCP\Util::isIE();
  269. $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false);
  270. $params['showHiddenFiles'] = $showHidden ? 1 : 0;
  271. $params['fileNotFound'] = $fileNotFound ? 1 : 0;
  272. $params['appNavigation'] = $nav;
  273. $params['appContents'] = $contentItems;
  274. $params['hiddenFields'] = $event->getHiddenFields();
  275. $response = new TemplateResponse(
  276. $this->appName,
  277. 'index',
  278. $params
  279. );
  280. $policy = new ContentSecurityPolicy();
  281. $policy->addAllowedFrameDomain('\'self\'');
  282. $response->setContentSecurityPolicy($policy);
  283. return $response;
  284. }
  285. /**
  286. * Redirects to the file list and highlight the given file id
  287. *
  288. * @param string $fileId file id to show
  289. * @return RedirectResponse redirect response or not found response
  290. * @throws \OCP\Files\NotFoundException
  291. */
  292. private function redirectToFile($fileId) {
  293. $uid = $this->userSession->getUser()->getUID();
  294. $baseFolder = $this->rootFolder->getUserFolder($uid);
  295. $files = $baseFolder->getById($fileId);
  296. $params = [];
  297. if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
  298. $baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
  299. $files = $baseFolder->getById($fileId);
  300. $params['view'] = 'trashbin';
  301. }
  302. if (!empty($files)) {
  303. $file = current($files);
  304. if ($file instanceof Folder) {
  305. // set the full path to enter the folder
  306. $params['dir'] = $baseFolder->getRelativePath($file->getPath());
  307. } else {
  308. // set parent path as dir
  309. $params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
  310. // and scroll to the entry
  311. $params['scrollto'] = $file->getName();
  312. }
  313. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
  314. }
  315. throw new \OCP\Files\NotFoundException();
  316. }
  317. }