~pvigo/+junk/owncloud-14.04

« back to all changes in this revision

Viewing changes to share/owncloud/lib/private/minimizer.php

  • Committer: Pablo Vigo
  • Date: 2014-12-15 13:36:46 UTC
  • Revision ID: pvigo@xtec.cat-20141215133646-7d6it90e1dbsijc2
2

Show diffs side-by-side

added added

removed removed

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