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 / HTTP / Compress.php
PHP Script  |  2001-08-07  |  4KB  |  107 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0                                                      |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2001 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Mark Nottingham <mnot@pobox.com>                            |
  17. // |          Chuck Hagenbuch <chuck@horde.org>                           |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Compress.php,v 1.3 2001/08/07 01:07:26 alexmerz Exp $
  21. //
  22.  
  23. /**
  24.  * HTTP_Compress:: provides a wrapper around php's output buffering
  25.  * mechanisms and also does compression, generates headers - ETag,
  26.  * Content-Length, etc. - which may be beneficial to bandwidth
  27.  * usage and performance.
  28.  *
  29.  * @author Mark Nottingham <mnot@pobox.com>
  30.  * @author Chuck Hagenbuch <chuck@horde.org>
  31.  * @version $Revision: 1.3 $
  32.  * @package HTTP
  33.  */
  34. class HTTP_Compress {
  35.     
  36.     /**
  37.      * Start the output buffer, and make sure that implicit flush is
  38.      * off so that data is always buffered.
  39.      * @access public
  40.      */
  41.     function start()
  42.     {
  43.         ob_start();
  44.         ob_implicit_flush(0);
  45.     }
  46.     
  47.     /**
  48.      * Output the contents of the output buffer, compressed if
  49.      * desired, along with any relevant headers.
  50.      *
  51.      * @param boolean $compress (optional) Use gzip compression, if the browser supports it.
  52.      * @param boolean $use_etag Generate an ETag, and don't send the body if the browser has the same object cached.
  53.      * @param boolean $send_body Send the body of the request? Might be false for HEAD requests.
  54.      * @access public
  55.      */
  56.     function output($compress = true, $use_etag = true, $send_body = true)
  57.     {
  58.         $min_gz_size = 1024;
  59.         $page = ob_get_contents();
  60.         $length = strlen($page);
  61.         ob_end_clean();
  62.         
  63.         if ($compress && extension_loaded('zlib') && (strlen($page) > $min_gz_size) && isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_ACCEPT_ENCODING'])) {
  64.             $ae = explode(',', str_replace(' ', '', $GLOBALS['HTTP_SERVER_VARS']['HTTP_ACCEPT_ENCODING']));
  65.             $enc = false;
  66.             if (in_array('gzip', $ae)) {
  67.                 $enc = 'gzip';
  68.             } else if (in_array('x-gzip', $ae)) {
  69.                 $enc = 'x-gzip';
  70.             }
  71.             
  72.             if ($enc) {
  73.                 $page = gzencode($page);
  74.                 $length = strlen($page);
  75.                 header('Content-Encoding: ' . $enc);
  76.                 header('Vary: Accept-Encoding');
  77.             } else {
  78.                 $compress = false;
  79.             }
  80.         } else {
  81.             $compress = false;
  82.         }
  83.         
  84.         if ($use_etag) {
  85.             $etag = '"' . md5($page) . '"';
  86.             header('ETag: ' . $etag);
  87.             if (isset($GLOBALS['HTTP_SERVER_VARS']['HTTP_IF_NONE_MATCH'])) {
  88.                 $inm = explode(',', $GLOBALS['HTTP_SERVER_VARS']['HTTP_IF_NONE_MATCH']);
  89.                 foreach ($inm as $i) {
  90.                     if (trim($i) == $etag) {
  91.                         header('HTTP/1.0 304 Not Modified');
  92.                         $send_body = false;
  93.                         break;
  94.                     }
  95.                 }
  96.             }
  97.         }
  98.         
  99.         if ($send_body) {
  100.             header('Content-Length: ' . $length);
  101.             echo $page;
  102.         }
  103.     }
  104.     
  105. }
  106. ?>
  107.