<?
#######################################
#                                                                                                                #
#  adaptetion for AntiSlaed  42 Vladimir Sulatskov aka Sulla_l_k   #
#                                                                                                                #
#######################################
/*
#################################################
config/config_comments.php
##################################################
if (!defined("FUNC_FILE")) die("Illegal File Access");

$confc = array();
$confc['num'] = "15";
$confc['letter'] = "100";
$confc['sort'] = "0";
$confc['anonpost'] = "1";
###################################################
В БД нужна вот такая табличка
###################################################
CREATE TABLE `{pref}_board_comment` (
  `id` int(11) NOT NULL auto_increment,
  `cid` int(11) NOT NULL default '0',
  `date` datetime default NULL,
  `uid` int(11) NOT NULL default '0',
  `name` varchar(25) NOT NULL default '',
  `host_name` varchar(60) default NULL,
  `comment` text NOT NULL,
  PRIMARY KEY (`id`),
  KEY `cid` (`cid`),
  KEY `uid` (`uid`)
) ENGINE=MyISAM;
###################################################
*/
# Show comments
function board_show_com($cid, $num, $pag) {
	global $prefix, $db, $admin_file, $conf, $user, $confu;
	include("config/config_comments.php");
	
	$num = isset($_GET['num']) ? intval($_GET['num']) : "1";
	$pag = isset($_GET['pag']) ? "&amp;pag=".intval($_GET['pag']) : "";
	
	list($numstories) = $db->sql_fetchrow($db->sql_query("SELECT Count(cid) FROM ".$prefix."_board_comment WHERE cid='$cid'"));
	if ($numstories > 0) {
	
		$offset = ($num - 1) * $confc['num'];
		$numpages = ceil($numstories / $confc['num']);
		if ($confc['sort']) {
			$sort = "ASC";
			$a = ($num) ? $offset+1 : 1;
		} else {
			$sort = "DESC";
			$a = $numstories;
			if ($numstories > $offset) $a -= $offset;
		}
		$result = $db->sql_query("SELECT id, cid, UNIX_TIMESTAMP(date), uid, name, host_name, comment FROM ".$prefix."_board_comment WHERE cid='$cid' ORDER BY date ".$sort." LIMIT ".$offset.", ".$confc['num']);
		$c = 0;
		while (list($com_id, $com_cid, $com_date, $com_uid, $com_name, $com_host, $com_text) = $db->sql_fetchrow($result)) {
			$cmassiv[] = array($com_id, $com_cid, $com_date, $com_uid, $com_name, $com_host, $com_text);
			if ($c == 0) {
				$where = "'".$com_uid."'";
			} else {
				$where .= ",'".$com_uid."'";
			}
			$c++;
		}
		$result2 = $db->sql_query("SELECT user_id, user_name, user_email, user_website, user_avatar, user_regdate, user_icq, user_sig, user_viewemail, user_aim, user_yim, user_msnm, user_points, user_gender, user_votes, user_totalvotes FROM ".$prefix."_users WHERE user_id IN ($where)");
		while (list($user_id, $user_name, $user_email, $user_website, $user_avatar, $user_regdate, $user_icq, $user_sig, $user_viewemail, $user_aim, $user_yim, $user_msn, $user_points, $user_gender, $user_votes, $user_totalvotes) = $db->sql_fetchrow($result2)) {
			$umassiv[] = array($user_id, $user_name, $user_email, $user_website, $user_avatar, $user_regdate, $user_icq, $user_sig, $user_viewemail, $user_aim, $user_yim, $user_msn, $user_points, $user_gender, $user_votes, $user_totalvotes);
		}
		// ----------------------------------------------------------------------------
		if (is_user($user)) {
			getusrinfo($user);
		}
		// ----------------------------------------------------------------------------
		open();
		foreach ($cmassiv as $val) {
			$com_id = $val[0];
			$com_cid = $val[1];
			$com_date = $val[2];
			$com_uid = $val[3];
			$com_name = $val[4];
			$com_host = $val[5];
			$com_text = $val[6];
			unset($user_id, $user_name, $user_email, $user_website, $user_avatar, $user_regdate, $user_icq, $user_sig, $user_viewemail, $user_aim, $user_yim, $user_msn, $user_points, $user_gender, $user_votes, $user_totalvotes);
			if ($umassiv) {
				foreach ($umassiv as $val2) {
					if (strtolower($com_uid) == strtolower($val2[0])) {
						$user_id = $val2[0];
						$user_name = $val2[1];
						$user_email = $val2[2];
						$user_website = $val2[3];
						$user_avatar = $val2[4];
						$user_regdate = $val2[5];
						$user_icq = $val2[6];
						$user_sig = $val2[7];
						$user_viewemail = $val2[8];
						$user_aim = $val2[9];
						$user_yim = $val2[10];
						$user_msn = $val2[11];
						$user_points = $val2[12];
						$user_gender = $val2[13];
						$user_votes = $val2[14];
						$user_totalvotes = $val2[15];
					}
				}
			}
			    $c_name = _ANONYM." ".$com_name;   
			$user_name = ($user_name) ? $user_name : $c_name;
			$avatar = ($user_avatar && file_exists($confu['adirectory']."/".$user_avatar)) ? "<img src=\"".$confu['adirectory']."/".$user_avatar."\" align=\"left\" alt=\"$user_name\" title=\"$user_name\"/>" : "<img src=\"".$confu['adirectory']."/00.gif\" align=\"left\" alt=\"$user_name\" title=\"$user_name\"/>";
			$user_regdate = ($user_regdate) ? format_time($user_regdate) : "";
			$text = ($user_sig) ? bb_decode($com_text, $conf['name'])."<hr/>".bb_decode($user_sig, $conf['name']) : bb_decode($com_text, $conf['name']);
			$user_point = ($confu['point'] && $user_points) ? _POINTS.": ".$user_points : "";
			$rate = ajax_rating(0, $user_id, "account", $user_votes, $user_totalvotes);
			$name = "<a href=\"javascript:InsertCode('name','".rawurlencode($user_name)."','','','n1')\">".$user_name."</a>";
			$uscomd = _DATE.": ".date(_DATESTRING." "._TIMESTRING,$com_date);
			$uscomn = _COMMENT." #".$a;
			$usrdate = ($user_regdate) ? _REG_DATE.": ".$user_regdate : "";
			$usgender = ($user_gender != "") ? " ".gender($user_gender, 2) : "";
			$usinfo = ($user_name != $c_name) ? " ".user_info($user_name, 2) : "";
			$ussite = ($user_website != "") ? " <a href=\"$user_website\" target=\"_blank\" title=\"$user_website\"><img src=\"".img_find("all/home")."\" border=\"0\" align=\"middle\" alt=\"\"/></a>" : "";
			$usmail = ((is_moder() || $user_viewemail == 1) && $user_email) ? " <a href=\"mailto:".$user_email."?subject=".rawurlencode($conf['sitename'])."\" title=\"$user_email\"><img src=\"".img_find("all/contact")."\" border=\"0\" align=\"middle\" alt=\"\"/></a>" : "";
			$usicq = ($user_icq != "") ? " <a href=\"index.php?name=account&amp;op=info&amp;uname=$user_name\" title=\""._ICQ.": ".$user_icq."\"><img src=\"".img_find("all/icq")."\" border=\"0\" align=\"middle\" alt=\"\"/></a>" : "";
			$usaim = ($user_aim != "") ? " <a href=\"index.php?name=account&amp;op=info&amp;uname=$user_name\" title=\""._AIM.": ".$user_aim."\"><img src=\"".img_find("all/aim")."\" border=\"0\" align=\"middle\" alt=\"\"/></a>" : "";
			$usyim = ($user_yim != "") ? " <a href=\"index.php?name=account&amp;op=info&amp;uname=$user_name\" title=\""._YIM.": ".$user_yim."\"><img src=\"".img_find("all/yim")."\" border=\"0\" align=\"middle\" alt=\"\"/></a>" : "";
			$usmsn = ($user_msn != "") ? " <a href=\"index.php?name=account&amp;op=info&amp;uname=$user_name\" title=\""._MSN.": ".$user_msn."\"><img src=\"".img_find("all/msn")."\" border=\"0\" align=\"middle\" alt=\"\"/></a>" : "";

			if (is_moder($conf['name'])) {
				$usip = " <a href=\"".$conf['ip_link'].$com_host."\" title=\""._IP.": $com_host\" target=\"_blank\"><img src=\"".img_find("all/question")."\" border=\"0\" align=\"middle\" alt=\"\"/></a>";
				$usban = " ".ad_bann($admin_file.".php?op=security_block&amp;new_ip=".$com_host, $com_host);
			}
			if (($userinfo['user_name'] == $user_name) OR (is_moder($conf['name']))){
				$usedit = " ".ad_edit("index.php?name=".$conf['name']."&op=board_comm_edit&amp;id=".$com_id);
				$usdel = " ".ad_delete("index.php?name=".$conf['name']."&op=board_comm_del&amp;id=$com_id&amp;cid=$com_cid", cutstr(text_filter(bb_decode($com_text, $conf['name'])), 10));
			}
			$info = "<table summary=\"\" width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"left\">".$user_point."</td><td align=\"right\">".$uscomd.$uscomn."</td></tr></table>";
			$link = "<table summary=\"\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"left\">".$usrdate."</td><td align=\"right\">".$usgender.$usinfo.$ussite.$usmail.$usicq.$usaim.$usyim.$usmsn.$usip.$usban.$usedit.$usdel."</td></tr></table>";
			comment($com_id, $name, $info, $avatar, $text, $rate, $link, $user_point, $uscomd, $uscomn, $usrdate, $usgender, $usinfo, $ussite, $usmail, $usicq, $usaim, $usyim, $usmsn, $usip, $usban, $usedit, $usdel);
			if ($confc['sort']) {
				$a++;
			} else {
				$a--;
			}
		}
		close();
		num_page($conf['name'], $numstories, $numpages, $confc['num'], "op=view&amp;id=".$cid.$pag."&amp;");
		
	} else {
		warning(_NOCOMMENTS, "", "", 2);
	}
	if (!is_user() && $confc['anonpost'] == 0) {
		warning(_NOANONCOMMENTS, "", "", 1);
	} else {
		open();
			echo "<form name=\"post\" action=\"index.php?name=".$conf['name']."\" method=\"post\" >"; //onsubmit=\"ButtonDisable(this)\"
			if (is_user()) {
				echo "<div class=\"left\">"._YOURNAME.":</div><div class=\"center\">".text_filter(substr($user[1], 0, 25))."</div>";
			} else {
				echo "<div class=\"left\">"._YOURNAME.":</div><div class=\"center\"><input type=\"text\" name=\"postname\" size=\"65\" maxlength=\"25\" class=\"".$conf['name']."\"/></div>";
			}
			echo "<div class=\"left\">"._COMMENT.":</div><div class=\"center\">".textarea("1", "comment", "", $conf['name'], "5")."</div>"
			.captcha_random()
			."<div class=\"button\"><input type=\"hidden\" name=\"cid\" value=\"$cid\"/><input type=\"hidden\" name=\"op\" value=\"board_save_com\"/><input type=\"submit\" value=\""._COMMENTREPLY."\" class=\"fbutton\"/></div></form>";
		close();
	}
}

