templatelayout.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. use Assetic\Asset\AssetCollection;
  3. use Assetic\Asset\FileAsset;
  4. use Assetic\AssetWriter;
  5. use Assetic\Filter\CssImportFilter;
  6. use Assetic\Filter\CssMinFilter;
  7. use Assetic\Filter\CssRewriteFilter;
  8. use Assetic\Filter\JSMinFilter;
  9. use OC\Assetic\SeparatorFilter; // waiting on upstream
  10. /**
  11. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  12. * This file is licensed under the Affero General Public License version 3 or
  13. * later.
  14. * See the COPYING-README file.
  15. */
  16. class OC_TemplateLayout extends OC_Template {
  17. private static $versionHash = '';
  18. /**
  19. * @var \OCP\IConfig
  20. */
  21. private $config;
  22. /**
  23. * @param string $renderAs
  24. * @param string $appId application id
  25. */
  26. public function __construct( $renderAs, $appId = '' ) {
  27. // yes - should be injected ....
  28. $this->config = \OC::$server->getConfig();
  29. // Decide which page we show
  30. if($renderAs == 'user') {
  31. parent::__construct( 'core', 'layout.user' );
  32. if(in_array(OC_App::getCurrentApp(), ['settings','admin', 'help']) !== false) {
  33. $this->assign('bodyid', 'body-settings');
  34. }else{
  35. $this->assign('bodyid', 'body-user');
  36. }
  37. // Update notification
  38. if($this->config->getSystemValue('updatechecker', true) === true &&
  39. OC_User::isAdminUser(OC_User::getUser())) {
  40. $updater = new \OC\Updater(\OC::$server->getHTTPHelper(),
  41. \OC::$server->getConfig());
  42. $data = $updater->check();
  43. if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array()) {
  44. $this->assign('updateAvailable', true);
  45. $this->assign('updateVersion', $data['versionstring']);
  46. $this->assign('updateLink', $data['web']);
  47. } else {
  48. $this->assign('updateAvailable', false); // No update available or not an admin user
  49. }
  50. } else {
  51. $this->assign('updateAvailable', false); // Update check is disabled
  52. }
  53. // Add navigation entry
  54. $this->assign( 'application', '', false );
  55. $this->assign( 'appid', $appId );
  56. $navigation = OC_App::getNavigation();
  57. $this->assign( 'navigation', $navigation);
  58. $this->assign( 'settingsnavigation', OC_App::getSettingsNavigation());
  59. foreach($navigation as $entry) {
  60. if ($entry['active']) {
  61. $this->assign( 'application', $entry['name'] );
  62. break;
  63. }
  64. }
  65. $userDisplayName = OC_User::getDisplayName();
  66. $this->assign('user_displayname', $userDisplayName);
  67. $this->assign('user_uid', OC_User::getUser());
  68. $this->assign('appsmanagement_active', strpos(\OC::$server->getRequest()->getRequestUri(), OC_Helper::linkToRoute('settings_apps')) === 0 );
  69. $this->assign('enableAvatars', $this->config->getSystemValue('enable_avatars', true));
  70. $this->assign('userAvatarSet', \OC_Helper::userAvatarSet(OC_User::getUser()));
  71. } else if ($renderAs == 'error') {
  72. parent::__construct('core', 'layout.guest', '', false);
  73. $this->assign('bodyid', 'body-login');
  74. } else if ($renderAs == 'guest') {
  75. parent::__construct('core', 'layout.guest');
  76. $this->assign('bodyid', 'body-login');
  77. } else {
  78. parent::__construct('core', 'layout.base');
  79. }
  80. // Send the language to our layouts
  81. $this->assign('language', OC_L10N::findLanguage());
  82. if(empty(self::$versionHash)) {
  83. $v = OC_App::getAppVersions();
  84. $v['core'] = implode('.', \OC_Util::getVersion());
  85. self::$versionHash = md5(implode(',', $v));
  86. }
  87. $useAssetPipeline = self::isAssetPipelineEnabled();
  88. if ($useAssetPipeline) {
  89. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
  90. $this->generateAssets();
  91. } else {
  92. // Add the js files
  93. $jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
  94. $this->assign('jsfiles', array(), false);
  95. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  96. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
  97. }
  98. foreach($jsFiles as $info) {
  99. $web = $info[1];
  100. $file = $info[2];
  101. $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  102. }
  103. // Add the css files
  104. $cssFiles = self::findStylesheetFiles(OC_Util::$styles);
  105. $this->assign('cssfiles', array());
  106. foreach($cssFiles as $info) {
  107. $web = $info[1];
  108. $file = $info[2];
  109. $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  110. }
  111. }
  112. }
  113. /**
  114. * @param array $styles
  115. * @return array
  116. */
  117. static public function findStylesheetFiles($styles) {
  118. // Read the selected theme from the config file
  119. $theme = OC_Util::getTheme();
  120. $locator = new \OC\Template\CSSResourceLocator(
  121. OC::$server->getLogger(),
  122. $theme,
  123. array( OC::$SERVERROOT => OC::$WEBROOT ),
  124. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  125. $locator->find($styles);
  126. return $locator->getResources();
  127. }
  128. /**
  129. * @param array $scripts
  130. * @return array
  131. */
  132. static public function findJavascriptFiles($scripts) {
  133. // Read the selected theme from the config file
  134. $theme = OC_Util::getTheme();
  135. $locator = new \OC\Template\JSResourceLocator(
  136. OC::$server->getLogger(),
  137. $theme,
  138. array( OC::$SERVERROOT => OC::$WEBROOT ),
  139. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  140. $locator->find($scripts);
  141. return $locator->getResources();
  142. }
  143. public function generateAssets() {
  144. $assetDir = \OC::$server->getConfig()->getSystemValue('assetdirectory', \OC::$SERVERROOT);
  145. $jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
  146. $jsHash = self::hashFileNames($jsFiles);
  147. if (!file_exists("$assetDir/assets/$jsHash.js")) {
  148. $jsFiles = array_map(function ($item) {
  149. $root = $item[0];
  150. $file = $item[2];
  151. // no need to minifiy minified files
  152. if (substr($file, -strlen('.min.js')) === '.min.js') {
  153. return new FileAsset($root . '/' . $file, array(
  154. new SeparatorFilter(';')
  155. ), $root, $file);
  156. }
  157. return new FileAsset($root . '/' . $file, array(
  158. new JSMinFilter(),
  159. new SeparatorFilter(';')
  160. ), $root, $file);
  161. }, $jsFiles);
  162. $jsCollection = new AssetCollection($jsFiles);
  163. $jsCollection->setTargetPath("assets/$jsHash.js");
  164. $writer = new AssetWriter($assetDir);
  165. $writer->writeAsset($jsCollection);
  166. }
  167. $cssFiles = self::findStylesheetFiles(OC_Util::$styles);
  168. $cssHash = self::hashFileNames($cssFiles);
  169. if (!file_exists("$assetDir/assets/$cssHash.css")) {
  170. $cssFiles = array_map(function ($item) {
  171. $root = $item[0];
  172. $file = $item[2];
  173. $assetPath = $root . '/' . $file;
  174. $sourceRoot = \OC::$SERVERROOT;
  175. $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT));
  176. return new FileAsset(
  177. $assetPath,
  178. array(
  179. new CssRewriteFilter(),
  180. new CssMinFilter(),
  181. new CssImportFilter()
  182. ),
  183. $sourceRoot,
  184. $sourcePath
  185. );
  186. }, $cssFiles);
  187. $cssCollection = new AssetCollection($cssFiles);
  188. $cssCollection->setTargetPath("assets/$cssHash.css");
  189. $writer = new AssetWriter($assetDir);
  190. $writer->writeAsset($cssCollection);
  191. }
  192. $this->append('jsfiles', OC_Helper::linkTo('assets', "$jsHash.js"));
  193. $this->append('cssfiles', OC_Helper::linkTo('assets', "$cssHash.css"));
  194. }
  195. /**
  196. * Converts the absolute file path to a relative path from \OC::$SERVERROOT
  197. * @param string $filePath Absolute path
  198. * @return string Relative path
  199. * @throws Exception If $filePath is not under \OC::$SERVERROOT
  200. */
  201. public static function convertToRelativePath($filePath) {
  202. $relativePath = explode(\OC::$SERVERROOT, $filePath);
  203. if(count($relativePath) !== 2) {
  204. throw new \Exception('$filePath is not under the \OC::$SERVERROOT');
  205. }
  206. return $relativePath[1];
  207. }
  208. /**
  209. * @param array $files
  210. * @return string
  211. */
  212. private static function hashFileNames($files) {
  213. foreach($files as $i => $file) {
  214. try {
  215. $files[$i] = self::convertToRelativePath($file[0]).'/'.$file[2];
  216. } catch (\Exception $e) {
  217. $files[$i] = $file[0].'/'.$file[2];
  218. }
  219. }
  220. sort($files);
  221. // include the apps' versions hash to invalidate the cached assets
  222. $files[] = self::$versionHash;
  223. return hash('md5', implode('', $files));
  224. }
  225. }