templatelayout.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Adam Williamson <awilliam@redhat.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Clark Tomlinson <fallen013@gmail.com>
  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. }
  105. } else if ($renderAs == 'error') {
  106. parent::__construct('core', 'layout.guest', '', false);
  107. $this->assign('bodyid', 'body-login');
  108. } else if ($renderAs == 'guest') {
  109. parent::__construct('core', 'layout.guest');
  110. $this->assign('bodyid', 'body-login');
  111. } else {
  112. parent::__construct('core', 'layout.base');
  113. }
  114. // Send the language to our layouts
  115. $this->assign('language', \OC_L10N::findLanguage());
  116. if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
  117. if (empty(self::$versionHash)) {
  118. $v = \OC_App::getAppVersions();
  119. $v['core'] = implode('.', \OCP\Util::getVersion());
  120. self::$versionHash = md5(implode(',', $v));
  121. }
  122. } else {
  123. self::$versionHash = md5('not installed');
  124. }
  125. $useAssetPipeline = self::isAssetPipelineEnabled();
  126. if ($useAssetPipeline) {
  127. $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
  128. $this->generateAssets();
  129. } else {
  130. // Add the js files
  131. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  132. $this->assign('jsfiles', array());
  133. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  134. $this->append( 'jsfiles', \OC::$server->getURLGenerator()->linkToRoute('js_config', ['v' => self::$versionHash]));
  135. }
  136. foreach($jsFiles as $info) {
  137. $web = $info[1];
  138. $file = $info[2];
  139. $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  140. }
  141. // Add the css files
  142. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  143. $this->assign('cssfiles', array());
  144. foreach($cssFiles as $info) {
  145. $web = $info[1];
  146. $file = $info[2];
  147. $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  148. }
  149. }
  150. }
  151. /**
  152. * @param array $styles
  153. * @return array
  154. */
  155. static public function findStylesheetFiles($styles) {
  156. // Read the selected theme from the config file
  157. $theme = \OC_Util::getTheme();
  158. $locator = new \OC\Template\CSSResourceLocator(
  159. \OC::$server->getLogger(),
  160. $theme,
  161. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  162. array( \OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT ));
  163. $locator->find($styles);
  164. return $locator->getResources();
  165. }
  166. /**
  167. * @param array $scripts
  168. * @return array
  169. */
  170. static public function findJavascriptFiles($scripts) {
  171. // Read the selected theme from the config file
  172. $theme = \OC_Util::getTheme();
  173. $locator = new \OC\Template\JSResourceLocator(
  174. \OC::$server->getLogger(),
  175. $theme,
  176. array( \OC::$SERVERROOT => \OC::$WEBROOT ),
  177. array( \OC::$THIRDPARTYROOT => \OC::$THIRDPARTYWEBROOT ));
  178. $locator->find($scripts);
  179. return $locator->getResources();
  180. }
  181. public function generateAssets() {
  182. $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT);
  183. $jsFiles = self::findJavascriptFiles(\OC_Util::$scripts);
  184. $jsHash = self::hashFileNames($jsFiles);
  185. if (!file_exists("$assetDir/assets/$jsHash.js")) {
  186. $jsFiles = array_map(function ($item) {
  187. $root = $item[0];
  188. $file = $item[2];
  189. // no need to minifiy minified files
  190. if (substr($file, -strlen('.min.js')) === '.min.js') {
  191. return new FileAsset($root . '/' . $file, array(
  192. new SeparatorFilter(';')
  193. ), $root, $file);
  194. }
  195. return new FileAsset($root . '/' . $file, array(
  196. new JSqueezeFilter(),
  197. new SeparatorFilter(';')
  198. ), $root, $file);
  199. }, $jsFiles);
  200. $jsCollection = new AssetCollection($jsFiles);
  201. $jsCollection->setTargetPath("assets/$jsHash.js");
  202. $writer = new AssetWriter($assetDir);
  203. $writer->writeAsset($jsCollection);
  204. }
  205. $cssFiles = self::findStylesheetFiles(\OC_Util::$styles);
  206. $cssHash = self::hashFileNames($cssFiles);
  207. if (!file_exists("$assetDir/assets/$cssHash.css")) {
  208. $cssFiles = array_map(function ($item) {
  209. $root = $item[0];
  210. $file = $item[2];
  211. $assetPath = $root . '/' . $file;
  212. $sourceRoot = \OC::$SERVERROOT;
  213. $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT));
  214. return new FileAsset(
  215. $assetPath,
  216. array(
  217. new CssRewriteFilter(),
  218. new CssMinFilter(),
  219. new CssImportFilter()
  220. ),
  221. $sourceRoot,
  222. $sourcePath
  223. );
  224. }, $cssFiles);
  225. $cssCollection = new AssetCollection($cssFiles);
  226. $cssCollection->setTargetPath("assets/$cssHash.css");
  227. $writer = new AssetWriter($assetDir);
  228. $writer->writeAsset($cssCollection);
  229. }
  230. $this->append('jsfiles', \OC::$server->getURLGenerator()->linkTo('assets', "$jsHash.js"));
  231. $this->append('cssfiles', \OC::$server->getURLGenerator()->linkTo('assets', "$cssHash.css"));
  232. }
  233. /**
  234. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  235. * @param string $filePath Absolute path
  236. * @return string Relative path
  237. * @throws \Exception If $filePath is not under \OC::$SERVERROOT
  238. */
  239. public static function convertToRelativePath($filePath) {
  240. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  241. if(count($relativePath) !== 2) {
  242. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  243. }
  244. return $relativePath[1];
  245. }
  246. /**
  247. * @param array $files
  248. * @return string
  249. */
  250. private static function hashFileNames($files) {
  251. foreach($files as $i => $file) {
  252. try {
  253. $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2];
  254. } catch (\Exception $e) {
  255. $files[$i] = $file[0].'/'.$file[2];
  256. }
  257. }
  258. sort($files);
  259. // include the apps' versions hash to invalidate the cached assets
  260. $files[] = self::$versionHash;
  261. return hash('md5', implode('', $files));
  262. }
  263. }