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

function ShowComments($cid) {
global $prefix, $db, $user, $module_name, $admin, $anonpost, $admin_file, $pagenum, $adirectory, $anonymous, $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);
$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_regdate, b.user_icq, b.user_sig, b.user_viewemail, b.user_aim, b.user_yim, b.user_msnm, b.user_gender 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");
while (list($id, $c_cid, $send_date, $sender_id, $sender_name, $sender_host, $com_text, $user_name, $user_email, $user_website, $user_avatar, $user_regdate, $sender_icq, $user_sig, $user_viewemail, $sender_aim, $sender_yim, $sender_msn, $user_gender) = $db->sql_fetchrow($result)) {
if ($user_gender == 1) {$gend = "<img border=\"0\" src=\"images/man.gif\" width=\"16\" height=\"16\" title=\""._MAN."\">";} elseif ($user_gender == 2) {$gend = "<img border=\"0\" src=\"images/woman.gif\" width=\"16\" height=\"16\" title=\""._WOMAN."\">";} else {$gend = "";}
$com_name = ($user_name) ? $user_name : (($sender_name) ? $sender_name : $anonymous);
$avatar = ($user_avatar && file_exists("".$adirectory."/".$user_avatar."")) ? "<img src=\"".$adirectory."/".$user_avatar."\" alt=\"$com_name\" title=\"$com_name\" />" : "<img src=\"".$adirectory."/00.gif\"  alt=\"$com_name\" title=\"$com_name\" />";
preg_match("/([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})/", $user_regdate, $datetime);
$user_regdate = ($user_regdate) ? "".$datetime[3].".".$datetime[2].".".$datetime[1]."" : "<i>"._NO_INFO."</i>";
$text = ($user_sig) ? "".bb_decode($com_text)."<hr>".bb_decode($user_sig)."" : "".bb_decode($com_text)."";
$user_point = ($conf['points'] && $user_points) ? ""._POINTS.": ".$user_points."" : "";
$name = "<a href=\"java script: InsertCode('name', '".$com_name."')\"><b>$com_name</b></a>";
$info = "<table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"left\">".$user_point."</td><td align=\"right\">"._DATE.": ".formatTimestamp($send_date)." | "._COMMENT.": $a</td></tr></table>";
$link = "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td align=\"left\">"._REG_DATE.": ".$user_regdate."</td><td align=\"right\">";
$link .= " " . $gend ." ";
$link .= ($user_name != "") ? "<a href=\"index.php?name=Account&op=info&uname=$user_name\" title=\""._PERSONALINFO."\"><img border=\"0\" src=\"images/info.gif\" width=\"16\" height=\"16\"></a> " : "";
$link .= ($user_website != "") ? "<a href=\"$user_website\" target=\"_blank\" title=\"$user_website\"><img border=\"0\" src=\"images/blocks/Home.gif\" width=\"16\" height=\"16\"></a> " : "";
$link .= ((is_admin($admin) || $user_viewemail == 1) && $user_email) ? "<a href=\"mailto:$user_email\" title=\"$user_email\"><img border=\"0\" src=\"images/blocks/Contact.gif\" width=\"16\" height=\"16\"></a> " : "";
$link .= ($sender_icq != "") ? "<a href=\"index.php?name=Account&op=info&uname=$user_name\" title=\"ICQ Number: $sender_icq\"><img border=\"0\" src=\"images/blocks/icq.gif\" width=\"16\" height=\"16\"></a> " : "";
$link .= ($sender_aim != "") ? "<a href=\"index.php?name=Account&op=info&uname=$user_name\" title=\"AIM Address: $sender_aim\"><img border=\"0\" src=\"images/blocks/aim.gif\" width=\"16\" height=\"16\"></a> " : "";
$link .= ($sender_yim != "") ? "<a href=\"index.php?name=Account&op=info&uname=$user_name\" title=\"Yahoo Messenger: $sender_yim\"><img border=\"0\" src=\"images/blocks/yim.gif\" width=\"16\" height=\"16\"></a> " : "";
$link .= ($sender_msn != "") ? "<a href=\"index.php?name=Account&op=info&uname=$user_name\" title=\"MSN Messenger: $sender_msn\"><img border=\"0\" src=\"images/blocks/msn.gif\" width=\"16\" height=\"16\"></a> " : "";
if (is_admin($admin)) {
$link .= "<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> <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> <a href=\"".$admin_file.".php?op=EditComments&id=$id\" title=\""._EDIT."\"><img border=\"0\" src=\"images/edit.gif\" width=\"16\" height=\"16\"></a> "
."<a href=\"http://www.dnsstuff.com/tools/whois.ch?ip=".$sender_host."\" title=\""._IP.": $sender_host\" target=\"_blank\"><img border=\"0\" src=\"images/help.gif\" width=\"16\" height=\"16\"></a>";
}
$link .= "</td></tr></table>";
comment($com_id, $name, $info, $avatar, $text, $rate, $link);
$a--;
}
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."&");
}
}
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>";


