TemplateLayout.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bart Visscher <bartv@thisnet.nl>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  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) <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 <tcit@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 OC\AppFramework\Http\Request;
  45. use OC\Template\JSCombiner;
  46. use OC\Template\JSConfigHelper;
  47. use OC\Template\SCSSCacher;
  48. use OCP\Defaults;
  49. use OCP\Support\Subscription\IRegistry;
  50. class TemplateLayout extends \OC_Template {
  51. private static $versionHash = '';
  52. /**
  53. * @var \OCP\IConfig
  54. */
  55. private $config;
  56. /**
  57. * @param string $renderAs
  58. * @param string $appId application id
  59. */
  60. public function __construct( $renderAs, $appId = '' ) {
  61. // yes - should be injected ....
  62. $this->config = \OC::$server->getConfig();
  63. if(\OCP\Util::isIE()) {
  64. \OC_Util::addStyle('ie');
  65. }
  66. // Decide which page we show
  67. if($renderAs === 'user') {
  68. parent::__construct( 'core', 'layout.user' );
  69. if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
  70. $this->assign('bodyid', 'body-settings');
  71. }else{
  72. $this->assign('bodyid', 'body-user');
  73. }
  74. // Add navigation entry
  75. $this->assign( 'application', '');
  76. $this->assign( 'appid', $appId );
  77. $navigation = \OC::$server->getNavigationManager()->getAll();
  78. $this->assign( 'navigation', $navigation);
  79. $settingsNavigation = \OC::$server->getNavigationManager()->getAll('settings');
  80. $this->assign( 'settingsnavigation', $settingsNavigation);
  81. foreach($navigation as $entry) {
  82. if ($entry['active']) {
  83. $this->assign( 'application', $entry['name'] );
  84. break;
  85. }
  86. }
  87. foreach($settingsNavigation as $entry) {
  88. if ($entry['active']) {
  89. $this->assign( 'application', $entry['name'] );
  90. break;
  91. }
  92. }
  93. $userDisplayName = \OC_User::getDisplayName();
  94. $this->assign('user_displayname', $userDisplayName);
  95. $this->assign('user_uid', \OC_User::getUser());
  96. if (\OC_User::getUser() === false) {
  97. $this->assign('userAvatarSet', false);
  98. } else {
  99. $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
  100. $this->assign('userAvatarVersion', $this->config->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
  101. }
  102. // check if app menu icons should be inverted
  103. try {
  104. /** @var \OCA\Theming\Util $util */
  105. $util = \OC::$server->query(\OCA\Theming\Util::class);
  106. $this->assign('themingInvertMenu', $util->invertTextColor(\OC::$server->getThemingDefaults()->getColorPrimary()));
  107. } catch (\OCP\AppFramework\QueryException $e) {
  108. $this->assign('themingInvertMenu', false);
  109. } catch (\OCP\AutoloadNotAllowedException $e) {
  110. $this->assign('themingInvertMenu', false);
  111. }
  112. } else if ($renderAs === 'error') {
  113. parent::__construct('core', 'layout.guest', '', false);
  114. $this->assign('bodyid', 'body-login');
  115. $this->assign('user_displayname', '');
  116. $this->assign('user_uid', '');
  117. } else if ($renderAs === 'guest') {
  118. parent::__construct('core', 'layout.guest');
  119. \OC_Util::addStyle('guest');
  120. $this->assign('bodyid', 'body-login');
  121. $userDisplayName = \OC_User::getDisplayName();
  122. $this->assign('user_displayname', $userDisplayName);
  123. $this->assign('user_uid', \OC_User::getUser());
  124. } else if ($renderAs === 'public') {
  125. parent::__construct('core', 'layout.public');
  126. $this->assign( 'appid', $appId );
  127. $this->assign('bodyid', 'body-public');
  128. /** @var IRegistry $subscription */
  129. $subscription = \OC::$server->query(IRegistry::class);
  130. $showSimpleSignup = $this->config->getSystemValueBool('simpleSignUpLink.shown', true);
  131. if ($showSimpleSignup && $subscription->delegateHasValidSubscription()) {
  132. $showSimpleSignup = false;
  133. }
  134. $this->assign('showSimpleSignUpLink', $showSimpleSignup);
  135. } else {
  136. parent::__construct('core', 'layout.base');
  137. }
  138. // Send the language and the locale to our layouts
  139. $lang = \OC::$server->getL10NFactory()->findLanguage();
  140. $locale = \OC::$server->getL10NFactory()->findLocale($lang);
  141. $lang = str_replace('_', '-', $lang);
  142. $this->assign('language', $lang);
  143. $this->assign('locale', $locale);
  144. if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
  145. if (empty(self::$versionHash)) {
  146. $v = \OC_App::getAppVersions();
  147. $v['core'] = implode('.', \OCP\Util::getVersion());
  148. self::$versionHash = substr(md5(implode(',', $v)), 0, 8);
  149. }
  150. } else {
  151. self::$versionHash = md5('not installed');
  152. }
  153. // Add the js files
  154. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  155. $this->assign('jsfiles', array());
  156. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  157. if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
  158. $jsConfigHelper = new JSConfigHelper(
  159. \OC::$server->getL10N('lib'),
  160. \OC::$server->query(Defaults::class),
  161. \OC::$server->getAppManager(),
  162. \OC::$server->getSession(),
  163. \OC::$server->getUserSession()->getUser(),
  164. $this->config,
  165. \OC::$server->getGroupManager(),
  166. \OC::$server->getIniWrapper(),
  167. \OC::$server->getURLGenerator(),
  168. \OC::$server->getCapabilitiesManager()
  169. );
  170. $this->assign('inline_ocjs', $jsConfigHelper->getConfig());
  171. } else {
  172. $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
  173. }
  174. }
  175. foreach($jsFiles as $info) {
  176. $web = $info[1];
  177. $file = $info[2];
  178. $this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  179. }
  180. try {
  181. $pathInfo = \OC::$server->getRequest()->getPathInfo();
  182. } catch (\Exception $e) {
  183. $pathInfo = '';
  184. }
  185. // Do not initialise scss appdata until we have a fully installed instance
  186. // Do not load scss for update, errors, installation or login page
  187. if(\OC::$server->getSystemConfig()->getValue('installed', false)
  188. && !\OCP\Util::needUpgrade()
  189. && $pathInfo !== ''
  190. && !preg_match('/^\/login/', $pathInfo)
  191. && $renderAs !== 'error'
  192. ) {
  193. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  194. } else {
  195. // If we ignore the scss compiler,
  196. // we need to load the guest css fallback
  197. \OC_Util::addStyle('guest');
  198. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
  199. }
  200. $this->assign('cssfiles', array());
  201. $this->assign('printcssfiles', []);
  202. $this->assign('versionHash', self::$versionHash);
  203. foreach($cssFiles as $info) {
  204. $web = $info[1];
  205. $file = $info[2];
  206. if (substr($file, -strlen('print.css')) === 'print.css') {
  207. $this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  208. } else {
  209. $suffix = $this->getVersionHashSuffix($web, $file);
  210. if (strpos($file, '?v=') == false) {
  211. $this->append( 'cssfiles', $web.'/'.$file . $suffix);
  212. } else {
  213. $this->append( 'cssfiles', $web.'/'.$file . '-' . substr($suffix, 3));
  214. }
  215. }
  216. }
  217. /** @var InitialStateService $initialState */
  218. $initialState = \OC::$server->query(InitialStateService::class);
  219. $this->assign('initialStates', $initialState->getInitialStates());
  220. }
  221. /**
  222. * @param string $path
  223. * @param string $file
  224. * @return string
  225. */
  226. protected function getVersionHashSuffix($path = false, $file = false) {
  227. if ($this->config->getSystemValue('debug', false)) {
  228. // allows chrome workspace mapping in debug mode
  229. return "";
  230. }
  231. $themingSuffix = '';
  232. $v = [];
  233. if ($this->config->getSystemValue('installed', false)) {
  234. if (\OC::$server->getAppManager()->isInstalled('theming')) {
  235. $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
  236. }
  237. $v = \OC_App::getAppVersions();
  238. }
  239. // Try the webroot path for a match
  240. if ($path !== false && $path !== '') {
  241. $appName = $this->getAppNamefromPath($path);
  242. if(array_key_exists($appName, $v)) {
  243. $appVersion = $v[$appName];
  244. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  245. }
  246. }
  247. // fallback to the file path instead
  248. if ($file !== false && $file !== '') {
  249. $appName = $this->getAppNamefromPath($file);
  250. if(array_key_exists($appName, $v)) {
  251. $appVersion = $v[$appName];
  252. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  253. }
  254. }
  255. return '?v=' . self::$versionHash . $themingSuffix;
  256. }
  257. /**
  258. * @param array $styles
  259. * @return array
  260. */
  261. static public function findStylesheetFiles($styles, $compileScss = true) {
  262. // Read the selected theme from the config file
  263. $theme = \OC_Util::getTheme();
  264. if($compileScss) {
  265. $SCSSCacher = \OC::$server->query(SCSSCacher::class);
  266. } else {
  267. $SCSSCacher = null;
  268. }
  269. $locator = new \OC\Template\CSSResourceLocator(
  270. \OC::$server->getLogger(),
  271. $theme,
  272. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  273. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  274. $SCSSCacher
  275. );
  276. $locator->find($styles);
  277. return $locator->getResources();
  278. }
  279. /**
  280. * @param string $path
  281. * @return string|boolean
  282. */
  283. public function getAppNamefromPath($path) {
  284. if ($path !== '' && is_string($path)) {
  285. $pathParts = explode('/', $path);
  286. if ($pathParts[0] === 'css') {
  287. // This is a scss request
  288. return $pathParts[1];
  289. }
  290. return end($pathParts);
  291. }
  292. return false;
  293. }
  294. /**
  295. * @param array $scripts
  296. * @return array
  297. */
  298. static public function findJavascriptFiles($scripts) {
  299. // Read the selected theme from the config file
  300. $theme = \OC_Util::getTheme();
  301. $locator = new \OC\Template\JSResourceLocator(
  302. \OC::$server->getLogger(),
  303. $theme,
  304. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  305. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  306. \OC::$server->query(JSCombiner::class)
  307. );
  308. $locator->find($scripts);
  309. return $locator->getResources();
  310. }
  311. /**
  312. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  313. * @param string $filePath Absolute path
  314. * @return string Relative path
  315. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  316. */
  317. public static function convertToRelativePath($filePath) {
  318. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  319. if(count($relativePath) !== 2) {
  320. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  321. }
  322. return $relativePath[1];
  323. }
  324. }