<?php
session_start();
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
$id = isset($_GET['id']) ? abs((int)$_GET['id']) : false;
if (empty($id)) {
header ('location: /');
exit;
}
$query = DB :: $dbs -> query ("SELECT * FROM `downs` WHERE `id`= ?", [$id]);
if ($query -> rowCount () == 0) {
header ('location: /');
exit;
}
$file = $query -> fetch();
$raz = DB :: $dbs -> queryFetch("SELECT * FROM `down_r` WHERE `id`= ?", [$file['rid']]);
$praz = DB :: $dbs -> queryFetch("SELECT * FROM `down_pr` WHERE `id`= ?", [$file['prid']]);
$url = $_SERVER['DOCUMENT_ROOT'].'/'.$file['url'];
$ext = pathinfo($url);
$ext = $ext['extension'];

$title->SetTitle('Просмотр файла '.$file['name']);
$title->GetHeader();
echo'<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i><a href="/"style="text-decoration:none; color:white;"> Главная</a> :: <a href="/downs"style="text-decoration:none; color:white;"> Загрузки</a> :: <a href="show_dir.html?id='.$raz['id'].'"style="text-decoration:none; color:white;">'.$raz['name'].'</a> :: <a href="show_kat.html?id='.$praz['id'].'"style="text-decoration:none; color:white;">'.$praz['name'].'</a> :: '.$file['name'].'</div></div></div>';
if ($file['user_id'] == $b['id'] || $b['level'] > 1) {
echo '<div class="list-group-item media"><a href="admin.php?id='.$id.'"><i class="zmdi zmdi-settings"></i>  <a href="admin.php?id='.$id.'">[Изменить]</a> <a href="admin.php?do=screen&id='.$id.'">[Скриншоты]</a>';
}
if ($b['level'] > 1) {
echo '  '.($file['block'] == 0 ? '<a href="?id='.$id.'&block">[Блокировать файл]</a>':'<a href="?id='.$id.'&unblock">[Разблокировать файл]</a>').' <a href="admin.php?do=per&id='.$id.'">[Перенести]</a>';
}
if ($file['user_id'] == $b['id'] || $b['level'] > 1) {
echo '</div>';
}

if ($b['level']< 1) {
if ($file['mod'] == 1) {
err('Файл на модерации!');
$title->GetMenu();
$title->GetFooter();
die();
}
}
if (!empty($file['pass']) && empty($_SESSION["password$id"])) {
$_SESSION['password'] = '';
if (isset($_POST['ok'])) {
$pass = check($_POST['pass']);
if (empty($pass) OR $file['pass'] != $pass) {
err('Введите пароль!');
} else {
$_SESSION["password$id"] = $pass;
header('location: /downs/file.html?id='.$id);
die();
}
}
echo '<div class="list-group-item media"><form action="/downs/file.html?id='.$id.'" method="POST">Пароль:<br/><input type="text" class="form-control" name="pass"/><br/><input type="submit" class="btn btn-primary" name="ok" value="Вперед"/></form></div>';
$title->GetMenu();
$title->GetFooter();
die();
}

if (isset($_GET['down'])) {
DB::$dbs->query("UPDATE `downs` SET `last_down` = ?, `count_down` = `count_down` + ? WHERE `id` = ?", [time(),1,$id]);
header('location: /'.$file['url']);
die();
}
if ($b['level'] >= 2) {
if (isset($_POST['block'])) {
if ($file['block'] == 1) {
header('location: ?id='.$id);
die();
}
$block_text = check($_POST['block_text']);
if (empty($block_text)) {
err('Введите причину!');
} else {
$mess = 'Ваш файл <a href="/downs/file.html?id='.$id.'">'.$file['name'].'</a> заблокирован администратором '.Nick($b['id']).' по причине [b]'.$block_text.'[/b]!';
jurnal($file['user_id'],$mess,5);
DB::$dbs->query("UPDATE downs SET block = ?, block_text = ?, block_who = ? WHERE id = ? LIMIT 1", [1,$block_text,$b['id'],$id]);
header('location: ?id='.$id);
die();
}
}
if (isset($_GET['unblock'])) {
DB::$dbs->query("UPDATE downs SET block = ?, block_text = ?, block_who = ? WHERE id = ? LIMIT 1", [0,NULL,NULL,$id]);
header('location: ?id='.$id);
die();
}
if (isset($_GET['block'])) {
echo '<div class="list-group-item media"><form action="?id='.$id.'&block" method="POST">Причина блокировки (max.: 250):<br/><textarea class="form-control" rows="6" name="block_text"></textarea><br/><input type="submit" name="block"class="btn btn-primary" value="Дальше"/></form></div>';
echo '<div class="tile"><ul class="nav nav-pills nav-stacked"><li><a href="file.html?id='.$id.'"><i class="zmdi zmdi-home"></i> Вернуться к файлу</a></li></ul></div>';
$title->GetMenu();
$title->GetFooter();
die();
}
}
if ($file['block'] == 1) {
echo '<div class="list-group-item media">'.Nick($file['block_who']).' заблокировал файл по причине: <b>'.text($file['block_text']).'</div>';
$title->GetMenu();
$title->GetFooter();
die();
}

