<?php
	/***************************\
	| HTML lehe pakkimine PHPga |
	| Zone Media OÜ klientidele |
	|   https://data.zone.ee    |
	|      (c) 1999-2002        |
	\***************************/
	if (!headers_sent () && !defined ('_gzippedpage_debug'))
	{
		// '_gzippedpage_debug' konstandi väärtuse muutmisel true-ks,
		// väljastakse käesoleva lehe lõppu antud lehe suurus pakitult ja pakkimata.
		define ('_gzippedpage_debug', false);

	
		if (preg_match ("/((?:x-)?gzip|deflate)/i", $_SERVER['HTTP_ACCEPT_ENCODING'], $_encoding_type))
		{
			if (constant ('_gzippedpage_debug') == true)
			{
				function _GZippedPage_Debug (& $data)
				{
					return '<p>Pakkimata lehe suurus: '.
						strlen($data).
						'<br>Pakitud lehe suurus: '.
						strlen (_GZippedPage_Callback ($data, false)).
						'</p>';
				}
			}
	
			if (strtolower (substr ($_encoding_type[1], -4)) == 'gzip')
			{	
				function _GZippedPage_Callback ($data, $_allow_debug = true)
				{
					$d = $data;
					
					if ($_allow_debug && constant ('_gzippedpage_debug') == true)
						$d .= _GZippedPage_Debug ($d);

					$_packed_data	= gzcompress($d, 5);
					$_retval	= "\x1f\x8b\x08\x00\x00\x00\x00\x00".
							  substr($_packed_data, 0, strlen($_packed_data) - 4).
							  pack('V', crc32($d)).
							  pack('V', strlen($d));
	
					return $_retval;
				}
			}
			else
			{
				function _GZippedPage_Callback ($data, $_allow_debug = true)
				{
					$d = $data;
					
					if ($_allow_debug && constant ('_gzippedpage_debug') == true)
						$d .= _GZippedPage_Debug ($d);

					$_retval = gzdeflate ($d, 5);
					
					return $_retval;
				}
			}
	
			header ("Content-Encoding: ".$_encoding_type[1]);
	
			ob_implicit_flush (0);
			ob_start ("_GZippedPage_Callback");
		}
	}
?>