/src/typo3_src-4.1.2/t3lib/class.gzip_encode.php

00001 <?php
00054 class gzip_encode {
00163         var $_version = 0.66; // Version of the gzip_encode class
00164 
00165         var $level;             // Compression level
00166         var $encoding;  // Encoding type
00167         var $crc;               // crc of the output
00168         var $size;              // size of the uncompressed content
00169         var $gzsize;    // size of the compressed content
00170 
00194         function gzip_encode($level=3, $debug=false, $outputCompressedSizes=false) {
00195                 if (!function_exists('gzcompress')) {
00196                     trigger_error('gzcompress not found, ' .
00197                             'zlib needs to be installed for gzip_encode',
00198                             E_USER_WARNING);
00199                     return;
00200                 }
00201                 if (!function_exists('crc32')) {
00202                     trigger_error('crc32() not found, ' .
00203                             'PHP >= 4.0.1 needed for gzip_encode', E_USER_WARNING);
00204                     return;
00205                 }
00206                 if (headers_sent()) return;
00207                 if (connection_status() !== 0) return;
00208                 $encoding = $this->gzip_accepted();
00209                 if (!$encoding) return;
00210                 $this->encoding = $encoding;
00211 
00212                 if (strtolower($level) == 'true' || $level === true) {
00213                     $level = $this->get_complevel();
00214                 }
00215                 $this->level = $level;
00216 
00217                 $contents = ob_get_contents();
00218                 if ($contents === false) return;
00219 
00220                 $gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00"; // gzip header
00221 
00222                         // By Kasper Skaarhoj, start
00223                 if ($outputCompressedSizes)     {
00224                         $contents.=chr(10)."<!-- Compressed, level ".$level.", original size was ".strlen($contents)." bytes. New size is ".strlen(gzcompress($contents, $level))." bytes -->";
00225                         $size = strlen($contents);      // Must set again!
00226                 }
00227                         // By Kasper Skaarhoj, end
00228 
00229                 $size = strlen($contents);
00230                 $crc = crc32($contents);
00231                 $gzdata .= gzcompress($contents, $level);
00232                 $gzdata = substr($gzdata, 0, strlen($gzdata) - 4); // fix crc bug
00233                 $gzdata .= pack("V",$crc) . pack("V", $size);
00234 
00235                 $this->size = $size;
00236                 $this->crc = $crc;
00237                 $this->gzsize = strlen($gzdata);
00238 
00239                 if ($debug) {
00240                     return;
00241                 }
00242 
00243                 ob_end_clean();
00244                 Header('Content-Encoding: ' . $encoding);
00245                 Header('Content-Length: ' . strlen($gzdata));
00246                 Header('X-Content-Encoded-By: class.gzip_encode '.$this->_version);
00247 
00248                 echo $gzdata;
00249         }
00250 
00251 
00267         function gzip_accepted() {
00268                         // Checks, if the accepted encoding supports gzip or x-gzip.
00269                         // Furthermore a qvalue check is done. "gzip;q=0" means no gzip accepted at all.
00270                 $acceptEncoding = t3lib_div::getIndpEnv('HTTP_ACCEPT_ENCODING');
00271                 if (preg_match('/(^|,\s*)(x-)?gzip(;q=(\d(\.\d+)?))?(,|$)/i', $acceptEncoding, $match) && ($match[4] === '' || $match[4] > 0)) {
00272                         $encoding = 'gzip';
00273                 } else {
00274                         return false;
00275                 }
00276 
00277                         // Test file type. I wish I could get HTTP response headers.
00278                 $magic = substr(ob_get_contents(),0,4);
00279                 if (substr($magic,0,2) === '^_') {
00280                     // gzip data
00281                     $encoding = false;
00282                 } else if (substr($magic,0,3) === 'GIF') {
00283                     // gif images
00284                     $encoding = false;
00285                 } else if (substr($magic,0,2) === "\xFF\xD8") {
00286                     // jpeg images
00287                     $encoding = false;
00288                 } else if (substr($magic,0,4) === "\x89PNG") {
00289                     // png images
00290                     $encoding = false;
00291                 } else if (substr($magic,0,3) === 'FWS') {
00292                     // Don't gzip Shockwave Flash files. Flash on windows incorrectly
00293                     // claims it accepts gzip'd content.
00294                     $encoding = false;
00295                 } else if (substr($magic,0,2) === 'PK') {
00296                     // pk zip file
00297                     $encoding = false;
00298                 }
00299 
00300                 return $encoding;
00301         }
00302 
00316     function get_complevel() {
00317                 $uname = posix_uname();
00318                 switch ($uname['sysname']) {
00319                     case 'Linux':
00320                                 $cl = (1 - $this->linux_loadavg()) * 10;
00321                                 $level = (int)max(min(9, $cl), 0);
00322                         break;
00323                     case 'FreeBSD':
00324                                 $cl = (1 - $this->freebsd_loadavg()) * 10;
00325                                 $level = (int)max(min(9, $cl), 0);
00326                                 break;
00327                             default:
00328                                 $level = 3;
00329                         break;
00330                 }
00331                 return $level;
00332         }
00333 
00341         function linux_loadavg() {
00342                 $buffer = "0 0 0";
00343                 $f = fopen("/proc/loadavg","rb");
00344                 if (!feof($f)) {
00345                     $buffer = fgets($f, 1024);
00346                 }
00347                 fclose($f);
00348                 $load = explode(" ",$buffer);
00349                 return max((float)$load[0], (float)$load[1], (float)$load[2]);
00350         }
00351 
00362         function freebsd_loadavg() {
00363                 $buffer= `uptime`;
00364                 $load = array();
00365                 ereg("averag(es|e): ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]), ([0-9][.][0-9][0-9]*)", $buffer, $load);
00366 
00367                 return max((float)$load[2], (float)$load[3], (float)$load[4]);
00368         }
00369 }
00370 
00371 ?>

This documentation has been generated automatically from TYPO3 source code using Doxygen and is provided as is by Cast Iron Coding as a courtesy to other TYPO3 developers and users. Please consider Cast Iron Coding — a full-service web development agency in Portland, Oregon specializing in TYPO3 extension development — for all of your TYPO3 development and consulting needs!