home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 April / PCWorld_2005-04_cd.bin / akce / web / phptriad / phptriad2-2-1.exe / php / pear / Cache / OutputCompression.php < prev   
PHP Script  |  2001-08-10  |  7KB  |  229 lines

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP version 4.0                                                      |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997, 1998, 1999, 2000, 2001 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@nomad.ch>                         |
  17. // +----------------------------------------------------------------------+
  18.  
  19. require_once 'Cache/Output.php';
  20.  
  21. /**
  22. * Cache using Output Buffering and contnet (gz) compression.
  23. *
  24. * Based upon a case study from Christian Stocker and inspired by jpcache.
  25. *
  26. * @version  $Id: OutputCompression.php,v 1.5 2001/08/10 16:12:34 sebastian Exp $
  27. * @author   Ulf Wendel <ulf.wendel@phpdoc.de>, Christian Stocker <chregu@nomad.ch>
  28. * @access   public
  29. * @package  Cache
  30. */
  31. class Cache_OutputCompression extends Cache_Output {
  32.     
  33.     /**
  34.     * Encoding, what the user (its browser) of your website accepts
  35.     * 
  36.     * "auto" stands for test using $HTTP_ACCEPT_ENCODING.
  37.     *
  38.     * @var  string
  39.     * @see  Cache_OutputCompression(), setEncoding()
  40.     */
  41.     var $encoding = 'auto';
  42.  
  43.     
  44.     /**
  45.     * Method used for compression
  46.     *
  47.     * @var  string
  48.     * @see  isCompressed()
  49.     */ 
  50.     var $compression = '';
  51.  
  52.     
  53.     /**
  54.     * Sets the storage details and the content encoding used (if not autodetection)
  55.     * 
  56.     * @param    string  Name of container class
  57.     * @param    array   Array with container class options
  58.     * @param    string  content encoding mode - auto => test which encoding the user accepts
  59.     */    
  60.     function Cache_OutputCompression($container, $container_options = '', $encoding = 'auto') {
  61.     
  62.         $this->setEncoding($encoding);
  63.         $this->Cache($container, $container_options);
  64.         
  65.     } // end constructor
  66.  
  67.     
  68.     /**
  69.     * Call parent deconstructor.
  70.     */
  71.     function _Cache_OutputCompression() {
  72.         $this->_Cache();
  73.     } // end deconstructor
  74.     
  75.  
  76.     function generateID($variable) {
  77.         
  78.         $this->compression = $this->getEncoding();
  79.         
  80.         return md5(serialize($variable) . serialize($this->compression));
  81.     } // end generateID
  82.  
  83.     
  84.     function get($id, $group) {
  85.         $this->content = '';
  86.         
  87.         if (!$this->caching)
  88.             return '';
  89.         
  90.         if ($this->isCached($id, $group) && !$this->isExpired($id, $group))
  91.             $this->content = $this->load($id, $group);
  92.             
  93.         return $this->content;
  94.     } // end func get
  95.     
  96.     
  97.     /**
  98.     * Stops the output buffering, saves it to the cache and returns the _compressed_ content. 
  99.     *
  100.     * If you need the uncompressed content for further procession before
  101.     * it's saved in the cache use endGet(). endGet() does _not compress_.
  102.     */    
  103.     function end($expire = 0, $userdata = '') {
  104.         $content = ob_get_contents();
  105.         ob_end_clean();
  106.  
  107.         // store in the cache
  108.         if ($this->caching) {
  109.             $this->extSave($this->output_id, $content, $userdata, $expire, $this->output_group);
  110.             return $this->content;                
  111.         }
  112.             
  113.         return $content;        
  114.     } // end func end()
  115.     
  116.     
  117.     function endPrint($expire = 0, $userdata = '') {
  118.         $this->printContent($this->end($expire, $userdata));
  119.     } // end func endPrint
  120.  
  121.     
  122.     /**
  123.     * Saves the given data to the cache.
  124.     * 
  125.     */   
  126.     function extSave($id, $cachedata, $userdata, $expires = 0, $group = 'default') {
  127.         if (!$this->caching)
  128.             return true;
  129.  
  130.         if ($this->compression) {            
  131.             
  132.             $len = strlen($cachedata);            
  133.             $crc = crc32($cachedata);
  134.             $cachedata = gzcompress($cachedata, 9);
  135.             $this->content = substr($cachedata, 0, strlen($cachedata) - 4) . pack('V', $crc) . pack('V', $len);
  136.             
  137.         } else {
  138.             
  139.             $this->content = $cachedata;
  140.             
  141.         }
  142.         return $this->container->save($id, $this->content, $expires, $group, $userdata);
  143.     } // end func extSave
  144.     
  145.     /**
  146.     * Sends the compressed data to the user.
  147.     * 
  148.     * @param    string
  149.     * @access   public
  150.     * @global   $HTTP_SERVER_VARS
  151.     */    
  152.     function printContent($content = '') {
  153.         global $HTTP_SERVER_VARS;
  154.  
  155.         if ('' == $content)
  156.             $content = &$this->content;
  157.          
  158.         if ($this->compression && $this->caching) {
  159.    
  160.             $etag = 'PEAR-Cache-' . md5(substr($content, -40));
  161.             header("ETag: $etag");
  162.             if (strstr(stripslashes($HTTP_SERVER_VARS['HTTP_IF_NONE_MATCH']), $etag)) {
  163.                 // not modified
  164.                 header('HTTP/1.0 304');
  165.                 return;
  166.             } else {
  167.    
  168.                 // client acceppts some encoding - send headers & data
  169.                 header("Content-Encoding: {$this->compression}");
  170.                 header('Vary: Accept-Encoding');
  171.                 print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
  172.             }
  173.         
  174.         }
  175.         
  176.         print $content;
  177.     } // end func printContent
  178.     
  179.     
  180.     /**
  181.     * Returns the encoding method of the current dataset. 
  182.     *
  183.     * @access   public
  184.     * @return   string  Empty string (which evaluates to false) means no compression
  185.     */
  186.     function isCompressed() {
  187.         return $this->compression;
  188.     } // end func isCompressed
  189.  
  190.     /**
  191.     * Sets the encoding to be used.
  192.     * 
  193.     * @param    string  "auto" means autodetect for every client
  194.     * @access   public
  195.     * @see      $encoding
  196.     */
  197.     function setEncoding($encoding = 'auto') {
  198.         $this->encoding = $encoding;
  199.     } // end func setEncoding
  200.     
  201.     
  202.     /**
  203.     * Returns the encoding to be used for the data transmission to the client.
  204.     *
  205.     * @global   $HTTP_ACCEPT_ENCODING
  206.     * @see      setEncoding()
  207.     */    
  208.     function getEncoding() {
  209.         global $HTTP_ACCEPT_ENCODING;
  210.         
  211.         // encoding set by user    
  212.         if ('auto' != $this->encoding)
  213.             return $this->encoding;
  214.         
  215.         // check what the client accepts
  216.         if (false !== strpos($HTTP_ACCEPT_ENCODING, 'x-gzip'))
  217.             return 'x-gzip';
  218.         if (false !== strpos($HTTP_ACCEPT_ENCODING, 'gzip'))
  219.             return 'gzip';
  220.             
  221.         // no compression
  222.         return '';
  223.         
  224.     } // end func getEncoding
  225.  
  226.  
  227. } // end class OutputCompression
  228. ?>
  229.