2012-05-14 19:57:43 +04:00
|
|
|
<?php
|
|
|
|
|
2012-06-18 17:40:43 +04:00
|
|
|
abstract class OC_Minimizer {
|
2012-09-07 15:40:04 +04:00
|
|
|
public function generateETag($files) {
|
|
|
|
$etag = '';
|
|
|
|
sort($files);
|
2012-05-14 19:57:43 +04:00
|
|
|
foreach($files as $file_info) {
|
|
|
|
$file = $file_info[0] . '/' . $file_info[2];
|
2012-09-07 15:40:04 +04:00
|
|
|
$stat = stat($file);
|
|
|
|
$etag .= $file.$stat['mtime'].$stat['size'];
|
2012-05-14 19:57:43 +04:00
|
|
|
}
|
2012-09-07 15:40:04 +04:00
|
|
|
return md5($etag);
|
2012-05-14 19:57:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract public function minimizeFiles($files);
|
|
|
|
|
2012-06-15 14:07:31 +04:00
|
|
|
public function output($files, $cache_key) {
|
2012-05-14 19:57:43 +04:00
|
|
|
header('Content-Type: '.$this->contentType);
|
|
|
|
OC_Response::enableCaching();
|
2012-09-07 15:40:04 +04:00
|
|
|
$etag = $this->generateETag($files);
|
|
|
|
$cache_key .= '-'.$etag;
|
2012-05-14 19:57:43 +04:00
|
|
|
|
2012-06-15 14:07:31 +04:00
|
|
|
$gzout = false;
|
2012-06-25 10:50:13 +04:00
|
|
|
$cache = OC_Cache::getGlobalCache();
|
2012-06-15 14:07:31 +04:00
|
|
|
if (!OC_Request::isNoCache() && (!defined('DEBUG') || !DEBUG)){
|
2012-09-07 15:40:04 +04:00
|
|
|
OC_Response::setETagHeader($etag);
|
2012-06-15 14:07:31 +04:00
|
|
|
$gzout = $cache->get($cache_key.'.gz');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$gzout) {
|
|
|
|
$out = $this->minimizeFiles($files);
|
|
|
|
$gzout = gzencode($out);
|
|
|
|
$cache->set($cache_key.'.gz', $gzout);
|
2012-09-07 15:40:04 +04:00
|
|
|
OC_Response::setETagHeader($etag);
|
2012-06-15 14:07:31 +04:00
|
|
|
}
|
|
|
|
if ($encoding = OC_Request::acceptGZip()) {
|
|
|
|
header('Content-Encoding: '.$encoding);
|
|
|
|
$out = $gzout;
|
|
|
|
} else {
|
|
|
|
$out = gzdecode($gzout);
|
|
|
|
}
|
2012-05-14 19:57:43 +04:00
|
|
|
header('Content-Length: '.strlen($out));
|
|
|
|
echo $out;
|
|
|
|
}
|
2012-09-07 14:27:47 +04:00
|
|
|
|
|
|
|
public function clearCache() {
|
|
|
|
$cache = OC_Cache::getGlobalCache();
|
2012-09-07 15:40:04 +04:00
|
|
|
$cache->clear('core.css');
|
|
|
|
$cache->clear('core.js');
|
2012-09-07 14:27:47 +04:00
|
|
|
}
|
2012-05-14 19:57:43 +04:00
|
|
|
}
|
2012-07-22 17:38:23 +04:00
|
|
|
|
|
|
|
if (!function_exists('gzdecode')) {
|
|
|
|
function gzdecode($data,$maxlength=null,&$filename='',&$error='')
|
|
|
|
{
|
|
|
|
if (strcmp(substr($data,0,9),"\x1f\x8b\x8\0\0\0\0\0\0")) {
|
|
|
|
return null; // Not the GZIP format we expect (See RFC 1952)
|
|
|
|
}
|
|
|
|
return gzinflate(substr($data,10,-8));
|
|
|
|
}
|
|
|
|
}
|