Project

General

Profile

Bug #16772 » 0004623.patch

Administrator Admin, 2007-03-16 13:16

View differences:

t3lib/class.gzip_encode.php (Arbeitskopie)
* had. Used for debugging.
*/
function gzip_encode($level = 3, $debug = false, $outputCompressedSizes=0) {
if (!function_exists('gzcompress')) {
trigger_error('gzcompress not found, ' .
'zlib needs to be installed for gzip_encode',
E_USER_WARNING);
return;
}
if (!function_exists('crc32')) {
trigger_error('crc32() not found, ' .
'PHP >= 4.0.1 needed for gzip_encode', E_USER_WARNING);
return;
}
if (headers_sent()) return;
if (connection_status() !== 0) return;
$encoding = $this->gzip_accepted();
if (!$encoding) return;
$this->encoding = $encoding;
if (!function_exists('gzcompress')) {
trigger_error('gzcompress not found, ' .
'zlib needs to be installed for gzip_encode',
E_USER_WARNING);
return;
}
if (!function_exists('crc32')) {
trigger_error('crc32() not found, ' .
'PHP >= 4.0.1 needed for gzip_encode', E_USER_WARNING);
return;
}
if (headers_sent()) return;
if (connection_status() !== 0) return;
$encoding = $this->gzip_accepted();
if (!$encoding) return;
$this->encoding = $encoding;
if (strtolower($level) == 'true' || $level === true) {
$level = $this->get_complevel();
}
$this->level = $level;
if (strtolower($level) == 'true' || $level === true) {
$level = $this->get_complevel();
}
$this->level = $level;
$contents = ob_get_contents();
if ($contents === false) return;
$contents = ob_get_contents();
if ($contents === false) return;
$gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00"; // gzip header
$gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00"; // gzip header
// By Kasper Skaarhoj, start
if ($outputCompressedSizes) {
$contents.=chr(10)."<!-- Compressed, level ".$level.", original size was ".strlen($contents)." bytes. New size is ".strlen(gzcompress($contents, $level))." bytes -->";
$size = strlen($contents); // Must set again!
}
// By Kasper Skaarhoj, end
// By Kasper Skaarhoj, start
if ($outputCompressedSizes) {
$contents.=chr(10)."<!-- Compressed, level ".$level.", original size was ".strlen($contents)." bytes. New size is ".strlen(gzcompress($contents, $level))." bytes -->";
$size = strlen($contents); // Must set again!
}
// By Kasper Skaarhoj, end
$size = strlen($contents);
$crc = crc32($contents);
$gzdata .= gzcompress($contents, $level);
$gzdata = substr($gzdata, 0, strlen($gzdata) - 4); // fix crc bug
$gzdata .= pack("V",$crc) . pack("V", $size);
$size = strlen($contents);
$crc = crc32($contents);
$gzdata .= gzcompress($contents, $level);
$gzdata = substr($gzdata, 0, strlen($gzdata) - 4); // fix crc bug
$gzdata .= pack("V",$crc) . pack("V", $size);
$this->size = $size;
$this->crc = $crc;
$this->gzsize = strlen($gzdata);
$this->size = $size;
$this->crc = $crc;
$this->gzsize = strlen($gzdata);
if ($debug) {
return;
}
if ($debug) {
return;
}
ob_end_clean();
Header('Content-Encoding: ' . $encoding);
Header('Content-Length: ' . strlen($gzdata));
Header('X-Content-Encoded-By: class.gzip_encode '.$this->_version);
ob_end_clean();
Header('Content-Encoding: ' . $encoding);
Header('Content-Length: ' . strlen($gzdata));
Header('X-Content-Encoded-By: class.gzip_encode '.$this->_version);
echo $gzdata;
echo $gzdata;
}
/*
/**
* gzip_accepted() - Test headers for Accept-Encoding: gzip
*
* Returns: if proper headers aren't found: false
* if proper headers are found: 'gzip' or 'x-gzip'
*
......
* }
* note the double colon syntax, I don't know where it is documented but
* somehow it got in my brain.
*/
*
* @return mixed Returns 'gzip' if the client browser accepts gzip encoding, otherwise false
**/
function gzip_accepted() {
if (strpos(getenv("HTTP_ACCEPT_ENCODING"), 'gzip') === false) return false;
if (strpos(getenv("HTTP_ACCEPT_ENCODING"), 'x-gzip') === false) {
$encoding = 'gzip';
} else {
$encoding = 'x-gzip';
}
// Checks, if the accepted encoding supports gzip or x-gzip.
// Furthermore a qvalue check is done. "gzip;q=0" means to gzip accepted at all.
$acceptEncoding = t3lib_div::getIndpEnv('HTTP_ACCEPT_ENCODING');
if (preg_match('/(^|,\s*)(x-)?gzip(;q=(\d(\.\d+)?))?(,|$)/', $acceptEncoding, $match) && ($match[4] === '' || $match[4] > 0)) {
$encoding = 'gzip';
} else {
return false;
}
// Test file type. I wish I could get HTTP response headers.
$magic = substr(ob_get_contents(),0,4);
if (substr($magic,0,2) === '^_') {
// gzip data
$encoding = false;
} else if (substr($magic,0,3) === 'GIF') {
// gif images
$encoding = false;
} else if (substr($magic,0,2) === "\xFF\xD8") {
// jpeg images
$encoding = false;
} else if (substr($magic,0,4) === "\x89PNG") {
// png images
$encoding = false;
} else if (substr($magic,0,3) === 'FWS') {
// Don't gzip Shockwave Flash files. Flash on windows incorrectly
// claims it accepts gzip'd content.
$encoding = false;
} else if (substr($magic,0,2) === 'PK') {
// pk zip file
$encoding = false;
}
// Test file type. I wish I could get HTTP response headers.
$magic = substr(ob_get_contents(),0,4);
if (substr($magic,0,2) === '^_') {
// gzip data
$encoding = false;
} else if (substr($magic,0,3) === 'GIF') {
// gif images
$encoding = false;
} else if (substr($magic,0,2) === "\xFF\xD8") {
// jpeg images
$encoding = false;
} else if (substr($magic,0,4) === "\x89PNG") {
// png images
$encoding = false;
} else if (substr($magic,0,3) === 'FWS') {
// Don't gzip Shockwave Flash files. Flash on windows incorrectly
// claims it accepts gzip'd content.
$encoding = false;
} else if (substr($magic,0,2) === 'PK') {
// pk zip file
$encoding = false;
}
return $encoding;
return $encoding;
}
/*
......
* this work with your OS - Thanks
*/
function get_complevel() {
$uname = posix_uname();
switch ($uname['sysname']) {
case 'Linux':
$cl = (1 - $this->linux_loadavg()) * 10;
$level = (int)max(min(9, $cl), 0);
break;
case 'FreeBSD':
$cl = (1 - $this->freebsd_loadavg()) * 10;
$level = (int)max(min(9, $cl), 0);
break;
default:
$level = 3;
break;
}
return $level;
$uname = posix_uname();
switch ($uname['sysname']) {
case 'Linux':
$cl = (1 - $this->linux_loadavg()) * 10;
$level = (int)max(min(9, $cl), 0);
break;
case 'FreeBSD':
$cl = (1 - $this->freebsd_loadavg()) * 10;
$level = (int)max(min(9, $cl), 0);
break;
default:
$level = 3;
break;
}
return $level;
}
/*
......
* The max() Load Average will be returned
*/
function linux_loadavg() {
$buffer = "0 0 0";
$f = fopen("/proc/loadavg","rb");
if (!feof($f)) {
$buffer = fgets($f, 1024);
}
fclose($f);
$load = explode(" ",$buffer);
return max((float)$load[0], (float)$load[1], (float)$load[2]);
$buffer = "0 0 0";
$f = fopen("/proc/loadavg","rb");
if (!feof($f)) {
$buffer = fgets($f, 1024);
}
fclose($f);
$load = explode(" ",$buffer);
return max((float)$load[0], (float)$load[1], (float)$load[2]);
}
/*
......
* test it?
*/
function freebsd_loadavg() {
$buffer= `uptime`;
$load = array();
ereg("averag(es|e): ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]*)", $buffer, $load);
$buffer= `uptime`;
$load = array();
ereg("averag(es|e): ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]*)", $buffer, $load);
return max((float)$load[2], (float)$load[3], (float)$load[4]);
return max((float)$load[2], (float)$load[3], (float)$load[4]);
}
}
t3lib/class.t3lib_div.php (Arbeitskopie)
case 'HTTP_HOST':
case 'HTTP_USER_AGENT':
case 'HTTP_ACCEPT_LANGUAGE':
case 'HTTP_ACCEPT_ENCODING':
case 'QUERY_STRING':
$retVal = $_SERVER[$getEnvName];
break;
(1-1/4)