<?php

essential_header();

// Hide from retail sites
//if (in_array($site_info['alias'], array('equipment2u.com','fineentertaining.com'))) {
//	header('Location: /');
//	exit;
//}

include_once('StaticPages/staticpages.class.php');

$pages = new StaticPages;

$path = $_SERVER['PATH_INFO'];
$path_pieces = explode("/", $path);

// Grab the last non-empty piece of the path
foreach($path_pieces as $piece) {
	if (!empty($piece)) {
		$path_query = $piece;
	}
}


// Set the page content
if (is_numeric($path_query)) {
	$pages->set_page($path_query);
} else {
	$query = "SELECT id FROM static_pages WHERE page_name = '$path_query'; ";
	$record = $db->query_fetch_row($query);
	if (is_numeric($record['id'])) {
		$pages->set_page($record['id']);
	}
}

// 404
if (empty($pages->page_data)) {
	header("HTTP/1.0 404 Not Found");
	page_header();
	print "<h2>Page Not Found!</h2>\n";
	print "<p class=\"alertMessage\">The page you requested could not be found on our Web site.</p>\n";
	page_footer();
	exit;
}

// Hide if draft
$page_status = $pages->field('page_status');
if ($page_status == 'draft') {
	if (!$auth->has_perm('admin')) {
		header("HTTP/1.0 404 Not Found");
		page_header();
		print "<h2>Page Not Found!</h2>\n";
		print "<p class=\"alertMessage\">The page you requested is not available on our Web site.</p>\n";
		page_footer();
		exit;
	}
	$is_draft = true;
}

// Check page permissions
$page_perms = unserialize($pages->field('page_perms'));
if (!empty($page_perms)) {

	$_SESSION['auth_url'] = '/page/' . $pages->field('page_name');

	// Missing Cart Permissions
	if ( in_array('cart', $page_perms) && !$auth->has_perm('cart') ) {
		header('Location: /login.php',TRUE,307);
		exit;
	} 

	// Missing Dealer Permissions
	if ( in_array('dealer', $page_perms) && !$auth->has_perm('dealer') ) {
		header('Location: /login.php',TRUE,307);
		exit;
	} 

	// Missing RHED Permissions
	if ( in_array('rhed', $page_perms) && !$auth->has_perm('rhed') ) {
		$page_title = 'Access Denied';
		page_header();
		print '<h2>You cannot access this page</h2><p class="alertMessage">Certain sections of our site are only available to our customers located within Tennessee, Kentucky and Georgia and are Registered Honda Engine Dealers (RHED). Please <a href="/contact.php">contact customer service</a> for assistance.</p>';
		page_footer();
		exit;
	} 
	
	// Missing GATNKY Permissons
	if ( in_array('gatnky', $page_perms) && !$auth->has_perm('gatnky') ) {
		$page_title = 'Access Denied';
		page_header();
		print '<h2>You cannot access this page</h2><p class="alertMessage">Certain sections of our site are only available to our customers located within Tennessee, Kentucky and Georgia. Please <a href="/contact.php">contact customer service</a> for assistance.</p>';
		page_footer();
		exit;
	} 

}

$site_info['navigation'] = $pages->get_navigation_breadcrumb();

$page_title = $pages->field('page_title');
$page_navigation = $pages->field('page_navigation');

page_header();

// Draft
if ($is_draft == true) {
	print '<p class="systemMessage">This page is in "draft" status and is not available on the site.</p>';
}

// Navigation Block
if (!empty($page_navigation)) {
	print '<div id="pageNavigation" style="text-align:right;">' . $page_navigation . '</div>' . "\n";
}

print '<h2>' . iso_htmlentities($page_title) . '</h2>' . "\n";
print $pages->field('page_content');

print '<!--' . $pages->field('page_perms') . '-->';

if ($auth->has_perm('admin')) {
	print "<div class=\"box\" style=\"text-align:right;clear:both;\"><p><strong><a href=\"/admin/static_pages_admin.php?action=update&amp;id=" . $pages->page_id . "\">Edit This Page</a></strong></p></div>";
}
page_footer();

essential_footer();