<?php

include 'system/sys.php';
include 'system/funcs.php';

if (!isset ($us['id'])) {
        header ('location: /error.php');
        exit;
}

/*
---
Форум
---
 */

switch ($_GET['mod']) {
        
        /*
        ---
        Главная
        ---
         */
        default:
        header ('location: /forum.php?mod=index');
        break;
        
        case index:
        
        H ('Форум', '<a href="/index.php" style="color:white;">Главная</a> | Форум');
        
        echo '<div class="list1">';

        echo '<img src="/design/tema.png" alt="" align="middle"/> Темы: <a href="/thems' . $us['id'] . '">Мои</a>';
        echo ' | ';
        echo '<a href="/forum.php?mod=stat&new_thems">Новые</a>';
        echo ' | ';
        echo '<a href="/forum.php?mod=stat&obn_thems">Обновлённые</a><br/>';
        echo '<img src="/design/npost.png" alt="" align="middle"/>Посты: <a href="/posts' . $us['id'] . '">Мои</a>';
        echo ' | ';
        echo '<a href="/forum?mod=stat&new_posts">Новые</a><br/>';
        echo '<img src="/images/book3.png" alt=""/> <a href="/forums/nep.php">Непрочитанное</a> (' . DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` LEFT JOIN `forum_visit` ON `forum_t`.`id`=`forum_visit`.`id_t` AND `forum_visit`.`us`='" . $us['id'] . "' WHERE `forum_t`.`type` != '2' AND (`forum_visit`.`id_t` IS NULL)") . ')';
        
        echo '</div>';
        
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_r` ORDER BY `id`");
        while ($r = $query -> fetch()) {
        
                echo '<div class="menu2"><img src="/design/inet.png" alt="" align="middle"/> <a href="/forum/razd' . $r['id'] . '/" style="text-decoration:none; color:white;"><b>' . $r['name'] . '</b></a> (' . DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`id_r`='" . $r['id'] . "') " . ($us['level'] > 0 ? '' : 'AND (`type`!=\'2\')')) . '/' . DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_r`='" . $r['id'] . "') " . ($us['level'] > 0 ? '' : 'AND (`del`=\'0\')')) . ')</div>';
                
                if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_pr` WHERE (`id_r`='" . $r['id'] . "')") != 0) {
                        
                        echo '<div class="raz">';
                        $q = DB :: $dbs -> query ("SELECT * FROM `forum_pr` WHERE (`id_r`='" . $r['id'] . "') ORDER BY `id`");
                        while ($pr = $q -> fetch ()) {
                        
                                echo '<img src="/design/categ.png" alt="" align="middle"/> <a href="/forum/' . $r['id'] . '/' . $pr['id']  .'">' . $pr['name'] . '</a> (' . DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`id_r`='" . $r['id'] . "') AND (`id_pr`='" . $pr['id'] . "') " . ($us['level'] > 0 ? '':'AND (`type`!=\'2\')')) . '/' . DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_r`='" . $r['id'] . "') AND (`id_pr`='" . $pr['id'] . "') " . ($us['level'] > 0 ? '' : 'AND (`del`=\'0\')')) . ')<br/>';
                            
                        }
                        echo '</div>';
                        
                        if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`id_r`='" . $r['id'] . "') " . ($us['level'] > 0 ? '' : 'AND (`type`!=\'2\')')) != 0) {
                        
                                $t   = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_t` WHERE (`id_r`='" . $r['id'] . "') " . ($us['level'] > 0 ? '' : 'AND (`type`!=\'2\')') . " ORDER BY `time` DESC LIMIT 1");                         
                                $c_p = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_r`='" . $t['id_r'] . "') AND (`id_pr`='" . $t['id_pr'] . "') AND (`id_t`='" . $t['id'] . "') " . ($us['level']>0 ? '' : 'AND (`del` = \'0\')'));
                                $pr  = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_pr` WHERE (`id`='" . $t['id_pr'] . "')");
                                
                                echo '<div class="raz">';
                                echo '<small>Подраздел: <a href="/forum/' . $t['id_r'] . '/' . $t['id_pr'] . '">' . $pr['name'] . '</a><br/>';
                                echo '<img src="/design/' . ($t['top']==1 ? 'prikrep' : ($t['type']==0 ? 'closed' : 'the')) . '.png" alt=""/> <a href="/forum/thema' . $t['id'] . '">' . $t['name'] . '</a> (' . $c_p . ') <a href="/forum/thema' . $t['id'] . '/page' . ceil ( $c_p / 10 ) .'">&gt;</a><br/>' . uNick ($t['us']) . '/' . uNick ($t['last']) . ' (' . datef ($t['time']) . ')</small>';
                                echo '</div>';
                        }
                    
                }
                else {
                        echo '<div class="navg">Подразделов нет!</div>';
                }
                echo '</div>';
        }
        echo '<div class="menu2">';

        echo '<img src="/design/alll.png" alt="" align="middle"/> <a href="/all/rulls.php?all" style="text-decoration:none; color:white;">Правила</a>';
        echo ' | ';
        echo '<img src="/design/emoc.png" alt="" align="middle"/> <a href="/all/smiles_r.php" style="text-decoration:none; color:white;">Смайлы</a>';
        echo ' | ';
        echo '<img src="/design/cod.png" alt="" align="middle"/>  <a href="/all/bb.php" style="text-decoration:none; color:white;">ББ коды</a>';
        echo ' | ';
        echo '<img src="/design/usear.png" alt="*" align="middle"/> <a href="/fsearch.php" style="text-decoration:none; color:white;">Поиск</a>';
        
        echo '</div>';
        
        break;

        /*
        ---
        Раздел
        ---
         */
        
        case r:
        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_r` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }
        $r = $query -> fetch ();
        H ('Раздел ' . $r['name'], '<a href="/forum.php" style="color:white;">Форум</a> | ' . $r['name']);
        if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_pr` WHERE (`id_r`='" . $r['id'] . "')")!=0) {
                $query = DB :: $dbs -> query ("SELECT * FROM `forum_pr` WHERE (`id_r`='" . $r['id'] . "') ORDER BY `id`");
                while ($pr = $query -> fetch ()) {
                        
                        echo '<div class="menu2"><img src="/design/categ.png" alt="" align="middle"/><a href="/forum/' . $r['id'] . '/' . $pr['id'] . '" style="text-decoration:none;color:white;">' . $pr['name'] . '</a> (' . DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`id_r`='" . $r['id'] . "') AND (`id_pr`='" . $pr['id'] . "') " . ($us['level']>0 ? '' : ' AND (`type`!=\'2\')')) . '/' . DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_r`='" . $r['id'] . "') AND (`id_pr`='" . $pr['id'] . "') " . ($us['level'] > 0 ? '':'AND (`del`=\'0\')')).')</div>';
                        
                        if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`id_r`='" . $r['id'] . "') AND (`id_pr`='" . $pr['id'] . "') " . ($us['level'] > 0 ? '' : 'AND (`type`!=\'2\')')) != 0) {
                                $t   = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_t` WHERE (`id_r`='" . $r['id'] . "') AND (`id_pr`='" . $pr['id'] . "') " . ($us['level'] > 0 ? '' : 'AND (`type`!=\'2\')') . " ORDER BY (`time`) LIMIT 1");
                                $c_p = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_r`='" . $r['id'] . "') AND (`id_pr`='" . $pr['id'] . "') AND (`id_t`='" . $t['id'] . "') " . ($us['level'] > 0 ? '':'AND (`del`=\'0\')'));
                                echo '<div class="navg">';
                                echo '<img src="/design/' . ($t['top'] ==1 ? 'prikrep' : ($t['type'] == 0 ? 'closed':'the')) . '.png" alt=""/> <a href="/forum/thema' . $t['id'] . '">' . $t['name'] . '</a> (' . $c_p .') <a href="/forum/thema' . $t['id'] . '/page' . ceil ($c_p / 10) . '">&gt;</a><br/>';
                                echo uNick ($t['us']) . '/' . uNick ($t['last']) . ' (' . datef ($t['time']) . ')';
                                echo '</div>';
                        }
                }
        }
        else {
        
                echo '<div class="list1">Подразделов нет!</div>';
        
        }
        break;

        /*
        ---
        Подраздел
        ---
         */

        case pr:
        if (!isset ($_GET['id']) || !isset ($_GET['r'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_pr` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }    
        $pr = $query -> fetch ();
        
        $_GET['r'] = intval ($_GET['r']);
        
        if ($pr['id_r']!=$_GET['r']) {
                header ('location: /forum.php');
                exit;
        }
        
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_r` WHERE (`id`=?)", array ($_GET['r']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }
        $r = $query -> fetch ();
                
        H ('Подраздел ' . $pr['name'], '<a href="/forum.php" style="color:white;">Форум</a> | <a href="/forum/razd' . $r['id'] . '" style="color:white;">' . $r['name'] . '</a> | ' . $pr['name']);
        
        echo '<div class="menu2"><img src="/design/nt.png" alt="" align="middle"/> <a href="/forum/' . $r['id'] . '/' . $pr['id'] . '?nt" style="text-decoration:none;color:white;">Новая тема</a></div>';
        
        if (isset ($_GET['nt'])) {
            
                echo '<div class="list1">';
                echo '<form action="/forum/' . $r['id'] . '/' . $pr['id'] . '?nt&add" method="post">';
                echo 'Название темы:<br/>';
                echo '<input type="text" name="name"/><br/>';
                echo 'Текст:<br/>';
                echo '<textarea name="text" rows="10"></textarea><br/>';
                echo '<input type="checkbox" name="podp"/> Оповещать об оставленных сообщениях<br/>';
                echo 'Ознакомьтесь с <a href="/forum/rulls' . $pr['id'] . '">правилами</a> п.д!<br/>';
                echo '<input type="submit" value="Добавить"/>';
                echo '</form>';
                echo '</div>';

                if (isset ($_GET['add'])) {
                        
                        if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`us`='" . $us['id'] . "') AND (`date`>'" . (time () - ( 60 * 15 )) . "')") != 0) {
                                
                                echo '<div class="list1"><b style="color:red;">Темы можно создавать только раз в 15 минут!</b></div>';
                                
                        }
                        else {
                        
                                $_POST['name'] = htmlspecialchars (addslashes (trim ($_POST['name'])));
                                $_POST['text'] = htmlspecialchars (addslashes (trim ($_POST['text'])));
                                if (empty ($_POST['name'])) {
                                        echo '<div class="list1"><b>Вы не ввели название темы..</b></div>';
                                }
                                elseif (empty ($_POST['text'])) {
                                        echo '<div class="list1"><b>Вы не ввели текст..</b></div>';                                
                                }
                                else {
                            
                                        DB :: $dbs -> query ("INSERT INTO `forum_t` (`id_r`,`id_pr`,`name`,`us`, `last`,`time`, `date`) VALUES ('" . $r['id'] . "', '" . $pr['id'] . "', '" . $_POST['name'] . "', '" . $us['id'] . "', '" . $us['id'] . "', '" . time () . "', '" . time () . "')");
                                        
                                        $id = DB :: $dbs -> lastInsertId ();
                                        
                                        DB :: $dbs -> query ("INSERT INTO `forum_p` (`id_r`,`id_pr`,`id_t`,`us`,`text`,`time`) VALUES ('" . $r['id'] . "','" . $pr['id'] . "','" . $id . "','" . $us['id'] . "','" . $_POST['text'] . "','" . time () . "')");
                                        
                                        if (isset ($_POST['podp'])) {
                                                DB :: $dbs -> query ("INSERT INTO `forum_podp` (`id_r`,`id_pr`, `id_t`,`us`) values ('" . $r['id'] . "', '" . $pr['id'] . "', '" . $id . "','" . $us['id'] . "')");
                                        }
                                        
                                        header ('location: /forum/thema' . $id);
                                
                                }
                                
                        }
                    
                }
                        
        }
        else {
            
            $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`id_pr`='" . $pr['id'] . "') " . ($us['level'] > 0 ? '':'AND (`type`!=\'2\')'));
            $items_per_page = 10;
            $pages = ceil ( $items / $items_per_page );
            
            $page = (int) $_GET['page'];
            if ($page < 1)
                    $page = 1;
            
            if ($page > $pages)
                    $page = $pages;
                    
            $start = $page * $items_per_page - $items_per_page;

            if ($items != 0) {
                    $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id_pr`='" . $pr['id'] . "') " . ($us['level'] > 0 ? '':'AND (`type`!=\'2\')') . " ORDER BY `top` DESC, `id` DESC LIMIT $start, $items_per_page");
                    while ($t = $query -> fetch ()) {
                    
                            $c_p = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_r`='" . $r['id'] . "') AND (`id_pr`='" . $pr['id'] . "') AND (`id_t`='" . $t['id'] . "') " . ($us['level'] > 0 ? '':'AND (`del`=\'0\')'));
                            
                            echo '<div class="navg">';
                            echo '<img src="/design/' . ($t['top'] != 0 ? 'prikrep' : ($t['type'] == 0 ? 'closed':'the')) . '.png" alt=""/> <a href="/forum/thema' . $t['id'] . '">' . $t['name'] . '</a> (' . $c_p .') <a href="/forum/thema' . $t['id'] . '/page' . ceil ($c_p / 10) . '">&gt;</a><br/>';

                            $c_f = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_f` WHERE (`id_t`= '" . $t['id'] . "')");
                            if ($c_f != 0) {
                                    echo '<img src="/images/page_white_get.png" alt="" align="middle"/> Файлов: <a href="/forum/files' . $t['id'] . '">' . $c_f . '</a><br/>';
                            }
                            
                            echo uNick ($t['us']) . '/' . uNick ($t['last']) . ' (' . datef ($t['time'])  .')';
                            echo '</div>';
                        
                    }
                    
                    echo '<div class="lst">' . pages ('/forum/' . $r['id'] . '/' . $pr['id']) . '</div>';
            
            }
            else {
            
                echo '<div class="lst">Тем нет!</div>';
            
            }
        
        }
        
        echo '<div class="menu2">';
        echo '<img src="/design/alll.png" alt="" align="middle"/> Правила: <a href="/all/rulls.php?all" style="text-decoration:none; color:white;">сайта</a>/<a href="/forum/rulls' . $pr['id'] . '" style="text-decoration:none; color:white;">подраздела</a>';
        echo ' | ';
        echo '<img src="/design/emoc.png" alt="" align="middle"/> <a href="/all/smiles_r.php" style="text-decoration:none; color:white;">Смайлы</a>';
        echo ' | ';
        echo '<img src="/design/cod.png" alt="" align="middle"/> <a href="/all/bb.php" style="text-decoration:none; color:white;">ББ коды</a>';
        echo ' | ';
        echo '<img src="/design/usear.png" alt="" align="middle"/> <a href="/fsearch.php" style="text-decoration:none; color:white;">Поиск</a>';
        echo '</div>';
        
        break;
        
        /*
         ---
         Правила подраздела
         ---
         */
        
        case rulls:
        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_pr` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }    
        $pr = $query -> fetch ();
                        
        H ('Правила подраздела ' . $pr['name'], 'Правила подраздела ' . $pr['name']);
        
        echo '<div class="list1">' . (empty ($pr['rulls']) ? 'Правил пока нет..' : bb ($pr['rulls'])) . '</div>';
        
        break;
    
    
        /*
        ---
        Тема
        ---
         */
    
        case thema:
        
        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }
        
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount ()==0) {
                header ('location: /forum.php');
                exit;
        }
        
        $t = $query -> fetch ();
        
        $a = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $t['us'] . "')");
        
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_pr` WHERE (`id`='" . $t['id_pr'] . "')");
        if ($query -> rowCount ()==0) {
                header ('location: /forum.php');
                exit;
        }

        $pr = $query -> fetch ();
        
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_r` WHERE (`id`='" . $t['id_r'] . "')");
        if ($query -> rowCount ()==0) {
                header ('location: /forum.php');
                exit;
        }

        $r = $query -> fetch ();
        
            
        H ($t['name'], '<a href="/forum" style="color:white;">Форум</a> | <a href="/forum/razd' . $r['id'] . '" style="color:white;">' . $r['name'].'</a> | <a href="/forum/' . $r['id'] . '/' . $pr['id'] . '" style="color:white;">' . $pr['name'] . '</a> | ' . $t['name']);
        
        if ($t['type']==2) {
            
                echo '<div class="list1">Тема удалена!</div>';
                
                if ($us['level']>1) {
                        if (isset ($_GET['up'])) {
                            
                                DB :: $dbs -> query ("UPDATE `forum_t` SET `type`='1' WHERE (`id`='" . $t['id'] . "')");
                                header ('location: /forum/thema' . $t['id']);
                        
                        }
                        
                        echo '<div class="list1"><a href="/forum/thema' . $t['id'] . '=up">Восстановить</a></div>';
                        
                }

        }
        else {


                if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_visit` WHERE (`id_t`='" . $t['id'] . "') AND (`us`='" . $us['id'] . "')") == 0) {
                        DB :: $dbs -> query ("INSERT INTO `forum_visit` (`id_r`, `id_pr`, `id_t`,`us`,`time`) VALUES ('" . $r['id'] . "', '" . $pr['id'] . "', '" . $t['id'] . "','" . $us['id'] . "','" . time () . "')");
                }
                else {
                        DB :: $dbs -> query ("UPDATE `forum_visit` SET `time`='" . time () . "' WHERE (`id_t`='" . $t['id'] . "') AND (`us`='" . $us['id'] . "')");
                }
                    
                $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_r`='" . $r['id'] . "') AND (`id_pr`='" . $pr['id'] . "') AND (`id_t`='" . $t['id'] . "') " . ($us['level']>0?'':'AND (`del`=\'0\')'));
                $items_per_page = 10;
                $pages = ceil ( $items / $items_per_page );
                $page = (int) $_GET['page'];

                if ($page < 1)
                        $page = 1;

                if ($page > $pages)
                        $page = $pages;
                    
                $start = $page * $items_per_page - $items_per_page;
            

                     if ($page == 1)
                                     $i =  $page;
                else if ($page == 2) $i =  $page + 9;
                else                 $i = ($page * 10) - 9;
            
            
            
            
            
                /*
                ---
                Ответ
                ---
                 */

                
                if (isset ($_GET['otv_i'])) {
                    
                        $query = DB :: $dbs -> query ("SELECT * FROM `us` WHERE (`id`=?)", array ((int) $_GET['otv_i']));
                        if ($query -> rowCount () == 0) {
                                header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                exit;
                        }
                        
                        $u = $query -> fetch ();
                        
                        if ($u['id']==$us['id']) {
                                header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                exit;
                        }
                        
                        echo '<div class="list1">';
                        echo '<form action="/form2/msg.php?id=' . $t['id'] . '&otv&otv_i=' . $u['id'] . '&page=' . $pages . '" method="post" enctype="multipart/form-data">';
                        echo 'Сообщение:<br/><textarea name="msg">[b]' . $u['nick'] . '[/b], </textarea><br/>';
                        if ($us['file']==1) {
                                echo 'Файл:<br/><input type="file" name="filename"/><br/>'; 
                        }
                        
                        echo '<input type="submit" value="Написать"/>';
                        echo '</form>';
                        echo '</div>';

                }
                /*
                ---
                Цитирование поста
                ---
                 */
                else if (isset ($_GET['cit'])) {
                    
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id`=?)", array ((int) $_GET['cit']));
                        if ($query -> rowCount () == 0) {
                                header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                exit;
                        }
                        $p = $query -> fetch ();
                        if ($p['del']!=0) {
                                header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                exit;
                        }
                        if ($p['us']==$us['id']) {
                                header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                exit;
                        }
                        
                        $query = DB :: $dbs -> query ("SELECT * FROM `us` WHERE (`id`='" . $p['us'] . "')");
                        if ($query -> rowCount () == 0) {
                                header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                exit;
                        }
                        $u = $query -> fetch ();
                        
                        echo '<div class="list1">';
                        echo 'Цитата:';
                        echo '<div class="cit"><b style="color:red;">' . $u['nick'] . '</b>: ' . bb ($p['text']) . '</div>';
                        echo '<form action="/form2/msg.php?id=' . $t['id'] . '&otv&cit=' . $p['id']  .'&page=' . $pages . '" method="post" enctype="multipart/form-data">';
                        echo 'Сообщение:<br/><textarea name="msg"></textarea><br/>';
                        if($us['file'] == 1) {
                            echo 'Файл:<br/><input type="file" name="filename"/><br/>';
                        }
                        
                        echo '<input type="submit" value="Написать"/>';
                        echo '</form>';
                        echo '</div>';

                }
                else {
                    
                        echo '<div class="list1">';
                        echo '<small>';
                        echo '<a href="/forum/thema' . $t['id'] . '/page' . $pages . '?' . passgen () . '">Обновить</a>';
                        echo ' | ';
                        echo '<a href="/forum/thema' . $t['id'] . '=thp">' . (DB :: $dbs -> querySingle ("SELECT * FROM `forum_podp` WHERE (`id_t`='" . $t['id'] . "') AND (`us`='" . $us['id'] . "')") == 0 ? 'Подписаться' : 'Отписаться') . '</a>';
                    
                        /*
                        ---
                        Подписка/Отписка с темы
                        ---
                         */
                        if (isset ($_GET['thp'])) {
                        
                                $query = DB :: $dbs -> query ("SELECT * FROM `forum_podp` WHERE (`id_t`='" . $t['id'] . "') AND (`us`='" . $us['id'] . "')");
                                if ($query -> rowCount () == 0) {
                                        DB :: $dbs -> query ("INSERT INTO `forum_podp` (`id_r`,`id_pr`,`id_t`,`us`) VALUES ('" . $r['id'] . "', '" . $pr['id'] . "', '" . $t['id'] . "','" . $us['id'] . "')");
                                }
                                else {
                                        DB :: $dbs -> query ("DELETE FROM `forum_podp` WHERE (`id_t`='" . $t['id'] . "') AND (`us`='" . $us['id'] . "')");
                                }
                            
                                header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                exit;
                        
                        }
                
                        /*
                        ---
                        Открыть/Зыкрыть тему
                        ---
                         */
                        if ($t['type'] == 1) {
                            
                                if ($a['id']==$us['id'] || $us['level']>$a['level']) {
                                    
                                        if (isset ($_GET['close'])) {
                                                DB :: $dbs -> query ("UPDATE `forum_t` SET `type`='0', `time`='" . time () . "',`last`='" . $us['id'] . "' WHERE (`id`='" . $t['id'] . "')");
                                                DB :: $dbs -> query ("INSERT INTO `forum_p` (`id_r`,`id_pr`,`id_t`,`us`,`text`,`time`) VALUES ('" . $r['id'] . "','" . $pr['id'] . "','" . $t['id'] . "','" . $us['id'] . "','[b]Тема закрыта для обсуждения![/b] :-)','" . time () . "')");
                                                header ('location: /forum/thema' . $t['id'] . '/page' . $pages);
                                                exit;
                                        }
                                        
                                        echo ' | ';
                                        echo '<a href="/forum/thema' . $t['id'] . '=close">Закрыть</a>';
                                    
                                }
                            
                        }
                        else {
                        
                                if ($a['id']==$us['id'] && $us['level']>0 || $us['level']>$a['level']) {
                                    
                                        if (isset($_GET['open'])) {
                                    
                                                DB :: $dbs -> query ("UPDATE `forum_t` SET `type`='1', `time`='" . time () . "',`last`='" . $us['id'] . "' WHERE (`id`='" . $t['id'] . "')");
                                                DB :: $dbs -> query ("INSERT INTO `forum_p` (`id_r`,`id_pr`,`id_t`,`us`,`text`,`time`) VALUES ('" . $r['id'] . "','" . $pr['id'] . "','" . $t['id'] . "','" . $us['id'] . "','[b]Тема открыта для обсуждения![/b] :-)','" . time () . "')");
                                                header ('location: /forum/thema' . $t['id'] . '/page' . $pages);
                                                exit;
                                        
                                        }
                                        
                                        echo ' | ';
                                        echo '<a href="/forum/thema' . $t['id'] . '=open">Открыть</a>';
                                    
                                }
                    
                        }
                
                        /*
                        ---
                        Открепить/Закрепить тему
                        ---
                         */
                        if ($t['us']==$us['id'] && $us['level']>1 || $us['level']>$a['level']) {
                            
                                if (isset ($_GET['top'])) {
                                        DB :: $dbs -> query ("UPDATE `forum_t` SET `top`='" . ($t['top']==0?1:0) . "' WHERE (`id`='" . $t['id'] . "')");
                                        header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                        exit;
                                }

                                echo ' | ';
                                echo '<a href="/forum/thema' . $t['id'] . '=top">' . ($t['top']==0?'Закрепить' : 'Открепить') . '</a>';
                        
                        }
                        
                    
                    
                    
                        if ($a['id']==$us['id'] && $us['level']>0 || $us['level']>$a['level']) {
                                echo ' | ';
                                echo '<a href="/forum.php?mod=thm&id=' . $t['id'] . '">Переместить</a>';
                        }
                            
                        if ($a['id']==$us['id'] && $us['level']>0 || $us['level']>$a['level']) {
                                echo ' | ';
                                echo '<a href="/forum.php?mod=thn&id=' . $t['id'].'">Переименовать</a>';
                        }
                    
                        /*
                        ---
                        Удаление темы
                        ---
                         */
                        if ($a['id']==$us['id'] && $us['level']>2 || $us['level']>$a['level']) {
                            
                                if (isset ($_GET['del'])) {
                                
                                        DB :: $dbs -> query ("UPDATE `forum_t` SET `type`='2' WHERE (`id`='" . $t['id'] . "')");
                                        header ('location:/forum/thema' . $t['id']);
                                
                                }
                                
                                echo ' | ';
                                echo '<a href="/forum/thema' . $t['id'].'=del">Удалить</a>';
                        
                        }    
                            
                        if ($t['type']==1) {
                            
                                if ($t['us']==$us['id'] || $us['level']>$a['level']) {
                                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_op` WHERE (`id_t`='" . $t['id'] . "')");
                                        if ($query -> rowCount () == 0) {
                                                echo ' | ';
                                                echo '<a href="/forum.php?mod=op_add&amp;thema=' . $t['id'] . '">Добавить опрос</a>';
                                        }
                                }

                        }
                        
                        echo '</small>';
                        echo '</div>';

                
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_op` WHERE (`id_t`='" . $t['id'] . "')");
                        if ($query -> rowCount () != 0) {
                                $op = $query -> fetch ();
                                echo '<div class="post2">';
                                echo 'Вопрос: ' . $op['vopros'] . ' (' . ($op['type']==1?'открытое':'закрытое') . ' голосование)<br/>';
                                if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_op_vote` WHERE (`id_op`='" . $op['id'] . "') AND (`us`='" . $us['id'] . "')")==0) {
                                        
                                        if (isset ($_GET['op_vote'])) {
                                                $_POST['otv'] = (int) $_POST['otv'];
                                                if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_op_otv` WHERE (`id_op`='" . $op['id'] . "') AND (`id`=?)", array ($_POST['otv'])) == 0) {
                                                        header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                                        exit;
                                                }
                                                DB :: $dbs -> query ("INSERT INTO `forum_op_vote` (`id_r`,`id_pr`,`id_t`,`id_op`,`id_otv`,`us`,`t`) VALUES ('" . $r['id'] . "', '" . $pr['id'] . "', '" . $t['id'] . "', '" . $op['id'] . "',?,'" . $us['id'] . "','" . time () . "')", array ($_POST['otv']));
                                                
                                                header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                                
                                        }
                                        
                                        
                                        echo '<form action="/forum/thema' . $t['id'] . '/page' . $page . '?op_vote" method="post">';
                                        echo '<select name="otv">';
                                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_op_otv` WHERE (`id_op`='" . $op['id'] . "')");
                                        while ($otv = $query -> fetch ()) {
                                                echo '<option value="' . $otv['id'] . '">' . $otv['text'] . '</option>';
                                        }
                                        echo '</select><br/>';
                                        echo '<input type="submit" value="Проголосовать"/>';
                                        echo '</form>';
                                }
                                else {
                                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_op_otv` WHERE (`id_op`='" . $op['id'] . "')");
                                        while ($otv = $query -> fetch ()) {
                                                echo '<b>' . $otv['text'] . '</b> (голосов: ' . DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_op_vote` WHERE (`id_op`='" . $op['id'] . "') AND (`id_otv`='" . $otv['id'] . "')") . ')<br/>';
                                        }
                                        
                                        if ($op['type']==1) {
                                                echo '- <a href="/form2/opros.php?id=' . $t['id'] . '">Список проголосовавших</a><br/>';
                                        }
                                }
                                
                                if ($a['id']==$us['id'] || $a['level']<$us['level']) {
                                        // 
                                        if (isset ($_GET['delo'])) {
                                        
                                                DB :: $dbs -> query ("DELETE FROM `forum_op` WHERE (`id_t`='" . $t['id'] . "')");
                                                DB :: $dbs -> query ("DELETE FROM `forum_op_otv` WHERE (`id_t`='" . $t['id'] . "')");
                                                DB :: $dbs -> query ("DELETE FROM `forum_op_vote` WHERE (`id_t`='" . $t['id'] . "')");
                                                
                                                header ('location: /forum/thema' . $t['id'] . '/page' . $page);
                                        }
                                        
                                        
                                        echo '- <a href="/forum.php?mod=vop_add&thema=' . $t['id'] . '&amp;op=' . $op['id'] . '">Добавить</a> | <a href="/forum.php?mod=redop&thema=' . $t['id'] . '&amp;op=' . $op['id'] . '">удалить</a> варианты<br/>';
                                        echo '- <a href="/forum.php?mod=thema&id=' . $t['id'] . '&amp;delo">Удалить опрос</a>';
                                      
                                }
                                
                                echo '</div>';
                        }


                if ($t['type'] == 0) {
                        echo '<div class="post2"><img src="/design/closed.png" alt="" align="middle"/> <b>Тема закрыта!</b></div>';
                }
            
                if ($items == 0) {
                        echo 'Сообщений нет..';
                }
                else {
            
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id_t`='" . $t['id'] . "') " . ($us['level']>0 ? '':'AND (`del`=\'0\')') . " ORDER BY `id` LIMIT $start, $items_per_page");
                        while ($p = $query -> fetch ()) {
                                $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['us'] . "')");

                                // display post
                                echo '<div class="lst">';
                                echo $i++ . '. ' . uNick ($u['id']) . ' ' . ( $p['us'] == $t['us'] ? '<b><font color="green">[автор]</font></b>' : '' ) . ' (' . datef ($p['time']) . ')';
                            
                                // if post is delete
                                if ($p['del'] != 0) {
                                
                                        $who = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['del'] . "')");
                                        echo ' <b style="color:red;">пост удалил</b> ' . uNick ($p['del']);
                                
                                }
                            
                                // if post recovered
                                if ($p['ups'] != 0) {
                                
                                        $who = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['ups'] . "')");
                                        echo ' <b style="color:green;">пост восстановил</b> ' . uNick ($p['ups']);
                                        
                                }
                            
                                if ($t['type'] == 1) {
                                        if ($p['us']==$us['id']) {                                
                                                echo ' [<a href="/forum/redk' . $p['id'] . '"">ред</a>] ';
                                        }
                                        else {
                                                if ($u['level']<$us['level']) {
                                                        echo ' [<a href="/forum/redk' . $p['id'] . '"">ред</a>] ';
                                                }
                                                if ($p['del'] == 0)  {
                                                        echo ' [<a href="/forum/thema' . $t['id'] . '=otv_i' . $p['us'] . '">отв</a>] [<a href="/forum/thema' . $t['id'] . '=cit' . $p['id'] . '">цит</a>] ';
                                                }
                                        }
                                }
                                
                                if ($p['us']!=$us['id']) {
                                        echo ' [<a href="/msg' . $p['us'] . '">лс</a>] ';
                                }  
                                
                                if ($p['us']!=$us['id']) {
                                        if ($u['level']<$us['level']) {
                                                echo ' [<a href="/nar.php?id=' . $u['id'] . '">нар</a>] ';
                                        }
                                }
                                
                                                               
                                if ($p['del']==0) {
                                
                                        echo ' [<a href="/form2/pos.php?id=' . $t['id'] . '&poslike=' . $p['id'] . '">под</a>] ';

                                
                                        if ($t['type'] == 1) {
                                                if ($p['us']!=$us['id']) {
                                                        if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_vote` WHERE (`id_t`='" . $t['id'] . "') AND (`us`='" . $p['us'] . "') AND (`kem`='" . $us['id'] . "')") == 0) {                                
                                                                echo '<a href="/forum.php?mod=vote&id=' . $p['id'] . '&vot=za&v=' . $p['us'] . '&page=' . $page .  '">+1</a> | <a href="/forum.php?mod=vote&id=' . $p['id'] . '&vot=protiv&v=' . $p['us'] . '&page=' . $page . '">-1</a>';;
                                                        }
                                                }
                                        }
                                }
                                
                            // 
                                if ($u['id'] == $us['id'] && $us['level']>1 || $u['level']<$us['level']) {
                                
                                        echo ' [<a href="/forum.php?mod=post&post=' . $p['id'] . '&page=' . $page . '">' . ( $p['del'] != 0 ? 'вос' : 'x' ) . '</a>]';
                                
                                }
                    
                                $za     = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_vote` WHERE (`id_p`='" . $p['id'] . "') AND (`vot`='za')");
                                $protiv = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_vote` WHERE (`id_p`='" . $p['id'] . "') AND (`vot`='protiv')");

                                // display post votes
                                echo ' (<span style="color:green;">' . $za . '</span>/<span style="color:red;">' . $protiv . '</span>)<br/>';
                        
                                // if post have quote
                                if ($p['cit'] != 0) {
                                    
                                        echo 'Цитата:';
                                        echo '<div class="cit">';
                                        
                                        $q = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id`='" . $p['cit'] . "')");                
                                        if ($q -> rowCount () == 0) {
                                            
                                                echo '<b>Сообщение удалено!</b>';
                                        
                                        }
                                        else {
                                                
                                                // 
                                                $cit = $q -> fetch ();
                                                if ($cit['del'] !=0 ) {
                                                
                                                        echo '<b>Сообщение удалено!</b>';
                                                
                                                }
                                                else {
                                                    
                                                        $citu = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $cit['us'] . "')");
                                                        echo '<b style="color:red;">' . $citu['nick'] . '</b>: ' .SM( bb ($cit['text']));
                                                
                                                }
                                        
                                        }
                                        
                                        echo '</div>';
                                
                                }
                        
                                // display post text
                                echo ''.bb(SM($p['text'])).'<br/>';

                            
                                // if post have attachment files
                                if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_f` WHERE (`id_p`='" . $p['id'] . "')") != 0) {
                                    
                                        echo '<b>Прикрепленные файлы:</b><br/>';
                                                        
                                        $q = DB :: $dbs -> query ("SELECT * FROM `forum_f` WHERE (`id_p`='" . $p['id'] . "') ORDER BY `id`");
                                        while ($f = $q -> fetch ()) {
                                            
                                                echo ' <a href="/files/forum/' . $f['name'] . '">' . $f['name'] . '</a> (' . sizef ($f['size']) . ') ' . ($f['size'] != 0 ? ($p['us']==$us['id'] && $us['level'] > 1 || $u['level']<$us['level'] && $us['level']>1 ? ' [<a href="/forum.php?mod=file&id=' . $f['id'] . '&page=' . $page . '">x</a>]' : '' ) : '') . '<br/>';
                                            
                                        }
                                }
                                
                                
                    
                                // if post has been edition 
                                $q = DB :: $dbs -> query ("SELECT * FROM `forum_reds` WHERE (`id_p`='" . $p['id'] . "')");
                                if ($q -> rowCount () != 0) {
                                    
                                    // find last edit message
                                    $last_redk = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_reds` WHERE (`id_p`='" . $p['id'] . "') ORDER BY `id` DESC LIMIT 1");
                                    
                                    echo '_____<br/>';
                                    echo '<small>Пост изменён <a href="/forums/reds.php?id=' . $p['id'] . '">' . $q -> rowCount () . ' раз.</a> Посл. ред. ' . uNick ($last_redk['us']) . ' (' . datef ($last_redk['time']) . ')</small>';
                                
                                }
                                
                    
                            // if post user have status
                            if(isset ($u['status'])) {
                                    echo '<hr/>';
                                    echo '<b>Статус: ' . $u['status'] . '</b>';
                            }
                            
                            echo '</div>';

                        }
                }


                    if ($t['type'] == 1) {
                        
                            echo '<div class="list1">';
                            echo '<form action="/form2/msg.php?id=' . $t['id'] . '&otv&page=' . $pages . '" method="POST" enctype="multipart/form-data">';
                            echo 'Сообщение:<br/><textarea name="msg"></textarea><br/>';                    
                            
                            // 
                            if ($us['file']==1) {
                                    echo 'Файл:<br/><input type="file" name="filename"/><br/>';
                            }
                            
                            echo '<input type="submit" value="Написать"/>';
                            echo '</form>';
                            echo '</div>';
                        
                    }
                    
                    //
                    if ($pages > 1) {
                            echo '<div class="lst">' . pages ('/forum/thema' . $t['id']) . '</div>';
                    }


                    echo '<div class="list1">';
                    
                    $who   = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_visit` WHERE (`id_t`='" . $t['id'] . "') AND (`time`>'" . (time () - 60) . "')");
                    $visit = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_visit` WHERE (`id_t`='" . $t['id'] . "')");
                    
                    
                    echo 'В теме: <a href="/forum/who' . $t['id'] . '">' . endi ($who, array ('человек', 'человека', 'человек')) . '</a>, <a href="/forum/visit' . $t['id'] . '">' . $visit . '</a> заходили<br/>';
                    echo '<img src="/images/download.png" alt="" align="middle"/> <a href="/dthema.php?id=' . $t['id'] . '&do">Скачать тему</a>';
                    echo ' | ';
                    echo '<img src="/images/files.png" alt="" align="middle"/> <a href="/forum/files' . $t['id'] . '">Файлы темы</a>';
                    echo '</div>';
                    
                    echo '<div class="menu2">';
                    echo '<img src="/design/alll.png" alt="" align="middle"/> Правила: <a href="/all/rulls.php?all" style="text-decoration:none; color:white;">сайта</a>/<a href="/forum/rulls' . $pr['id'] . '" style="text-decoration:none; color:white;">подраздела</a>';
                    echo ' | ';
                    echo '<img src="/design/emoc.png" alt="" align="middle"/> <a href="/all/smiles_r.php" style="text-decoration:none; color:white;">Смайлы</a>';
                    echo ' | ';
                    echo '<img src="/design/cod.png" alt="" align="middle"/> <a href="/all/bb.php" style="text-decoration:none; color:white;">ББ коды</a>';
                    echo ' | ';
                    echo '<img src="/design/usear.png" alt="" align="middle"/> <a href="/fsearch.php" style="text-decoration:none; color:white;">Поиск</a>';
                    echo '</div>';
        
                }

        }
    
        break;
    
        /*
         ---
         Кто в теме
         ---
         */
        case who:
        
        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`=?)" . ($us['level']>0?'':'AND (`type`!=\'2\')'), array ((int) $_GET['id']));
        if ($query -> rowCount ()==0) {
                header ('location: /forum.php');
                exit;
        }
        $t = $query -> fetch ();

        $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_visit` WHERE (`id_t`='" . $t['id'] . "') AND (`time`>'" . (time () - 60) . "')");
        H ('В теме ' . $items, 'В теме "' . $t['name'] . '" ' . $items . ' человек');
        
        $items_per_page = 10;
        $pages = ceil ( $items / $items_per_page );
        $page = (int) $_GET['page'];

        if ($page < 1) {
                $page = 1;
        }

        if ($page > $pages) {
                $page = $pages;
        }
            
        $start = $page * $items_per_page - $items_per_page;

        if ($items ==0) {
                echo '<div class="lst">В теме никого нет!</div>';
        }
        else {
        
                echo '<div class="list1">';
                $query = DB :: $dbs -> query ("SELECT * FROM `forum_visit` WHERE (`id_t`='" . $t['id'] . "') AND (`time`>'" . (time () - 60) . "') ORDER BY `time` DESC LIMIT $start, $items_per_page");
                while ($visit = $query -> fetch ()) {
                        echo uNick ($visit['us']).', ';
                }
                echo '</div>';
                echo '<div class="lst">' . pages ('/forum/who' . $t['id']) . '</div>';
        
        }
        
        echo '<div class="navg"><a href="/forum/thema' . $t['id'] . '">Вернуться</a></div>';
        
        break;
    
        /*
         ---
         Кто был в теме
         ---
         */
        case visit:
        if (!isset ($_GET['id'])) {
            header ('location: /forum.php');
            exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`=?)" . ($us['level']>0?'':'AND (`type`!=\'2\')'), array ((int) $_GET['id']));
        if ($query -> rowCount ()==0) {
            header ('location: /forum.php');
            exit;
        }
        $t = $query -> fetch ();

        $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_visit` WHERE (`id_t`='" . $t['id'] . "')");
        H ('В теме ' . $items, 'В тему "' . $t['name'] . '" зашло ' . $items . ' человек');

        $items_per_page = 10;
        $pages = ceil ( $items / $items_per_page );
        $page = (int) $_GET['page'];

        if ($page < 1)
            $page = 1;

        if ($page > $pages)
            $page = $pages;
            
        $start = $page * $items_per_page - $items_per_page;

        if ($items == 0) {
                echo '<div class="lst">В теме никого небыло!</div>';
        }
        else {
        
                echo ' <div class="list1">';
                $query = DB :: $dbs -> query ("SELECT * FROM `forum_visit` WHERE (`id_t`='" . $t['id'] . "') ORDER BY `time` DESC LIMIT $start, $items_per_page");
                while ($visit = $query -> fetch ()) {
                        echo uNick ($visit['us']) . ' (' . datef ($visit['time']) . ')<br/>';
                }
                echo '</div>';
                echo '<div class="lst">' . pages ('/forum/visit' . $t['id']) . '</div>';
                
        }
        
        echo '<div class="navg"><a href="/forum/thema' . $t['id'] . '">Вернуться</a></div>';
        
        break;

        
        case post:
        
        if (!isset ($_GET['post'])) {
                header ('location: /forum.php');
                exit;
        }
        
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id`=?)", array ((int) $_GET['post']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }
        
        $p = $query -> fetch ();
        
        $t = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_t` WHERE (`id`='" . $p['id_t'] . "')");
        if ($t['type']==2) {
                header ('location: /forum.php');
                exit;
        }

        $page = isset ($_GET['page']) ? (int) $_GET['page'] : 1;
        
        $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['us'] . "')");    
        if ($u['id']==$us['id'] && $us['level']>0 || $u['level']<$us['level']) {
           
            
            if ($p['del']==0) {
                    if ($p['ups']!=0) {
                            $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['ups'] . "')");
                            if ($u['id'] == $us['id'] || $u['level']<$us['level']) {
                                    DB :: $dbs -> query ("UPDATE `forum_p` SET `del` = '" . $us['id'] . "', `ups`='0' WHERE (`id`='" . $p['id'] . "')");
                            }
                    }
                    else {
                            DB :: $dbs -> query ("UPDATE `forum_p` SET `del`='" . $us['id'] . "' WHERE (`id`='" . $p['id'] . "')");
                    }
            }
            else {
                    $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['del'] . "')");
                    if ($u['id'] == $us['id'] || $u['level']<$us['level']) {
                            DB :: $dbs -> query ("UPDATE `forum_p` SET `del` = '0', `ups`='" . $us['id'] . "' WHERE (`id`='" . $p['id'] . "')");
                    }
            }
        
        }
        
        header ('location: /forum/thema' . $t['id'] . '/page' . $page);

        break;
        
        case redk:
        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount () == 0) {
            header ('location: /forum.php');
            exit;
        }
        
        $p = $query -> fetch ();
        
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`='" . $p['id_t'] . "')");
        if ($query -> rowCount () == 0) {
            header ('location: /forum.php');
            exit;
        }
        $t = $query -> fetch ();
        
        H ('Редактирование сообщения', $t['name']);
        
        if ($t['type']==2) {
                echo '<div class="lst">Данная тема удалена!</div>';
        }
        else {
                if ($t['type']==0) {
                        echo '<div class="lst">Ошибка!</div>';
                }
                else {
                    
                        $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['us'] . "')");
                        if ($p['us']==$us['id'] || $p['us']!=$us['id'] && $u['level']<$us['level']) {
                            
                                echo '<div class="list1">';
                                echo '<form action="/forum/redk' . $p['id'] . '?ok" method="post" enctype="multipart/form-data">';
                                echo 'Сообщение:<br/>';
                                echo '<textarea name="msg">' . $p['text'] . '</textarea><br/>';
                                
                                if ($us['file']==1) {
                                        echo 'Файл:<br/>';
                                        echo '<input type="file" name="filename"/><br/>';
                                }
                                
                                
                                echo '<input type="submit" value="Написать"/>';
                                echo '</form>';
                                echo '</div>';
                            
                                if (isset ($_GET['ok'])) {
                                    
                                        $_POST['msg'] = htmlspecialchars (addslashes (trim ($_POST['msg'])));
                                        if (empty ($_POST['msg'])) {
                                                echo '<div class="list1"><b>Вы не ввели сообщение..</b></div>';
                                        }
                                        else {
                                            
                                                if ($_POST['msg']!=$p['text']) {
                                                    DB :: $dbs -> query ("UPDATE `forum_p` SET `text`=? WHERE (`id`='" . $p['id'] . "')", array ($_POST['msg']));
                                                    DB :: $dbs -> query ("INSERT INTO `forum_reds` (`id_p`,`us`,`time`) VALUES ('" . $p['id'] . "', '" . $us['id'] . "', '" . time () . "')");
                                                }

                                                    
                                                $filename = NULL;
                                                    
                                                $ext = substr ($_FILES['filename']['name'], strrpos ($_FILES['filename']['name'], '.') + 1);  
                                                if (in_array ($ext, array ('jpg','gif','png','jpeg','bmp','zip','rar','7z','txt','mp3','avi','mp4','3gp'))) {
                                                            
                                                        $filename = $us['id'] . '_' . passgen () . '.' . $ext;
                                                        copy ($_FILES['filename']['tmp_name'], $_SERVER['DOCUMENT_ROOT'] . '/files/forum/' . $filename);
                                                            
                                                        DB :: $dbs -> query ("INSERT INTO `forum_f` (`id_t`,`id_p`, `name`, `size`) VALUES ('" . $t['id'] . "', '" . $p['id'] . "', '" . $filename . "', '" . $_FILES['filename']['size'] . "')");
                                                        
                                                }
                                        
                                                                            
                                                
                                                
                                                
                                                
                                                header ('location: /forum/thema' . $t['id']  . '/page' . ceil ( DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_t`='" . $t['id'] . "') " . ($us['level']>0 ? '' : 'AND (`del` = \'0\')')) / 10));
                                                
                                        }
                                    
                                }
                        
                        
                        }
                
                }
        }
        
        break;
        
        
        case vote:
        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');            
                exit;            
        }
        
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');            
                exit;            
        }
        $p = $query -> fetch ();
        
        if (DB :: $dbs -> querySingle ("SELECT * FROM `forum_vote` WHERE (`id_t`='" . $p['id_t'] . "') AND (`id_p`='" . $p['id'] . "') AND (`us`='" . $us['id'] . "')") != 0) {
                header ('location: /forum.php');
                exit;
        }
        
        $t = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_t` WHERE (`id`='" . $p['id_t'] . "')");
        if ($t['type']!=1) {
                header ('location: /forum.php');            
                exit;            
        }
        
        if ($p['del']!=0) {
                header ('location: /forum.php');
                exit;
        }
        
        if (!isset ($_GET['vot'])) {        
                header ('location: /forum.php');
                exit;
        }
        
        if (!isset ($_GET['v'])) {
                header ('location: /forum.php');
                exit;
        }

        $query = DB :: $dbs -> query ("SELECT * FROM `us` WHERE (`id`=?)", array ((int) $_GET['v']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }
        
        $v = $query -> fetch ();
        if ($v['id'] == $us['id']) {
                header ('location: /forum.php');
                exit;
        }
        
        $page = isset ($_GET['page']) ? (int) $_GET['page'] : 1;
        
            if ($_GET['vot']=='za') {
                DB :: $dbs -> query ("UPDATE `us` SET `reit`='" . ($v['reit'] + 0.01) . "' WHERE (`id`='" . $v['id'] . "')");
                DB :: $dbs -> query ("INSERT INTO `action` (`value`,`t`,`us`,`see`) VALUES ('us{" . $us['id'] . "} оценил [b]положительно[/b] ваш пост:[br]" . $p['text'] . " в теме [url=/forum/thema" . $t['id'] . "/page" . $page . "]" . $t['name'] . "[/url]!', '" . time () . "', '" . $v['id'] . "', '1')");
                DB :: $dbs -> query ("INSERT INTO `forum_vote` (`id_t`,`id_p`,`us`,`vot`, `kem`,`t`) VALUES ('" . $t['id'] . "','" . $p['id'] . "','" . $v['id'] . "','za', '" . $us['id'] . "', '" . time () . "')");
        }
        elseif ($_GET['vot']=='protiv') {
                DB :: $dbs -> query ("UPDATE `us` SET `reit`='" . ($v['reit'] - 0.01) . "' WHERE (`id`='" . $v['id'] . "')");
                DB :: $dbs -> query ("INSERT INTO `action` (`value`,`t`,`us`,`see`) VALUES ('us{" . $us['id'] . "} оценил [b]отрицательно[/b] ваш пост:[br]" . $p['text'] . " в теме [url=/forum/thema" . $t['id'] . "/page" . $page . "]" . $t['name'] . "[/url]!', '" . time () . "', '" . $v['id'] . "', '1')");
                DB :: $dbs -> query ("INSERT INTO `forum_vote` (`id_t`,`id_p`,`us`,`vot`, `kem`,`t`) VALUES ('" . $t['id'] . "','" . $p['id'] . "','" . $us['id'] . "','protiv', '" . $us['id'] . "','" . time () ."')");
        }
        
        header ('location: /forum/thema' . $t['id'] . '/page' . $page);
        
        break;

        case thems:
        if (!isset ($_GET['id'])) {
            header ('location: /forum.php');
            exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `us` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount () == 0) {
            header ('location: /forum.php');
            exit;
        }
        
        $u = $query -> fetch ();
        
        H ('Темы ' . $u['nick'], '<a href="/forum.php" style="color:white;">Форум</a> | Темы ' . uNick ($u['id']));
        
        $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`us`='" . $u['id'] . "') " . ($us['level']>0 ? '' : 'AND (`type`!=\'2\')'));
        $items_per_page = 10;
        $pages = ceil ( $items / $items_per_page );
        $page = (int) $_GET['page'];

        if ($page < 1)
            $page = 1;

        if ($page > $pages)
            $page = $pages;
            
        $start = $page * $items_per_page - $items_per_page;
        
        echo '<div class="menu2">Вcего ' . endi ($items, array ('тема', 'темы', 'тем')) . '</div>';
        
        if ($items == 0) {
            echo '<div class="lst">' . uNick ($u['id']) . ' не создавал тем на форуме!</div>';
        }
        else {
            
            $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`us`='" . $u['id'] . "') " . ($us['level']>0 ? '' : 'AND (`type`!=\'2\')') . " ORDER BY `id` DESC LIMIT $start, $items_per_page");
            while ($t = $query -> fetch ()) {
                
                $r = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_r` WHERE (`id`='" . $t['id_r'] . "')");
                $pr= DB :: $dbs -> queryFetch ("SELECT * FROM `forum_pr` WHERE (`id`='" . $t['id_pr'] . "')");
                
                $c_p = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_t`='" . $t['id'] . "') " . ($us['level']>0? '' : 'AND `del`=\'0\''));
                
                echo '<div class="lst">Раздел: <a href="/forum/' . $t['id_r'] . '">' . $r['name'] . '</a><br/>';
                echo 'Подраздел: <a href="/forum/' . $t['id_r'] . '/' . $t['id_pr'] . '">' . $pr['name'] . '</a><br/>';
                echo '<img src="/design/' . ($t['top'] == 1 ? 'prikrep' : ($t['type'] == 0 ? 'closed' : 'the')) . '.png" alt=""/> Тема: <a href="/forum/thema' . $t['id'] . '">' . $t['name'] . '</a> (' . $c_p . ') <a href="/forum/thema' . $t['id'] . '/page' . ceil ($c_p/10) . '">></a><br/>' . uNick ($t['us']) . '/' . uNick ($t['last']) . ' (' . datef ($t['time']) . ')<br/>';
                
                
                
                $c_f = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_f` WHERE (`id_t`= '" . $t['id'] . "')");
                if ($c_f != 0) {
                        echo '<img src="/images/page_white_get.png" alt="" align="middle"/> Файлов: <a href="/forum/files' . $t['id'] . '">' . $c_f . '</a><br/>';
                }

                echo '</div>';
        
            }
            
            echo '<div class="lst">' . pages ('/thems' . $u['id']) . '</div>';
        
        }
        
        break;

        case posts:

        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `us` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount () == 0) {
        
            header ('location: /forum.php');
            
            exit;
        
        }
        $u = $query -> fetch ();

        H ('Посты ' . $u['nick'], '<a href="/forum.php" style="color:white;">Форум</a> | Посты ' . uNick ($u['id']));

        $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` LEFT JOIN `forum_t` ON `forum_t`.`id`=`forum_p`.`id_t` WHERE (`forum_p`.`us`='" . $u['id'] . "') " . ($us['level']>0 ? '' : 'AND (`forum_t`.`type`!=\'2\') AND (`forum_p`.`del`=\'0\')'));
        $items_per_page = 10;
        $pages = ceil ( $items / $items_per_page );
        $page = (int) $_GET['page'];

        if ($page < 1)
            $page = 1;

        if ($page > $pages)
            $page = $pages;
            
        $start = $page * $items_per_page - $items_per_page;
        
        echo '<div class="menu2">Вcего ' . endi ($items, array ('пост', 'поста', 'постов')) . '</div>';
        
        if ($items == 0) {
                echo '<div class="lst">' . uNick ($u['id']) . ' не оставлял постов на форуме!</div>';
        }
        else {
        
            $query = DB :: $dbs -> query ("SELECT `forum_p`.* FROM `forum_p` LEFT JOIN `forum_t` ON `forum_t`.`id`=`forum_p`.`id_t`  WHERE (`forum_p`.`us`='" . $u['id'] . "') " . ($us['level']>0 ? '' : 'AND (`forum_t`.`type`!=\'2\') AND (`forum_p`.`del`=\'0\')') . " ORDER BY `forum_p`.`id` DESC LIMIT $start, $items_per_page");
            while ($p = $query -> fetch ()) {
            
                    $t = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_t` WHERE (`id`='" . $p['id_t'] . "')");
                
                    echo '<div class="list1">';
                    echo '<img src="/design/' . ($t['top']==1? 'prikrep' : ($t['type']==0 ? 'closed' : 'the')) . '.png" alt=""/> Тема: <a href="/forum/thema' . $t['id'] . '">' . $t['name'] . '</a><br/>';
                    echo uNick ($p['us']) . ' (' . datef ($p['time']) . ')';
                        
                    $za     = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_vote` WHERE (`id_p`='" . $p['id'] . "') AND (`vot`='za')");
                    $protiv = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_vote` WHERE (`id_p`='" . $p['id'] . "') AND (`vot`='protiv')");
                      
                    // display post votes
                    echo ' (<span style="color:green;">' . $za . '</span>/<span style="color:red;">' . $protiv . '</span>)<br/>';
                    
                    // if post have quote
                    if ($p['cit'] != 0) {
                        
                            echo 'Цитата:';
                            echo '<div class="cit">';
                            
                            $q = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id`='" . $p['cit'] . "')");                
                            if ($q -> rowCount () == 0) {
                                    echo '<b>Сообщение удалено!</b>';
                            }
                            else {
                                    
                                    // 
                                    $cit = $q -> fetch ();
                                    if ($cit['del'] !=0 ) {
                                            echo '<b>Сообщение удалено!</b>';
                                    }
                                    else {
                                        
                                            $citu = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $cit['us'] . "')");
                                            echo '<b style="color:red;">' . $citu['nick'] . '</b>: ' . bb(SM  ($cit['text']));
                                    
                                    }
                            
                            }
                            
                            echo '</div>';
                    
                    }
                    
                    
                    echo bb(SM  ($p['text']) ). '<br/>';

                    
                    // if post have attachment files
                    if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_f` WHERE (`id_p`='" . $p['id'] . "')") != 0) {
                    
                        echo '<b>Прикрепленные файлы:</b><br/>';
                        
                        $q = DB :: $dbs -> query ("SELECT * FROM `forum_f` WHERE (`id_p`='" . $p['id'] . "') ORDER BY `id`");
                        while ($f = $q -> fetch ()) {
                            echo '<a href="/files/forum/' . $f['name'] . '">' . $f['name'] . '</a> (' . sizef ($f['size']) . ')<br/>';
                        }
                    }

                    
                    $q = DB :: $dbs -> query ("SELECT * FROM `forum_reds` WHERE (`id_p`='" . $p['id'] . "')");
                    if ($q -> rowCount () != 0) {
                            $redk = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_reds` WHERE (`id_p`='" . $p['id'] . "') ORDER BY `id` DESC LIMIT 1");
                            echo '
                            ________<br/>
                            <small>Пост изменён <a href="/forums/reds.php?id=' . $p['id'] . '">' . $q -> rowCount () . ' раз.</a> Посл. ред. ' . uNick ($redk['us']) . ' (' . datef ($redk['time']) . ')</small>
                            ';
                    }
                    
                    echo '</div>';
                
            }
            
            echo '<div class="list1">' . pages ('/posts' . $u['id']) . '</div>';
        
        }
        
        
        break;

        case filez:
        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`=?)" . ($us['level']>0?'':'AND (`type`!=\'2\')'), array ((int) $_GET['id']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }
        $t = $query -> fetch ();
                
        $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_f` WHERE (`id_t`='" . $t['id'] . "')");
        H ('В теме ' . $items . ' файлов', 'В теме "' . $t['name'].'" ' . endi ($items, array ('файл', 'файла', 'файлов')));

        if ($t['type']==2) {
                echo '<div class="list1">Данная тема удалена!</div>';
        }
        else {

                $items_per_page = 10;
                $pages = ceil ( $items / $items_per_page );
                $page = (int) $_GET['page'];

                if ($page < 1)
                        $page = 1;

                if ($page > $pages)
                        $page = $pages;
                    
                $start = $page * $items_per_page - $items_per_page;

                if ($items==0) {
                        echo '<div class="list1">В этой теме файлов нет!</div>';
                }
                else {
                        echo '<div class="list1">';
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_f` WHERE (`id_t`='" . $t['id'] . "') ORDER BY `id` DESC LIMIT $start, $items_per_page");
                        while ($f = $query -> fetch ()) {
                                echo 'Файл: <a href="/files/forum/' . $f['name'] . '">' . $f['name'] . '</a> (' . sizef ($f['size']) . ')<br/>';
                        }
                        echo '</div>';
                        echo '<div class="lst">' . pages ('/forum/visit' . $t['id']) . '</div>';
                }
        
        }
        
        echo '<div class="navg"><a href="/forum/thema' . $t['id'] . '">Вернуться</a></div>';
        
        break;




        /*
         ---
         Добавление опроса
         ---
         */
        case op_add:
        if (!isset ($_GET['thema'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`=?)", array ((int) $_GET['thema']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }
        $t = $query -> fetch ();

        $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $t['us'] . "')");
        if ($u['id']!=$us['id'] || $u['id']!= $us['id'] && $u['level']>$us['level']) {
                header ('location: /forum.php');
                exit;
        }

        H ('Добавление голосования', $t['name']);
        if ($t['type']==2) {
                echo '<div class="list1">Данная тема удалена!</div>';
        }
        else {
                
                if ($t['type']==0) {
                        echo '
                        <div class="list1">
                                Тема закрыта!
                        </div>
                        ';
                }
                else {
                
                                                
                        if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_op` WHERE (`id_t`='" . $t['id'] . "')") != 0) {
                                header ('location: /forum/thema' . $t['id']);
                                exit;
                        }
                        
                        echo '
                        <div class="list1">
                                <form action="/forum.php?mod=op_add&thema=' . $t['id'] . '&add" method="post">
                                    Вопрос:<br/>
                                    <input name="vopros"/><br/>
                        ';
                        
                        for ($i = 1; $i < 9; $i++) {
                                echo '
                                    Вариант ответа ' . $i . ':<br/>
                                    <input name="otv[]"/><br/>
                                ';
                        }
                        echo 'Тип:<br/><select name="type">';
                        echo '<option value="1">открытое</option>';
                        echo '<option value="0">закрытое</option>';
                        echo '</select><br/>';
                        echo '<input type="submit" value="Добавить опрос"/>';
                        echo '</form>';
                        echo '</div>';
                        
                        if (isset ($_GET['add'])) {
                                $_POST['vopros'] = htmlspecialchars (addslashes (trim ($_POST['vopros'])));
                                if (empty ($_POST['vopros'])) {
                                        echo '
                                        <div class="list1">
                                                <b style="color:red;">Вы не ввели вопрос..</b>
                                        </div>
                                        ';
                                }
                                else {
                                        // 
                                        foreach ($_POST['otv'] as $otv) {
                                                $otv = htmlspecialchars (addslashes (trim ($otv)));
                                                if (!empty ($otv)) {
                                                        $otv_[] = $otv;
                                                }
                                        }
                                        
                                        if (count ($otv_)==0) {
                                                echo '
                                                <div class="list1">
                                                        <b style="color:red;">Вы не указали ни одного ответа..</b>
                                                </div>
                                                ';
                                        }
                                        else {
                                                
                                                if (count ($otv_)<2) {
                                                
                                                        echo '
                                                        <div class="list1">
                                                                <b style="color:red;">Ошибка!</b>
                                                        </div>
                                                        ';
                                                
                                                }
                                                else {
                                                        
                                                        $_POST['type'] == intval ($_POST['type']);
                                                        
                                                        DB :: $dbs -> query ("INSERT INTO `forum_op` (`id_t`,`vopros`,`type`) VALUES ('" . $t['id'] . "',?, ?)", array ($_POST['vopros'], $_POST['type']));
                                                        $id = DB :: $dbs -> lastInsertId ();
                                                        
                                                        foreach ($otv_ as $otv) {
                                                                DB :: $dbs -> query ("INSERT INTO `forum_op_otv` (`id_r`,`id_pr`,`id_t`,`id_op`,`text`) VALUES ('" . $t['id_r'] . "', '" . $t['id_pr'] . "', '" . $t['id'] . "','" . $id . "',?)", array ($otv));
                                                        }
                                                        
                                                        header ('location: /forum/thema' . $t['id']);
                                                        
                                                }
                                        }
                                }
                        }
                }
              
                echo '
                <div class="navg">
                        <a href="/forum/thema' . $t['id'] . '">Вернуться</a>
                </div>
                ';

        }

        break;

        /*
         ---
         Добавление пунктов
         ---
         */
        case 'vop_add':
        if (!isset ($_GET['thema'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`=?)", array ((int) $_GET['thema']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }
        $t = $query -> fetch ();

        $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $t['us'] . "')");
        if ($u['id']!=$us['id'] || $u['level']>$us['level']) {
                header ('location: /forum.php');
                exit;
        }
        
        H ('Добавление пунктов', $t['name']);
        
        if ($t['type']==2) {

                echo '
                <div class="list1">
                        Данная тема удалена!
                </div>
                ';

        }
        else {
                
                if ($t['type']==0) {
                
                        echo '
                        <div class="list1">
                                Тема закрыта!
                        </div>
                        ';
                
                }
                else {
                        
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_op` WHERE (`id_t`='" . $t['id'] . "') AND (`id`=?)", array ((int) $_GET['op']));
                        if ($query -> rowCount () == 0) {
                                header ('location: /forum/thema' . $t['id']);
                                exit;
                        }
                        $op = $query -> fetch ();
                        
                        echo '
                        <div class="list1">
                                <form action="/forum.php?mod=vop_add&thema=' . $t['id'] . '&op=' . $op['id'] . '&add" method="post">
                        ';
                        
                        for ($i = 1; $i < 9; $i++) {
                        
                                echo '
                                        Вариант ответа ' . $i . ':<br/>
                                        <input name="otv[]"/><br/>
                                ';
                        
                        }
                                
                        echo '
                                    <input type="submit" value="Добавить"/>
                                </form>
                        </div>
                        ';
                        
                        if (isset ($_GET['add'])) {
                        
                                foreach ($_POST['otv'] as $otv) {
                                        $otv = htmlspecialchars (addslashes (trim ($otv)));
                                        if (!empty ($otv)) {
                                                $otv_[] = $otv;
                                        }
                                }
                                        
                                if (count ($otv_)==0) {
                                        echo '
                                        <div class="list1">
                                                <b style="color:red;">Вы не указали ни одного ответа..</b>
                                        </div>
                                        ';
                                }
                                else {
                                        
                                        foreach ($otv_ as $otv) {
                                                DB :: $dbs -> query ("INSERT INTO `forum_op_otv` (`id_r`,`id_pr`,`id_t`,`id_op`,`text`) VALUES ('" . $t['id_r'] . "', '" . $t['id_pr'] . "', '" . $t['id'] . "','" . $op['id'] . "',?)", array ($otv));
                                        }
                                        
                                        header ('location: /forum/thema' . $t['id']);
                                        
                                }
                                
                        }

                        
                }
                
                echo '<div class="navg"><a href="/forum/thema' . $t['id'] . '">Вернуться</a></div>';

        }
        break;

        /*
         ---
         Удаление пунктов
         ---
         */
        case redop:
        if (!isset ($_GET['thema'])) {
                header ('location: /forum.php');
                exit;
        }
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`=?)", array ((int) $_GET['thema']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }
        $t = $query -> fetch ();
        
        $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $t['us'] . "')");
        if ($u['id']!=$us['id'] || $u['level']>$us['level']) {
                header ('location: /forum.php');
                exit;
        }
        
        H ('Удаление пунктов', $t['name']);
        if ($t['type']==2) {
                echo '
                <div class="list1">
                        Данная тема удалена!
                </div>
                ';
        }
        else {
                
                if ($t['type']==0) {
                
                        echo '
                        <div class="list1">
                                Тема закрыта!
                        </div>
                        ';
                
                }
                else {
                        
                        if (!isset ($_GET['op'])) {
                                header ('location: /forum.php');
                                exit;
                        }
                        
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_op` WHERE (`id_t`='" . $t['id'] . "') AND (`id`=?)", array ((int) $_GET['op']));
                        if ($query -> rowCount () == 0) {
                                header ('location: /forum/thema' . $t['id']);
                                exit;
                        }
                        $op = $query -> fetch ();
                        
                        echo '
                        <div class="list1">
                                <form action="/forum.php?mod=redop&thema=' . $t['id'] . '&op=' . $op['id'] . '&del" method="post">
                        ';
                        
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_op_otv` WHERE (`id_op`='" . $op['id'] . "')");
                        while ($otv = $query -> fetch ()) {
                                echo '
                                        <input type="checkbox" name="otv[]" value="' . $otv['id'] . '"/> ' . $otv['text'] . '<br/>
                                ';
                        }
                        
                        
                        echo '
                                        <input type="submit" value="Удалить"/>
                                </form>
                        </div>
                        ';
                        
                        if (isset ($_GET['del'])) {
                        
                                foreach ($_POST['otv'] as $otv) {
                                        $otv = (int) $otv;
                                        if ($otv!=0) {
                                                if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_op_otv` WHERE (`id_op`='" . $op['id'] . "') AND (`id`='" . $otv . "')")!=0) {
                                                        DB :: $dbs -> query ("DELETE FROM `forum_op_otv` WHERE (`id`='" . $otv . "')");
                                                        DB :: $dbs -> query ("DELETE FROM `forum_op_vote` WHERE (`otv`='" . $otv . "')");
                                                }
                                        }
                                }
                                
                                header ('location: /forum/thema' . $t['id']);
                            
                        }
                        
                }
                
        }

        echo '<div class="navg"><a href="/forum/thema' . $t['id'] . '">Вернуться</a></div>';

        break;

        case stat:
        
            if (isset ($_GET['new_thems'])) {
                
                H ('Новые темы', 'Новые темы');
                
                $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`date`>'" . ( time () - 86400 ) . "')");
                $items_per_page = 10;
                $pages = ceil ( $items / $items_per_page );
                $page = (int) $_GET['page'];

                if ($page < 1) {
                        $page = 1;
                }

                if ($page > $pages) {
                        $page = $pages;
                }

                $start = $page * $items_per_page - $items_per_page;

                if ($items==0) {
                        echo '<div class="list1">Новых тем нет..</div>';
                }
                else {
                            
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`date`>'" . (time () - 86400) . "') ORDER BY `id` DESC LIMIT $start, $items_per_page");
                        while ($t = $query -> fetch()) {
                                    
                                $r   = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_r`  WHERE (`id`='" . $t['id_r'] . "')");
                                $pr  = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_pr` WHERE (`id`='" . $t['id_pr']. "')");

                                $c_p = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_t`='" . $t['id'] . "') " . (isset ($us) ? ($us['level']>0 ? '' : 'AND (`del`=\'0\')') : 'AND (`del`=\'0\')'));

                                echo '<div class="list1">';
                                echo 'Раздел: <a href="/forum/razd' . $r['id'] . '">' . $r['name'] . '</a><br/>';
                                echo 'Подраздел: <a href="/forum/' . $r['id'] . '/' . $pr['id'] . '">' . $pr['name'] . '</a><br/>';
                                echo '<img src="/design/' . ($t['top']==1? 'prikrep' : ($t['type']==0 ? 'closed' : 'the')) . '.png" alt=""/> Тема: <a href="/forum/thema' . $t['id'] . '">' . $t['name'] . '</a> (' . $c_p.') <a href="/forum/thema' . $t['id'] . '/page' . ceil ( $c_p / 10 ) . '">>></a><br/>';
                                echo uNick ($t['us']) . '/' . uNick ($t['last']) . ' (' . datef ($t['time']) . ')';
                                echo '</div>';
                        }
                        echo '<div class="list1">' . pages ('/forum/new_thems') . '</div>';
                } 
            
        }
        elseif (isset ($_GET['obn_thems'])) {
            
                H ('Обновленные темы', 'Обновленные темы');
            
                $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`time`>'" . ( time () - 3600 ). "')");
                $items_per_page = 10;
                $pages = ceil ( $items / $items_per_page );
                $page = (int) $_GET['page'];

                if ($page < 1) {
                        $page = 1;
                }

                if ($page > $pages) {
                        $page = $pages;
                }

                $start = $page * $items_per_page - $items_per_page;

                if ($items==0) {
                        echo '<div class="list1">Обновленных тем нет..</div>';
                }
                else {
                            
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`time`>'" . (time () - 3600) . "') ORDER BY `id` DESC LIMIT $start, $items_per_page");
                        while ($t = $query -> fetch()) {
                                    
                                $r   = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_r`  WHERE (`id`='" . $t['id_r'] . "')");
                                $pr  = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_pr` WHERE (`id`='" . $t['id_pr']. "')");

                                $c_p = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_t`='" . $t['id'] . "') " . (isset ($us) ? ($us['level']>0 ? '' : 'AND (`del`=\'0\')') : 'AND (`del`=\'0\')'));

                                echo '<div class="list1">';
                                echo 'Раздел: <a href="/forum/razd' . $r['id'] . '">' . $r['name'] . '</a><br/>';
                                echo 'Подраздел: <a href="/forum/' . $r['id'] . '/' . $pr['id'] . '">' . $pr['name'] . '</a><br/>';
                                echo 'Тема: <img src="/design/' . ($t['top']==1? 'prikrep' : ($t['type']==0 ? 'closed' : 'the')) . '.png" alt=""/> <a href="/forum/thema' . $t['id'] . '">' . $t['name'] . '</a> (' . $c_p.') <a href="/forum/thema' . $t['id'] . '/page' . ceil ( $c_p / 10 ) . '">>></a><br/>';
                                echo uNick ($t['us']) . '/' . uNick ($t['last']) . ' (' . datef ($t['time']) . ')';
                                echo '</div>';
                        }
                        echo '<div class="list1">' . pages ('/forum/obn_thems') . '</div>';
                }
                            
        }
        else if (isset ($_GET['new_posts'])) {
            
                H ('Новые посты', 'Новые посты');
            
                $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`time`>'" . ( time () - 86400 ). "')");
                $items_per_page = 10;
                $pages = ceil ( $items / $items_per_page );
                $page = (int) $_GET['page'];

                if ($page < 1) {
                        $page = 1;
                }

                if ($page > $pages) {
                        $page = $pages;
                }

                $start = $page * $items_per_page - $items_per_page;

                if ($items==0) {
                        echo '<div class="list1">Новых постов нет..</div>';
                }
                else {
                            
                        $query = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`time`>'" . (time () - 86400) . "') ORDER BY `id` DESC LIMIT $start, $items_per_page");
                        while ($p = $query -> fetch()) {

                                $t  = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_t` WHERE (`id`='" . $p['id_t']. "')");
                                $c_p = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`id_t`='" . $t['id'] . "') " . (isset ($us) ? ($us['level']>0 ? '' : 'AND (`del`=\'0\')') : 'AND (`del`=\'0\')'));

                                echo '<div class="list1">';
                                echo 'Тема: <img src="/design/' . ($t['top']==1? 'prikrep' : ($t['type']==0 ? 'closed' : 'the')) . '.png" alt=""/> <a href="/forum/thema' . $t['id'] . '">' . $t['name'] . '</a> (' . $c_p.') <a href="/forum/thema' . $t['id'] . '/page' . ceil ( $c_p / 10 ) . '">>></a><br/>';
                                echo uNick ($p['us']) . ' (' . datef ($p['time']) . ')<br/>';

                                // if post have quote
                                if ($p['cit'] != 0) {
                                    
                                        echo '
                                        Цитата:
                                        <div class="cit">
                                        ';
                                        
                                        $q = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id`='" . $p['cit'] . "')");                
                                        if ($q -> rowCount () == 0) {
                                            
                                                echo '
                                                <b>Сообщение удалено!</b>
                                                ';
                                        
                                        }
                                        else {
                                                
                                                // 
                                                $cit = $q -> fetch ();
                                                if ($cit['del'] !=0 ) {
                                                
                                                        echo '
                                                        <b>Сообщение удалено!</b>
                                                        ';
                                                
                                                }
                                                else {
                                                    
                                                        $citu = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $cit['us'] . "')");
                                                        echo '
                                                        <b style="color:red;">' . $citu['nick'] . '</b>: ' . bb(SM  ($cit['text']));
                                                
                                                }
                                        
                                        }
                                        
                                        echo '
                                        </div>
                                        ';
                                
                                }
                                
                                
                                echo bb(SM ($p['text'])) . '<br/>
                                ';


                                
                                // if post have attachment files
                                if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_f` WHERE (`id_p`='" . $p['id'] . "')") != 0) {
                                
                                    echo '
                                    <b>Прикрепленные файлы:</b><br/>
                                    ';
                                    
                                    $q = DB :: $dbs -> query ("SELECT * FROM `forum_f` WHERE (`id_p`='" . $p['id'] . "') ORDER BY `id`");
                                    while ($f = $q -> fetch ()) {
                                        

                                        echo '<a href="/files/forum/' . $f['name'] . '">' . $f['name'] . '</a> (' . sizef ($f['size']) . ')<br/>
                                        ';
                                        
                                    }
                                }

                                
                                $q = DB :: $dbs -> query ("SELECT * FROM `forum_reds` WHERE (`id_p`='" . $p['id'] . "')");
                                if ($q -> rowCount () != 0) {
                                        $redk = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_reds` WHERE (`id_p`='" . $p['id'] . "') ORDER BY `id` DESC LIMIT 1");
                                        echo '
                                        ________<br/>
                                        <small>Пост изменён <a href="/forums/reds.php?id=' . $p['id'] . '">' . $q -> rowCount () . ' раз.</a> Посл. ред. ' . uNick ($redk['us']) . ' (' . datef ($redk['time']) . ')</small>
                                        ';
                                }
                                
                                echo '
                                </div>  
                                ';

                        }
                        echo '<div class="list1">' . pages ('/forum/new_posts') . '</div>';
                }
            
        }
        else {
                
        }
        
        break;


        /*
         ---
         Переименование темы
         ---
         */
        case thn:
        
        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }

        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount ()==0) {
                header ('location: /forum.php');
                exit;
        }

        $t = $query -> fetch ();
        $a = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $t['us'] . "')");
        if ($t['us'] != $us['id'] && $us['level']<1 || $a['level']>$us['level']) {
                header ('location: /forum/thema' . $t['id']);
                exit;
        }
        
        H ('Переименовать тему', '<a href="/forum/thema' . $t['id'] . '" style="color:white;">' . $t['name'] . '</a>');
            
        echo '
        <div class="list1">
                <form action="/forum.php?mod=thn&id=' . $t['id'] . '&ok" method="post">
                        Введите название темы:<br/>
                        <input name="name" value="' . $t['name'] . '"/><br/>
                        <input type="submit" value="Изменить"/>
                </form>
        </div>
        ';
        
        if (isset ($_GET['ok'])) {
                $_POST['name'] = htmlspecialchars (addslashes (trim ($_POST['name'])));
                if (empty ($_POST['name'])) {
                
                        echo '
                        <div class="list1">
                                <b style="color:red;">Вы не ввели название темы..</b>
                        </div>
                        ';
                
                }
                else {
                    
                        if ($t['name']!=$_POST['name']) {
                        
                                DB :: $dbs -> query ("UPDATE `forum_t` SET `name`=? WHERE (`id`='" . $t['id'] . "')", array ($_POST['name']));
                        
                        }
                        
                        header ('location: /forum/thema' . $t['id']);
                
                }
        
        }

        echo '<div class="navg"><a href="/forum/thema' . $t['id'] . '">Вернуться</a></div>';

        break;
    
        /*
         ---
         Перемещение темы
         ---
         */
        case thm:

        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }

        $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount ()==0) {
                header ('location: /forum.php');
                exit;
        }

        $t = $query -> fetch ();
        $a = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $t['us'] . "')");
        if ($t['us'] != $us['id'] && $us['level']<1 || $a['level']>$us['level']) {
                header ('location: /forum/thema' . $t['id']);
                exit;
        }
        
        H ('Переместить тему', '<a href="/forum/thema' . $t['id'] . '" style="color:white;">' . $t['name'] . '</a>');
            
        echo '
        <div class="list1">
            <form action="/forum.php?mod=thm&id=' . $t['id'] . '&ok" method="post">
                <select name="pr"/>
        ';
        
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_r` ORDER BY `id`");
        while ($r = $query -> fetch()) {
                echo '
                        <option value="">' . $r['name'] . '</option>
                ';
                if (DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_pr` WHERE (`id_r`='" . $r['id'] . "')") != 0) {
                        $q = DB :: $dbs -> query ("SELECT * FROM `forum_pr` WHERE (`id_r`='" . $r['id'] . "') ORDER BY `id`");
                        while ($pr = $q -> fetch ()) {
                                echo '
                                <option value="' . $pr['id'] . '">---' . $pr['name'] . '</option>
                                ';
                        }
                }
        }

        echo '        
                </select><br/>
                <input type="submit" value="Переместить"/>
            </form>
        </div>
        ';
        
        if (isset ($_GET['ok'])) {
                
                $query = DB :: $dbs -> query ("SELECT * FROM `forum_pr` WHERE (`id`=?)", array (intval ($_POST['pr'])));
                if ($query -> rowCount () == 0) {
                        header ('location: /forum.php?mod=thm&id=' . $t['id']);
                        exit;
                }
                
                $pr = $query -> fetch ();
                $r  = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_r` WHERE (`id`='" . $pr['id_r'] . "')");
                
                if ($t['id_r']!=$r['id'] || $t['id_pr']!=$pr['id']) {
                            
                        
                        DB :: $dbs -> query ("UPDATE `forum_t` SET `id_r`='" . $pr['id_r'] . "', `id_pr`='" . $pr['id'] . "', `name`='" . $t['name'] . " (Перемещена)' WHERE (`id`='" . $t['id'] . "')");

                        DB :: $dbs -> query ("UPDATE `forum_visit`   SET `id_r`='" . $r['id'] . "', `id_pr`='" . $pr['id'] .  "'  WHERE (`id_t`='" . $t['id'] . "')");
                        //
                        DB :: $dbs -> query ("UPDATE `forum_podp`    SET `id_r`='" . $r['id'] . "', `id_pr`='" . $pr['id'] .  "'  WHERE (`id_t`='" . $t['id'] . "')");
                        //
                        DB :: $dbs -> query ("UPDATE `forum_op`      SET `id_r`='" . $r['id'] . "', `id_pr`='" . $pr['id'] .  "'  WHERE (`id_t`='" . $t['id'] . "')");
                        //
                        DB :: $dbs -> query ("UPDATE `forum_op_otv`  SET `id_r`='" . $r['id'] . "', `id_pr`='" . $pr['id'] .  "'  WHERE (`id_t`='" . $t['id'] . "')");
                        //
                        DB :: $dbs -> query ("UPDATE `forum_op_vote` SET `id_r`='" . $r['id'] . "', `id_pr`='" . $pr['id'] .  "'  WHERE (`id_t`='" . $t['id'] . "')");
                        //
                        DB :: $dbs -> query ("UPDATE `forum_p`       SET `id_r`='" . $r['id'] . "', `id_pr`='" . $pr['id'] .  "'  WHERE (`id_t`='" . $t['id'] . "')");
                        //
                        DB :: $dbs -> query ("UPDATE `forum_reds`    SET `id_r`='" . $r['id'] . "', `id_pr`='" . $pr['id'] .  "'  WHERE (`id_t`='" . $t['id'] . "')");
                        //
                        DB :: $dbs -> query ("UPDATE `forum_vote`    SET `id_r`='" . $r['id'] . "', `id_pr`='" . $pr['id'] .  "'  WHERE (`id_t`='" . $t['id'] . "')");
                        //
                        DB :: $dbs -> query ("UPDATE `forum_f`       SET `id_r`='" . $r['id'] . "', `id_pr`='" . $pr['id'] .  "'  WHERE (`id_t`='" . $t['id'] . "')");
                        



                        DB :: $dbs -> query ("INSERT INTO `forum_p` (`id_r`, `id_pr`, `id_t`,`us`, `text`, `time`) VALUES ('" . $r['id'] . "', '" . $pr['id'] . "', '" . $t['id'] . "', '" . $us['id'] . "', '[b]Тема перемещена в " . $r['name'] . "/" . $pr['name'] . "![/b]', '" . time () . "')");
                        
                }
                    
                header ('location: /forum/thema' . $t['id']);
            
        }
        
        echo '<div class="navg"><a href="/forum/thema' . $t['id'] . '">Вернуться</a></div>';
        
        break;
        
        /*
         ---
         Удаление файла
         ---
         */
        case 'file':
        if (!isset ($_GET['id'])) {
                header ('location: /forum.php');
                exit;
        }
         
        $query = DB :: $dbs -> query ("SELECT * FROM `forum_f` WHERE (`id`=?)", array ((int) $_GET['id']));
        if ($query -> rowCount () == 0) {
                header ('location: /forum.php');
                exit;
        }
        $f = $query -> fetch ();
         
        $p = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_p` WHERE (`id`='" . $f['id_p'] . "')");
        $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $t['us'] . "')");
        
        $t = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_t` WHERE (`id`='" . $p['id_t'] . "')");
        
        if ($t['type']==2) {
                header ('location: /forum.php');
                exit;
        }
        
        if ($f['size']==0) {
                header ('location: /forum.php');
                exit;
        }
        
        $page = isset ($_GET['page']) ? (int) $_GET['page'] : 1;         
        
        if ($p['us']==$us['id'] && $us['level']>1 || $u['level']<$us['level']) {
                DB :: $dbs -> query ("UPDATE `forum_f` SET `size`='0' WHERE (`id`='" . $f['id'] . "')");
                if (file_exists ($_SERVER['DOCUMENT_ROOT'] . '/files/forum/' . $f['filename'])) {
                        unlink ('files/forum/' . $f['name']);
                }
        }
         
        header ('location: /forum/thema' . $t['id'] . '/page' . $page);
        break;

}

F ();

?>