<?php
define('ROOT','../');
define('GCMS',true);
require_once (ROOT.'system/power.php');
if ($aut) {
if ($id) {
$type = $db -> query("SELECT * FROM `load_files` WHERE `id`='".$id."");
if ($type -> num_rows > 0) $the = $type -> fetch_assoc(); else $the = false;
} else $the = false;
switch($mode) {
case 'write':
if ($ya['balls']>=$nastr['obcom']) {
$text = shit($_POST['text']);
$te = $db -> query("SELECT * FROM `comes` WHERE `where`='load' and `idwh` = '".(int)$id."' and `user` = '".(int)$ya['id']."' LIMIT 1");
if (mb_strlen($text) < 3) {
     $diz -> head('Ошибка');
     $diz -> title('Ошибка');
     $msg .= '<div class="fon">Короткий комментарий!</div>';
     header('refresh: 3; url=com'.$id.'');
     $diz -> out($msg);
}else{
$db -> query("UPDATE `users` SET `balls`=balls+'".$nastr['bcoms']."' WHERE `id` = '".(int)$ya['id']."'");
$db -> multi_query("INSERT INTO `comes` SET `where`='load', `user` = '".(int)$ya['id']."', `idwh` = '".$id."',`text` = '".$text."',`time` = '".time()."'");
header('location: comes.php?id='.(int)$id.'&page='.(int)$page);
}
}else{
      $diz -> head('Ошибка');
     $diz -> title('Ошибка');
     $msg .= '<div class="fon">Вам недоступна данная операция!</div>';
     header('refresh: 3; url=com'.$id.'');
     $diz -> out($msg);   
}
break;

case 'del':
if ($aut == true and $id != false) {
$sql = $db -> query("SELECT * FROM `comes` WHERE `id` = '".$sid."' LIMIT 1");
if ($sql -> num_rows > 0) {
$ass = $sql -> fetch_assoc();
if ($ya['level']>4) {
$db -> query("DELETE FROM `comes` WHERE `id` = '".$sid."' LIMIT 1");
header('location: comes.php?id='.(int)$ass['idwh'].'&page='.(int)$page);
} else header('location: comes.php?id='.(int)$ass['idwh'].'&page='.(int)$page);
} else header('location: comes.php?id='.(int)$ass['idwh'].'&page='.(int)$page);
} else header('location: comes.php?id='.(int)$ass['idwh'].'&page='.(int)$page);
break;

default:
$diz -> head('Комментарии');
$new = $db -> query("SELECT * FROM `load_files` WHERE `id` = '".$id."' LIMIT 1")-> fetch_assoc();
$diz -> title("<a href='./view/$new[id]/'>".$new['name']."</a>/<b>Комментарии</b>");
$count = $db -> dbcount('COUNT(`id`)','comes',"`idwh` = '".(int)$id."' and `where`='load'");
$num = isset($ya['num']) ? $ya['num'] : 10;
$total = intval(($count - 1) / $num) + 1; 
if (!isset($_GET['page']) || !is_numeric($_GET['page']) || $_GET['page'] < 1 )
{
$page = 1;
} elseif($_GET['page'] > $total)
{ $page = $total;
} else $page = (int)$_GET['page'];
$start = $page * $num - $num;
if ($count>0){
$for = $db -> query("SELECT * FROM `comes` WHERE `idwh` = '".(int)$id."' and `where`='load' ORDER BY `id` DESC LIMIT ".$start.",".$num);
while ($assoc = $for -> fetch_assoc()) {
$arr = $db -> query("SELECT `login` FROM `users` WHERE `id` = '".(int)$assoc['user']."' LIMIT 1");
if ($arr -> num_rows > 0) {
$array = $arr -> fetch_assoc();
$array['login'] = '<a href="'.ROOT.'my/id'.$assoc['user'].'">'.$array['login'].'</a>';
}
$dop = null;
if ($aut) {
if ((int)$ya['level'] > 4) {
$dop = ' | <a href="comes.php?id='.$id.'&amp;sid='.$assoc['id'].'&amp;mode=del&amp;page='.$page.'">[Удалить]</a>';
}
}
$msg .= '<div class="top">'.$array['login'].' ('.$func -> vremja($assoc['time']).') '.$dop.'<br/>';
$text = $func -> tags($func -> smiles($assoc['text']));
$msg .=''.$text.'</div>';
}
}else{$msg .= '<div class="fon">Комментариев пока нет!</div>';}
if ($aut == true) {
$msg .= '<form action="comes.php?id='.$id.'&amp;mode=write&amp;page='.$page.'" method="post" enctype="multipart/form-data"><div>';
$msg .= '<div class="fon">Комментарий:<br/>
<textarea name="text" rows="3" cols="15"></textarea><br/>
<input type="submit" value="Написать!"/>
</div></form></div>';}
$act = 'comes.php?id='.(int)$id;
$msg .= ''.$func -> pagenav($act,$page,$total).'';
$msg .= '<div class="levo">'.$diz -> img('home.png').'<a href="/index.php">На главную</a></div>';
#####Копирайт#######
require_once (ROOT.'system/kopy.php');
$diz -> out($msg);
}
} else header('location: aut.php');
?>