<?php


require_once( "_inc.php" );
if ( !empty( $_POST['btnSubmit'] ) )
{
	$id = !empty( $_POST['id'] ) ? trim( $_POST['id'] ) : "";
	$name = !empty( $_POST['name'] ) ? trim( $_POST['name'] ) : "";
	$url = !empty( $_POST['url'] ) ? trim( $_POST['url'] ) : "";
	$order = !empty( $_POST['order'] ) ? trim( $_POST['order'] ) : "1000";
	if ( empty( $name ) || empty( $url ) )
	{
		$tpl->assign( "msg", "缺少参数。" );
		$tpl->display( "_msg.tpl" );
		exit( );
	}
	$exist = $db->fetchone( "SELECT f_id FROM ".$gDb['prefix'].( "usersites WHERE (f_userId='".$dUserId."' AND f_name='{$name}' AND f_id<>'{$id}')" ) );
	if ( $exist )
	{
		$tpl->assign( "msg", "该网址已存在。" );
		$tpl->display( "_msg.tpl" );
		exit( );
	}
	$now = time( );
	$sql = "UPDATE ".$gDb['prefix'].( "usersites SET f_name='".$name."', f_url='{$url}', f_order='{$order}' WHERE (f_userId='{$dUserId}' AND f_id='{$id}')" );
	$db->query( $sql );
	header( "location:sites.php" );
	exit( );
}
$id = !empty( $_GET['id'] ) ? intval( $_GET['id'] ) : "";
if ( empty( $id ) )
{
	header( "location: sites.php" );
	exit( );
}
$tpl->assign( "id", $id );
$tpl->assign( "userId", $dUserId );
$tpl->display( );
?>
