minimizer.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. abstract class OC_Minimizer {
  3. public function getLastModified($files) {
  4. $last_modified = 0;
  5. foreach($files as $file_info) {
  6. $file = $file_info[0] . '/' . $file_info[2];
  7. $filemtime = filemtime($file);
  8. if ($filemtime > $last_modified) {
  9. $last_modified = $filemtime;
  10. }
  11. }
  12. return $last_modified;
  13. }
  14. abstract public function minimizeFiles($files);
  15. public function output($files, $cache_key) {
  16. header('Content-Type: '.$this->contentType);
  17. OC_Response::enableCaching();
  18. $last_modified = $this->getLastModified($files);
  19. OC_Response::setLastModifiedHeader($last_modified);
  20. $gzout = false;
  21. $cache = OC_Cache::getGlobalCache();
  22. if (!OC_Request::isNoCache() && (!defined('DEBUG') || !DEBUG)){
  23. $gzout = $cache->get($cache_key.'.gz');
  24. if ($gzout) {
  25. OC_Response::setETagHeader(md5($gzout));
  26. }
  27. }
  28. if (!$gzout) {
  29. $out = $this->minimizeFiles($files);
  30. $gzout = gzencode($out);
  31. OC_Response::setETagHeader(md5($gzout));
  32. $cache->set($cache_key.'.gz', $gzout);
  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. }
  44. if (!function_exists('gzdecode')) {
  45. function gzdecode($data,$maxlength=null,&$filename='',&$error='')
  46. {
  47. if (strcmp(substr($data,0,9),"\x1f\x8b\x8\0\0\0\0\0\0")) {
  48. return null; // Not the GZIP format we expect (See RFC 1952)
  49. }
  50. return gzinflate(substr($data,10,-8));
  51. }
  52. }