<?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'] = '';
		$text = ' <span class="gray" style="font-size: 15px;">&#160;'.functions::image('clock.png').'&#160;' . functions::display_date($row['mtime']) . '</span>';
		require_once('../incfiles/head.php');
		echo '<div class="phdr">' . $lng_news['commenting_news'] . '</div>';
		
		$com = isset($_REQUEST) ? abs(intval($_REQUEST['com'])) : '';
		
		switch($mod) {
			case 'replay':

			if ($user_id) {
						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` = '" . $datauser['id'] . "',
						        `text` = '". mysql_real_escape_string($text) . "'");
								mysql_query("UPDATE `users` SET
									`lastpost` = '" . time() . "',
									`balans`='" . ($datauser['balans'] + 3) . "'
									
									WHERE `id` = '$user_id'
								");
								$_SESSION['error'] = '<div class="gmenu">' . $lng_news['added_message'] . '</div>';
							} else {
								$_SESSION['error'] = '<div class="rmenu">' . implode('<br />', $error) . '</div>';
							}
							Header('Location: /news_' . $id.'.html');
							exit;
						}
						$usernick = mysql_fetch_array(mysql_query("SELECT * FROM `cms_news_comments` WHERE `id`='".$com."' LIMIT 1;"));
						$name_otv = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `id`='".$usernick['user_id']."' LIMIT 1;"));
						echo $_SESSION['error'] . '<div class="gmenu">
						<form action="/news/index.php?act=comments&amp;mod=replay&amp;id=' . $id . '&amp;com=' . $com . '" method="post"  enctype="multipart/form-data"><div>
						<b>' . $lng['message'] . ':</b><br/>
						<textarea rows="3" name="text">[b]'.$name_otv['name'].'[/b], ' . 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>';
						
}
			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="gmenu">' . $lng_news['message_deleted'] . '</div>';
							Header('Location: /news_' . $id.'.html');
							exit;
						}
						echo $_SESSION['error'] . '<div class="rmenu">
						<form action="/news/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() . "',
							`balans`='" . ($datauser['balans'] + 3) . "'
							WHERE `id` = '$user_id'
						");
						$_SESSION['error'] = '<div class="gmenu">' . $lng_news['added_message'] . '</div>';
					} else {
						$_SESSION['error'] = '<div class="rmenu">' . implode('<br />', $error) . '</div>';
					}
					Header('Location: /news_' . $id.'.html');
					exit;
				}
				echo $_SESSION['error'] . '<div class="gmenu">
				<form action="/news/index.php?act=comments&amp;id=' . $id . '" method="post"  enctype="multipart/form-data"><div>
				<b>' . $lng['message'] . ':</b><br/>
				<textarea rows="3" name="text">' . (!empty($_POST['text']) ? htmlentities($_POST['text'], 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['sent'] . '"/>
				</div></form>
				</div>';
			}
			echo '<div class="phdr">' . $lng['comments'] . '</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 '<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($rights >= 7) $subtext = '<a href="/news/index.php?act=comments&amp;mod=replay&amp;id=' . $id . '&amp;com=' . $row['mid'] . '">' . $lng['reply']. '</a> | <a href="/news/index.php?act=comments&amp;mod=delete&amp;id=' . $id . '&amp;com=' . $row['mid'] . '">' . $lng['delete']. '</a>';
					elseif($user_id) $subtext = '<a href="/news/index.php?act=comments&amp;mod=replay&amp;id=' . $id . '&amp;com=' . $row['mid'] . '">' . $lng['reply']. '</a>';
					$text = ' <span class="gray" style="font-size: 15px;">&#160;'.functions::image('clock.png').'&#160;' . 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=comments&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>';
				}
				
			} else {
				echo '<div class="rmenu">' . $lng_news['message_news_empty'] . '!</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');
}