You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

templatelayout.php 7.4KB

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