2 // +----------------------------------------------------------------------+
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997-2003 The PHP Group |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 2.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/2_02.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+
15 // | Authors: Ulf Wendel <ulf.wendel@phpdoc.de> |
16 // | Christian Stocker <chregu@phant.ch> |
17 // +----------------------------------------------------------------------+
19 require_once 'Cache/Output.php';
22 * Cache using Output Buffering and contnet (gz) compression.
25 * // place this somewhere in a central config file
26 * define(CACHE_STORAGE_CLASS, 'file');
27 * // file storage needs a dir to put the cache files
28 * define(CACHE_DIR, '/var/tmp/');
30 * // get a cache object
31 * $cache = new Cache_Output(CACHE_STORAGE_CLASS, array('cache_dir' => CACHE_DIR));
33 * if (!($content = $cache->start($cache->generateID($REQUEST_URI)))) {
34 * print "hello world";
35 * $cache->endPrint(+1000);
38 * $cache->printContent();
43 * if (($content = $cache->start($cache->generateID($REQUEST_URI)))) {
44 * $cache->printContent();
47 * print "hello world";
48 * $cache->endPrint(+1000);
51 * Based upon a case study from Christian Stocker and inspired by jpcache.
53 * @version $Id: OutputCompression.php 178289 2005-01-26 09:47:28Z dufuz $
54 * @author Ulf Wendel <ulf.wendel@phpdoc.de>, Christian Stocker <chregu@phant.ch>
58 class Cache_OutputCompression extends Cache_Output
62 * Encoding, what the user (its browser) of your website accepts
64 * "auto" stands for test using $_SERVER['HTTP_ACCEPT_ENCODING']($HTTP_ACCEPT_ENCODING).
67 * @see Cache_OutputCompression(), setEncoding()
69 var $encoding = 'auto';
73 * Method used for compression
78 var $compression = '';
82 * Sets the storage details and the content encoding used (if not autodetection)
84 * @param string Name of container class
85 * @param array Array with container class options
86 * @param string content encoding mode - auto => test which encoding the user accepts
88 function Cache_OutputCompression($container, $container_options = '', $encoding = 'auto')
90 $this->setEncoding($encoding);
91 $this->Cache($container, $container_options);
97 * Call parent deconstructor.
99 function _Cache_OutputCompression()
102 } // end deconstructor
105 function generateID($variable)
107 $this->compression = $this->getEncoding();
108 return md5(serialize($variable) . serialize($this->compression));
112 function get($id, $group)
116 if (!$this->caching) {
120 if ($this->isCached($id, $group) && !$this->isExpired($id, $group)) {
121 $this->content = $this->load($id, $group);
123 return $this->content;
128 * Stops the output buffering, saves it to the cache and returns the _compressed_ content.
130 * If you need the uncompressed content for further procession before
131 * it's saved in the cache use endGet(). endGet() does _not compress_.
133 function end($expire = 0, $userdata = '')
135 $content = ob_get_contents();
138 // store in the cache
139 if ($this->caching) {
140 $this->extSave($this->output_id, $content, $userdata, $expire, $this->output_group);
141 return $this->content;
148 function endPrint($expire = 0, $userdata = '')
150 $this->printContent($this->end($expire, $userdata));
151 } // end func endPrint
155 * Saves the given data to the cache.
158 function extSave($id, $cachedata, $userdata, $expires = 0, $group = 'default')
160 if (!$this->caching) {
164 if ($this->compression) {
165 $len = strlen($cachedata);
166 $crc = crc32($cachedata);
167 $cachedata = gzcompress($cachedata, 9);
168 $this->content = substr($cachedata, 0, strlen($cachedata) - 4) . pack('V', $crc) . pack('V', $len);
170 $this->content = $cachedata;
172 return $this->container->save($id, $this->content, $expires, $group, $userdata);
173 } // end func extSave
176 * Sends the compressed data to the user.
181 function printContent($content = '')
183 $server = &$this->_importGlobalVariable("server");
185 if ($content == '') {
186 $content = &$this->container->cachedata;
189 if ($this->compression && $this->caching) {
190 $etag = '"PEAR-Cache-' . md5(substr($content, -40)) .'"';
191 header("ETag: $etag");
192 if (isset($server['HTTP_IF_NONE_MATCH']) && strstr(stripslashes($server['HTTP_IF_NONE_MATCH']), $etag)) {
194 header('HTTP/1.0 304');
197 // client acceppts some encoding - send headers & data
198 header("Content-Encoding: {$this->compression}");
199 header('Vary: Accept-Encoding');
200 print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
206 } // end func printContent
210 * Returns the encoding method of the current dataset.
213 * @return string Empty string (which evaluates to false) means no compression
215 function isCompressed()
217 return $this->compression;
218 } // end func isCompressed
221 * Sets the encoding to be used.
223 * @param string "auto" means autodetect for every client
227 function setEncoding($encoding = 'auto')
229 $this->encoding = $encoding;
230 } // end func setEncoding
234 * Returns the encoding to be used for the data transmission to the client.
238 function getEncoding()
240 $server = &$this->_importGlobalVariable("server");
242 // encoding set by user
243 if ('auto' != $this->encoding) {
244 return $this->encoding;
246 // check what the client accepts
247 if (false !== strpos($server['HTTP_ACCEPT_ENCODING'], 'x-gzip')) {
250 if (false !== strpos($server['HTTP_ACCEPT_ENCODING'], 'gzip')) {
256 } // end func getEncoding
258 // {{{ _importGlobalVariable()
261 * Import variables from special namespaces.
264 * @param string Type of variable (server, session, post)
267 function &_importGlobalVariable($variable)
272 switch (strtolower($variable)) {
275 if (isset($_SERVER)) {
278 $var = &$GLOBALS['HTTP_SERVER_VARS'];
283 if (isset($_SESSION)) {
286 $var = &$GLOBALS['HTTP_SESSION_VARS'];
294 $var = &$GLOBALS['HTTP_POST_VARS'];
307 } // end class OutputCompression