$sql = DB::$dbs->query("SELECT * FROM `down_screen` WHERE `fid` = ? ORDER BY `id` DESC",[$id]);
if ($sql -> rowCount() > 0) {
echo '<div class="list-group-item media"><center>';
while($a = $sql->fetch()) {
echo '<a href="/'.$a['file'].'"> <img src="/'.$a['file'].'" alt="*"  style="max-width:100%; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; border-top-right-radius: 20px; border-top-left-radius: 20px;"/></a>';
}
echo '</center></div>';
}
if ($file['screen'] != NULL) {
//echo'<div class="list-group-item media"><a href="/'.$file['screen'].'"> <img src="/'.$file['screen'].'" alt="*" style="max-width:100%; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; border-top-right-radius: 20px; border-top-left-radius: 20px;"/></a></div>';
}
if (in_array($ext, ['png','jpg','jpeg','gif'])) {
echo'<div class="list-group-item media"><a href="/'.$file['url'].'"> <img src="/'.$file['url'].'" alt="*" style="max-width:100%; border-bottom-left-radius: 20px; border-bottom-right-radius: 20px; border-top-right-radius: 20px; border-top-left-radius: 20px;"/></a></div>';
}
$za = DB::$dbs->querySingle("SELECT COUNT(id) FROM `down_votes` WHERE `type` = ? AND `id_file` = ?", ['za',$id]);
$protiv = DB::$dbs->querySingle("SELECT COUNT(id) FROM `down_votes` WHERE `type` = ? AND `id_file` = ?", ['protiv',$id]);
$vo = DB::$dbs->querySingle("SELECT COUNT(id) FROM `down_votes` WHERE `user_id` = ? AND `id_file` = ?", [$b['id'],$id]);
echo '<div class="list-group-item media"><i class="zmdi zmdi-comment"></i> Название:  <b>'.$file['name'].'</b></div>
      <div class="list-group-item media"><i class="zmdi zmdi-account"></i> Загрузил: '.Nick($file['user_id']).' ('.vr($file['time']).')</div>';
