TemplateLayout.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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\JSConfigHelper;
  38. use OC\Template\SCSSCacher;
  39. class TemplateLayout extends \OC_Template {
  40. private static $versionHash = '';
  41. /**
  42. * @var \OCP\IConfig
  43. */
  44. private $config;
  45. /**
  46. * @param string $renderAs
  47. * @param string $appId application id
  48. */
  49. public function __construct( $renderAs, $appId = '' ) {
  50. // yes - should be injected ....
  51. $this->config = \OC::$server->getConfig();
  52. // Decide which page we show
  53. if($renderAs == 'user') {
  54. parent::__construct( 'core', 'layout.user' );
  55. if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
  56. $this->assign('bodyid', 'body-settings');
  57. }else{
  58. $this->assign('bodyid', 'body-user');
  59. }
  60. // Code integrity notification
  61. $integrityChecker = \OC::$server->getIntegrityCodeChecker();
  62. if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
  63. \OCP\Util::addScript('core', 'integritycheck-failed-notification');
  64. }
  65. // Add navigation entry
  66. $this->assign( 'application', '');
  67. $this->assign( 'appid', $appId );
  68. $navigation = \OC_App::getNavigation();
  69. $this->assign( 'navigation', $navigation);
  70. $navigation = \OC_App::getHeaderNavigation();
  71. $this->assign( 'headernavigation', $navigation);
  72. $settingsNavigation = \OC_App::getSettingsNavigation();
  73. $this->assign( 'settingsnavigation', $settingsNavigation);
  74. foreach($navigation as $entry) {
  75. if ($entry['active']) {
  76. $this->assign( 'application', $entry['name'] );
  77. break;
  78. }
  79. }
  80. foreach($settingsNavigation as $entry) {
  81. if ($entry['active']) {
  82. $this->assign( 'application', $entry['name'] );
  83. break;
  84. }
  85. }
  86. $userDisplayName = \OC_User::getDisplayName();
  87. $appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0;
  88. if ($appsMgmtActive) {
  89. $l = \OC::$server->getL10N('lib');
  90. $this->assign('application', $l->t('Apps'));
  91. }
  92. $this->assign('user_displayname', $userDisplayName);
  93. $this->assign('user_uid', \OC_User::getUser());
  94. $this->assign('appsmanagement_active', $appsMgmtActive);
  95. if (\OC_User::getUser() === false) {
  96. $this->assign('userAvatarSet', false);
  97. } else {
  98. $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
  99. $this->assign('userAvatarVersion', \OC::$server->getConfig()->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
  100. }
  101. } else if ($renderAs == 'error') {
  102. parent::__construct('core', 'layout.guest', '', false);
  103. $this->assign('bodyid', 'body-login');
  104. } else if ($renderAs == 'guest') {
  105. parent::__construct('core', 'layout.guest');
  106. $this->assign('bodyid', 'body-login');
  107. } else {
  108. parent::__construct('core', 'layout.base');
  109. }
  110. // Send the language to our layouts
  111. $this->assign('language', \OC::$server->getL10NFactory()->findLanguage());
  112. if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
  113. if (empty(self::$versionHash)) {
  114. $v = \OC_App::getAppVersions();
  115. $v['core'] = implode('.', \OCP\Util::getVersion());
  116. self::$versionHash = md5(implode(',', $v));
  117. }
  118. } else {
  119. self::$versionHash = md5('not installed');
  120. }
  121. // Add the js files
  122. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  123. $this->assign('jsfiles', array());
  124. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  125. if (\OC::$server->getContentSecurityPolicyNonceManager()->browserSupportsCspV3()) {
  126. $jsConfigHelper = new JSConfigHelper(
  127. \OC::$server->getL10N('core'),
  128. \OC::$server->getThemingDefaults(),
  129. \OC::$server->getAppManager(),
  130. \OC::$server->getSession(),
  131. \OC::$server->getUserSession()->getUser(),
  132. \OC::$server->getConfig(),
  133. \OC::$server->getGroupManager(),
  134. \OC::$server->getIniWrapper(),
  135. \OC::$server->getURLGenerator()
  136. );
  137. $this->assign('inline_ocjs', $jsConfigHelper->getConfig());
  138. } else {
  139. $this->append('jsfiles', \OC::$server->getURLGenerator()->linkToRoute('core.OCJS.getConfig', ['v' => self::$versionHash]));
  140. }
  141. }
  142. foreach($jsFiles as $info) {
  143. $web = $info[1];
  144. $file = $info[2];
  145. $this->append( 'jsfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  146. }
  147. try {
  148. $pathInfo = \OC::$server->getRequest()->getPathInfo();
  149. } catch (\Exception $e) {
  150. $pathInfo = '';
  151. }
  152. // Do not initialise scss appdata until we have a fully installed instance
  153. // Do not load scss for update, errors, installation or login page
  154. if(\OC::$server->getSystemConfig()->getValue('installed', false)
  155. && !\OCP\Util::needUpgrade()
  156. && $pathInfo !== ''
  157. && !preg_match('/^\/login/', $pathInfo)) {
  158. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  159. } else {
  160. // If we ignore the scss compiler,
  161. // we need to load the guest css fallback
  162. \OC_Util::addStyle('guest');
  163. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles, false);
  164. }
  165. $this->assign('cssfiles', array());
  166. $this->assign('printcssfiles', []);
  167. $this->assign('versionHash', self::$versionHash);
  168. foreach($cssFiles as $info) {
  169. $web = $info[1];
  170. $file = $info[2];
  171. if (substr($file, -strlen('print.css')) === 'print.css') {
  172. $this->append( 'printcssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  173. } else {
  174. $this->append( 'cssfiles', $web.'/'.$file . $this->getVersionHashSuffix() );
  175. }
  176. }
  177. }
  178. protected function getVersionHashSuffix() {
  179. if(\OC::$server->getConfig()->getSystemValue('debug', false)) {
  180. // allows chrome workspace mapping in debug mode
  181. return "";
  182. }
  183. return '?v=' . self::$versionHash;
  184. }
  185. /**
  186. * @param array $styles
  187. * @return array
  188. */
  189. static public function findStylesheetFiles($styles, $compileScss = true) {
  190. // Read the selected theme from the config file
  191. $theme = \OC_Util::getTheme();
  192. if($compileScss) {
  193. $SCSSCacher = new SCSSCacher(
  194. \OC::$server->getLogger(),
  195. \OC::$server->getAppDataDir('css'),
  196. \OC::$server->getURLGenerator(),
  197. \OC::$server->getConfig(),
  198. \OC::$SERVERROOT
  199. );
  200. } else {
  201. $SCSSCacher = null;
  202. }
  203. $locator = new \OC\Template\CSSResourceLocator(
  204. \OC::$server->getLogger(),
  205. $theme,
  206. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  207. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  208. $SCSSCacher);
  209. $locator->find($styles);
  210. return $locator->getResources();
  211. }
  212. /**
  213. * @param array $scripts
  214. * @return array
  215. */
  216. static public function findJavascriptFiles($scripts) {
  217. // Read the selected theme from the config file
  218. $theme = \OC_Util::getTheme();
  219. $locator = new \OC\Template\JSResourceLocator(
  220. \OC::$server->getLogger(),
  221. $theme,
  222. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  223. array( \OC::$SERVERROOT => \OC::$WEBROOT ));
  224. $locator->find($scripts);
  225. return $locator->getResources();
  226. }
  227. /**
  228. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  229. * @param string $filePath Absolute path
  230. * @return string Relative path
  231. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  232. */
  233. public static function convertToRelativePath($filePath) {
  234. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  235. if(count($relativePath) !== 2) {
  236. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  237. }
  238. return $relativePath[1];
  239. }
  240. }