2012-05-14 15:57:43 +00:00
|
|
|
<?php
|
|
|
|
|
2012-06-18 13:40:43 +00:00
|
|
|
abstract class OC_Minimizer {
|
2012-09-07 11:40:04 +00:00
|
|
|
public function generateETag($files) {
|
2012-10-28 16:49:43 +00:00
|
|
|
$fullpath_files = array();
|
2012-05-14 15:57:43 +00:00
|
|
|
foreach($files as $file_info) {
|
2012-10-28 16:49:43 +00:00
|
|
|
$fullpath_files[] = $file_info[0] . '/' . $file_info[2];
|
2012-05-14 15:57:43 +00:00
|
|
|
}
|
2012-10-28 16:49:43 +00:00
|
|
|
return OC_Cache::generateCacheKeyFromFiles($fullpath_files);
|
2012-05-14 15:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
abstract public function minimizeFiles($files);
|
|
|
|
|
2012-06-15 10:07:31 +00:00
|
|
|
public function output($files, $cache_key) {
|
2012-05-14 15:57:43 +00:00
|
|
|
header('Content-Type: '.$this->contentType);
|
|
|
|
OC_Response::enableCaching();
|
2012-09-07 11:40:04 +00:00
|
|
|
$etag = $this->generateETag($files);
|
|
|
|
$cache_key .= '-'.$etag;
|
2012-05-14 15:57:43 +00:00
|
|
|
|
2012-06-15 10:07:31 +00:00
|
|
|
$gzout = false;
|
2012-06-25 06:50:13 +00:00
|
|
|
$cache = OC_Cache::getGlobalCache();
|
2012-09-07 13:22:01 +00:00
|
|
|
if (!OC_Request::isNoCache() && (!defined('DEBUG') || !DEBUG)) {
|
2012-09-07 11:40:04 +00:00
|
|
|
OC_Response::setETagHeader($etag);
|
2012-06-15 10:07:31 +00: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 11:40:04 +00:00
|
|
|
OC_Response::setETagHeader($etag);
|
2012-06-15 10:07:31 +00:00
|
|
|
}
|
2012-11-14 11:59:36 +00:00
|
|
|
// on some systems (e.g. SLES 11, but not Ubuntu) mod_deflate and zlib compression will compress the output twice.
|
2012-11-14 11:53:36 +00:00
|
|
|
// This results in broken core.css and core.js. To avoid it, we switch off zlib compression.
|
|
|
|
// Since mod_deflate is still active, Apache will compress what needs to be compressed, i.e. no disadvantage.
|
2012-11-14 12:27:19 +00:00
|
|
|
if(function_exists('apache_get_modules') && ini_get('zlib.output_compression') && in_array('mod_deflate', apache_get_modules())) {
|
2012-11-14 11:59:36 +00:00
|
|
|
ini_set('zlib.output_compression', 'Off');
|
|
|
|
}
|
2012-06-15 10:07:31 +00:00
|
|
|
if ($encoding = OC_Request::acceptGZip()) {
|
|
|
|
header('Content-Encoding: '.$encoding);
|
|
|
|
$out = $gzout;
|
|
|
|
} else {
|
|
|
|
$out = gzdecode($gzout);
|
|
|
|
}
|
2012-05-14 15:57:43 +00:00
|
|
|
header('Content-Length: '.strlen($out));
|
|
|
|
echo $out;
|
|
|
|
}
|
2012-09-07 10:27:47 +00:00
|
|
|
|
|
|
|
public function clearCache() {
|
|
|
|
$cache = OC_Cache::getGlobalCache();
|
2012-09-07 11:40:04 +00:00
|
|
|
$cache->clear('core.css');
|
|
|
|
$cache->clear('core.js');
|
2012-09-07 10:27:47 +00:00
|
|
|
}
|
2012-05-14 15:57:43 +00:00
|
|
|
}
|
2012-07-22 13:38:23 +00:00
|
|
|
|
|
|
|
if (!function_exists('gzdecode')) {
|
2012-11-02 18:53:02 +00:00
|
|
|
function gzdecode($data, $maxlength=null, &$filename='', &$error='')
|
2012-07-22 13:38:23 +00:00
|
|
|
{
|
2012-11-02 18:53:02 +00:00
|
|
|
if (strcmp(substr($data, 0, 9),"\x1f\x8b\x8\0\0\0\0\0\0")) {
|
2012-07-22 13:38:23 +00:00
|
|
|
return null; // Not the GZIP format we expect (See RFC 1952)
|
|
|
|
}
|
2012-11-02 18:53:02 +00:00
|
|
|
return gzinflate(substr($data, 10, -8));
|
2012-07-22 13:38:23 +00:00
|
|
|
}
|
|
|
|
}
|