minimizer.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. abstract class OC_Minimizer {
  3. public function generateETag($files) {
  4. $etag = '';
  5. sort($files);
  6. foreach($files as $file_info) {
  7. $file = $file_info[0] . '/' . $file_info[2];
  8. $stat = stat($file);
  9. $etag .= $file.$stat['mtime'].$stat['size'];
  10. }
  11. return md5($etag);
  12. }
  13. abstract public function minimizeFiles($files);
  14. public function output($files, $cache_key) {
  15. header('Content-Type: '.$this->contentType);
  16. OC_Response::enableCaching();
  17. $etag = $this->generateETag($files);
  18. $cache_key .= '-'.$etag;
  19. $gzout = false;
  20. $cache = OC_Cache::getGlobalCache();
  21. if (!OC_Request::isNoCache() && (!defined('DEBUG') || !DEBUG)) {
  22. OC_Response::setETagHeader($etag);
  23. $gzout = $cache->get($cache_key.'.gz');
  24. }
  25. if (!$gzout) {
  26. $out = $this->minimizeFiles($files);
  27. $gzout = gzencode($out);
  28. $cache->set($cache_key.'.gz', $gzout);
  29. OC_Response::setETagHeader($etag);
  30. }
  31. if ($encoding = OC_Request::acceptGZip()) {
  32. header('Content-Encoding: '.$encoding);
  33. $out = $gzout;
  34. } else {
  35. $out = gzdecode($gzout);
  36. }
  37. header('Content-Length: '.strlen($out));
  38. echo $out;
  39. }
  40. public function clearCache() {
  41. $cache = OC_Cache::getGlobalCache();
  42. $cache->clear('core.css');
  43. $cache->clear('core.js');
  44. }
  45. }
  46. if (!function_exists('gzdecode')) {
  47. function gzdecode($data,$maxlength=null,&$filename='',&$error='')
  48. {
  49. if (strcmp(substr($data,0,9),"\x1f\x8b\x8\0\0\0\0\0\0")) {
  50. return null; // Not the GZIP format we expect (See RFC 1952)
  51. }
  52. return gzinflate(substr($data,10,-8));
  53. }
  54. }