<?php

#############################################################################
#                              WIZZ FORUM
#  Wizz Forum is a KISS Forum: Keep It Small and Simple.
#
# About the Wizz Forum:
# Original Idea & PHP coding: Mike Kroger (mikek@icon.co.za),
# who granted permission to put Wizz forum uder the GNU GPL.  
# Code adaptations and modularization for Site@School:
# Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# forumpostreply.php: last changed 05/10/2006  version 2.4.10
#                     by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: forumpostreply.php,v 1.2 2006/10/15 19:08:53 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2006, Rosa Boekdrukker Primary School, Holland.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
##############################################################################
if (!defined('IN_SAS'))
{
	die("Hacking attempt");
}

if (IsSet ($_POST['replydetail']))
{
	$replydetail = sql_addslashes($_POST['replydetail']);
}
if (IsSet ($_POST['topictitle']))
{
	$topictitle = sql_addslashes($_POST['topictitle']);
}
if (IsSet ($_POST['topicid']))
{
	$topicid = intval($_POST['topicid']);
}
if (IsSet ($_GET['forumid']))
{
	$forumid = intval($_GET['forumid']);
}

if (IsSet ($_SESSION['forum_username']))
{
	$forum_username = $_SESSION['forum_username'];
}
else
{
	$forum_username = "";
}

$error = true;

if ($replydetail == "")
{
	$msg = $sas_lang['forum_no_topic_detail'];
}
else
{
	require ("$cmsdir/core/class.phpmailer.php"); //use the phpmailer class
	$replydetail = nl2br($replydetail);
	$now = date("Y-m-d H:i:s");
	if (!get_magic_quotes_gpc()) //if magic quotes are off add slash
	{
		$replydetail = addslashes($replydetail);
	}
	$ins_str = "Insert into $forumreplies_table set topicindex=$topicid, replybody='$replydetail', userid='$forum_username', date='$now', cat_index='$forumid'";
	if (mysql_query($ins_str))
	{
		$msg = "$sas_lang[forum_reply_saved]";
		$error = False;
		$ins_str = "Select replies from $forumuser_table where username = '$forum_username'";
		$result = mysql_query($ins_str);
		$row = mysql_fetch_array($result);
		$newval = $row["replies"];
		$newval++;
		$ins_str = "Update $forumuser_table set lastcontribution='$now', replies=$newval where userid='$forum_username'";
		mysql_query($ins_str);
		$ins_str = "Select replies, send_email, userid from $forumtopics_table where topic_index=$topicid";
		$result = mysql_query($ins_str);
		$row = mysql_fetch_array($result);
		$send_email = $row["send_email"];
		$topic_userid = $row["userid"];
		$newval = $row["replies"];
		$newval++;
		$ins_str = "Update $forumtopics_table set replies=$newval where topic_index=$topicid";
		mysql_query($ins_str);
		$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
		$result = mysql_query("Select name, email from $forumcat_table where cat_index = '$forumid'"); //get name and email of forum
		$row = mysql_fetch_array($result);
		$replydetail = stripslashes($replydetail);
		if ($row[email] != '')
		{
			$message_to_sent = "<html><body>" . $sas_lang['forum_post_reply_email'] . " $row[name] <br><br>\n\n";
			$message_to_sent .= "$sas_lang[forum_topic_title] $topictitle <br><br>\n\n";
			$message_to_sent .= "$sas_lang[forum_text]: $replydetail <br><br>\n\n";
			$message_to_sent .= "<a href=$site_url/index.php?section=$section&page=$page&option=topicdetails&forumid=$forumid&topicid=$topicid>$sas_lang[forum_link] $sas_lang[forum_reply]</a></body></html>";
			$message_to_sent .= "</body></html>";
			$mail = new PHPMailer();
			$mail->From = $sas_config['email']; //site email address
			$mail->FromName = $sitename;
			$mail->Subject = "$sas_lang[forum_post_reply_email] $row[name] - $sitename";
			$mail->Body = $message_to_sent;
			$mail->IsHTML(true);
			$mail->AddAddress($row[email]); //add email address ads owner.
			$mail->Send();
		}
		if ($send_email == "1")
		{
			$message_to_sent = "<html><body>" . $sas_lang['forum_post_reply_email'] . " $row[name] <br><br>\n\n";
			$message_to_sent .= "$sas_lang[forum_topic_title] $topictitle <br><br>\n\n";
			$message_to_sent .= "$sas_lang[forum_text]: $replydetail <br><br>\n\n";
			$result2 = mysql_query("SELECT  type FROM $forumcat_table where cat_index = '$forumid'");
			if (mysql_num_rows($result2) > 0) //what is the forum type
			{
				$forumtype = mysql_result($result2, 0);
			}
			if ($forumtype > 2)
			{
				$result2 = mysql_query("SELECT  email FROM $forumuser_table WHERE username='$topic_userid'"); //get email FROM topic poster

				$message_to_sent .= "<a href=$site_url/index.php?section=$section&page=$page&option=topicdetails&forumid=$forumid&topicid=$topicid>$sas_lang[forum_link] $sas_lang[forum_reply]</a>";
			}
			if ($forumtype == 1)
			{
				$result2 = mysql_query("SELECT  email FROM $table_users WHERE username='$topic_userid'");
				$message_to_sent .= "<a href=$site_url/index.php?section=$section&page=$page&option=forum&forumid=$forumid>$sas_lang[forum_link] $sas_lang[forum_reply]</a>";
			}
			if ($forumtype == 2)
			{
				$result2 = mysql_query("SELECT  email FROM $table_users WHERE username='$topic_userid'");
				$message_to_sent .= "<a href=$site_url/index.php?section=$section&page=$page&option=forum&forumid=$forumid>$sas_lang[forum_link] $sas_lang[forum_reply]</a>";
			}
			$user_email = mysql_result($result2, 0);
			if ($user_email != '')
			{
				$message_to_sent .= "</body></html>";
				$mail = new PHPMailer();
				$mail->From = $sas_config['email']; //site email address
				$mail->FromName = $sitename;
				$mail->Subject = "$sas_lang[forum_post_reply_email] $row[name] - $sitename";
				$mail->Body = $message_to_sent;
				$mail->IsHTML(true);
				$mail->AddAddress($useremail); //add email address.
				$mail->Send();
			}
		}

	}
	else
	{
		$msg = "A database error occured while trying to post your reply.";
	}
}
print "<table border='0' cellpadding='2' cellspacing='0' width='100%'>
           <tr>";
