/***************************************
PHP4 实现 HTTP 动态内容压缩加速传输
** 标题:PHP4 HTTP 压缩加速 Web 浏览
** 版本:1.10
** 作者:catoc
** 文件名:gzdoc.php
** 最后修改:25/08/2000
** 需求:PHP4 >= 4.0.1
** PHP 需配置 --with-zlib[=DIR]
** 说明:动态内容加速压缩
** 实时处理数据传输数据
** 代码由 sun jin hu (catoc) 编写
** 大多数自 1998/1999 年以来的较新浏览器
** 均已配备支持 HTTP 1.1
** 标准中被称为 "content-encoding" 的功能。
** 本质上,浏览器向服务器指示
** 它能够接受 "content encoding"
** 如果服务器具备能力,它将随后
** 压缩数据并传输。
** 浏览器解压缩它,然后渲染
** 页面。
** 用法:
** 在第一个 '' 标签开始之前不要留空格。
** ------------文件开始----------
** |
** | include('gzdoc.php');
** | print "Start output !!";
** |?>
** |
** |... 页面 ...
** |
** |
** | gzdocout();
** |?>
** -------------文件结束-----------
***************************************/
ob_start();
ob_implicit_flush(0);
function GetHeader(){
$headers = getallheaders();
while (list($header, $value) = each($headers)) {
$Message .= "$header: $value
\n";
}
return $Message;
}
function CheckCanGzip(){
global $HTTP_ACCEPT_ENCODING, $PHP_SELF, $Wget, $REMOTE_ADDR, $S_UserName;
if (connection_timeout() || connection_aborted()){
return 0;
}
if ((strpos('catoc'.$HTTP_ACCEPT_ENCODING, 'gzip')) || $Wget == 'Y'){
if (strpos('catoc'.$HTTP_ACCEPT_ENCODING, 'x-gzip')){
$ENCODING = "x-gzip";
$Error_Msg = str_replace('
','',GetHeader());
$Error_Msg .= "Time: ".date("Y-m-d H:i:s")."\n";
$Error_Msg .= "Remote-Address: ".$REMOTE_ADDR."\n";
//mail('your@none.net', "User have x-gzip output in file $PHP_SELF!!!", $Error_Msg);
}else{
$ENCODING = "gzip";
}
return $ENCODING;
}else{
return 0;
}
}
function GzDocOut(){
global $PHP_SELF, $CatocGz, $REMOTE_ADDR, $S_UserName;
$ENCODING = CheckCanGzip();
if ($ENCODING){
print "\n\n";
$Contents = ob_get_contents();
ob_end_clean();
if ($CatocGz == 'Y'){
print "Not compress lenth: ".strlen($Contents)."
";
print "Compressed lenth: ".strlen(gzcompress($Contents))."
";
exit;
}else{
header("Content-Encoding: $ENCODING");
}
print pack('cccccccc',0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00);
$Size = strlen($Contents);
$Crc = crc32($Contents);
$Contents = gzcompress($Contents);
$Contents = substr($Contents, 0, strlen($Contents) - 4);
print $Contents;
print pack('V',$Crc);
print pack('V',$Size);
exit;
}else{
ob_end_flush();
$Error_Msg = str_replace('
','',GetHeader());
$Error_Msg .= "Time: ".date("Y-m-d H:i:s")."\n";
$Error_Msg .= "Remote-Address: ".$REMOTE_ADDR."\n";
//mail('your@none.net', "User can not use gzip output in file $PHP_SELF!!!", $Error_Msg);
exit;
}
}
?>