<?php
$title->SetTitle('Маркет - Параметры скриншотов');
$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="/market"style="text-decoration:none; color:white;"> Маркет</a> :: Параметры скриншотов</div></div></div>';
$id = isset($_GET['id']) ? abs((int)$_GET['id']) : false;
if (empty($id)) {
header ('location: /');
exit;
}
$query = DB::$dbs->query("SELECT * FROM `magazin_file` WHERE `id`= ?", [$id]);
if ($query -> rowCount () == 0) {
header ('location: /');
exit();
}
$file = $query->fetch();
switch($act) {
default:
if (isset($_GET['del'])) {
$del = abs(intval($_GET['del']));
$screen = DB::$dbs->queryFetch("SELECT * FROM magazin_screen WHERE id = ? LIMIT 1",[$del]);
if(file_exists(ROOT.'files/mag_screen/'.$screen['screen'])) unlink(ROOT.'files/mag_screen/'.$screen['screen']);
DB::$dbs->query("DELETE FROM magazin_screen WHERE id = ?",[$del]);
header('location: /market/screen/'.$id);
}
echo '<div class="list-group-item media"> <span class="label label-danger">Новые</span> <a href="/market/screen/add/'.$id.'"> Добавить скриншот</a></div>';
$array = DB::$dbs->query("SELECT * FROM magazin_screen WHERE file = ? ORDER BY id DESC", [$id]);
if ($array->rowCount()==0) {
echo '<div class="alert alert-danger"role="alert">Скриншотов пока нет!</div>';
}
while($arr = $array->fetch()){
echo '<div class="list-group-item media">[<a href="/market/screen/'.$id.'?del='.$arr['id'].'">X</a>] <a href="/files/mag_screen/'.$arr['screen'].'"><img src="/files/mag_screen/'.$arr['screen'].'" alt="*" width="120"/></a></div>';
}
break;
case 'add':
if (isset($_POST['ok'])) {
$FileName = $_FILES['file']['name'];
$FileSize = $_FILES['file']['size'];
$GetExt = array('.bmp',
'.gif','.jpeg','.jpg','.png');
$ext = strtolower(strrchr($_FILES['file']['name'], '.'));
if (empty($FileName)) $err .= 'Не выбран файл!';
if(!preg_match('#([a-z0-9-_]{1,32})#i', $FileName)) $err .= 'Не правильное имя файла!';
if ($FileSize > 1024 * 2 * 1024) $err .= 'Размер файла более 2 Мб!';
if(preg_match('/(\.php|\.pl|\.htaccess)/i', $FileName) || !in_array($ext, $GetExt)) $err .= 'Запрещенный формат файла!';
$filename = homeurl.'_'.mt_rand(1000, 9999).$ext;
if (!empty($err)){
echo '<div class="alert alert-danger"role="alert">Не выбран файл</div>';
} else {
copy($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/files/mag_screen/'.$filename);
$copy_file = ROOT.'files/mag_screen/'.$filename;
$imgc = imagecreatefromstring(file_get_contents($_FILES['file']['tmp_name']));
$imgc = copy_img($imgc);
imagejpeg($imgc,$copy_file,90);
DB::$dbs->query("INSERT INTO `magazin_screen` SET `file` = ?, `screen` = ?",array($id,$filename));
header('location: /market/file/'.$id);
}
}
echo '<div class="list-group-item media"><form action="/market/screen/add/'.$id.'" method="post" enctype="multipart/form-data">Скриншоты:<br/>
<input name="file" class="form-control"type="file"/><br/><input type="submit" name="ok" class="btn btn-primary" value="Добавить"></form></div>';
break;

}
?>