function mail_read()
{
    global $smarty;
    $mail_id = (int) $_REQUEST['mail_id'];
    $db_query = "\n\t\t\tSELECT \n\t\t\t\t`players`.`name` as 'from', \n\t\t\t\t`mail`.`mail_id`, \n\t\t\t\t`mail`.`from_player_id`, \n\t\t\t\t`mail`.`body`, \n\t\t\t\t`mail`.`subject`, \n\t\t\t\t`mail`.`time`, \n\t\t\t\t`mail`.`status` \n\t\t\tFROM `mail` \n\t\t\tLEFT JOIN `players` ON `players`.`player_id` = `mail`.`from_player_id` \n\t\t\tWHERE \n\t\t\t\t`mail`.`round_id` = '" . $_SESSION['round_id'] . "' AND \n\t\t\t\t`mail`.`mail_id` = '" . $mail_id . "' AND \n\t\t\t\t`mail`.`to_player_id` = '" . $_SESSION['player_id'] . "' AND \n\t\t\t\t`mail`.`status` != '" . MAILSTATUS_DELETED . "'\n\t\t\tORDER BY `time` ASC \n\t\t\tLIMIT 30";
    $db_result = mysql_query($db_query);
    if (mysql_num_rows($db_result) == 0) {
        $status[] = 'That mail does not exist or you do not have permission to view it.';
        $smarty->append('status', $status);
        mail_list();
        exit;
    }
    $mail = mysql_fetch_array($db_result, MYSQL_ASSOC);
    $mail['time'] = format_timestamp($mail['time'] + 3600 * $_SESSION['preferences']['timezone']);
    $mail['subject'] = htmlentities($mail['subject']);
    $mail['body'] = nl2br(htmlentities($mail['body']));
    if ($mail['from_player_id'] == 0) {
        $mail['from'] = 'Administration';
    }
    $bbtags = array('b' => array('Name' => 'b', 'HtmlBegin' => '<span style="font-weight: bold;">', 'HtmlEnd' => '</span>'), 'i' => array('Name' => 'i', 'HtmlBegin' => '<span style="font-style: italic;">', 'HtmlEnd' => '</span>'), 'u' => array('Name' => 'u', 'HtmlBegin' => '<span style="text-decoration: underline;">', 'HtmlEnd' => '</span>'), 's' => array('Name' => 's', 'HtmlBegin' => '<span style="text-decoration: line-through;">', 'HtmlEnd' => '</span>'), 'quote' => array('Name' => 'quote', 'HasParam' => true, 'HtmlBegin' => '<b>Quote %%P%%:</b><div class="mailquote">', 'HtmlEnd' => '</div>'));
    require_once dirname(__FILE__) . '/includes/bbcode.php';
    $bbcode = new bbcode();
    $bbcode->add_tag($bbtags['b']);
    $bbcode->add_tag($bbtags['i']);
    $bbcode->add_tag($bbtags['u']);
    $bbcode->add_tag($bbtags['s']);
    $bbcode->add_tag($bbtags['quote']);
    $mail['body'] = $bbcode->parse_bbcode($mail['body']);
    if ($mail['status'] == 1) {
        $db_query = "UPDATE `mail` SET `status` = '2' WHERE `mail_id` = '" . $mail_id . "' LIMIT 1";
        $db_result = mysql_query($db_query);
    }
    $smarty->assign('mail', $mail);
    $smarty->display('mail_read.tpl');
}
 function messages()
 {
     if (empty($_REQUEST['forum_topic_id'])) {
         $this->topics();
         exit;
     }
     $forum_topic_id = abs((int) request_variable('forum_topic_id', NULL, 0));
     $skip = abs((int) request_variable('skip', NULL, 0));
     $this->smarty->assign('skip', $skip);
     $this->sql->select(array(array('forum_topics', 'forum_topic_id'), array('forum_topics', 'replies'), array('forum_topics', 'subject')));
     $this->sql->where(array(array('forum_topics', 'round_id', $_SESSION['round_id']), array('forum_topics', 'kingdom_id', $_SESSION['kingdom_id']), array('forum_topics', 'forum_topic_id', $forum_topic_id)));
     $this->sql->limit(1);
     $db_result = $this->sql->execute();
     if (!$db_result || mysql_num_rows($db_result) == 0) {
         $this->smarty->append('status', 'Topic does not exist or you do not have access to it');
         $this->topics();
         exit;
     }
     $db_row = mysql_fetch_array($db_result, MYSQL_ASSOC);
     $this->smarty->assign('subject', htmlentities($db_row['subject']));
     $this->smarty->assign('forum_topic_id', $db_row['forum_topic_id']);
     $this->smarty->assign('count', ceil(($db_row['replies'] + 1) / 15));
     require_once dirname(__FILE__) . '/includes/bbcode.php';
     $bbcode = new bbcode();
     $bbtags = array('b' => array('Name' => 'b', 'HtmlBegin' => '<span style="font-weight: bold;">', 'HtmlEnd' => '</span>'), 'i' => array('Name' => 'i', 'HtmlBegin' => '<span style="font-style: italic;">', 'HtmlEnd' => '</span>'), 'u' => array('Name' => 'u', 'HtmlBegin' => '<span style="text-decoration: underline;">', 'HtmlEnd' => '</span>'), 's' => array('Name' => 's', 'HtmlBegin' => '<span style="text-decoration: line-through;">', 'HtmlEnd' => '</span>'), 'quote' => array('Name' => 'quote', 'HasParam' => true, 'HtmlBegin' => '<b>Quote %%P%%:</b><div class="mailquote">', 'HtmlEnd' => '</div>'), 'code' => array('Name' => 'code', 'HtmlBegin' => '<div class="bbcode_code">', 'HtmlEnd' => '</div>'));
     $bbcode->add_tag($bbtags['b']);
     $bbcode->add_tag($bbtags['i']);
     $bbcode->add_tag($bbtags['u']);
     $bbcode->add_tag($bbtags['s']);
     $bbcode->add_tag($bbtags['quote']);
     $bbcode->add_tag($bbtags['code']);
     $db_query = "SELECT COUNT(*) AS 'count' FROM `players` WHERE `kingdom_id` = '" . $_SESSION['kingdom_id'] . "' AND `rank` > '0'";
     $db_result = mysql_query($db_query);
     $player = mysql_fetch_array($db_result, MYSQL_ASSOC);
     $this->smarty->assign('player_count', $player['count']);
     $this->sql->select(array(array('players', 'player_id'), array('players', 'name', 'name_poster'), array('forum_messages', 'forum_message_id'), array('forum_messages', 'posttime'), array('forum_messages', 'message'), array('forum_messages', 'marked')));
     $this->sql->leftjoin(array('players', 'player_id', array('forum_messages', 'poster_id')));
     $this->sql->where(array(array('forum_messages', 'kingdom_id', $_SESSION['kingdom_id']), array('forum_messages', 'forum_topic_id', $forum_topic_id)));
     $this->sql->orderby(array('forum_messages', 'posttime', 'asc'));
     $this->sql->limit(array(15, $skip * 15));
     $db_result = $this->sql->execute();
     if (mysql_num_rows($db_result) == 0 && $skip > 0) {
         $_REQUEST['skip'] = 0;
         $this->messages();
     }
     while ($db_row = mysql_fetch_array($db_result, MYSQL_ASSOC)) {
         $db_row['posttime'] = format_timestamp($db_row['posttime']);
         $db_row['message'] = nl2br(htmlentities($db_row['message']));
         $db_row['message'] = $bbcode->parse_bbcode($db_row['message']);
         $db_row['marked'] = unserialize($db_row['marked']);
         if (!empty($db_row['marked'][$_SESSION['player_id']])) {
             $db_row['marked'] = count($db_row['marked']);
             if ($db_row['marked'] >= $player['count'] * (2 / 3)) {
                 $_REQUEST['forum_message_id'] = $db_row['forum_message_id'];
                 $this->mark_display = true;
                 $this->mark();
             }
         } else {
             $db_row['marked'] = '';
         }
         $messages[] = $db_row;
     }
     $this->smarty->assign('messages', $messages);
     $this->smarty->display('forum_messages.tpl');
     exit;
 }