<?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)
############################################################################
# forumpostnewtopic.php: last changed 05/10/2006  version 2.4.10
#                        by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: forumpostnewtopic.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 ($_GET['forumid']))
{
	$forumid = intval($_GET['forumid']);
}
if (IsSet ($_POST['topictitle']))
{
	$topictitle = $_POST['topictitle'];
}
$topictitle = htmlspecialchars(strip_tags($topictitle)); //remove html tags
if (IsSet ($_POST['topicdetail']))
{
	$topicdetail = $_POST['topicdetail'];
}
$topicdetail = htmlspecialchars(strip_tags($topicdetail)); //remove html tags
if (IsSet ($_POST['send_email']))
{
	$send_email = $_POST['send_email'];
}

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

setlocale(LC_ALL, '$sas_lang[forum_locale]');

$error = true;

if ($topictitle == "")
{
	$msg = $sas_lang['forum_no_topic_title'];
}
elseif ($topicdetail == "")
{
	$msg = $sas_lang['forum_no_topic_text'];
}
elseif (strlen($topicdetail) > $MaxTopicBody)
{
	$msg = "The topic detail can't be more than " . $MaxTopicBody . " characters long.";
}
else
{
	require ("$cmsdir/core/class.phpmailer.php"); //use the phpmailer class
	$now = date("Y-m-d H:i:s");
	$topicdetail = nl2br($topicdetail);
	if (!get_magic_quotes_gpc()) //if magic quotes are off add slash
	{
		$topicdetail = addslashes($topicdetail);
	}
	$result = mysql_query("SELECT email,name FROM $forumcat_table WHERE cat_index = '$forumid'"); //get name and email for forum
	$row = mysql_fetch_array($result);
	if ($send_email == "on")
	{
		$semail = 1;
	}
	else
	{
		$semail = 0;
	}
	$ins_str = "INSERT INTO $forumtopics_table set topictitle='$topictitle', topicbody='$topicdetail', userid='$forum_username', date='$now', replies=0, topic_reads=0, cat_index='$forumid', send_email='$semail'";
	$topicdetail = stripslashes($topicdetail);
	//$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
	$message_to_sent = "<html><body>$sas_lang[forum_post_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]: $topicdetail <br><br>\n\n";
	$message_to_sent .= "<a href=$site_url/index.php?section=$section&page=$page&option=forum&forumid=$forumid>$sas_lang[forum_link] $sas_lang[forum_topic]</a></body></html>";
	if ($row[email] != '') // send mail to forum administrator
	{
		$mail = new PHPMailer();
		$mail->From = $sas_config['email']; //site email address
		$mail->FromName = $sitename;
		$mail->Subject = "$sas_lang[forum_post_topic_email] $row[name] - $sitename";
		$mail->Body = $message_to_sent;
		$mail->IsHTML(true);
		$mail->AddAddress($row[email]); //add email address ads owner.
		$mail->Send();
		//mail("$row[email]", "$sas_lang[forum_post_topic_email] $row[name] - $sitename", "$message_to_sent", "$headers")  ;
	}

	if (mysql_query($ins_str))
	{
		$msg = "$sas_lang[forum_topic_saved]:";
		$error = False;
		$ins_str = "SELECT topics FROM $forumuser_table WHERE username = '" . $forum_username . "'";
		$result = mysql_query($ins_str);
		$row = mysql_fetch_array($result);
		$newval = $row["topics"];
		$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='$forum_username'"); //get email FROM topic poster
		}

		if ($forumtype == 1)
		{
			$result2 = mysql_query("SELECT email FROM $table_users WHERE username='$forum_username'");
		}

		if ($forumtype == 2)
		{
			$result2 = mysql_query("SELECT email FROM $table_users WHERE username='$forum_username'");
		}

		if (mysql_num_rows($result2) > 0)
		{
			$user_email = mysql_result($result2, 0);
		}

		$newval++;
		$ins_str = "UPDATE $forumuser_table set lastcontribution='" . $now . "', topics=" . $newval . " WHERE username='" . $forum_username . "'";
		mysql_query($ins_str);

		if (($user_email != '') and ($send_email == "on")) //send topicposter email
		{
			$mail = new PHPMailer();
			$mail->From = $sas_config['email']; //site email address
			$mail->FromName = $sitename;
			$mail->Subject = "$sas_lang[forum_post_topic_email] $row[name] - $sitename";
			$mail->Body = $message_to_sent;
			$mail->IsHTML(true);
			$mail->AddAddress($user_email); //add email address ads owner.
			$mail->Send();
			//mail("$user_email", "$sas_lang[forum_post_topic_email] $row[name] - $sitename", "$message_to_sent", "$headers")  ;
		}
	}
	else
	{
		$msg = "A database error occured while trying to post your new topic.";
	}
}
print "<table border='0' cellpadding='2' cellspacing='0' width='100%'>";
print "<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'><b>$msg</b></font>";
}
else
{
	print "</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'><b>" . strftime("%A %d %B %Y %H:%M:%S", $ts) . "</b></font></td>
	              </tr>
	              <tr>
	              <td width='50%' valign='top' align='right' bgcolor='$EvenBackgroundColor'>
	               <font face='Arial' size='2'>$sas_lang[forum_topic]</font></td>
	               <td width='50%' bgcolor='$EvenBackgroundColor'><font face='Arial' size='2'><b>$topicdetail</b></font></td>
	               </tr>
	               <tr>
	               <td width='50%' valign='top' align='right' bgcolor='$OddBackgroundColor'>
	               <font face='Arial' size='2'>$sas_lang[forum_keep_posted]</font></td>";
	if ($send_email == "on")
	{
		$semail = "$sas_lang[forum_yes]";
	}
	else
	{
		$semail = "$sas_lang[forum_no]";
	}
	print "<td width='50%' bgcolor='$OddBackgroundColor'><font face='Arial' size='2'><b>$semail</b></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>
	               </tr></table>";
}
?>
