<?php
/*
Автор мода: Shot_
Скрипт написан под StrongCms
Официальны сайт поддержки: http://strongcms.ru
*/

$title = 'Газета сайта';
require_once ('../system/function.php');
require_once ('../system/header.php');

if(!$user['id']) {
header('Location: '.$HOME.'');
exit();
}

switch($_GET['act'])
{
default:

echo '<div class="title">'.$title.'</div>';

if($user['level'] >= 3) echo '<div class="menudiv"><a href="/newspaper/kat_add" class="k_menu"><img src="images/new_add.png" alt="*" /> Добавить категорию</div></a>';

echo '<div class="menudiv"><a href="/newspaper/search" class="k_menu"><img src="images/search.png" alt="*" /> Поиск по газете</div></a>
<div class="menudiv"><a href="/newspaper/zaklad" class="k_menu"><img src="images/star.png" alt="*" /> Закладки</div></a>
';
echo '<div class="menu"><img src="'.$HOME.'/design/gradient/lol.png" alt="*" /><div class="divmenu">Последние статьи:</div></div>';


############################
## Вывод последних стетей ##
############################
$count_paper = mysql_result(mysql_query("SELECT COUNT(*) FROM `newspaper_paper` "),0);

$posld = mysql_query("SELECT * FROM `newspaper_paper` ORDER BY `time` DESC LIMIT 3");
while($a = mysql_fetch_assoc($posld))
{
echo '<div class="menudiv">
<a href="/newspaper/read'.$a['id'].'" class="k_menu"><img src="images/newspaper.png" alt="*" /> '.$a['name'].'</a>
</div></a>';
}

if($count_paper < 1) {
echo '<div class="podmenu"><b><center>Пусто!</center></b></div>';
}

############################
##### Вывод категорий  #####
############################

echo '<div class="menu"><img src="'.$HOME.'/design/gradient/lol.png" alt="*" /><div class="divmenu">Категории:</div></div>';


$count_kat = mysql_result(mysql_query("SELECT COUNT(*) FROM `newspaper_kat` "),0);

$kat = mysql_query("SELECT * FROM `newspaper_kat` ORDER BY `id` DESC ");
while($b = mysql_fetch_assoc($kat))
{
echo '<table cols="2" width="100%" cellpadding="0" cellspacing="0" style="font-size:14px;"><tr>
<td class="menudiv" width="95%"><a href="/newspaper/kat'.$b['id'].'" class="k_menu">'.$b['name'].'</a></td>
<td class="menudiv"><a href="/newspaper/kat'.$b['id'].'" class="k_menu">'.mysql_result(mysql_query('select count(`id`) from `newspaper_paper` where `kat` = "'.$b['id'].'"'),0).'</a></td>';

if($user['level'] >= 3) echo '<td class="menudiv"><a href="/newspaper/edit_kat'.$b['id'].'" class="k_menu">Изменить</a></td> <td class="menudiv"><a href="/newspaper/del_kat'.$b['id'].'" class="k_menu">Удалить</a></td>';

echo '</tr></table>';
}

if($count_kat < 1) {
echo '<div class="podmenu"><b><center>Пусто!</center></b></div>';
}

break;
############################
##### Новая категория  #####
############################
case 'kat_add':

echo '<div class="title"><a href="/newspaper">'.$title.'</a> | Новая категория</div>';

if($user['level'] < 3) {
header('Location: /newspaper');
exit();
}

if(isset($_REQUEST['submit'])) {

$name = strong($_POST['name']);

if(mb_strlen($name) < 3) $err = 'Минимум для ввода 3 символа!';
if(empty($name)) $err = 'Введите название категории!';

if($err) {
echo '<div class="podmenu"><center><b>'.$err.'</b></center></div>';
require_once ('../system/footer.php');
exit();
}

mysql_query("INSERT INTO `newspaper_kat` SET `name` = '".$name."'");
header('Location: /newspaper');
exit();
}

echo '<div class="podmenu"><form action="" method="POST">
Введите имя категории:<br /> <input type="text" name="name" value=""/><br />
<input type="submit" name="submit" value="Создать" />
</form></div>
<div class="menudiv"><a href="/newspaper" class="k_menu">Назад в газету сайта</div></a>';

break;
############################
##### Реда. категорию  #####
############################
case 'edit_kat':

$id = abs(intval($_GET['id']));
$kat = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_kat` WHERE `id` = '".$id."'"));

if($kat == 0) {
echo '<div class="title">'.$title.' | Ошибка</div><div class="podmenu"><center><b>Такой категории не существует!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

if($user['level'] < 3) {
header('Location: '.$HOME.'/newspaper');
exit();
}

echo '<div class="title"><a href="/newspaper">'.$title.'</a> | <a href="/newspaper/kat'.$id.'">'.$kat['name'].'</a> | Редактирование категории</div>';

if(isset($_REQUEST['submit'])) {

$name = strong($_POST['name']);

if(mb_strlen($name) < 3) $err = 'Минимум для ввода 3 символа!';
if(empty($name)) $err = 'Введите название категории!';

if($err) {
echo '<div class="podmenu"><center><b>'.$err.'</b></center></div>';
require_once ('../system/footer.php');
exit();
}

mysql_query("UPDATE `newspaper_kat` SET `name` = '".$name."' WHERE `id` = '".$id."'");
echo '<div class="podmenu"><center><b>Успешно!</b></center></div>';
}

echo '<div class="podmenu"><form action="" method="POST">
Имя категории:<br /> <input type="text" name="name" value="'.$kat['name'].'"/><br />
<input type="submit" name="submit" value="Редактировать" />
</form></div>
<div class="menudiv"><a href="/newspaper" class="k_menu">Назад в газету сайта</div></a>';

break;
############################
##### Удлт. категорию  #####
############################
case 'del_kat':

$id = abs(intval($_GET['id']));
$kat = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_kat` WHERE `id` = '".$id."'"));

if($kat == 0) {
echo '<div class="title">'.$title.' | Ошибка</div><div class="podmenu"><center><b>Такой категории не существует!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

if($user['level'] < 3) {
header('Location: '.$HOME.'/newspaper');
exit();
}

if(isset($_REQUEST['ok'])) {

mysql_query("DELETE FROM `newspaper_kat` where `id` = '".$id."'");
mysql_query("DELETE FROM `newspaper_paper` where `kat` = '".$id."'");

header('Location: /newspaper');
exit();
}


echo '<div class="title"><a href="/newspaper/">'.$title.'</a> | Удаление категории</div>
<div class="podmenu">Вы действительно хотите удалить эту категорию?<br /><a href="?ok">Да</a> | <a href="/newspaper/">Нет</a></div>';

break;
############################
##### Вывод категории  #####
############################
case 'kat':

$id = abs(intval($_GET['id']));
$kat = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_kat` WHERE `id` = '".$id."'"));

if($kat == 0) {
echo '<div class="title">'.$title.' | Ошибка</div><div class="podmenu"><center><b>Такой категории не существует!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

echo '<div class="title"><a href="/newspaper">'.$title.'</a> | '.$kat['name'].'</div>
<div class="menudiv"><a href="/newspaper/new_paper'.$id.'" class="k_menu"><img src="images/newspaper_add.png" alt="*" /> Добавить статью</div></a>';

if (empty($user['max'])) $user['max']=10;
$max = $user['max'];
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `newspaper_paper` WHERE `kat` = '".$id."'"),0);
$k_page = k_page($k_post,$max);
$page = page($k_page);
$start = $max*$page-$max;

$paper = mysql_query("SELECT * FROM `newspaper_paper` WHERE `kat` = '".$id."' ORDER BY `id` DESC LIMIT $start, $max");
while($a = mysql_fetch_assoc($paper))
{

$array = explode(" ", $a['text']);
$text_num = array_slice($array, 0, 35);
$text_v = implode(" ", $text_num);
$text_v .= '...';

echo '<div class="menudiv"><a href="/newspaper/read'.$a['id'].'" class="k_menu">
<img src="images/newspaper.png" alt="*" /> '.$a['name'].' <br />
<div class="divnews">'.smile(bb($text_v)).'</div></a></div>';
}

if($k_post < 1) {
echo '<div class="podmenu"><b><center>Пусто!</center></b></div>';
}

if ($k_page>1) {
echo str(''.$HOME.'/newspaper/kat'.$id.'?',$k_page,$page); // Вывод страниц
}

break;
############################
####### Нова статья  #######
############################
case 'new_paper';

$id = abs(intval($_GET['id']));
$kat = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_kat` WHERE `id` = '".$id."'"));

if($kat == 0) {
echo '<div class="title">'.$title.' | Ошибка</div><div class="podmenu"><center><b>Такой категории не существует!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

echo '<div class="title"><a href="/newspaper">'.$title.'</a> | <a href="/newspaper/kat'.$id.'">'.$kat['name'].'</a> | Новая статья</div>';

if(isset($_REQUEST['submit'])) {

$name = strong($_POST['name']);
$text = strong($_POST['text']);

if(mb_strlen($text) < 50) $err = 'Минимум для ввода 50 символов!';
if(empty($text)) $err = 'Введите текст статьи!';
if(mb_strlen($name) < 5) $err = 'Минимум для ввода 5 символов!';
if(empty($name)) $err = 'Введите название статьи!';

if($err) {
echo '<div class="podmenu"><center><b>'.$err.'</b></center></div>';
require_once ('../system/footer.php');
exit();
}

mysql_query("INSERT INTO `newspaper_paper` SET `kat` = '".$id."',`name` = '".$name."',`text` = '".$text."',`us` = '".$user['id']."',`time` = '".time()."'");

$sql = mysql_insert_id();
header('Location: /newspaper/read'.$sql.'');
exit();
}

echo '<div class="podmenu"><form action="" method="POST">
Название статьи:<br /> <input type="name" name="name" value=""/><br />
Текст статьи:<br /><textarea name="text"></textarea><br />
<input type="submit" name="submit" value="Опубликовать" />
</form></div>
<div class="menudiv"><a href="/newspaper/kat'.$id.'" class="k_menu">Назад</div></a>';

break;
############################
###### Вывод Статьи  #######
############################
case 'read':

$id = abs(intval($_GET['id']));
$read = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_paper` WHERE `id` = '".$id."'"));
$kat = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_kat` WHERE `id` = '".$read['kat']."'"));

if($read == 0) {
echo '<div class="title">'.$title.' | Ошибка</div><div class="podmenu"><center><b>Такой статьи не существует!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

mysql_query("UPDATE `newspaper_paper` SET `kolvo` = `kolvo` +1 WHERE `id` = '".$id."'");

echo '<div class="title"><a href="/newspaper">'.$title.'</a> | <a href="/newspaper/kat'.$kat['id'].'">'.$kat['name'].'</a> | '.$read['name'].'</div>';

if($user['level'] >= 1 or $user['id'] == $read['us']) {
echo '<table style="width:100%" cellspacing="0" cellpadding="0"><tr>
<td class="podmenu"><a href="/newspaper/foto_read'.$id.'"><center>
Добавить фото
</center></a></td>
<td class="podmenu"><a href="/newspaper/edit_read'.$id.'"><center>
Редактировать статью
</center></a></td>
<td class="podmenu"><a href="/newspaper/del_read'.$id.'"><center>
Удалить статью
</center></a></td></tr></table>';
}

echo '<div class="podmenu"><img src="images/newspaper.png" alt="*" /> '.$read['name'].'
<div class="divnews">';

if(!$read['scrin'] == NULL) echo '<img src="files/'.$read['scrin'].'" 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;"/><br />';

echo ''.nl2br(smile(bb($read['text']))).'</div></div>
<div class="podmenu"><img src="images/user.png" alt="*" /> Добавил: '.nick($read['us']).' ['.vremja($read['time']).']</div>
<div class="podmenu"><img src="images/oko.png" alt="*" /> Просмотров: '.$read['kolvo'].'</div>';

$zaklad = mysql_fetch_array(mysql_query('select * from `newspaper_zaklad` where `paper` = '.$id.' and `us` = "'.$user['id'].'"'));

if($zaklad == 0) {
echo '<div class="podmenu"><img src="images/star.png" alt="*" /> <a href="/newspaper/zaklad_read'.$id.'">Добавить в закладки</a></div>';
} else {
echo '<div class="podmenu"><img src="images/star.png" alt="*" /> <a href="/newspaper/zaklad_read'.$id.'">Удалить из закладок</a></div>';
}

break;
############################
###### Добавить фото  ######
############################
case 'foto_read':

$id = abs(intval($_GET['id']));
$read = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_paper` WHERE `id` = '".$id."'"));
$kat = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_kat` WHERE `id` = '".$read['kat']."'"));

if($read == 0) {
echo '<div class="title">'.$title.' | Ошибка</div><div class="podmenu"><center><b>Такой статьи не существует!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

if($user['level'] < 1 && $user['id'] != $read['us']) {
header('Location: '.$HOME.'/newspaper/read'.$id.'');
exit();
}

echo '<div class="title"><a href="/newspaper">'.$title.'</a> | <a href="/newspaper/kat'.$kat['id'].'">'.$kat['name'].'</a> | <a href="/newspaper/read'.$id.'">'.$read['name'].'</a> | Добавить фото</div>';

if (isset($_REQUEST['submit'])) {

$maxsize = 2;
$size = $_FILES['filename']['size'];


$filetype = array ( 'jpg', 'gif', 'png', 'jpeg', 'bmp' );
$upfiletype = substr($_FILES['filename']['name'],  strrpos( $_FILES['filename']['name'], ".")+1);

if ($size > (1048576 * $maxsize)) $err ='Максимальный размер файла '.$maxsize.'мб!';
if(!in_array($upfiletype,$filetype)) $err ='К загрузке разрешены файлы форматом jpg,gif,png,jpeg,bmp!';
if(!@file_exists($_FILES['filename']['tmp_name'])) $err ='Вы не выбрали файл!';

if($err) {
echo '<div class="podmenu"><center><b>'.$err.'</b></center></div>';
require_once ('../system/footer.php');
exit();
}

$files = 'files_'.rand(1234,5678).'_'.rand(1234,5678).'_'.$_FILES['filename']['name'];
move_uploaded_file($_FILES['filename']['tmp_name'], "files/".$files."");

mysql_query("UPDATE `newspaper_paper` SET `scrin` = '".$files."' WHERE `id` = '".$id."'");
header('Location: /newspaper/read'.$id.'');
exit();
}

if (isset($_REQUEST['ok'])) {
mysql_query("UPDATE `newspaper_paper` SET `scrin` = '' WHERE `id` = '".$id."'");
header('Location: /newspaper/read'.$id.'');
exit();
}

echo '<div class="podmenu"><form action="" method="post" enctype="multipart/form-data">
Фото:<br /><input type="file" name="filename"/><br />
<input type="submit" name="submit" value="Загрузить" />
</form></div>
<div class="menudiv"><a href="?ok" class="k_menu">Удалить фото</div></a>
<div class="menudiv"><a href="/newspaper/read'.$id.'" class="k_menu">Назад</div></a>';

break;
case 'edit_read':

$id = abs(intval($_GET['id']));
$read = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_paper` WHERE `id` = '".$id."'"));
$kat = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_kat` WHERE `id` = '".$read['kat']."'"));

if($read == 0) {
echo '<div class="title">'.$title.' | Ошибка</div><div class="podmenu"><center><b>Такой статьи не существует!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

if($user['level'] < 1 && $user['id'] != $read['us']) {
header('Location: '.$HOME.'/newspaper/read'.$id.'');
exit();
}

echo '<div class="title"><a href="/newspaper">'.$title.'</a> | <a href="/newspaper/kat'.$kat['id'].'">'.$kat['name'].'</a> | <a href="/newspaper/read'.$id.'">'.$read['name'].'</a> | Редактирование статьи</div>';

if (isset($_REQUEST['submit'])) {

$name = strong($_POST['name']);
$text = strong($_POST['text']);

if(mb_strlen($text) < 50) $err = 'Минимум для ввода 50 символов!';
if(empty($text)) $err = 'Введите текст статьи!';
if(mb_strlen($name) < 5) $err = 'Минимум для ввода 5 символов!';
if(empty($name)) $err = 'Введите название статьи!';

if($err) {
echo '<div class="podmenu"><center><b>'.$err.'</b></center></div>';
require_once ('../system/footer.php');
exit();
}


mysql_query("UPDATE `newspaper_paper` SET `name` = '".$name."',`text` = '".$text."' WHERE `id` = '".$id."'");
header('Location: /newspaper/read'.$id.'');
exit();
}

echo '<div class="podmenu"><form action="" method="POST">
Название статьи:<br /> <input type="name" name="name" value="'.$read['name'].'"/><br />
Текст статьи:<br /><textarea name="text">'.$read['text'].'</textarea><br />
<input type="submit" name="submit" value="Изменить" />
</form></div>
<div class="menudiv"><a href="/newspaper/read'.$id.'" class="k_menu">Назад</div></a>';

break;
case 'del_read':

$id = abs(intval($_GET['id']));
$read = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_paper` WHERE `id` = '".$id."'"));
$kat = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_kat` WHERE `id` = '".$read['kat']."'"));

if($read == 0) {
echo '<div class="title">'.$title.' | Ошибка</div><div class="podmenu"><center><b>Такой статьи не существует!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

if($user['level'] < 1 && $user['id'] != $read['us']) {
header('Location: '.$HOME.'/newspaper/read'.$id.'');
exit();
}

if(isset($_REQUEST['ok'])) {

mysql_query("DELETE FROM `newspaper_paper` where `id` = '".$id."'");

header('Location: /newspaper/kat'.$kat['id'].'');
exit();
}


echo '<div class="title"><a href="/newspaper">'.$title.'</a> | <a href="/newspaper/kat'.$kat['id'].'">'.$kat['name'].'</a> | <a href="/newspaper/read'.$id.'">'.$read['name'].'</a> | Удаление статьи</div>
<div class="podmenu">Вы действительно хотите удалить эту статью?<br /><a href="?ok">Да</a> | <a href="/newspaper/read'.$id.'">Нет</a></div>';

break;
case 'zaklad_read':

$id = abs(intval($_GET['id']));
$read = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_paper` WHERE `id` = '".$id."'"));
$kat = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_kat` WHERE `id` = '".$read['kat']."'"));

if($read == 0) {
echo '<div class="title">'.$title.' | Ошибка</div><div class="podmenu"><center><b>Такой статьи не существует!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

$zaklad = mysql_fetch_array(mysql_query('select * from `newspaper_zaklad` where `paper` = '.$id.' and `us` = "'.$user['id'].'"'));

if($zaklad == 0) {
mysql_query("INSERT INTO `newspaper_zaklad` SET `paper` = '".$id."', `us` = '".$user['id']."' ");
header('Location: /newspaper/read'.$id.'');
exit();
} else {
mysql_query("DELETE FROM `newspaper_zaklad` WHERE `id` = '".$zaklad['id']."'");
header('Location: /newspaper/read'.$id.'');
exit();
}

break;
case 'my_zaklad':

echo '<div class="title"><a href="/newspaper/">'.$title.'</a> | Мои закладки</div>';

if (empty($user['max'])) $user['max']=10;
$max = $user['max'];
$k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `newspaper_zaklad` WHERE `us` = '".$user['id']."' "),0);
$k_page = k_page($k_post,$max);
$page = page($k_page);
$start = $max*$page-$max;

$b = mysql_query("SELECT * FROM `newspaper_zaklad` WHERE `us` = '".$user['id']."' ORDER BY `id` DESC LIMIT $start, $max");
while($a = mysql_fetch_assoc($b))
{

$a = mysql_fetch_assoc(mysql_query("SELECT * FROM `newspaper_paper` WHERE `id` = '".$a['paper']."'"));

$array = explode(" ", $a['text']);
$text_num = array_slice($array, 0, 35);
$text_v = implode(" ", $text_num);
$text_v .= '...';

echo '<div class="menudiv"><a href="/newspaper/read'.$a['id'].'" class="k_menu">
<img src="images/newspaper.png" alt="*" /> '.$a['name'].' <br />
<div class="divnews">'.smile(bb($text_v)).'</div></a></div>';
}

if($k_post < 1) {
echo '<div class="podmenu"><b><center>У вас нет закладок!</center></b></div>';
}

if ($k_page>1) {
echo str('/newspaper/zaklad'.$id.'?',$k_page,$page); // Вывод страниц
}

break;
case 'search':

echo '<div class="title"><a href="/newspaper">'.$title.'</a> | Поиск по газете</div>';

echo '<div class="podmenu"><form action="" method="POST">
Текст: <br />
<input type="text" name="text" value="" maxlength="30" /><br />
<input type="submit" name="submit" value="Искать" />
</form></div>';

if(isset($_REQUEST['submit'])) {

$text = strong($_POST['text']);

if(strlen($text) <1) {
echo '<div class="podmenu"><center><b>Минимальная длина запроса 1 символ!</b></center></div>';
require_once ('../system/footer.php');
exit();
}

echo '<div class="menu"><img src="/design/gradient/lol.png" alt="*" /> Результаты поиска:</div>';

$s = mysql_query("SELECT * FROM `newspaper_paper` where `name` LIKE '%".$text."%' ORDER BY `id` DESC ");
$sql = mysql_result(mysql_query("SELECT COUNT(*) FROM `newspaper_paper` where `name` LIKE '%".$text."%' "),0);

/* Выводим */
while($a = mysql_fetch_assoc($s)){

$array = explode(" ", $a['text']);
$text_num = array_slice($array, 0, 35);
$text_v = implode(" ", $text_num);
$text_v .= '...';

echo '<div class="menudiv"><a href="/newspaper/read'.$a['id'].'" class="k_menu">
<img src="images/newspaper.png" alt="*" /> '.$a['name'].' <br />
<div class="divnews">'.smile(bb($text_v)).'</div></a></div>';
}
}

if($sql == 0) echo '<div class="podmenu"><center><b>По вашему запросу ничего не найдено!</b></center></div>';

break;
}

require_once ('../system/footer.php');
?>