<?php
/**
* $Revision: 11 $
* $Author: CrashX $
* $Date: 2010-02-02 14:27:01 +0600 (Вт, 02 фев 2010)$
* $Id: gzip.php 11 2010-02-02 08:52:10Z CrashX $
* Copyright © CrashX <XSiteCMS@gmail.com>
* Всі права захищено © CrashX
*/
if(!defined('_SHELL')) die();
/* Класс GZip (сжатия страниц) */
class GZip {
var $version=0.02;
//BEGIN class ZIP
var $encoding=false;
var $data='';
var $size='';
var $crc32='';
var $http_encoding=false;
/**
* Конфигурация определение поддержки GZip браузером и сервером
* @return boolean
*/
function config() {
if(GZIP && (isset($_SERVER['HTTP_ACCEPT_ENCODING']) || isset($_SERVER['HTTP_TE']) )
if(headers_sent() || connection_aborted()
return false;
endif;
if(isset($_SERVER['HTTP_ACCEPT_ENCODING'])
$this->http_encoding=$_SERVER['HTTP_ACCEPT_ENCODING'];
elseif(isset($_SERVER['HTTP_TE'])
$this->http_encoding=$_SERVER['HTTP_TE'];
endif;
if(strpos($this->http_encoding,'x-gzip') !== false
$this->encoding="x-gzip";
endif;
if(strpos($this->http_encoding,'gzip') !== false
$this->encoding="gzip";
endif;
if(extension_loaded('zlib') && $this->encoding
ob_start();
ob_implicit_flush(0);
return true;
endif;
endif;
return false;
}
/**
* Сжатия данных
* @param string данные для сжатия
* @return string
*/
function compress($page) {
if(GZIP
if($this->encoding && function_exists("gzcompress")
$this->data=ob_get_contents();
if((strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 5")>0 || strpos($_SERVER['HTTP_USER_AGENT'], "MSIE 6.0")>0) && strpos($_SERVER['HTTP_USER_AGENT'], "Opera")===false
$this->data=str_repeat(" ", 2048)."\r\n".$this->data;
endif;
ob_end_clean();
$this->data.=$page;
$this->crc32=crc32($this->data);
$this->data=gzcompress($this->data,GZIP);
$this->size=strlen($this->data);
$this->data=substr($this->data,0,$this->size-4);
header("Content-Encoding: ".$this->encoding);
header("Content-Length: ".$this->size);
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00";
echo $this->data;
echo pack('V',$this->crc32);
echo pack('V',$this->size);
echo "\n<!-- Use ".$this->encoding." compress ".GZIP." -->";
echo "\n<!-- Not compress length: ".strlen($page)." -->";
echo "\n<!-- Compressed length: ".$this->size." -->";
exit();
endif;
endif;
return $page;
}
}
//END class GZIP
?>