if (extension_loaded("gd")) {
echo"<tr><td>"._SECURITYCODE.":</td><td><img src=\"index.php?name=$module_name&gfx=gfx&random_nume=".time()."\" border=\"1\" alt=\""._SECURITYCODE."\"></td></tr>"
."<tr><td>"._TYPESECCODE.":</td><td><input type=\"text\" name=\"gfx_check\" size=\"10\" maxlength=\"6\"></td></tr>";
/*$random_num = gen_pass(10);
echo"<tr><td>"._SECURITYCODE.":</td><td><img src=\"index.php?name=$module_name&gfx=gfx&random_num=$random_num\" border=\"1\" alt=\""._SECURITYCODE."\"></td></tr>"
."<tr><td>"._TYPESECCODE.":</td><td><input type=\"text\" name=\"gfx_check\" size=\"10\" maxlength=\"6\"></td></tr>"
."<input type=\"hidden\" name=\"random_num\" value=\"$random_num\">";*/
}

echo "<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, $anonpost,$sitekey;
    require_once("config/config_comments.php");
    $e = explode(" ", $comment);
    for ($a = 0; $a < sizeof($e); $a++) $o = strlen($e[$a]);
session_start();
//if (!isset($_POST['gfx_check'])||empty($_POST['gfx_check'])||!isset($_POST['random_num'])||!($_POST['gfx_check']===substr(hexdec(md5("".date("F j")."".$_POST["random_num"]."".$sitekey."")), 2, 6)))
if (!isset($_POST['gfx_check'])||empty($_POST['gfx_check'])||!isset($_SESSION['gfx_num'])||!($_POST['gfx_check']===substr(hexdec(md5("".date("F j")."".$_SESSION['gfx_num']."".$sitekey."")), 2, 6)))
{
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."");
}
warning("Вы робот ? или вы просто капчу не заметили ? Введите пожалуйста еще раз...", "", "", 1);
OpenTable();
echo "<form name=\"post\" action=\"index.php?name=$module_name\" method=\"post\">";
//$random_num = gen_pass(10);
//echo"<center><img src=\"index.php?name=$module_name&gfx=gfx&random_num=$random_num\" border=\"1\" alt=\""._SECURITYCODE."\"><br /><br />";
echo"<center><img src=\"index.php?name=$module_name&gfx=gfx&random_nume=".time()."\" border=\"1\" alt=\""._SECURITYCODE."\"><br /><br />";
echo"<input type=\"text\" name=\"gfx_check\" size=\"10\" maxlength=\"6\" style=\"width:75px\"><br /><br /><input type=\"submit\" value=\""._COMMENTREPLY."\">";
//echo"<input type=\"hidden\" name=\"random_num\" value=\"$random_num\">";
if (!empty($postname)) {echo "<input type=\"hidden\" name=\"postname\" value=\"$postname\">";}
if (!empty($comment)) {echo "<input type=\"hidden\" name=\"comment\" value=\"$comment\">";}
echo "<br /><input type=\"hidden\" name=\"cid\" value=\"$cid\"><input type=\"hidden\" name=\"op\" value=\"SaveComments\"></form>";
CloseTable();
foot();

}else{
unset($_SESSION['gfx_num']);

    $stop = "";
    
    if ($comment == "") $stop = ""._CERROR1."";
    if ($o > $comletter) $stop = ""._CERROR2."";
    if ((!is_user($user) && $anonpost == 0) || ($postname == "" && !is_user($user))) $stop = ""._CERROR3."";


   //Строка, после которой вставим обработку еще одной ошибки
      if ($postname == "" && !is_user($user)) $stop = ""._CERROR3."";
      //Начало кода "Проверка имени Гостя"
//Если пришел Гость
   if (!is_user($user)) {

//Обнуляем переменную
      $stop_name = "";

//Проверяем, есть ли имя, написанное при комментарии в таблицах пользователей и админов
      $stop_name .= list($stop_name1) = $db->sql_fetchrow( $db->sql_query("SELECT user_id FROM ".$prefix."_users WHERE user_name = '$postname' ") );
      $stop_name .= list($stop_name1) = $db->sql_fetchrow( $db->sql_query("SELECT user_id FROM ".$prefix."_users_temp WHERE user_name = '$postname' ") );
      $stop_name .= list($stop_name1) = $db->sql_fetchrow( $db->sql_query("SELECT id FROM ".$prefix."_admins  WHERE name = '$postname' ") );

//Если есть совпадения, то выводим сообщение об ошибке.
         if ($stop_name != "") {
            $stop = "Ошибка!  <br>Имя $postname используется зарегистрированным пользователем. <br><br>Если Вы зарегистрированный пользователь, <a href=\"index.php?name=Account\">войдите</a> под своим Login'ом.";
         }
   }

//Конец кода "Проверка имени Гостя"


// Мин символов в сообщении


$maxsimbols='10'; //---Минимальная длина

$commentcheck = strip_tags(bb_decode(text_filter($comment, 2))); //------Обрезаем все тэги

$commentcheck = preg_replace('/ +/','',$commentcheck ); //----Удаляем все пробелы

if (strlen($commentcheck)<$maxsimbols) {$stop="Ошибка! Вы ввели слишком мало символов!<font color=red>";}

// Конец мин символов в сообщении

if (!$stop) {
if (is_user($user)) {
getusrinfo($user);
$postid = intval($userinfo['user_id']);
$postname = "";
list($last_time) =$db->sql_fetchrow($db->sql_query("SELECT UNIX_TIMESTAMP(date) FROM ".$prefix."_comment WHERE uid = '$postid'  ORDER BY date DESC LIMIT 1"));
} else {
$postid = "";
$postname = text_filter(substr($postname, 0, 25));
list($last_time) =$db->sql_fetchrow($db->sql_query("SELECT UNIX_TIMESTAMP(date) FROM ".$prefix."_comment WHERE name = '$postname'  ORDER BY date DESC LIMIT 1"));
}
$cek=120;
$cek2=time()-$last_time;
if($cek2<$cek)
{
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."");
}
warning("Извините у нас действует ограничение.<br /> Вы можете посылать не больше одного коментария в ".$cek." секунд. <br /> Подождите еще ".($cek-$cek2)." секeунд(у/ы) , введите код, и отошлите Ваш коментарий ", "", "", 1);
OpenTable();
echo "<form name=\"post\" action=\"index.php?name=$module_name\" method=\"post\">";
//$random_num = gen_pass(10);
//echo"<center><img src=\"index.php?name=$module_name&gfx=gfx&random_num=$random_num\" border=\"1\" alt=\""._SECURITYCODE."\"><br /><br />";
echo"<center><img src=\"index.php?name=$module_name&gfx=gfx&random_nume=".time()."\" border=\"1\" alt=\""._SECURITYCODE."\"><br /><br />";
echo"<input type=\"text\" name=\"gfx_check\" size=\"10\" maxlength=\"6\" style=\"width:75px\"><br /><br /><input type=\"submit\" value=\""._COMMENTREPLY."\">";
//echo"<input type=\"hidden\" name=\"random_num\" value=\"$random_num\">";
if (!empty($postname)) {echo "<input type=\"hidden\" name=\"postname\" value=\"$postname\">";}
if (!empty($comment)) {echo "<input type=\"hidden\" name=\"comment\" value=\"$comment\">";}
echo "<br /><input type=\"hidden\" name=\"cid\" value=\"$cid\"><input type=\"hidden\" name=\"op\" value=\"SaveComments\"></form>";
CloseTable();
foot();
}else{
$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");
}
}} 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."");
}
warning("".$stop."<br /><br />"._GOBACK."", "", "", 1);
foot();
}
}
}

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