<?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="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>';

$do = isset($_GET['do']) ? check($_GET['do']) : false;
switch($do) {
default:
if ($file['user_id'] != $b['id'] && $b['level'] < 1) {
header('location: /');
die();
}
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="alert alert-success"role="alert">Успешно сохранено!</div>';
}
}
echo '<div class="list-group-item media"><form action="?id='.$id.'" method="POST">Название:<br/><input type="text" class="form-control" name="name" value="'.$file['name'].'"/><br/>Описание:<br/><br/>';
quickpaste('text');
quickpanel();
echo'<br/><br/><textarea class="form-control" rows="8"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();
}
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="alert alert-success"role="alert">Успешно сохранено!</div>';
$title->GetMenu();
$title->GetFooter();
die();
}
}
echo '<div class="list-group-item media"><form action="?do=per&id='.$id.'" method="post"><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",[$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();
}
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/'.homeurl.'_' . 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="list-group-item media"><form action="?do=screen&id='.$id.'" method="POST" enctype="multipart/form-data">Скриншот:<br/><input type="file" class="form-control" 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="list-group-item media">[<a href="?do=screen&id='.$id.'&x='.$a['id'].'">Удлить</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 '<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();
?>