<?php
include_once "config.php";
include_once "common.inc.php";
include_once "auth.inc.php";
require_once "jbbcode/Parser.php";
 


$user = loggedin($_COOKIE['ticket'], $_SERVER['REMOTE_ADDR']);
global $user;

if ($config['login_required'] && !isset($user))
  {
    header("Location: login.php");
    die;
  }

function post_format($text)
  {
    $text = str_replace("<", "&lt;", $text);
    $text = str_replace(">", "&gt;", $text);
    $parser = new JBBCode\Parser();
    $parser->addCodeDefinitionSet(new JBBCode\DefaultCodeDefinitionSet());
    $parser->parse($text);
    $text = $parser->getAsHtml();
    $text = newline_to_br($text);
    return $text;
  }

function print_post($connection, $post)
  {
    global $user;
    $q = $connection->query("select * from posts where follow_up='$post'");
    while ($r = $q->fetch_array())
      {
        print "<div class=\"topic-text\">\n";
        print "<div class=\"topic-post-title\">";
        printf("%s @ %s from %s", $r['username'], $r['timestamp'], $r['ip']);
        if (isset($user))
          { 
            print "<span class=\"right\">";
            if ($user == $r['username']) { print "[<a href=\"edit.php?id=".$r['id']."\">Edit</a>] "; }
            print "[<a href=\"reply.php?id=".$r['id']."\">Reply</a>";
            print "</span>";
          }
        print "</div>\n";
        print post_format($r['text']);
        print_post($connection, $r['id']);
        print "</div>\n";
      }
  }

function print_topic($connection, $topic)
  {
    global $user;
    $q = $connection->query("select * from posts where topic='$topic' and follow_up is null");
    while ($r = $q->fetch_array())
      {
        print "<div class=\"topic-text\">\n";
        print "<div class=\"topic-post-title\">";
        printf("%s @ %s from %s", $r['username'], $r['timestamp'], $r['ip']);
        if (isset($user))
          { 
            print "<span class=\"right\">";
            if ($user == $r['username']) { print "[<a href=\"edit.php?id=".$r['id']."\">Edit</a>] "; }
            print "[<a href=\"reply.php?id=".$r['id']."\">Reply</a>";
            print "</span>";
          }
        print "</div>\n";
        print post_format($r['text']);
        print_post($connection, $r['id']);
        print "</div>\n";
      }
  }

$c = mysqli_connect($config['host'], $config['user'], $config['pass'], $config['database'])
     or die("Connect to database failed");
$q = $c->query("select * from topics order by posted desc")
     or die("Failed to get topics list");

page_open($config['board_title']);
print "<div class=\"right\">";
if (isset($user)) {
  print " [ <a href=\"logout.php\">Logout</a> ]";
  print " [ <a href=\"newtopic.php\">New topic</a> ]";
  print " : <a href=\"mod-user.php\">$user</a>";
} else {
  print "<a href=\"login.php\">Login</a>";
}
print "</div>\n";
print "<div id=\"padding\">";
while ($r = $q->fetch_array())
  {
    print "<div class=\"topic\">\n";
    print "<div class=\"topic-title\">".$r['title']." (".$r['posted'].")";
    if (isset($user))
      { 
        print "<span class=\"right\">";
        print "[<a href=\"reply.php?topic=".$r['id']."\">Reply</a>]";
        print "</span>";
      }
    print "</div>\n";
    print_topic($c, $r['id']);
    print "</div>\n";
  }
print "</div>";

page_close();
?>

