<?php

#############################################################################
#                              Site@School
# Site@School is a website management system designed for schools.
#
# About Site@School:
# Original Idea: Mark de Haan (mark@starwave.nl)
# PHP coding & Design: Mark de Haan (mark@starwave.nl)
# Comissioned by: Stadsdeel de Baarsjes, Amsterdam, Netherlands
############################################################################
# mailpage.php: last changed 31/04/2006  , version: 2.4                                       
#               by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: mailpage.php,v 1.1 2006/10/03 09:17:55 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
#
# 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['sendemail']))
{
	$sendemail = $_GET['sendemail'];
}
if (IsSet ($_GET['contact_id']))
{
	$contact_id = $_GET['contact_id'];
}
if (IsSet ($_POST['contact_id']))
{
	$contact_id = $_POST['contact_id'];
}
if (IsSet ($_POST['fullname']))
{
	$fullname = $_POST['fullname'];
}
else
{
	$fullname = "";
}
if (IsSet ($_POST['message']))
{
	$message = $_POST['message'];
}
else
{
	$message = "";
}

if (IsSet ($_POST['email2']))
{
	$email = explode(",", $_POST['email2']); //if more than one email is entered
	$email = "$email[0]"; //ony use the first one.
}
else
{
	$email = "";
}

$table_email_adresses = $dbprefix . "m_mail_address";

load_language($cmsdir . '/', 'sn_mailpage'); //load language file

if (IsSet ($_GET['contact_id']))
{
	$query = "SELECT name, description, email, welcometext, thankyoutext, id FROM $table_email_adresses WHERE id = '$contact_id'";
}
else
{
	$query = "SELECT name, description, email, welcometext, thankyoutext, id FROM $table_email_adresses ORDER BY sort LIMIT 1";
}
$result = mysql_query($query) or die_script($query, mysql_error());
$RecordCount = mysql_num_rows($result);
if ($RecordCount > 0)
{ //if table is empty use old email
	$contact_name = mysql_result($result, 0, 0); //address from config table.
	$contact_description = mysql_result($result, 0, 1);
	$contact_email = mysql_result($result, 0, 2);
	$contact_welcome_text = mysql_result($result, 0, 3);
	$contact_thankyou_text = mysql_result($result, 0, 4);
	$contact_id = mysql_result($result, 0, 5);
}
else
{
	$contact_name = "";
	$contact_description = "";
	$contact_email = $sas_config['email'];
	$contact_welcome_text = $sas_config['email_text'];
	$contact_thankyou_text = $sas_config['email_thankyou_text'];
}
unset ($query);
unset ($result);

function check_email($str)
{
	//returns 1 if valid email, 0 if not
	if (ereg("^.+@.+\\..+$", $str))
		return 1;
	else
		return 0;
}