echo '<div class="list-group-item media"><i class="zmdi zmdi-comments"></i> <b>Описание:</b> '.text($file['info']).'</div>
<div class="list-group-item media"><i class="zmdi zmdi-download"></i> Скачиваний: '.($file['count_down']).'</b></div>
<div class="list-group-item media"><i class="zmdi zmdi-cloud-upload"></i> Последнее скачивание: '.($file['last_down'] != NULL ? vr($file['last_down']) : 'еще не скачивали').'</div>
<div class="list-group-item media"><i class="zmdi zmdi-upload"></i> Вес файла:<b> '.get_size(filesize($url)).'</b></div>
'.($raz['id'] == 7 ? NULL : '<div class="list-group-item media"><i class="zmdi zmdi-account"></i> Автор файла<b> '.$file['author'].'</b></div>');
if ($file['user_id'] != $b['id']) {
$vot = ($vo == 0 ? '<a href="votes.html?id='.$id.'&vote=za"style="text-decoration:none; color:white;">+ 1</a> | <a href="votes.html?id='.$id.'&vote=protiv"style="text-decoration:none; color:white;">- 1</a>':NULL);
} else {
$vot = '';
}
echo '<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-widgets"></i> <a href="whovote.php?id='.$id.'"style="text-decoration:none; color:white;">Рейтинг</a>: '.$vot.' (<font color="green">'.$za.'</font> / <font color="red">'.$protiv.'</font>)</div></div></div>';
if ($ext == 'mp3') {
echo '<div class="list-group-item media">Прослушка: <object type="application/x-shockwave-flash" data="/downs/mp3.swf" width="200" height="20"><param name="wmode" value="transparent" /><param name="movie" value="/downs/mp3.swf" />
<param name="FlashVars" value="mp3=/'.$file['url'].'&amp;bgcolor1=ffffff&amp;bgcolor2=cccccc&amp;buttoncolor=999999&amp;buttonovercolor=0&amp;slidercolor1=cccccc&amp;slidercolor2=999999&amp;sliderovercolor=666666&amp;textcolor=0&amp;showvolume=1&amp;showstop=1" /></object><br/>';
include 'classAudioFile.php';
$AF = new AudioFile;
$AF->loadFile($url);
$AF->printSampleInfo();
echo '</div>';
}
echo '<div class="list-group-item media"><center> <a href="?id='.$id.'&down" style="text-decoration:none; color:black;"class="btn btn-primary btn-lg btn-block">Скачать файл "'.$file['name'].'"</a><br/></center></div>';
echo '<div class="list-group-item media"><a href="zip.html?id='.$id.'"><i class="zmdi zmdi-shuffle"></i>  Просмотр архива</a></div>';
echo '<div class="list-group-item media"><i class="zmdi zmdi-shuffle"></i> Импорт</a> <input class="form-control" class "enter"  type="text" value="http://'.homeurl.'/'.$file['url'].'"/></div>';
echo '<div class="tile"><div class="t-header th-alt bg-teal"><div class="th-title"><i class="zmdi zmdi-comments"></i> Обсуждение файла</div></div></div>';
if ($b == true){ 
if (isset($_GET['delkom'])) {
if ($b['level'] < 2){
header('Location: /');
die();
}
DB::$dbs->query("DELETE FROM down_komm WHERE id = ?", [abs((int)$_GET['delkom'])]);
header('location: /downs/file.html?id='.$id);
die();
}
if (isset($_POST['ok'])) {
$msg = check($_POST['msg']);
if (DB::$dbs->querySingle("SELECT count(id) FROM `down_komm` WHERE `text` =? AND `user_id` = ?", [$msg,$b['id']]) > 0) {
$err = 'Вы это уже писали!!!';
} elseif (empty($msg)) {
$err = 'Введите комментарий';
}
if (empty($err)) {
DB::$dbs->query("INSERT INTO down_komm SET file = ?, text = ?, time = ?, user_id = ?", [$id,$msg,time(),$b['id']]);
} else {
err('Критическая ошибка!');
}
}
if (isset($_GET['otv'])) {
$otv = abs(intval($_GET['otv']));
$u = DB::$dbs->queryFetch("SELECT * FROM blogger WHERE id = ? LIMIT 1", [$otv]);
if (empty($u['id']) && $u['id']==$b['id']){
header('location:?');
}
$st = $u['nick'].', ';
} else {
$st = '';
}
echo '<div class="list-group-item media"><form action="/downs/file.html?id='.$id.'" method="POST">Комментарий к файлу:<br/><br/>';
quickpaste('text');
quickpanel();
echo'<br/><br/><textarea class="form-control" rows="8" name="msg">'.$st.'</textarea><br/><input type="submit"  class="btn btn-primary btn-lg btn-block" name="ok" value="Добавить"/></form></div>';
} else {echo'<div class="list-group-item media">Только зарегистрированные пользователи могут оставлять комментарии. <a href= "/login" >Войдите</a>, пожалуйста.</div>';}

$k_post = DB::$dbs->querySingle("SELECT COUNT(id) FROM `down_komm` WHERE `file` = ?", [$id]);
if ($k_post == 0) {
err('Комментариев нет!');
}
$num = 10;
$k_page = k_page($k_post, $num);
$page = page($k_page);
$start = $num*$page-$num;
$array = DB::$dbs->query("SELECT * FROM `down_komm` WHERE `file` = ? ORDER BY time DESC LIMIT $start,$num",[$id]);
while($arr = $array -> fetch()) {
$del = ($b['level']>=2 ? '[<a href="/downs/file.html?id='.$id.'&delkom='.$arr['id'].'">Удл</a>] ':NULL);
echo '<div class="list-group-item media">'.$del . Nick($arr['user_id']).' ('.vr($arr['time']).') '. (($arr['user_id'] != $b['id']) ? ' [<a href="/downs/file.html?id='.$id.'&otv='.$arr['user_id'].'">отв</a>]':NULL).' </br>'.text($arr['text']) .'</div>';
}
if ($k_page > 1) {
str('?id='.$id.'&',$k_page,$page);
}
$title->GetMenu();
$title->GetFooter();
?>