templatelayout.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. use Assetic\Asset\AssetCollection;
  3. use Assetic\Asset\FileAsset;
  4. use Assetic\AssetWriter;
  5. use Assetic\Filter\CssRewriteFilter;
  6. /**
  7. * Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
  8. * This file is licensed under the Affero General Public License version 3 or
  9. * later.
  10. * See the COPYING-README file.
  11. */
  12. class OC_TemplateLayout extends OC_Template {
  13. /**
  14. * @param string $renderas
  15. * @param string $appid application id
  16. */
  17. public function __construct( $renderas, $appid = '' ) {
  18. // Decide which page we show
  19. if( $renderas == 'user' ) {
  20. parent::__construct( 'core', 'layout.user' );
  21. if(in_array(OC_APP::getCurrentApp(), array('settings','admin', 'help'))!==false) {
  22. $this->assign('bodyid', 'body-settings');
  23. }else{
  24. $this->assign('bodyid', 'body-user');
  25. }
  26. // Update notification
  27. if(OC_Config::getValue('updatechecker', true) === true) {
  28. $data=OC_Updater::check();
  29. if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array() && OC_User::isAdminUser(OC_User::getUser())) {
  30. $this->assign('updateAvailable', true);
  31. $this->assign('updateVersion', $data['versionstring']);
  32. $this->assign('updateLink', $data['web']);
  33. } else {
  34. $this->assign('updateAvailable', false); // No update available or not an admin user
  35. }
  36. } else {
  37. $this->assign('updateAvailable', false); // Update check is disabled
  38. }
  39. // Add navigation entry
  40. $this->assign( 'application', '', false );
  41. $this->assign( 'appid', $appid );
  42. $navigation = OC_App::getNavigation();
  43. $this->assign( 'navigation', $navigation);
  44. $this->assign( 'settingsnavigation', OC_App::getSettingsNavigation());
  45. foreach($navigation as $entry) {
  46. if ($entry['active']) {
  47. $this->assign( 'application', $entry['name'] );
  48. break;
  49. }
  50. }
  51. $user_displayname = OC_User::getDisplayName();
  52. $this->assign( 'user_displayname', $user_displayname );
  53. $this->assign( 'user_uid', OC_User::getUser() );
  54. $this->assign( 'appsmanagement_active', strpos(OC_Request::requestUri(), OC_Helper::linkToRoute('settings_apps')) === 0 );
  55. $this->assign('enableAvatars', \OC_Config::getValue('enable_avatars', true));
  56. } else if ($renderas == 'guest' || $renderas == 'error') {
  57. parent::__construct('core', 'layout.guest');
  58. } else {
  59. parent::__construct('core', 'layout.base');
  60. }
  61. $versionParameter = '?v=' . md5(implode(OC_Util::getVersion()));
  62. $useAssetPipeline = $this->isAssetPipelineEnabled();
  63. if ($useAssetPipeline) {
  64. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config') . $versionParameter);
  65. $this->generateAssets();
  66. } else {
  67. // Add the js files
  68. $jsfiles = self::findJavascriptFiles(OC_Util::$scripts);
  69. $this->assign('jsfiles', array(), false);
  70. if (OC_Config::getValue('installed', false) && $renderas!='error') {
  71. $this->append( 'jsfiles', OC_Helper::linkToRoute('js_config') . $versionParameter);
  72. }
  73. foreach($jsfiles as $info) {
  74. $web = $info[1];
  75. $file = $info[2];
  76. $this->append( 'jsfiles', $web.'/'.$file . $versionParameter);
  77. }
  78. // Add the css files
  79. $cssfiles = self::findStylesheetFiles(OC_Util::$styles);
  80. $this->assign('cssfiles', array());
  81. foreach($cssfiles as $info) {
  82. $web = $info[1];
  83. $file = $info[2];
  84. $this->append( 'cssfiles', $web.'/'.$file . $versionParameter);
  85. }
  86. }
  87. }
  88. /**
  89. * @param array $styles
  90. * @return array
  91. */
  92. static public function findStylesheetFiles($styles) {
  93. // Read the selected theme from the config file
  94. $theme = OC_Util::getTheme();
  95. // Read the detected formfactor and use the right file name.
  96. $fext = self::getFormFactorExtension();
  97. $locator = new \OC\Template\CSSResourceLocator( $theme, $fext,
  98. array( OC::$SERVERROOT => OC::$WEBROOT ),
  99. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  100. $locator->find($styles);
  101. return $locator->getResources();
  102. }
  103. /**
  104. * @param array $scripts
  105. * @return array
  106. */
  107. static public function findJavascriptFiles($scripts) {
  108. // Read the selected theme from the config file
  109. $theme = OC_Util::getTheme();
  110. // Read the detected formfactor and use the right file name.
  111. $fext = self::getFormFactorExtension();
  112. $locator = new \OC\Template\JSResourceLocator( $theme, $fext,
  113. array( OC::$SERVERROOT => OC::$WEBROOT ),
  114. array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
  115. $locator->find($scripts);
  116. return $locator->getResources();
  117. }
  118. public function generateAssets()
  119. {
  120. $jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
  121. $jsHash = self::hashScriptNames($jsFiles);
  122. if (!file_exists("assets/$jsHash.js")) {
  123. $jsFiles = array_map(function ($item) {
  124. $root = $item[0];
  125. $file = $item[2];
  126. return new FileAsset($root . '/' . $file, array(), $root, $file);
  127. }, $jsFiles);
  128. $jsCollection = new AssetCollection($jsFiles);
  129. $jsCollection->setTargetPath("assets/$jsHash.js");
  130. $writer = new AssetWriter(\OC::$SERVERROOT);
  131. $writer->writeAsset($jsCollection);
  132. }
  133. $cssFiles = self::findStylesheetFiles(OC_Util::$styles);
  134. $cssHash = self::hashScriptNames($cssFiles);
  135. if (!file_exists("assets/$cssHash.css")) {
  136. $cssFiles = array_map(function ($item) {
  137. $root = $item[0];
  138. $file = $item[2];
  139. $assetPath = $root . '/' . $file;
  140. $sourceRoot = \OC::$SERVERROOT;
  141. $sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT));
  142. return new FileAsset($assetPath, array(new CssRewriteFilter()), $sourceRoot, $sourcePath);
  143. }, $cssFiles);
  144. $cssCollection = new AssetCollection($cssFiles);
  145. $cssCollection->setTargetPath("assets/$cssHash.css");
  146. $writer = new AssetWriter(\OC::$SERVERROOT);
  147. $writer->writeAsset($cssCollection);
  148. }
  149. $this->append('jsfiles', OC_Helper::linkTo('assets', "$jsHash.js"));
  150. $this->append('cssfiles', OC_Helper::linkTo('assets', "$cssHash.css"));
  151. }
  152. /**
  153. * @param array $files
  154. * @return string
  155. */
  156. private static function hashScriptNames($files)
  157. {
  158. $files = array_map(function ($item) {
  159. $root = $item[0];
  160. $file = $item[2];
  161. return $root . '/' . $file;
  162. }, $files);
  163. sort($files);
  164. return hash('md5', implode('', $files));
  165. }
  166. /**
  167. * @return bool
  168. */
  169. private function isAssetPipelineEnabled() {
  170. // asset management enabled?
  171. $useAssetPipeline = OC_Config::getValue('asset-pipeline.enabled', false);
  172. if (!$useAssetPipeline) {
  173. return false;
  174. }
  175. // assets folder exists?
  176. $assetDir = \OC::$SERVERROOT . '/assets';
  177. if (!is_dir($assetDir)) {
  178. if (!mkdir($assetDir)) {
  179. \OCP\Util::writeLog('assets',
  180. "Folder <$assetDir> does not exist and/or could not be generated.", \OCP\Util::ERROR);
  181. return false;
  182. }
  183. }
  184. // assets folder can be accessed?
  185. if (!touch($assetDir."/.oc")) {
  186. \OCP\Util::writeLog('assets',
  187. "Folder <$assetDir> could not be accessed.", \OCP\Util::ERROR);
  188. return false;
  189. }
  190. return $useAssetPipeline;
  191. }
  192. }