Copyright (C) 2005 James Grant 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, version 2. 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; see the file COPYING. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ ?> >../data/logs/emailqueue.log 2>&1 &"); } /* dialog_choose * select: comm_dialog_choose_select(emails_id) * cancel: comm_dialog_choose_cancel() */ switch(get_value_from_array($_GET, 'action')) { case 'dialog_choose_load': $emails_id = intval($_GET['emails_id']); $q = $pdo->prepare("SELECT * FROM emails WHERE id='$emails_id'"); $q->execute(); $e = $q->fetch(PDO::FETCH_ASSOC); ?>
:
:
:
prepare("INSERT INTO emails(type,val) VALUES('$type','$key')"); $q->execute(); echo $pdo->errorInfo(); $id = lastInsertId(); } else { error_("Email Key and Name are required"); exit; } } /* Allow the fundraising campaigns id to be NULL, it'll never be 0 */ $fcstr = ($fcid == 0) ? 'NULL' : "'$fcid'"; $body=getTextFromHtml($bodyhtml); $q = $pdo->prepare("UPDATE emails SET name='$name', description='$description', `from`='$from', subject='$subject', body='$body', bodyhtml='$bodyhtml', fundraising_campaigns_id=$fcstr WHERE id='$id'"); $q->execute(); echo $pdo->errorInfo(); happy_("Email Saved"); exit; case 'dialog_edit': if(array_key_exists('id', $_GET)) { $id = intval($_GET['id']); $cloneid = 0; } else if(array_key_exists('cloneid', $_GET)) { $id = intval($_GET['cloneid']); $clone_id = $id; } else { /* new email, set defaults which may be specified */ $id = 0; $key = htmlspecialchars($_GET['key']); if(array_key_exists('fundraising_campaigns_id', $_GET)) { $fcid = intval( $_GET['fundraising_campaigns_id']); $type = 'fundraising'; $q=$pdo->prepare("SELECT * FROM fundraising_campaigns WHERE id='$fcid'"); $fc=$q->fetch(PDO::FETCH_OBJ); $name=i18n("%1 communication for %2",array(ucfirst($key),$fc->name)); } else { $fcid = 0; $type = (array_key_exists('type',$_GET)) ? $_GET['type'] : 'user'; } $from=$_SESSION['name']." <".$_SESSION['email'].">"; } if($id) { $q = $pdo->prepare("SELECT * FROM emails WHERE id='$id'"); $q->execute(); if($q->rowCount() != 1) { echo "Ambiguous edit"; exit; } $e = $q->fetch(PDO::FETCH_ASSOC); /* If we're supposed to clone it, load it then zero out the * id so we make a new record on save, and override the key */ if($clone_id) { $e['id'] = 0; $e['val'] = $_GET['key']; $e['fundraising_campaigns_id'] = $_GET['fundraising_campaigns_id']; } $emails_id = $e['id']; $name = htmlspecialchars($e['name']); $key = htmlspecialchars($e['val']); $description = htmlspecialchars($e['description']); $from = htmlspecialchars($e['from']); if(!$from && $config['fairmanageremail']) $from="Fair Manager <".$config['fairmanageremail'].">"; $subject = htmlspecialchars($e['subject']); $body = $e['body']; $bodyhtml = $e['bodyhtml']; $fcid = intval($e['fundraising_campaigns_id']); if($bodyhtml == '') $bodyhtml = nl2br($body); } ?> prepare("SELECT * FROM emails WHERE `val`='".$_GET['template']."'"); $emailq->execute(); $e=$emailq->fetch(PDO::FETCH_ASSOC); } else $e=null; $from=htmlspecialchars($_SESSION['name']." <".$_SESSION['email'].">"); $to=htmlspecialchars($u['emailrecipient']); $subject = htmlspecialchars($e['subject']); //useless but we might as well have it $name = htmlspecialchars($e['name']); $key = htmlspecialchars($e['val']); $description = htmlspecialchars($e['description']); //do the replacements from the template now, so what the person see's is what gets sent. $body = communication_replace_vars($e['body'],$u); $bodyhtml = communication_replace_vars($e['bodyhtml'],$u); //if there's no html,. grab the html from the non-html version if($bodyhtml == '') $bodyhtml = nl2br($body); ?> prepare("SELECT * FROM emails ORDER BY type,name"); echo ""; echo ""; echo " "; echo " "; echo " "; echo ""; while($r=$q->fetch(PDO::FETCH_OBJ)) { if($r->fundraising_campaigns_id) $fcid=$r->fundraising_campaigns_id; else $fcid='null'; if($r->name) $name=$r->name; else $name=i18n("no email name specified"); echo ""; echo ""; echo " \n"; echo ""; } echo "
".i18n("Name")."".i18n("Type")."".i18n("Actions")."
val)."',$r->id,$fcid)\">",htmlspecialchars($name)."$r->type"; //only user emails can be deleted, system ones are required and cannot be removed if($r->type=="user") { echo " "; echo "id\">"; echo " "; echo "id\">".i18n("Send").""; } echo "
"; exit; case 'cancel': if($_GET['cancel']) { $q = $pdo->prepare("UPDATE emailqueue SET finished=NOW() WHERE id='".intval($_GET['cancel'])."'"); $q->execute(); $q = $pdo->prepare("UPDATE emailqueue_recipients SET result='cancelled' WHERE emailqueue_id='".intval($_GET['cancel'])."' AND sent IS NULL AND result IS NULL"); $q->execute(); echo "ok"; } exit; case 'loadaddresses': if($_GET['query'] && array_key_exists($_GET['query'],$mailqueries)) { $q = $pdo->prepare($mailqueries[$_GET['query']]['query']); $q->execute(); while($r=$q->fetch(PDO::FETCH_OBJ)) { if($r->organization) $s="($r->organization) "; else $s=""; echo "$r->firstname $r->lastname {$s}<$r->email>
"; } } exit; } if(get_value_from_array($_GET, 'action') == "sendqueue") { $fcid=intval($_POST['fundraising_campaigns_id']); $emailid=intval($_POST['emails_id']); $fcq = $pdo->prepare("SELECT * FROM fundraising_campaigns WHERE id='$fcid'"); $fcq->execute(); $fc=$fcq->fetch(PDO::FETCH_OBJ); $emailq = $pdo->prepare("SELECT * FROM emails WHERE id='$emailid'"); $emailq.execute(); $email=$emailq.fetch(PDO::FETCH_OBJ); $recipq = $pdo->prepare("SELECT * FROM fundraising_campaigns_users_link WHERE fundraising_campaigns_id='$fcid'"); $recipq.execute(); echo $pdo->errorInfo(); $numtotal=$recipq->rowCount(); $q = $pdo->prepare("INSERT INTO emailqueue (val,name,users_uid,`from`,subject,body,bodyhtml,`type`,fundraising_campaigns_id,started,finished,numtotal,numsent) VALUES ( '".$email->val."', '".$email->name."', '".$_SESSION['users_uid']."', '".$email->from."', '".$email->subject."', '".$email->body."', '".$email->bodyhtml."', '".$email->type."', $fcid, NOW(), NULL, $numtotal, 0)"); $q->execute(); $emailqueueid=$pdo->lastInsertId(); echo $pdo->errorInfo(); $urlproto = $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $urlmain = "$urlproto{$_SERVER['HTTP_HOST']}{$config['SFIABDIRECTORY']}"; $urllogin = "$urlmain/login.php"; while($r=$recipq->fetch(PDO::FETCH_OBJ)) { $u=user_load_by_uid($r->users_uid); //we only send school access codes to science heads or principals $acq = $pdo->prepare("SELECT accesscode FROM schools WHERE (sciencehead_uid='{$u['uid']}' OR principal_uid='{$u['uid']}') AND `year`='{$config['FAIRYEAR']}'"); $acq->execute(); $acr=$acq->fetch(PDO::FETCH_OBJ); $accesscode=$acr->accesscode; $replacements=array( "FAIRNAME"=>$config['fairname'], "SALUTATION"=>$u['salutation'], "FIRSTNAME"=>$u['firstname'], "LASTNAME"=>$u['lastname'], "NAME"=>$u['name'], "EMAIL"=>$u['email'], "ORGANIZATION"=>$u['sponsor']['organization'], "URLMAIN"=>$urlmain, "URLLOGIN"=>$urllogin, "ACCESSCODE"=>$accesscode, ); if($u['email'] && $u['email'][0] != '*') { $q = $pdo->prepare("INSERT INTO emailqueue_recipients (emailqueue_id,toemail,toname,replacements,sent) VALUES ( '$emailqueueid', '".$u['email']."', '".$u['name']."', '".json_encode($replacements."', NULL)")); $q->execute(); echo $pdo->erroInfo(); } $q = $pdo->prepare("UPDATE emails SET lastsent=NOW() WHERE id='$emailid'"); $q->execute(); } echo "ok"; launchQueue(); exit; } send_header("Communication", array('Committee Main' => 'committee_main.php', 'Administration' => 'admin/index.php'), "communication" ); echo "
"; ?> prepare("DELETE FROM emails WHERE id='".$_GET['delete']."' AND `type`='user'"); $q->execute(); echo happy("Email successfully deleted"); } if(get_value_from_array($_GET, 'action') == "send" && get_value_from_array($_GET, 'send')) { echo $pdo->errorInfo(); $q = $pdo->prepare("SELECT * FROM emails WHERE id='".$_GET['send']."'"); $q->execute(); $r=$q->fetch(PDO::FETCH_OBJ); echo i18n("Please confirm you would like to send the following email, and choose who to send it to"); echo "
"; echo "
"; echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; if($r->bodyhtml) { $body=$r->bodyhtml; } else { $body=nl2br(htmlspecialchars($r->body)); } echo ""; echo "
From:".htmlspecialchars($r->from)."
To:"; echo ""; echo ""; echo "
empty
"; echo "
Date:".date("r")."
Subject:".htmlspecialchars($r->subject)."
".$body."
(".mb_detect_encoding($body).")
"; if(!function_exists("exec")) { echo "
Sending requires php's exec() function to be available
\n"; } else { echo ""; echo ""; echo "
"; echo ""; echo ""; echo ""; echo ""; echo ""; echo "
"; echo ""; echo "
"; echo "
"; } //echo $str; } else if(get_value_from_array($_POST, 'action') == "reallysend" && get_value_from_array($_POST, 'reallysend') && get_value_from_array($_POST, 'to')) { $emailid=intval($_POST['reallysend']); $emailq=$pdo->prepare("SELECT * FROM emails WHERE id='$emailid'"); $email=$emailq->fetch(PDO::FETCH_OBJ); $to=$_POST['to']; if(array_key_exists($to,$mailqueries)) { $recipq=$pdo->prepare($mailqueries[$to]['query']); $recipq->execute(); } $numtotal=$recipq->rowCount(); $q = $pdo->prepare("INSERT INTO emailqueue (val,name,users_uid,`from`,subject,body,bodyhtml,`type`,fundraising_campaigns_id,started,finished,numtotal,numsent) VALUES ( '".$email->val."', '".$email->name."', '".$_SESSION['users_uid']."', '".$email->from."', '".$email->subject."', '".$email->body."', '".$email->bodyhtml."', '".$email->type."', NULL, NOW(), NULL, $numtotal, 0)"); $q->execute(); $emailqueueid=lastInsertId(); echo $pdo->errorInfo(); $urlproto = $_SERVER['SERVER_PORT'] == 443 ? "https://" : "http://"; $urlmain = "$urlproto{$_SERVER['HTTP_HOST']}{$config['SFIABDIRECTORY']}"; $urllogin = "$urlmain/login.php"; while($r=$recipq->fetch(PDO::FETCH_OBJ)) { if($r->uid) $u=user_load_by_uid($r->uid); else if($r->users_uid) $u=user_load_by_uid($r->users_uid); else { $toname=$r->firstname." ".$r->lastname; $toemail=$r->email; $replacements=array( "FAIRNAME"=>$config['fairname'], "FIRSTNAME"=>$r->firstname, "LASTNAME"=>$r->lastname, "NAME"=>$r->firstname." ".$r->lastname, "EMAIL"=>$r->email, "ORGANIZATION"=>$r->organization, "URLMAIN"=>$urlmain, "URLLOGIN"=>$urllogin, "ACCESSCODE"=>"unknown", ); } if($u) { //we only send school access codes to science heads or principals $acq=$pdo->prepare("SELECT accesscode FROM schools WHERE (sciencehead_uid='{$u['uid']}' OR principal_uid='{$u['uid']}') AND `year`='{$config['FAIRYEAR']}'"); $acq->execute(); echo $pdo->errorInfo(); $acr=$acq->fetch(PDO::FETCH-OBJ); $accesscode=$acr->accesscode; $replacements=array( "FAIRNAME"=>$config['fairname'], "SALUTATION"=>$u['salutation'], "FIRSTNAME"=>$u['firstname'], "LASTNAME"=>$u['lastname'], "NAME"=>$u['name'], "EMAIL"=>$u['email'], "ORGANIZATION"=>$u['sponsor']['organization'], "URLMAIN"=>$urlmain, "URLLOGIN"=>$urllogin, "ACCESSCODE"=>$accesscode, ); $toname=$u['name']; $toemail=$u['email']; } if($toemail) { $q = $pdo->prepare("INSERT INTO emailqueue_recipients (emailqueue_id,toemail,toname,replacements,sent) VALUES ( '$emailqueueid', '".$toemail."', '".$toname."', '".json_encode($replacements)."', NULL)"); $q->execute(); echo $pdo->errorInfo(); } $q = $pdo->prepare("UPDATE emails SET lastsent=NOW() WHERE id='$emailid'"); $q->execute(); } launchQueue(); echo "
"; echo happy("Email Communication sending has started!"); echo "
"; echo "Click here to see the sending progress"; } else if(get_value_from_array($_GET, 'action') == "restartqueue") { launchQueue(); echo "
"; echo happy("Email Communication sending has started!"); echo "
"; echo "Click here to see the sending progress"; } else { if(!$config['fairmanageremail']) echo notice(i18n("Warning: The 'Fair Manager Email' has not been set in SFIAB Configuration / Configuration Variables / Global. Please set it. The 'Fair Manager Email' is the default 'From' address for all emails and without a 'From' address, no emails can be sent!")); echo "".i18n("Email Queue Status and History")."
"; echo "".i18n("Add New Email").""; echo "
\n"; echo "
\n"; echo "
"; ?>