<?php
// THIS is the root directory
define('ROOT', getcwd());

// make some URL's
$url = new stdClass();
	$url->root = ROOT;
	$url->base = 'http://' . $_SERVER['HTTP_HOST'] . str_replace($_SERVER['DOCUMENT_ROOT'], '', ROOT);
	$url->theme = $url->base . '/theme';

// include system files.php
include(ROOT . '/config.php');
include(ROOT . '/system/make_path.php');

// start output buffering
ob_start('output_display');

// load /folder/file
if(isset($_PATH[1], $_PATH[2]))
{
	//load the module file
	if(file_exists(ROOT . '/modules/' . $_PATH[1] . '/' . $_PATH[2] . '.php'))
		include(ROOT . '/modules/' . $_PATH[1] . '/' . $_PATH[2] . '.php');
	
	//load the template 
	if(file_exists(ROOT . '/theme/templates/' . $_PATH[1] . '/' . $_PATH[2] . '.php'))
		include(ROOT . '/theme/templates/' . $_PATH[1] . '/' . $_PATH[2] . '.php');
	else 
		include(ROOT . '/theme/templates/404.php');
}
// load /folder(/index.php)
elseif(isset($_PATH[1]) && !isset($_PATH[2]))
{
	//load the module file
	if(file_exists(ROOT . '/modules/' . $_PATH[1] . '/' . 'index.php'))
		include(ROOT . '/modules/' . $_PATH[1] . '/' . 'index.php');
	
	//load the template 
	if(file_exists(ROOT . '/theme/templates/' . $_PATH[1] . '/' . 'index.php'))
		include(ROOT . '/theme/templates/' . $_PATH[1] . '/' . 'index.php');
	else 
		include(ROOT . '/theme/templates/404.php');
}
else
{	
	//load the module file
	include(ROOT . '/modules/home.php');

	//load the template 
	include(ROOT . '/theme/templates/home.php');
}

//get the data so far
define('INNER_CONTENT', ob_get_contents());

//clear the buffer
ob_clean();

//get the frame
require_once(ROOT . '/modules/frame.php');
require_once(ROOT . '/theme/templates/frame.php');

//everything is finished lets remove some extra crap and display it
ob_end_flush();

//this will clean and output the data
function output_display($data)
{
	//remove white space
	// $data = preg_replace('/\s+/ism', ' ', $data);
	
	if(acceptsGZip())
	{
		header('Content-Encoding: gzip');
		return gzencode($data);
	}
	else	
		return $data;
}

function acceptsGZip(){
    $accept = str_replace(' ','',
        strtolower($_SERVER['HTTP_ACCEPT_ENCODING'])
    );
    $accept = explode(',',$accept);
    return in_array('gzip',$accept);
}

function fnc() {
		$args = func_get_args(); 
		$path = ROOT . '/system/functions/';
		
		if ($loc = strrpos($args[0], '/')) list($path, $func) = split('-l-', chunk_split($args[0], $loc+1, '-l-'));
		else $func = $args[0];
		
		unset($args[0]);

		if (!function_exists($func)) include_once($path . $func . '.php');
		return call_user_func_array($func, $args);
	}
?>