<?php


// Include this function on your pages
function print_gzipped_page() {
    if( headers_sent() ){
        $encoding = false;
    }elseif( strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== false ){
        $encoding = 'x-gzip';
    }elseif( strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false ){
        $encoding = 'gzip';
    }else{
        $encoding = false;
    }

    if( $encoding ){
        $contents = ob_get_contents();
        ob_end_clean();
        header('Content-Encoding: '.$encoding);
		header('Content-type: text/css; charset=utf-8'); 
        print("\x1f\x8b\x08\x00\x00\x00\x00\x00");
        $size = strlen($contents);
        $contents = gzcompress($contents, 9);
        $contents = substr($contents, 0, $size);
        print($contents);
        exit();
    }else{
        ob_end_flush();
        exit();
    }
}

// At the beginning of each page call these two functions
ob_start();
ob_implicit_flush(0);

// Get the requested CSS and output it
$file = str_replace('\\','',$_GET['file']);
$file = str_replace('/','',$file);
$files = explode(',',$file);
$count = count($files);
foreach($files as $file){
	$file = trim($file);
	$file .= '.css';
	if(file_exists($file)){
		$contents = file_get_contents($file);
		if($count > 1){
			echo "\n\n/* <<<<<< FILE: ".strtoupper($file)." >>>>>> */\n\n";
		}
		// the nex code fixes the transparant PNG in IE version < 7, It adds the microsoft alpha image loader
		$useragent = $_SERVER['HTTP_USER_AGENT'];
		if (preg_match('|MSIE ([0-6]\.[0-9]{1,2})|',$useragent) && !preg_match('|MSIE ([7-9]\.[0-9]{1,2})|',$useragent)) { // is an old version of IE
			// filter normal png pictures (pictures are cropped)
			$pattern = '/(url\((.*\.png)\).*\n)/';
			$replace = '$1'."\t".'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'$2\', sizingMethod=\'crop\'); background-image:none;'."\n";
			$contents = preg_replace($pattern,$replace,$contents);
			// filter png pictures that need to be scaled (when using background repeat for example)
			$pattern = '/(\/\*SCALE\*\/.*\n.*filter:progid:DXImageTransform.Microsoft.AlphaImageLoader\(.*, sizingMethod=\').*\'/';
			$replace = '$1scale\'';
			$contents = preg_replace($pattern,$replace,$contents);
		}
		echo $contents;
	}
}


// Call this function to output everything as gzipped content.
print_gzipped_page();


?> 