<?php require("variables.inc"); ?>
<?php require("functions.inc"); ?>
<?php require("classes.inc");
require("classes/threadedlistitem.inc");

$parentmessagenum = $message;

// Load forum info
$currentforum = new Forum;
$currentforum->loadInfo($forum);

// Load thread info
$currentthread = new Thread;
$currentthread->loadInfo($forum,$threadid);

// Load message being replied to
$filename = $forumpath . "forums/" . $forum . "/$threadid.txt";
$fd = fopen($filename,"r");
$threadfile = fread($fd,filesize($filename));
fclose($fd);

$themessage = getStringPortion($threadfile,"<message$message>","</message$message>");
$author = getStringPortion($themessage,"<messageby>","</messageby>");
$postedon = getStringPortion($themessage,"<postedon>","</postedon>");
$parent = getStringPortion($themessage,"<inreplyto>","</inreplyto>");
$message_text = getStringPortion($themessage,"<message>","</message>");
if (getStringPortion($themessage,"<signature>","</signature>") == "yes")
   {
   $sig = true;
   }

$currentmessage = new Message($author,$postedon,$parent,$message_text,$sig);

// Load member info
$currentmember = new Member($author);

?>
<html>
<head>
 <title><?php echo $boardname; ?>: <?php echo $currentforum->getFullName(); ?>: <?php echo $currentthread->getSubject(); ?></title>
 <?php echo stylesheetLink(); ?>
</head>

<body bgcolor="#<?php echo $bgcolour; ?>" text="#<?php echo $textcolour; ?>" link="#<?php echo $linkcolour; ?>" vlink="#<?php echo $vlinkcolour; ?>" alink="#<?php echo $alinkcolour; ?>"<?php if (!$margins) { print " marginwidth=\"0\" marginheight=\"0\" leftmargin=\"0\" topmargin=\"0\""; } ?>>

<?php include("options/header.txt"); ?>

<div align="center">
<table border="0" width="98%" cellspacing="3">
 <tr valign="top">
  <td><a href="index.php3?serve=cached"><b><?php echo $boardname ?></b></a>: <a href="viewforum.php3?forum=<?php echo $forum; ?>"><b><?php echo $currentforum->getFullName(); ?></b></a>: <b><?php echo $currentthread->getSubject(); ?></b><br><small><?php

if (isset($cookieusername))
   {
   print "Logged in as $cookieusername | <a href=\"logout.php3\">Logout</a>\n";
   }
else
   {
   print "<a href=\"login.php3\">Login</a> | <a href=\"register.php3\">Register</a>\n";
   }

?></small></td>
  <td align="right"><a href="post.php3?postto=<?php echo $forum; ?>">Post new topic</a> | <a href="register.php3">Register</a> | <a href="faq.php3">FAQ</a><?php

if (isset($cookieoldlastvisit))
   {
   print "<br><small>You last visited: ";
   echo date("H:i, j/n/Y",$cookieoldlastvisit);
   }

?></td>
 </tr>
</table>
</div>

<p>&nbsp;</p>

<div align="center">
<table border="0" cellspacing="2" cellpadding="3" width="90%">
<?php

$now = time();
$threadfile = substr($message,strpos($message,"-") + 1);

