<?php

#############################################################################
#           			                   Site@School
# Site@School is a website management system designed for primary schools.
#
# About Site@School:
# Comissioned by: City District 'de Baarsjes', Amsterdam, Netherlands
# Original idea, coding (version 1.0) and design : Mark de Haan (mark@starwave.nl)
# Ideas, improvements, expansions (version 1.1 - 2.3): Fred Stuurman 
# Current coder & maintainer: Fred Stuurman (fc.stuurman@hccnet.nl)
############################################################################
# send_alerts.inc.php: last changed 11/04/2005  2.3
#                       by Fred Stuurman (fc.stuurman@hccnet.nl)
#$Id: send_alerts.php,v 1.1 2006/10/03 08:33:55 fstuurman Exp $
############################################################################
#                        LICENSE INFORMATION
# Copyright (C) 2002, 2003, 2004 Municipality of Amsterdam, City District
#  'de Baarsjes', Amsterdam, the Netherlands.
#
# 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");
}

$send_alerts = $sas_config['send_alerts'];

$table_alerts = $dbprefix . "alerts";
$query = "SELECT * FROM $table_alerts "; // getting the alert info 
$result = mysql_query($query) or die_script($query, mysql_error());

if (strrpos($siteurl, '/') != strlen($siteurl) - 1)
{
	$siteurl .= '/'; //add last slash if not present
}

if (($send_alerts == "1") and (mysql_num_rows($result) > 0))
{
	$send_alerts_per_section = $sas_config['send_alerts_per_section'];

	if ($site == "protected")
	{
		$link = "<a href=" . $site_url . "index.php?section=$section&page=$savepage&site=protected>" . $site_url . "index.php?section=$section&page=$savepage&site=protected";
		$msg = $sas_lang['page'] . ": " . stripslashes($title) . " \n" . $link . "</a>";
		$section = "P" . $section;
	}
	else
	{
		$link = "<a href=" . $site_url . "index.php?section=$section&page=$savepage>" . $site_url . "index.php?section=$section&page=$savepage";
		$msg = $sas_lang['page'] . ": " . stripslashes($title) . " \n" . $link . "</a>";
	}

	require ("core/class.phpmailer.php"); //use the phpmailer class
	$mail = new PHPMailer();
	$mail->From = $sas_config['email']; //site email address
	$mail->FromName = $sitename;
	$mail->Subject = $sas_lang['alert_message'] . $sitename;
	$mail->Body = $msg;
	$mail->IsHTML(true);

	if ($send_alerts_per_section == "1") //we are going to send alerts per section
	{

		while ($alert_list = mysql_fetch_array($result))
		{

			$alert_sections = explode(",", $alert_list[2]);
			$i = 0;
			while ($i < count($alert_sections) + 1) //loop through the sections
			{
				if ($section == $alert_sections[$i]) //section matches the saved section
				{
					$email_list .= $alert_list[1] . ","; //add email to the list
					$mail->AddAddress($alert_list[1]); //add email to the list
				}
				next($alert_sections);
				$i++;
			}
		}
		if ($email_list != "")
		{
			$mail->Send();
		}
	}
	else //we have send the whole list a mail
		{
		while ($alert_list = mysql_fetch_array($result)) //create list with emails
		{
			$mail->AddAddress($alert_list[1]); //add email to the list
		}
		$mail->Send(); //send the mail

	}
}
?>