<?php


require_once( "_inc.php" );
$type = !empty( $_GET['type'] ) ? trim( $_GET['type'] ) : "";
$templateId = !empty( $_GET['tId'] ) ? intval( $_GET['tId'] ) : "";
if ( empty( $templateId ) )
{
	$templateId = !empty( $_POST['tId'] ) ? intval( $_POST['tId'] ) : "";
}
if ( empty( $templateId ) )
{
	$tpl->assign( "msg", "缺少参数。" );
	$tpl->display( "_msg.tpl" );
	exit( );
}
switch ( $templateId )
{
case "1" :
	$templateFile = "index.tpl";
	break;
case "2" :
	$templateFile = "category.tpl";
	break;
	$tpl->assign( "msg", "参数有误。" );
	$tpl->display( "_msg.tpl" );
	exit( );
}
if ( !empty( $type ) )
{
	if ( "1" == $type )
	{
		copy( "../templates/bak.".$templateFile, "../templates/".$templateFile );
	}
	else
	{
		copy( "../#data/".$templateFile, "../templates/".$templateFile );
	}
	header( "location: template.php?tId=".$templateId );
}
if ( !empty( $_POST['btnSubmit'] ) )
{
	util::auth( );
	$content = !empty( $_POST['content'] ) ? trim( $_POST['content'] ) : "";
	if ( empty( $content ) )
	{
		$tpl->assign( "msg", "缺少参数。" );
		$tpl->display( "_msg.tpl" );
		exit( );
	}
	copy( "../templates/".$templateFile, "../templates/bak.".$templateFile );
	$content = stripslashes( $content );
	$done = util::writefile( "../templates/".$templateFile, $content );
	if ( $done )
	{
		$msg = "修改成功！";
	}
	else
	{
		$msg = "修改失败。";
	}
	$tpl->assign( "msg", $msg );
	$tpl->display( "_msg.tpl" );
	exit( );
}
$tpl->assign( $gSite );
$tpl->assign( "templateId", $templateId );
$content = util::readfile( "../templates/".$templateFile );
$content = htmlspecialchars( $content );
$tpl->assign( "content", $content );
$tpl->display( );
?>