if (isset($submit))
   {
   // Check member username and password
   if (checkMemberDetails($username,$password) == true)
      {
      if ($newmessage != "")
         {
         // Convert message into a suitable format
         $newmessage = convertMessage($newmessage);

         $author = new Member(getMemberNumber($username));

         if ($signature)
            {
            $sig = "yes";
            }
         else
            {
            $sig = "no";
            }

         /* Add message to thread file */
         $new_message_num = $currentthread->getNumMessages();
         $newmessage = "<message$new_message_num>\n <messageby>" . $author->getNumber() . "</messageby>\n <postedon>" . $now . "</postedon>\n <message>" . $newmessage . "</message>\n <inreplyto>" . $postedon . "</inreplyto>\n <signature>" . $sig . "</signature>\n</message$new_message_num>\n";

         // Add new message to end of thread file
         $filename = $forumpath . "forums/" . $forum . "/$threadid.txt";
         $fd = fopen($filename,"a");
         fwrite($fd,$newmessage);
         fclose($fd);

         /* Add one to member's total posts */
         if ($spammeasures)
            {
            if (strlen(smileyRemoval($newmessage)) > 15)
               {
               $author->incrementTotalPosts();
               }
            else
               {
               $antispammessage = "This forum employs anti-spam measures to discourage very short messages posted only to increase the users' total post count.  Consequently, due to the length of your message, your post count has not been incremented. ";
               }
            }
         else
            {
            $author->incrementTotalPosts();
            }

         /* Edit forum info file */
         $currentforum->setLastPostTime($now);
         $currentforum->setLastPoster($author->getNumber());
         $currentforum->saveInfo($forum);

         /* Update forum threadindex */
         $filename = $forumpath . "forums/" . $forum . "/threadindex.txt";
         $threadindex = new Vector;
         $threadindex = loadThreadIndex($forum);
         $threadindex->promoteElement($threadid);
         saveThreadIndex($forum,$threadindex);

         /* Edit thread info file */
         $currentthread->setLastPostTime($now);
         $currentthread->setLastPoster($author->getNumber());
         $currentthread->incrementNumMessages();
         $currentthread->addMessage($postedon,getMemberNumber($username),$now);
         $currentthread->saveInfo();

         /* Display message */
         print " <tr>\n  <td>Your message has been successfully posted.  $antispammessage ";
         if (isset($antispammessage))
            {
            print "<p>";
            }
         print "Click <a href=\"viewthread.php3?forum=";
         echo $currentforum->getShortName();
         print "&threadid=$threadid";
         if (($new_message_num + 1) > $maxmessagesonapage)
            {
            print "&page=";
            echo ceil(($new_message_num + 1) / $maxmessagesonapage);
            }
         print "#$now\">here</a> to return to the &quot;";
         echo $currentthread->getSubject();
         print "&quot; thread, or <a href=\"viewforum.php3?forum=";
         echo $currentforum->getShortName();
         print "\">here</a> to return to the ";
         echo $currentforum->getFullName();
         print " forum.</td>\n </tr>\n";
         }
      else
         {
         print " <tr>\n  <td>You forgot to enter a message!</td>\n </tr>\n";
         }
      }
   else
      {
      print " <tr>\n  <td>Invalid username/password entered, please try again.</td>\n </tr>\n";
      }
   }
