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.

TemplateLayout.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Clark Tomlinson <fallen013@gmail.com>
  9. * @author Daniel Calviño Sánchez <danxuliu@gmail.com>
  10. * @author Guillaume COMPAGNON <gcompagnon@outlook.com>
  11. * @author Hendrik Leppelsack <hendrik@leppelsack.de>
  12. * @author Joas Schilling <coding@schilljs.com>
  13. * @author John Molakvoæ <skjnldsv@protonmail.com>
  14. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  15. * @author Julius Haertl <jus@bitgrid.net>
  16. * @author Julius Härtl <jus@bitgrid.net>
  17. * @author Lukas Reschke <lukas@statuscode.ch>
  18. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  19. * @author Morris Jobke <hey@morrisjobke.de>
  20. * @author Nils <git@to.nilsschnabel.de>
  21. * @author Remco Brenninkmeijer <requist1@starmail.nl>
  22. * @author Robin Appelman <robin@icewind.nl>
  23. * @author Robin McCorkell <robin@mccorkell.me.uk>
  24. * @author Roeland Jago Douma <roeland@famdouma.nl>
  25. * @author Thomas Citharel <nextcloud@tcit.fr>
  26. * @author Thomas Müller <thomas.mueller@tmit.eu>
  27. *
  28. * @license AGPL-3.0
  29. *
  30. * This code is free software: you can redistribute it and/or modify
  31. * it under the terms of the GNU Affero General Public License, version 3,
  32. * as published by the Free Software Foundation.
  33. *
  34. * This program is distributed in the hope that it will be useful,
  35. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37. * GNU Affero General Public License for more details.
  38. *
  39. * You should have received a copy of the GNU Affero General Public License, version 3,
  40. * along with this program. If not, see <http://www.gnu.org/licenses/>
  41. *
  42. */
  43. namespace OC;
  44. use bantu\IniGetWrapper\IniGetWrapper;
  45. use OC\Search\SearchQuery;
  46. use OC\Template\JSCombiner;
  47. use OC\Template\JSConfigHelper;
  48. use OC\Template\SCSSCacher;
  49. use OCP\AppFramework\Http\TemplateResponse;
  50. use OCP\Defaults;
  51. use OCP\IConfig;
  52. use OCP\IInitialStateService;
  53. use OCP\INavigationManager;
  54. use OCP\IUserSession;
  55. use OCP\Support\Subscription\IRegistry;
  56. use OCP\Util;
  57. class TemplateLayout extends \OC_Template {
  58. private static $versionHash = '';
  59. /** @var IConfig */
  60. private $config;
  61. /** @var IInitialStateService */
  62. private $initialState;
  63. /** @var INavigationManager */
  64. private $navigationManager;
  65. /**
  66. * @param string $renderAs
  67. * @param string $appId application id
  68. */
  69. public function __construct($renderAs, $appId = '') {
  70. /** @var IConfig */
  71. $this->config = \OC::$server->get(IConfig::class);
  72. /** @var IInitialStateService */
  73. $this->initialState = \OC::$server->get(IInitialStateService::class);
  74. if (\OC_Util::isIe()) {
  75. Util::addStyle('ie');
  76. }
  77. // Decide which page we show
  78. if ($renderAs === TemplateResponse::RENDER_AS_USER) {
  79. /** @var INavigationManager */
  80. $this->navigationManager = \OC::$server->get(INavigationManager::class);
  81. parent::__construct('core', 'layout.user');
  82. if (in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
  83. $this->assign('bodyid', 'body-settings');
  84. } else {
  85. $this->assign('bodyid', 'body-user');
  86. }
  87. $this->initialState->provideInitialState('core', 'active-app', $this->navigationManager->getActiveEntry());
  88. $this->initialState->provideInitialState('unified-search', 'limit-default', SearchQuery::LIMIT_DEFAULT);
  89. Util::addScript('dist/unified-search', null, true);
  90. // Add navigation entry
  91. $this->assign('application', '');
  92. $this->assign('appid', $appId);
  93. $navigation = $this->navigationManager->getAll();
  94. $this->assign('navigation', $navigation);
  95. $settingsNavigation = $this->navigationManager->getAll('settings');
  96. $this->assign('settingsnavigation', $settingsNavigation);
  97. foreach ($navigation as $entry) {
  98. if ($entry['active']) {
  99. $this->assign('application', $entry['name']);
  100. break;
  101. }
  102. }
  103. foreach ($settingsNavigation as $entry) {
  104. if ($entry['active']) {
  105. $this->assign('application', $entry['name']);
  106. break;
  107. }
  108. }
  109. $userDisplayName = false;
  110. $user = \OC::$server->get(IUserSession::class)->getUser();
  111. if ($user) {
  112. $userDisplayName = $user->getDisplayName();
  113. }
  114. $this->assign('user_displayname', $userDisplayName);
  115. $this->assign('user_uid', \OC_User::getUser());
  116. if (\OC_User::getUser() === false) {
  117. $this->assign('userAvatarSet', false);
  118. } else {
  119. $this->assign('userAvatarSet', true);
  120. $this->assign('userAvatarVersion', $this->config->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
  121. }
  122. // check if app menu icons should be inverted
  123. try {
  124. /** @var \OCA\Theming\Util $util */
  125. $util = \OC::$server->query(\OCA\Theming\Util::class);
  126. $this->assign('themingInvertMenu', $util->invertTextColor(\OC::$server->getThemingDefaults()->getColorPrimary()));
  127. } catch (\OCP\AppFramework\QueryException $e) {
  128. $this->assign('themingInvertMenu', false);
  129. } catch (\OCP\AutoloadNotAllowedException $e) {
  130. $this->assign('themingInvertMenu', false);
  131. }
  132. } elseif ($renderAs === TemplateResponse::RENDER_AS_ERROR) {
  133. parent::__construct('core', 'layout.guest', '', false);
  134. $this->assign('bodyid', 'body-login');
  135. $this->assign('user_displayname', '');
  136. $this->assign('user_uid', '');
  137. } elseif ($renderAs === TemplateResponse::RENDER_AS_GUEST) {
  138. parent::__construct('core', 'layout.guest');
  139. \OC_Util::addStyle('guest');
  140. $this->assign('bodyid', 'body-login');
  141. $userDisplayName = false;
  142. $user = \OC::$server->get(IUserSession::class)->getUser();
  143. if ($user) {
  144. $userDisplayName = $user->getDisplayName();
  145. }
  146. $this->assign('user_displayname', $userDisplayName);
  147. $this->assign('user_uid', \OC_User::getUser());
  148. } elseif ($renderAs === TemplateResponse::RENDER_AS_PUBLIC) {
  149. parent::__construct('core', 'layout.public');
  150. $this->assign('appid', $appId);
  151. $this->assign('bodyid', 'body-public');
  152. /** @var IRegistry $subscription */
  153. $subscription = \OC::$server->query(IRegistry::class);
  154. $showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true);
  155. if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) {
  156. $showSimpleSignup = false;
  157. }
  158. $this->assign('showSimpleSignUpLink', $showSimpleSignup);
  159. } else {
  160. parent::__construct('core', 'layout.base');
  161. }
  162. // Send the language and the locale to our layouts
  163. $lang = \OC::$server->getL10NFactory()->findLanguage();
  164. $locale = \OC::$server->getL10NFactory()->findLocale($lang);
  165. $lang = str_replace('_', '-', $lang);
  166. $this->assign('language', $lang);
  167. $this->assign('locale', $locale);
  168. if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
  169. if (empty(self::$versionHash)) {
  170. $v = \OC_App::getAppVersions();
  171. $v['core'] = implode('.', \OCP\Util::getVersion());
  172. self::$versionHash = substr(md5(implode(',', $v)), 0, 8);
  173. }
  174. } else {
  175. self::$versionHash = md5('not installed');
  176. }
  177. // Add the js files
  178. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  179. $this->assign('jsfiles', []);
  180. if ($this->config->getSystemValue('installed', false) && $renderAs != TemplateResponse::RENDER_AS_ERROR) {
  181. // this is on purpose outside of the if statement below so that the initial state is prefilled (done in the getConfig() call)
  182. // see https://github.com/nextcloud/server/pull/22636 for details
  183. $jsConfigHelper = new JSConfigHelper(
  184. \OC::$server->getL10N('lib'),
  185. \OC::$server->query(Defaults::class),
  186. \OC::$server->getAppManager(),
  187. \OC::$server->getSession(),
  188. \OC::$server->getUserSession()->getUser(),
  189. $this->config,
  190. \OC::$server->getGroupManager(),
  191. \OC::$server->get(IniGetWrapper::class),
  192. \OC::$server->getURLGenerator(),
  193. \OC::$server->getCapabilitiesManager(),
  194. \OC::$server->query(IInitialStateService::class)
  195. );
  196. $config = $jsConfigHelper->getConfig();
  197. if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
  198. $this->assign('inline_ocjs', $config);
  199. } else {
  200. $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
  201. }
  202. }
  203. foreach ($jsFiles as $info) {
  204. $web = $info[1];
  205. $file = $info[2];
  206. $this->append('jsfiles', $web.'/'.$file . $this->getVersionHashSuffix());
  207. }
  208. try {
  209. $pathInfo = \OC::$server->getRequest()->getPathInfo();
  210. } catch (\Exception $e) {
  211. $pathInfo = '';
  212. }
  213. // Do not initialise scss appdata until we have a fully installed instance
  214. // Do not load scss for update, errors, installation or login page
  215. if (\OC::$server->getSystemConfig()->getValue('installed', false)
  216. && !\OCP\Util::needUpgrade()
  217. && $pathInfo !== ''
  218. && !preg_match('/^\/login/', $pathInfo)
  219. && $renderAs !== TemplateResponse::RENDER_AS_ERROR
  220. ) {
  221. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  222. } else {
  223. // If we ignore the scss compiler,
  224. // we need to load the guest css fallback
  225. \OC_Util::addStyle('guest');
  226. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
  227. }
  228. $this->assign('cssfiles', []);
  229. $this->assign('printcssfiles', []);
  230. $this->assign('versionHash', self::$versionHash);
  231. foreach ($cssFiles as $info) {
  232. $web = $info[1];
  233. $file = $info[2];
  234. if (substr($file, -strlen('print.css')) === 'print.css') {
  235. $this->append('printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix());
  236. } else {
  237. $suffix = $this->getVersionHashSuffix($web, $file);
  238. if (strpos($file, '?v=') == false) {
  239. $this->append('cssfiles', $web.'/'.$file . $suffix);
  240. } else {
  241. $this->append('cssfiles', $web.'/'.$file . '-' . substr($suffix, 3));
  242. }
  243. }
  244. }
  245. $this->assign('initialStates', $this->initialState->getInitialStates());
  246. }
  247. /**
  248. * @param string $path
  249. * @param string $file
  250. * @return string
  251. */
  252. protected function getVersionHashSuffix($path = false, $file = false) {
  253. if ($this->config->getSystemValue('debug', false)) {
  254. // allows chrome workspace mapping in debug mode
  255. return "";
  256. }
  257. $themingSuffix = '';
  258. $v = [];
  259. if ($this->config->getSystemValue('installed', false)) {
  260. if (\OC::$server->getAppManager()->isInstalled('theming')) {
  261. $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
  262. }
  263. $v = \OC_App::getAppVersions();
  264. }
  265. // Try the webroot path for a match
  266. if ($path !== false && $path !== '') {
  267. $appName = $this->getAppNamefromPath($path);
  268. if (array_key_exists($appName, $v)) {
  269. $appVersion = $v[$appName];
  270. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  271. }
  272. }
  273. // fallback to the file path instead
  274. if ($file !== false && $file !== '') {
  275. $appName = $this->getAppNamefromPath($file);
  276. if (array_key_exists($appName, $v)) {
  277. $appVersion = $v[$appName];
  278. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  279. }
  280. }
  281. return '?v=' . self::$versionHash . $themingSuffix;
  282. }
  283. /**
  284. * @param array $styles
  285. * @return array
  286. */
  287. public static function findStylesheetFiles($styles, $compileScss = true) {
  288. // Read the selected theme from the config file
  289. $theme = \OC_Util::getTheme();
  290. if ($compileScss) {
  291. $SCSSCacher = \OC::$server->query(SCSSCacher::class);
  292. } else {
  293. $SCSSCacher = null;
  294. }
  295. $locator = new \OC\Template\CSSResourceLocator(
  296. \OC::$server->getLogger(),
  297. $theme,
  298. [ \OC::$SERVERROOT => \OC::$WEBROOT ],
  299. [ \OC::$SERVERROOT => \OC::$WEBROOT ],
  300. $SCSSCacher
  301. );
  302. $locator->find($styles);
  303. return $locator->getResources();
  304. }
  305. /**
  306. * @param string $path
  307. * @return string|boolean
  308. */
  309. public function getAppNamefromPath($path) {
  310. if ($path !== '' && is_string($path)) {
  311. $pathParts = explode('/', $path);
  312. if ($pathParts[0] === 'css') {
  313. // This is a scss request
  314. return $pathParts[1];
  315. }
  316. return end($pathParts);
  317. }
  318. return false;
  319. }
  320. /**
  321. * @param array $scripts
  322. * @return array
  323. */
  324. public static function findJavascriptFiles($scripts) {
  325. // Read the selected theme from the config file
  326. $theme = \OC_Util::getTheme();
  327. $locator = new \OC\Template\JSResourceLocator(
  328. \OC::$server->getLogger(),
  329. $theme,
  330. [ \OC::$SERVERROOT => \OC::$WEBROOT ],
  331. [ \OC::$SERVERROOT => \OC::$WEBROOT ],
  332. \OC::$server->query(JSCombiner::class)
  333. );
  334. $locator->find($scripts);
  335. return $locator->getResources();
  336. }
  337. /**
  338. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  339. * @param string $filePath Absolute path
  340. * @return string Relative path
  341. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  342. */
  343. public static function convertToRelativePath($filePath) {
  344. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  345. if (count($relativePath) !== 2) {
  346. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  347. }
  348. return $relativePath[1];
  349. }
  350. }