<?php
	
// editor_support.php
// -----------------------------------------------------------------------------
// PHP that pre and post parses panel HTML, stripping and reinserting
// PHP variables, and saves edited panels
//
// Project    > A16-dmABS-1.5-Feb09
// Copyright  > (C) Digital Mail Limited (2005 - 2009)
// Author     > Richard Knight (RAK) for DML
// -----------------------------------------------------------------------------

$panel_path = $_REQUEST['panel_path'];
$panel_id   = str_replace('.html', '', substr($panel_path, strrpos($panel_path, DIRECTORY_SEPARATOR) + 1));

if (isset($_POST['edit_action']) && $_POST['edit_action'] == 'save')
{
	// If content has been edited save it and return to page
	$panel_html =  $_POST['content'];
	if (get_magic_quotes_gpc()) $panel_html = stripslashes($panel_html);
	$panel_html = post_parse_panel($panel_html);
	if ($panel->save_edited_panel($panel_id, $panel_html))
	{
		header('LOCATION: ' . $_POST['return_url']);
		exit;
	}
	$error_flag = 1; // Saving the panel failed
}
else if (isset($_POST['edit_action']) && $_POST['edit_action'] == 'cancel')
{
	// The user has cancelled the edit, so return to page
	header('LOCATION: ' . $_POST['return_url']);
}
else
{
	// Load the panel into the editor
	$panel_html = file_get_contents($panel->root . $panel_path . '.html');
	$panel_html = pre_parse_panel($panel_html);
	
	// Construct the return URL
	$return_url = $_GET['return_url'];
	if (isset($_GET['owner'])) $return_url .= '?owner=' . $_GET['owner']; // Set if coming from /admin/index.php
}

// -------------------------------------------------------------------------------------------------

function pre_parse_panel($panel_html)
{
	// Replace $abs_root, $abs_opt['theme'], $dmx_glib with their current values
	// and references to the current numbver range $abs_subject[\'range\']; with {DYN:range}
	global $abs_root, $abs_opt, $dmx_glib;

	// echo '<h2>Before</h2>';
	// echo '<pre>'. htmlentities($panel_html) . '</pre>';
	
	$panel_html = str_replace('{ROOT}',			               	$abs_root,            						$panel_html);
	$panel_html = str_replace('{THEME}',     					$abs_root . '/themes/' . $abs_opt['theme'], $panel_html);
	$panel_html = str_replace('{GLIB}',			               	$dmx_glib,            						$panel_html);
	$panel_html = str_replace('<?=$abs_subject[\'range\'];?>', 	'{SUB:NO_RANGE}',     						$panel_html);
		
	// echo '<h2>After pre_parse_panel()</h2>';
	// echo '<pre>'. htmlentities($panel_html) . '</pre>';
	
	return $panel_html;
}

// -------------------------------------------------------------------------------------------------

function post_parse_panel($panel_html)
{
	// Reinstate $abs_root, $abs_opt['theme'], $dmx_glib, and the range
	global $abs_root, $abs_opt, $dmx_glib;
	
	$panel_html = str_replace($dmx_glib,         							'{GLIB}',	$panel_html);
	$panel_html = str_replace($abs_root . '/themes/' . $abs_opt['theme'], 	'{THEME}',	$panel_html);
	$panel_html = str_replace($abs_root,         							'{ROOT}',	$panel_html);
	$panel_html = str_replace('{SUB:NO_RANGE}',  '<?=$abs_subject[\'range\'];?>', 		$panel_html);
	$panel_html = str_replace('&#39;',           '\'',                            		$panel_html);
	
	// echo '<h2>After post_parse_panel()</h2>';
	// echo '<pre>'. htmlentities($panel_html) . '</pre>';
	
	return $panel_html;
}

// -------------------------------------------------------------------------------------------------

?>