<?php


require_once( "_inc.php" );
if ( !empty( $_GET['btnSubmit'] ) )
{
	util::auth( );
	$name = !empty( $_GET['name'] ) ? trim( $_GET['name'] ) : "";
	$domain = !empty( $_GET['domain'] ) ? trim( $_GET['domain'] ) : "";
	$keywords = !empty( $_GET['keywords'] ) ? trim( $_GET['keywords'] ) : "";
	$description = !empty( $_GET['description'] ) ? trim( $_GET['description'] ) : "";
	$categoryDir = !empty( $_GET['categoryDir'] ) ? trim( $_GET['categoryDir'] ) : "";
	$enableClickIn = !empty( $_GET['enableClickIn'] ) ? "1" : "0";
	$clickInRefreshInterval = !empty( $_GET['clickInRefreshInterval'] ) ? $_GET['clickInRefreshInterval'] : "1440";
	$timezone = !empty( $_GET['timezone'] ) ? intval( $_GET['timezone'] ) : "0";
	if ( empty( $name ) || empty( $domain ) )
	{
		$tpl->assign( "msg", "缺少参数。" );
		$tpl->display( "_msg.tpl" );
		exit( );
	}
	$srcFile = "../#data/_config.php";
	$dstFile = "../inc/config.php";
	$content = util::readfile( $srcFile );
	$arr = array(
		array(
			"SITENAME",
			$name
		),
		array(
			"SITEDOMAIN",
			$domain
		),
		array(
			"SITEKEYWORDS",
			$keywords
		),
		array(
			"SITEDESCRIPTION",
			$description
		),
		array(
			"DBHOST",
			$gDb['host']
		),
		array(
			"DBUSER",
			$gDb['user']
		),
		array(
			"DBPWD",
			$gDb['pwd']
		),
		array(
			"DBNAME",
			$gDb['db']
		),
		array(
			"DBPREFIX",
			$gDb['prefix']
		),
		array(
			"CATEGORYDIR",
			$categoryDir
		),
		array(
			"ENABLECLICKIN",
			$enableClickIn
		),
		array(
			"CLICKINREFRESHINTERVAL",
			$clickInRefreshInterval
		),
		array(
			"TIMEZONE",
			$timezone
		),
		array(
			"DATE",
			date( "Y-m-d" )
		),
		array(
			"USER",
			$dAdminName
		)
	);
	$i = 0;
	for ( ;	$i < count( $arr );	++$i	)
	{
		$content = str_replace( "~`~".$arr[$i][0]."~`~", $arr[$i][1], $content );
	}
	copy( "../inc/config.php", "../inc/bak.config.php" );
	if ( !$enableClickIn )
	{
		util::writefile( "../apps/clickindata.js", "" );
	}
	$done = util::writefile( $dstFile, $content );
	if ( $done )
	{
		$msg = "修改成功！";
	}
	else
	{
		$msg = "修改失败。";
	}
	$tpl->assign( "msg", $msg );
	$tpl->display( "_msg.tpl" );
	exit( );
}
$tpl->assign( $gSite );
$tpl->assign( "categoryDir", $gCategoryDir );
$tpl->assign( "enableClickIn", $gEnableClickIn );
$tpl->assign( "clickInRefreshInterval", $gClickInRefreshInterval );
$tpl->assign( "timezone", $gTimezone );
$tpl->assign( "now", strtotime( $gTimezone." hours" ) );
$tpl->display( );
?>
