templatelayout.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. /**
  3. * @author Adam Williamson <awilliam@redhat.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Clark Tomlinson <fallen013@gmail.com>
  8. * @author Joas Schilling <nickvergessen@owncloud.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@owncloud.com>
  11. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  12. * @author Remco Brenninkmeijer <requist1@starmail.nl>
  13. * @author Robin Appelman <icewind@owncloud.com>
  14. * @author Robin McCorkell <rmccorkell@karoshi.org.uk>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. * @author Victor Dubiniuk <dubiniuk@owncloud.com>
  17. *
  18. * @copyright Copyright (c) 2015, ownCloud, Inc.
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. use Assetic\Asset\AssetCollection;
  35. use Assetic\Asset\FileAsset;
  36. use Assetic\AssetWriter;
  37. use Assetic\Filter\CssImportFilter;
  38. use Assetic\Filter\CssMinFilter;
  39. use Assetic\Filter\CssRewriteFilter;
  40. use Assetic\Filter\JSMinFilter;
  41. use OC\Assetic\SeparatorFilter; // waiting on upstream
  42. /**
  43. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  44. * This file is licensed under the Affero General Public License version 3 or
  45. * later.
  46. * See the COPYING-README file.
  47. */
  48. class OC_TemplateLayout extends OC_Template {
  49. private static $versionHash = '';
  50. /**
  51. * @var \OCP\IConfig
  52. */
  53. private $config;
  54. /**
  55. * @param string $renderAs
  56. * @param string $appId application id
  57. */
  58. public function __construct( $renderAs, $appId = '' ) {
  59. // yes - should be injected ....
  60. $this->config = \OC::$server->getConfig();
  61. // Decide which page we show
  62. if($renderAs == 'user') {
  63. parent::__construct( 'core', 'layout.user' );
  64. if(in_array(OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
  65. $this->assign('bodyid', 'body-settings');
  66. }else{
  67. $this->assign('bodyid', 'body-user');
  68. }
  69. // Update notification
  70. if($this->config->getSystemValue('updatechecker', true) === true &&
  71. OC_User::isAdminUser(OC_User::getUser())) {
  72. $updater = new \OC\Updater(\OC::$server->getHTTPHelper(),
  73. \OC::$server->getConfig());
  74. $data = $updater->check();
  75. if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array()) {
  76. $this->assign('updateAvailable', true);
  77. $this->assign('updateVersion', $data['versionstring']);
  78. $this->assign('updateLink', $data['web']);
  79. } else {
  80. $this->assign('updateAvailable', false); // No update available or not an admin user
  81. }
  82. } else {
  83. $this->assign('updateAvailable', false); // Update check is disabled
  84. }
  85. // Add navigation entry
  86. $this->assign( 'application', '', false );
  87. $this->assign( 'appid', $appId );
  88. $navigation = OC_App::getNavigation();
  89. $this->assign( 'navigation', $navigation);
  90. $this->assign( 'settingsnavigation', OC_App::getSettingsNavigation());
  91. foreach($navigation as $entry) {
  92. if ($entry['active']) {
  93. $this->assign( 'application', $entry['name'] );
  94. break;
  95. }
  96. }
  97. $userDisplayName = OC_User::getDisplayName();
  98. $this->assign('user_displayname', $userDisplayName);
  99. $this->assign('user_uid', OC_User::getUser());
  100. $this->assign('appsmanagement_active', strpos(\OC::$server->getRequest()->getRequestUri(), OC_Helper::linkToRoute('settings_apps')) === 0 );
  101. $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true));
  102. $this->assign('userAvatarSet', \OC_Helper::userAvatarSet(OC_User::getUser()));
  103. } else if ($renderAs == 'error') {
  104. parent::__construct('core', 'layout.guest', '', false);
  105. $this->assign('bodyid', 'body-login');
  106. } else if ($renderAs == 'guest') {
  107. parent::__construct('core', 'layout.guest');
  108. $this->assign('bodyid', 'body-login');
  109. } else {
  110. parent::__construct('core', 'layout.base');
  111. }
  112. // Send the language to our layouts
  113. $this->assign('language', OC_L10N::findLanguage());
  114. if(empty(self::$versionHash)) {
  115. $v = OC_App::getAppVersions();
  116. $v['core'] = implode('.', \OC_Util::getVersion());
  117. self::$versionHash = md5(implode(',', $v));
  118. }
  119. $useAssetPipeline = self::isAssetPipelineEnabled();
  120. if ($useAssetPipeline) {
  121. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
  122. $this->generateAssets();
  123. } else {
  124. // Add the js files
  125. $jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
  126. $this->assign('jsfiles', array(), false);
  127. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  128. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
  129. }
  130. foreach($jsFiles as $info) {
  131. $web = $info[1];
  132. $file = $info[2];
  133. $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  134. }
  135. // Add the css files
  136. $cssFiles = self::findStylesheetFiles(OC_Util::$styles);
  137. $this->assign('cssfiles', array());
  138. foreach($cssFiles as $info) {
  139. $web = $info[1];
  140. $file = $info[2];
  141. $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  142. }
  143. }
  144. }
  145. /**
  146. * @param array $styles
  147. * @return array
  148. */
  149. static public function findStylesheetFiles($styles) {
  150. // Read the selected theme from the config file
  151. $theme = OC_Util::getTheme();
  152. $locator = new \OC\Template\CSSResourceLocator(
  153. OC::$server->getLogger(),
  154. $theme,
  155. array( OC::$SERVERROOT => OC::$WEBROOT ),
  156. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  157. $locator->find($styles);
  158. return $locator->getResources();
  159. }
  160. /**
  161. * @param array $scripts
  162. * @return array
  163. */
  164. static public function findJavascriptFiles($scripts) {
  165. // Read the selected theme from the config file
  166. $theme = OC_Util::getTheme();
  167. $locator = new \OC\Template\JSResourceLocator(
  168. OC::$server->getLogger(),
  169. $theme,
  170. array( OC::$SERVERROOT => OC::$WEBROOT ),
  171. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  172. $locator->find($scripts);
  173. return $locator->getResources();
  174. }
  175. public function generateAssets() {
  176. $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT);
  177. $jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
  178. $jsHash = self::hashFileNames($jsFiles);
  179. if (!file_exists("$assetDir/assets/$jsHash.js")) {
  180. $jsFiles = array_map(function ($item) {
  181. $root = $item[0];
  182. $file = $item[2];
  183. // no need to minifiy minified files
  184. if (substr($file, -strlen('.min.js')) === '.min.js') {
  185. return new FileAsset($root . '/' . $file, array(
  186. new SeparatorFilter(';')
  187. ), $root, $file);
  188. }
  189. return new FileAsset($root . '/' . $file, array(
  190. new JSMinFilter(),
  191. new SeparatorFilter(';')
  192. ), $root, $file);
  193. }, $jsFiles);
  194. $jsCollection = new AssetCollection($jsFiles);
  195. $jsCollection->setTargetPath("assets/$jsHash.js");
  196. $writer = new AssetWriter($assetDir);
  197. $writer->writeAsset($jsCollection);
  198. }
  199. $cssFiles = self::findStylesheetFiles(OC_Util::$styles);
  200. $cssHash = self::hashFileNames($cssFiles);
  201. if (!file_exists("$assetDir/assets/$cssHash.css")) {
  202. $cssFiles = array_map(function ($item) {
  203. $root = $item[0];
  204. $file = $item[2];
  205. $assetPath = $root . '/' . $file;
  206. $sourceRoot = \OC::$SERVERROOT;
  207. $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT));
  208. return new FileAsset(
  209. $assetPath,
  210. array(
  211. new CssRewriteFilter(),
  212. new CssMinFilter(),
  213. new CssImportFilter()
  214. ),
  215. $sourceRoot,
  216. $sourcePath
  217. );
  218. }, $cssFiles);
  219. $cssCollection = new AssetCollection($cssFiles);
  220. $cssCollection->setTargetPath("assets/$cssHash.css");
  221. $writer = new AssetWriter($assetDir);
  222. $writer->writeAsset($cssCollection);
  223. }
  224. $this->append('jsfiles', OC_Helper::linkTo('assets', "$jsHash.js"));
  225. $this->append('cssfiles', OC_Helper::linkTo('assets', "$cssHash.css"));
  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. /**
  241. * @param array $files
  242. * @return string
  243. */
  244. private static function hashFileNames($files) {
  245. foreach($files as $i => $file) {
  246. try {
  247. $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2];
  248. } catch (\Exception $e) {
  249. $files[$i] = $file[0].'/'.$file[2];
  250. }
  251. }
  252. sort($files);
  253. // include the apps' versions hash to invalidate the cached assets
  254. $files[] = self::$versionHash;
  255. return hash('md5', implode('', $files));
  256. }
  257. }