<?
// Include functions libarary.
include_once 'functions.php';
include_once 'define.php';
include_once COMMON_PATH.'/sendmail.php';

// Connect to DB
connectDb('mysql.ezd3.com','test','test','migrainehelper');
//$db_link = connectDb(DB_HOST,DB_USER,DB_PWD,DB_NAME);

// Get page name
$page = parseUrl($_SERVER['REQUEST_URI']);

// Default Template
$template = 'template/article.tpl';

// Get content from DB
//$query = "SELECT * FROM content WHERE name = '$page' AND stat";
$query = "SELECT * FROM content WHERE name = '$page'";

// If page does not exist, return 404 header
if (!($result = @mysql_query($query)) || !(mysql_num_rows($result)) || !($record = @mysql_fetch_assoc($result))) {
	header("HTTP/1.0 404 Not Found");
	$html = return_template('','template/404.tpl');
} else if( $record['stat'] == 0 ) {
    //header('Location: '.$row['redirect_url']);
    // For 302 redirection
    //header('Location: '.$row['redirect_url']);
    // For 301 redirection
    header('Location: '.$record['redirect_url'],true, 301);
    exit;
}
else {
	//$record = mysql_fetch_assoc($result);

	// Store data into hash array 'content'
	$content = array(
		'id' 			=>	$record['id'],
		'p_id'			=>	$record['p_id'],
		'title'			=>	$record['title'],
		'nav_title'		=>	$record['nav_title'],
		'keywords'		=>	$record['keywords'],
		'description'	=>	$record['description'],
	);
	
	// Main Category ID
	if ($content['p_id'] == -1)
		$main_id = 0;
	else
		$main_id = ($content['p_id'] == 0) ? $content['id'] : mainID($content['p_id']);

	$content['topnav'] = top_nav($main_id);

	// Left Navigation
	$content['navigation'] = left_nav($main_id);

	// 728x15 Google Ad
	$content['728x15'] = file_get_contents('google/728x15.txt');

	// For non-database content
	switch ($page) {
		case 'index.shtml':
			$template = 'template/home.tpl';
			$content['content'] = $record['content'];
			break;
		case 'contact.shtml':
			include_once 'contact.php';
			$tmp['notice'] = $notice;
			$content['content'] = return_template($tmp,'template/contact.tpl');
			break;
		case 'links.shtml':
			include_once 'links.php';
			break;
		default:
			$content['content'] = ($content['p_id'] == -1) ? $record['content'] : wrap($record['content']);
			break;
	}


	// merge data and template and return to $html for output
	$html = return_template($content,$template);
}
// Output HTML page
echo $html;

?>