<?php
/*
 *      PHP Frontend for pocsag monitor
 *
 *      Copyright (C) 2004-2005
 *          Manuel Weiser (manuelw@fire-devils.org)
 *
 *      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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
// Done in Haupttabelle updaten
$result_check_ins = mysqli_query($dbconn, "UPDATE dme SET zeit='$row[zeit]', done='1' WHERE num='$row[num]'") or die (mysqli_error($dbconn));


if( trim($row["funktion"]) != 'Klartext' && trim($row["funktion"]) != 'Info' && $amenable == 1 )
{
	$result_alarm_mail = mysqli_query($dbconn, "SELECT a.userid, a.time_from, a.time_to, a.stiwo, b.id, b.a_mail, b.a_alarmmail, b.a_alarmmail2sms, b.amail_count, b.ft_allow
									FROM alarm2mail a
									LEFT JOIN admin_users b ON b.id = a.userid
									WHERE adresse=".$row["adresse_neu"]) or die(mysqli_error($dbconn));
	while( $row_alarm_mail = mysqli_fetch_array($result_alarm_mail) )
	{
		// Alarmmail Counter
		$amail_count = $row_alarm_mail["amail_count"]+1;
		
		// schauen ob f�r Alarm extra Mailadresse
		if( $row_alarm_mail["a_alarmmail"] ) $row_alarm_mail["a_mail"] = $row_alarm_mail["a_alarmmail"];
		
		// schauen ob Alarm im Zeitfenster liegt
		if( $row_alarm_mail["time_from"] > $row_alarm_mail["time_to"] ) $row_alarm_mail["time_to"] = 24+$row_alarm_mail["time_to"];
		
		// Stichwort checken
		if( empty($row_alarm_mail["stiwo"]) ) { $stiwo_check = 'hit'; }
		else { $stiwo_check = check_stiwo($row_alarm_mail["stiwo"], $row["text"]); }
		
		// Meldung im EMailformat senden
		if( $stiwo_check == 'hit' && $row_alarm_mail["a_alarmmail2sms"] == '0' && ( $row_alarm_mail["time_from"] == $row_alarm_mail["time_to"] || ($row_alarm_mail["time_from"] <= get_hour( get_time($row["zeit"]) ) && $row_alarm_mail["time_to"] > get_hour( get_time($row["zeit"]) ) ) ) )
		{
				// Mail normal senden
				if ( $row_alarm_mail["ft_allow"] == 1 )
				{
					// Wenn FullText erlaubt
					mail_alarm(get_date_long($row["zeit"]), get_time($row["zeit"]), $row_alarm_mail["a_mail"],$row["org"],$row["org_name"],$row["bezeichnung"],$row["text"]);
				}
				else
				{
					// Wenn FullText erlaubt
					mail_alarm(get_date_long($row["zeit"]), get_time($row["zeit"]), $row_alarm_mail["a_mail"],$row["org"],$row["org_name"],$row["bezeichnung"],"Einsatzalarm");
				}
				
				// Mailcounter
				$result_counter = mysqli_query($dbconn, "UPDATE admin_users SET amail_count='$amail_count' WHERE id='$row_alarm_mail[id]'");
		}
		
		// Meldung im SMSformat senden
		if( $stiwo_check == 'hit' && $row_alarm_mail["a_alarmmail2sms"] == '1' && ( $row_alarm_mail["time_from"] == $row_alarm_mail["time_to"] || ($row_alarm_mail["time_from"] <= get_hour( get_time($row["zeit"]) ) && $row_alarm_mail["time_to"] > get_hour( get_time($row["zeit"]) ) ) ) )
		{
				// Mail im SMS Format senden
				if ( $row_alarm_mail["ft_allow"] == 1 )
				{
					// Wenn FullText erlaubt
					mail_alarm_sms(get_date_long($row["zeit"]), get_time($row["zeit"]), $row_alarm_mail["a_mail"],$row["org"],$row["org_name"],utf8_decode($row["text"]));
				}
				else
				{
					// Wenn kein FullText erlaubt
					mail_alarm_sms(get_date_long($row["zeit"]), get_time($row["zeit"]), $row_alarm_mail["a_mail"],$row["org"],$row["org_name"],"Einsatzalarm");
				}
				
				// Mailcounter
				$result_counter = mysqli_query($dbconn, "UPDATE admin_users SET amail_count='$amail_count' WHERE id='$row_alarm_mail[id]'");
		}
		
	}
}


// Einsatzcounter updaten
if( ($row["zeit"] - $row["last_alarm"]) > 1000 )
{
	$alarmcount = $row["alarm_count"] + 1;
	$result_check_ins = mysqli_query($dbconn, "UPDATE organisation SET alarm_count='$alarmcount', last_alarm='$row[zeit]' WHERE id='$row[org_id]'") or die (mysqli_error($dbconn));
}
?>