<?php
/*
+--------------------------------------------------------+
| O-BLOG - PHP Blog System                               |
| Copyright (c) 2004 phpBlog.CN                          |
| Support : http://www.phpBlog.cn                        |
| Author : ShiShiRui (shishirui@163.com)                 |
|--------------------------------------------------------+
*/
//error_reporting(7);

if (!defined('BLOG')) {
	die('Access Denied');
}

if($newremark)
{
	$email = checkPost($_POST[get_input('email')]);
	$username = checkPost($_POST[get_input('username')]);
	$content = checkPost($_POST[get_input('content')]);
	$check_code = checkPost($_POST[get_input('check_code')]);
	$inblog = checkPost($_POST[get_input('inblog')]);

	if(!$username || !$content)
	{
		sleep($delay_time);	
		ob_exit("您的表单没有填写完整");
	}
	else if (get_check_code() != $check_code)
	{
		ob_exit("系统验证码错误");
	}
	else
	{
		//检查发布间隔时间
		if ($post_gb_time >0)
		{
			if(isset($_COOKIE['ob_post_gb_time']) && $_COOKIE['ob_post_gb_time'] == "enable_post")
			{
				ob_exit("您两次发表间隔应该大于{$post_gb_time}秒");
			}
		}
		//检查日志是否允许评论
		$allow_remark = $DB->fetch_one("SELECT `allow_remark` FROM `".$mysql_prefix."blog` WHERE `id` = ".$inblog);
		if(!$allow_remark)
		{
			ob_exit("此日志不允许评论");
		}
		
		//检查email格式
		if($email)
		{
			if(!CheckEmail($email))
			{
				ob_exit("E-mail 格式不正确");
			}
		}
		//是否审核评论
		$checkremark = $DB->fetch_one("SELECT `checkremark` FROM `".$mysql_prefix."config`");
		$checkremark = ($checkremark == 1) ? 0 : 1;
		$username = nl2br(htmlspecialchars($username));
		$content = nl2br(htmlspecialchars($content));
		//过滤用户名
		foreach($banned_username as $val)
		{
			$val = trim($val);
			if ($val && strpos($username,$val) !== false)
			{
				sleep($delay_time);	
				ob_exit("对不起，管理员禁止使用这个用户名");
			}
		}
		//过滤ip
		$ip = getip();
		if (trim($ip) != "")
		{
			foreach($banned_ip as $bip)
			{
				if(compare_ip($bip,$ip))
				{
					sleep($delay_time);	
					ob_exit("对不起，此IP地址($ip)已经被管理员禁止发表评论/留言");
				}
			}
		}
		//过滤内容
		foreach($banned_word as $val)
		{
			$val = trim($val);
			if ($val && strpos($content,$val) !== false)
			{
				sleep($delay_time);	
				ob_exit("对不起，您的留言里含有非法词语");
			}
		}
		//检查字数
		if(strlen($content) > $max_gb_char)
		{
				ob_exit("评论字数超出限制");
		}

		if (!$DB->query("INSERT INTO {$mysql_prefix}remark (`date`, `content`, `username`, `email`, `inblog`, `ip`, `ischeck`) VALUES ('".time()."', '".$content."', '".$username."', '".$email."', '".$inblog."','".getip()."','".$checkremark."')"))
		{
			ob_exit("添加评论时发生错误");
		}

		if($checkremark)
		{
			cache_lastremark();
			//生成HTML页面
			if($makehtml)
			{
				require('admin/class/build.php');
				$html = new build;
				$html->makeindex();
				$html->make($inblog);
			}
			if(!$post_gb_time)
			{
				sleep($config['submit_wait_time']);
				header("location:".$_SERVER['HTTP_REFERER']);
			}
			else
			{
				setcookie("ob_post_gb_time","enable_post",time()+$post_gb_time,"/");
				sleep($config['submit_wait_time']);
				header("location:".$_SERVER['HTTP_REFERER']);
			}
			
			
		}
		else
		{
			ob_exit("感谢您的评论，但是您的评论需要审核后才能显示");
		}
	}
} 
else
{
	if(isset($_POST['inblog']))
	{
		$id = intval($_POST['inblog']);
	}
	if ($config['desc_remark'])
	{
		$remarks = $DB->query("SELECT * FROM `".$mysql_prefix."remark` WHERE `ischeck` = 1 AND `inblog` = ".$id." ORDER BY id DESC");
	}
	else
	{
		$remarks = $DB->query("SELECT * FROM `".$mysql_prefix."remark` WHERE `ischeck` = 1 AND `inblog` = ".$id." ORDER BY id ASC");
	}
	$remarkNum = $DB->num_rows($remarks);
	$orderid = 1;
	$remark = array();
	while($remarkRe = $DB->fetch_array($remarks)) {
		$remark[] = array("id" => $orderid++,
						"content" => $remarkRe['content'],
						"username" => $remarkRe['username'],
						"date" => obdate($date_format,$remarkRe['date']),
						"email" => $remarkRe['email'],
			);
	}
	if($makehtml) {
		$_COOKIE['remark_name'] = "";
		$_COOKIE['remark_email'] = "";
	}
	$remarkT = template("remark");
	$remarkT->assign($ads);
	$remarkT->assign("name",$_COOKIE['remark_name']);
	$remarkT->assign("email",$_COOKIE['remark_email']);
	$remarkT->assign("num",$remarkNum);
	$remarkT->assign("remark",$remark);
	$remarkT->assign("inblog",$id);
	$remarkT->assign("blogurl",$blogurl);
	$remarkT->assign("makehtml",$makehtml);
	$remarkT->assign("allow_trackback",$config['allow_trackback']);
	
	$trackbacks = $DB->query("SELECT * FROM `".$mysql_prefix."trackback` WHERE `inblog` = ".$id." ORDER BY `trackbackid` ASC");
	$tbnum = $DB->num_rows($trackbacks);
	$tborderid = 1;
	$trackback = array();
	while($trackbackRe = $DB->fetch_array($trackbacks)) {
		$trackback[] = array("id" => $tborderid++,
							"trackbackid" => $trackbackRe['trackbackid'],
							"date" => obdate($date_format,$trackbackRe['adddate']),
							"title" => trim($trackbackRe['title']),
							"url" => trim($trackbackRe['url']),
							"excerpt" => trim($trackbackRe['excerpt']),
							"blogname" => trim($trackbackRe['blogname']),
							"inblog" => trim($trackbackRe['inblog']),
		);
	}
	$remarkT->assign("trackback",$trackback);
	$remarkT->assign("tbnum",$tbnum);

	$remark_data = $remarkT->result();
}
?>