<?
	/**
	 * File save script.
	 *
	 * @author	$Author: suraj $
	 * @version	$Revision: 1.10 $, $Date: 2003/02/11 03:48:02 $
	 */
	//
	
	require_once("assets/includes/config.inc.php");
	
	// boolean to check if the path is ready to be read from
		$path_ok = !is_dir($path);
	
	// user posted values
		$overwrite = $HTTP_POST_VARS['overwrite'];
		$file_contents = $HTTP_POST_VARS['file_contents'];
	
	// adjust for magic quotes
	
		if(get_magic_quotes_gpc())
		{
			$file_contents = stripslashes($file_contents); // removes magic quotes
		}
	
	// start displaying page
	$page_title = $appVars['name'] . " - Save File";
	include("assets/includes/page-header.inc.php");
	
?>
<h1>Save File</h1>
<?
	if($overwrite == "")
		$overwrite = 0;
		
	if($path_ok)
	{
		if(file_exists($path) && $overwrite == 0)
		{
			$file_contents = htmlspecialchars($file_contents);
		?>
			<form method="post" action="">
				<input type="hidden" name="file_contents" value="<?= $file_contents ?>">
				<input type="hidden" name="path" value="<?= $path ?>">
				<input type="hidden" name="overwrite" value="1">
				
				<table class="dialog" border="0" cellspacing="0" cellpadding="10">
				<tr>
					<td>The file <tt><?= $path ?></tt> already exists. Overwrite it?</td>
				</tr>
				<tr>
					<td align="right"><input type="submit" value="Yes, Overwrite"> <input type="button" value="Cancel" onclick="window.close()"></td>
				</tr>
				</table>
			</form>
		<?
		}
		else if(!file_exists($path) || $overwrite == 1)
		{
			if($fp = fopen($path, "w"))
			{
				$file_contents = str_replace("\r\n", "\n", $file_contents); // removes double line spacing
				fwrite($fp, $file_contents); // write content
				fclose($fp); // close file
				
				echo "<div class=\"dialog\">File <tt>$path</tt> was saved successfully.</div>";
			}
			else
			{
				echo "<span class=\"error\">Unable to write to file <tt>$path</tt>.</span>";
			}
		}
	}
	else
	{
		echo "<span class=\"error\">Unable to write to file <tt>$path</tt>.</span>";
	}
?>
<p><a href="#" onclick="window.close()">Close Window</a></p>
<?
	include("assets/includes/page-footer.inc.php");
?>
