<?
/*--------------------------------------------------------------------------*
*-- GZipped/Cached by ScripZip (c)2006-2007( http://www.php-vision.de/ ) --*
*--------------------------------------------------------------------------*
* Orginal Script Filesize: 10913 Bytes
* GZipped Script Filesize: 3295 Bytes
* GZip Compression Level : 9 GZip Compression Rate : 30.19% of Org. File *
* Last Time File Changed : Sun, 05 Feb 2012 01:08:55 GMT *
* Last Browser Load File : Sun, 05 Feb 2012 01:08:55 GMT *
* Cached File Expires on : Sun, 12 Feb 2012 01:08:55 GMT *
*--------------------------------------------------------------------------*
*-- Download ScripZip and speed up your Page on http://www.php-vision.de/ *
*--------------------------------------------------------------------------*/
?>
<?php
/** ScribZip provides GZiped & Clean Output of Javascripts and CSS Files
* @name ScripZip
* @version 0.7.1
* @link http://www.php-vision.de/
* @copyright Michael Schwarz (M.Schwarz@php-vision.de)
* @author Michael Schwarz (M.Schwarz@php-vision.de)
* @example /.gzip.php?file=YourCSSFile.css
* @todo Script gets false Modification time from Files under Linux (PHP:filemtime)
*/
/** - Change Log ----------------------------------
*
* Version 0.1.0 (13.08.2006)
* - Std gzip Output for css Files
* Version 0.2.0 (14.08.2006)
* - Std gzip Output for js Files
* - #Bugfix: Special Chars for Filenames in mod_rewrite
* Version 0.5.0 (17.08.2006)
* - Added xml Support
* Version 0.6.0 (18.08.2006)
* - Browser Cache Support Added ( Conditional Get )
* Version 0.6.2 (20.08.2006)
* - #Bugfix: Conditional Get failed with '"' in etag ( FF 1.5 )
* Version 0.6.5 (20.08.2006)
* - #Bugfix: Conditional Get failed with ';length=X' in last_modified (IE6)
* Version 0.7.0 (29.08.2006)
* - Strip Comments and Whitespaces fo Javacsript and CSS Files
* Version 0.7.1 (30.08.2006)
* - #Bugfix: Comment Strip fails for Slash-Star Comments
*/
/** Donate / Spenden
*
* If this little Script is usefull to you, please send some bucks.
* Donate to PAYPAL: M.Schwarz@php-vision.de
*
* ATTENTION:
* If you decide to not donate some bucks while using this Script
* ... eat my shorts !!! ... and after that i need new ones, so
* send Money to PAYPAL: M.Schwarz@php-vision.de
*
* Thanks
*/
define( 'ZLIB_COMPRESS_LEVEL' , 9 ); // Compress Level 0-9 (0 is none)
define( 'FILE_CHARSET' , 'ISO-8859-1' ); // is UTF-8 better ?
define( 'USE_BROWSER_CACHE' , true ); // Use the Browser Cache funktion
define( 'CACHE_EXPIRES_TIME', 60*60*24*7 ); // Browser Cache Life Tim
define( 'HEADER_OUTPUT' , true );
define( 'PRINT_SELF' , true );
define( 'CLEAN_CODE' , true );
/**********************************************************************************\
* START of the Script *
\**********************************************************************************/
#$_GET['file'] = 'test.css'; // DEBUG ONLY
#error_reporting( E_All | E_Strict ); // DEBUG ONLY
/*----------------------------------------------------------------------------*/
if ( file_exists('../inc/settings.php') )
require_once('../inc/settings.php');
if ( ! isset( $CONFIG['page']['gzip'] ) )// For Compatibility with my Systems
$CONFIG['page']['gzip'] = ZLIB_COMPRESS_LEVEL;
/*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*/
ob_start();
ob_implicit_flush(0);
/*----------------------------------------------------------------------------*/
if ( !isset( $_GET['file']) )
if( PRINT_SELF ) ThisScriptSelfOutput();
else die(header('Status: 404 Not found'));
if ( ! isset( $_GET['file'] ) ) die(header('Status: 404 Not found'));
if ( ! is_file( $_GET['file'] ) ) die(header('Status: 404 Not found'));
if (stristr($_GET['file'],'../')) die(header('Status: 404 Not found'));
/*----------------------------------------------------------------------------*/
if( USE_BROWSER_CACHE ){
clearstatcache();
doConditionalGet( filemtime($_GET['file']) );
}
/*----------------------------------------------------------------------------*/
$code = file_get_contents( $_GET['file'] );
/*----------------------------------------------------------------------------*/
switch( $type = strrchr($_GET['file'] ,'.') ) {
case '.css': header("Content-type: text/css; charset: ".FILE_CHARSET);
break;
case '.js' : header("Content-type: text/javascript; charset: ".FILE_CHARSET);
break;
default : print("<b>Error :</b> Unsuported/Unallowed Filetype Requested");
exit;
}
/*----------------------------------------------------------------------------*/
if( CLEAN_CODE )
switch( $type = strrchr($_GET['file'] ,'.') ){
case '.css': $code = clean_css_code($code); break;
case '.js' : $code = clean_js_code( $code); break;
default : $code = $code;
}
/*----------------------------------------------------------------------------*/
header("Cache-Control: must-revalidate");
header("Expires: ".gmdate("D, d M Y H:i:s",time()+(CACHE_EXPIRES_TIME))." GMT");
if( HEADER_OUTPUT )
print OutputInfoText($_GET['file'], $code );
print $code;
/*----------------------------------------------------------------------------*/
if ( isset ($_SERVER['HTTP_ACCEPT_ENCODING']))
$accept_enc = $_SERVER['HTTP_ACCEPT_ENCODING'];
else if ( isset ($HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'] ))
$accept_enc = $HTTP_SERVER_VARS['HTTP_ACCEPT_ENCODING'];
else if ( isset ($HTTP_ACCEPT_ENCODING))
$accept_enc = $HTTP_ACCEPT_ENCODING;
else $accept_enc = '';
/*----------------------------------------------------------------------------*/
@ini_set( 'zlib.output_compression' , 'Off' );
if ( $CONFIG['page']['gzip'] )
if ( extension_loaded('zlib') )
if ( strstr( $accept_enc , 'gzip') )
{
header('Content-Encoding: gzip');
$gzip_size = ob_get_length();
$gzip_contents = ob_get_clean();
echo "\x1f\x8b\x08\x00\x00\x00\x00\x00",
substr(gzcompress($gzip_contents, $CONFIG['page']['gzip'] ), 0, - 4),
pack('V', crc32($gzip_contents)),
pack('V', $gzip_size);
}
exit();
/**********************************************************************************\
* END of the Script ****************************** Start of used Functions *
\**********************************************************************************/
/** This Functions Cleans CSS Stylesheet Code
* @author M. Schwarz
* @param String $jscode
* @return String
*/
function clean_css_code( $code )
{
$code = clean_comments ( $code );
$code = preg_replace('/\s+/',' ', $code);
$code = preg_replace('/([\ ]?)\{([\ ]?)/',"{\r\n\t", $code);
$code = preg_replace('/\;([\ ]?)/',";\r\n\t", $code);
$code = preg_replace("/([\ ]?)\:([\ ]?)/",":", $code);
$code = preg_replace("/([\t]?)([\ ]?)\}([\ ]?)/","}\r\n", $code);
return $code;
}
/** This Functions Cleans Javascript Code
* @author M. Schwarz
* @param String $jscode
* @return String
*/
function clean_js_code( $jscode )
{
$code = clean_comments ( $jscode );
$code = preg_replace('#//(.*)(\n|\r|\r\n)#','', $code);
$code = preg_replace('/([\ ]+)/',' ', $code);
$code = preg_replace('/([\t]+)/',' ', $code);
$code = preg_replace('#([\n\r]{1,2}?)( ?)\{( ?)([\n\r]{1,2}?)#',"{", $code);
$code = preg_replace('/\;( ?)([\n\r]{1,2}?)/',";", $code);
return $code;
}
/** This Functions Cleans Standard Block Comments( /* )
* @author M. Schwarz
* @param String $code
* @return String
*/
function clean_comments ( $code )
{ // $code = preg_replace('#(/\*)(.*)(\*/)#','', $code);
do{ $start = strpos($code ,'/*');
$ende = strpos($code ,'*/', $start);
$comm = substr( $code, $start+2,$ende-$start-2 );
$code = str_replace('/*'.$comm.'*/','',$code);
}while( $ende );
return $code;
}
/**********************************************************************************\
\**********************************************************************************/
function OutputInfoText($file, $code ='' )
{
global $CONFIG;
$cl = $CONFIG['page']['gzip'];
if( ! $code )
$fc = file_get_contents($file);
else $fc = $code;
$fs = filesize( $file );
$cs = strlen( gzcompress( $fc , $cl )) -11;
$cr = number_format( ($cs/$fs)*100 , 2);
$cd = gmdate( "D, d M Y H:i:s" , fileatime( $file ));
$ld = gmdate( "D, d M Y H:i:s" , time());
$ed = gmdate( "D, d M Y H:i:s" , time()+CACHE_EXPIRES_TIME);
$info = '*--------------------------------------------------------------------------*'."\r\n";
$info.= ' *-- GZipped/Cached by ScripZip (c)2006-2007( http://www.php-vision.de/ ) --*'."\r\n";
$info.= ' *--------------------------------------------------------------------------*'."\r\n";
$info.= ' * Orginal Script Filesize: '.$fs.' Bytes '."\r\n";
$info.= ' * GZipped Script Filesize: '.$cs.' Bytes '."\r\n";
$info.= ' * GZip Compression Level : '.$cl;
$info.= ' GZip Compression Rate : '.$cr.'% of Org. File *'."\r\n";
$info.= ' * Last Time File Changed : '.$cd.' GMT *'."\r\n";
$info.= ' * Last Browser Load File : '.$ld.' GMT *'."\r\n";
$info.= ' * Cached File Expires on : '.$ed.' GMT *'."\r\n";
$info.= ' *--------------------------------------------------------------------------*'."\r\n";
$info.= ' *-- Download ScripZip and speed up your Page on http://www.php-vision.de/ *'."\r\n";
$info.= ' *--------------------------------------------------------------------------*';
switch( $type = strrchr($file,'.') ){
case '.css' :
case '.js' : return("/" .$info. "/\r\n\r\n"); break;
case '.php' : return(highlight_string("<?\r\n/" .$info. "/\r\n?>\r\n",true));
default : return('');
}
}
/**********************************************************************************\
* Diese Funktion gibt den Inhalt dieser Datei im Browser aus *
\**********************************************************************************/
function ThisScriptSelfOutput ()
{
global $_SERVER;
header("Cache-Control: must-revalidate");
header("Expires: ".gmdate("D, d M Y H:i:s",time()+(CACHE_EXPIRES_TIME))." GMT");
header("Content-type: text/html; charset:".FILE_CHARSET);
if(USE_BROWSER_CACHE) doConditionalGet( filemtime( __FILE__ ) );
echo '<body bgcolor="#FFFFFF" text="#000000">';
echo OutputInfoText( __FILE__ );
die(highlight_string(file_get_contents( __FILE__ ),true));
}
/** A PHP implementation of conditional get
* @link http://fishbowl.pastiche.org/archives/001132.html
* @copyright Public Domain License
* @author Urheber + Jan Piotrowski + M. Schwarz
* @param Int $timestamp
*/
function doConditionalGet($timestamp)
{
$last_modified = gmdate("D, d M Y H:i:s", $timestamp).' GMT';
$etag = md5($last_modified);
header("Last-Modified: ".$last_modified);
header('ETag: "'.$etag."\"");
$if_modified_since = isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) ?
stripslashes($_SERVER['HTTP_IF_MODIFIED_SINCE']) :
false;
$if_none_match = isset($_SERVER['HTTP_IF_NONE_MATCH']) ?
trim(stripslashes($_SERVER['HTTP_IF_NONE_MATCH']),'"') :
false;
if (!$if_modified_since && !$if_none_match)
return;
if ($if_none_match && $if_none_match != $etag)
return;
if ($if_modified_since && !stristr($if_modified_since,$last_modified))
return;
die( header('HTTP/1.0 304 Not Modified') );
}
?>