else if (isset($preview))
   {
   // Check member username and password
   if (checkMemberDetails($username,$password) == true)
      {
      if ($newmessage != "")
         {
         $poster = new Member(getMemberNumber($username));

         // Display message info
         print " <tr>\n";
         print "  <td><h3><font color=\"#FF0000\">Preview:</font></h3></td>\n";
         print " </tr>\n";
         print " <tr>\n";
         print "  <td bgcolor=\"#$main_tdbgcolor\" width=\"19%\"><b><font color=\"#$secondary_textcolor\">";
         if ($displayrealnames)
            {
            echo str_replace(" ","&nbsp;",$poster->getFullName());
            }
         else
            {
            echo $poster->getUsername();
            }
         print "</font></b></td>\n";
         print "  <td bgcolor=\"#$main_tdbgcolor\"><small>Posted by <a href=\"profile.php3?username=";
         echo $poster->getUsername();
         print "\">";
         echo $poster->getUsername();
         print "</a> at ";
         echo date("H:i",time());
         print ", on ";
         echo date("d/n/Y",time());
         print "</small></td>\n";
         print " </tr>\n";
         print " <tr valign=\"top\">\n";
         print "  <td bgcolor=\"#$secondary_tdbgcolor\"><small>";
         if (isset($siteabbrev))
            {
            echo str_replace($siteabbrev,"<b><a href=\"$siteurl\">$siteabbrev</a></b>",str_replace(" ","&nbsp;",$poster->getStatus()));
            }
         print "</small></td>\n";
         print "  <td bgcolor=\"#$secondary_tdbgcolor\">";
         $temp = convertMessage($newmessage);
         $temp = str_replace("[quote]",$quoteson,$temp);
         $temp = str_replace("[/quote]",$quotesoff,$temp);
         echo $temp;

         if ($signature)
            {
            print "<br>________<br>";
            echo $poster->getSignature();
            }
         print "</td>\n";
         print " </tr>\n";

         // Display form
         print " <tr>\n";
         print "  <td>&nbsp;</td>\n";
         print "  <td><form method=\"post\" action=\"$PHP_SELF\">&nbsp;</td>\n";
         print " </tr>\n";
         print " <tr>\n";
         print "  <td align=\"right\">Username:</td>\n";
         print "  <td><input type=\"text\" name=\"username\" size=\"10\" value=\"$username\">\n";
         print " </tr>\n";
         print " <tr>\n";
         print "  <td align=\"right\">Password:</td>\n";
         print "  <td><input type=\"password\" name=\"password\" size=\"10\" value=\"$password\"> <small><a href=\"forgotpassword.php3\">Forgotten your password?</a></small></td>\n";
         print " </tr>\n";
         print " <tr>\n  <td>&nbsp;</td>\n </tr>\n";
         print " <tr valign=\"top\">\n";
         print "  <td align=\"right\">Message:</td>\n";
         print "  <td><textarea name=\"newmessage\" rows=\"12\" cols=\"50\" wrap=\"virtual\">" . stripslashes($newmessage) . "</textarea></td>\n";
         print " </tr>\n";
         print " <tr valign=\"top\">\n";
         print "  <td bgcolor=\"#$secondary_tdbgcolor\" align=\"right\">Posting options:</td>\n";
         print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"checkbox\" name=\"smileys\" checked> Smileys <small>(<a href=\"smileys.php3\">View all smileys</a>)</small><br><input type=\"checkbox\" name=\"parseurls\" checked> Convert URLs into clickable links<br><input type=\"checkbox\" name=\"signature\"";
         if ($signature)
            {
            print "checked";
            }
         print "> Signature</td>\n";
         print " </tr>\n";
         if (isAdministrator($cookieusername))
            {
            print " <tr valign=\"top\">\n";
            print "  <td bgcolor=\"#$secondary_tdbgcolor\" align=\"right\">Administrator options:</td>\n";
            print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"checkbox\" name=\"bypasshtmlcheck\"> Bypass HTML removal<br><input type=\"checkbox\" name=\"bypassswearfilter\"> Bypass swear word filter</td>\n";
            print " </tr>\n";
            }
         print " <tr>\n  <td>&nbsp;</td>\n </tr>\n";
         print " <tr>\n";
         print "  <td bgcolor=\"#$main_tdbgcolor\">";
         print "<input type=\"hidden\" name=\"forum\" value=\"";
         echo $forum;
         print "\">";
         print "<input type=\"hidden\" name=\"message\" value=\"";
         echo $message;
         print "\"><input type=\"hidden\" name=\"threadid\" value=\"";
         echo $threadid;
         print "\"></td>";
         print "  <td bgcolor=\"#$main_tdbgcolor\"><input type=\"submit\" name=\"submit\" value=\"Post reply\">&nbsp;&nbsp;&nbsp;<input type=\"submit\" name=\"preview\" value=\"Preview\"></td>\n";
         print " </tr>\n";
         }
      else
         {
         print " <tr>\n  <td>You forgot to enter a message!</td>\n </tr>\n";
         }
      }
   else
      {
      print " <tr>\n  <td>Invalid username/password entered, please try again.</td>\n </tr>\n";
      }
   }
