View file news.php

File size: 8.1Kb
<?php 
// by mides, wmclub.mobi

$title = 'Новости';
require_once 'system/sys.php';
require_once 'system/header.php';

switch ($_GET['act']) {
	default:
		echo '<div class="s1">Новости</div><div class="list">';
		$config['onpage'] = 7;
		$all_news = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `news`"), 0);
		if($all_news > 0){
			$pages = ceil($all_news/$config['onpage']);
			if (intval($_REQUEST['p'])) {
				$page = intval($_REQUEST['p']);
			} else {
				$page = 1;
			}
			$from = ($page-1)*$config['onpage'];
			
			$news_r = mysql_query("SELECT * FROM `news` ORDER BY `time` DESC LIMIT $from, $config[onpage]");
			while ($news = mysql_fetch_assoc($news_r)) {
				if (!isset($num) ) $num = 1;  
				$num++;
				$row_class = (!($num % 2)) ? 'row1' : 'row2';	
				$comm = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `news_comm` WHERE `id_news` = '$news[id]'"), 0);
				echo '<div class="'.$row_class.'"><b>'.$news['title'].'</b>: '.date('d.m.Y, H:i', $news['time']).'<br />'.bb($news['text']).'<br /><a href="?act=comm&amp;id='.$news['id'].'">Комментарии</a> ('.$comm.')</div>';
			}
		} else {
			echo 'Новостей еще нет<br />';
		}
		navig($page, '?', $pages);
		
		nav_main();
		break;

	case 'comm':
		$id = intval($_REQUEST['id']);
		$news = mysql_fetch_assoc(mysql_query("SELECT `title` FROM `news` WHERE `id` = '$id'"));
		if ($news['title']) {
			echo '<div class="s1">'.$news['title'].'</div><div class="list">';
			$count_comm = mysql_result(mysql_query("SELECT COUNT(`id`) FROM `news_comm` WHERE `id_news` = '$id'"), 0);
			if ($count_comm > 0) {
				$pages = ceil($count_comm/$config['onpage']);
				if (intval($_REQUEST['p'])) {
					$page = intval($_REQUEST['p']);
				} else {
					$page = 1;
				}
				$from = ($page-1)*$config['onpage'];
		
				$comm_r = mysql_query("SELECT * FROM `news_comm` WHERE `id_news` = '$id' ORDER BY `time` DESC LIMIT $from, $config[onpage]");
				while ($comm = mysql_fetch_assoc($comm_r)) {
					if (!isset($num) ) $num = 1;  
					$num++;
					$row_class = (!($num % 2)) ? 'row1' : 'row2';	
					echo '<div class="'.$row_class.'">';
				
					echo '<a href="profile.php?id='.$comm['id_user'].'">'.login($comm['id_user']).'</a> '.online(date('d.m.y, H:i', $comm['time']), $comm['id_user']).' <a href="?act=reply&amp;id='.$comm['id'].'">Отв</a>';
					if ($u['access'] > 0 or $u['id'] and $u['id'] == $comm['id_user'] and time() - $comm['time'] < $config['edit_time']) echo '|<a href="?act=edit&amp;id='.$comm['id'].'">Ред</a>';
					if ($u['access'] > 0) echo '|<a href="?act=del&amp;id='.$comm['id'].'">Уд</a>';
					echo '<br />'.bb($comm['msg']);
					if ($comm['edit_by']) echo '<br />_______<br /><span style="font-size: 10px;">отредактировано: '.login_simple($comm['edit_by']).' ('.date('d.m.y, H:i', $comm['edit_time']).' )</span>';
					echo '</div>';
				}
				navig($page, '?act=comm&amp;id='.$id.'&amp;', $pages);
			} else {
				echo 'Комментарий к этой новости еще нет.';
			}
			echo '</div>';
			if ($u['id']) {
				echo '<div class="s2">Добавить (max250):<br />
				<form action="?act=add&amp;id='.$id.'" method="post">
				<textarea name="msg" cols="" rows="3"></textarea>';
				echo '<input name="" type="submit" value="Ok">
				</form>
				</div>';
			}
		} else {
			error('Новость не существует или она была удалена.');
		}
		nav2('?', 'К новостям');
	break;
	
	case 'add':
		$id = intval($_REQUEST['id']);
		$news = mysql_fetch_assoc(mysql_query("SELECT `id` FROM `news` WHERE `id` = '$id'"));
		if ($news['id']) {
			if ($_POST['msg']) {
				if (time() > $_SESSION['antispam']) {
					$msg = check($_POST['msg']);
					mysql_query("INSERT INTO `news_comm` SET `id_news` = '$id', `id_user` = '$u[id]', `msg` = '$msg', `time` = '".time()."'");
					mysql_query("UPDATE `users` SET `karma` = (karma+1) WHERE `id` = '$u[id]'");
					$_SESSION['antispam'] = time() + $config['antispam'];
					header('location: ?act=comm&id='.$id);
				} else {
					error('Антиспам. Разрешено писать раз в '.$config['antispam'].' сек.');
					nav('?act=comm&amp;id='.$id);
				}
			} else {
				error('Вы не заполнили поле.');
				nav('?act=comm&amp;id='.$id);
			}
		} else {
			error('Новость не существует или она была удалена.');
			nav2('?', 'К новостям');
		}
	break;
	
	case 'reply':
		if ($u['id']) {
			$id = intval($_REQUEST['id']);
			$comm_r = mysql_query("SELECT `id_news`, `id_user` FROM `news_comm` WHERE `id` = '$id'");
			$comm = mysql_fetch_assoc($comm_r);
			if ($comm['id_news']) {
				if ($_REQUEST['ok']) {
					if (time() > $_SESSION['antispam']) {
						if ($_POST['msg']) {
							$msg = check($_POST['msg']);
							mysql_query("INSERT INTO `news_comm` SET `id_news` = '$comm[id_news]', `id_user` = '$u[id]', `msg` = '$msg', `time` = '".time()."'");
							mysql_query("UPDATE `users` SET `karma` = (karma+1) WHERE `id` = '$u[id]'");
							$_SESSION['antispam'] = time() + $config['antispam'];
							header('location: ?act=comm&id='.$comm['id_news']);
						} else {
							error('Вы не заполнили поле.');
							nav('?act=reply&amp;id='.$id);
						}
					} else {
						error('Антиспам. Разрешено писать раз в '.$config['antispam'].' сек.');
						nav('?act=comm&amp;id='.$comm['id_news']);
					}
				} else {
					tp('Ответ на сообщение');
					echo '<form action="?act=reply&amp;id='.$id.'&amp;ok=1" method="post">
					Сообщение(max250):<br /><textarea name="msg" cols="" rows="3">[b]'.login_simple($comm['id_user']).'[/b], </textarea><br />';
					echo '<input name="submit" type="submit" value="Ok" />
					</form>';
					nav('?act=comm&amp;id='.$comm['id_news']);
				}
			} else {
				error('Комментарий, на которое вы хотите ответить, не существует.');
				nav2('?', 'К новостям');
			}
		} else {
			header('location: login.php');
		}
	break;
	
	case 'edit':
		if ($u['id']) {
			$id = intval($_REQUEST['id']);
			$comm_r = mysql_query("SELECT `id_news`, `id_user`, `msg`, `time` FROM `news_comm` WHERE `id` = '$id'");
			$comm = mysql_fetch_assoc($comm_r);
			if ($comm['id_news']) {
				if ($u['access'] > 0 or $u['id'] == $comm['id_user'] and time() - $comm['time'] < $config['edit_time']) {
					tp('Редактирование комментария');
					if (empty($_REQUEST['ok'])) {
						echo '<form action="?act=edit&amp;id='.$id.'&amp;ok=1" method="post">
						Сообщение(max250):<br /><textarea name="msg" cols="" rows="3">'.$comm['msg'].'</textarea>
						<input name="submit" type="submit" value="Ok" />
						</form>';
						nav('?act=comm&amp;id='.$comm['id_news']);
					} else {
						if ($_POST['msg']) {
							$msg = check($_POST['msg']);
							mysql_query("UPDATE `news_comm` SET `id_news` = '$comm[id_news]', `msg` = '$msg', `edit_by` = '$u[id]', `edit_time` = '".time()."' WHERE `id` = '$id'");
							header('location: ?act=comm&id='.$comm['id_news']);
						} else {
							error('Вы не заполнили поле.');
							nav('?act=edit&amp;id='.$id);
						}
					}
				} else {
					error('Нельзя отредактировать этот комментарий.');
					nav('?act=comm&amp;id='.$comm['id_news']);
				}
			} else {
				error('Комментарий не существует.');
				nav2('?', 'К новостям');
			}
		} else {
			header('location: login.php');
		}
	break;
	
	case 'del':
		if ($u['access'] > 0) {
			$id = intval($_REQUEST['id']);
			$comm_r = mysql_query("SELECT `id_news` FROM `news_comm` WHERE `id` = '$id'");
			$comm = mysql_fetch_assoc($comm_r);
			if ($comm['id_news']) {
				mysql_query("DELETE FROM `news_comm` WHERE `id` = '$id'");
				header('location: ?act=comm&id='.$comm['id_news']);
			} else {
				header('location: ?');
			}
		} else {
			header('location: ?');
		}
	break;	
}

require_once ('system/tail.php');
?>