# Save comments
function board_save_com($postname, $comment, $cid) {
	global $prefix, $db, $user, $conf;
	include("config/config_comments.php");

	$e = explode(" ", $comment);
	for ($a = 0; $a < sizeof($e); $a++) $o = strlen($e[$a]);
	$stop = "";
	if ($comment == "") $stop = _CERROR1;
	if ($o > $confc['letter']) $stop = _CERROR2;
	if ((!is_user() && $postname == "") || (!is_user() && $confc['anonpost'] == 0)) $stop = _CERROR3;
	if (captcha_check(1)) $stop = _SECCODEINCOR;
	if (!$stop) {
		$postid = (is_user()) ? intval($user[0]) : "0";
		$postname = (!is_user()) ? $postname : "";

		$ip = getip();
		$comment = save_text($comment);
		$res=$db->sql_query("INSERT INTO ".$prefix."_board_comment VALUES (NULL, '$cid', now(), '$postid', '$postname', '$ip', '$comment')");
		if(!$res){
			echo "Болт:  INSERT INTO ".$prefix."_board_comment VALUES (NULL, '$cid', now(), '$postid', '$postname', '$ip', '$comment')";
		}else{
			Header("Location: index.php?name=".$conf['name']."&op=view&lid=".$cid);
		}
		
	} else {
		head();
			title(_WORK);
			warning($stop."<br/><br/>"._GOBACK, "", "", 1);
		foot();
	}
}


