Darkbyte Library

Nonstandard Web Browser

It appears that your browser does not comply with the W3C web standards which define how web pages are encoded, transmitted, and rendered. This site would look much better in a standards-compliant web browser, but its content is accessible to any browser or Internet device.

Gzip Compress Class :: Gzip & set correct Mime type for XHTML/HTML pages.

Download

License : GPL

Description :
A simple PHP Class to Gzip Encode your webpages and to set the mime type to application/xhtml+xml if it's supported.

Source Code :


<?php
/***************************************************************************
 * class.gzipcontent.php
 * ---------------------
 * Author: Alan Doyle (alan.doyle@darkbyte.com)
 * Copyright: (c) 2000 - 2005 Darkbyte Software
 * Release Version: 1.2
 * Date Started: 1st October 2002
 *
 * A simple PHP Class to Gzip Encode your webpages and to set the mime type to
 * application/xhtml+xml if it's supported.
 *
 ***************************************************************************
 *
 *   class.gzipcontent.php is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU General Public License as
 *   published by the Free Software Foundation; either version 2 of the
 *   License, or (at your option) any later version.
 *
 *   class.gzipcontent.php is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with class.gzipcontent.php; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 **************************************************************************/
 
class gzipcontent
{
	var $name	= "Darkbyte Software Gzip Contents";
	var $version = "1.2";
	var $url	 = "http://darkbyte.com/library.php/PHP/gzipcontents";
 
	var $size;     // size of the uncompressed content
	var $compsize; // size of the compressed content
	var $level;    // Compression level
	var $encoding; // Encoding type
	var $crc;      // crc of the output
	var $mime;     // mime type to use for encoding
	var $charset;  // Output charset
	var $override; // Used to override Mime detection
 
	/*
	 * gzipcontent constructor - setup gzip encoding variables and outputs the
	 *                           corresponding header.
	 *
	 * Note: The arguments are optionial.
	 *
	 * You can specify one of the following for the first argument:
	 *	0:	No compression ... useful for debugging
	 *	1:	Min compression
	 *	...
	 *	9:	Max compression
	 *
	 * You can specify any character set using the second argument
	 *  Default is iso-8859-1
	 *
	 * The third argument is used to override Mime detection to use "text/html"
	 *
	 */
	function gzipcontent($level = 3, $charset="iso-8859-1", $override = false )
	{
		if (!function_exists('gzcompress'))
		{
			trigger_error('gzcompress not found, zlib needs to be installed for gzip_content', E_USER_WARNING);
			return;
		}
		if (!function_exists('crc32'))
		{
			trigger_error('crc32() not found, PHP >= 4.0.1 needed for gzip_content', E_USER_WARNING);
			return;
		}
 
		if (headers_sent())
			return;
 
		if (connection_status() !== 0)
			return;
 
		$this->charset  = $charset;
		$this->level	= $level;
		$this->override = $override;
		$this->mime	 = $this->getmime();
		$this->encoding = $this->accepted();
	}
 
	function debuginfo()
	{
		print "<!-- Charset  : " . $this->charset . " -->\n";
		print "<!-- Level	: " . $this->level . " -->\n";
		print "<!-- Override : " . ($this->override ? "Enabled" : "Disabled") . " -->\n";
		print "<!-- Mime	 : " . $this->mime . " -->\n";
		print "<!-- Encoding : " . $this->encoding . " -->\n";
	}
 
	function compress( $contents )
	{
		$encoded = $this->name . " v" . $this->version . " [" . $this->url . "]";
		$skip = false;
 
		if (!is_string($contents))
			$contents = ob_get_contents();
 
		if ($contents === false)
			return;
 
		if($this->mime === "text/html")
			$contents = ereg_replace(" />", ">", $contents);
 
		$contents = ereg_replace("</head>", "</head>\n<!-- Compressed with " . $encoded . " -->", $contents);
 
		if ($this->level === 0 )
			$skip = true;
 
		if ($this->encoding === false )
			$skip = true;
 
		if( $skip === false )
		{
			// OK Let's compress this file
			$gzdata = "\x1f\x8b\x08\x00\x00\x00\x00\x00"; // gzip header
			$size = strlen($contents);
			$crc = crc32($contents);
			$gzdata .= gzcompress($contents, $this->level);
			$gzdata = substr($gzdata, 0, strlen($gzdata) - 4);
			$gzdata .= pack("V",$crc) . pack("V", $size);
 
			$this->size   = $size;
			$this->crc	= $crc;
			$this->compsize = strlen($gzdata);
 
			ob_end_clean();
 
			Header('Content-Type: ' . $this->mime . ';charset=' . $this->charset);
			Header('Content-Encoding: ' . $this->encoding);
			Header('Vary: Accept-Encoding');
			Header('Content-Length: ' . strlen($gzdata));
			Header('X-Content-Encoded-By: ' . $encoded );
			echo $gzdata;
		}
		else
		{
			ob_end_clean();
			// No compression, simply send headers and away we go
			Header('Content-Type: ' . $this->mime . ';charset=' . $this->charset);
			echo $contents;
		}
 
	}
 
	function getmime()
	{
		$mime	= "text/html";
		$charset = "iso-8859-1";
 
		if(stristr($_SERVER["HTTP_ACCEPT"],"application/xhtml+xml"))
		{
			if(preg_match("/application\/xhtml\+xml;q=0(\.[1-9]+)/i", $_SERVER["HTTP_ACCEPT"], $matches))
			{
				$xhtml_q = $matches[1];
				if(preg_match("/text\/html;q=0(\.[1-9]+)/i", $_SERVER["HTTP_ACCEPT"], $matches))
				{
					$html_q = $matches[1];
					if($xhtml_q >= $html_q)
						$mime = "application/xhtml+xml";
				}
			}
			else
				$mime = "application/xhtml+xml";
		}
 
		// special check for the W3C_Validator
		if (stristr($_SERVER["HTTP_USER_AGENT"],"W3C_Validator"))
			$mime = "application/xhtml+xml";
 
		// Override the about and force "text/html"
		if( $this->override === true )
			$mime = "text/html";
 
		return $mime;
	}
 
	function accepted()
	{
		if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'gzip') === false)
			return false;
		if (strpos($_SERVER["HTTP_ACCEPT_ENCODING"], 'x-gzip') === false)
			$encoding = 'gzip';
		else
			$encoding = 'x-gzip';
		// Test file type.
		$magic = substr(ob_get_contents(),0,4);
		if (substr($magic,0,2) === '^_')
			$encoding = false;   // gzip data
		else if (substr($magic,0,2) === "\xFF\xD8")
			$encoding = false;   // jpeg images
		else if (substr($magic,0,2) === 'PK')
			$encoding = false;   // pkzip file
		else if (substr($magic,0,3) === 'GIF')
			$encoding = false;   // gif images
		else if (substr($magic,0,3) === 'FWS')
			$encoding = false;   // Shockwave Flash
		else if (substr($magic,0,4) === "\x89PNG")
			$encoding = false;   // png images
 
		return $encoding;
	}
}
?>
Copyright © 2000-2008 Darkbyte Software, All Rights Reserved. 
Please direct your questions or comments to

CCBot/1.0 (+http://www.commoncrawl.org/bot.html) [38.103.63.57 ( )]