<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., 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 = mysql_query($query, $bwb_link);
	$row = mysql_fetch_array($result);
	$sValue = $row["tmp_page_data"];
	
	if ($mode == "blog")
	{
		$query = "SELECT * FROM " . BWB_TABLE_PREFIX . "page_data WHERE page_name = '" . $page_name . "'";	
		$result = mysql_query($query, $bwb_link);
		$row = mysql_fetch_array($result);
		$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 PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>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 = mysql_query($query, $bwb_link);
	while ($row = mysql_fetch_array($result))
	{
		$name = $row["name"];
		echo "<link rel=\"stylesheet\" href=\".." . $userfiles_path . "/" . BWB_CSS_DIR . "/" . $name . ".css\" type=\"text/css\">\n";
	}
?>
<style>
 #help_button_sm	{
	position:inherit;
	width:30px;
	height:28px;
	background-image:url(images/HelpIcon_xsm.png);
}

#help	{
	display:none;
	position:absolute;
	top:100px;
	left:100px;
	background-color:#FFF;
	padding:10px;
	border:thick;
	border-style:outset;
	border-color:#060;
}

#help_iframe	{
	width:500px;
	height:500px;
}
</style>
<script language="javascript" type="text/javascript">
	function OpenHelpWindow()
	{
		window.open("http://breezewebsitebuilder.com/docs/cms/help.html#preview_page", "Help", "scrollbars=1,resizable=1,status=0,toolbar=0,menubar=0,location=0,height=700,width=800");
	}
</script>
</head>

<body>

<?php

	echo "<b>Page Name: $page_name</b><br>";
	
	// If border area, find a host page to use for display.
	if ($special == 1)
	{
		$query = "SELECT page_name FROM " . BWB_TABLE_PREFIX . "page_data";
		$result = mysql_query($query, $bwb_link);
		$page_found = false;
		while($row = mysql_fetch_array($result))
		{
			$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)	// 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 exists.  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:
		$search = "\$$page_name = \$row[\"page_content\"];";
		$page_content = str_replace($search, "", $page_content);
		$search = "\$$page_name= stripslashes(\$$page_name);";
		$page_content = str_replace($search, "", $page_content);
		$search = "\$$page_name = stripslashes(\$$page_name);";
		$page_content = str_replace($search, "", $page_content);
		
		// Then pop border content where it was going to go.
		$replace = "\"" . addslashes($sValue) . "\"";
		$page_content = str_replace("\$$page_name", $replace, $page_content);
		$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 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="100%" scrolling="auto" src="/<?php echo $website_path . $preview_file; ?>" id="preview_frame" ></iframe>

	<table width="80" align="center">
	  <tr>
	  	  <td align="center">
		  <form action="save_page.php" method="post" name="frmSaveAndEditPage">
		  <input type="submit" value="Save and go back to Editor" name="update_from_preview">
		  <?php
		  	echo "<input type='hidden' name='page_name' value='$page_name'>\n";
			echo "<input type='hidden' name='special' value='$special'>\n";
			echo "<input type='hidden' name='mode' value='$mode'>\n";
			echo "<input type='hidden' name='blog_id' value='$blog_id'>\n";
		  ?>
		  </form>
		  </td>
		  <td align="center">
		   <form action="save_page.php" method="post" name="frmSavePage">
		  	<input type="submit" value="Publish and go back to Main Menu" name="update_and_return_to_main">
			<?php
				echo "<input type='hidden' name='page_name' value='$page_name'>\n";
				echo "<input type='hidden' name='special' value='$special'>\n";
				echo "<input type='hidden' name='mode' value='$mode'>\n";
				echo "<input type='hidden' name='blog_id' value='$blog_id'>\n";
		  	?>
		   </form>
		  </td>
		  <td align="center">
		  <form action="edit_page.php" method="post" name="frmGoBackToEditPageWoSaving">
		  <input type="submit" value="Discard and go back to Editor" name="Back">
          <?php
				echo "<input type='hidden' name='page_name' value='$page_name'>\n";
				echo "<input type='hidden' name='special' value='$special'>\n";
				echo "<input type='hidden' name='mode' value='$mode'>\n";
				echo "<input type='hidden' name='preview' value='1'>\n";
				echo "<input type='hidden' name='blog_id' value='$blog_id'>\n";
		  	?>
		  </form>
		  </td>
          <td align="center" valign="top">
		  <div id="help_button_sm" onClick="OpenHelpWindow();">
            &nbsp;
          </div>
		  </td>
	  </tr>
	</table>
	
<script language="javascript" type="text/javascript">
    if (navigator.appName == "Microsoft Internet Explorer")
		document.getElementById("preview_frame").height = document.documentElement.clientHeight - 80;
	else if (navigator.appName == "Opera")
		document.getElementById("preview_frame").height = window.innerHeight - 80;
	else
		document.getElementById("preview_frame").height = window.innerHeight - 80;
</script>

</body>
</html>