<?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;
$do = isset($_GET['do']) ? check($_GET['do']) : false;
switch($do) {
default:
$array = DB::$dbs->query("SELECT * FROM gazeta_article WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
DB::$dbs->query("UPDATE `gazeta_article` SET `chit` = `chit` + ? WHERE id = ?", [1,$id]);
$cat = DB::$dbs->queryFetch("SELECT id,name FROM gazeta_cat WHERE id = ? LIMIT 1", [$arr['cid']]);
$title->SetTitle('Свежие новости - '.($arr['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>Свежие новости </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="/allnews" rel="v:url" property="v:title">Свежие новости</a></li><li typeof="v:Breadcrumb"><a href="category.html?id='.$cat['id'].'" rel="v:url" property="v:title">'.$cat['name'].'</a></li><li class="active" typeof="v:Breadcrumb"><span property="v:title">'.$arr['name'].'</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"><i class="md-format-list-bulleted"></i> '.$arr['name'].'</div>';
echo '<div class="row text_box"><a href="index.html">Свежие новости</a> :: <a href="category.html?id='.$cat['id'].'">'.$cat['name'].'</a> :: '.$arr['name'].'</div>';
echo '<div class="row text_box">';
echo '<b>'.$arr['name'].'</b><br/>';
echo (empty($arr['image']) ? NULL : '<img src="/files/gazeta/'.$arr['image'].'" 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;"/>');
echo'</br>';
echo text($arr['text']);
echo '</div>';
echo '<div class="row text_box"><i class="md-group"></i> Добавил: '.Nick($arr['user_id']).' ('.vr($arr['time']).')</div>';

if ($b['id'] == $arr['user_id'] OR $b['level'] > 2) {
echo '<div class="row text_box"><i class="md-shuffle"></i>  Действие: <a href="?do=img&id='.$id.'">[Изображение]</a> / <a href="?do=up&id='.$id.'">[Изменить]</a> / <a href="?do=del&id='.$id.'">[Удалить]</a></div>';
}
$array = DB::$dbs->query("SELECT * FROM gazeta_article WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
$cat = DB::$dbs->queryFetch("SELECT id,name FROM gazeta_cat WHERE id = ? LIMIT 1", [$arr['cid']]);
echo '<div class="catSides"><i class="md-comment"></i> Обсуждение новости  ('.DB::$dbs->querySingle("SELECT COUNT(id) FROM gazeta_comm WHERE cid = ?", [$id]).')</div>';
if (isset($_GET['del'])) {
if ($b['level'] < 3) {
header('location: ?');
die();
}
DB::$dbs->query("DELETE FROM gazeta_comm WHERE id = ?", [abs((int)$_GET['del'])]);
header('location: news.html?id='.$id);
die();
}
if (isset($_GET['otv'])) {
$t = DB::$dbs->queryFetch("SELECT * FROM blogger WHERE id = ?", [abs((int)$_GET['otv'])]);
if (empty($t['id'])) {
header('location: /');
}
$reply = $t['nick'].', ';
} else {
$reply = '';
}
$otv = isset($_GET['otv']) ? abs((int)$_GET['otv']) : false;
$k_mess = DB::$dbs->querySingle("SELECT COUNT(id) FROM gazeta_comm WHERE cid = ?", [$id]);
$num = 10;
$k_page = k_page($k_mess, $num);
$page = page($k_page);
$start = $num*$page-$num;
$sql = DB::$dbs->query("SELECT * FROM gazeta_comm WHERE cid = ? ORDER BY time DESC LIMIT $start,$num", [$id]);
if ($sql->rowCount() == 0) {
err('Комментариев нет!');
} else {
while($a = $sql -> fetch()) {
$D = ($b['level'] > 2 ? '[<a href=news.html?id='.$id.'&del='.$a['id'].'">Удл</a>] ':NULL);
echo '<div class="row text_box">'.$D.Nick($a['user_id']).' ('.vr($a['time']).') '.(($a['user_id'] != $b['id']) ? ' <a href="news.html?id='.$id.'&otv='.$a['user_id'].'">[отв]</a>':NULL).'</br>'.text($a['text']).'</div>';
}
}
if ($k_page>1) {
str('news.html?id='.$id.'&', $k_page,$page);
}
echo '<div class="catSides"><i class="md-comment"></i> Добавить комментарий</div>';
if ($b == true){ 
if (isset($_POST['add'])) {
$text = check($_POST['text']);
if (empty($text)) {
err('Введите текст!');
} elseif (DB::$dbs->querySingle("SELECT COUNT(id) FROM gazeta_comm WHERE text = ? AND cid = ?", [$text,$id]) > 0) {
err('Такой коммент уже есть :)');
} else {
DB::$dbs->query("INSERT INTO gazeta_comm SET text = ?, time = ?, user_id = ?, cid= ?", [$text,time(),$b['id'],$id]);
header('location: news.html?id='.$id);
die();
}
}
$o = (!empty($otv) ? '&otv='.$otv : NULL);
echo '<form name="form" method="post" action="news.html?id='.$id.$o.'"><div class="row text_box">Комментарий:<br/></br>';
quickpaste('text');
quickpanel();
echo'</br></br><textarea name="text" class="form-control" rows="3">'.$reply.'</textarea> <input type="submit" name="add" class="btn btn-primary" value="Добавить"/></div></form>';
} else {echo'<div class="row text_box">Только зарегистрированные пользователи могут оставлять комментарии. <a href= "/login" >Войдите</a>, пожалуйста.</div>';}
break;
case 'img':
$array = DB::$dbs->query("SELECT * FROM gazeta_article WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
$cat = DB::$dbs->queryFetch("SELECT id,name FROM gazeta_cat WHERE id = ? LIMIT 1", [$arr['cid']]);
$title->SetTitle('Свежие новости - '.($arr['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>Свежие новости </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="/allnews" rel="v:url" property="v:title">Свежие новости</a></li><li typeof="v:Breadcrumb"><a href="category.html?id='.$cat['id'].'" rel="v:url" property="v:title">'.$cat['name'].'</a></li><li class="active" typeof="v:Breadcrumb"><span property="v:title">'.$arr['name'].'</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"><i class="md-format-list-bulleted"></i> <a href="?id='.$id.'">'.$arr['name'].'</a> - Добавить изображение</div>';
if ($arr['user_id'] != $b['id'] && $b['level'] < 2) {
header('location: /');
die();
}
if (isset($_POST['upl'])) {
$err = [];
$fnames = $_FILES['file']['name'];
$pictures = ['.gif', '.jpg', '.jpeg', '.png'];
$ext = strtolower(strrchr($_FILES['file']['name'], '.'));
$par = @getimagesize($_FILES['file']['tmp_name']);
if (empty($fnames)) {
$err[] = 'Выберите файл!';
} elseif ($par[0] > 1800 || $par[1] > 1600) {
$err[] = 'Большое расширение файла!';
} elseif (preg_match('/(\.php|\.pl|\.htaccess)/i', $fnames) || !in_array($ext, $pictures)) {
$err[] = 'Запрещенный формат файла!';
} elseif ($_FILES['file']['size'] > 1024 * 1 * 1024){
$err[] = 'Большой размер файла!';
}
$foto = ''.homeurl.'_' . mt_rand(1000, 9999) . $ext;
if (empty($err)) {
copy($_FILES['file']['tmp_name'], $_SERVER['DOCUMENT_ROOT'].'/files/gazeta/'.$foto);
DB::$dbs->query("UPDATE gazeta_article SET image = ? WHERE id = ? LIMIT 1", [$foto,$id]);
header('location: ?id='.$id);
} else {
echo '<div class="error">';
foreach($err as $error) {
echo $error.'<br/>';
}
echo '</div>';
}
}
echo '<form ENCTYPE="multipart/form-data" method="post" action="?do=img&id='.$id.'"><div class="row text_box">Изображение: (*max. 1800x1600px. 1mb.)<br/><input name="file" type="file" accept="image/jpeg, image/png, image/gif"/>
<br/><input type="submit" name="upl" class="btn btn-primary"value="Добавить"/></div></form>';
break;
case 'up':
$array = DB::$dbs->query("SELECT * FROM gazeta_article WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
$cat = DB::$dbs->queryFetch("SELECT id,name FROM gazeta_cat WHERE id = ? LIMIT 1", [$arr['cid']]);
$title->SetTitle('Свежие новости - '.($arr['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>Свежие новости </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="/allnews" rel="v:url" property="v:title">Свежие новости</a></li><li typeof="v:Breadcrumb"><a href="category.html?id='.$cat['id'].'" rel="v:url" property="v:title">'.$cat['name'].'</a></li><li class="active" typeof="v:Breadcrumb"><span property="v:title">'.$arr['name'].'</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"><i class="md-format-list-bulleted"></i> <a href="?id='.$id.'">'.$arr['name'].'</a> - Изменить запись</div>';
if ($b['id'] != $arr['user_id'] && $b['level'] < 2) {
header('location: /');
die();
}
if (isset($_POST['add'])) {
if (empty($_POST['name']) OR empty($_POST['text'])) {
err('Введите название и содержание!');
} elseif (DB::$dbs->querySingle("SELECT COUNT(id) FROM gazeta_article WHERE name = ? AND text = ?", [$name,$text]) > 0) {
err('Такая запись уже есть!');
} else {
DB::$dbs->query("UPDATE gazeta_article SET name = ?, text = ? WHERE id = ?", [check($_POST['name']),check($_POST['text']),$id]);
header('location: ?id='.$id);
}
}
echo '<div class="row text_box"><form action="?do=up&id='.$id.'" method="POST">Название:<br/> <input type="text" class="form-control" name="name" value="'.$arr['name'].'"/><br/>Содержание:<br/><textarea class="form-control" rows="3" name="text">'.$arr['text'].'</textarea><br/><input type="submit" name="add" class="btn btn-primary" value="Изменить"/></form></div>';
break;
case 'del':
$array = DB::$dbs->query("SELECT * FROM gazeta_article WHERE id = ? LIMIT 1", [$id]);
if ($array -> rowCount() == 0) {
header('location: /');
die();
}
$arr = $array -> fetch();
$title->SetTitle('Свежие новости - '.($arr['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>Свежие новости </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="/allnews" rel="v:url" property="v:title">Свежие новости</a></li><li typeof="v:Breadcrumb"><a href="category.html?id='.$cat['id'].'" rel="v:url" property="v:title">'.$cat['name'].'</a></li><li class="active" typeof="v:Breadcrumb"><span property="v:title">'.$arr['name'].'</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"><i class="md-format-list-bulleted"></i> <a href="?id='.$id.'">'.$arr['name'].'</a> - Удаление статьи</div>';
$cat = DB::$dbs->queryFetch("SELECT id,name FROM gazeta_cat WHERE id = ? LIMIT 1", [$arr['cid']]);
if ($b['id'] != $arr['user_id'] && $b['level'] < 2) {
header('location: /');
die();
}
if (isset($_GET['ok'])) {
DB::$dbs->query("DELETE FROM gazeta_comm WHERE cid = ?", [$id]);
if ($arr['image'] != NULL) {
unlink($_SERVER['DOCUMENT_ROOT'].'/files/gazeta/'.$arr['image']);
}
DB::$dbs->query("DELETE FROM gazeta_article WHERE id = ?", [$id]);
echo '<div class="row text_box">Успешно удалено!</div>';
///Подключаем футер
$title->GetFooter();
die();
}
echo '<div class="row text_box">Действительно удалить?<br/><a href="?do=del&id='.$id.'&ok">Да</a> | <a href="?id='.$id.'">Нет</a></div>';
break;
}

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

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