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 11KB

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