<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 7/19/2012	ELL
// Complete retrofit from older design.
// 
// 

	include ("check_session.php");
		
	$page_name = $_REQUEST['page_name'];
	$special = $_REQUEST['special'];	//Flag to tell if editing special page (header, footer, side panel...).
	$mode = $_REQUEST['mode'];
	$blog_id = $_REQUEST['blog_id'];
		
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_cache WHERE id = 1";	
	$result = $bwb_dbh->query($query);
	$row = $result->fetch(PDO::FETCH_ASSOC);
	$sValue = $row["tmp_page_data"];
	
	if ($mode == "blog")
	{
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data WHERE page_name = '" . $page_name . "'";	
		$result = $bwb_dbh->query($query);
		$row = $result->fetch(PDO::FETCH_ASSOC);
		$sValue .= $row["page_content"];
	}
	
	if (BWB_BASE_DIR == '')
		$userfiles_path = '';
	else
		$userfiles_path = "/" . BWB_BASE_DIR;
		
	if (BWB_WEBPAGE_DIR == '')
		$website_path = $userfiles_path;
	else
		$website_path = $userfiles_path . "/" . BWB_WEBPAGE_DIR;
		
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Preview Page | Breeze Website Builder</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon">
<?php
	$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "style_sheets ORDER BY sort_order";
	$result = $bwb_dbh->query($query);
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$name = $row["name"];
		echo "<link rel=\"stylesheet\" href=\".." . $userfiles_path . "/" . BWB_CSS_DIR . "/" . $name . ".css\" type=\"text/css\">\n";
	}
?>
<style type="text/css">
	#preview_close_button	{
		display:block;
		position:absolute;
		right:50px;
		bottom:50px;
	}
</style>
</head>

<body>

