<?php
require_once('../classes/body.class.php');
require_once('../classes/db.class.php');
require_once('../classes/system.class.php');
$title= new Body;
$title->SetTitle(homeurl.' - Новый аватар');
$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> :: Новый аватар</div></div></div>';

if ($b['ava'] !=null) {
echo '<div class="list-group-item media"><img src="/'.$b['ava'].'" alt="not image" style="max-width: 208px; max-height: 208px; border-radius: 5px;"/><br/><a href="?del">Удалить</a></div>';
if (isset($_GET['del'])) {
if ($b['ava'] != NULL) {
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/'.$b['ava'])) {
unlink($_SERVER['DOCUMENT_ROOT'].'/'.$b['ava']);
}
DB::$dbs->query("UPDATE `blogger` SET `ava` = ? WHERE `id` = ? LIMIT 1", ['',$b['id']]);
}
header('Location: ?');
}
} else {
if (isset($_POST['ok'])) {
$err = [];
$FileName = $_FILES['file']['name'];
$FileSize = (int)$_FILES['file']['size'];
$ext = strtolower(strrchr($FileName, '.'));
if ($FileSize > (1048576 * 2)) {
$err[] = 'Максимальный размер файла 2 Mb!';
} elseif ($FileSize == 0) {
$err[] = 'Маленький размер файла!';
} elseif (empty($FileName)) {
$err[] = 'Выберите файл!';
} elseif (preg_match('/(.php|.pl|.htaccess)/i', $FileName) || !in_array($ext, ['.png','.jpg','.jpeg','.gif'])) {
$err[] = 'Запрещенный формат файла!';
}

if (empty($err)) {
$NameFile = 'files/avs/'.$b['id'] . $ext;
if (file_exists($_SERVER['DOCUMENT_ROOT'].'/'.$b['ava'])) {
unlink($_SERVER['DOCUMENT_ROOT'].'/'.$b['ava']);
}
copy($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/'.$NameFile);
DB::$dbs->query("UPDATE `blogger` SET `ava` = ? WHERE `id` = ? LIMIT 1", [$NameFile,$b['id']]);
header('Location: ?');
} else {
foreach($err as $error) {
err($error);
}
}
}
?><div class="list-group-item media"><form action="" method="POST" enctype="multipart/form-data">Файл: [max.: 2 Mb / png, jpg, jpeg, gif]<br/> <input type="file" name="file"class="form-control"/></br><input type="submit" class="btn btn-primary" name="ok"> </form></div>
<?
}


$title->GetMenu();
$title->GetFooter();
?>