tp->toDB(varset($_GET['mode'],'makemail'));
$pageMode = varset($_GET['savepage'], $action); // Sometimes we need to know what brought us here - $action gets changed
$mailId = intval(varset($_GET['m'],0));
$targetId = intval(varset($_GET['t'],0));
// Create mail admin object, load all mail handlers
$mailAdmin = new mailoutAdminClass($action); // This decodes parts of the query using $_GET syntax
e107::setRegistry('_mailout_admin', $mailAdmin);
if ($mailAdmin->loadMailHandlers() == 0)
{ // No mail handlers loaded
echo 'No mail handlers loaded!!';
exit;
}
require_once(e_ADMIN.'auth.php');
$errors = array();
$subAction = '';
$midAction = '';
$fromHold = FALSE;
if (isset($_POST['mailaction']))
{
if (is_array($_POST['mailaction']))
{
foreach ($_POST['mailaction'] as $k => $v)
{
if ($v) // Look for non-empty action
{
$mailId = $k;
$action = $v;
break;
}
}
}
}
if (isset($_POST['targetaction']))
{
if (is_array($_POST['targetaction']))
{
foreach ($_POST['targetaction'] as $k => $v)
{
if ($v) // Look for non-empty action
{
$targetId = $k;
$action = $v;
break;
}
}
}
}
//echo "Action: {$action} MailId: {$mailId} Target: {$targetId}
";
// ----------------- Actions ------------------->
switch ($action)
{
case 'prefs' :
if (getperms('0'))
{
if (isset($_POST['testemail']))
{ // Send test email - uses standard 'single email' handler
if(trim($_POST['testaddress']) == '')
{
$emessage->add(LAN_MAILOUT_19, E_MESSAGE_ERROR);
$subAction = 'error';
}
else
{
$mailheader_e107id = USERID;
require_once(e_HANDLER.'mail.php');
$add = ($pref['mailer']) ? " (".strtoupper($pref['mailer']).")" : ' (PHP)';
$sendto = trim($_POST['testaddress']);
if (!sendemail($sendto, LAN_MAILOUT_113." ".SITENAME.$add, LAN_MAILOUT_114,LAN_MAILOUT_189))
{
$emessage->add(($pref['mailer'] == 'smtp') ? LAN_MAILOUT_67 : LAN_MAILOUT_106, E_MESSAGE_ERROR);
}
else
{
$emessage->add(LAN_MAILOUT_81. ' ('.$sendto.')', E_MESSAGE_SUCCESS);
$admin_log->log_event('MAIL_01',$sendto,E_LOG_INFORMATIVE,'');
}
}
}
elseif (isset($_POST['updateprefs']))
{
saveMailPrefs($emessage);
}
}
break;
case 'mailcopy' : // Copy existing email and go to edit screen
if (isset($_POST['mailaction']))
{
$action = 'makemail';
$mailData = $mailAdmin->retrieveEmail($mailId);
if ($mailData === FALSE)
{
$emessage->add(LAN_MAILOUT_164.':'.$mailId, E_MESSAGE_ERROR);
break;
}
unset($mailData['mail_source_id']);
}
break;
case 'mailedit' : // Edit existing mail
if (isset($_POST['mailaction']))
{
$action = 'makemail';
$mailData = $mailAdmin->retrieveEmail($mailId);
if ($mailData === FALSE)
{
$emessage->add(LAN_MAILOUT_164.':'.$mailId, E_MESSAGE_ERROR);
break;
}
}
break;
case 'makemail' :
$newMail = TRUE;
if (isset($_POST['save_email']))
{
$subAction = 'new';
}
elseif (isset($_POST['update_email']))
{
$subAction = 'update';
$newMail = FALSE;
}
elseif (isset($_POST['send_email']))
{ // Send bulk email
$subAction = 'send';
}
if ($subAction != '')
{
$mailData = $mailAdmin->parseEmailPost($newMail);
$errors = $mailAdmin->checkEmailPost($mailData, $subAction == 'send'); // Full check if sending email
if ($errors !== TRUE)
{
$subAction = 'error';
break;
}
$mailData['mail_selectors'] = $mailAdmin->getAllSelectors(); // Add in the selection criteria
}
// That's the checking over - now do something useful!
switch ($subAction)
{
case 'send' : // This actually creates the list of recipients in the display routine
$action = 'marksend';
break;
case 'new' :
// TODO: Check all fields created - maybe
$mailData['mail_content_status'] = MAIL_STATUS_SAVED;
$mailData['mail_create_app'] = 'core';
$result = $mailAdmin->saveEmail($mailData, TRUE);
if (is_numeric($result))
{
$mailData['mail_source_id'] = $result;
$emessage->add(LAN_MAILOUT_145, E_MESSAGE_SUCCESS);
}
else
{
$emessage->add(LAN_MAILOUT_146, E_MESSAGE_ERROR);
}
break;
case 'update' :
$mailData['mail_content_status'] = MAIL_STATUS_SAVED;
$result = $mailAdmin->saveEmail($mailData, FALSE);
if (is_numeric($result))
{
$mailData['mail_source_id'] = $result;
$emessage->add(LAN_MAILOUT_147, E_MESSAGE_SUCCESS);
}
else
{
$emessage->add(LAN_MAILOUT_146, E_MESSAGE_ERROR);
}
break;
}
break;
case 'mailhold' :
$action = 'held';
if ($mailAdmin->holdEmail($mailId))
{
$emessage->add(str_replace('--ID--', $mailId, LAN_MAILOUT_229), E_MESSAGE_SUCCESS);
}
else
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_230);
}
break;
case 'mailcancel' :
$action = $pageMode; // Want to return to some other page
if ($mailAdmin->cancelEmail($mailId))
{
$emessage->add(str_replace('--ID--', $mailId, LAN_MAILOUT_220), E_MESSAGE_SUCCESS);
}
else
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_221);
}
break;
case 'maildelete' :
break;
case 'marksend' : // Actually do something with an email and list of recipients - entry from email confirm page
$action = 'saved';
if (isset($_POST['email_cancel'])) // 'Cancel' in this context means 'delete' - don't want it any more
{
$midAction = 'midDeleteEmail';
}
elseif (isset($_POST['email_hold']))
{
if ($mailAdmin->activateEmail($mailId, TRUE))
{
$emessage->add(str_replace('--ID--', $mailId, LAN_MAILOUT_187), E_MESSAGE_SUCCESS);
}
else
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_166);
}
$action = 'held';
}
elseif (isset($_POST['email_send']))
{
$midAction = 'midMoveToSend';
$action = 'pending';
}
break;
case 'mailsendnow' : // Send mail previously on 'held' list. Need to give opportunity to change time/date etc
// $midAction = 'midMoveToSend';
// $action = 'pending';
$action = 'marksend'; // This shows the email details for confirmation
$fromHold = TRUE;
$mailData['mail_source_id'] = $mailId;
break;
case 'maildeleteconfirm' :
$action = $pageMode; // Want to return to some other page
$midAction = 'midDeleteEmail';
if (!isset($_POST['mailIDConf']) || (intval($_POST['mailIDConf']) != $mailId))
{
$errors[] = str_replace(array('--ID--', '--CHECK--'), array($mailId, intval($_POST['mailIDConf'])), LAN_MAILOUT_174);
break;
}
break;
case 'mailonedelete' :
case 'debug' :
$emessage->add('Not implemented yet', E_MESSAGE_ERROR);
break;
case 'mailtargets' :
$action = 'recipients';
// Intentional fall-through
case 'recipients' :
case 'saved' : // Show template emails - probably no actions
case 'sent' :
case 'pending' :
case 'held' :
if (isset($_POST['etrigger_ecolumns']))
{
$mailAdmin->mailbodySaveColumnPref($action);
}
break;
case 'maint' : // Perform any maintenance actions required
if (isset($_POST['email_dross']))
if ($mailAdmin->dbTidy()) // Admin logging done in this routine
{
$emessage->add(LAN_MAILOUT_184, E_MESSAGE_SUCCESS);
}
else
{
$errors[] = LAN_MAILOUT_183;
}
break;
default :
$emessage->add('Code malfunction 23! ('.$action.')', E_MESSAGE_ERROR);
$e107->ns->tablerender(LAN_MAILOUT_97, $emessage->render());
exit; // Could be a hack attempt
} // switch($action) - end of 'executive' tasks
// ------------------------ Intermediate actions ---------------------------
// (These have more than one route to trigger them)
switch ($midAction)
{
case 'midDeleteEmail' :
// $emessage->add($pageMode.': Would delete here: '.$mailId, E_MESSAGE_SUCCESS);
// break; // Delete this
$result = $mailAdmin->deleteEmail($mailId, 'all');
$admin_log->log_event('MAIL_04','ID: '.$mailId,E_LOG_INFORMATIVE,'');
if (($result === FALSE) || !is_array($result))
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_166);
}
else
{
if (isset($result['content']))
{
if ($result['content'] === FALSE)
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_167);
}
else
{
$emessage->add(str_replace('--ID--', $mailId, LAN_MAILOUT_167), E_MESSAGE_SUCCESS);
}
}
if (isset($result['recipients']))
{
if ($result['recipients'] === FALSE)
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_169);
}
else
{
$emessage->add(str_replace(array('--ID--', '--NUM--'), array($mailId, $result['recipients']), LAN_MAILOUT_170), E_MESSAGE_SUCCESS);
}
}
}
break;
case 'midMoveToSend' :
$notify = isset($_POST['mail_notify_complete']) ? 3 : 2;
$first = 0;
$last = 0; // Set defaults for earliest and latest send times.
// TODO: Save these fields
if (isset($_POST['mail_earliest_time']))
{
$first = e107::getDateConvert()->decodeDateTime($_POST['mail_earliest_time'], 'datetime', CORE_DATE_ORDER, FALSE);
}
if (isset($_POST['mail_latest_time']))
{
$last = e107::getDateConvert()->decodeDateTime($_POST['mail_earliest_time'], 'datetime', CORE_DATE_ORDER, TRUE);
}
if ($mailAdmin->activateEmail($mailId, FALSE, $notify, $first, $last))
{
$emessage->add(LAN_MAILOUT_185, E_MESSAGE_SUCCESS);
$admin_log->log_event('MAIL_06','ID: '.$mailId,E_LOG_INFORMATIVE,'');
}
else
{
$errors[] = str_replace('--ID--', $mailId, LAN_MAILOUT_188);
}
break;
}
// --------------------- Display errors and results ------------------------
if (is_array($errors) && (count($errors) > 0))
{
foreach ($errors as $e)
{
$emessage->add($e, E_MESSAGE_ERROR);
}
unset($errors);
}
if ($emessage->hasMessage())
{
$e107->ns->tablerender(LAN_MAILOUT_97, $emessage->render());
}
// ---------------------- Display required page ----------------------------
// At this point $action determines which page display is required - one of a
// fairly limited number of choices
$mailAdmin->newMode($action);
//echo "Action: {$action} MailId: {$mailId} Target: {$targetId}
";
switch ($action)
{
case 'prefs' :
if (getperms('0'))
{
show_prefs($mailAdmin);
}
break;
case 'maint' :
if (getperms('0'))
{
show_maint(FALSE);
}
break;
case 'debug' :
if (getperms('0'))
{
show_maint(TRUE);
}
break;
case 'saved' : // Show template emails
case 'sent' :
case 'pending' :
case 'held' :
$mailAdmin->showEmailList($action, -1, -1);
break;
case 'maildelete' : // NOTE:: need to set previous page in form
$mailAdmin->showDeleteConfirm($mailId, $pageMode);
break;
case 'marksend' : // Show the send confirmation page
$mailAdmin->sendEmailCircular($mailData, $fromHold);
break;
case 'recipients' :
$mailAdmin->showmailRecipients($mailId, $action);
break;
case 'makemail' :
default :
if (!is_array($mailData))
{
$mailData = array(); // Empty array just in case
}
$mailAdmin->show_mailform($mailData);
break;
}
require_once(e_ADMIN."footer.php");
// Update Preferences. (security handled elsewhere)
function saveMailPrefs(&$emessage)
{
global $pref;
$e107 = e107::getInstance();
$bounceOpts = array('none' => LAN_MAILOUT_232, 'auto' => LAN_MAILOUT_233, 'mail' => LAN_MAILOUT_234);
unset($temp);
if (!in_array($_POST['mailer'], array('smtp', 'sendmail', 'php'))) $_POST['mailer'] = 'php';
$temp['mailer'] = $_POST['mailer'];
// Allow qmail as an option as well - works much as sendmail
if ((strpos($_POST['sendmail'],'sendmail') !== FALSE) || (strpos($_POST['sendmail'],'qmail') !== FALSE)) $temp['sendmail'] = $e107->tp->toDB($_POST['sendmail']);
$temp['smtp_server'] = $e107->tp->toDB($_POST['smtp_server']);
$temp['smtp_username'] = $e107->tp->toDB($_POST['smtp_username']);
$temp['smtp_password'] = $e107->tp->toDB($_POST['smtp_password']);
$smtp_opts = array();
switch (trim($_POST['smtp_options']))
{
case 'smtp_ssl' :
$smtp_opts[] = 'secure=SSL';
break;
case 'smtp_tls' :
$smtp_opts[] = 'secure=TLS';
break;
case 'smtp_pop3auth' :
$smtp_opts[] = 'pop3auth';
break;
}
if (varsettrue($_POST['smtp_keepalive'])) $smtp_opts[] = 'keepalive';
if (varsettrue($_POST['smtp_useVERP'])) $smtp_opts[] = 'useVERP';
$temp['smtp_options'] = implode(',',$smtp_opts);
$temp['mail_sendstyle'] = $e107->tp->toDB($_POST['mail_sendstyle']);
$temp['mail_pause'] = intval($_POST['mail_pause']);
$temp['mail_pausetime'] = intval($_POST['mail_pausetime']);
$temp['mail_workpertick'] = intval($_POST['mail_workpertick']);
$temp['mail_workpertick'] = min($temp['mail_workpertick'],1000);
$temp['mail_bounce'] = isset($bounceOpts[$_POST['mail_bounce']]) ? $_POST['mail_bounce'] : 'none';
switch ($temp['mail_bounce'])
{
case 'none' :
$temp['mail_bounce_email'] = '';
break;
case 'auto' :
$temp['mail_bounce_email'] = $e107->tp->toDB($_POST['mail_bounce_email2']);
break;
case 'mail' :
$temp['mail_bounce_email'] = $e107->tp->toDB($_POST['mail_bounce_email']);
$temp['mail_bounce_auto'] = intval($_POST['mail_bounce_auto']);
break;
}
$temp['mail_bounce_pop3'] = $e107->tp->toDB($_POST['mail_bounce_pop3']);
$temp['mail_bounce_user'] = $e107->tp->toDB($_POST['mail_bounce_user']);
$temp['mail_bounce_pass'] = $e107->tp->toDB($_POST['mail_bounce_pass']);
$temp['mail_bounce_type'] = $e107->tp->toDB($_POST['mail_bounce_type']);
$temp['mail_bounce_delete'] = intval($_POST['mail_bounce_delete']);
$temp['mailout_enabled'] = implode(',',$_POST['mail_mailer_enabled']);
$temp['mail_log_options'] = intval($_POST['mail_log_option']).','.intval($_POST['mail_log_email']);
if ($e107->admin_log->logArrayDiffs($temp, $pref, 'MAIL_03'))
{
save_prefs(); // Only save if changes - generates its own message
// $emessage->add(LAN_SETSAVED, E_MESSAGE_SUCCESS);
}
else
{
$emessage->add(LAN_NO_CHANGE, E_MESSAGE_INFO);
}
}
//----------------------------------------------------
// MAILER OPTIONS
//----------------------------------------------------
function show_prefs($mailAdmin)
{
global $pref;
$e107 = e107::getInstance();
$frm = e107::getForm();
$mes = e107::getMessage();
e107::getCache()->CachePageMD5 = '_';
$lastload = e107::getCache()->retrieve('emailLastBounce',FALSE,TRUE,TRUE);
$lastBounce = round((time() - $lastload) / 60);
$lastBounceText = ($lastBounce > 1256474) ? "Never" : "".$lastBounce . " minutes ago.";
$text = "