2012-06-18 17:04:35 +04:00
|
|
|
<?php
|
2014-02-20 16:28:27 +04:00
|
|
|
use Assetic\Asset\AssetCollection;
|
|
|
|
use Assetic\Asset\FileAsset;
|
|
|
|
use Assetic\AssetWriter;
|
|
|
|
use Assetic\Filter\CssRewriteFilter;
|
|
|
|
|
2012-06-18 17:04:35 +04:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2012 Bart Visscher <bartv@thisnet.nl>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
class OC_TemplateLayout extends OC_Template {
|
2014-02-06 19:30:58 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $renderas
|
2013-12-09 16:18:13 +04:00
|
|
|
* @param string $appid application id
|
2014-02-06 19:30:58 +04:00
|
|
|
*/
|
2013-12-09 16:18:13 +04:00
|
|
|
public function __construct( $renderas, $appid = '' ) {
|
2012-06-18 17:04:35 +04:00
|
|
|
// Decide which page we show
|
2012-06-21 23:19:39 +04:00
|
|
|
|
2012-09-07 17:22:01 +04:00
|
|
|
if( $renderas == 'user' ) {
|
2012-06-18 17:04:35 +04:00
|
|
|
parent::__construct( 'core', 'layout.user' );
|
2012-11-04 14:10:46 +04:00
|
|
|
if(in_array(OC_APP::getCurrentApp(), array('settings','admin', 'help'))!==false) {
|
2013-02-28 01:55:39 +04:00
|
|
|
$this->assign('bodyid', 'body-settings');
|
2012-06-18 17:04:35 +04:00
|
|
|
}else{
|
2013-02-28 01:55:39 +04:00
|
|
|
$this->assign('bodyid', 'body-user');
|
2012-06-18 17:04:35 +04:00
|
|
|
}
|
|
|
|
|
2013-04-24 20:47:38 +04:00
|
|
|
// Update notification
|
|
|
|
if(OC_Config::getValue('updatechecker', true) === true) {
|
|
|
|
$data=OC_Updater::check();
|
|
|
|
if(isset($data['version']) && $data['version'] != '' and $data['version'] !== Array() && OC_User::isAdminUser(OC_User::getUser())) {
|
|
|
|
$this->assign('updateAvailable', true);
|
|
|
|
$this->assign('updateVersion', $data['versionstring']);
|
|
|
|
$this->assign('updateLink', $data['web']);
|
|
|
|
} else {
|
|
|
|
$this->assign('updateAvailable', false); // No update available or not an admin user
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->assign('updateAvailable', false); // Update check is disabled
|
|
|
|
}
|
|
|
|
|
2012-06-18 17:04:35 +04:00
|
|
|
// Add navigation entry
|
2013-02-07 21:07:27 +04:00
|
|
|
$this->assign( 'application', '', false );
|
2013-12-09 16:18:13 +04:00
|
|
|
$this->assign( 'appid', $appid );
|
2012-06-18 17:04:35 +04:00
|
|
|
$navigation = OC_App::getNavigation();
|
2013-02-28 01:55:39 +04:00
|
|
|
$this->assign( 'navigation', $navigation);
|
|
|
|
$this->assign( 'settingsnavigation', OC_App::getSettingsNavigation());
|
2012-06-18 17:04:35 +04:00
|
|
|
foreach($navigation as $entry) {
|
|
|
|
if ($entry['active']) {
|
2013-02-28 01:55:39 +04:00
|
|
|
$this->assign( 'application', $entry['name'] );
|
2012-06-18 17:04:35 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-02-07 21:07:27 +04:00
|
|
|
$user_displayname = OC_User::getDisplayName();
|
|
|
|
$this->assign( 'user_displayname', $user_displayname );
|
2013-03-02 19:09:36 +04:00
|
|
|
$this->assign( 'user_uid', OC_User::getUser() );
|
2013-12-09 18:47:51 +04:00
|
|
|
$this->assign( 'appsmanagement_active', strpos(OC_Request::requestUri(), OC_Helper::linkToRoute('settings_apps')) === 0 );
|
2013-09-11 09:11:33 +04:00
|
|
|
$this->assign('enableAvatars', \OC_Config::getValue('enable_avatars', true));
|
2014-08-29 12:52:52 +04:00
|
|
|
} else if ($renderas == 'error') {
|
|
|
|
parent::__construct('core', 'layout.guest', '', false);
|
|
|
|
} else if ($renderas == 'guest') {
|
2012-08-29 19:54:31 +04:00
|
|
|
parent::__construct('core', 'layout.guest');
|
|
|
|
} else {
|
|
|
|
parent::__construct('core', 'layout.base');
|
2012-06-18 17:04:35 +04:00
|
|
|
}
|
2014-02-20 16:28:27 +04:00
|
|
|
|
2013-04-05 01:33:05 +04:00
|
|
|
$versionParameter = '?v=' . md5(implode(OC_Util::getVersion()));
|
2014-04-15 18:26:12 +04:00
|
|
|
$useAssetPipeline = $this->isAssetPipelineEnabled();
|
2014-02-20 16:28:27 +04:00
|
|
|
if ($useAssetPipeline) {
|
|
|
|
|
2013-05-11 23:07:05 +04:00
|
|
|
$this->append( 'jsfiles', OC_Helper::linkToRoute('js_config') . $versionParameter);
|
2012-06-18 17:04:35 +04:00
|
|
|
|
2014-02-20 16:28:27 +04:00
|
|
|
$this->generateAssets();
|
2012-06-21 23:19:39 +04:00
|
|
|
|
2014-02-20 16:28:27 +04:00
|
|
|
} else {
|
|
|
|
|
|
|
|
// Add the js files
|
|
|
|
$jsfiles = self::findJavascriptFiles(OC_Util::$scripts);
|
|
|
|
$this->assign('jsfiles', array(), false);
|
|
|
|
if (OC_Config::getValue('installed', false) && $renderas!='error') {
|
|
|
|
$this->append( 'jsfiles', OC_Helper::linkToRoute('js_config') . $versionParameter);
|
|
|
|
}
|
|
|
|
foreach($jsfiles as $info) {
|
|
|
|
$web = $info[1];
|
|
|
|
$file = $info[2];
|
|
|
|
$this->append( 'jsfiles', $web.'/'.$file . $versionParameter);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add the css files
|
|
|
|
$cssfiles = self::findStylesheetFiles(OC_Util::$styles);
|
|
|
|
$this->assign('cssfiles', array());
|
|
|
|
foreach($cssfiles as $info) {
|
|
|
|
$web = $info[1];
|
|
|
|
$file = $info[2];
|
|
|
|
|
|
|
|
$this->append( 'cssfiles', $web.'/'.$file . $versionParameter);
|
|
|
|
}
|
2012-06-18 17:04:35 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param array $styles
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
static public function findStylesheetFiles($styles) {
|
2012-06-18 17:04:35 +04:00
|
|
|
// Read the selected theme from the config file
|
2013-04-24 15:45:40 +04:00
|
|
|
$theme = OC_Util::getTheme();
|
2012-06-18 17:04:35 +04:00
|
|
|
|
|
|
|
// Read the detected formfactor and use the right file name.
|
|
|
|
$fext = self::getFormFactorExtension();
|
|
|
|
|
2013-03-17 02:02:51 +04:00
|
|
|
$locator = new \OC\Template\CSSResourceLocator( $theme, $fext,
|
|
|
|
array( OC::$SERVERROOT => OC::$WEBROOT ),
|
|
|
|
array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
|
|
|
|
$locator->find($styles);
|
|
|
|
return $locator->getResources();
|
2012-06-18 17:04:35 +04:00
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param array $scripts
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return array
|
|
|
|
*/
|
2012-09-07 17:22:01 +04:00
|
|
|
static public function findJavascriptFiles($scripts) {
|
2012-06-18 17:04:35 +04:00
|
|
|
// Read the selected theme from the config file
|
2013-04-24 15:45:40 +04:00
|
|
|
$theme = OC_Util::getTheme();
|
2012-06-18 17:04:35 +04:00
|
|
|
|
|
|
|
// Read the detected formfactor and use the right file name.
|
|
|
|
$fext = self::getFormFactorExtension();
|
|
|
|
|
2013-03-17 02:02:51 +04:00
|
|
|
$locator = new \OC\Template\JSResourceLocator( $theme, $fext,
|
|
|
|
array( OC::$SERVERROOT => OC::$WEBROOT ),
|
|
|
|
array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT ));
|
|
|
|
$locator->find($scripts);
|
|
|
|
return $locator->getResources();
|
2012-06-18 17:04:35 +04:00
|
|
|
}
|
2014-02-20 16:28:27 +04:00
|
|
|
|
|
|
|
public function generateAssets()
|
|
|
|
{
|
|
|
|
$jsFiles = self::findJavascriptFiles(OC_Util::$scripts);
|
|
|
|
$jsHash = self::hashScriptNames($jsFiles);
|
|
|
|
|
|
|
|
if (!file_exists("assets/$jsHash.js")) {
|
|
|
|
$jsFiles = array_map(function ($item) {
|
|
|
|
$root = $item[0];
|
|
|
|
$file = $item[2];
|
|
|
|
return new FileAsset($root . '/' . $file, array(), $root, $file);
|
|
|
|
}, $jsFiles);
|
|
|
|
$jsCollection = new AssetCollection($jsFiles);
|
|
|
|
$jsCollection->setTargetPath("assets/$jsHash.js");
|
|
|
|
|
|
|
|
$writer = new AssetWriter(\OC::$SERVERROOT);
|
|
|
|
$writer->writeAsset($jsCollection);
|
|
|
|
}
|
|
|
|
|
|
|
|
$cssFiles = self::findStylesheetFiles(OC_Util::$styles);
|
|
|
|
$cssHash = self::hashScriptNames($cssFiles);
|
|
|
|
|
|
|
|
if (!file_exists("assets/$cssHash.css")) {
|
|
|
|
$cssFiles = array_map(function ($item) {
|
|
|
|
$root = $item[0];
|
|
|
|
$file = $item[2];
|
|
|
|
$assetPath = $root . '/' . $file;
|
|
|
|
$sourceRoot = \OC::$SERVERROOT;
|
|
|
|
$sourcePath = substr($assetPath, strlen(\OC::$SERVERROOT));
|
|
|
|
return new FileAsset($assetPath, array(new CssRewriteFilter()), $sourceRoot, $sourcePath);
|
|
|
|
}, $cssFiles);
|
|
|
|
$cssCollection = new AssetCollection($cssFiles);
|
|
|
|
$cssCollection->setTargetPath("assets/$cssHash.css");
|
|
|
|
|
|
|
|
$writer = new AssetWriter(\OC::$SERVERROOT);
|
|
|
|
$writer->writeAsset($cssCollection);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->append('jsfiles', OC_Helper::linkTo('assets', "$jsHash.js"));
|
|
|
|
$this->append('cssfiles', OC_Helper::linkTo('assets', "$cssHash.css"));
|
|
|
|
}
|
|
|
|
|
2014-04-21 17:44:54 +04:00
|
|
|
/**
|
2014-05-12 00:51:30 +04:00
|
|
|
* @param array $files
|
2014-04-21 17:44:54 +04:00
|
|
|
* @return string
|
|
|
|
*/
|
2014-02-20 16:28:27 +04:00
|
|
|
private static function hashScriptNames($files)
|
|
|
|
{
|
|
|
|
$files = array_map(function ($item) {
|
|
|
|
$root = $item[0];
|
|
|
|
$file = $item[2];
|
|
|
|
return $root . '/' . $file;
|
|
|
|
}, $files);
|
|
|
|
|
|
|
|
sort($files);
|
|
|
|
return hash('md5', implode('', $files));
|
|
|
|
}
|
2014-04-15 18:26:12 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
private function isAssetPipelineEnabled() {
|
|
|
|
// asset management enabled?
|
|
|
|
$useAssetPipeline = OC_Config::getValue('asset-pipeline.enabled', false);
|
|
|
|
if (!$useAssetPipeline) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assets folder exists?
|
|
|
|
$assetDir = \OC::$SERVERROOT . '/assets';
|
|
|
|
if (!is_dir($assetDir)) {
|
|
|
|
if (!mkdir($assetDir)) {
|
|
|
|
\OCP\Util::writeLog('assets',
|
|
|
|
"Folder <$assetDir> does not exist and/or could not be generated.", \OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// assets folder can be accessed?
|
|
|
|
if (!touch($assetDir."/.oc")) {
|
|
|
|
\OCP\Util::writeLog('assets',
|
|
|
|
"Folder <$assetDir> could not be accessed.", \OCP\Util::ERROR);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return $useAssetPipeline;
|
|
|
|
}
|
2012-06-18 17:04:35 +04:00
|
|
|
}
|