<?php
define('NTLCMS', 1);
$module = 'comment';
include ('../system/start.php');
include ('../system/function.php');
include ('../system/antidos.php');
include ('../system/head.php');
if ($set['news'] == '1' && $_USER['admin'] != '1'){
echo '<div class="title"><b>'.razd.' Новости системы</b></div>';
echo '<div class="post"><center><i class="fa fa-close fa-fw"></i> Модуль закрыт администратором!</center></div>';
echo '<div class="block"><i class="fa fa-home fa-fw"></i> <a href="'.URL.'/cabinet">В кабинет</a></div>';
include ('../system/foot.php');
exit;
}
$id = isset($_GET['id']) ? filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT) : '0';
if (!DB :: run() -> querySingle("select count(`id`) from `news` where `id` = ?", array($id))){
$_SESSION['fail'] = 'Новость не существует!';
header('Location: '.URL.'/feed');
exit;
} else {
$k_post = DB :: run() -> querySingle("select count(`id`) from `comment` where `pid` = ?", array($id));
$k_page = k_page($k_post, $onpage); 
$page = page($k_page); 
$start = $onpage * $page - $onpage;
$sql = DB :: run() -> query("select * from `comment` where `pid` = ? order by `id` desc limit ".$start.", ".$onpage, array($id));
$room = DB :: run() -> query("select (`name`) from `news` where `id` = ?", array($id)) -> fetch(PDO::FETCH_OBJ);
echo '<div class="title"><b>'.razd.' Новости - '.checkout($room -> name).'</b></div>';
$error = null;
if (isset($_POST['text'])){
$text = isset($_POST['text']) ? trim($_POST['text']) : '';
$text = preg_replace('/[\x00-\x1F\x7F]/u', '', $text);
$text = preg_replace('/[\p{Mn}\p{Me}]+/u', '', $text);
$text = preg_replace('/[\x{200B}-\x{200F}\x{202A}-\x{202E}\x{FEFF}]/u', '', $text);
$text = preg_replace('/\s{2,}/u', ' ', $text);
$text = htmlspecialchars($text, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
$user_clicks = DB :: run() -> querySingle("select `click_all` from `users` where `login` = ?", array($ulogin));
$min_clicks_for_comment = isset($set['min_clicks_comment']) ? (int)$set['min_clicks_comment'] : 100;
if ((int)$user_clicks < $min_clicks_for_comment && $_USER['admin'] != '1') {
$error.= 'Для комментирования необходимо иметь минимум '.$min_clicks_for_comment.' кликов!<br/>';
$error.= 'У вас: '.(int)$user_clicks.' кликов<br/>';
}
if (empty($text)){
$error.= 'Введите текст комментария!<br/>';
} elseif (mb_strlen($text, 'UTF-8') < 2 || mb_strlen($text, 'UTF-8') > 500){
$error.= 'Комментарий должен содержать от 2 до 500 символов!<br/>';
} elseif(DB :: run() -> querySingle("select (`date`) from `comment` where `login` = ? and `date` > ?", array($ulogin, TIME - $set['antispam']))){
$error.= 'Антиспам! Лимит '.ints($set['antispam']).' сек!<br/>';
}
if($error){
echo '<div class="post"><center><i class="fa fa-close fa-fw"></i> '.$error.'</center></div>';
} else {
if (DB :: run() -> query("insert into `comment` (`date`, `pid`, `login`, `text`) values (?, ?, ?, ?)", array(TIME, $id, $ilogin, $text))){
$_SESSION['success'] = 'Комментарий успешно добавлен!';    
header('Location: '.URL.'/feed_comment'.$id);
exit;
} else {
$_SESSION['fail'] = 'Ошибка при добавлении комментария!';    
header('Location: '.URL.'/feed_comment'.$id);
exit;
}
}
}
if ($active){
$user_clicks = DB :: run() -> querySingle("select `click_all` from `users` where `login` = ?", array($ulogin));
$min_clicks_for_comment = isset($set['min_clicks_comment']) ? (int)$set['min_clicks_comment'] : 100;
echo '<div class="post"><center>';
echo '<i class="fa fa-info-circle fa-fw"></i> Ваши клики: <b>'.(int)$user_clicks.'</b>';
echo ' | Необходимо: <span style="color:red"><b>'.$min_clicks_for_comment.'</b></span>';
if ((int)$user_clicks < $min_clicks_for_comment && $_USER['admin'] != '1') {
} else {
echo ' | <span style="color:green">Можно</span>';
}
echo '</center></div>';
if ((int)$user_clicks >= $min_clicks_for_comment || $_USER['admin'] == '1') {
echo '<div class="menu"><form name="form" action="'.URL.'/feed_comment'.$id.'" method="post">';
echo fast_smile() . panel('text').'<textarea name="text" rows="3" cols="20">'.(isset($_POST['text']) ? htmlspecialchars($_POST['text']) : '').'</textarea><br/>
<input type="submit" value="Добавить комментарий"/></form></div>';
}
}
}
if ($k_post == 0){
echo '<div class="post"><center><i class="fa fa-close fa-fw"></i> Комментариев еще нет!</center></div>';
} else {
while ($row = $sql -> fetch(PDO::FETCH_OBJ)){
if ($_USER['admin'] == '1'){ $delete = '[<a href="'.URL.'/feed_delete'.$id.'_'.ints($row -> id).'">D</a>]'; }
if (!$active || $ulogin == $row -> login){ $answer = NULL; } else { $answer = '[<a href="#form" onclick="javascript:tag(\' [b]'.checkout($row -> login).'[/b], \', \'\');">Отв</a>]'; }
echo '<div class="menu">';
echo '<table><tr>';
echo '<td>';
echo ava($row -> login).' '.login($row -> login, 1).' '.online($row -> login).' ('.date('d.m.Y H:i', $row -> date).') '.$delete.'';
echo '<br/><i class="fa fa-file-text-o fa-fw"></i> '.outtext($row -> text, 1, 1, 1).'  '.$answer.'</div>';
echo '</td>';
echo '</tr></table>';
echo '</div>';
}
if($k_page > 1) navigation(URL.'/feed_comment'.$id.'?', $k_page, $page);
}
echo '<div class="block"><i class="fa fa-home fa-fw"></i> '.(isset($active) ? '<a href="'.URL.'/cabinet">В кабинет</a>' : '<a href="'.URL.'">На главную</a>').' | <a href="'.URL.'/feed">Новости системы</a></div>';
include ('../system/foot.php');
?>