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

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