function board_comm_edit($id) {
	global $db, $prefix, $admin_file, $admin, $conf;
		if (is_user($user)) {
			getusrinfo($user);
			$author = $userinfo['user_name'];
			$authormail = $userinfo['user_email'];
		}
			$result = $db->sql_query("SELECT id, cid, name, comment FROM ".$prefix."_board_comment WHERE id='$id'");
			list($id, $cid, $name, $com_text) = $db->sql_fetchrow($result);
	
	if (is_admin($admin) OR ($name == $author)){
		head();
		menu();
			open();

				echo "<h2>"._EDIT."</h2>"
					."<form name=\"post\" action=\"index.php?name=".$conf['name']."\" method=\"post\" >"
					."<div class=\"left\">"._YOURNAME.":</div><div class=\"center\"><input type=\"text\" name=\"com_name\" value=\"".$name."\"size=\"65\" maxlength=\"25\" /></div>"
					."<div class=\"left\">"._COMMENT.":</div><div class=\"center\">".textarea("1", "comment", $com_text, $conf['name'], "5")."</div>"
					."<div class=\"button\">"._GOBACK." <input type=\"hidden\" name=\"id\" value=\"$id\"/><input type=\"hidden\" name=\"cid\" value=\"$cid\"/><input type=\"hidden\" name=\"op\" value=\"board_comm_edit_save\"><input type=\"submit\" value=\""._SAVE."\" class=\"fbutton\"/></div></form>";

			close();
			
		foot();
	} else {
	  Header("Location: index.php?name=".$conf['name']."");
    }
	
}


?>