<?php
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) || $b == false) {
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'];
$title->SetTitle('Действие с файлом '.$file['name']);
///Подключаем шапку
$title->GetHeader();

echo '<div class="container-fluid page_header"><div class="container"><div class="row"><div class="col-xs-12 col-sm-8 col-md-9 col-lg-9"><h1>Загруз-центр - '.$file['name'].' </h1><ol class="breadcrumb" xmlns:v="http://'.homeurl.'/#"><li typeof="v:Breadcrumb"><a href="/" rel="v:url" property="v:title">'.homeurl.'</a></li><li typeof="v:Breadcrumb"><a href="/downs" rel="v:url" property="v:title">Загруз-центр</a></li><li typeof="v:Breadcrumb"><a href="show_dir.html?id='.$raz['id'].'" rel="v:url" property="v:title">'.$raz['name'].'</a></li><li typeof="v:Breadcrumb"><a href="show_kat.html?id='.$praz['id'].'" rel="v:url" property="v:title">'.$praz['name'].'</a></li><li class="active" typeof="v:Breadcrumb"><span property="v:title"><b>'.$file['name'].'</b></span></li></ol></div><div class="col-xs-12 col-sm-4 col-md-3 col-lg-3 text-right page_header_sidebar"></div></div></div></div>';
echo'<div class="container page_body"><div class="row"><div class="col-xs-12 page_content col-lg-9"><div class="navbar page_content_navbar collapse hidden-lg"><div class="container-fluid"><div class="navbar-collapse"></div></div></div><div class="text_box hidden-lg pr_block_small hidden"></div><div class="container-fluid cols_table">';

$do = isset($_GET['do']) ? check($_GET['do']) : false;
switch($do) {
default:
if ($file['user_id'] != $b['id'] && $b['level'] < 1) {
header('location: /');
die();
}
echo '<div class="catSides"><a href="/downs"><i class="md-format-list-bulleted"></i> Загруз-центр</a> - Изменить описание и название</div>';
if (isset($_POST['save'])) {
if (empty($_POST['name']) OR empty($_POST['info'])) {
err('Введите название и описание!');
} else {
DB::$dbs->query("UPDATE downs SET name = ?, info = ? WHERE id = ?", [check($_POST['name']),check($_POST['info']),$id]);
echo '<div class="access">Успешно сохранено!</div>';
}
}
echo '<div class="row text_box"><form action="?id='.$id.'" method="POST">Название:<br/><input type="text" class="form-control" name="name" value="'.$file['name'].'"/><br/>Описание:<br/><textarea class="form-control" rows="3"name="info">'.$file['info'].'</textarea><br/><input type="submit" name="save" class="btn btn-primary"value="Сохранить"/></form></div>';
break;
case 'per':
if ($b['level'] < 1) {
header('location: /');
die();
}
echo '<div class="catSides"><a href="/downs"><i class="md-format-list-bulleted"></i> Загруз-центр</a> - Перенос файла</div>';
if (isset($_POST['ok'])) {
$nr = abs((int)$_POST['razd']);
if (empty($nr)) {
err('Вы не выбрали новый раздел..');
} elseif (DB::$dbs->querySingle("SELECT count(id) FROM down_pr WHERE id = ?", [$nr])==0) {
err('Данный подраздел не существует!');
} else {
$nrp = DB::$dbs->queryFetch("SELECT id_r,name FROM down_pr WHERE id = ? LIMIT 1", [$nr]);
$nrp2 = DB::$dbs->queryFetch("SELECT name FROM down_pr WHERE id = ? LIMIT 1", [$file['prid']]);
DB::$dbs->query("UPDATE downs SET rid = ?, prid = ? WHERE id = ? LIMIT 1",[$nrp['id_r'],$nr,$id]);
echo '<div class="row text_box">Успешно перенесено!</div>';
///Подключаем меню
$title->GetMenu();


///Подключаем футер
$title->GetFooter();
die();
}
}
echo '<form action="?do=per&id='.$id.'" method="post"><div class="row text_box"> <select class="form-control" size="1" name="razd">';
$razds = DB::$dbs->query("SELECT * from down_pr order by id desc");
while($pr = $razds->fetch()){
$r = DB::$dbs->queryFetch("SELECT * from down_r where id = ? limit 1",array($pr['id_r']));
$selected = ($file['prid'] == $pr['id']) ? 'selected="selected"':NULL;
echo'<option '.$selected.' value="'.$pr['id'].'">'.$pr['name'].' ('.$r['name'].')</option><br />';
}
echo'</select><br/><input type="submit" class="btn btn-primary" name="ok" value="Перенести"/></div></form>';
break;
case 'screen':
if ($file['user_id'] != $b['id'] && $b['level'] < 1) {
header('location: /');
die();
}
echo '<div class="catSides"><a href="/downs"><i class="md-format-list-bulleted"></i> Загруз-центр</a> - Скриншоты</div>';
if (isset($_POST['add'])) {
$ScreenName = $_FILES['screen']['name'];
$ScreenSize = $_FILES['screen']['size'];
$frm = array('.jpg', '.jpeg', '.gif', '.png');
$scr = mb_strtolower(strrchr($ScreenName, '.'), 'UTF8');
if (empty($ScreenName)) {
err('Выберите скриншот.');
} elseif (!empty($ScreenName) && $ScreenSize > (1024 * 500)){
err('Размер скриншота не должен быть более 500 Kb!');
} elseif (!empty($ScreenName) && preg_match('/(\.php|\.pl|\.htaccess)/i', $ScreenName) || !empty($ScreenName) && !in_array($scr, $frm)) {
err('Скриншот не является изображением!');
} else {
$ScreenFile = 'files/downs/screen/SKYWAP_ORG' . mt_rand(10000, 99999) . '_' . retrans($ScreenName);
copy($_FILES['screen']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/'.$ScreenFile);
DB::$dbs->query("INSERT INTO `down_screen` SET `fid` = ?, `file` = ?", [$id,$ScreenFile]);
header('location: ?do=screen&id='.$id);
die();
}
}
echo '<div class="row text_box"><form action="?do=screen&id='.$id.'" method="POST" enctype="multipart/form-data">Скриншот:<br/><input type="file" name="screen"/><br/><input type="submit" name="add" class="btn btn-primary" value="Загрузить"/></form></div>';
if (isset($_GET['x'])) {
$del = abs((int)$_GET['x']);
$ss = DB::$dbs->query("SELECT * FROM `down_screen` WHERE `id` = ? LIMIT 1", [$del]);
if ($ss -> rowCount() != FALSE) {
$info = $ss -> fetch();
if (file_exists('/'.$info['file'])) unlink('/'.$info['file']);
DB::$dbs->query("DELETE FROM `down_screen` WHERE `id` = ? LIMIT 1", [$del]);
header('location: ?do=screen&id='.$id);
die();
}
}
$look = DB::$dbs->querySingle("SELECT COUNT(id) FROM `down_screen` WHERE `fid` = ?", [$id]);
if ($look == 0) {
err('Скриншотов нет!');
}
$num = 10;
$k_page = k_page($look, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM `down_screen` WHERE `fid` = ? ORDER BY `id` DESC LIMIT $start,$num",[$id]);
while($a = $sql->fetch()) {
echo'<div class="row text_box">[<a href="?do=screen&id='.$id.'&x='.$a['id'].'">X</a>] <a href="/'.$a['file'].'"> <img class="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></div>';
}
if ($k_page > 1) {
str('?do=screen&id='.$id.'&',$k_page,$page);
}
break;
}
echo '<ul class="nav nav-pills nav-stacked"><li><a href="file.html?id='.$id.'"><i class="md-apps"></i> Вернуться к файлу</a></ul>';

///Подключаем меню
$title->GetMenu();


///Подключаем футер
$title->GetFooter();
?>