<?php 

# LemurCMS by Alec Gorge
#
#     This file contains the PHP functions used throught the CMS.
#
# updated: Jan. 12, 2009

//Initiate classes
$page = new init;
$theme = new themeManager($page->page_info['id']);
$smarty = new Smarty();
if($page->getSetting('Clean Urls') == "Enabled") {
	$clean = new CleanURL;
}	

//Use clean URLS
if($page->getSetting('Clean Urls') == "Enabled") {
	$clean->parseURL(); 
	$clean->setParts('title', 'page'); 
}

//Setup Smarty Templating System
$smarty->left_delimiter = '[--';
$smarty->right_delimiter = '--]';
$smarty->template_dir = "";
$smarty->compile_dir = BASE_URL.'temp/';
$smarty->cache_dir = BASE_URL.'temp/';
$smarty->config_dir = BASE_URL.'includes/smarty/configs';


//Get the requested page id
global $pageid;
if($_GET['page'] && is_numeric($_GET['page'])) {
	$page->page_info['id'] = $_GET['page'];
	$GLOBALS['pageid'] = $_GET['page'];
}
elseif ($_GET['page'] == "login") {
	$page->pageContent($adminLoginForm);
	$page->page_info['content']['page_title'] = "Login";
}
else {
	$page->page_info['id'] = $page->page_info['settings']['Home Page Id']['value'];
	$GLOBALS['pageid'] = $page->page_info['settings']['Home Page Id']['value'];
}

function smarty_navigation($params) {
	if(empty($params['ulid'])) {
		$a = "ul-navigation";
	} else {
		$a = $params['ulid'];
	}
	if(empty($params['subulclass'])) {
		$b = "ul-sub-navigation";
	} else {
		$b = $params['subulclass'];
	}
	if(empty($params['selected'])) {
		$c = "selected";
	} else {
		$c = $params['selected'];
	}
	if(empty($params['link'])) {
		$d = true;
	} else {
		$d = $params['format'];
	}
	if(empty($params['one_row'])) {
		$e = false;
	} else {
		$e = $params['format'];
	}
	if(empty($params['array'])) {
		$f = false;
	} else {
		$f = $params['array'];
	}
	$theme = new themeManager($GLOBALS['pageid']);
	$return =&  $theme->navigation($a,$b,$c,$d,$e,$f);
	return $return;
}
function smarty_static_content($params) {
	if(($params['name'])) {
		$a = $params['name'];
	}
	if(($params['default'])) {
		$b = $params['default'];
	}
	$theme = new themeManager();
	$return =&  $theme->static_content($a,$b);
	return $return;
}


if($_POST['login']) {
	session_start();
	$_SESSION['username'] = sanitize_sql_string($_POST['username']);
	$_SESSION['password'] = sha1(sanitize_sql_string($_POST['password']));
	$page->validate("", BASE_URL."admin.php");
	if($page->validate() == false) {
		$_SESSION = array();
		session_destroy();
		$page->pageContent($adminLoginForm, 0);	
		$page->pageContent("<style type='text/css'>@import '".BASE_URL."includes/admin/css/messages.css';</style>".$page->message('error', 'Login Failed', 'Invalid user name or password. Please try again.'), -1);	
	}	
}


//Set the page content and title, based on the id, in the $page->page_info['content'] array
$page->displayPage($page->page_info['id']);


//Prepare the settings arrays to be Smarty safe
$page->prepareSettingArraysSmarty();

//Assign Smarty Templating Sytem values
$smarty->assign('theme_url', THEME_URL.'themes/'.trim($page->page_info['settings']['Theme']['value']).'/');
$smarty->assign('base_url', THEME_URL);
$smarty->assign('pageid', $pageid);


	//Assign settings arrays
	$smarty->assign('settings', $page_settings);
	$smarty->assign('page_info', $page->page_info);
	$smarty->assign('system', $page_settings_system);
	
	//Assign content array
	$smarty->assign('content', $page->page_info['content']);

//Assign page loading time
$smarty->assign('page_generation_time', round(microtime(), 4) - PAGE_LOAD_START + 0.0025);
$smarty->register_function('navigation', 'smarty_navigation');
$smarty->register_function('staticContent', 'smarty_static_content');

//Pre-release version raw print out of global array
if($_GET['debug']) {
	$Obj1=new CuteArray($page->page_info);
	echo $Obj1->makeUList(1,1);
}
	
//Display page with Smarty
$smarty->display(BASE_URL.'themes/'.trim($page->page_info['settings']['Theme']['value']).'/index.tpl');


?>