<?php
/*
= check calendar for email reminders to be send =

© Copyright 2009-2012  LuxSoft - www.LuxSoft.eu

---------------------------------------------------------------------
| This script depends on and is executed via the file lcalcron.php. |
| See lcalcron.php header for details.                              |
---------------------------------------------------------------------

This file is part of the LuxCal Web Calendar.

The LuxCal Web Calendar 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 3 of the License, or (at your option) any later version.

The LuxCal Web Calendar 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 the LuxCal 
Web Calendar. If not, see <http://www.gnu.org/licenses/>.

 ------------- THIS SCRIPT USES THE FOLLOWING CALENDAR SETTINGS --------------
| $set['calendarEmail'] : Email source ("from") for notification messages     |
| $set['calendarTitle'] : The calendar title is part of the reminder email    |
| $set['calendarUrl']   : A calendar hyperlink is added to the reminder email |
| $set['eventColor']    : Specifies event colors (0:user color, 1:cat color)  |
 -----------------------------------------------------------------------------
*/

$emlStyle = "background:#FFFFDD; color:#000099; font:12px arial, sans-serif;"; //email body style definition

//
//Send email notification
//
function notify(&$evt,$date,$daysDue) {
global $emlStyle, $count, $set, $mailSent, $xx, $ax, $todayD00;

	//compose email message
	$dateTime = IDtoDD($date)." @ ".(($evt['sti'] or $evt['eti']) ? ITtoDT($evt['sti']) : $ax['not_all_day']);
	if ($evt['eti']) { $dateTime .= " - ".ITtoDT($evt['eti']); }
	$status = '';
	if ($evt['ch1']) { $status .= $evt['la1'].': '.(strpos($evt['chk'], $date.'a') ? $evt['mk1'] : '- -').'&nbsp;&nbsp;'; }
	if ($evt['ch2']) { $status .= $evt['la2'].': '.(strpos($evt['chk'], $date.'b') ? $evt['mk2'] : '- -'); }
	$subject = $set['calendarTitle']." - ".$ax['not_due_in']." ".$daysDue." ".$ax['not_days'].": ".$evt['tit'];
	if ($set['eventColor']) {
		$eColor = ($evt['cco'] ? 'color:'.$evt['cco'].';' : '').($evt['cbg'] ? 'background-color:'.$evt['cbg'].';' : '');
	} else {
		$eColor = $evt['uco'] ? 'background-color:'.$evt['uco'].';' : '';
	}
	$eStyle = $eColor ? ' style="'.$eColor.'"' : '';
	if ($set['showAdEd']) {
		$adEdData = '<tr><td>'.$xx['vws_added'].':</td><td>'.IDtoDD($evt['ada']).' ('.$evt['una'].')'."</td></tr>\n";
		if ($evt['mda'] and $evt['edr']) { $adEdData .= '<tr><td>'.$xx['vws_edited'].':</td><td>'.IDtoDD($evt['mda']).' ('.$evt['edr'].')'."</td></tr>\n"; }
	} else {
		$adEdData = '';
	}
	$message = "
<html>
<head>\n<title>".$set['calendarTitle']." ".$ax['not_mailer']."</title>
<style type='text/css'>
body, p, table {".$emlStyle."}
td {vertical-align:top;}
</style>
</head>
<body>
<p>".$set['calendarTitle']." ".$ax['not_mailer']." ".IDtoDD($todayD00)."</p>
<p>".$ax['not_event_due_in']." ".$daysDue." ".$ax['not_days'].":</p>
<table>
	<tr><td>".$ax['not_title'].":</td><td><b><span".$eStyle.">".$evt['tit']."</span></b></td></tr>
	".(($evt['ch1'] or $evt['ch2']) ? '<tr><td>'.$ax['not_status'].':</td><td>'.$status.'</td></tr>' : '')."
	<tr><td>".$ax['not_category'].":</td><td>".$evt['cnm']."</td></tr>
	<tr><td>".$ax['not_date_time'].":</td><td>".$dateTime."</td></tr>
	<tr><td>".$ax['not_venue'].":</td><td>".(($evt['ven']) ? $evt['ven'] : "- -")."</td></tr>
	<tr><td>".$ax['not_description'].":</td><td>".(($evt['des']) ? $evt['des'] : "- -")."</td></tr>
".$adEdData."
</table>
<p><a href=\"".$set['calendarUrl']."\">".$ax['not_open_calendar']."</a></p>
</body>
</html>
";
	$headers = 'MIME-Version: 1.0'."\n".'Content-type: text/html; charset=utf-8'."\n".'From: '.$set['calendarEmail'];
	//send emails
	if ($evt['rml']) { //email address(es) to notify
		$notList = explode(";", $evt['rml']);
		foreach ($notList as $emlAorL) {
			$emlList = array();
			if (strpos($emlAorL, '@')) { //email address
				$emlList[] = $emlAorL;
			} else { //email list
				if (file_exists("./emlists/$emlAorL.txt")) {
					$emlList = file("./emlists/$emlAorL.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
				}
			}
			foreach ($emlList as $emlAddress) {
				$emlAddress = trim($emlAddress);
				if (preg_match("/^\D\w*?(\.{0,1}(\w|-)+?){0,2}@((\w|-){2,}\.){1,4}\D{2,4}$/", $emlAddress)) { //valid email address
					if (mail($emlAddress, $subject, $message, $headers)) {
						$mailSent .= ++$count.'. '.str_replace("@","[at]",$emlAddress).' - '.$ax['not_subject'].': '.$subject."\n";
					}
				}
			}
		}
	}
}


//main program

//sanity check
if (!defined('LCC')) { exit('not permitted ('.substr(basename(__FILE__),0,-4).')'); } //lounch via script only

//initialize
$todayT = mktime(12,0,0); //12:00
$todayD00 = date("Y-m-d", $todayT); //today
$todayD30 = date("Y-m-d", $todayT + 2592000); //today + 30 days
$count = 0;
$mailSent = "";

//set filter
$filter = 'notify >= 0';

//retrieve and process events
retrieve($todayD00,$todayD30,'',$filter);

if ($evtList) {
	foreach($evtList as $date => &$events) {
		$daysDue = round((mktime(12,0,0,substr($date,5,2),substr($date,8,2),substr($date,0,4)) - $todayT) / 86400);
		foreach ($events as $evt) {
			if ($daysDue == $evt['rem'] or $date == $todayD00) {
				notify($evt,$date,$daysDue); //send reminder email
			}
		}
	}
}

$sumName = "-- ".$ax['not_sum_title']." --";
$sumText = (strlen($mailSent) > 0) ? $ax['not_not_sent_to'].":\n".$mailSent : $ax['not_no_not_dates_due'].".\n";
?>
