Cache
[ class tree: Cache ] [ index: Cache ] [ all elements ]

Source for file OutputCompression.php

Documentation is available at OutputCompression.php

  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Cache                                                        |
  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. // +----------------------------------------------------------------------+
  18.  
  19. require_once 'Cache/Output.php';
  20.  
  21. /**
  22. * Cache using Output Buffering and contnet (gz) compression.
  23. ** Usage example:
  24. *
  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/');
  29. *
  30. *  // get a cache object
  31. *  $cache = new Cache_Output(CACHE_STORAGE_CLASS, array('cache_dir' => CACHE_DIR));
  32. *
  33. *  if (!($content = $cache->start($cache->generateID($REQUEST_URI)))) {
  34. *    print "hello world";
  35. *    $cache->endPrint(+1000);
  36. *  }
  37. *  else {
  38. *    $cache->printContent();
  39. *  }
  40. *
  41. *   OR
  42. *
  43. *  if (($content = $cache->start($cache->generateID($REQUEST_URI)))) {
  44. *    $cache->printContent();
  45. *    die();
  46. *  }
  47. *    print "hello world";
  48. *    $cache->endPrint(+1000);
  49. *
  50. *
  51. * Based upon a case study from Christian Stocker and inspired by jpcache.
  52. *
  53. @version  $Id: OutputCompression.php,v 1.7 2003/01/04 11:54:45 mj Exp $
  54. @author   Ulf Wendel <ulf.wendel@phpdoc.de>, Christian Stocker <chregu@phant.ch>
  55. @access   public
  56. @package  Cache
  57. */
  58.     
  59.     /**
  60.     * Encoding, what the user (its browser) of your website accepts
  61.     * 
  62.     * "auto" stands for test using $_SERVER['HTTP_ACCEPT_ENCODING']($HTTP_ACCEPT_ENCODING).
  63.     *
  64.     * @var  string 
  65.     * @see  Cache_OutputCompression(), setEncoding()
  66.     */
  67.     var $encoding = 'auto';
  68.  
  69.     
  70.     /**
  71.     * Method used for compression
  72.     *
  73.     * @var  string 
  74.     * @see  isCompressed()
  75.     */ 
  76.     var $compression = '';
  77.  
  78.     
  79.     /**
  80.     * Sets the storage details and the content encoding used (if not autodetection)
  81.     * 
  82.     * @param    string  Name of container class
  83.     * @param    array   Array with container class options
  84.     * @param    string  content encoding mode - auto => test which encoding the user accepts
  85.     */    
  86.     function Cache_OutputCompression($container$container_options ''$encoding 'auto'{
  87.     
  88.         $this->setEncoding($encoding);
  89.         $this->Cache($container$container_options);
  90.         
  91.     // end constructor
  92.  
  93.     
  94.     /**
  95.     * Call parent deconstructor.
  96.     */
  97.     function _Cache_OutputCompression({
  98.         $this->_Cache();
  99.     // end deconstructor
  100.     
  101.  
  102.     function generateID($variable{
  103.         
  104.         $this->compression = $this->getEncoding();
  105.         
  106.         return md5(serialize($variableserialize($this->compression));
  107.     // end generateID
  108.  
  109.     
  110.     function get($id$group{
  111.         $this->content '';
  112.         
  113.         if (!$this->caching)
  114.             return '';
  115.         
  116.         if ($this->isCached($id$group&& !$this->isExpired($id$group))
  117.             $this->content $this->load($id$group);
  118.             
  119.         return $this->content;
  120.     // end func get
  121.     
  122.     
  123.     /**
  124.     * Stops the output buffering, saves it to the cache and returns the _compressed_ content.
  125.     *
  126.     * If you need the uncompressed content for further procession before
  127.     * it's saved in the cache use endGet(). endGet() does _not compress_.
  128.     */    
  129.     function end($expire = 0$userdata ''{
  130.         $content ob_get_contents();
  131.         ob_end_clean();
  132.  
  133.         // store in the cache
  134.         if ($this->caching{
  135.             $this->extSave($this->output_id$content$userdata$expire$this->output_group);
  136.             return $this->content;                
  137.         }
  138.             
  139.         return $content;        
  140.     // end func end()
  141.     
  142.     
  143.     function endPrint($expire = 0$userdata ''{
  144.         $this->printContent($this->end($expire$userdata));
  145.     // end func endPrint
  146.  
  147.     
  148.     /**
  149.     * Saves the given data to the cache.
  150.     * 
  151.     */   
  152.     function extSave($id$cachedata$userdata$expires = 0$group 'default'{
  153.         if (!$this->caching)
  154.             return true;
  155.  
  156.         if ($this->compression{            
  157.             
  158.             $len strlen($cachedata);            
  159.             $crc crc32($cachedata);
  160.             $cachedata gzcompress($cachedata9);
  161.             $this->content substr($cachedata0strlen($cachedata- 4pack('V'$crcpack('V'$len);
  162.             
  163.         else {
  164.             
  165.             $this->content $cachedata;
  166.             
  167.         }
  168.         return $this->container->save($id$this->content$expires$group$userdata);
  169.     // end func extSave
  170.     
  171.     /**
  172.     * Sends the compressed data to the user.
  173.     * 
  174.     * @param    string 
  175.     * @access   public
  176.     */    
  177.     function printContent($content ''{
  178.         $server &$this->_importGlobalVariable("server");
  179.  
  180.         if ('' == $content)
  181.             $content &$this->container->cachedata;
  182.                  
  183.         if ($this->compression && $this->caching{
  184.    
  185.             $etag 'PEAR-Cache-' md5(substr($content-40));
  186.             header("ETag: $etag");
  187.             if (isset($server['HTTP_IF_NONE_MATCH']&& strstr(stripslashes($server['HTTP_IF_NONE_MATCH'])$etag)) {
  188.                 // not modified
  189.                 header('HTTP/1.0 304');
  190.                 return;
  191.             else {
  192.    
  193.                 // client acceppts some encoding - send headers & data
  194.                 header("Content-Encoding: {$this->compression}");
  195.                 header('Vary: Accept-Encoding');
  196.                 print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
  197.             }
  198.         
  199.         }
  200.         
  201.         die($content);
  202.     } // end func printContent
  203.     
  204.     
  205.     /**
  206.     * Returns the encoding method of the current dataset. 
  207.     *
  208.     * @access   public
  209.     * @return   string  Empty string (which evaluates to false) means no compression
  210.     */
  211.     function isCompressed() {
  212.         return $this->compression;
  213.     } // end func isCompressed
  214.     /**
  215.     * Sets the encoding to be used.
  216.     * 
  217.     * @param    string  "auto" means autodetect for every client
  218.     * @access   public
  219.     * @see      $encoding
  220.     */
  221.     function setEncoding($encoding = 'auto') {
  222.         $this->encoding = $encoding;
  223.     } // end func setEncoding
  224.     
  225.     
  226.     /**
  227.     * Returns the encoding to be used for the data transmission to the client.
  228.     *
  229.     * @see      setEncoding()
  230.     */    
  231.     function getEncoding() {
  232.         $server = &$this->_importGlobalVariable("server");
  233.  
  234.         // encoding set by user    
  235.         if ('auto' != $this->encoding)
  236.             return $this->encoding;
  237.         
  238.         // check what the client accepts
  239.         if (false !== strpos($server['HTTP_ACCEPT_ENCODING'], 'x-gzip'))
  240.             return 'x-gzip';
  241.         if (false !== strpos($server['HTTP_ACCEPT_ENCODING'], 'gzip'))
  242.             return 'gzip';
  243.             
  244.         // no compression
  245.         return '';
  246.         
  247.     } // end func getEncoding
  248.     // {{{ _importGlobalVariable()
  249.     /**
  250.      * Import variables from special namespaces.
  251.      *
  252.      * @access private
  253.      * @param string Type of variable (server, session, post)
  254.      * @return array
  255.      */
  256.     function &_importGlobalVariable($variable) 
  257.     {
  258.       
  259.         $var = null;
  260.  
  261.         switch (strtolower($variable)) {
  262.  
  263.             case "server" :
  264.                 if (isset($_SERVER)) {
  265.                     $var = &$_SERVER;
  266.                 } else {
  267.                     $var = &$GLOBALS['HTTP_SERVER_VARS'];
  268.                 }
  269.                 break;
  270.  
  271.             case "session" :
  272.                 if (isset($_SESSION)) {
  273.                     $var = &$_SESSION;
  274.                 } else {
  275.                     $var = &$GLOBALS['HTTP_SESSION_VARS'];
  276.                 }
  277.                 break;
  278.  
  279.             case "post" :
  280.                 if (isset($_POST)) {
  281.                     $var = &$_POST;
  282.                 } else {
  283.                     $var = &$GLOBALS['HTTP_POST_VARS'];
  284.                 }
  285.                 break;
  286.  
  287.             default:
  288.                 break;
  289.  
  290.         }
  291.  
  292.         return $var;
  293.     } 
  294.  
  295.     // }}
  296. } // end class OutputCompression

Documentation generated on Mon, 11 Mar 2019 10:14:24 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.