<?php
defined('_IN_JOHNCMS') or die('Error: restricted access');
if($id) {
	$query = mysql_query("SELECT * FROM `cms_news` WHERE `id`='$id' LIMIT 1;");
	if (mysql_num_rows($query)) {
		$res1 = mysql_fetch_assoc($query);
		if(empty($_SESSION['error']))
		$_SESSION['error'] = '';
		$textl = $lng['news'] . ' | ' . $lng_news['comments_on_news'] . ' "' . htmlentities($res1['name'], ENT_QUOTES, 'UTF-8') . '"';
		require_once('../incfiles/head.php');
		//echo '<div class="phdr">' . $lng_news['comments_on_news'] . ' "<a href="index.php?act=view&amp;id=' . $id . '">' . htmlentities($res1['name'], ENT_QUOTES, 'UTF-8') . '</a>"</div>';
$balans='5';// 1 - bu 1 tanga berish degani

mysql_query("UPDATE `users` SET `balans`='".($datauser['balans']+$balans)."' WHERE `id` = '$user_id'");
		
		$com = isset($_REQUEST) ? abs(intval($_REQUEST['com'])) : '';
		
		switch($mod) {
			case 'replay':
				if($rights >= 7) {
					$q = mysql_query("SELECT * FROM `cms_news_comments` WHERE `id`='$com' LIMIT 1;");
					if (mysql_num_rows($q)) {
						$r = mysql_fetch_assoc($q);
						if(isset($_POST['submit'])) {
							$text = isset($_POST['text']) ? trim($_POST['text']) : '';
							$error = array();
							if(!$text)
								$error[] = $lng_news['message_not_empty'] . '!';
							elseif (mb_strlen($text) < 4 || mb_strlen($text) > 5000)
								$error[] = $lng_news['message_long_error'] . '!';
							$flood = functions::antiflood();
							if($flood)
								$error[] = $lng['error_flood'] . ' ' . $flood . $lng['sec'];
							
							if(empty($error)) {
								mysql_query("UPDATE `cms_news_comments` SET
								`reply` = '[b]" . $login . "[/b]:" . mysql_real_escape_string($text) . "' WHERE `id`='$com'");
								mysql_query("UPDATE `users` SET
									`lastpost` = '" . time() . "'
									WHERE `id` = '$user_id'
								");
								$_SESSION['error'] = '<div class="menu">' . $lng_news['added_message'] . '</div>';
							} else {
								$_SESSION['error'] = '<div class="menu">' . implode('<br />', $error) . '</div>';
							}
							Header('Location: index.php?act=view&id=' . $id);
							exit;
						}
						echo $_SESSION['error'] . '<div class="menu">
						<form action="index.php?act=comments&amp;mod=replay&amp;id=' . $id . '&amp;com=' . $com . '" method="post"  enctype="multipart/form-data"><div class="menu">
						<b>Izoh qoldirish:</b><br/>
						<textarea rows="3" name="text">' . htmlentities($r['reply'], ENT_QUOTES, 'UTF-8') . '</textarea>
						<br /><span style="font-size: x-small;">' . $lng_news['recommended_message'] . '</span><br />
						<input type="submit" name="submit" value="' . $lng['reply'] . '"/>
						</div></form>
						</div>';
					} else {
						echo functions::display_error($lng_news['message_does_not_exist'] . '!');
					}
				} else {
					Header('Location: ../?err');
					exit;
				}
			break;
			
			case 'delete':
				if($rights >= 7) {
					$q = mysql_query("SELECT * FROM `cms_news_comments` WHERE `id`='$com' LIMIT 1;");
					if (mysql_num_rows($q)) {
						if(isset($_POST['submit'])) {
							mysql_query("DELETE FROM `cms_news_comments` WHERE `id`='$com'");
							$_SESSION['error'] = '<div class="menu">' . $lng_news['message_deleted'] . '</div>';
							Header('Location: index.php?act=view&id=' . $id);
							exit;
						}
						echo $_SESSION['error'] . '<div class="menu">
						<form action="index.php?act=comments&amp;mod=delete&amp;id=' . $id . '&amp;com=' . $com . '" method="post"  enctype="multipart/form-data"><div>
						' . $lng_news['confirm_the_deletion'] . '<br />
						<input type="submit" name="submit" value="' . $lng['delete'] . '"/>
						</div></form>
						</div>';
					} else {
						echo functions::display_error($lng_news['message_does_not_exist'] . '!');
					}
				} else {
					Header('Location: ../?err');
					exit;
				}
			break;
			
			default:
			if($user_id && empty($ban[1]) ) {
				if(isset($_POST['submit'])) {
					$text = isset($_POST['text']) ? trim($_POST['text']) : '';
					$error = array();
					if(!$text)
						$error[] = $lng_news['message_not_empty'] . '!';
					elseif (mb_strlen($text) < 4 || mb_strlen($text) > 5000)
						$error[] = $lng_news['message_long_error'] . '!';
					$flood = functions::antiflood();
					if($flood)
						$error[] = $lng['error_flood'] . ' ' . $flood . $lng['sec'];
					
					if(empty($error)) {
						mysql_query("INSERT INTO `cms_news_comments` SET
						`refid` = '$id',
						`time` = '" . time() . "',
						`user_id` = '" . $user_id . "',
						`text` = '" . mysql_real_escape_string($text) . "';");
						mysql_query("UPDATE `users` SET
							`lastpost` = '" . time() . "'
							WHERE `id` = '$user_id'
						");
						$_SESSION['error'] = '<div class="gmenu">' . $lng_news['added_message'] . '</div>';
					} else {
						$_SESSION['error'] = '<div class="menu">' . implode('<br />', $error) . '</div>';
					}
					Header('Location: index.php?act=view&id=' . $id);
					exit;
				}
				echo $_SESSION['error'] . '<div class="menu">
				<form action="index.php?act=comments&amp;id=' . $id . '" method="post"  enctype="multipart/form-data"><div>
				<b>Fikr bildirish:</b><br/>
				<textarea rows="3" name="text">' . (!empty($_POST['text']) ? htmlentities($_POST['text'], ENT_QUOTES, 'UTF-8') : '') . '</textarea>
				<br />
				<input type="submit" name="submit" value="' . $lng['sent'] . '"/>
				</div></form>
				</div>';
			}
			//echo '<div class="gmenu">Fikrlar</div>';
			$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `cms_news_comments` WHERE `refid`='$id';"), 0);
			if($total) {
				if ($total > $kmess)
					//echo '<div class="topmenu">' . functions::display_pagination('index.php?act=comments&amp;id=' . $id . '&amp;', $start, $total, $kmess) . '</div>';
				$i = 1;
				$req = mysql_query("SELECT `cms_news_comments`.*, `cms_news_comments`.`time` as `mtime`, `cms_news_comments`.`id` as `mid`, `users`.* FROM `cms_news_comments` LEFT JOIN `users` ON `cms_news_comments`.`user_id`=`users`.`id` WHERE `cms_news_comments`.`refid`='$id' ORDER BY `cms_news_comments`.`time` DESC LIMIT "
						. $start . "," . $kmess);
				while (($row = mysql_fetch_assoc($req)) !== false) {
					echo $i % 2 ? '<div class="list1">' : '<div class="list2">';
					$post = $row['text'];
					$post = functions::checkout($post, 1, 1);
					if ($set_user['smileys'])
						$post = functions::smileys($post, $row['rights'] >= 1 ? 1 : 0);
					if($row['reply'])
$post .= '<div class="phdr">' . functions::checkout($row['reply'], 1, 1) . '</div>';
					if($rights >= 7) $subtext = '<a href="index.php?act=comments&amp;mod=replay&amp;id=' . $id . '&amp;com=' . $row['mid'] . '">' . $lng['reply']. '</a> | <a href="index.php?act=comments&amp;mod=delete&amp;id=' . $id . '&amp;com=' . $row['mid'] . '">' . $lng['delete']. '</a>';
					else $subtext = '';
					$text = ' <span class="gray">(' . functions::display_date($row['mtime']) . ')</span>';
					$arg = array(
						'header' => $text,
						'body' => $post,
						'sub' => $subtext
					);
					echo functions::display_user($row, $arg);
					echo '</div>';
					++$i;
				}
				//echo '<div class="phdr">' . $lng['total'] . ': ' . $total . '</div>';
				if ($total > $kmess) {
					echo '<div class="topmenu">' . functions::display_pagination('index.php?act=view&amp;id=' . $id . '&amp;', $start, $total, $kmess) . '</div>';
					echo '<p><form action="index.php" method="get">
					<input type="hidden" name="act" value="comments"/>
					<input type="hidden" name="id" value="' . $id . '"/>
					<input type="text" name="page" size="2"/>
					<input type="submit" value="' . $lng['to_page'] . ' &gt;&gt;"/></form></p>';
				}
				
} if ($user_id) {  
echo "<div  class='menu'>Unutmang bemani izoqlar yozmang!</div>";
 } else { 
echo "<div class='menu'><center>Izoh qoldirish uchun saytga foydalanuvchi sifatida tashrif buyuring.<br>[ <a href='" . $set['homeurl'] . "/registration.php'>Ro'yhatdan o'tish</a> | ";
echo "<a href='" . $set['homeurl'] . "/login.php'>Kirish</a> ]</center></div>";
}

		}
		//echo '<div class="bmenu"><a href="index.php?act=view&amp;id=' . $id . '">' . $lng_news['back'] . '</a></div>';
	} else {
		$textl = $lng['news'];
		require_once('../incfiles/head.php');
		echo functions::display_error($lng_news['news_does_not_exist']);
	}
} else {
	$textl = $lng['news'];
	require_once('../incfiles/head.php');
}