TemplateLayout.php 11 KB

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