You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TemplateLayout.php 10KB

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