TemplateLayout.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. 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. // Decide which page we show
  59. if($renderAs == 'user') {
  60. parent::__construct( 'core', 'layout.user' );
  61. if(in_array(\OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
  62. $this->assign('bodyid', 'body-settings');
  63. }else{
  64. $this->assign('bodyid', 'body-user');
  65. }
  66. // Code integrity notification
  67. $integrityChecker = \OC::$server->getIntegrityCodeChecker();
  68. if(\OC_User::isAdminUser(\OC_User::getUser()) && $integrityChecker->isCodeCheckEnforced() && !$integrityChecker->hasPassedCheck()) {
  69. \OCP\Util::addScript('core', 'integritycheck-failed-notification');
  70. }
  71. // Add navigation entry
  72. $this->assign( 'application', '');
  73. $this->assign( 'appid', $appId );
  74. $navigation = \OC_App::getNavigation();
  75. $this->assign( 'navigation', $navigation);
  76. $settingsNavigation = \OC_App::getSettingsNavigation();
  77. $this->assign( 'settingsnavigation', $settingsNavigation);
  78. foreach($navigation as $entry) {
  79. if ($entry['active']) {
  80. $this->assign( 'application', $entry['name'] );
  81. break;
  82. }
  83. }
  84. foreach($settingsNavigation as $entry) {
  85. if ($entry['active']) {
  86. $this->assign( 'application', $entry['name'] );
  87. break;
  88. }
  89. }
  90. $userDisplayName = \OC_User::getDisplayName();
  91. $appsMgmtActive = strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0;
  92. if ($appsMgmtActive) {
  93. $l = \OC::$server->getL10N('lib');
  94. $this->assign('application', $l->t('Apps'));
  95. }
  96. $this->assign('user_displayname', $userDisplayName);
  97. $this->assign('user_uid', \OC_User::getUser());
  98. $this->assign('appsmanagement_active', $appsMgmtActive);
  99. $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true) === true);
  100. if (\OC_User::getUser() === false) {
  101. $this->assign('userAvatarSet', false);
  102. } else {
  103. $this->assign('userAvatarSet', \OC::$server->getAvatarManager()->getAvatar(\OC_User::getUser())->exists());
  104. $this->assign('userAvatarVersion', \OC::$server->getConfig()->getUserValue(\OC_User::getUser(), 'avatar', 'version', 0));
  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 {
  113. parent::__construct('core', 'layout.base');
  114. }
  115. // Send the language to our layouts
  116. $this->assign('language', \OC_L10N::findLanguage());
  117. if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
  118. if (empty(self::$versionHash)) {
  119. $v = \OC_App::getAppVersions();
  120. $v['core'] = implode('.', \OCP\Util::getVersion());
  121. self::$versionHash = md5(implode(',', $v));
  122. }
  123. } else {
  124. self::$versionHash = md5('not installed');
  125. }
  126. // Add the js files
  127. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  128. $this->assign('jsfiles', array());
  129. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  130. $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
  131. }
  132. foreach($jsFiles as $info) {
  133. $web = $info[1];
  134. $file = $info[2];
  135. $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  136. }
  137. // Add the css files
  138. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  139. $this->assign('cssfiles', array());
  140. $this->assign('printcssfiles', []);
  141. $this->assign('versionHash', self::$versionHash);
  142. foreach($cssFiles as $info) {
  143. $web = $info[1];
  144. $file = $info[2];
  145. if (substr($file, -strlen('print.css')) === 'print.css') {
  146. $this->append( 'printcssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  147. } else {
  148. $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  149. }
  150. }
  151. }
  152. /**
  153. * @param array $styles
  154. * @return array
  155. */
  156. static public function findStylesheetFiles($styles) {
  157. // Read the selected theme from the config file
  158. $theme = \OC_Util::getTheme();
  159. $locator = new \OC\Template\CSSResourceLocator(
  160. \OC::$server->getLogger(),
  161. $theme,
  162. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  163. array( \OC::$SERVERROOT => \OC::$WEBROOT ));
  164. $locator->find($styles);
  165. return $locator->getResources();
  166. }
  167. /**
  168. * @param array $scripts
  169. * @return array
  170. */
  171. static public function findJavascriptFiles($scripts) {
  172. // Read the selected theme from the config file
  173. $theme = \OC_Util::getTheme();
  174. $locator = new \OC\Template\JSResourceLocator(
  175. \OC::$server->getLogger(),
  176. $theme,
  177. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  178. array( \OC::$SERVERROOT => \OC::$WEBROOT ));
  179. $locator->find($scripts);
  180. return $locator->getResources();
  181. }
  182. /**
  183. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  184. * @param string $filePath Absolute path
  185. * @return string Relative path
  186. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  187. */
  188. public static function convertToRelativePath($filePath) {
  189. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  190. if(count($relativePath) !== 2) {
  191. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  192. }
  193. return $relativePath[1];
  194. }
  195. }