<?php

	echo "<b>" . EDITOR_PAGE_NAME . ": $page_name</b><br>";
	
	// If head content, then get index page.
	if ($special == 2)
	{
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data";
		$result = $bwb_dbh->query($query);
		$page_found = false;
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$host_page_name = $row["page_name"];	
			/////////////////////////////////////////
			//Test to see if file already exists.
			$document_root = $_SERVER['DOCUMENT_ROOT'];
			$file_name = $document_root . $website_path . "/" . $host_page_name . ".php";
			if (!file_exists($file_name))
				continue;	// Somehow file not found.  Just go find another one.
				
			// Read file.
			$fh = fopen($file_name, 'r') or die("Can't read $file_name: $php_errormsg");
			$page_content = fread($fh, filesize($file_name));
			fclose($fh) or die($php_errormsg);
		
			$search_str = "head_name='$page_name'";
			
			$pos = strpos($page_content, $search_str, 1);
			if ($start_pos !== false)
			{
				$page_found = true;
				break;	// Found this border area in this file.  Use this file.
			}
		}
		if (!$page_found)
			echo "Oops!  This head is not used is any pages.";
	}
	// If border area, find a host page to use for display.
	elseif ($special == 1)
	{
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data";
		$result = $bwb_dbh->query($query);
		$page_found = false;
		while($row = $result->fetch(PDO::FETCH_ASSOC))
		{
			$host_page_name = $row["page_name"];	
			/////////////////////////////////////////
			//Test to see if file already exists.
			$document_root = $_SERVER['DOCUMENT_ROOT'];
			$file_name = $document_root . $website_path . "/" . $host_page_name . ".php";
			if (!file_exists($file_name))
				continue;	// Somehow file not found.  Just go find another one.
				
			// Read file.
			$fh = fopen($file_name, 'r') or die("Can't read $file_name: $php_errormsg");
			$page_content = fread($fh, filesize($file_name));
			fclose($fh) or die($php_errormsg);
		
			$search_str = "page_name='$page_name'";
			
			$pos = strpos($page_content, $search_str, 1);
			if ($start_pos !== false)
			{
				$page_found = true;
				break;	// Found this border area in this file.  Use this file.
			}
		}
		if (!$page_found)
			echo "Oops!  This border area is not used is any pages.  We need to display content of just this border area.";
	}
	// If normal page content, then go get that page.
	elseif (($special != 1) && ($special != 2))	// Page content
	{
		//Test to see if file already exists.
		$document_root = $_SERVER['DOCUMENT_ROOT'];
		$file_name = $document_root . $website_path . "/" . $page_name . ".php";
		if (!file_exists($file_name))
		{
			echo "Error: $file_name does not exist.  Click <a href=\"manage_special.php\" title=\"here\">here</a> to return to the area assignment page.";
			exit;
		}
		
		// Read file.
		$fh = fopen($file_name, 'r') or die("Can't read $file_name: $php_errormsg");
		$page_content = fread($fh, filesize($file_name));
		fclose($fh) or die($php_errormsg);
	}
	
	// If special, than swap sValue to correct location.
	if ($special == 1)
	{
		// First, get rid of DB call for this area as we arn't going to use it:
		if (strpos($page_name, "header") !== false)
			$tmp = "header";
		elseif (strpos($page_name, "top") !== false)
			$tmp = "top";
		elseif (strpos($page_name, "bottom") !== false)
			$tmp = "bottom";
		elseif (strpos($page_name, "left") !== false)
			$tmp = "left";
		elseif (strpos($page_name, "right") !== false)
			$tmp = "right";
		elseif (strpos($page_name, "footer") !== false)
			$tmp = "footer";
			
		$search = "\$$tmp = \$row[\"page_content\"];";
		$page_content = str_replace($search, "", $page_content);
		$search = "\$$tmp = stripslashes(\$$tmp);";
		$page_content = str_replace($search, "", $page_content);
		$search = "\$$tmp= stripslashes(\$$tmp);";
		$page_content = str_replace($search, "", $page_content);
		
		// Then pop border content where it was going to go.
		$replace = "\"" . addslashes($sValue) . "\"";
		
		if ($tmp == "header")
		{
			$search = "if (isset($header) && ($header != \"\"))
{
	echo \"<header>\n<div id='header' class='header'>\n\";
	echo $header;
	$border_area = \"header\"; include ($menu_script_path);
	if ($show_login_links == 2)
		include_once($login_links_path);
	echo \"\n</div>\n</header>\n\";
}";
		}
		elseif ($tmp == "footer")
		{
			$search ="if (isset($footer) && ($footer != \"\"))
{
	echo \"<footer>\n<div id='footer' class='footer'>\n\";
	echo $footer;
	$border_area = \"footer\"; include ($menu_script_path);
	echo \"\n</div></footer>\n\";
}";
		}
		else
			$page_content = str_replace("\$$tmp", $replace, $page_content);
		
	}
	// If head, than swap sValue to head.
	elseif ($special == 2)
	{		
		$search = "\$head = \$row[\"head_content\"];";
		$page_content = str_replace($search, "", $page_content);
		$search = "\$head = stripslashes(\$head);";
		$page_content = str_replace($search, "", $page_content);
		$search = "\$head= stripslashes(\$head);";
		$page_content = str_replace($search, "", $page_content);
		
		// Then pop border content where it was going to go.
		$replace = "\"" . addslashes($sValue) . "\";";
		$page_content = str_replace("\$head; ", $replace, $page_content);
	}

	if (($special == 1) || ($special == 2))
	{
		$sValue = "<br><br><b>Host Test Page for $page_name.</b><br><br>";
	}
		
	$contents_arr = explode("<!-- Page Content Divider -->", $sValue, 2);
	$page_content = str_replace("<?php echo \$page_content_arr[0]; ?>", $contents_arr[0], $page_content);
	$page_content = str_replace("<?php if (isset($page_content_arr[1])) echo $page_content_arr[1]; ?>", $contents_arr[1], $page_content);
	
	
	$preview_file = "pREVIEW.php";
	$preview_path = $document_root . $website_path . "/" . $preview_file;
	$fh = fopen($preview_path, 'w') or die("can't open file");
	fwrite($fh, $page_content);
	fclose($fh);

?>

	<iframe width="970" scrolling="auto" src="<?php echo $website_path . "/" . $preview_file; ?>" id="preview_frame" ></iframe>

	<div id="preview_close_button"><img src="theme/<?php echo $theme; ?>/images/close-button.png" alt="<?php echo CLOSE_WORD; ?>" onClick="window.close();" style="cursor:pointer;" /></div>
	
<script language="javascript" type="text/javascript">
    if (navigator.appName == "Microsoft Internet Explorer")
		document.getElementById("preview_frame").height = document.documentElement.clientHeight - 32;
	else if (navigator.appName == "Opera")
		document.getElementById("preview_frame").height = window.innerHeight - 32;
	else
		document.getElementById("preview_frame").height = window.innerHeight - 32;
</script>

</body>
</html>