2012-05-14 19:57:43 +04:00
|
|
|
<?php
|
|
|
|
|
2012-06-18 17:40:43 +04:00
|
|
|
abstract class OC_Minimizer {
|
2012-05-14 19:57:43 +04:00
|
|
|
public function getLastModified($files) {
|
|
|
|
$last_modified = 0;
|
|
|
|
foreach($files as $file_info) {
|
|
|
|
$file = $file_info[0] . '/' . $file_info[2];
|
|
|
|
$filemtime = filemtime($file);
|
|
|
|
if ($filemtime > $last_modified) {
|
|
|
|
$last_modified = $filemtime;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $last_modified;
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
$last_modified = $this->getLastModified($files);
|
|
|
|
OC_Response::setLastModifiedHeader($last_modified);
|
|
|
|
|
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)){
|
|
|
|
$gzout = $cache->get($cache_key.'.gz');
|
2012-06-25 19:46:03 +04:00
|
|
|
if ($gzout) {
|
|
|
|
OC_Response::setETagHeader(md5($gzout));
|
|
|
|
}
|
2012-06-15 14:07:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$gzout) {
|
|
|
|
$out = $this->minimizeFiles($files);
|
|
|
|
$gzout = gzencode($out);
|
2012-06-25 19:46:03 +04:00
|
|
|
OC_Response::setETagHeader(md5($gzout));
|
2012-06-15 14:07:31 +04:00
|
|
|
$cache->set($cache_key.'.gz', $gzout);
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|