if (IsSet ($sendemail))
{

	if ((IsSet ($_POST['fullname'])) and ($fullname == ""))
	{
		print $sas_lang['email_module_noname_error'];
		$error = 1;
	}
	elseif ((trim($email) == "") or (check_email($email) == 0))
	{
		print $sas_lang['email_module_noemail_error'];
		$error = 1;
	}
	elseif ((IsSet ($_POST['message'])) and ($message == ""))
	{
		print $sas_lang['email_module_nomessage_error'];
		$error = 1;
	}

	if ($error == 1)
	{
		print "<FORM action=\"index.php?section=$section&page=$page\" method=\"post\">
				   <br>
				   <INPUT type=\"hidden\" name=\"fullname\" value=\"" . $_POST['fullname'] . "\">
				   <INPUT type=\"hidden\" name=\"message\" value=\"" . $_POST['message'] . "\">
				   <INPUT type=\"hidden\" name=\"email2\" value=\"" . $_POST['email2'] . "\">
				   <input type=\"submit\" value=\"$sas_lang[goback]\">
				   </FORM>";
	}
	else
	{
		$message = nl2br($message);
		$message = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
						\"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd\">
						<html>
						<head>
						<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" . $sas_lang['lang_charset'] . "\">
						<title></title>
						</head><body>" . $message . "</body></html>";

		require ("$cmsdir/core/class.phpmailer.php"); //use the phpmailer class
		$mail = new PHPMailer();
		$mail->From = $email; //email address of submitter
		$mail->FromName = $sitename;
		$mail->Subject = $sas_lang['email_module_email_from'] . " " . $fullname . " - $email ($ip)";
		$mail->Body = $message;
		$mail->IsHTML(true);
		$mail->AddAddress($contact_email); //add email address owner form.
		$mail->Send();

		print "<table width=100% border=\"0\"><tr>
				<td width='100%'><b>" . $sas_lang['email_module_sent_us'] . "</b></td>
				</tr> </table><br>
				<table width='100%' border=\"0\">
				<tr>
				<td width='8%'><b>" . $sas_lang['email_module_to'] . "</b></td>
				<td width=1%>:</td><td>$contact_name</td>
				</tr><tr>
				<td><b>" . $sas_lang['email_module_name'] . "</b></td>
				<td>:</td><td>$fullname</td>
				</tr><tr>
				<td><b>" . $sas_lang['email_module_email'] . "</b></td>
				<td>:</td><td>$email</td>
				</tr><tr>
				<td valign=top><b>" . $sas_lang['email_module_text'] . "</b></td>
				<td valign=top>:</td><td>$message</td>
				</tr></table><br><br>";

		if ($contact_thankyou_text == "")
		{
			print $sas_config['email_thankyou_text'];
		}
		else
		{
			print $contact_thankyou_text;
		}
		print "<p><form><input type=\"button\" value=\"$sas_lang[goback]\" name=\"backbutton\"
						onClick=\"parent.location='index.php?section=$section&page=$page'\" class=\"backbutton\"></form>";
	}

}
else
{
	$query = "SELECT id, name, description FROM $table_email_adresses ORDER BY sort";
	$result = mysql_query($query) or die_script($query, mysql_error());

	print "<script type=\"text/javascript\">
	<!--
		function ViewCrossReference (selSelectObject){
			if (selSelectObject.options[selSelectObject.selectedIndex].value != '')
			{
			location.href=selSelectObject.options[selSelectObject.selectedIndex].value
			}
		}
	//-->
	</script>";

	print "<form method=\"POST\" action=\"index.php?section=$section&page=$page&contact_id=$contact_id&sendemail=yes\">";
	print "<TABLE cellSpacing=0 cellPadding=0 width=\"100%\" border=0>
	  <TBODY>
	  <tr><td>";
	if ($contact_welcome_text == "")
	{
		print $sas_config['email_text'];
	}
	else
	{
		print $contact_welcome_text;
	}
	print "</td></tr>
	  <tr><td>&nbsp;</td></tr>
	  <TR>
	    <TD width=500>
	      <TABLE cellSpacing=0 cellPadding=0 width=\"500\" border=0>
	        <TBODY>";

	if ($RecordCount > 0)
	{
		print "  <tr>
				      <td colspan=\"3\">" . $sas_lang['email_module_select_contact'] . ": <select name=\"contact_id\" onChange=\"ViewCrossReference(this);\">";
		while ($address_list = mysql_fetch_array($result))
		{
			if ($contact_name == $address_list[1])
			{
				print "<option value=\"index.php?section=$section&page=$page&contact_id=$address_list[0]\" selected>$address_list[1]</option>";
			}
			else
			{
				print "<option value=\"index.php?section=$section&page=$page&contact_id=$address_list[0]\">$address_list[1]</option>";
			}
		}
		print "</select>
		    </tr>
		    <tr><td colspan=\"3\">&nbsp;</td>
		    <tr><td colspan=\"3\">$contact_description</td></tr>";
	}
	print "<tr><td colspan=\"3\">&nbsp;</td>
	    
	    </tr>
	        <TR>
	          <TD vAlign=top width=120 align=\"right\"><font size=\"2\"><b>" . $sas_lang['email_module_name'] . " (*)&nbsp;</b></font></TD>
	          <TD class=text vAlign=top><INPUT size=50 name=fullname value=\"$fullname\"><br>&nbsp;</TD>
	          <td rowspan=\"2\" valign=\"top\">";

	if (file_exists($sas_config['email_picture']))
	{
		print "<img border=\"0\" src=\"" . $sas_config['email_picture'] . "\">";
	}

	print "&nbsp;</td>
	          </TR>
	        <TR>
	          <TD vAlign=top width=120 align=\"right\" height=\"50\"><font size=\"2\"><b>" . $sas_lang['email_module_email'] . " (*)&nbsp;</b></font></TD>
	          <TD class=text vAlign=top colspan=\"2\"><INPUT size=50 name=email2 value=\"$email\"><br>&nbsp;</TD>
	          </TR>
	        <TR>
	          <TD vAlign=top width=120 align=\"right\"><font size=\"2\"><b>" . $sas_lang['email_module_text'] . " (*)&nbsp;</b></font></TD>
	          <TD class=text vAlign=top colspan=\"2\"><TEXTAREA name=message rows=9 cols=40>$message</TEXTAREA></TD></TR>  
	        <tr><td colspan=\"3\">&nbsp;</td>
	        <TR>
	          <TD>&nbsp;</TD>
	          <TD colspan=\"2\">" . $sas_lang['required_fields'] . "</TD>
	          </TR>
	        <TR>
	          <TD vAlign=top width=120>&nbsp</TD>
	          <TD class=text vAlign=top colspan=\"2\">
	             <P><br><input type=\"hidden\" name=\"contact_id\" value=\"$contact_id\">
	             <INPUT type=submit value=" . $sas_lang['email_module_send'] . " name=B1></P></TD>
	          </TR>
	</TBODY></TABLE><BR></TD>
	    </TR></TBODY></TABLE>";
}
?>
