minimizer.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. abstract class OC_Minimizer {
  3. public function generateETag($files) {
  4. $fullpath_files = array();
  5. foreach($files as $file_info) {
  6. $fullpath_files[] = $file_info[0] . '/' . $file_info[2];
  7. }
  8. return OC_Cache::generateCacheKeyFromFiles($fullpath_files);
  9. }
  10. abstract public function minimizeFiles($files);
  11. public function output($files, $cache_key) {
  12. header('Content-Type: '.$this->contentType);
  13. OC_Response::enableCaching();
  14. $etag = $this->generateETag($files);
  15. $cache_key .= '-'.$etag;
  16. $gzout = false;
  17. $cache = OC_Cache::getGlobalCache();
  18. if (!OC_Request::isNoCache() && (!defined('DEBUG') || !DEBUG)) {
  19. OC_Response::setETagHeader($etag);
  20. $gzout = $cache->get($cache_key.'.gz');
  21. }
  22. if (!$gzout) {
  23. $out = $this->minimizeFiles($files);
  24. $gzout = gzencode($out);
  25. $cache->set($cache_key.'.gz', $gzout);
  26. OC_Response::setETagHeader($etag);
  27. }
  28. // on some systems (e.g. SLES 11, but not Ubuntu) mod_deflate and zlib compression will compress the output twice.
  29. // This results in broken core.css and core.js. To avoid it, we switch off zlib compression.
  30. // Since mod_deflate is still active, Apache will compress what needs to be compressed, i.e. no disadvantage.
  31. if(function_exists('apache_get_modules') && ini_get('zlib.output_compression') && in_array('mod_deflate', apache_get_modules())) {
  32. ini_set('zlib.output_compression', 'Off');
  33. }
  34. if ($encoding = OC_Request::acceptGZip()) {
  35. header('Content-Encoding: '.$encoding);
  36. $out = $gzout;
  37. } else {
  38. $out = gzdecode($gzout);
  39. }
  40. header('Content-Length: '.strlen($out));
  41. echo $out;
  42. }
  43. public function clearCache() {
  44. $cache = OC_Cache::getGlobalCache();
  45. $cache->clear('core.css');
  46. $cache->clear('core.js');
  47. }
  48. }
  49. if (!function_exists('gzdecode')) {
  50. function gzdecode($data, $maxlength=null, &$filename='', &$error='')
  51. {
  52. if (strcmp(substr($data, 0, 9),"\x1f\x8b\x8\0\0\0\0\0\0")) {
  53. return null; // Not the GZIP format we expect (See RFC 1952)
  54. }
  55. return gzinflate(substr($data, 10, -8));
  56. }
  57. }