<?
include_once '../sys/inc/start.php';
include_once '../sys/inc/compress.php';
include_once '../sys/inc/sess.php';
include_once '../sys/inc/home.php';
include_once '../sys/inc/settings.php';
include_once '../sys/inc/db_connect.php';
include_once '../sys/inc/ipua.php';
include_once '../sys/inc/fnc.php';
include_once '../sys/inc/user.php';
$set['title']='Новости - комментарии';
include_once '../sys/inc/thead.php';
title();





if (!isset($_GET['id']) && !is_numeric($_GET['id'])){header("Location: index.php?".SID);exit;}
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `blog_list` WHERE `id` = '".intval($_GET['id'])."' LIMIT 1",$db), 0)==0){header("Location: index.php?".SID);exit;}
$blog=mysql_fetch_array(mysql_query("select * from `blog_list` where `id`='".intval($_GET['id'])."';"));


if($blog['komm'] != 1)
{

if (isset($_POST['msg']) && isset($user))
{


$msg = trim(stripcslashes(htmlspecialchars(mysql_real_escape_string($_POST['msg']))));

if (isset($_POST['translit']) && $_POST['translit']==1)$msg=translit($msg);
if (strlen2($msg)>1024){$err='Сообщение слишком длинное';}
elseif (strlen2($msg)<2){$err='Короткое сообщение';}


elseif (mysql_result(mysql_query("SELECT COUNT(*) FROM `blog_komm` WHERE `id_blog` = '".intval($_GET['id'])."' AND `id_user` = '$user[id]' AND `msg` = '".mysql_real_escape_string($msg)."' LIMIT 1"),0)!=0){$err='Ваше сообщение повторяет предыдущее';}


else{
mysql_query("INSERT INTO `blog_komm` (`id_user`, `time`, `msg`, `id_blog`) values('$user[id]', '$time', '".mysql_real_escape_string($msg)."', '".intval($_GET['id'])."')");

if($user['id'] !==$blog['id_user'])
{

$msg_jurnal = stripcslashes(htmlspecialchars(trim(mysql_real_escape_string($_POST['msg_jurnal']))));

$msg_jurnal="Пользователь <a href=\"/info.php?id=$user[id]\">$user[nick]</a> оставил коментарий в твоем дневнике \"<a href=\"/blog/list.php?id=$blog[id]\">$blog[name]</a>\"";
mysql_query("INSERT INTO `blog_jurnal` (`msg`, `id_user`, `time`) values('$msg_jurnal', '$blog[id_user]', '".time()."')");
}





$q = mysql_query("SELECT * FROM `frends` WHERE `user` = '$user[id]' AND `lenta_komm` = '1' AND `i` = '1'");
while ($f = mysql_fetch_array($q))
{
$a = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '$f[frend]' LIMIT 1"));




$msgenta="Добавил комментарий [url=/blog/list.php?id=$blog[id] ]в дневниках [/url]";

mysql_query("INSERT INTO `lenta` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '$a[id]', '$msgenta', '$time')"); 
}






msg('Ваш комментарий успешно принят');


}
}
elseif (isset($_GET['del']) && mysql_result(mysql_query("SELECT COUNT(*) FROM `blog_komm` WHERE `id` = '".intval($_GET['del'])."' && `id_blog` = '".intval($_GET['id'])."'"),0))
{
if (isset($user) && ($user['level']>=4) || isset($user) && ($user['id']=$blog['id_user']))
{
mysql_query("DELETE FROM `blog_komm` WHERE `id` = '".intval($_GET['del'])."' LIMIT 1");
msg('Комментарий удален');
}

}

err();

aut(); // форма авторизации




$k_post=mysql_result(mysql_query("SELECT COUNT(*) FROM `blog_komm` WHERE `id_blog` = '".intval($_GET['id'])."'"),0);
$k_page=k_page($k_post,$set['p_str']);
$page=page($k_page);
$start=$set['p_str']*$page-$set['p_str'];
$q=mysql_query("SELECT * FROM `blog_komm` WHERE `id_blog` = '".intval($_GET['id'])."' ORDER BY `id` DESC LIMIT $start, $set[p_str]");

if ($k_post==0)
{
echo "   <tr>\n";
echo "  <td class='p_t'>\n";
echo "Нет комментариев\n";
echo "  </td>\n";
echo "   </tr>\n";
}
while ($post = mysql_fetch_assoc($q))
{
//$ank=mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = $post[id_user] LIMIT 1"));
$ank=get_user($post['id_user']);

echo "   <tr>\n";
if ($set['set_show_icon']==2){
echo "  <td class='icon48' rowspan='2'>\n";
avatar($ank['id']);
echo "  </td>\n";
}
elseif ($set['set_show_icon']==1)
{
echo "  <td class='icon14'>\n";

echo "  </td>\n";
}
if($num==1){ 
echo "<div class='rowdown'>"; 
$num=0;
}else{ 
echo "<div class='rowup'>"; 
$num=1;}
echo "".online($ank['id'])."<a href='/info.php?id=$ank[id]'><span style=\"color:$ank[color]\">$ank[nick]</span></a> (".vremja($post['time']).")\n";
echo "  </br>\n";

if ($set['set_show_icon']==1)


echo "<span style=\"color:$ank[scolor]\">\n";

echo esc(trim(br(bbcode(smiles(links(stripcslashes(htmlspecialchars($post['msg']))))))))."<br />\n";
echo "</span>";


if (isset($user) && ($user['level']>=4) || isset($user) && ($user['id'] == $blog['id_user']))
echo "<a href='?id=".intval($_GET['id'])."&amp;del=$post[id]'>Удалить</a><br />\n";


echo"</div>\n";
echo "  </td>\n";
echo "   </tr>\n";
}
echo "</table>\n";


if ($k_page>1)str("komm.php?id=".intval($_GET['id']).'&amp;',$k_page,$page); // Вывод страниц



if (isset($user))
{
echo "<form method=\"post\" name='message' action=\"?id=".intval($_GET['id'])."&amp;page=$page\">\n";
if ($set['web'] && is_file(H.'style/themes/'.$set['set_them'].'/altername_post_form.php'))
include_once H.'style/themes/'.$set['set_them'].'/altername_post_form.php';
else
echo "Сообщение:<br />\n<textarea name=\"msg\"></textarea><br />\n";
if ($user['set_translit']==1)echo "<label><input type=\"checkbox\" name=\"translit\" value=\"1\" /> Транслит</label> <a><a href='/smiles.php'>[Смайлы]</a><br />\n";
echo "<input value=\"Отправить\" type=\"submit\" />\n";
echo "</form>\n";
}

}
else
{
echo"Комментарии запрещены";
}

echo"<div class='foot'>\n";
echo"<a href='list.php?id=".intval($_GET['id'])."'>Назад</a><br />\n";
echo"<a href='index.php'>Дневники</a><br />\n";
echo"</div>\n";
include_once '../sys/inc/tfoot.php';
?>