TemplateLayout.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. /** @var InitialStateService $initialState */
  201. $initialState = \OC::$server->query(InitialStateService::class);
  202. $this->assign('initialStates', $initialState->getInitialStates());
  203. }
  204. /**
  205. * @param string $path
  206. * @param string $file
  207. * @return string
  208. */
  209. protected function getVersionHashSuffix($path = false, $file = false) {
  210. if ($this->config->getSystemValue('debug', false)) {
  211. // allows chrome workspace mapping in debug mode
  212. return "";
  213. }
  214. $themingSuffix = '';
  215. $v = [];
  216. if ($this->config->getSystemValue('installed', false)) {
  217. if (\OC::$server->getAppManager()->isInstalled('theming')) {
  218. $themingSuffix = '-' . $this->config->getAppValue('theming', 'cachebuster', '0');
  219. }
  220. $v = \OC_App::getAppVersions();
  221. }
  222. // Try the webroot path for a match
  223. if ($path !== false && $path !== '') {
  224. $appName = $this->getAppNamefromPath($path);
  225. if(array_key_exists($appName, $v)) {
  226. $appVersion = $v[$appName];
  227. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  228. }
  229. }
  230. // fallback to the file path instead
  231. if ($file !== false && $file !== '') {
  232. $appName = $this->getAppNamefromPath($file);
  233. if(array_key_exists($appName, $v)) {
  234. $appVersion = $v[$appName];
  235. return '?v=' . substr(md5($appVersion), 0, 8) . $themingSuffix;
  236. }
  237. }
  238. return '?v=' . self::$versionHash . $themingSuffix;
  239. }
  240. /**
  241. * @param array $styles
  242. * @return array
  243. */
  244. static public function findStylesheetFiles($styles, $compileScss = true) {
  245. // Read the selected theme from the config file
  246. $theme = \OC_Util::getTheme();
  247. if($compileScss) {
  248. $SCSSCacher = \OC::$server->query(SCSSCacher::class);
  249. } else {
  250. $SCSSCacher = null;
  251. }
  252. $locator = new \OC\Template\CSSResourceLocator(
  253. \OC::$server->getLogger(),
  254. $theme,
  255. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  256. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  257. $SCSSCacher
  258. );
  259. $locator->find($styles);
  260. return $locator->getResources();
  261. }
  262. /**
  263. * @param string $path
  264. * @return string|boolean
  265. */
  266. public function getAppNamefromPath($path) {
  267. if ($path !== '' && is_string($path)) {
  268. $pathParts = explode('/', $path);
  269. if ($pathParts[0] === 'css') {
  270. // This is a scss request
  271. return $pathParts[1];
  272. }
  273. return end($pathParts);
  274. }
  275. return false;
  276. }
  277. /**
  278. * @param array $scripts
  279. * @return array
  280. */
  281. static public function findJavascriptFiles($scripts) {
  282. // Read the selected theme from the config file
  283. $theme = \OC_Util::getTheme();
  284. $locator = new \OC\Template\JSResourceLocator(
  285. \OC::$server->getLogger(),
  286. $theme,
  287. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  288. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  289. \OC::$server->query(JSCombiner::class)
  290. );
  291. $locator->find($scripts);
  292. return $locator->getResources();
  293. }
  294. /**
  295. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  296. * @param string $filePath Absolute path
  297. * @return string Relative path
  298. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  299. */
  300. public static function convertToRelativePath($filePath) {
  301. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  302. if(count($relativePath) !== 2) {
  303. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  304. }
  305. return $relativePath[1];
  306. }
  307. }