viewcontroller.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. /**
  3. * @author Lukas Reschke <lukas@owncloud.com>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  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, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\Files\Controller;
  23. use OC\AppFramework\Http\Request;
  24. use OCP\AppFramework\Controller;
  25. use OCP\AppFramework\Http\ContentSecurityPolicy;
  26. use OCP\AppFramework\Http\RedirectResponse;
  27. use OCP\AppFramework\Http\TemplateResponse;
  28. use OCP\IL10N;
  29. use OCP\INavigationManager;
  30. use OCP\IRequest;
  31. use OCP\IURLGenerator;
  32. use OCP\IConfig;
  33. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  34. /**
  35. * Class ViewController
  36. *
  37. * @package OCA\Files\Controller
  38. */
  39. class ViewController extends Controller {
  40. /** @var string */
  41. protected $appName;
  42. /** @var IRequest */
  43. protected $request;
  44. /** @var IURLGenerator */
  45. protected $urlGenerator;
  46. /** @var INavigationManager */
  47. protected $navigationManager;
  48. /** @var IL10N */
  49. protected $l10n;
  50. /** @var IConfig */
  51. protected $config;
  52. /** @var EventDispatcherInterface */
  53. protected $eventDispatcher;
  54. /**
  55. * @param string $appName
  56. * @param IRequest $request
  57. * @param IURLGenerator $urlGenerator
  58. * @param INavigationManager $navigationManager
  59. * @param IL10N $l10n
  60. * @param IConfig $config
  61. * @param EventDispatcherInterface $eventDispatcherInterface
  62. */
  63. public function __construct($appName,
  64. IRequest $request,
  65. IURLGenerator $urlGenerator,
  66. INavigationManager $navigationManager,
  67. IL10N $l10n,
  68. IConfig $config,
  69. EventDispatcherInterface $eventDispatcherInterface) {
  70. parent::__construct($appName, $request);
  71. $this->appName = $appName;
  72. $this->request = $request;
  73. $this->urlGenerator = $urlGenerator;
  74. $this->navigationManager = $navigationManager;
  75. $this->l10n = $l10n;
  76. $this->config = $config;
  77. $this->eventDispatcher = $eventDispatcherInterface;
  78. }
  79. /**
  80. * @param string $appName
  81. * @param string $scriptName
  82. * @return string
  83. */
  84. protected function renderScript($appName, $scriptName) {
  85. $content = '';
  86. $appPath = \OC_App::getAppPath($appName);
  87. $scriptPath = $appPath . '/' . $scriptName;
  88. if (file_exists($scriptPath)) {
  89. // TODO: sanitize path / script name ?
  90. ob_start();
  91. include $scriptPath;
  92. $content = ob_get_contents();
  93. @ob_end_clean();
  94. }
  95. return $content;
  96. }
  97. /**
  98. * FIXME: Replace with non static code
  99. *
  100. * @return array
  101. * @throws \OCP\Files\NotFoundException
  102. */
  103. protected function getStorageInfo() {
  104. $dirInfo = \OC\Files\Filesystem::getFileInfo('/', false);
  105. return \OC_Helper::getStorageInfo('/', $dirInfo);
  106. }
  107. /**
  108. * @NoCSRFRequired
  109. * @NoAdminRequired
  110. *
  111. * @param string $dir
  112. * @param string $view
  113. * @return TemplateResponse
  114. * @throws \OCP\Files\NotFoundException
  115. */
  116. public function index($dir = '', $view = '') {
  117. $nav = new \OCP\Template('files', 'appnavigation', '');
  118. // Load the files we need
  119. \OCP\Util::addStyle('files', 'files');
  120. \OCP\Util::addStyle('files', 'upload');
  121. \OCP\Util::addStyle('files', 'mobile');
  122. \OCP\Util::addscript('files', 'app');
  123. \OCP\Util::addscript('files', 'file-upload');
  124. \OCP\Util::addscript('files', 'newfilemenu');
  125. \OCP\Util::addscript('files', 'jquery.iframe-transport');
  126. \OCP\Util::addscript('files', 'jquery.fileupload');
  127. \OCP\Util::addscript('files', 'jquery-visibility');
  128. \OCP\Util::addscript('files', 'fileinfomodel');
  129. \OCP\Util::addscript('files', 'filesummary');
  130. \OCP\Util::addscript('files', 'breadcrumb');
  131. \OCP\Util::addscript('files', 'filelist');
  132. \OCP\Util::addscript('files', 'search');
  133. \OCP\Util::addScript('files', 'favoritesfilelist');
  134. \OCP\Util::addScript('files', 'tagsplugin');
  135. \OCP\Util::addScript('files', 'favoritesplugin');
  136. \OCP\Util::addScript('files', 'detailfileinfoview');
  137. \OCP\Util::addScript('files', 'detailtabview');
  138. \OCP\Util::addScript('files', 'mainfileinfodetailview');
  139. \OCP\Util::addScript('files', 'detailsview');
  140. \OCP\Util::addStyle('files', 'detailsView');
  141. \OC_Util::addVendorScript('core', 'handlebars/handlebars');
  142. \OCP\Util::addscript('files', 'fileactions');
  143. \OCP\Util::addscript('files', 'fileactionsmenu');
  144. \OCP\Util::addscript('files', 'files');
  145. \OCP\Util::addscript('files', 'keyboardshortcuts');
  146. \OCP\Util::addscript('files', 'navigation');
  147. // if IE8 and "?dir=path&view=someview" was specified, reformat the URL to use a hash like "#?dir=path&view=someview"
  148. $isIE8 = $this->request->isUserAgent([Request::USER_AGENT_IE_8]);
  149. if ($isIE8 && ($dir !== '' || $view !== '')) {
  150. $dir = !empty($dir) ? $dir : '/';
  151. $view = !empty($view) ? $view : 'files';
  152. $hash = '#?dir=' . \OCP\Util::encodePath($dir);
  153. if ($view !== 'files') {
  154. $hash .= '&view=' . urlencode($view);
  155. }
  156. return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index') . $hash);
  157. }
  158. // mostly for the home storage's free space
  159. // FIXME: Make non static
  160. $storageInfo = $this->getStorageInfo();
  161. \OCA\Files\App::getNavigationManager()->add(
  162. [
  163. 'id' => 'favorites',
  164. 'appname' => 'files',
  165. 'script' => 'simplelist.php',
  166. 'order' => 5,
  167. 'name' => $this->l10n->t('Favorites')
  168. ]
  169. );
  170. $navItems = \OCA\Files\App::getNavigationManager()->getAll();
  171. usort($navItems, function($item1, $item2) {
  172. return $item1['order'] - $item2['order'];
  173. });
  174. $nav->assign('navigationItems', $navItems);
  175. $contentItems = [];
  176. // render the container content for every navigation item
  177. foreach ($navItems as $item) {
  178. $content = '';
  179. if (isset($item['script'])) {
  180. $content = $this->renderScript($item['appname'], $item['script']);
  181. }
  182. $contentItem = [];
  183. $contentItem['id'] = $item['id'];
  184. $contentItem['content'] = $content;
  185. $contentItems[] = $contentItem;
  186. }
  187. $this->eventDispatcher->dispatch('OCA\Files::loadAdditionalScripts');
  188. $params = [];
  189. $params['usedSpacePercent'] = (int)$storageInfo['relative'];
  190. $params['owner'] = $storageInfo['owner'];
  191. $params['ownerDisplayName'] = $storageInfo['ownerDisplayName'];
  192. $params['isPublic'] = false;
  193. $params['mailNotificationEnabled'] = $this->config->getAppValue('core', 'shareapi_allow_mail_notification', 'no');
  194. $params['mailPublicNotificationEnabled'] = $this->config->getAppValue('core', 'shareapi_allow_public_notification', 'no');
  195. $params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes');
  196. $params['appNavigation'] = $nav;
  197. $params['appContents'] = $contentItems;
  198. $this->navigationManager->setActiveEntry('files_index');
  199. $response = new TemplateResponse(
  200. $this->appName,
  201. 'index',
  202. $params
  203. );
  204. $policy = new ContentSecurityPolicy();
  205. $policy->addAllowedFrameDomain('\'self\'');
  206. $response->setContentSecurityPolicy($policy);
  207. return $response;
  208. }
  209. }