Blame view

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