summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorBjoern Schiessle <schiessle@owncloud.com>2012-06-18 15:38:24 +0200
committerBjoern Schiessle <schiessle@owncloud.com>2012-06-18 15:38:24 +0200
commit405ac36e0c34ec03fc00eab1ee57520c34cd7d28 (patch)
tree0293b34c3c56ccf1ab9c7f79dafcbc4601ab3ab4 /lib
parent2fd7df57d9255e9a8b2fa57c7e9fd435f2b44f98 (diff)
parent7064f3a8a8e249b62bd8b6a018cf6d79b7ae133c (diff)
downloadnextcloud-server-405ac36e0c34ec03fc00eab1ee57520c34cd7d28.tar.gz
nextcloud-server-405ac36e0c34ec03fc00eab1ee57520c34cd7d28.zip
Merge branch 'master' of gitorious.org:owncloud/owncloud
Diffstat (limited to 'lib')
-rw-r--r--lib/base.php2
-rw-r--r--lib/cache/fileglobal.php78
-rw-r--r--lib/minimizer.php22
-rw-r--r--lib/request.php28
4 files changed, 126 insertions, 4 deletions
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
new file mode 100644
index 00000000000..1c2c9bdc82d
--- /dev/null
+++ b/lib/cache/fileglobal.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * 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_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;
+ }
+
+ 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);
+ }
+ return null;
+ }
+
+ 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) {
+ $ttl = 86400; // 60*60*24
+ }
+ return touch($cache_dir.$key, time() + $ttl);
+ }
+ return false;
+ }
+
+ 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);
+ 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;
+ }
+ $key = $this->fixKey($key);
+ 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);
+ }
+ }
+ }
+ }
+}
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..0b5aaf8ef30
--- /dev/null
+++ b/lib/request.php
@@ -0,0 +1,28 @@
+<?php
+/**
+ * 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_Request {
+ static public function isNoCache() {
+ if (!isset($_SERVER['HTTP_CACHE_CONTROL'])) {
+ return false;
+ }
+ return $_SERVER['HTTP_CACHE_CONTROL'] == 'no-cache';
+ }
+
+ 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';
+ else if( strpos($HTTP_ACCEPT_ENCODING,'gzip') !== false )
+ return 'gzip';
+ return false;
+ }
+}