<?php
global $is_js;
$dir = __DIR__;
$files = isset($_GET['f']) ? $_GET['f'] : '';
$is_js = isset($_GET['js']) ? $_GET['js'] : FALSE;
$last_modified_date = isset($_GET['cache_bust']) ? $_GET['cache_bust'] : FALSE;

$md5 = md5($files.$last_modified_date);
$files = explode('+', $files);

//echo $dir;
$dir = str_replace("/resource/libraries", "", $dir);
//var_dump($last_modified_date);
function compress($buffer) {
	global $is_js;
	
	/* remove comments */
	$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);

	if($is_js){
		return $buffer;
	}
	/* remove tabs, spaces, newlines, etc. */
	$buffer = str_replace(array("\r\n", "\r", "\n", "\t", '  ', '    ', '    '), '', $buffer);
	return $buffer;
}
if($is_js){
	header("Content-type: application/x-javascript");
} else {
	header('Content-type: text/css');
}
header("Cache-Control: max-age=10368000 public");
//header("Pragma: private");
header("Expires: " . date(DATE_RFC822,strtotime(" 365 day")));
$etag = '"'.$md5.'"';
if( $last_modified_date !=  FALSE)
{
	//header("Last-Modified: ".gmdate("D, d M Y H:i:s", $last_modified_date)." GMT"); 
	header('Etag: '.$etag); 

	//*
	// always send headers
	// exit if not modified
	if (@trim($_SERVER['HTTP_IF_NONE_MATCH']) == $etag) { 
		header("HTTP/1.1 304 Not Modified"); 
		exit; 
	}
	//*/
}
//ob_start("compress");
ob_start();
foreach($files as $file){
	$file_path = $dir.'/'.$file.($is_js ? '.js' : '.css');
	//pr($file_path);
	if(file_exists($file_path)){
		//echo '/*'.$file_path.'*/';
		include($file_path);
		//print_r($file_path); exit;
	}
}
ob_end_flush();