<?php

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

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


if (isset($_GET['id'])) {
    $wall['id'] = intval($_GET['id']);
}

$wall = mysql_fetch_array(mysql_query("SELECT * FROM `public_wall` WHERE `id` = '" . $wall['id'] . ")'"));

if ($wall['id'] == intval($_GET['id'])) {
    $public = mysql_fetch_array(mysql_query("SELECT * FROM `public` WHERE `id` = '" . $wall['id_public'] . "' LIMIT 1"));
    if (isset($_GET['otvet']) && $_GET['otvet'] == 'add' && intval($_GET['pos'])) {
        $set['title'] = lang('Комментарии') . ' ' . $public['name'];
        require_once H . 'sys/inc/thead.php';

        $wall_komm = mysql_fetch_array(mysql_query("SELECT * FROM `public_wall_komm` WHERE `id` = '" . intval($_GET['pos']) . "'"));
        $ank = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '" . $wall_komm['id_user'] . "'"));

        if (isset($_POST['komm'])) {
            $komm = $_POST['komm'];
            $ank = $_POST['ank'];

            if (!isset($err)) {
                mysql_query("INSERT INTO `public_wall_komm` (`id_row`, `id_user`, `msg`, `time`, `id_kont`) values ('" . $wall['id'] . "', '" . $user['id'] . "', '" . $komm . "', '" . $time . "', '" . $user['id'] . "')");
                $id_komm = mysql_insert_id();

                $msg = "$SexOtv " . lang('Вам на сообщение в ') . " [url=/modules/public/otvet.php?id=" . $wall['id'] . "&komm=all]" . lang('записи на стене') . "[/url] ";
                mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$ank', '3', '$msg', '$time')");

                if (!isset($_GET['ajax'])) {
                    $_SESSION['message'] = lang('Сообщение успешно добавлено');
                    header('Location: otvet.php?id=' . $wall['id'] . '&komm=all');
                    exit;
                }
            }
        }


        aut();
        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='/public.php'>" . lang('Сообщества') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='index.php?id=$public[id]'>" . toOutput($public['name']) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Комментарии к записи');
        echo "</small></div><br />";

        $q = mysql_query("SELECT * FROM `public_wall_komm` WHERE `id` = '" . $wall_komm['id'] . "' AND `id_row` = '" . $wall['id'] . "' ORDER BY id DESC LIMIT 1");
        while ($pos = mysql_fetch_array($q)) {
            echo "<table class='list-group-item-null'><tr><td class='icon14' valign='top'>";
            if ($wall['id_pub'] > 0) {
                $fot = mysql_fetch_array(mysql_query("SELECT * FROM `public_foto` WHERE `id_public` = '" . $wall['id_pub'] . "' AND `foto` = '1' LIMIT 1"));
                if (is_file(H . "sys/public/ava/" . $wall['id_pub'] . "." . $fot['ras'] . "")) {
                    echo "<img src='/sys/public/ava/" . $wall['id_pub'] . "." . $fot['ras'] . "' width='50'/>";
                } else {
                    echo "<img src='/modules/public/images/no_foto.jpg' width='50'/>";
                }
            } else {
                echo avatar($wall['id_ank'], '48', 'border-radius: 2px;');
            }
            echo "</td><td class='null'>";

            echo "<b>" . toOutput($wall['name']) . "</b><br />";
            if ($wall['ras'] == 'jpg')
                echo "<center><img src='" . $wall['link'] . "' width='30%' alt='Скрин...' /></center><br />\n";

            if ($wall['ras'] == 'jpeg')
                echo "<center><img src='" . $wall['link'] . "' width='30%' alt='Скрин...' /></center><br />\n";

            if ($wall['ras'] == 'gif')
                echo "<center><img src='" . $wall['link'] . "' width='30%' alt='Скрин...' /></center><br />\n";

            if ($wall['ras'] == 'png')
                echo "<center><img src='" . $wall['link'] . "' width='30%' alt='Скрин...' /></center><br />\n";

            if ($wall['ras'] == 'mp3') {
                if (IS_WEB) {
                    echo '<center>
     <audio controls="controls" style="width:100%">
     <source src="' . $wall['link'] . '" type="audio/ogg; codecs=vorbis">
    Тег audio не поддерживается вашим браузером. 
  </audio>
  </center>';
                }
                echo "<a href='" . $wall['link'] . "'>Скачать mp3</a><br />";
            }
            if ($wall['ras'] == 'mp4') {
                if (IS_WEB) {
                    echo "<center><object type=application/x-shockwave-flash data=/sys/uppod.swf width=60% height=210>
<param name=bgcolor value=#ffffff />
<param name=allowFullScreen value=true />
<param name=allowScriptAccess value=always />
<param name=wmode value=transparent />
<param name=movie value=/sys/uppod.swf/>
<param name=flashvars value=file=" . $wall['link'] . "&amp;st=/sys/public.txt />
</object></center>";
                } else {
                    echo "<center><img src='/sys/obmen/screens/128/" . $file_id['id'] . ".gif' width='60%' alt='scr...' /></center><br />\n";
                }
                echo "<a href='" . $wall['link'] . "'>Скачать mp4</a><br />";
            }
            if ($wall['ras'] == 'flv') {
                if (IS_WEB) {
                    echo "<center><object type=application/x-shockwave-flash data=/sys/uppod.swf width=60% height=210>
<param name=bgcolor value=#ffffff />
<param name=allowFullScreen value=true />
<param name=allowScriptAccess value=always />
<param name=wmode value=transparent />
<param name=movie value=/sys/uppod.swf/>
<param name=flashvars value=file=" . $wall['link'] . "&amp;st=/sys/public.txt />
</object></center>";
                } else {
                    echo "<center><img src='/sys/obmen/screens/128/" . $file_id['id'] . ".gif' width='60%' alt='scr...' /></center><br />\n";
                }
                echo "<a href='" . $wall['link'] . "'>Скачать flv</a><br />";
            }
            echo toOutput($wall['opis']);
            echo "</td></tr></table>";
            echo "<table class='list-group-item-null list-group-item-grey'><tr><td valign='top' class='icon14'>";
            echo avatar($ank['id'], '48');
            echo "</td><td class='null'>";
            echo "<small style='color: grey;float: right;'>" . date::timek($pos['time']) . "</small>";

            echo user($ank['id']);
            echo "<br />";

            echo toOutput($pos['msg']);
            echo "</td></tr></table>";
        }
        if (isset($user)) {
            echo "<form method='post' class='list-group-item-null' name='message' action='?id=" . $wall['id'] . "&amp;otvet=add&amp;pos=" . $wall_komm['id'] . "'>\n";
            echo "<textarea name='komm' class='form-control'></textarea><br />\n";
            echo "<input type='hidden' name='ank' value='" . $ank['id'] . "' />";
            $doc->Button('btn btn-primary btn-sm', null, 'pencil', 'Опубликовать');
            echo "</form>";
        }
    }


##########################################################################

    if (isset($_GET['komm']) && $_GET['komm'] == 'all') {
        $set['title'] = lang('Коментарии') . ' - ' . $public['name'];
        require_once H . 'sys/inc/thead.php';

        $ank = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '" . $wall['id_wall'] . "'"));

        if (isset($_POST['komm'])) {
            $komm = $_POST['komm'];
            $ank = $_POST['ank'];
            if (!isset($err)) {
                $us = mysql_fetch_assoc(mysql_query("SELECT * FROM `user` WHERE `id` = '" . $ank . "'"));
                mysql_query("INSERT INTO `public_wall_komm` (`id_row`, `id_user`, `msg`, `time`, `id_kont`) values('" . $wall['id'] . "', '" . $user['id'] . "', '" . $komm . "', '" . $time . "', '" . $us['id'] . "')");
                if (isset($user) && $user['id'] != $us['id']) {
                    $id_komm = mysql_insert_id();
                    //$msg = "$SexOst " . lang('сообщение в ') . " [url=/modules/public/otvet.php?id=" . $id_komm . "&komm=all]" . lang('Вашей') . "[/url] " . lang('записи на стене') . " ";
                    //mysql_query("INSERT INTO `notification` (`id_user`, `id_kont`, `id_img`, `msg`, `time`) values('$user[id]', '$us[id]', '3', '$msg', '$time')");
                }

                if (!isset($_GET['ajax'])) {
                    $_SESSION['message'] = lang('Сообщение успешно добавлено');
                    header('Location: otvet.php?id=' . $wall['id'] . '&komm=all');
                }
                exit;
            }
        }


        aut();
        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='/public.php'>" . lang('Сообщества') . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo "<a href='index.php?id=$public[id]'>" . toOutput($public['name']) . "</a> <i class='fa fa-angle-right fa-fw'></i> ";
        echo lang('Комментарии к записи');
        echo "</small></div><br />";

        $q = mysql_query("SELECT * FROM `public_wall` WHERE `id` = '" . $wall['id'] . "' ORDER BY id DESC LIMIT 1");
        while ($pos = mysql_fetch_array($q)) {
            echo "<table class='list-group-item-null'><tr><td class='icon14' valign='top'>";
            if ($wall['id_pub'] > 0) {
                $fot = mysql_fetch_array(mysql_query("SELECT * FROM `public_foto` WHERE `id_public` = '" . $wall['id_pub'] . "' AND `foto` = '1' LIMIT 1"));
                if (is_file(H . "sys/public/ava/" . $wall['id_pub'] . "." . $fot['ras'] . "")) {
                    echo "<img src='/sys/public/ava/" . $wall['id_pub'] . "." . $fot['ras'] . "' width='48' />";
                } else {
                    echo "<img src='/modules/public/images/no_foto.jpg' width='48' />";
                }
            } else {
                echo avatar($wall['id_ank'], '48', 'border-radius: 2px;');
            }
            echo "</td><td class='null' valign='top'>";
            echo "<b>" . toOutput($wall['name']) . "</b><br />";
            if ($wall['ras'] == 'jpg')
                echo "<center><img src='" . $wall['link'] . "' width='30%' alt='Скрин...' /></center><br />\n";

            if ($wall['ras'] == 'jpeg')
                echo "<center><img src='" . $wall['link'] . "' width='30%' alt='Скрин...' /></center><br />\n";

            if ($wall['ras'] == 'gif')
                echo "<center><img src='" . $wall['link'] . "' width='30%' alt='Скрин...' /></center><br />\n";

            if ($wall['ras'] == 'png')
                echo "<center><img src='" . $wall['link'] . "' width='30%' alt='Скрин...' /></center><br />\n";

            if ($wall['ras'] == 'mp3') {
                if (IS_WEB) {
                    echo '<center>
     <audio controls="controls" style="width:100%">
    <source src="' . $wall['link'] . '" type="audio/mpeg">
    Тег audio не поддерживается вашим браузером. 
  </audio>
  </center>';
                }
                echo "<a href='" . $wall['link'] . "'><i class='fa fa-save fa-fw'></i> Скачать mp3</a><br />";
            }
            if ($wall['ras'] == 'mp4') {
                if (IS_WEB) {
                    echo "<center><object type=application/x-shockwave-flash data=/sys/uppod.swf width=75% height=210>
<param name=bgcolor value=#ffffff />
<param name=allowFullScreen value=true />
<param name=allowScriptAccess value=always />
<param name=wmode value=transparent />
<param name=movie value=/sys/uppod.swf/>
<param name=flashvars value=file=" . $wall['link'] . "&amp;st=/sys/public.txt />
</object></center>";
                } else {
                    echo "<center><img src='/sys/obmen/screens/128/" . $file_id['id'] . ".gif' width='75%' alt='scr...' /></center><br />\n";
                }
                echo "<a href='" . $wall['link'] . "'><i class='fa fa-save fa-fw'></i> Скачать mp4</a><br />";
            }
            if ($wall['ras'] == 'flv') {
                if (IS_WEB) {
                    echo "<center><object type=application/x-shockwave-flash data=/sys/uppod.swf width=75% height=210>
<param name=bgcolor value=#ffffff />
<param name=allowFullScreen value=true />
<param name=allowScriptAccess value=always />
<param name=wmode value=transparent />
<param name=movie value=/sys/uppod.swf/>
<param name=flashvars value=file=" . $wall['link'] . "&amp;st=/sys/public.txt />
</object></center>";
                } else {
                    echo "<center><img src='/sys/obmen/screens/128/" . $file_id['id'] . ".gif' width='75%' alt='scr...' /></center><br />\n";
                }
                echo "<a href='" . $wall['link'] . "'><i class='fa fa-save fa-fw'></i> Скачать flv</a><br />";
            }
            echo toOutput($wall['opis']);
            echo "</td></tr></table>";
        }

        if (isset($user)) {
            echo "<form method='post' name='message' class='list-group-item-null list-group-item-grey' action='?id=" . $wall['id'] . "&amp;komm=all'>\n";
            echo "<textarea name='komm' class='form-control' required placeholder='" . lang('Ваш комментарий') . "...'></textarea><br />\n";
            echo "<input type='hidden' name='ank' value='" . $ank['id'] . "' />";
            $doc->Button('btn btn-primary btn-sm', null, 'pencil', 'Опубликовать');
            echo "</form>";
        }
        $k_post = mysql_result(mysql_query("SELECT COUNT(*) FROM `public_wall_komm` WHERE `id_row` = '" . $wall['id'] . "'"), 0);
        $k_page = k_page($k_post, $set['p_str']);
        $page = page($k_page);
        $start = $set['p_str'] * $page - $set['p_str'];

        $qq = mysql_query("SELECT * FROM `public_wall_komm` WHERE `id_row` = '" . $wall['id'] . "' ORDER BY id DESC LIMIT $start, " . $set['p_str']);
        while ($post = mysql_fetch_array($qq)) {
            $us = mysql_fetch_array(mysql_query("SELECT * FROM `user` WHERE `id` = '" . $post['id_user'] . "'"));
            echo "<table class='list-group-item-null'><tr><td class='icon14'>";
            echo avatar($us['id'], '48', 'border-radius: 3px;');
            echo "</td><td class='null'>";
            echo "<small style='float: right;color: grey;'>" . date::timek($post['time']) . "</small>";
            echo user($us['id']);
            echo "<br />";
            echo toOutput($post['msg']);
            echo "<span style='float: right;'>";
            if (isset($user) && $user['id'] != $us['id']) {
                echo "<a href='?id=" . $wall['id'] . "&amp;otvet=add&amp;pos=" . $post['id'] . "' title='" . lang('Ответить') . "'><i class='fa fa-pencil fa-fw'></i></a>";
            }
            if (isset($user) && $user['id'] == $ank['id'] || $user['id'] == $public['id_user']) {
                echo "<a href='?id=" . $wall['id'] . "&amp;del=del&amp;pos=" . $post['id'] . "' title='" . lang('Удалить') . "'><i class='fa fa-trash-o fa-fw'></i></a>";
            }
            echo "</span>";

            echo "</td></tr></table>";
            echo "<div class='hr'></div>";
        }

        if ($k_page > 1) {
            str("?id=" . $wall['id'] . "&amp;komm=all&amp;", $k_page, $page);
        }
    }
} else {
    echo "<div class='list-group-item-null list-group-item-warning'>Запись Вам не принадлежит!</div>";
}

if (isset($_GET['del']) && $_GET['del'] == 'del' && intval($_GET['pos'])) {
    if (isset($user) && $user['id'] == $public['id_user']) {
        mysql_query("DELETE FROM `public_wall_komm` WHERE `id` = '" . intval($_GET['pos']) . "'");

        $_SESSION['message'] = lang('Комментарий успешно удален');
        header("Location: otvet.php?id=" . $wall['id'] . "&komm=all");
        exit;
    } else {
        $msg = "Совершил попытку удаления комментария записи в сообществе [b]$public[name][/b]";
        mysql_query("INSERT INTO `events` (`id_user`, `id_kont`, `msg`, `time`) values('$user[id]', '1', '" . $msg . "', '$time')");

        $_SESSION['message'] = lang('Администрация уведомлена');
        header("Location: otvet.php?id=$wall[id]&komm=all");
        exit;
    }
}

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