if ($error)
{
	print "<td width='100%'>
	               <font face='Arial' size='4'>$sas_lang[forum_error]</font></td>
	               </tr>
	               </table>
	               <p><font face='Arial' size='2'>" . $msg . "</font>
	               <font face='Arial' size='2'><p>
	               <A HREF=JavaScript:history.back()>$sas_lang[forum_back]</a></font>";

}
else
{
	print "<td width='100%'>&nbsp</td>
	              </tr>
	              </table>
	              <p><font face='Arial' size='2'><b>$msg</b></font></p>
	              <center>
	              <p><table border='1' cellpadding='0' cellspacing='0' width='80%' bordercolor='$sas_lang[forum_bordercolor]'>
	              <tr>
	              <td width='100%'>
	              <table border='0' cellpadding='3' cellspacing='0' width='100%'>
	              <tr>
	              <td width='50%' align='right' bgcolor='$OddBackgroundColor'>
	              <font face='Arial' size='2'>$sas_lang[forum_topic_title]</font></td>
	              <td width='50%' bgcolor='$OddBackgroundColor'>
	              <font face='Arial' size='2'><b>$topictitle</b></font></td>
	              </tr>
	              <tr>
	              <td width='50%' align='right' bgcolor='$EvenBackgroundColor'>
	              <font face='Arial' size='2'>$sas_lang[forum_author]</font></td>
	              <td width='50%' bgcolor='$EvenBackgroundColor'>
	              <font face='Arial' size='2'><b>$forum_username</b></font></td>
	              </tr>
	              <tr>
	              <td width='50%' align='right' bgcolor='" . $OddBackgroundColor . "'>
	              <font face='Arial' size='2'>$sas_lang[forum_date]</font></td>";
	$ts = mktime(substr($now, 11, 2), substr($now, 14, 2), substr($now, 17, 2), substr($now, 5, 2), substr($now, 8, 2), substr($now, 0, 4));
	print "<td width='50%' bgcolor='" . $OddBackgroundColor . "'>
	              <font face='Arial' size='2'>" . date("D d F Y H:i:s", $ts) . "</font></td>
	              </tr>
	              <tr>
	              <td width='50%' align='right' bgcolor='$EvenBackgroundColor'>
	              <font face='Arial' size='2'>$sas_lang[forum_reply]:</font></td>
	              <td width='50%' bgcolor='$EvenBackgroundColor'>
	              <font face='Arial' size='2'>$replydetail</font></td>
	              </tr>
	              </table>
	              </td></tr>
	              </table></p>
	              </center>
	              <p><table cellspacing=\"0\" cellpadding=\"0\"><tr height=\"25\">
	              <td background=\"$cmsdir/modules/sn_forum/left.gif\" width=\"10\">&nbsp;</td>
	              <td background=\"$cmsdir/modules/sn_forum/middle.gif\" align=\"center\">
	              <a href='index.php?section=$section&page=$page&option=forum&forumid=$forumid'><b>$sas_lang[forum_main_page]</b></a></td>
	              <td background=\"$cmsdir/modules/sn_forum/right.gif\" width=\"10\">&nbsp;</td>
	              <td width=\"10\">&nbsp;</td>
	              <td background=\"$cmsdir/modules/sn_forum/left.gif\" width=\"10\">&nbsp;</td>
	              <td background=\"$cmsdir/modules/sn_forum/middle.gif\" align=\"center\">
	              <a href='index.php?section=$section&page=$page&option=topicdetails&forumid=$forumid&topicid=$topicid'><b>$sas_lang[forum_to_topic]</b></a></td>
	              <td background=\"$cmsdir/modules/sn_forum/right.gif\" width=\"10\">&nbsp;</td>
	              </tr></table>";
}
?>