TemplateLayout.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // Code integrity notification
  70. $integrityChecker = \OC::$server->getIntegrityCodeChecker();
  71. if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
  72. \OCP\Util::addScript('core', 'integritycheck-failed-notification');
  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. }
  110. } else if ($renderAs == 'error') {
  111. parent::__construct('core', 'layout.guest', '', false);
  112. $this->assign('bodyid', 'body-login');
  113. } else if ($renderAs == 'guest') {
  114. parent::__construct('core', 'layout.guest');
  115. \OC_Util::addStyle('guest');
  116. $this->assign('bodyid', 'body-login');
  117. } else if ($renderAs == 'public') {
  118. parent::__construct('core', 'layout.public');
  119. $this->assign( 'appid', $appId );
  120. $this->assign('bodyid', 'body-public');
  121. $this->assign('showSimpleSignUpLink', $this->config->getSystemValue('simpleSignUpLink.shown', true) !== false);
  122. } else {
  123. parent::__construct('core', 'layout.base');
  124. }
  125. // Send the language and the locale to our layouts
  126. $lang = \OC::$server->getL10NFactory()->findLanguage();
  127. $lang = str_replace('_', '-', $lang);
  128. $this->assign('language', $lang);
  129. $this->assign('locale', \OC::$server->getL10NFactory()->findLocale($lang));
  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'),
  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. $this->append( 'cssfiles', $web.'/'.$file . $this->getVersionHashSuffix($web, $file) );
  196. }
  197. }
  198. }
  199. /**
  200. * @param string $path
  201. * @param string $file
  202. * @return string
  203. */
  204. protected function getVersionHashSuffix($path = false, $file = false) {
  205. if ($this->config->getSystemValue('debug', false)) {
  206. // allows chrome workspace mapping in debug mode
  207. return "";
  208. }
  209. $themingSuffix = '';
  210. $v = [];
  211. if ($this->config->getSystemValue('installed', false)) {
  212. if (\OC::$server->getAppManager()->isInstalled('theming')) {
  213. $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
  214. }
  215. $v = \OC_App::getAppVersions();
  216. }
  217. // Try the webroot path for a match
  218. if ($path !== false && $path !== '') {
  219. $appName = $this->getAppNamefromPath($path);
  220. if(array_key_exists($appName, $v)) {
  221. $appVersion = $v[$appName];
  222. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  223. }
  224. }
  225. // fallback to the file path instead
  226. if ($file !== false && $file !== '') {
  227. $appName = $this->getAppNamefromPath($file);
  228. if(array_key_exists($appName, $v)) {
  229. $appVersion = $v[$appName];
  230. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  231. }
  232. }
  233. return '?v=' . self::$versionHash . $themingSuffix;
  234. }
  235. /**
  236. * @param array $styles
  237. * @return array
  238. */
  239. static public function findStylesheetFiles($styles, $compileScss = true) {
  240. // Read the selected theme from the config file
  241. $theme = \OC_Util::getTheme();
  242. if($compileScss) {
  243. $SCSSCacher = \OC::$server->query(SCSSCacher::class);
  244. } else {
  245. $SCSSCacher = null;
  246. }
  247. $locator = new \OC\Template\CSSResourceLocator(
  248. \OC::$server->getLogger(),
  249. $theme,
  250. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  251. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  252. $SCSSCacher
  253. );
  254. $locator->find($styles);
  255. return $locator->getResources();
  256. }
  257. /**
  258. * @param string $path
  259. * @return string|boolean
  260. */
  261. public function getAppNamefromPath($path) {
  262. if ($path !== '' && is_string($path)) {
  263. $pathParts = explode('/', $path);
  264. if ($pathParts[0] === 'css') {
  265. // This is a scss request
  266. return $pathParts[1];
  267. }
  268. return end($pathParts);
  269. }
  270. return false;
  271. }
  272. /**
  273. * @param array $scripts
  274. * @return array
  275. */
  276. static public function findJavascriptFiles($scripts) {
  277. // Read the selected theme from the config file
  278. $theme = \OC_Util::getTheme();
  279. $locator = new \OC\Template\JSResourceLocator(
  280. \OC::$server->getLogger(),
  281. $theme,
  282. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  283. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  284. \OC::$server->query(JSCombiner::class)
  285. );
  286. $locator->find($scripts);
  287. return $locator->getResources();
  288. }
  289. /**
  290. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  291. * @param string $filePath Absolute path
  292. * @return string Relative path
  293. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  294. */
  295. public static function convertToRelativePath($filePath) {
  296. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  297. if(count($relativePath) !== 2) {
  298. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  299. }
  300. return $relativePath[1];
  301. }
  302. }