fluxcms /inc/Cache/OutputCompression.php

Language PHP Lines 302
MD5 Hash 51f6e13d27f115a573b89fde57a1ac67
Repository https://github.com/chregu/fluxcms.git View Raw File View Project SPDX
  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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
// +----------------------------------------------------------------------+
// | PEAR :: Cache                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available at through the world-wide-web at                           |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | license@php.net so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Ulf Wendel <ulf.wendel@phpdoc.de>                           |
// |          Christian Stocker <chregu@phant.ch>                         |
// +----------------------------------------------------------------------+

require_once 'Cache/Output.php';

/**
* Cache using Output Buffering and contnet (gz) compression.
** Usage example:
*
*  // place this somewhere in a central config file
*  define(CACHE_STORAGE_CLASS, 'file');
*  // file storage needs a dir to put the cache files
*  define(CACHE_DIR, '/var/tmp/');
*
*  // get a cache object
*  $cache = new Cache_Output(CACHE_STORAGE_CLASS, array('cache_dir' => CACHE_DIR));
*
*  if (!($content = $cache->start($cache->generateID($REQUEST_URI)))) {
*    print "hello world";
*    $cache->endPrint(+1000);
*  }
*  else {
*    $cache->printContent();
*  }
*
*   OR
*
*  if (($content = $cache->start($cache->generateID($REQUEST_URI)))) {
*    $cache->printContent();
*    die();
*  }
*    print "hello world";
*    $cache->endPrint(+1000);
*
*
* Based upon a case study from Christian Stocker and inspired by jpcache.
*
* @version  $Id$
* @author   Ulf Wendel <ulf.wendel@phpdoc.de>, Christian Stocker <chregu@phant.ch>
* @access   public
* @package  Cache
*/
class Cache_OutputCompression extends Cache_Output {
    
    /**
    * Encoding, what the user (its browser) of your website accepts
    * 
    * "auto" stands for test using $_SERVER['HTTP_ACCEPT_ENCODING']($HTTP_ACCEPT_ENCODING).
    *
    * @var  string
    * @see  Cache_OutputCompression(), setEncoding()
    */
    var $encoding = 'auto';
 
    
    /**
    * Method used for compression
    *
    * @var  string
    * @see  isCompressed()
    */ 
    var $compression = '';

    
    /**
    * Sets the storage details and the content encoding used (if not autodetection)
    * 
    * @param    string  Name of container class
    * @param    array   Array with container class options
    * @param    string  content encoding mode - auto => test which encoding the user accepts
    */    
    function Cache_OutputCompression($container, $container_options = '', $encoding = 'auto') {
    
        $this->setEncoding($encoding);
        $this->Cache($container, $container_options);
        
    } // end constructor

    
    /**
    * Call parent deconstructor.
    */
    function _Cache_OutputCompression() {
        $this->_Cache();
    } // end deconstructor
    

    function generateID($variable) {
        
        $this->compression = $this->getEncoding();
        
        return md5(serialize($variable) . serialize($this->compression));
    } // end generateID

    
    function get($id, $group) {
        $this->content = '';
        
        if (!$this->caching)
            return '';
        
        if ($this->isCached($id, $group) && !$this->isExpired($id, $group))
            $this->content = $this->load($id, $group);
            
        return $this->content;
    } // end func get
    
    
    /**
    * Stops the output buffering, saves it to the cache and returns the _compressed_ content. 
    *
    * If you need the uncompressed content for further procession before
    * it's saved in the cache use endGet(). endGet() does _not compress_.
    */    
    function end($expire = 0, $userdata = '') {
        $content = ob_get_contents();
        ob_end_clean();

        // store in the cache
        if ($this->caching) {
            $this->extSave($this->output_id, $content, $userdata, $expire, $this->output_group);
            return $this->content;                
        }
            
        return $content;        
    } // end func end()
    
    
    function endPrint($expire = 0, $userdata = '') {
        $this->printContent($this->end($expire, $userdata));
    } // end func endPrint

    
    /**
    * Saves the given data to the cache.
    * 
    */   
    function extSave($id, $cachedata, $userdata, $expires = 0, $group = 'default') {
        if (!$this->caching)
            return true;

        if ($this->compression) {            
            
            $len = strlen($cachedata);            
            $crc = crc32($cachedata);
            $cachedata = gzcompress($cachedata, 9);
            $this->content = substr($cachedata, 0, strlen($cachedata) - 4) . pack('V', $crc) . pack('V', $len);
            
        } else {
            
            $this->content = $cachedata;
            
        }
        return $this->container->save($id, $this->content, $expires, $group, $userdata);
    } // end func extSave
    
    /**
    * Sends the compressed data to the user.
    * 
    * @param    string
    * @access   public
    */    
    function printContent($content = '') {
        $server = &$this->_importGlobalVariable("server");

        if ('' == $content)
            $content = &$this->container->cachedata;
                 
        if ($this->compression && $this->caching) {
   
            $etag = 'PEAR-Cache-' . md5(substr($content, -40));
            header("ETag: $etag");
            if (isset($server['HTTP_IF_NONE_MATCH']) && strstr(stripslashes($server['HTTP_IF_NONE_MATCH']), $etag)) {
                // not modified
                header('HTTP/1.0 304');
                return;
            } else {
   
                // client acceppts some encoding - send headers & data
                header("Content-Encoding: {$this->compression}");
                header('Vary: Accept-Encoding');
                print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
            }
        
        }
        
        die($content);
    } // end func printContent
    
    
    /**
    * Returns the encoding method of the current dataset. 
    *
    * @access   public
    * @return   string  Empty string (which evaluates to false) means no compression
    */
    function isCompressed() {
        return $this->compression;
    } // end func isCompressed

    /**
    * Sets the encoding to be used.
    * 
    * @param    string  "auto" means autodetect for every client
    * @access   public
    * @see      $encoding
    */
    function setEncoding($encoding = 'auto') {
        $this->encoding = $encoding;
    } // end func setEncoding
    
    
    /**
    * Returns the encoding to be used for the data transmission to the client.
    *
    * @see      setEncoding()
    */    
    function getEncoding() {
        $server = &$this->_importGlobalVariable("server");

        // encoding set by user    
        if ('auto' != $this->encoding)
            return $this->encoding;
        
        // check what the client accepts
        if (false !== strpos($server['HTTP_ACCEPT_ENCODING'], 'x-gzip'))
            return 'x-gzip';
        if (false !== strpos($server['HTTP_ACCEPT_ENCODING'], 'gzip'))
            return 'gzip';
            
        // no compression
        return '';
        
    } // end func getEncoding

    // {{{ _importGlobalVariable()

    /**
     * Import variables from special namespaces.
     *
     * @access private
     * @param string Type of variable (server, session, post)
     * @return array
     */
    function &_importGlobalVariable($variable) 
    {
      
        $var = null;

        switch (strtolower($variable)) {

            case "server" :
                if (isset($_SERVER)) {
                    $var = &$_SERVER;
                } else {
                    $var = &$GLOBALS['HTTP_SERVER_VARS'];
                }
                break;

            case "session" :
                if (isset($_SESSION)) {
                    $var = &$_SESSION;
                } else {
                    $var = &$GLOBALS['HTTP_SESSION_VARS'];
                }
                break;

            case "post" :
                if (isset($_POST)) {
                    $var = &$_POST;
                } else {
                    $var = &$GLOBALS['HTTP_POST_VARS'];
                }
                break;

            default:
                break;

        }

        return $var;
    } 

    // }}
} // end class OutputCompression
?>
Back to Top