<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright EB Dynamics, Inc., Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 6/3/2009
// Rezised: 6/29/2009	ELL
// Fixed bug of assignments corrupting templates that do not have all 4: top, bottom, left and right sections.
// Revised: 10/13/2009	ELL
// Added changing of $border_area to new border areas.  $border_area is a new var used for the new PHP menu generator.
// This change will allow different menus to be assigned based on border area assignment.
// Revised: 2/7/2012	ELL
// Added reference to user files folder constant.
	
	include ("check_session.php");
	
	$page_name = $_REQUEST['page_name'];
	$right_area_name = $_REQUEST['right_area_name'];
	$left_area_name = $_REQUEST['left_area_name'];
	$top_area_name = $_REQUEST['top_area_name'];
	$bottom_area_name = $_REQUEST['bottom_area_name'];
	
	//Test to see if file already exists.
	$document_root = $_SERVER['DOCUMENT_ROOT'];
	if (BWB_BASE_DIR == '')
		$file_name = $document_root . "/" . BWB_WEBPAGE_DIR . "/" . $page_name . ".php";
	else
		$file_name = $document_root . "/" . BWB_BASE_DIR . "/" . BWB_WEBPAGE_DIR . "/" . $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");
	$contents = fread($fh, filesize($file_name));
	fclose($fh) or die($php_errormsg);
		
	$search_str_right = "page_name='right";
	$search_str_left = "page_name='left";
	$search_str_top = "page_name='top";
	$search_str_bottom = "page_name='bottom";
	
	$search_str_right_menu = "$border_area = \"right";
	$search_str_left_menu = "$border_area = \"left";
	$search_str_top_menu = "$border_area = \"top";
	$search_str_bottom_menu = "$border_area = \"bottom";
	
	$new_contents = $contents;
		
	$start_pos = strpos($contents, $search_str_right, 1);
	if ($start_pos !== false)
	{
		$end_pos = strpos($contents, "\"", $start_pos);
		$search_str = substr($contents, $start_pos, $end_pos - $start_pos);
		$replace_str = "page_name='" . $right_area_name . "'";
		$new_contents = str_replace($search_str, $replace_str, $new_contents);
	}
	
	$start_pos = strpos($contents, $search_str_left, 1);
	if ($start_pos !== false)
	{
		$end_pos = strpos($contents, "\"", $start_pos);
		$search_str = substr($contents, $start_pos, $end_pos - $start_pos);
		$replace_str = "page_name='" . $left_area_name . "'";
		$new_contents = str_replace($search_str, $replace_str, $new_contents);
	}
	
	$start_pos = strpos($contents, $search_str_top, 1);
	if ($start_pos !== false)
	{
		$end_pos = strpos($contents, "\"", $start_pos);
		$search_str = substr($contents, $start_pos, $end_pos - $start_pos);
		$replace_str = "page_name='" . $top_area_name . "'";
		$new_contents = str_replace($search_str, $replace_str, $new_contents);
	}
	
	$start_pos = strpos($contents, $search_str_bottom, 1);
	if ($start_pos !== false)
	{
		$end_pos = strpos($contents, "\"", $start_pos);
		$search_str = substr($contents, $start_pos, $end_pos - $start_pos);
		$replace_str = "page_name='" . $bottom_area_name . "'";
		$new_contents = str_replace($search_str, $replace_str, $new_contents);
	}
	/////////////////////////////////////
	$start_pos = strpos($contents, $search_str_right_menu, 1);
	if ($start_pos !== false)
	{
		$end_pos = strpos($contents, ";", $start_pos);
		$search_str = substr($contents, $start_pos, $end_pos - $start_pos);
		$replace_str = "$border_area = \"" . $right_area_name . "\"";
		$new_contents = str_replace($search_str, $replace_str, $new_contents);
	}
	
	$start_pos = strpos($contents, $search_str_left_menu, 1);
	if ($start_pos !== false)
	{
		$end_pos = strpos($contents, ";", $start_pos);
		$search_str = substr($contents, $start_pos, $end_pos - $start_pos);
		$replace_str = "$border_area = \"" . $left_area_name . "\"";
		$new_contents = str_replace($search_str, $replace_str, $new_contents);
	}
	
	$start_pos = strpos($contents, $search_str_top_menu, 1);
	if ($start_pos !== false)
	{
		$end_pos = strpos($contents, ";", $start_pos);
		$search_str = substr($contents, $start_pos, $end_pos - $start_pos);
		$replace_str = "$border_area = \"" . $top_area_name . "\"";
		$new_contents = str_replace($search_str, $replace_str, $new_contents);
	}
	
	$start_pos = strpos($contents, $search_str_bottom_menu, 1);
	if ($start_pos !== false)
	{
		$end_pos = strpos($contents, ";", $start_pos);
		$search_str = substr($contents, $start_pos, $end_pos - $start_pos);
		$replace_str = "$border_area = \"" . $bottom_area_name . "\"";
		$new_contents = str_replace($search_str, $replace_str, $new_contents);
	}
	
	// Write file.
	$fh = fopen($file_name, 'w') or die("Can't open $file_name: $php_errormsg");
	$result = (fputs($fh, $new_contents));
	fclose($fh) or die($php_errormsg);
	@chmod($file_name, 0644);
	
	header("Location: manage_special.php");
?>