<?php

require '../system/start.php';
require '../system/functions.php';
$str['mesto'] = 'Комментарии';
$str['block'] = $set['site'].' / Комментарии';
include '../style/'.$style.'/head.php';
$q = mysql_fetch_assoc(mysql_query("select * from `news` where `id` = ".int($_GET['id']).""));
if(empty($q['title']))
{
    echo '<div class="where">Новости не существует!</div>';
    include '../style/'.$style.'/foot.php';
    exit;
}
if(isset($_GET['act']) && htmlspecialchars(trim($_GET['act'])) == 'delete')
{
	if($user['level'] > 1)
	{
		mysql_query ("DELETE FROM `news_comments` WHERE `id` = ".int($_GET['pid'])."");
		echo '<div class="where">Комментарий успешно удален!</div>';
	}
	else
	{
		echo '<div class="where">У вас недостаточно прав!</div>';
	}
}
if(isset($_POST['text']) && !empty($_POST['text']))
{
    $flood = mysql_fetch_assoc(mysql_query("select * from `news_comments` where `id_user` = ".$user['id']." order by `time` desc limit 1"));
    if($flood['time']<(time()-30))
    {
        $text = protect($_POST['text']);
        mysql_query("update `users` set `balls`=`balls`+1 where `id` = ".$user['id']."");
        mysql_query ("INSERT INTO `news_comments` (id_news,id_user,text,time) VALUES (".$q['id'].",".$user['id'].",'".$text."',".time().")");
        echo '<div class="mess">Комментарий успешно добавлен!</div>';
    }
    else
    {
        echo '<div class="mess">Антифлуд 30 секунд!</div>';
    }
}
echo '<div class="mess"><form action="comments.php?id='.$q['id'].'" method="post"><div><b>Сообщение:</b><br/><textarea name="text"></textarea><br/><input type="submit" value="Написать"/></div></form></div>';
$query = mysql_query("select * from `news_comments` where `id_news` = ".$q['id']." order by `id` desc limit ".$page.",".$set['str']."");
$total = mysql_result(mysql_query("select count(*) from `news_comments` where `id_news` = ".$q['id'].""),0);
if ($total == 0)
{
    echo '<div class="forum1">Комментариев еще нет! Будь первым!</div>';
}
while ($row = mysql_fetch_assoc($query))
{
    $i++;
	if($i%2)
	{
		echo '<div class="forum1">';
	} else
	{
		echo '<div class="forum2">';
	}
    $us = mysql_fetch_assoc(mysql_query("select * from `users` where `id` = ".$row['id_user'].""));
    echo '<img class="ico" src="../style/default/ico/personal.gif" alt="" /> <a href="../p/'.$us['login'].'">'.$us['login'].'</a>';
    echo status($us['id']);
    echo '['.clock($row['time']).']<div class="ftext">Сообщение: '.text($row['text']).'</div>';
    if($user['level'] > 1)
    {
        echo '<a href="comments.php?act=delete&amp;id='.$q['id'].'&amp;pid='.$row['id'].'">[удалить]</a>';
    }
    echo '</div>';
}
if($total > $set['str'])
{
	pages($page, $total, 'comments.php?id='.$q['id'].'&amp;', $set['str']);
}
echo '<div class="where">

<a href="../pages/info.php?act=smiles">Смайлы</a> |

<a href="../pages/info.php?act=bbcode">BBcode</a> |

<a href="index.php">К новостям</a><br/>
</div>';
include '../style/'.$style.'/foot.php';
?>