<?php

/* DCMS Special
 * Дата последнего редактирования 25.09.2016
 * Модифицировал densnet
 */

foreach (array('start', 'compress', 'sess', 'settings', 'db_connect', 'ipua', 'fnc', 'user') as $inc) {
    require_once "../../sys/inc/$inc.php";
}

access('adm_news_edit', null, 'index.php?' . SID);

$news = mysql_fetch_assoc(mysql_query("SELECT * FROM `news` WHERE `id` = '" . intval($_GET['edit']) . "' LIMIT 1"));

if (mysql_result(mysql_query("SELECT COUNT(*) FROM `news` WHERE `id` = '$news[id]'"), 0) == 0) {
    header("Location:?");
    exit;
}

if (isset($_GET['img'])) {
    if (isset($_GET['save']) && isset($_POST['add'])) {
        if (isset($_FILES['file'])) {
            $type = $_FILES['file']['type'];
            if ($type !== 'image/jpeg' && $type !== 'image/jpg' && $type !== 'image/gif' && $type !== 'image/png') {
                $err = lang('Это не изображение');
            }
        }
        if (!isset($err)) {
            $tmp = $_FILES['file']['tmp_name'];
            unlink(H . 'modules/news/images/' . $news['id'] . '.png');
            move_uploaded_file($tmp, H . 'modules/news/images/' . $news['id'] . '.png');
            chmod(H . 'modules/news/images/' . $news['id'] . '.png', 0777);
            header("location: ?edit=$news[id]");
        }
    }
    err();

    #Навигация
    echo "<div class='list-group-item-null list-group-item-grey'><small>";
    echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='/modules/news/'>" . lang('Новости') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='/modules/news/?news=$news[id]'>" . cutStr($news['title'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Файл');
    echo "</small></div><br />";

    echo "<form method='post' class='list-group-item-null' action='?edit=$news[id]&amp;img&amp;save' enctype='multipart/form-data'>";

    $doc->File('file', 'Файл');

    $doc->Button('btn btn-success btn-sm', 'add', 'plus', 'Добавить');

    echo "</form>";
} else {
    if (isset($_POST['title']) && isset($_POST['msg']) && isset($_POST['link']) && isset($_POST['ok'])) {

        $title = esc($_POST['title'], 1);
        $link = esc($_POST['link'], 1);

        if ($link != NULL && !preg_match('#^https?://#i', $link) && !preg_match('#^/#', $link)) {
            $link = '/' . $link;
        }

        $msg = esc($_POST['msg']);

        if (utf8_strlen($title) > 250) {
            $err = lang('Заголовок длинее 250-ти символов');
        }

        if (utf8_strlen($title) < 3) {
            $err = lang('Заголовок короче 3-х символов');
        }

        $mat = antimat($title);

        if ($mat) {
            $err[] = lang("В заголовке обнаружен мат") . ": $mat";
        }

        if (utf8_strlen($msg) > 10024) {
            $err = lang('Содержание длинее 10024-х символов');
        }

        if (utf8_strlen($msg) < 5) {
            $err = lang('Содержание короче 5-ти символов');
        }

        $mat = antimat($msg);

        if ($mat) {
            $err[] = lang("В содержании обнаружен мат") . ": $mat";
        }

        $title = mysql_real_escape_string($_POST['title']);
        $msg = mysql_real_escape_string($_POST['msg']);

        $news['close'] = intval($_POST['close']);
        mysql_query("UPDATE `news` SET `close` = '" . mysql_real_escape_string($news['close']) . "' WHERE `id` = '$news[id]' LIMIT 1");

        if (!isset($err)) {
            $ch = intval($_POST['ch']);
            $mn = intval($_POST['mn']);
            $main_time = time() + $ch * $mn * 60 * 60 * 24;

            if ($main_time <= time()) {
                $main_time = 0;
            }

            mysql_query("UPDATE `news` SET `title` = '$title', `msg` = '$msg', `link` = '$link', `main_time` = '$main_time', `time` = '$time' WHERE `id` = '$news[id]' LIMIT 1");
            mysql_query("UPDATE `user` SET `news_read` = '0'");
            $_SESSION['message'] = lang('Изменения сохранены');
            header("Location: ?news=$news[id]");
            exit;
        }
    }

    $set['title'] = lang('Новости') . ' - ' . lang('Редактирование');

    require_once H . 'sys/inc/thead.php';

    err();

    if (isset($_GET['del_img']) && is_file(H . 'modules/news/images/' . $news['id'] . '.png')) {
        unlink(H . 'modules/news/images/' . $news['id'] . '.png');
        header("Location:?edit=$news[id]");
    }

    #Навигация
    echo "<div class='list-group-item-null list-group-item-grey'><small>";
    echo "<a href='/' class='hint--right' data-hint='" . lang('На главную') . "'><i class='fa fa-home fa-lg'></i></a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='/modules/news/'>" . lang('Новости') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo "<a href='/modules/news/?news=$news[id]'>" . cutStr($news['title'], 30) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
    echo lang('Редактирование');
    echo "</small></div><br />";

    echo "<form class='list-group-item-null' name='edit' method='POST' action='?edit=$news[id]'>\n";
    echo lang('Заголовок новости') . "<br />";
    echo "<input name='title' maxlength='250' length='250' type='text' value='$news[title]' class='form-control'>";
    echo lang('Ссылка на источник') . "<br />";
    echo "<input name='link' maxlength='64' length='64' type='text' value='$news[link]' class='form-control'>";

    echo lang('Содержание новости') . "<br />";
    echo auto_bb("add", "msg");
    echo "<textarea name='msg' maxlength='10024' length='10024' rows='' class='form-control'>$news[msg]</textarea><br />";

    echo lang('Файл') . ": ";
    if (is_file(H . 'modules/news/images/' . $news['id'] . '.png')) {
        echo "<a href='?edit=$news[id]&amp;img'><small>" . lang('Заменить') . "</small></a> | ";
        echo "<a href='?edit=$news[id]&amp;del_img'><small>" . lang('Удалить') . "</small></a>";
    } else {
        echo "<a href='?edit=$news[id]&amp;img' title='" . lang('Прикрепить изображение') . "'><i class='fa fa-paperclip fa-fw'></i> " . lang('Прикрепить') . "</a>";
    }

    if (is_file(H . 'modules/news/images/' . $news['id'] . '.png')) {
        echo "<br /><img src='/modules/news/images/$news[id].png' style = 'border: 1px solid #CCDDED; padding: 2px; border-radius: 5px; max-width: 150px;' />";
    }

    echo "<hr>";

    echo lang('Показывать');
    echo "<input name='ch' maxlength='64' type='number' value='" . (isset($_POST['ch']) ? intval($_POST['ch']) : '1') . "' class='form-control' style='width: 50%'><br />";

    echo "<select class='form-control' name='mn' style='width: 50%'>";
    echo "<option value='0' " . (isset($_POST['mn']) && $_POST['mn'] == 0 ? "selected='selected'" : null) . ">" . lang('Не показывать') . "</option>";
    echo "<option value='1' " . (isset($_POST['mn']) && $_POST['mn'] == 1 ? "selected='selected'" : null) . ">" . lang('Дней') . "</option>";
    echo "<option value='7' " . (isset($_POST['mn']) && $_POST['mn'] == 7 ? "selected='selected'" : null) . ">" . lang('Недель') . "</option>";
    echo "<option value='31' " . (isset($_POST['mn']) && $_POST['mn'] == 31 ? "selected='selected'" : null) . ">" . lang('Месяцев') . "</option>";
    echo "</select>";

    echo "<hr>";

    echo lang('Обсуждение') . "<br />";
    echo "<label class='custom-control custom-radio'><input value='0' name='close' type='radio' " . ($news['close'] == 0 ? ' checked="checked"' : null) . " class='custom-control-input'><span class='custom-control-indicator'></span> <span class='custom-control-description'><i class='fa fa-unlock fa-fw'></i> " . lang('Открытое') . "</span></label><br />";
    echo "<label class='custom-control custom-radio'><input value='1' name='close' type='radio' " . ($news['close'] == 1 ? ' checked="checked"' : null) . " class='custom-control-input'><span class='custom-control-indicator'></span> <span class='custom-control-description'><i class='fa fa-lock fa-fw'></i> " . lang('Закрытое') . "</span></label>";

    echo "<hr>";

    $doc->Button('btn btn-success btn-sm', 'ok', 'save', 'Сохранить');

    echo "</form>";
}

require_once H . 'sys/inc/tfoot.php';
