<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 6/3/2009
// Revised: 2/7/2012	ELL
// Added reference to user files folder constant.
	
	include ("check_session.php");
	
	$page_name = $_REQUEST['page_name'];
	$head_name = $_REQUEST['head_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_head.php\" title=\"here\">here</a> to return to the head content 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_head = "head_name='head";
		
	$start_pos = strpos($contents, $search_str_head, 1);
	$end_pos = strpos($contents, "\"", $start_pos);
	$search_str = substr($contents, $start_pos, $end_pos - $start_pos);
	$replace_str = "head_name='" . $head_name . "'";
	$new_contents = str_replace($search_str, $replace_str, $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_head.php?head_assigned=1");
?>