<?php
$dr = $_SERVER['DOCUMENT_ROOT'];
function extractCMSContent($content, $links)
{
	?>
	<div class="editcontentwrapper">
		<div class="editbuttonbar" style="text-align: center"><a href="#" class="addnewcontent">Add new content area</a></div>
	</div>
	<?php
	$contentarray = explode("<content-divider>", $content);
	foreach($contentarray as $thiscontent)
	{
		?><div class="editcontentwrapper">
		<?php
		$commandarray = explode("<!>", $thiscontent);
		$thislocation = substr($commandarray[1], strpos($commandarray[1], 'loc:')+4);
		$contenttype = substr($commandarray[2], strpos($commandarray[2], 'type:')+5);
		?>Location: <select name="locationselect">
			<option value="main" <?php if ($thislocation == "main") { ?>selected="selected"<?php }; ?>>main</option>
			<option value="banner" <?php if ($thislocation == "banner") { ?>selected="selected"<?php }; ?>>banner</option>
			<option value="sidebar" <?php if ($thislocation == "sidebar") { ?>selected="selected"<?php }; ?>>sidebar</option>
		</select>
		Content type: <select name="contenttypeselect">
			<option value="html" <?php if ($contenttype == "html") { ?>selected="selected"<?php }; ?>>html</option>
			<option value="include" <?php if ($contenttype == "include") { ?>selected="selected"<?php }; ?>>include</option>
			<option value="plugin" <?php if ($contenttype == "plugin") { ?>selected="selected"<?php }; ?>>plugin</option>
			<option value="eval" <?php if ($contenttype == "eval") { ?>selected="selected"<?php }; ?>>eval</option>
		</select>
		<?php
		switch($contenttype)
		{
			case 'html':
				$htmlcontent = stripslashes($commandarray[3]);
				?>
				<textarea style="width: 100%; height: 130px" class="editpage tinymce"><?=$htmlcontent?></textarea>
				<?php
				break;
			case 'include':
				$includestring = trim($commandarray[3]);
				?>
				<textarea style="width: 100%; height: 50px" class="editpage"><?=$includestring?></textarea>
				<?php
				break;
			case 'plugin':
				$thisplugin = trim($commandarray[3]);
				if (strpos($commandarray[4], "true") !== false) { $thisplugin = $thisplugin."<!>true"; };
				?>
				<textarea style="width: 100%; height: 50px" class="editpage"><?=$thisplugin?></textarea>
				<?php
				break;
			case 'eval':
				$thiseval = trim($commandarray[3]);
				?>
				<textarea style="width: 100%; height: 50px" class="editpage"><?=$thiseval?></textarea>
				<?php
				break;
		};
		?>
		<div class="editbuttonbar" style="text-align: center"><a href="#" class="addnewcontent">Add new content area</a> | <a href="#" class="removesection">Remove this section</a></div>
		</div>
		<?php
	};
	/*if ($links != "") {
		$linksarray = explode(";", $links);
		$count = 0;
		$linkstring = "";
		foreach($linksarray as $thislink) {
			if ($count != 0) { $linkstring .= "\n"; };
			$linkstring .= $thislink;
			$count++;
		};
		?>
		<div style="font-weight: bold; text-align: center">Links content (separate names and links with a | symbol, and place a new line between links)</div>
		<div class="editlinksarea editarea"><textarea style="width: 100%; height: 150px" class="editpage links"><?=$linkstring?></textarea></div>
		<?php
	};*/
};
?>

<div class="maintext">
	<form action="#" method="post" enctype="multipart/form-data">
		<?php
		require_once($dr."/scripts/dbConnect.php");
		$locationarray = explode("/", $loc);
		$filename = array_pop($locationarray);
		$tree = implode("/", $locationarray);
		$query = "select content, links from pages where tree = \"$tree\" and filename = \"$filename\"";
		$result = mysql_query($query);
		$row = mysql_fetch_array($result);
		$row = array_map('stripslashes', $row);
		extract($row);
		extractCMSContent($content, $links);
		?>
		<div id="editpagebuttons"><div class="editpagebutton" id="editpagepreview">Preview changes</div><div class="editpagebutton" id="editpagesave">Publish changes</div><div class="editpagebutton" id="editpagecancel">Cancel changes</div></div>
		<div class="clearboth"></div>
	</form>
</div>