<?php

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

if (!isset ($us)) {
        header ('location: /');
        exit;
}

H ('Поиск по форуму', 'Поиск по форуму');

    if (isset ($_GET['thems'])) {
    
        $_SESSION['thema'] = htmlspecialchars (addslashes (trim ($_SESSION['thema'])));
    
        echo '<div class="list1">';
        echo '<form action="/fsearch.php?thems&get" method="post">';
        echo '<input name="name" value="' . $_SESSION['thema'] . '"/><br/>';
        echo '<input type="submit" value="Поиск"/>';
        echo '</form>';
        echo '</div>';

        if (isset ($_GET['get'])) {
                $_POST['name'] = htmlspecialchars (addslashes (trim ($_POST['name'])));
                if (!empty ($_POST['name'])) {
                        $_SESSION['thema'] = $_POST['name'];
                }
                
                if (isset ($_SESSION['thema'])) {
                
                        $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_t` WHERE (`name` LIKE '%" . $_SESSION['thema'] . "%') 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) {
                                echo '<div class="list1">По вашему запросу ничего не найдено!</div>';
                        }
                        else {

                                $query = DB :: $dbs -> query ("SELECT * FROM `forum_t` WHERE (`name` LIKE '%" . $_SESSION['thema'] . "%') AND (`type`!='2') ORDER BY `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>';
                                        
                                }
                                
                                if ($pages>1) {
                                        echo '<div class="list1">' . pages ('/fsearch/thems') . '</div>';
                                }
                
                        }
                        
                }
        
        }


}
elseif (isset ($_GET['posts'])) {

        $_SESSION['post'] = htmlspecialchars (addslashes (trim ($_SESSION['post'])));
        
        echo '<div class="list1">';
        echo '<form action="/fsearch.php?posts&get" method="post">';
        echo '<input name="text"/><br/>';
        echo '<input type="submit" value="Поиск"/>';
        echo '</form>';
        echo '</div>';
        
        if (isset ($_GET['get'])) {
                $_POST['text'] = htmlspecialchars (addslashes (trim ($_POST['text'])));
                if (!empty ($_POST['text'])) {
                        $_SESSION['post'] = $_POST['text'];
                }
                
                if (isset ($_SESSION['post'])) {
                
                        $items = DB :: $dbs -> querySingle ("SELECT COUNT(*) FROM `forum_p` WHERE (`text` LIKE '%" . $_SESSION['post'] . "%') 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 ($items==0) {
                                echo '<div class="list1">По вашему запросу ничего не найдено!</div>';
                        }
                        else {

                                $query = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`text` LIKE '%" . $_SESSION['post'] . "%') AND (`del`='0') 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'] . "')");
                                    
                                        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 ($cit['text']);
                                                        
                                                        }
                                                
                                                }
                                                
                                                echo '</div>';
                                        
                                        }
                                        
                                        $p['text'] = bb ($p['text']);
                                        
                                        echo $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>';




                                        
                                }
                                
                                if ($pages>1) {
                                        echo '<div class="list1">' . pages ('/fsearch/posts') . '</div>';
                                }
                
                        }
                        
                }
        
        }
        
}
else {
        echo '<div class="list1"><a href="/fsearch.php?thems">По названию тем</a></div>';
        echo '<div class="list1"><a href="/fsearch.php?posts">По постам</a></div>';
}


F ();

?>