else
   {
   // Display message info
   print " <tr>\n";
   print "  <td bgcolor=\"#$main_tdbgcolor\" width=\"19%\"><b><font color=\"#$secondary_textcolor\">";
   if ($displayrealnames)
      {
      echo str_replace(" ","&nbsp;",$currentmember->getFullName());
      }
   else
      {
      echo $currentmember->getUsername();
      }
   print "</font></b></td>\n";
   print "  <td bgcolor=\"#$main_tdbgcolor\"><small>Posted by <a href=\"profile.php3?username=";
   echo $currentmember->getUsername();
   print "\">";
   echo $currentmember->getUsername();
   print "</a> at ";
   echo $currentmessage->getMessagePostTime("H:i");
   print ", on ";
   echo $currentmessage->getMessagePostTime("d/n/Y");
   print "</small></td>\n";
   print " </tr>\n";
   print " <tr valign=\"top\">\n";
   print "  <td bgcolor=\"#$secondary_tdbgcolor\"><small>";
   if (isset($siteabbrev))
      {
      echo str_replace($siteabbrev,"<b><a href=\"$siteurl\">$siteabbrev</a></b>",$currentmember->getStatus());
      }
   print "</small></td>\n";
   print "  <td bgcolor=\"#$secondary_tdbgcolor\">";

   $year = date("Y",time());
   $messagetext = $currentmessage->getMessageText();
   $messagetext = str_replace("[Edited by","<small>[Edited by",$messagetext);
   $messagetext = str_replace("$year]","$year]</small>",$messagetext);
   $messagetext = str_replace("[quote]",$quoteson,$messagetext);
   $messagetext = str_replace("[/quote]",$quotesoff,$messagetext);
   echo $messagetext;

   print "</td>\n";
   print " </tr>\n";
   print " <tr>\n";
   print "  <td>&nbsp;</td>\n";
   print " </tr>\n";

   // Display form
   print " <tr>\n";
   print "  <td colspan=\"2\" bgcolor=\"$main_tdbgcolor\"><b><font color=\"#$secondary_textcolor\">Post reply:</font></b></td>\n";
   print "  <td><form method=\"post\" action=\"$PHP_SELF\">&nbsp;</td>\n";
   print " </tr>\n";
   print " <tr>\n";
   print "  <td align=\"right\">Username:</td>\n";
   print "  <td><input type=\"text\" name=\"username\" size=\"10\"";
   if (isset($cookieusername))
      {
      print " value=\"$cookieusername\"";
      }
   print ">\n";
   print " </tr>\n";
   print " <tr>\n";
   print "  <td align=\"right\">Password:</td>\n";
   print "  <td><input type=\"password\" name=\"password\" size=\"10\"";
   if (isset($cookieusername))
      {
      print " value=\"$cookiepassword\"";
      }
   print "> <small><a href=\"forgotpassword.php3\">Forgotten your password?</a></small></td>\n";
   print " </tr>\n";
   print " <tr>\n  <td>&nbsp;</td>\n </tr>\n";
   print " <tr valign=\"top\">\n";
   print "  <td align=\"right\">Message:</td>\n";
   print "  <td><textarea name=\"newmessage\" rows=\"12\" cols=\"50\" wrap=\"virtual\">";
   if ($quote)
      {
      print "[quote]";
      echo unconvertMessage($messagetext);
      print "[/quote]";
      }
   print "</textarea></td>\n";
   print " </tr>\n";
   print " <tr valign=\"top\">\n";
   print "  <td bgcolor=\"#$secondary_tdbgcolor\" align=\"right\">Posting options:</td>\n";
   print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"checkbox\" name=\"smileys\" checked> Smileys <small>(<a href=\"smileys.php3\">View all smileys</a>)</small><br><input type=\"checkbox\" name=\"parseurls\" checked> Convert URLs into clickable links<br><input type=\"checkbox\" name=\"signature\"> Signature</td>\n";
   print " </tr>\n";
   if (isAdministrator($cookieusername))
      {
      print " <tr valign=\"top\">\n";
      print "  <td bgcolor=\"#$secondary_tdbgcolor\" align=\"right\">Administrator options:</td>\n";
      print "  <td bgcolor=\"#$secondary_tdbgcolor\"><input type=\"checkbox\" name=\"bypasshtmlcheck\"> Bypass HTML removal<br><input type=\"checkbox\" name=\"bypassswearfilter\"> Bypass swear word filter</td>\n";
      print " </tr>\n";
      }
   print " <tr>\n  <td>&nbsp;</td>\n </tr>\n";
   print " <tr>\n";
   print "  <td bgcolor=\"#$main_tdbgcolor\">";
   print "<input type=\"hidden\" name=\"forum\" value=\"";
   echo $forum;
   print "\">";
   print "<input type=\"hidden\" name=\"message\" value=\"";
   echo $message;
   print "\"><input type=\"hidden\" name=\"threadid\" value=\"";
   echo $threadid;
   print "\"></td>";
   print "  <td bgcolor=\"#$main_tdbgcolor\"><input type=\"submit\" name=\"submit\" value=\"Post reply\">&nbsp;&nbsp;&nbsp;<input type=\"submit\" name=\"preview\" value=\"Preview\"></td>\n";
   print " </tr>\n";
   }

?>
</table>
</div>

<p align="center"><small>Powered by alpha board version <?php echo $versionnum; ?>.<br>
&copy; <a href="http://www.tfountain.co.uk/">Tim Fountain</a>, 2000 - <?php echo $year ?>.</small></p>

<?php include("options/footer.txt"); ?>

</body>
</html>