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.

230 lines
7.0 KiB

  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();
  40. $data = $updater->check('http://apps.owncloud.com/updater.php');
  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. } else if ($renderAs == 'error') {
  69. parent::__construct('core', 'layout.guest', '', false);
  70. $this->assign('bodyid', 'body-login');
  71. } else if ($renderAs == 'guest') {
  72. parent::__construct('core', 'layout.guest');
  73. $this->assign('bodyid', 'body-login');
  74. } else {
  75. parent::__construct('core', 'layout.base');
  76. }
  77. if(empty(self::$versionHash)) {
  78. self::$versionHash = md5(implode(',', OC_App::getAppVersions()));
  79. }
  80. $useAssetPipeline = self::isAssetPipelineEnabled();
  81. if ($useAssetPipeline) {
  82. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
  83. $this->generateAssets();
  84. } else {
  85. // Add the js files
  86. $jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
  87. $this->assign('jsfiles', array(), false);
  88. if ($this->config->getSystemValue('installed', false) && $renderAs != 'error') {
  89. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config', array('v' => self::$versionHash)));
  90. }
  91. foreach($jsFiles as $info) {
  92. $web = $info[1];
  93. $file = $info[2];
  94. $this->append( 'jsfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  95. }
  96. // Add the css files
  97. $cssFiles = self::findStylesheetFiles(OC_Util::$styles);
  98. $this->assign('cssfiles', array());
  99. foreach($cssFiles as $info) {
  100. $web = $info[1];
  101. $file = $info[2];
  102. $this->append( 'cssfiles', $web.'/'.$file . '?v=' . self::$versionHash);
  103. }
  104. }
  105. }
  106. /**
  107. * @param array $styles
  108. * @return array
  109. */
  110. static public function findStylesheetFiles($styles) {
  111. // Read the selected theme from the config file
  112. $theme = OC_Util::getTheme();
  113. // Read the detected form factor and use the right file name.
  114. $formFactorExt = self::getFormFactorExtension();
  115. $locator = new \OC\Template\CSSResourceLocator( $theme, $formFactorExt,
  116. array( OC::$SERVERROOT => OC::$WEBROOT ),
  117. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  118. $locator->find($styles);
  119. return $locator->getResources();
  120. }
  121. /**
  122. * @param array $scripts
  123. * @return array
  124. */
  125. static public function findJavascriptFiles($scripts) {
  126. // Read the selected theme from the config file
  127. $theme = OC_Util::getTheme();
  128. // Read the detected form factor and use the right file name.
  129. $formFactorExt = self::getFormFactorExtension();
  130. $locator = new \OC\Template\JSResourceLocator( $theme, $formFactorExt,
  131. array( OC::$SERVERROOT => OC::$WEBROOT ),
  132. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  133. $locator->find($scripts);
  134. return $locator->getResources();
  135. }
  136. public function generateAssets() {
  137. $jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
  138. $jsHash = self::hashScriptNames($jsFiles);
  139. if (!file_exists("assets/$jsHash.js")) {
  140. $jsFiles = array_map(function ($item) {
  141. $root = $item[0];
  142. $file = $item[2];
  143. // no need to minifiy minified files
  144. if (substr($file, -strlen('.min.js')) === '.min.js') {
  145. return new FileAsset($root . '/' . $file, array(), $root, $file);
  146. }
  147. return new FileAsset($root . '/' . $file, array(
  148. new JSMinFilter()
  149. ), $root, $file);
  150. }, $jsFiles);
  151. $jsCollection = new AssetCollection($jsFiles);
  152. $jsCollection->setTargetPath("assets/$jsHash.js");
  153. $writer = new AssetWriter(\OC::$SERVERROOT);
  154. $writer->writeAsset($jsCollection);
  155. }
  156. $cssFiles = self::findStylesheetFiles(OC_Util::$styles);
  157. $cssHash = self::hashScriptNames($cssFiles);
  158. if (!file_exists("assets/$cssHash.css")) {
  159. $cssFiles = array_map(function ($item) {
  160. $root = $item[0];
  161. $file = $item[2];
  162. $assetPath = $root . '/' . $file;
  163. $sourceRoot = \OC::$SERVERROOT;
  164. $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT));
  165. return new FileAsset(
  166. $assetPath,
  167. array(
  168. new CssRewriteFilter(),
  169. new CssMinFilter(),
  170. new CssImportFilter()
  171. ),
  172. $sourceRoot,
  173. $sourcePath
  174. );
  175. }, $cssFiles);
  176. $cssCollection = new AssetCollection($cssFiles);
  177. $cssCollection->setTargetPath("assets/$cssHash.css");
  178. $writer = new AssetWriter(\OC::$SERVERROOT);
  179. $writer->writeAsset($cssCollection);
  180. }
  181. $this->append('jsfiles', OC_Helper::linkTo('assets', "$jsHash.js"));
  182. $this->append('cssfiles', OC_Helper::linkTo('assets', "$cssHash.css"));
  183. }
  184. /**
  185. * @param array $files
  186. * @return string
  187. */
  188. private static function hashScriptNames($files) {
  189. $files = array_map(function ($item) {
  190. $root = $item[0];
  191. $file = $item[2];
  192. return $root . '/' . $file;
  193. }, $files);
  194. sort($files);
  195. // include the apps' versions hash to invalidate the cached assets
  196. $files[] = self::$versionHash;
  197. return hash('md5', implode('', $files));
  198. }
  199. }