<?php

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

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

if ($t['type']==2) {
        header ('location: /');
        exit;
}

$pr= DB :: $dbs -> queryFetch ("SELECT * FROM `forum_pr` WHERE (`id`='" . $t['id_pr']. "')");
$r = DB :: $dbs -> queryFetch ("SELECT * FROM `forum_r`  WHERE (`id`='" . $t['id_r'] . "')");

if (isset ($_GET['do'])) {

        header ('content-type: text/plain');
        header ('content-disposition: attachment; filename="' . $t['id'] . '.txt";');

        echo "Тема: " . $t['name'] . ", создана " . date ("d.m.Y в H:i", $t['date']) . "\r\n\r\n";
        
        if ($t['type']==0) {
                echo "Тема закрыта!\r\n";
        }
        
        $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\')'));
        if ($items == 0) {
                echo "Постов нет..";
        }
        else {
                
                // 
                $i = 1;
                
                $query = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id_t`='" . $t['id'] . "') " . ($us['level']>0 ? '':'AND (`del`=\'0\')') . " ORDER BY `id`");
                while ($p = $query -> fetch ()) {
                        $u = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['us'] . "')");

                        echo $i++ . ". " . $u['nick'] . " " . ( $p['us'] == $t['us'] ? "[автор]" : "" ) . " (" . date ("d.m.Y в H:i:s", $p['time']) . ")";
                        
                        // if post is delete
                        if ($p['del'] != 0) {
                                $who = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['del'] . "')");
                                echo " пост удалил " . $who['nick'];
                        }
                        
                        // if post recovered
                        if ($p['ups'] != 0) {
                                $who = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $p['ups'] . "')");
                                echo " пост восстановил " . $who['nick'];
                        }
                        
                        $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 " ($za/$protiv)\r\n";
                    
                        // if post have quote
                        if ($p['cit'] != 0) {
                                
                                echo "Цитата:\r\n";
                                    
                                $q = DB :: $dbs -> query ("SELECT * FROM `forum_p` WHERE (`id`='" . $p['cit'] . "')");                
                                if ($q -> rowCount () == 0) {
                                        
                                        echo "Сообщение удалено!";
                                    
                                }
                                else {
                                            
                                        // 
                                        $cit = $q -> fetch ();
                                        if ($cit['del'] !=0 ) {
                                            
                                                echo "Сообщение удалено!";
                                            
                                        }
                                        else {
                                                    
                                                $citu = DB :: $dbs -> queryFetch ("SELECT * FROM `us` WHERE (`id`='" . $cit['us'] . "')");
                                                echo $citu['nick'] . ": " . $cit['text'];
                                            
                                        }
                                    
                                }
                                    
                                echo "\r\n";
                            
                        }
                    
                        // display post text
                        echo $p['text'] . "\r\n";

                
                }
                
        }
        
        echo "// wap-mas.ru 

}
else {

        H ($t['name'], '<a href="/forum.php" style="color:white;">Форум</a> | <a href="/forum/' . $r['id'] . '" style="color:white;">' . $r['name'] . '</a> | <a href="/forum/' . $pr['id'] . '" style="color:white;">' . $pr['name'] . '</a> | ' . $t['name']);

        echo '<div class="lst"><a href="/dthema.php?id=' . $t['id'] . '&do">Скачать .txt</a></div>';

        F ();

}

?>