<?php

require_once($_SERVER['DOCUMENT_ROOT']. "/$ADMIN_DIR/templates/template_common.php");
    
list($access, $User) = check_basic_access($_REQUEST);
if (!$access) {
	header("Location: /$ADMIN/logout.php");
	exit;
}

if (!$User->hasPermission('ManageTempl')) {
	header("Location: /$ADMIN/ad.php?ADReason=".urlencode(getGS("You do not have the right to modify templates.")));
	exit;
}

$cPath = Input::Get('cPath', 'string', '');
if (!Template::IsValidPath($cPath)) {
	header("Location: /$ADMIN/templates/");
	exit;
}
$cName = Input::Get('cName', 'string', '');
$Name = Input::Get('Name', 'string', '');

$correct = trim($cName) != "";
if ($correct) {
	$cName = strtr($cName,'?~#%*&|"\'\\/<>', '_____________');
	$newTempl = $Campsite['HTML_DIR']."/look/".urldecode($cPath)."/$cName";
	$exists = file_exists($newTempl);
	$ok = 0;
	if (!$exists) {
		$tpl1_name = urldecode($cPath)."/$Name";
		$tpl1 = $Campsite['HTML_DIR']."/look/".urldecode($cPath)."/$Name";
		$fd = fopen ($tpl1, "r");
		$contents = fread ($fd, filesize ($tpl1));
		fclose ($fd);

		$tpl2_name = urldecode($cPath)."/$cName";
		$tpl2 = $Campsite['HTML_DIR']."/look/".urldecode($cPath)."/$cName";
		$fd = fopen ($tpl2, "w");
		$res = fwrite ($fd, $contents);
		fclose ($fd);
		$ok = $res == true || strlen($contents) == 0;
		if ($ok) {
			$logtext = getGS('Template $1 was duplicated into $2', $tpl1_name, $tpl2_name);
			query ("INSERT INTO Log SET TStamp=NOW(), IdEvent=115, User='".$User->getUserName()."', Text='$logtext'");
		}
	}
}

$crumbs = array();
$crumbs[] = array(getGS("Configure"), "");
$crumbs[] = array(getGS("Templates"), "/$ADMIN/templates");
$crumbs = array_merge($crumbs, camp_template_path_crumbs($cPath));
$crumbs[] = array(getGS("Duplicate template").": $Name", "");
echo camp_html_breadcrumbs($crumbs);


?>
<P>
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="8" ALIGN="CENTER" class="table_input">
	<TR>
		<TD COLSPAN="2">
			<B> <?php  putGS("Duplicate template"); ?> </B>
			<HR NOSHADE SIZE="1" COLOR="BLACK">
		</TD>
	</TR>
	<TR>
		<TD COLSPAN="2"><BLOCKQUOTE>
<?php  
if (!$correct) {
?>		<LI><?php  putGS('You must complete the $1 field.','<B>'.getGS('Name').'</B>'); ?></LI>
<?php
} else {
	if ($ok) {
		putGS('The template $1 has been created.','<b>'.$cName.'</B>');
	} else {
		putGS('The template $1 could not be created.','<b>'.$cName.'</B>');
	}
}
?>		</BLOCKQUOTE></TD>
	</TR>
<?php  if ($ok) { ?>	<TR>
		<TD COLSPAN="2"><BLOCKQUOTE><LI><?php  putGS('Do you want to edit the template ?'); ?></LI></BLOCKQUOTE></TD>
	</TR>
<?php  } ?>
	<TR>
		<TD COLSPAN="2">
		<DIV ALIGN="CENTER">
<?php  if ($ok) { ?>		<INPUT TYPE="button" class="button" NAME="Yes" VALUE="<?php  putGS('Yes'); ?>" ONCLICK="location.href='/<?php echo $ADMIN; ?>/templates/edit_template.php?Path=<?php  p(urlencode($cPath)); ?>&Name=<?php p(urlencode($cName)); ?>'">
		<INPUT TYPE="button" class="button" NAME="No" VALUE="<?php  putGS('No'); ?>" ONCLICK="location.href='/<?php echo $ADMIN; ?>/templates?Path=<?php  p(urlencode($cPath)); ?>'">
<?php  } else { ?>
		<INPUT TYPE="button" class="button" NAME="OK" VALUE="<?php  putGS('OK'); ?>" ONCLICK="location.href='/<?php echo $ADMIN; ?>/templates/dup.php?Path=<?php  p(urlencode($cPath)); ?>&Name=<?php  p(urlencode($Name)); ?>'">
<?php  } ?>
		</DIV>
		</TD>
	</TR>
</TABLE>
<P>
<?php
camp_html_copyright_notice();
?>

</HTML>

