summaryrefslogtreecommitdiffstats
path: root/lib/minimizer.php
blob: 3dc89e331a648f0498ab33b187052f0fc2fc1b59 (plain)
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
<?php

abstract class OC_Minimizer {
	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);

	public function output($files, $cache_key) {
		header('Content-Type: '.$this->contentType);
		OC_Response::enableCaching();
		$last_modified = $this->getLastModified($files);
		OC_Response::setLastModifiedHeader($last_modified);

		$gzout = false;
		$cache = OC_Cache::getGlobalCache();
		if (!OC_Request::isNoCache() && (!defined('DEBUG') || !DEBUG)){
			$gzout = $cache->get($cache_key.'.gz');
			if ($gzout) {
				OC_Response::setETagHeader(md5($gzout));
			}
		}

		if (!$gzout) {
			$out = $this->minimizeFiles($files);
			$gzout = gzencode($out);
			OC_Response::setETagHeader(md5($gzout));
			$cache->set($cache_key.'.gz', $gzout);
		}
		if ($encoding = OC_Request::acceptGZip()) {
			header('Content-Encoding: '.$encoding);
			$out = $gzout;
		} else {
			$out = gzdecode($gzout);
		}
		header('Content-Length: '.strlen($out));
		echo $out;
	}
}

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));
	}
}