<?php
if (!defined('FUNC_FILE')) die("Illegal File Access");

function ShowComments($cid) {
	global $prefix, $db, $user, $module_name, $admin, $anonpost, $admin_file, $pagenum, $adirectory;
	require_once("config/config_comments.php");
	list($numstories) = $db->sql_fetchrow($db->sql_query("SELECT Count(cid) FROM ".$prefix."_comment WHERE cid='$cid' AND modul='$module_name'"));
    if (!$numstories) {
		OpenTable();
		echo "<center><font class=\"option\">"._NOCOMMENTS."</font></center>";
		CloseTable();
	} else {
		OpenTable();
		echo "<center><font class=\"option\">"._COMMENTS."</font></center><br />";
		if ($pagenum == "") $pagenum = 1;
		$offset = ($pagenum - 1) * $comnum;
		$numpages = ceil($numstories / $comnum);
		$a = $numstories;
		if ($numstories > $offset) $a -= $offset;
		$offset = intval($offset);
		$comnum = intval($comnum);
		$i = 0;
		$result = $db->sql_query("SELECT a.id, a.cid, a.date, a.uid, a.name, a.host_name, a.comment, b.user_name, b.user_email, b.user_website, b.user_avatar, b.user_icq, b.user_sig, b.user_viewemail, b.user_aim, b.user_yim, b.user_msnm FROM ".$prefix."_comment AS a LEFT JOIN ".$prefix."_users AS b ON (a.uid=b.user_id) WHERE cid='$cid' AND modul='$module_name' ORDER BY date DESC LIMIT $offset, $comnum");
		echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"1\" width=\"100%\" class=\"bgcolor4\">";
		while (list($id, $c_cid, $send_date, $sender_id, $sender_name, $sender_host, $com_text, $user_name, $user_email, $user_website, $user_avatar, $sender_icq, $user_sig, $user_viewemail, $sender_aim, $sender_yim, $sender_msn) = $db->sql_fetchrow($result)) {
			$c_cid = intval($c_cid);
			$sender_name = ($user_name) ? $user_name : $sender_name;
			echo "<tr class=\"bgcolor1\"><td><a href=\"javascript: InsertCode('name', '".$sender_name."')\"><b>$sender_name</b></a></td><td width=\"100%\"><table width=\"100%\"><tr>";
			if ($user_name) echo "<td><a href=\"index.php?name=Account&op=info&uname=$sender_name\" title=\""._PERSONALINFO."\"><img border=\"0\" src=\"images/info.gif\" width=\"16\" height=\"16\"></a></td>";
			if ($user_website != "") echo "<td><a href=\"$user_website\" target=\"_blank\" title=\"$user_website\"><img border=\"0\" src=\"images/blocks/Home.gif\" width=\"16\" height=\"16\"></a></td>";
			if ((is_admin($admin) || $user_viewemail == 1) && $user_email) echo "<td><a href=\"mailto:$user_email\" title=\"$user_email\"><img border=\"0\" src=\"images/blocks/Contact.gif\" width=\"16\" height=\"16\"></a></td>";
			if ($sender_icq != "") echo "<td><a href=\"index.php?name=Account&op=info&uname=$sender_name\" title=\"ICQ Number: $sender_icq\"><img border=\"0\" src=\"images/blocks/icq.gif\" width=\"16\" height=\"16\"></a></td>";
			if ($sender_aim != "") echo "<td><a href=\"index.php?name=Account&op=info&uname=$sender_name\" title=\"AIM Address: $sender_aim\"><img border=\"0\" src=\"images/blocks/aim.gif\" width=\"16\" height=\"16\"></a></td>";
			if ($sender_yim != "") echo "<td><a href=\"index.php?name=Account&op=info&uname=$sender_name\" title=\"Yahoo Messenger: $sender_yim\"><img border=\"0\" src=\"images/blocks/yim.gif\" width=\"16\" height=\"16\"></a></td>";
			if ($sender_msn != "") echo "<td><a href=\"index.php?name=Account&op=info&uname=$sender_name\" title=\"MSN Messenger: $sender_msn\"><img border=\"0\" src=\"images/blocks/msn.gif\" width=\"16\" height=\"16\"></a></td>";
			if (is_admin($admin)) {
				echo "<td><a href=\"".$admin_file.".php?op=SecurityBlocker&new_ip=$sender_host\" title=\""._BANIPSENDER."\"><img border=\"0\" src=\"images/blocks/Close.gif\" width=\"16\" height=\"16\"></a></td><td><a href=\"".$admin_file.".php?op=DeleteComments&id=$id&cid=$c_cid&module_name=$module_name&com_text=".htmlspecialchars($com_text)."\" title=\""._DELETE."\"><img border=\"0\" src=\"images/delete.gif\" width=\"16\" height=\"16\"></a></td><td><a href=\"".$admin_file.".php?op=EditComments&id=$id\" title=\""._EDIT."\"><img border=\"0\" src=\"images/edit.gif\" width=\"16\" height=\"16\"></a></td>";
				$userip = ""._IP.": <a href=\"http://www.dnsstuff.com/tools/whois.ch?ip=$sender_host\" title=\""._IP.": $sender_host\" target=\"_blank\">$sender_host</a> | ";
			}
			echo "<td width=\"100%\" align=\"right\">".$userip.""._DATE.": ".formatTimestamp($send_date)." | "._COMMENT.": $a</td></tr></table></td></tr>";
			$user_avatar = ($user_avatar && file_exists("".$adirectory."/".$user_avatar."")) ? "<img src=\"".$adirectory."/".$user_avatar."\" align=\"left\" alt=\"$sender_name\" title=\"$sender_name\" />" : "<img src=\"".$adirectory."/00.gif\" align=\"left\" alt=\"$sender_name\" title=\"$sender_name\" />";
			$user_sig = ($user_sig) ? "<hr>".bb_decode($user_sig)."" : "";
			echo "<tr class=\"bgcolor2\"><td align=\"center\" valign=\"top\">".$user_avatar."</td><td valign=\"top\">".bb_decode($com_text)."".$user_sig."</td></tr>";
			if ($i < ($comnum-1)) echo "<tr class=\"bgcolor3\"><td height=\"3px\" colspan=\"2\"></td></tr>";
			$a--;
			$i++;
		}
		echo "</table>";
		CloseTable();
		if ($module_name == "Files") {
			num_page($module_name, $numstories, $numpages, $comnum, "op=view_file&lid=".$cid."&");
		} elseif ($module_name == "Links") {
			num_page($module_name, $numstories, $numpages, $comnum, "op=view_link&lid=".$cid."&");
		} elseif ($module_name == "Voting") {
			num_page($module_name, $numstories, $numpages, $comnum, "op=poll_result&poll_id=".$cid."&");
		} elseif ($module_name == "News") {
			num_page($module_name, $numstories, $numpages, $comnum, "op=article&sid=".$cid."&");
		} elseif ($module_name == "Media") {
			num_page($module_name, $numstories, $numpages, $comnum, "op=view_file&lid=".$cid."&");
		} 
	}
	if (!is_user($user) && $anonpost == 0) {
		OpenTable();
        echo "<center><font class=\"option\">"._NOANONCOMMENTS."</font></center>";
        CloseTable();
	} else {
		OpenTable();
		echo "<table border=\"0\" cellpadding=\"3\" cellspacing=\"0\" align=\"center\"><form name=\"post\" action=\"index.php?name=$module_name\" method=\"post\">";
		if (is_user($user)) {
			$uname = htmlspecialchars(substr($user[1], 0, 25));
			echo "<tr><td>"._YOURNAME.":</td><td>".$uname."</td></tr>";
		} else {
			echo "<tr><td>"._YOURNAME.":</td><td><input type=\"text\" name=\"postname\" size=\"65\" style=\"width:400px\"></td></tr>";
		}
		echo "<tr><td>"._COMMENT.":</td><td><textarea id=\"area\" name=\"comment\" cols=\"65\" rows=\"5\" style=\"width:400px\" OnKeyPress=\"TransliteFeld(this, event)\" OnSelect=\"FieldName(this, this.name)\" OnClick=\"FieldName(this, this.name)\" OnKeyUp=\"FieldName(this, this.name)\"></textarea>".get_code("area")."</td></tr>"
		."<tr><td colspan=\"2\" align=\"center\"><br /><input type=\"hidden\" name=\"cid\" value=\"$cid\"><input type=\"hidden\" name=\"op\" value=\"SaveComments\"><input type=\"submit\" value=\""._COMMENTREPLY."\"></td></tr></form></table>";
		CloseTable();
	}
	foot();
}

