templatelayout.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. \OCP\Util::addScript('core', 'update-notification');
  80. } else {
  81. $this->assign('updateAvailable', false); // No update available or not an admin user
  82. }
  83. } else {
  84. $this->assign('updateAvailable', false); // Update check is disabled
  85. }
  86. // Add navigation entry
  87. $this->assign( 'application', '', false );
  88. $this->assign( 'appid', $appId );
  89. $navigation = OC_App::getNavigation();
  90. $this->assign( 'navigation', $navigation);
  91. $this->assign( 'settingsnavigation', OC_App::getSettingsNavigation());
  92. foreach($navigation as $entry) {
  93. if ($entry['active']) {
  94. $this->assign( 'application', $entry['name'] );
  95. break;
  96. }
  97. }
  98. $userDisplayName = OC_User::getDisplayName();
  99. $this->assign('user_displayname', $userDisplayName);
  100. $this->assign('user_uid', OC_User::getUser());
  101. $this->assign('appsmanagement_active', strpos(\OC::$server->getRequest()->getRequestUri(), \OC::$server->getURLGenerator()->linkToRoute('settings.AppSettings.viewApps')) === 0 );
  102. $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true));
  103. $this->assign('userAvatarSet', \OC_Helper::userAvatarSet(OC_User::getUser()));
  104. } else if ($renderAs == 'error') {
  105. parent::__construct('core', 'layout.guest', '', false);
  106. $this->assign('bodyid', 'body-login');
  107. } else if ($renderAs == 'guest') {
  108. parent::__construct('core', 'layout.guest');
  109. $this->assign('bodyid', 'body-login');
  110. } else {
  111. parent::__construct('core', 'layout.base');
  112. }
  113. // Send the language to our layouts
  114. $this->assign('language', OC_L10N::findLanguage());
  115. if(empty(self::$versionHash)) {
  116. $v = OC_App::getAppVersions();
  117. $v['core'] = implode('.', \OC_Util::getVersion());
  118. self::$versionHash = md5(implode(',', $v));
  119. }
  120. $useAssetPipeline = self::isAssetPipelineEnabled();
  121. if ($useAssetPipeline) {
  122. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
  123. $this->generateAssets();
  124. } else {
  125. // Add the js files
  126. $jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
  127. $this->assign('jsfiles', array(), false);
  128. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  129. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
  130. }
  131. foreach($jsFiles as $info) {
  132. $web = $info[1];
  133. $file = $info[2];
  134. $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  135. }
  136. // Add the css files
  137. $cssFiles = self::findStylesheetFiles(OC_Util::$styles);
  138. $this->assign('cssfiles', array());
  139. foreach($cssFiles as $info) {
  140. $web = $info[1];
  141. $file = $info[2];
  142. $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  143. }
  144. }
  145. }
  146. /**
  147. * @param array $styles
  148. * @return array
  149. */
  150. static public function findStylesheetFiles($styles) {
  151. // Read the selected theme from the config file
  152. $theme = OC_Util::getTheme();
  153. $locator = new \OC\Template\CSSResourceLocator(
  154. OC::$server->getLogger(),
  155. $theme,
  156. array( OC::$SERVERROOT => OC::$WEBROOT ),
  157. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  158. $locator->find($styles);
  159. return $locator->getResources();
  160. }
  161. /**
  162. * @param array $scripts
  163. * @return array
  164. */
  165. static public function findJavascriptFiles($scripts) {
  166. // Read the selected theme from the config file
  167. $theme = OC_Util::getTheme();
  168. $locator = new \OC\Template\JSResourceLocator(
  169. OC::$server->getLogger(),
  170. $theme,
  171. array( OC::$SERVERROOT => OC::$WEBROOT ),
  172. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  173. $locator->find($scripts);
  174. return $locator->getResources();
  175. }
  176. public function generateAssets() {
  177. $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT);
  178. $jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
  179. $jsHash = self::hashFileNames($jsFiles);
  180. if (!file_exists("$assetDir/assets/$jsHash.js")) {
  181. $jsFiles = array_map(function ($item) {
  182. $root = $item[0];
  183. $file = $item[2];
  184. // no need to minifiy minified files
  185. if (substr($file, -strlen('.min.js')) === '.min.js') {
  186. return new FileAsset($root . '/' . $file, array(
  187. new SeparatorFilter(';')
  188. ), $root, $file);
  189. }
  190. return new FileAsset($root . '/' . $file, array(
  191. new JSMinFilter(),
  192. new SeparatorFilter(';')
  193. ), $root, $file);
  194. }, $jsFiles);
  195. $jsCollection = new AssetCollection($jsFiles);
  196. $jsCollection->setTargetPath("assets/$jsHash.js");
  197. $writer = new AssetWriter($assetDir);
  198. $writer->writeAsset($jsCollection);
  199. }
  200. $cssFiles = self::findStylesheetFiles(OC_Util::$styles);
  201. $cssHash = self::hashFileNames($cssFiles);
  202. if (!file_exists("$assetDir/assets/$cssHash.css")) {
  203. $cssFiles = array_map(function ($item) {
  204. $root = $item[0];
  205. $file = $item[2];
  206. $assetPath = $root . '/' . $file;
  207. $sourceRoot = \OC::$SERVERROOT;
  208. $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT));
  209. return new FileAsset(
  210. $assetPath,
  211. array(
  212. new CssRewriteFilter(),
  213. new CssMinFilter(),
  214. new CssImportFilter()
  215. ),
  216. $sourceRoot,
  217. $sourcePath
  218. );
  219. }, $cssFiles);
  220. $cssCollection = new AssetCollection($cssFiles);
  221. $cssCollection->setTargetPath("assets/$cssHash.css");
  222. $writer = new AssetWriter($assetDir);
  223. $writer->writeAsset($cssCollection);
  224. }
  225. $this->append('jsfiles', OC_Helper::linkTo('assets', "$jsHash.js"));
  226. $this->append('cssfiles', OC_Helper::linkTo('assets', "$cssHash.css"));
  227. }
  228. /**
  229. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  230. * @param string $filePath Absolute path
  231. * @return string Relative path
  232. * @throws Exception If $filePath is not under \OC::$SERVERROOT
  233. */
  234. public static function convertToRelativePath($filePath) {
  235. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  236. if(count($relativePath) !== 2) {
  237. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  238. }
  239. return $relativePath[1];
  240. }
  241. /**
  242. * @param array $files
  243. * @return string
  244. */
  245. private static function hashFileNames($files) {
  246. foreach($files as $i => $file) {
  247. try {
  248. $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2];
  249. } catch (\Exception $e) {
  250. $files[$i] = $file[0].'/'.$file[2];
  251. }
  252. }
  253. sort($files);
  254. // include the apps' versions hash to invalidate the cached assets
  255. $files[] = self::$versionHash;
  256. return hash('md5', implode('', $files));
  257. }
  258. }