From 617de811f786dd9282ad52c062a58072027f5637 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 15 Jun 2012 12:07:31 +0200 Subject: [PATCH 1/4] Cache the minimized output also on the server --- core/minimizer.php | 4 ++-- lib/minimizer.php | 22 +++++++++++++++++++--- lib/request.php | 25 +++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 5 deletions(-) create mode 100644 lib/request.php diff --git a/core/minimizer.php b/core/minimizer.php index 709c7508e90..47e3d855e7b 100644 --- a/core/minimizer.php +++ b/core/minimizer.php @@ -6,10 +6,10 @@ OC_App::loadApps(); if ($service == 'core.css'){ $minimizer = new OC_Minimizer_CSS(); $files = $minimizer->findFiles(OC_Util::$core_styles); - $minimizer->output($files); + $minimizer->output($files, $service); } else if ($service == 'core.js'){ $minimizer = new OC_Minimizer_JS(); $files = $minimizer->findFiles(OC_Util::$core_scripts); - $minimizer->output($files); + $minimizer->output($files, $service); } diff --git a/lib/minimizer.php b/lib/minimizer.php index 9f9ef086c4a..428fa477f77 100644 --- a/lib/minimizer.php +++ b/lib/minimizer.php @@ -26,14 +26,30 @@ abstract class OC_Minimizer abstract public function minimizeFiles($files); - public function output($files) { + public function output($files, $cache_key) { header('Content-Type: '.$this->contentType); OC_Response::enableCaching(); $last_modified = $this->getLastModified($files); OC_Response::setLastModifiedHeader($last_modified); - $out = $this->minimizeFiles($files); - OC_Response::setETagHeader(md5($out)); + $gzout = false; + $cache = new OC_Cache_FileGlobal(); + if (!OC_Request::isNoCache() && (!defined('DEBUG') || !DEBUG)){ + $gzout = $cache->get($cache_key.'.gz'); + OC_Response::setETagHeader(md5($gzout)); + } + + if (!$gzout) { + $out = $this->minimizeFiles($files); + $gzout = gzencode($out); + $cache->set($cache_key.'.gz', $gzout); + } + if ($encoding = OC_Request::acceptGZip()) { + header('Content-Encoding: '.$encoding); + $out = $gzout; + } else { + $out = gzdecode($gzout); + } header('Content-Length: '.strlen($out)); echo $out; } diff --git a/lib/request.php b/lib/request.php new file mode 100644 index 00000000000..d152d0c73ba --- /dev/null +++ b/lib/request.php @@ -0,0 +1,25 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class OC_Request { + static public function isNoCache() { + if (!isset($_SERVER['HTTP_CACHE_CONTROL'])) { + return false; + } + return $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache'; + } + + static public function acceptGZip() { + $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"]; + if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ) + return 'x-gzip'; + else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false ) + return 'gzip'; + return false; + } +} From a5a1a9fd4a2f1cd5f2457ba3ab0ceb432da1be33 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 18 Jun 2012 11:12:53 +0200 Subject: [PATCH 2/4] Forgot a file --- lib/cache/fileglobal.php | 70 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 lib/cache/fileglobal.php diff --git a/lib/cache/fileglobal.php b/lib/cache/fileglobal.php new file mode 100644 index 00000000000..469dd4b6dd4 --- /dev/null +++ b/lib/cache/fileglobal.php @@ -0,0 +1,70 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + + +class OC_Cache_FileGlobal{ + protected function getCacheDir() { + $cache_dir = get_temp_dir().'/owncloud-'.OC_Util::getInstanceId().'/'; + if (!is_dir($cache_dir)) { + mkdir($cache_dir); + } + return $cache_dir; + } + + public function get($key) { + if ($this->hasKey($key)) { + $cache_dir = $this->getCacheDir(); + return file_get_contents($cache_dir.$key); + } + return null; + } + + public function set($key, $value, $ttl=0) { + $cache_dir = $this->getCacheDir(); + if ($cache_dir and file_put_contents($cache_dir.$key, $value)) { + if ($ttl === 0) { + $ttl = 86400; // 60*60*24 + } + return touch($cache_dir.$key, time() + $ttl); + } + return false; + } + + public function hasKey($key) { + $cache_dir = $this->getCacheDir(); + if ($cache_dir && is_file($cache_dir.$key)) { + $mtime = filemtime($cache_dir.$key); + if ($mtime < time()) { + unlink($cache_dir.$key); + return false; + } + return true; + } + return false; + } + + public function remove($key) { + $cache_dir = $this->getCacheDir(); + if(!$cache_dir){ + return false; + } + return unlink($cache_dir.$key); + } + + public function clear(){ + $cache_dir = $this->getCacheDir(); + if($cache_dir and is_dir($cache_dir)){ + $dh=opendir($cache_dir); + while($file=readdir($dh)){ + if($file!='.' and $file!='..'){ + unlink($cache_dir.$file); + } + } + } + } +} From 977cd0df6b502c1a15147c1641fea754128a0875 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 18 Jun 2012 11:33:24 +0200 Subject: [PATCH 3/4] Fix errors for minimizer --- lib/base.php | 2 +- lib/cache/fileglobal.php | 8 ++++++++ lib/request.php | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/base.php b/lib/base.php index 30f7e5bba63..94ae26c4d1b 100644 --- a/lib/base.php +++ b/lib/base.php @@ -282,7 +282,7 @@ class OC{ if(substr(OC::$REQUESTEDFILE, -3) == 'css'){ $file = 'apps/' . OC::$REQUESTEDAPP . '/' . OC::$REQUESTEDFILE; $minimizer = new OC_Minimizer_CSS(); - $minimizer->output(array(array(OC::$APPSROOT, OC::$APPSWEBROOT, $file))); + $minimizer->output(array(array(OC::$APPSROOT, OC::$APPSWEBROOT, $file)), $file); exit; }elseif(substr(OC::$REQUESTEDFILE, -3) == 'php'){ require_once(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/' . OC::$REQUESTEDFILE); diff --git a/lib/cache/fileglobal.php b/lib/cache/fileglobal.php index 469dd4b6dd4..1c2c9bdc82d 100644 --- a/lib/cache/fileglobal.php +++ b/lib/cache/fileglobal.php @@ -16,7 +16,12 @@ class OC_Cache_FileGlobal{ return $cache_dir; } + protected function fixKey($key) { + return str_replace('/', '_', $key); + } + public function get($key) { + $key = $this->fixKey($key); if ($this->hasKey($key)) { $cache_dir = $this->getCacheDir(); return file_get_contents($cache_dir.$key); @@ -25,6 +30,7 @@ class OC_Cache_FileGlobal{ } public function set($key, $value, $ttl=0) { + $key = $this->fixKey($key); $cache_dir = $this->getCacheDir(); if ($cache_dir and file_put_contents($cache_dir.$key, $value)) { if ($ttl === 0) { @@ -36,6 +42,7 @@ class OC_Cache_FileGlobal{ } public function hasKey($key) { + $key = $this->fixKey($key); $cache_dir = $this->getCacheDir(); if ($cache_dir && is_file($cache_dir.$key)) { $mtime = filemtime($cache_dir.$key); @@ -53,6 +60,7 @@ class OC_Cache_FileGlobal{ if(!$cache_dir){ return false; } + $key = $this->fixKey($key); return unlink($cache_dir.$key); } diff --git a/lib/request.php b/lib/request.php index d152d0c73ba..0b5aaf8ef30 100644 --- a/lib/request.php +++ b/lib/request.php @@ -15,6 +15,9 @@ class OC_Request { } static public function acceptGZip() { + if (!isset($_SERVER['HTTP_ACCEPT_ENCODING'])) { + return false; + } $HTTP_ACCEPT_ENCODING = $_SERVER["HTTP_ACCEPT_ENCODING"]; if( strpos($HTTP_ACCEPT_ENCODING, 'x-gzip') !== false ) return 'x-gzip'; From 4a50c15a8ef596a075c851e08479b468d446545a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20H=C3=BClsmann?= Date: Mon, 18 Jun 2012 13:36:49 +0200 Subject: [PATCH 4/4] corrected header width and floating (can now dynamically add stuff to header); someone may test this in IE --- core/css/styles.css | 11 +++++------ core/templates/layout.user.php | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/core/css/styles.css b/core/css/styles.css index b6b5918d653..6c5c264353d 100644 --- a/core/css/styles.css +++ b/core/css/styles.css @@ -16,7 +16,7 @@ body { background:#fefefe; font:normal .8em/1.6em "Lucida Grande", Arial, Verdan /* HEADERS */ -#body-user #header, #body-settings #header { position:fixed; top:0; z-index:100; width:100%; height:2.5em; padding:.5em; background:#1d2d44; -moz-box-shadow:0 0 10px rgba(0, 0, 0, .5), inset 0 -2px 10px #222; -webkit-box-shadow:0 0 10px rgba(0, 0, 0, .5), inset 0 -2px 10px #222; box-shadow:0 0 10px rgba(0, 0, 0, .5), inset 0 -2px 10px #222; } +#body-user #header, #body-settings #header { position:fixed; top:0; left:0; right:0; z-index:100; height:2.5em; line-height:2.5em; padding:.5em; background:#1d2d44; -moz-box-shadow:0 0 10px rgba(0, 0, 0, .5), inset 0 -2px 10px #222; -webkit-box-shadow:0 0 10px rgba(0, 0, 0, .5), inset 0 -2px 10px #222; box-shadow:0 0 10px rgba(0, 0, 0, .5), inset 0 -2px 10px #222; } #body-login #header { margin: -2em auto 0; text-align:center; height:10em; padding:1em 0 .5em; -moz-box-shadow:0 0 1em rgba(0, 0, 0, .5); -webkit-box-shadow:0 0 1em rgba(0, 0, 0, .5); box-shadow:0 0 1em rgba(0, 0, 0, .5); background: #1d2d44; /* Old browsers */ @@ -28,8 +28,9 @@ background: -ms-linear-gradient(top, #35537a 0%,#1d2d42 100%); /* IE10+ */ background: linear-gradient(top, #35537a 0%,#1d2d42 100%); /* W3C */ filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#35537a', endColorstr='#1d2d42',GradientType=0 ); /* IE6-9 */ } -#owncloud { float:left; } - +#owncloud { float:left; vertical-align:middle; } +.header-right { float:right; vertical-align:middle; padding:0 0.5em; } +.header-right > * { vertical-align:middle; } /* INPUTS */ input[type="text"], input[type="password"] { cursor:text; } @@ -49,7 +50,7 @@ input[type="checkbox"] { width:auto; } #body-login input[type="text"], #body-login input[type="password"] { width: 13em; } #body-login input.login { width: auto; float: right; } #remember_login { margin:.8em .2em 0 1em; } -.searchbox input[type="search"] { position:fixed; font-size:1.2em; top:.4em; right:3em; padding:.2em .5em .2em 1.5em; background:#fff url('../img/actions/search.svg') no-repeat .5em center; border:0; -moz-border-radius:1em; -webkit-border-radius:1em; border-radius:1em; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=70)"; filter:alpha(opacity=70);opacity:.7; -webkit-transition:opacity 300ms; -moz-transition:opacity 300ms; -o-transition:opacity 300ms; transition:opacity 300ms; } +.searchbox input[type="search"] { font-size:1.2em; padding:.2em .5em .2em 1.5em; background:#fff url('../img/actions/search.svg') no-repeat .5em center; border:0; -moz-border-radius:1em; -webkit-border-radius:1em; border-radius:1em; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=70)"; filter:alpha(opacity=70);opacity:.7; -webkit-transition:opacity 300ms; -moz-transition:opacity 300ms; -o-transition:opacity 300ms; transition:opacity 300ms; } input[type="submit"].enabled { background:#66f866; border:1px solid #5e5; -moz-box-shadow:0 1px 1px #f8f8f8, 0 1px 1px #cfc inset; -webkit-box-shadow:0 1px 1px #f8f8f8, 0 1px 1px #cfc inset; box-shadow:0 1px 1px #f8f8f8, 0 1px 1px #cfc inset; } input[type="submit"].highlight{ background:#ffc100; border:1px solid #db0; text-shadow:#ffeedd 0 1px 0; -moz-box-shadow:0 1px 1px #f8f8f8, 0 1px 1px #ffeedd inset; -webkit-box-shadow:0 1px 1px #f8f8f8, 0 1px 1px #ffeedd inset; box-shadow:0 1px 1px #f8f8f8, 0 1px 1px #ffeedd inset; } #select_all{ margin-top: .4em !important;} @@ -101,8 +102,6 @@ label.infield { cursor: text !important; } #expand { position:relative; z-index:100; margin-bottom:-.5em; padding:.5em 10.1em .7em 1.2em; cursor:pointer; } #expand+span { position:absolute; z-index:99; margin:-1.7em 0 0 2.5em; font-size:1.2em; color:#666; text-shadow:#f8f8f8 0 1px 0; -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)"; filter:alpha(opacity=0); opacity:0; -webkit-transition:opacity 300ms; -moz-transition:opacity 300ms; -o-transition:opacity 300ms; transition:opacity 300ms; } #expand:hover+span, #expand+span:hover { -ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=100)"; filter:alpha(opacity=100); opacity:1; cursor:pointer; } -#logout { position:absolute; right:0; top:0; padding:1.2em 2em .55em 1.2em; } - /* VARIOUS REUSABLE SELECTORS */ .hidden { display:none; } diff --git a/core/templates/layout.user.php b/core/templates/layout.user.php index e271acf10ee..c6696fc72bf 100644 --- a/core/templates/layout.user.php +++ b/core/templates/layout.user.php @@ -45,10 +45,10 @@