<?
require'config.php';
$align='left';
$title='Новости чата';
head();
aut();
who_add(0,'enter');

$mid = (int)$_GET['mid'];

switch($mod){
default:

if(isset($_GET['ok']))echo '<b>Голос принят.</b>';
if(isset($_GET['stop']))echo '<b>Вы уже голосовали.</b>';

if (empty($user['max'])) $user['max']=10;
$max = $user['max'];
$k_post=$db->sql_fetchfield($db->sql_query("select count(*) as num from news"),0);
$k_page=k_page($k_post,$max);
$page=page($k_page);
$start=$max*$page-$max;

$r = $db->sql_query ("Select * from news order by time desc LIMIT $start, $max");
if ($k_post == 0)echo 'Новостей нет<br />';
$post_k=$start+1;
while($a = @$db->sql_fetchrow($r)){
$plus=$db->sql_fetchfield($db->sql_query("select count(*) as num from `likes` where `news_id` ='".$a['id']."' and plus"),0);
$minus=$db->sql_fetchfield($db->sql_query("select count(*) as num from `likes` where `news_id` ='".$a['id']."' and minus"),0);
echo links.''.nik($a[user_id]).' ('.times($a['time']).')'.div;
echo text($a[msg]);
echo '<br/>- - -<br/>[ '.$plus.' ]<a href="news?mod=like&amp;mid='.$a[id].'&amp;plus"><img src="'.H.'img/plus.gif" alt="*"/> Плюс</a></img> '.$a['like'].'| <a href="news?mod=like&amp;mid='.$a[id].'&amp;minus">Минус<img src="'.H.'img/minus.gif" alt="*"/></a></img>[ '.$minus.' ]<br/>
<a href="news?mod=who_vote&amp;mid='.$a[id].'">Голоса за новость</a><br/>
<a href="news?mod=komm&amp;new='.$a[id].'">Комментарии</a> ('.$db->sql_fetchfield($db->sql_query("select count(*) as num from newskom where new = '".$a[id]."'"),0).')<br/>';
echo '<dl></dl>';

}

if ($k_page>1){
echo "<dl><dt></dt></dl>";
str('news?',$k_page,$page); // Вывод страниц
echo "<br/><dl><dt></dt></dl><br/>";
}



break;

case 'komm':
$new = check(intval($_GET['new']));
$r = $db->sql_query("Select * from news WHERE `id` = '".$new."'");
if (mysql_affected_rows()==0){echo 'Новость не найдена!<br />';
}else{

if (isset($_GET['delk'])){
if($user[level]>=5){
$db->sql_query("DELETE FROM `newskom` WHERE `id` = '".intval($_GET['delk'])."'");
}
header ('Location: news?mod='.$mod.'&new='.$new.'&'.SID);
}

if (isset($_POST['msg'])){
if (strlen2($_POST['msg'])>3){
if ($user['translit']==1)$_POST['msg'] = translit($_POST['msg']);
if ($db->sql_query ("INSERT INTO newskom (new,user_id,time,msg) VALUES ('$new','$user[id]','$time','".check($_POST['msg'])."')")){header ('Location: news?mod='.$mod.'&new='.$new.'&'.SID); exit;
}else echo 'Ошибка!<br />';


}

}


if (empty($user['max'])) $user['max']=10;
$max = $user['max'];
$k_post=$db->sql_fetchfield($db->sql_query("select count(*) as num from newskom where new = '".check(intval($_GET['new']))."'"),0);
$k_page=k_page($k_post,$max);
$page=page($k_page);
$start=$max*$page-$max;

$r = $db->sql_query ("Select * from newskom where new = '".check(intval($_GET['new']))."' order by time desc LIMIT $start, $max");
if ($k_post == 0)echo 'Комментариев нет<br />';
$post_k=$start+1;
while($a = @$db->sql_fetchrow($r)){
echo links.''.nik($a[user_id]).'';
if($user[level]>=5)echo '<small><a href="news?mod='.$mod.'&amp;&amp;new='.$new.'&amp;delk='.$a['id'].'">[X]</a></small>';
echo '('.times($a['time']).')'.div;

echo text($a[msg]);
echo '<dl></dl>';

}

if ($k_page>1){
echo "<dl><dt></dt></dl>";
str('news?mod='.$mod.'&amp;&amp;new='.$new.'&amp;',$k_page,$page); // Вывод страниц
echo "<br/><dl><dt></dt></dl><br/>";
}

if($user['posts']>=$set['post_diaries']){
echo "<dl><dt></dt></dl>";
if ($ver!='wml')echo '<form action="news?mod='.$mod.'&amp;new='.$new.'&amp;'.SID.'" method="post">';
if ($ver=='wml')echo '<br/>Ваш комментарий:<br/><input name="msg'.$ref.'" maxlength="15" title="Text"/><br/>';
else echo '<br/>Ваш комментарий:<br/><textarea cols="'.$user['pole2'].'" rows="'.$user['pole1'].'" name="msg"></textarea><br/><br/>';
if ($ver=='wml'){
echo '<anchor title="go">Добавить<go href="news?mod='.$mod.'&amp;new='.$new.'&amp;'.SID.'" method="post">';
echo '<postfield name="msg" value="$msg'.$ref.'"/>
</go></anchor><br/><br/>';
;
}else{
echo '<input type="submit" class="ibutton" value="Добавить"/></form><br/><br/>';

}
}else{
echo 'Для Комментирования нужно<br/>иметь более <b>'.$set['post_diaries'].'</b> постов!<br/><dl><dt></dt></dl>';
}
}
break;

case like:
$q = $db->sql_query("select * from `likes` where `news_id` ='$mid' and `user_id` ='".$user['id']."' limit 1");
$i = $db->sql_fetchrow($q);
if ($i['user_id'] == $user['id'])
{
header ('Location: news?stop'.SID);exit;
}
$r = $db->sql_query ("select * from news where id = '".$mid."' limit 1");
while($a = @$db->sql_fetchrow($r)){
if(isset($_GET['plus'])){
$like = $a['like']+1;
$db->sql_query("update `news` set `likes` ='".$like."' where `id` ='".$mid."'");
$db->sql_query("insert into `likes` (news_id,user_id,plus) values ('$mid','$user[id]','1')");
header ('Location: news?ok'.SID);
}
if(isset($_GET['minus'])){
$unlike = $a['like']-1;
$db->sql_query("update `news` set `likes` ='".$unlike."' where `id` ='".$mid."'");
$db->sql_query ("insert into `likes` (news_id,user_id,minus) VALUES ('$mid','$user[id]','1')");
header ('Location: news?ok'.SID);
}
}
break;

case who_vote:

if (empty($user['max'])) $user['max']=10;
$max = $user['max'];
$k_post=$db->sql_fetchfield($db->sql_query("select count(*) as num  from likes where `news_id` ='".$mid."' "),0);
$k_page=k_page($k_post,$max);
$page=page($k_page);
$start=$max*$page-$max;

$r = $db->sql_query ("Select *from `likes` where `news_id` ='".$mid."' order by time desc LIMIT $start, $max");

$up=$db->sql_fetchfield($db->sql_query("select count(*) as num from `likes` where `news_id` ='".$mid."' and plus ='1'"),0);
$down=$db->sql_fetchfield($db->sql_query("select count(*) as num from `likes` where `news_id` ='".$mid."' and minus ='1'"),0);

if (($max*$page)<$k_post) $maxi = $max*$page; else $maxi = $k_post;
echo 'Показаны голоса '.($start+1).'-'.$maxi.' из '.$k_post.' [<font color="green"><b>'.$up.'</b></font>l<font color="red"><b>'.$down.'</b></font>]<br/><br/>';
if ($k_post == 0)echo 'Голосов пока нет...<br /><br/>';

$post_k=$start+1;
while($a = @$db->sql_fetchrow($r)){

if($a['plus']=='1'){
$a['plus'] = Положительно;
echo links.'<b>'.nik($a['user_id']).'</b>[<font color="green"><b>'.$a['plus'].'</b></font>]'.div;
} 
if($a['minus']=='1'){
$a['minus'] = Отрицательно;
echo links.'<b>'.nik($a['user_id']).'</b>[<font color="red"><b>'.$a['minus'].'</b></font>]'.div;
}
if(($user['level'])>=7){
echo '<a href="news?mod=who_votes&amp;news_id='.$mid.'&amp;dell='.$a['id'].'">Удалить</a><br/>';

}
echo '<br/><hr/>';
}
if ($k_page>1){
echo "<dl><dt></dt></dl>";
str('news?mod=who_vote&amp;news_id='.$mid.'&amp;',$k_page,$page);
echo "<dl><dt></dt></dl>";
}
if (isset($_GET['dell'])){
$a = $db->sql_fetchrow($db->sql_query ("Select * from likes where id = '".intval($_GET['dell'])."'"));
if(($user['level'])>=7){
$db->sql_query("DELETE FROM `likes` WHERE `id` = '".intval($_GET['dell'])."'");
}
header ('Location: news?mod=who_vote&news_id='.$mid.'&'.SID);
}
break;


}
echo '<br/>';
if ($mod)echo gb.'<a href="news">Новости</a><br/>'.div;
echo gb.'<a href="'.H.'enter">Прихожая</a><br/>'.div;
foot();

?>