TemplateLayout.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Remco Brenninkmeijer <requist1@starmail.nl>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Robin McCorkell <robin@mccorkell.me.uk>
  17. * @author Roeland Jago Douma <roeland@famdouma.nl>
  18. * @author Thomas Müller <thomas.mueller@tmit.eu>
  19. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  20. *
  21. * @license AGPL-3.0
  22. *
  23. * This code is free software: you can redistribute it and/or modify
  24. * it under the terms of the GNU Affero General Public License, version 3,
  25. * as published by the Free Software Foundation.
  26. *
  27. * This program is distributed in the hope that it will be useful,
  28. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30. * GNU Affero General Public License for more details.
  31. *
  32. * You should have received a copy of the GNU Affero General Public License, version 3,
  33. * along with this program. If not, see <http://www.gnu.org/licenses/>
  34. *
  35. */
  36. namespace OC;
  37. use OC\Template\JSCombiner;
  38. use OC\Template\JSConfigHelper;
  39. use OC\Template\SCSSCacher;
  40. class TemplateLayout extends \OC_Template {
  41. private static $versionHash = '';
  42. /**
  43. * @var \OCP\IConfig
  44. */
  45. private $config;
  46. /**
  47. * @param string $renderAs
  48. * @param string $appId application id
  49. */
  50. public function __construct( $renderAs, $appId = '' ) {
  51. // yes - should be injected ....
  52. $this->config = \OC::$server->getConfig();
  53. // Decide which page we show
  54. if($renderAs == 'user') {
  55. parent::__construct( 'core', 'layout.user' );
  56. if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
  57. $this->assign('bodyid', 'body-settings');
  58. }else{
  59. $this->assign('bodyid', 'body-user');
  60. }
  61. // Code integrity notification
  62. $integrityChecker = \OC::$server->getIntegrityCodeChecker();
  63. if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
  64. \OCP\Util::addScript('core', 'integritycheck-failed-notification');
  65. }
  66. // Add navigation entry
  67. $this->assign( 'application', '');
  68. $this->assign( 'appid', $appId );
  69. $navigation = \OC_App::getNavigation();
  70. $this->assign( 'navigation', $navigation);
  71. $navigation = \OC_App::getHeaderNavigation();
  72. $this->assign( 'headernavigation', $navigation);
  73. $settingsNavigation = \OC_App::getSettingsNavigation();
  74. $this->assign( 'settingsnavigation', $settingsNavigation);
  75. foreach($navigation as $entry) {
  76. if ($entry['active']) {
  77. $this->assign( 'application', $entry['name'] );
  78. break;
  79. }
  80. }
  81. foreach($settingsNavigation as $entry) {
  82. if ($entry['active']) {
  83. $this->assign( 'application', $entry['name'] );
  84. break;
  85. }
  86. }
  87. $userDisplayName = \OC_User::getDisplayName();
  88. $this->assign('user_displayname', $userDisplayName);
  89. $this->assign('user_uid', \OC_User::getUser());
  90. if (\OC_User::getUser() === false) {
  91. $this->assign('userAvatarSet', false);
  92. } else {
  93. $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
  94. $this->assign('userAvatarVersion', \OC::$server->getConfig()->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
  95. }
  96. } else if ($renderAs == 'error') {
  97. parent::__construct('core', 'layout.guest', '', false);
  98. $this->assign('bodyid', 'body-login');
  99. } else if ($renderAs == 'guest') {
  100. parent::__construct('core', 'layout.guest');
  101. $this->assign('bodyid', 'body-login');
  102. } else {
  103. parent::__construct('core', 'layout.base');
  104. }
  105. // Send the language to our layouts
  106. $this->assign('language', \OC::$server->getL10NFactory()->findLanguage());
  107. if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
  108. if (empty(self::$versionHash)) {
  109. $v = \OC_App::getAppVersions();
  110. $v['core'] = implode('.', \OCP\Util::getVersion());
  111. self::$versionHash = md5(implode(',', $v));
  112. }
  113. } else {
  114. self::$versionHash = md5('not installed');
  115. }
  116. // Add the js files
  117. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  118. $this->assign('jsfiles', array());
  119. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  120. if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
  121. $jsConfigHelper = new JSConfigHelper(
  122. \OC::$server->getL10N('core'),
  123. \OC::$server->getThemingDefaults(),
  124. \OC::$server->getAppManager(),
  125. \OC::$server->getSession(),
  126. \OC::$server->getUserSession()->getUser(),
  127. \OC::$server->getConfig(),
  128. \OC::$server->getGroupManager(),
  129. \OC::$server->getIniWrapper(),
  130. \OC::$server->getURLGenerator()
  131. );
  132. $this->assign('inline_ocjs', $jsConfigHelper->getConfig());
  133. } else {
  134. $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
  135. }
  136. }
  137. foreach($jsFiles as $info) {
  138. $web = $info[1];
  139. $file = $info[2];
  140. $this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  141. }
  142. try {
  143. $pathInfo = \OC::$server->getRequest()->getPathInfo();
  144. } catch (\Exception $e) {
  145. $pathInfo = '';
  146. }
  147. // Do not initialise scss appdata until we have a fully installed instance
  148. // Do not load scss for update, errors, installation or login page
  149. if(\OC::$server->getSystemConfig()->getValue('installed', false)
  150. && !\OCP\Util::needUpgrade()
  151. && $pathInfo !== ''
  152. && !preg_match('/^\/login/', $pathInfo)) {
  153. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  154. } else {
  155. // If we ignore the scss compiler,
  156. // we need to load the guest css fallback
  157. \OC_Util::addStyle('guest');
  158. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
  159. }
  160. $this->assign('cssfiles', array());
  161. $this->assign('printcssfiles', []);
  162. $this->assign('versionHash', self::$versionHash);
  163. foreach($cssFiles as $info) {
  164. $web = $info[1];
  165. $file = $info[2];
  166. if (substr($file, -strlen('print.css')) === 'print.css') {
  167. $this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  168. } else {
  169. $this->append( 'cssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  170. }
  171. }
  172. }
  173. protected function getVersionHashSuffix() {
  174. if(\OC::$server->getConfig()->getSystemValue('debug', false)) {
  175. // allows chrome workspace mapping in debug mode
  176. return "";
  177. }
  178. return '?v=' . self::$versionHash;
  179. }
  180. /**
  181. * @param array $styles
  182. * @return array
  183. */
  184. static public function findStylesheetFiles($styles, $compileScss = true) {
  185. // Read the selected theme from the config file
  186. $theme = \OC_Util::getTheme();
  187. if($compileScss) {
  188. /** @var \OC\Memcache\Factory $cache */
  189. $cache = \OC::$server->query('MemCacheFactory');
  190. $SCSSCacher = new SCSSCacher(
  191. \OC::$server->getLogger(),
  192. \OC::$server->getAppDataDir('css'),
  193. \OC::$server->getURLGenerator(),
  194. \OC::$server->getConfig(),
  195. \OC::$SERVERROOT,
  196. $cache->createLocal('SCSS')
  197. );
  198. } else {
  199. $SCSSCacher = null;
  200. }
  201. $locator = new \OC\Template\CSSResourceLocator(
  202. \OC::$server->getLogger(),
  203. $theme,
  204. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  205. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  206. $SCSSCacher);
  207. $locator->find($styles);
  208. return $locator->getResources();
  209. }
  210. /**
  211. * @param array $scripts
  212. * @return array
  213. */
  214. static public function findJavascriptFiles($scripts) {
  215. // Read the selected theme from the config file
  216. $theme = \OC_Util::getTheme();
  217. $locator = new \OC\Template\JSResourceLocator(
  218. \OC::$server->getLogger(),
  219. $theme,
  220. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  221. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  222. new JSCombiner(
  223. \OC::$server->getAppDataDir('js'),
  224. \OC::$server->getURLGenerator(),
  225. \OC::$server->getMemCacheFactory()->create('JS'),
  226. \OC::$server->getSystemConfig()
  227. )
  228. );
  229. $locator->find($scripts);
  230. return $locator->getResources();
  231. }
  232. /**
  233. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  234. * @param string $filePath Absolute path
  235. * @return string Relative path
  236. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  237. */
  238. public static function convertToRelativePath($filePath) {
  239. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  240. if(count($relativePath) !== 2) {
  241. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  242. }
  243. return $relativePath[1];
  244. }
  245. }