templatelayout.php 9.5 KB

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