#!/usr/local/bin/php -q
\n";
// Get a list of people who have asked not to receive email
$res = dbi_query ( "SELECT cal_login FROM webcal_user_pref " .
"WHERE cal_setting = 'EMAIL_REMINDER' " .
"AND cal_value = 'N'" );
$noemail = array ();
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$user = $row[0];
$noemail[$user] = 1;
if ( $debug )
echo "User $user does not want email.
\n";
}
dbi_free_result ( $res );
}
// Get a list of the email users in the system.
// They must also have an email address. Otherwise, we can't
// send them mail, so what's the point?
$allusers = user_get_users ();
for ( $i = 0; $i < count ( $allusers ); $i++ ) {
$names[$allusers[$i]['cal_login']] = $allusers[$i]['cal_fullname'];
$emails[$allusers[$i]['cal_login']] = $allusers[$i]['cal_email'];
}
// Get all users language settings.
$res = dbi_query ( "SELECT cal_login, cal_value FROM webcal_user_pref " .
"WHERE cal_setting = 'LANGUAGE'" );
$languages = array ();
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$user = $row[0];
$user_lang = $row[1];
$languages[$user] = $user_lang;
if ( $debug )
echo "Language for $user is \"$user_lang\"
\n";
}
dbi_free_result ( $res );
}
// Get all users timezone settings.
$res = dbi_query ( "SELECT cal_login, cal_value FROM webcal_user_pref " .
"WHERE cal_setting = 'TZ_OFFSET'" );
$tzoffset = array ();
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$user = $row[0];
$user_tzoffset = $row[1];
$tzoffset[$user] = $user_tzoffset;
if ( $debug )
echo "TZ OFFSET for $user is \"$user_tzoffset\"
\n";
}
dbi_free_result ( $res );
}
$startdate = date ( "Ymd" );
$enddate = date ( "Ymd", time() + ( $DAYS_IN_ADVANCE * 24 * 3600 ) );
// Now read events all the repeating events (for all users)
$repeated_events = query_events ( "", true, "AND (webcal_entry_repeats.cal_end > $startdate OR webcal_entry_repeats.cal_end IS NULL) " );
// Read non-repeating events (for all users)
if ( $debug )
echo "Checking for events from date $startdate to date $enddate
\n";
$events = read_events ( "", $startdate, $enddate );
if ( $debug )
echo "Found " . count ( $events ) . " events in time range.
\n";
function indent ( $str ) {
return " " . str_replace ( "\n", "\n ", $str );
}
// Send a reminder for a single event for a single day to all
// participants in the event.
// Send to participants who have accepted as well as those who have not yet
// approved. But, don't send to users how rejected (cal_status='R').
function send_reminder ( $id, $event_date ) {
global $names, $emails, $site_extras, $debug, $only_testing,
$server_url, $languages, $tzoffset, $application_name;
global $EXTRA_TEXT, $EXTRA_MULTILINETEXT, $EXTRA_URL, $EXTRA_DATE,
$EXTRA_EMAIL, $EXTRA_USER, $EXTRA_REMINDER, $LANGUAGE, $LOG_REMINDER;
global $allow_external_users, $external_reminders;
$pri[1] = translate("Low");
$pri[2] = translate("Medium");
$pri[3] = translate("High");
// get participants first...
$sql = "SELECT cal_login FROM webcal_entry_user " .
"WHERE cal_id = $id AND cal_status IN ('A','W') " .
"ORDER BY cal_login";
$res = dbi_query ( $sql );
$participants = array ();
$num_participants = 0;
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$participants[$num_participants++] = $row[0];
}
}
// get external participants
$ext_participants = array ();
$num_ext_participants = 0;
if ( ! empty ( $allow_external_users ) && $allow_external_users == "Y" &&
! empty ( $external_reminders ) && $external_reminders == "Y" ) {
$sql = "SELECT cal_fullname, cal_email FROM webcal_entry_ext_user " .
"WHERE cal_id = $id AND cal_email IS NOT NULL " .
"ORDER BY cal_fullname";
$res = dbi_query ( $sql );
if ( $res ) {
while ( $row = dbi_fetch_row ( $res ) ) {
$ext_participants[$num_ext_participants] = $row[0];
$ext_participants_email[$num_ext_participants++] = $row[1];
}
}
}
if ( ! $num_participants && ! $num_ext_participants ) {
if ( $debug )
echo "No participants found for event id: $id
\n";
return;
}
// get event details
$res = dbi_query (
"SELECT cal_create_by, cal_date, cal_time, cal_mod_date, " .
"cal_mod_time, cal_duration, cal_priority, cal_type, cal_access, " .
"cal_name, cal_description FROM webcal_entry WHERE cal_id = $id" );
if ( ! $res ) {
echo "Db error: could not find event id $id.\n";
return;
}
if ( ! ( $row = dbi_fetch_row ( $res ) ) ) {
echo "Error: could not find event id $id in database.\n";
return;
}
// send mail. we send one user at a time so that we can switch
// languages between users if needed.
$mailusers = array ();
$recipients = array ();
if ( isset ( $single_user ) && $single_user == "Y" ) {
$mailusers[] = $emails[$single_user_login];
$recipients[] = $single_user_login;
} else {
for ( $i = 0; $i < count ( $participants ); $i++ ) {
if ( strlen ( $emails[$participants[$i]] ) ) {
$mailusers[] = $emails[$participants[$i]];
$recipients[] = $participants[$i];
} else {
if ( $debug )
echo "No email for user $participants[$i]
\n";
}
}
for ( $i = 0; $i < count ( $ext_participants ); $i++ ) {
$mailusers[] = $ext_participants_email[$i];
$recipients[] = $ext_participants[$i];
}
}
if ( $debug )
echo "Found " . count ( $mailusers ) . " with email addresses
\n";
for ( $j = 0; $j < count ( $mailusers ); $j++ ) {
$recip = $mailusers[$j];
$user = $participants[$j];
if ( ! empty ( $languages[$user] ) )
$userlang = $languages[$user];
else
$userlang = $LANGUAGE; // system default
if ( $userlang == "none" )
$userlang = "English-US"; // gotta pick something
if ( $debug )
echo "Setting language to \"$userlang\"
\n";
reset_language ( $userlang );
// reset timezone setting for current user
if ( empty ( $tzoffset[$user] ) )
$GLOBALS["TZ_OFFSET"] = 0;
else
$GLOBALS["TZ_OFFSET"] = $tzoffset[$user];
$body = translate("This is a reminder for the event detailed below.") .
"\n\n";
$create_by = $row[0];
$name = $row[9];
$description = $row[10];
// add trailing '/' if not found in server_url
if ( ! empty ( $server_url ) ) {
if ( substr ( $server_url, -1, 1 ) == "/" ) {
$body .= $server_url . "view_entry.php?id=" . $id . "\n\n";
} else {
$body .= $server_url . "/view_entry.php?id=" . $id . "\n\n";
}
}
$body .= strtoupper ( $name ) . "\n\n";
$body .= translate("Description") . ":\n";
$body .= indent ( $description ) . "\n";
$body .= translate("Date") . ": " . date_to_str ( $event_date ) . "\n";
if ( $row[2] >= 0 )
$body .= translate ("Time") . ": " . display_time ( $row[2] ) . "\n";
if ( $row[5] > 0 )
$body .= translate ("Duration") . ": " . $row[5] .
" " . translate("minutes") . "\n";
if ( ! empty ( $disable_priority_field ) && ! $disable_priority_field )
$body .= translate("Priority") . ": " . $pri[$row[6]] . "\n";
if ( ! empty ( $disable_access_field ) && ! $disable_access_field )
$body .= translate("Access") . ": " .
( $row[8] == "P" ? translate("Public") : translate("Confidential") ) .
"\n";
if ( ! empty ( $single_user_login ) && $single_user_login == false )
$body .= translate("Created by") . ": " . $row[0] . "\n";
$body .= translate("Updated") . ": " . date_to_str ( $row[3] ) . " " .
display_time ( $row[4] ) . "\n";
// site extra fields
$extras = get_site_extra_fields ( $id );
for ( $i = 0; $i < count ( $site_extras ); $i++ ) {
$extra_name = $site_extras[$i][0];
$extra_descr = $site_extras[$i][1];
$extra_type = $site_extras[$i][2];
if ( $extras[$extra_name]['cal_name'] != "" ) {
$body .= translate ( $extra_descr ) . ": ";
if ( $extra_type == $EXTRA_DATE ) {
$body .= date_to_str ( $extras[$extra_name]['cal_date'] ) . "\n";
} else if ( $extra_type == $EXTRA_MULTILINETEXT ) {
$body .= "\n" . indent ( $extras[$extra_name]['cal_data'] ) . "\n";
} else if ( $extra_type == $EXTRA_REMINDER ) {
$body .= ( $extras[$extra_name]['cal_remind'] > 0 ?
translate("Yes") : translate("No") ) . "\n";
} else {
// default method for $EXTRA_URL, $EXTRA_TEXT, etc...
$body .= $extras[$extra_name]['cal_data'] . "\n";
}
}
}
if ( ! empty ( $single_user ) && $single_user != "Y" &&
! empty ( $disable_participants_field ) && ! $disable_participants_field ) {
$body .= translate("Participants") . ":\n";
for ( $i = 0; $i < count ( $participants ); $i++ ) {
$body .= " " . $names[$participants[$i]] . "\n";
}
for ( $i = 0; $i < count ( $ext_participants ); $i++ ) {
$body .= " " . $ext_participants[$i] . " (" .
translate("External User") . ")\n";
}
}
$subject = translate("Reminder") . ": " . $name;
if ( strlen ( $GLOBALS["email_fallback_from"] ) )
$extra_hdrs = "From: " . $GLOBALS["email_fallback_from"] . "\r\n" .
"X-Mailer: " . translate($application_name);
else
$extra_hdrs = "X-Mailer: " . translate($application_name);
if ( $debug )
echo "Sending mail to $recip (in $userlang)\n";
if ( $only_testing ) {
if ( $debug )
echo "
To: $recip\nSubject: $subject\n$extra_hdrs\n\n$body\n\n\n"; } else { mail ( $recip, $subject, $body, $extra_hdrs ); activity_log ( $id, "system", $user, $LOG_REMINDER, "" ); } } } // keep track of the fact that we send the reminder, so we don't // do it again. function log_reminder ( $id, $name, $event_date ) { global $only_testing; if ( ! $only_testing ) { dbi_query ( "DELETE FROM webcal_reminder_log " . "WHERE cal_id = $id AND cal_name = '$name' " . "AND cal_event_date = $event_date" ); dbi_query ( "INSERT INTO webcal_reminder_log " . "( cal_id, cal_name, cal_event_date, cal_last_sent ) VALUES ( " . "$id, '" . $name . "', $event_date, " . time() . ")" ); } } // Process an event for a single day. Check to see if it has // a reminder, when it needs to be sent and when the last time it // was sent. function process_event ( $id, $name, $event_date, $event_time ) { global $site_extras, $debug, $only_testing; global $EXTRA_REMINDER_WITH_OFFSET, $EXTRA_REMINDER_WITH_DATE; if ( $debug ) printf ( "Event %d: \"%s\" at %s on %s