function SaveComments($cid, $postname, $comment) {
	global $prefix, $db, $user, $userinfo, $module_name;
	require_once("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 > $comletter) $stop = ""._CERROR2."";
	if ($postname == "" && !is_user($user)) $stop = ""._CERROR3."";
	if (!$stop) {
		if (is_user($user)) {
			getusrinfo($user);
			$postid = intval($userinfo['user_id']);
			$postname = "";
		} else {
			$postid = "";
			$postname = text_filter(substr($postname, 0, 25));
		}
		$cid = intval($cid);
		$ip = getenv("REMOTE_ADDR");
		$comment = nl2br(text_filter($comment, 2));
		$db->sql_query("INSERT INTO ".$prefix."_comment VALUES (NULL, '$cid', '$module_name', now(), '$postid', '$postname', '$ip', '$comment')");
		if ($module_name == "Files") {
			update_points(17);
			$db->sql_query("UPDATE ".$prefix."_files SET totalcomments=totalcomments+1 WHERE lid='$cid'");
			Header("Location: index.php?name=$module_name&op=view_file&lid=$cid");
		} elseif ($module_name == "Links") {
			update_points(14);
			$db->sql_query("UPDATE ".$prefix."_links SET totalcomments=totalcomments+1 WHERE lid='$cid'");
			Header("Location: index.php?name=$module_name&op=view_link&lid=$cid");
		} elseif ($module_name == "Voting") {
			update_points(9);
			$db->sql_query("UPDATE ".$prefix."_survey SET pool_comments=pool_comments+1 WHERE poll_id='$cid'");
			Header("Location: index.php?name=$module_name&op=poll_result&poll_id=$cid");
		} elseif ($module_name == "News") {
			update_points(5);
			$db->sql_query("UPDATE ".$prefix."_stories SET comments=comments+1 WHERE sid='$cid'");
			Header("Location: index.php?name=$module_name&op=article&sid=$cid");
		} elseif ($module_name == "Blogs") {
			update_points(5);
			$db->sql_query("UPDATE ".$prefix."_blogs SET totalcomments=totalcomments+1 WHERE sid='$id'");
			Header("Location: index.php?name=$module_name&op=blogid&pid$cid");
		} elseif ($module_name == "Media") {
			update_points(17);
			$db->sql_query("UPDATE ".$prefix."_media SET totalcomments=totalcomments+1 WHERE lid='$cid'");
			Header("Location: index.php?name=$module_name&op=view_file&lid=$cid");
		}

	} else {
		head();
		if ($module_name == "Files") {
			title(""._FILE_LOGO."");
		} elseif ($module_name == "Links") {
			title(""._LINK_LOGO."");
		} elseif ($module_name == "Voting") {
			title(""._SURVEYS_LOGO."");
		} elseif ($module_name == "News") {
			title(""._NEWS."");
		} elseif ($module_name == "Blogs") {
			title(""._Blogs."");
		} elseif ($module_name == "Media") {
			title("Медиа каталог");
		}
		warning("".$stop."<br /><br />"._GOBACK."", "", "", 1);
		foot();
	}
}

switch($op) {
	case "SaveComments":
	SaveComments($cid, $postname, $comment);
	break;
}
?>