<?php
$title->SetTitle('Маркет - Параметры скриншотов');
///Подключаем шапку
$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>Маркет - Параметры скриншотов </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="/market" rel="v:url" property="v:title">Маркет</a></li><li class="active" typeof="v:Breadcrumb"><span property="v:title">Параметры скриншотов</span></li></ol></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">';
echo'<div class="catSides"><a href="/market"><i class="md-format-list-bulleted"></i> Маркет</a> - Параметры скриншотов </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 '<ul class="nav nav-pills nav-stacked"><li><a href="/market/screen/add/'.$id.'"><i class="md-shuffle"></i> Добавить скриншот</a></ul>';
$array = DB::$dbs->query("SELECT * FROM magazin_screen WHERE file = ? ORDER BY id DESC", [$id]);
if ($array->rowCount()==0) {
echo '<div class="error">Скриншотов пока нет!</div>';
}
while($arr = $array->fetch()){
echo '<div class="row text_box">[<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 = 'SKYWAP_ORG_'.mt_rand(1000, 9999).$ext;
if (!empty($err)){
echo '<div class="error">Не выбран файл</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="row text_box"><form action="/market/screen/add/'.$id.'" method="post" enctype="multipart/form-data">Скриншоты:<br/>
<input name="file" type="file"/><br/><input type="submit" name="ok" class="btn btn-primary" value="Добавить"></form></div>';
break;

}
echo '<ul class="nav nav-pills nav-stacked"><li><a href="/market/file/'.$id.'"><i class="md-apps"></i> Вернуться к файлу</a></ul>';
?>