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

Documentation generated on Mon, 11 Mar 2019 15:44:50 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.