<?
include_once "../SYSTEM/include/functions.php";
include_once "../SYSTEM/include/mysql.php";
include_once "../SYSTEM/include/param.php";
include_once "../SYSTEM/include/ban.php";
include_once "../SYSTEM/include/sess.php";
include_once "../SYSTEM/include/ban_2.php";
include_once "../SYSTEM/include/theme.php";
$title='Комментарии';
$mesto='Читает комменты новостей';
head();


if (isset($_GET['id']))
{
$id_news=intval($_GET['id']);
$q = mysql_query("SELECT COUNT(*) FROM `$mysql[pref]news` WHERE `id` = '$id_news';");
if (mysql_result($q, 0)==0)
{
$q45 = mysql_query("SELECT MAX(id) FROM `$mysql[pref]news`;");
$max_id_news=mysql_fetch_array($q45);
$id_news=$max_id_news[0];
}
}
else
{

$q44 = mysql_query("SELECT MAX(id) FROM `$mysql[pref]news`;");
$max_id_news=mysql_fetch_array($q44);
$id_news=$max_id_news[0];
}


if (isset($_GET['act']) && $_GET['act']=='add' && isset($_POST['msg']))
{

if (!isset($user))
{
header ("Location: index.php?".SID."");
exit;
}




$msg=$_POST['msg'];
$msg = iconv('utf-8', 'windows-1251', $msg);
$msg=substr($msg, 0, 512);
$msg = iconv('windows-1251', 'utf-8', $msg);
$msg=stripcslashes(htmlspecialchars($msg));
$msg=str_replace("\r\n","<br />",$msg);
$msg=str_replace("\r","<br />",$msg);
$msg=str_replace("\n","<br />",$msg);

if (isset($_POST['tr']))
{
$msg=translit($msg);
}


$msg=eregi_replace("((https?|ftp)://[[:alnum:]_=/-]+(\\.[[:alnum:]_=/-]+)*(/[[:alnum:]+&amp;._=/~%#]*(\\?[[:alnum:]?+&amp;_=/%#]*)?)?)", "<a href='\\1'>\\1</a>", $msg);


if ($msg=='')
{
header("Location: index.php?".SID."");
exit;
}


$time_q=$time-30;
$q = mysql_query("SELECT COUNT(*) FROM `$mysql[pref]news_komm` WHERE `time` > '$time_q' AND `text` = '$msg' AND `id_n` = '$id_news';");
$repeat=mysql_result($q, 0);
if ($repeat==0)
{


mysql_query("INSERT INTO `$mysql[pref]news_komm` (id_n, nick, time, text, id_user)
values('$id_news' ,'$user[nickname]', '$time', '$msg', '$user[id]')");
ball_add();
}

header("Location: komm.php?".SID."&page=end&id=$id_news");
exit;
}













echo "<div class=\"h\">Комментарии новостей</div><hr />\n";

if ($mail_in_new>0)echo "<div class=\"privat\"><img src=\"../SYSTEM/themes/$them/img/mail0.gif\" alt=\"\" /> <a href=\"../mail.php?act=in\">Приват</a> [$mail_in_new]</div><hr />\n";






$q = mysql_query("SELECT * FROM `$mysql[pref]news_komm` WHERE `id_n` = '$id_news';");

$k_post=0;
$k_page=1;

while ($posts = mysql_fetch_array($q))
{
$post[$k_post]=$posts;
$k_post++;
}
if (isset($user))
{
$k_p_str=$user['k_page'];
}
else
{
$k_p_str=$set['k_p_s'];
}
if (isset ($post) && count($post)!="0")
{
$v_pages_okr=count($post)/$k_p_str%100;
$v_pages_tochn=count($post)/$k_p_str;
if ($v_pages_okr==$v_pages_tochn)
{
$k_page=$v_pages_okr;
}
else
{
$k_page=$v_pages_okr+1;
}


//echo "Количество страниц: $k_page<br />";
//echo "Количество сообщений: ".count($post)."<br /><br />";
$page=1;
if (isset($_GET['page']) && $_GET['page']!='')
{
if ($_GET['page']=='end')
{
$page=$k_page;
}
else
{
$page=intval($_GET['page']);
}
}
$page=intval($page);
if ($k_page<$page){$page=$k_page;}
if (0>$page){$page=1;}

usort ($post, "stime");



for ($i=$k_p_str*$page-$k_p_str;$i<$k_post && $i<$k_p_str*$page;$i++)
{
$timep=vremja($post[$i]['time']+$timesdvig);

$post_user_id=$post[$i]['id_user'];
$q = mysql_query("SELECT * FROM `$mysql[pref]users` WHERE `id` = '$post_user_id';");
$stat='';

$on="<span style=\"color:$color[off]\">off</span>";

if ($post_user_id!=0)
{
$post_user = mysql_fetch_array($q);

if ($post_user['level']==1)
{
$stat=" <span style=\"color:$color[level]\">mod</span>";
}
if ($post_user['level']==2)
{
$stat=" <span style=\"color:$color[level]\">smd</span>";
}
if ($post_user['level']==3)
{
$stat=" <span style=\"color:$color[level]\">adm</span>";
}
if ($post_user['level']==4)
{
$stat=" <span style=\"color:$color[level]\">adm</span>";
}

if ($post_user['time']>$time-$set['time_sess'])
{
$on="<span style=\"color:$color[on]\">on</span>";
}

}
echo "<div class=\"mess\">\n";
if (isset($user) && $post_user_id!=0)
{

if (isset($user) && ($web_browser==0 && $user['show_avatar']==1 || $web_browser==1 && $user['show_avatar2']==1) && $post_user['avatar']!='' && is_file("../SYSTEM/images/avat/$post_user[avatar]"))
echo "<img src=\"../SYSTEM/images/avat/$post_user[avatar]\" height=\"32\" width=\"32\" alt=\"Аватар\" />\n";
else
echo "<img src=\"../SYSTEM/themes/$them/img/user/pol_$post_user[pol].gif\" alt=\"\" />";
echo " <a href=\"../info.php?id=$post_user_id\"><span style=\"color:$color[mess_nick]\"><b>".$post[$i]['nick']."</b></span></a>$stat $on";
}
else
{
if ($post_user_id==0)
{
$on='';
}
echo "<img src=\"../SYSTEM/themes/$them/img/user/pol_$post_user[pol].gif\" alt=\"\" />";
echo " <span style=\"color:$color[user]\"><b>".$post[$i]['nick']."</b></span> $stat $on";
}

echo "<br />\n";
echo "<span style=\"color:$color[post_time]\">[$timep]</span>";
echo "<br />\n";


if ($post_user['level']>0)
echo "<span style=\"color:$color[post]\">".admin_smiles(smiles($post[$i]['text']))."</span>";
else
echo "<span style=\"color:$color[post]\">".smiles($post[$i]['text'])."</span>";

echo "</div>\n";
echo "<br />\n";
}
}
else
{
echo "Комментариев нет<br />\n";
}
echo "<hr />\n";
echo "<div class=\"str\">";
str("komm.php?id=$id_news&amp;",$k_page,$page);
echo "</div>\n";
echo "<hr />\n";
if (isset($user))
{
echo "<form method=\"post\" action=\"komm.php?act=add&amp;page=$page&amp;id=$id_news\">\n";

echo "Комментарий:<br />\n<textarea name=\"msg\" class=\"form\" rows=\"3\"></textarea><br />\n";
echo "<input type=\"checkbox\" class=\"form\" name=\"tr\" value=\"1\" /> Транслит<br />\n";
echo "<input value=\"Добавить\" class=\"form\" type=\"submit\" />\n";

echo "</form>";
}


echo "<a href=\"../index.php\">&lt;&lt; На главную</a>\n";

foot();
?>
