TemplateLayout.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. } else if ($renderAs == 'public') {
  113. parent::__construct('core', 'layout.public');
  114. $this->assign( 'appid', $appId );
  115. $this->assign('bodyid', 'body-public');
  116. $this->assign('showSimpleSignUpLink', $this->config->getSystemValue('simpleSignUpLink.shown', true) !== false);
  117. } else {
  118. parent::__construct('core', 'layout.base');
  119. }
  120. // Send the language and the locale to our layouts
  121. $lang = \OC::$server->getL10NFactory()->findLanguage();
  122. $locale = \OC::$server->getL10NFactory()->findLocale($lang);
  123. $localeLang = \OC::$server->getL10NFactory()->findLanguageFromLocale('lib', $locale);
  124. $lang = str_replace('_', '-', $lang);
  125. $this->assign('language', $lang);
  126. $this->assign('locale', $locale);
  127. if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
  128. if (empty(self::$versionHash)) {
  129. $v = \OC_App::getAppVersions();
  130. $v['core'] = implode('.', \OCP\Util::getVersion());
  131. self::$versionHash = substr(md5(implode(',', $v)), 0, 8);
  132. }
  133. } else {
  134. self::$versionHash = md5('not installed');
  135. }
  136. // Add the js files
  137. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  138. $this->assign('jsfiles', array());
  139. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  140. if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
  141. $jsConfigHelper = new JSConfigHelper(
  142. \OC::$server->getL10N('lib', $localeLang ?: $lang),
  143. \OC::$server->query(Defaults::class),
  144. \OC::$server->getAppManager(),
  145. \OC::$server->getSession(),
  146. \OC::$server->getUserSession()->getUser(),
  147. $this->config,
  148. \OC::$server->getGroupManager(),
  149. \OC::$server->getIniWrapper(),
  150. \OC::$server->getURLGenerator(),
  151. \OC::$server->getCapabilitiesManager()
  152. );
  153. $this->assign('inline_ocjs', $jsConfigHelper->getConfig());
  154. } else {
  155. $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
  156. }
  157. }
  158. foreach($jsFiles as $info) {
  159. $web = $info[1];
  160. $file = $info[2];
  161. $this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  162. }
  163. try {
  164. $pathInfo = \OC::$server->getRequest()->getPathInfo();
  165. } catch (\Exception $e) {
  166. $pathInfo = '';
  167. }
  168. // Do not initialise scss appdata until we have a fully installed instance
  169. // Do not load scss for update, errors, installation or login page
  170. if(\OC::$server->getSystemConfig()->getValue('installed', false)
  171. && !\OCP\Util::needUpgrade()
  172. && $pathInfo !== ''
  173. && !preg_match('/^\/login/', $pathInfo)
  174. && $renderAs !== 'error'
  175. ) {
  176. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  177. } else {
  178. // If we ignore the scss compiler,
  179. // we need to load the guest css fallback
  180. \OC_Util::addStyle('guest');
  181. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
  182. }
  183. $this->assign('cssfiles', array());
  184. $this->assign('printcssfiles', []);
  185. $this->assign('versionHash', self::$versionHash);
  186. foreach($cssFiles as $info) {
  187. $web = $info[1];
  188. $file = $info[2];
  189. if (substr($file, -strlen('print.css')) === 'print.css') {
  190. $this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  191. } else {
  192. $suffix = $this->getVersionHashSuffix($web, $file);
  193. if (strpos($file, '?v=') == false) {
  194. $this->append( 'cssfiles', $web.'/'.$file . $suffix);
  195. } else {
  196. $this->append( 'cssfiles', $web.'/'.$file . '-' . substr($suffix, 3));
  197. }
  198. }
  199. }
  200. }
  201. /**
  202. * @param string $path
  203. * @param string $file
  204. * @return string
  205. */
  206. protected function getVersionHashSuffix($path = false, $file = false) {
  207. if ($this->config->getSystemValue('debug', false)) {
  208. // allows chrome workspace mapping in debug mode
  209. return "";
  210. }
  211. $themingSuffix = '';
  212. $v = [];
  213. if ($this->config->getSystemValue('installed', false)) {
  214. if (\OC::$server->getAppManager()->isInstalled('theming')) {
  215. $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
  216. }
  217. $v = \OC_App::getAppVersions();
  218. }
  219. // Try the webroot path for a match
  220. if ($path !== false && $path !== '') {
  221. $appName = $this->getAppNamefromPath($path);
  222. if(array_key_exists($appName, $v)) {
  223. $appVersion = $v[$appName];
  224. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  225. }
  226. }
  227. // fallback to the file path instead
  228. if ($file !== false && $file !== '') {
  229. $appName = $this->getAppNamefromPath($file);
  230. if(array_key_exists($appName, $v)) {
  231. $appVersion = $v[$appName];
  232. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  233. }
  234. }
  235. return '?v=' . self::$versionHash . $themingSuffix;
  236. }
  237. /**
  238. * @param array $styles
  239. * @return array
  240. */
  241. static public function findStylesheetFiles($styles, $compileScss = true) {
  242. // Read the selected theme from the config file
  243. $theme = \OC_Util::getTheme();
  244. if($compileScss) {
  245. $SCSSCacher = \OC::$server->query(SCSSCacher::class);
  246. } else {
  247. $SCSSCacher = null;
  248. }
  249. $locator = new \OC\Template\CSSResourceLocator(
  250. \OC::$server->getLogger(),
  251. $theme,
  252. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  253. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  254. $SCSSCacher
  255. );
  256. $locator->find($styles);
  257. return $locator->getResources();
  258. }
  259. /**
  260. * @param string $path
  261. * @return string|boolean
  262. */
  263. public function getAppNamefromPath($path) {
  264. if ($path !== '' && is_string($path)) {
  265. $pathParts = explode('/', $path);
  266. if ($pathParts[0] === 'css') {
  267. // This is a scss request
  268. return $pathParts[1];
  269. }
  270. return end($pathParts);
  271. }
  272. return false;
  273. }
  274. /**
  275. * @param array $scripts
  276. * @return array
  277. */
  278. static public function findJavascriptFiles($scripts) {
  279. // Read the selected theme from the config file
  280. $theme = \OC_Util::getTheme();
  281. $locator = new \OC\Template\JSResourceLocator(
  282. \OC::$server->getLogger(),
  283. $theme,
  284. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  285. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  286. \OC::$server->query(JSCombiner::class)
  287. );
  288. $locator->find($scripts);
  289. return $locator->getResources();
  290. }
  291. /**
  292. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  293. * @param string $filePath Absolute path
  294. * @return string Relative path
  295. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  296. */
  297. public static function convertToRelativePath($filePath) {
  298. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  299. if(count($relativePath) !== 2) {
  300. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  301. }
  302. return $relativePath[1];
  303. }
  304. }