<?php
if (!defined('WEBROOT')) { define('WEBROOT', $_SERVER['DOCUMENT_ROOT']); }

class ImpresarioCSS
{
	/**
	 * Prints out the necessary css header information and all the requested
	 * css files
	 *
	 */
	function getCSS() 
	{
		header("Content-type: text/css");
		echo $this->compressCSS($this->_getContentsOfAllRequestedFiles());
	}
	
	function compressCSS($css)
	{
		// Remove comments
		$css = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $css);
		// Compress all space into single spaces
		$css	= preg_replace('/\s+/', ' ', $css);
		return $css;
	}
	
	/**
	 * Returns a string of the contents of all requested files, including the files in the
	 * requested set, as well as the additional files requested individually
	 *
	 * @return string
	 */
	function _getContentsOfAllRequestedFiles()
	{
		$setName	= $this->_getFileSetName();
		$setFiles	= $this->_getFileNamesInSet($setName);
		$extraFiles	= $this->_getExtraFilesToInclude();
		$allFiles	= array_merge($extraFiles, $setFiles);
		
		return $this->_concatenateContentsOfFilesWithNames($allFiles);
	}
	
	/**
	 * Returns the name of the requested file set, or an empty string if none 
	 * was requested
	 *
	 * @return string
	 */
	function _getFileSetName()
	{
		if (key_exists("set", $_GET))
		{
			return $_GET["set"];
		}
		else 
		{
			return "";
		}
	}
	
	/**
	 * Returns an array of the names of the files in the requested file set,
	 * which can be one of "default", "contentStyles", or "baseStyles" (where "default"
	 * is both the baseStyles and contentStyles together).
	 *
	 * @param string $setName
	 * @return array
	 */
	function _getFileNamesInSet($setName)
	{
		switch ($setName) {
			case "default":
				return array(
					"impresarioDefault.css",
					"impresarioContentStyles.css",
					"impresarioLightbox.css",
					"/css/default.css",
					"/css/contentStyles.css",
					"/css/lightbox.css",
				);
				break;
			case "contentStyles":
				return array(
					"impresarioContentStyles.css",
					"/css/contentStyles.css",
				);
				break;
				
			case "baseStyles":
				return array(
					"impresarioDefault.css",
					"/css/default.css",
				);
				break;
			case "print":
				return array(
					"impresarioPrint.css",
					"/css/print.css",
				);
				break;
			case "lightbox2":
				return array(
					"impresarioLightbox.css",
					"/css/lightbox.css",
				);
				break;
			case "simpleTest":
				return array(
					"impresarioSimpleTest.css",
					"/css/simpleTest.css",
				);
				break;
			case "admin":
				return array(
					"admin/changelists.css",
					"admin/forms.css",
					"admin/widgets.css",
				);
				break;
			default:
				return array();
				break;
		}
	}
	
	/**
	 * Returns an array of the names of the extra files to include, or an 
	 * empty array if none were requested
	 *
	 * @return array
	 */
	function _getExtraFilesToInclude()
	{
		$files	= array();
		if (key_exists("files", $_GET))
		{
			$files	= explode(",", $_GET["files"]);
			for($i=0; $i<count($files); $i++)
			{
				$files[$i]	= trim(str_replace("..", "", $files[$i]));
				if (substr($files[$i],strlen($files[$i])-4) != ".css")
				{
					unset($files[$i]);
				}
			}
		}
		return $files;
	}
	
	/**
	 * Returns a string of the contents of the files whose names are in $fileNames
	 * Searching both the current path and also the path defined in WEBROOT for the file names
	 *
	 * @param array $fileNames
	 * @return string
	 */
	function _concatenateContentsOfFilesWithNames($fileNames = array())
	{
		$contents	= "";
		foreach ($fileNames as $fileName)
		{
			$include	= false;
			if (file_exists($fileName))
			{
				$include		= true;
				$fileToInclude	= $fileName;
			} else if (file_exists(WEBROOT . $fileName))
			{
				$include		= true;
				$fileToInclude	= WEBROOT . $fileName;
			}
			
			if ($include == true)
			{
				$contents	.=	"/************************************\n".
								" **\t Begin: ". basename($fileToInclude) ."\n".
								" ************************************/\n\n";
				$contents	.= file_get_contents($fileToInclude) ."\n";
				$contents	.=	"/************************************\n".
								" **\t End: ". basename($fileToInclude) ."\n".
								" ************************************/\n\n";
			}
		}
		return $contents;	
	}
}

$css	= new ImpresarioCSS();
$css->getCSS();

?>