<?php
// define some variables
$def_wait = 10;
$def_size = 100;
$def_email_include_to = '1'; // YES is the default
$email_include_to_yes = 'checked="checked"';
$email_include_to_no = '';
$def_email_format = '1'; // HTML is the default
$email_format_html = 'checked="checked"';
$email_format_text = '';
// here starts the admin part
if( !empty($setmodules) )
{
$filename = basename(__FILE__);
$module['General']['Мега Mail'] = $filename;
return;
}
require('./pagestart.php');
//
// Load default header
//
$no_page_header = TRUE;
//define('BB_ROOT', './');
//BB_ROOT = './../';
//include(BB_ROOT . 'language/lang_' . $bb_cfg['default_lang_dir'] . 'lang_megamail.php' );
include($bb_cfg['default_lang_dir'] .'lang_megamail.php');
//define('BB_MEGAMAIL', $table_prefix.'megamail');
//
// Increase maximum execution time in case of a lot of users, but don't complain about it if it isn't
// allowed.
//
@set_time_limit(1200);
$message = '';
$subject = '';
if ( isset($_GET['mode']))
{
$sql = "CREATE TABLE ". BB_MEGAMAIL ." (
mail_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
mailsession_id VARCHAR(32) NOT NULL,
group_id MEDIUMINT(8) NOT NULL,
email_subject VARCHAR(60) NOT NULL,
email_body TEXT NOT NULL,
batch_start MEDIUMINT(8) NOT NULL,
batch_size SMALLINT UNSIGNED NOT NULL,
batch_wait SMALLINT NOT NULL,
status SMALLINT(1) NOT NULL,
user_id MEDIUMINT(8) NOT NULL,
email_format SMALLINT(1) NOT NULL,
email_include_to SMALLINT(1) NOT NULL
)";
if ( !($result = DB()->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not create tables. Are you sure you are using mySQL? Are you sure the table does not already exist?', '', __LINE__, __FILE__, $sql);
}
}
//
// Do the job ...
//
if ( isset($_POST['message']) || isset($_POSTS['subject']) )
{
$batchsize = (is_numeric($_POST['batchsize'])) ? intval($_POST['batchsize']) : $def_size;
$batchwait = (is_numeric($_POST['batchwait'])) ? intval($_POST['batchwait']) : $def_wait;
$email_format = ( isset($_POS['email_format']) ) ? $_POST['email_format'] : $def_email_format;
$email_include_to = ( isset($_POST['email_include_to']) ) ? $_POST['email_include_to'] : $def_email_include_to;
$mail_session_id = md5(uniqid(''));
$sql = "INSERT INTO ". BB_MEGAMAIL ." (
mailsession_id, group_id,
email_subject, email_body,
batch_start, batch_size, batch_wait, status, user_id,
email_format, email_include_to)
VALUES ('".$mail_session_id."',".intval($_POST[POST_GROUPS_URL])
." ,'".str_replace("\'","''",trim($_POST['subject']))
."','".str_replace("\'","''",trim($_POST['message']))
."', 0, ".$batchsize.",".$batchwait.", 0,".$userdata['user_id']
." ,".$email_format." ,".$email_include_to.")";
if ( !($result = DB()->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not insert the data into '. BB_MEGAMAIL, '', __LINE__, __FILE__, $sql);
}
$mail_id = DB()->sql_nextid();
$url= "admin_megamail.php?mail_id=".$mail_id."&mail_session_id=".$mail_session_id;
$template->assign_vars(array(
"META" => '<meta http-equiv="refresh" content="'.$batchwait.';url=' . $url . '">')
);
$message = sprintf($lang['megamail_created_message'], '<a href="' . $url . '">', '</a>', $batchwait);
message_die(GENERAL_MESSAGE, $message);
}
if ( isset($_GET['mail_id']) && isset($_GET['mail_session_id']) )
{
@ignore_user_abort(true);
$mail_id = intval($_GET['mail_id']);
$mail_session_id = stripslashes(trim($_GET['mail_session_id']));
// Let's see if that session exists
$sql = "SELECT *
FROM ". BB_MEGAMAIL ."
WHERE mail_id = $mail_id AND mailsession_id LIKE '$mail_session_id'";
if ( !($result = DB()->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not query '. BB_MEGAMAIL , '', __LINE__, __FILE__, $sql);
}
$mail_data = DB()->sql_fetchrow($result);
if ( !($mail_data) )
{
message_die(GENERAL_MESSAGE, 'Mail ID and Mail Session ID do not match.', '', __LINE__, __FILE__, $sql);
}
// Ok, the session exists
$subject = $mail_data['email_subject'];
$message = $mail_data['email_body'];
$group_id = $mail_data['group_id'];
$email_format = $mail_data['email_format'];
$email_include_to = $mail_data['email_include_to'];
//Now, let's see if we reached the upperlimit, if yes adjust the batch_size
$sql = ( $group_id != -1 ) ? "SELECT COUNT(u.user_email) FROM " . BB_USERS . " u, " . BB_USER_GROUP . " ug WHERE ug.group_id = $group_id AND ug.user_pending <> " . TRUE . " AND u.user_id = ug.user_id" : "SELECT COUNT(u.user_email) FROM " . BB_USERS . " u";
if ( !($result = DB()->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not select group members', '', __LINE__, __FILE__, $sql);
}
$totalrecipients = DB()->sql_fetchrow($result);
$totalrecipients = $totalrecipients['COUNT(u.user_email)'];
$is_done = '';
if ( ($mail_data['batch_start']+$mail_data['batch_size']) > $totalrecipients )
{
$mail_data['batch_size'] = $totalrecipients-$mail_data['batch_start'];
$is_done = ', status = 1';
}
// Create new mail session
$mail_session_id = md5(uniqid(''));
$sql = "UPDATE ". BB_MEGAMAIL. " SET
mailsession_id = '".$mail_session_id."', batch_start= ".($mail_data['batch_start']+$mail_data['batch_size'])
.$is_done." WHERE mail_id = $mail_id";
if ( !($result = DB()->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not insert the data into '. BB_MEGAMAIL, '', __LINE__, __FILE__, $sql);
}
// OK, now let's start sending
$error = FALSE;
$error_msg = '';
$sql = ( $group_id != -1 ) ? "SELECT u.user_email FROM " . BB_USERS . " u, " . BB_USER_GROUP . " ug WHERE ug.group_id = $group_id AND ug.user_pending <> " . TRUE . " AND u.user_id = ug.user_id" : "SELECT user_email FROM " . BB_USERS;
$sql .= " LIMIT ".$mail_data['batch_start'].", ".$mail_data['batch_size'];
if ( !($result = DB()->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not select group members', '', __LINE__, __FILE__, $sql);
}
if ( $row = DB()->sql_fetchrow($result) )
{
$bcc_list = '';
do
{
$bcc_list .= ( ( $bcc_list != '' ) ? ', ' : '' ) . $row['user_email'];
}
while ( $row = DB()->sql_fetchrow($result) );
DB()->sql_freeresult($result);
}
else
{
$message = ( $group_id != -1 ) ? $lang['Group_not_exist'] : $lang['No_such_user'];
$error = true;
$error_msg .= ( !empty($error_msg) ) ? '<br />' . $message : $message;
}
if ( !$error )
{
include(BB_ROOT . 'includes/emailer.class.php');
//
// Let's do some checking to make sure that mass mail functions
// are working in win32 versions of php.
//
if ( preg_match('/[c-z]:\\\.*/i', getenv('PATH')) && !$bb_cfg['smtp_delivery'])
{
$ini_val = ( @phpversion() >= '4.0.0' ) ? 'ini_get' : 'get_cfg_var';
// We are running on windows, force delivery to use our smtp functions
// since php's are broken by default
$bb_cfg['smtp_delivery'] = 1;
$bb_cfg['smtp_host'] = @$ini_val('SMTP');
}
$emailer = new emailer($bb_cfg['smtp_delivery']);
// create the emailer class
$emailer->from($bb_cfg['board_email']);
$emailer->replyto($bb_cfg['board_email']);
$email_headers = 'Return-Path: ' . $bb_cfg['board_email'] . "\n";
$email_headers .= 'X-AntiAbuse: Board servername - ' . $bb_cfg['server_name'] . "\n";
$email_headers .= 'X-AntiAbuse: User_id - ' . $userdata['user_id'] . "\n";
$email_headers .= 'X-AntiAbuse: Username - ' . $userdata['username'] . "\n";
$email_headers .= 'X-AntiAbuse: User IP - ' . decode_ip($user_ip) . "\n";
$email_headers .= "Bcc: $bcc_list\n";
// check if we should use an html template or the default (plain text) one
if ( $email_format == '1' )
{
$emailer->use_template('admin_send_html_email');
}
else
{
$emailer->use_template('admin_send_email');
}
// Check if we should include "To:" in every batch mail or only in the first batch mail
// That way we can avoid identical emails per batch
if ( $email_include_to == '1' )
{
$emailer->email_address($bb_cfg['board_email']);
}
else
{
if ( $mail_data['batch_start'] < $mail_data['batch_size'] )
{
$emailer->email_address($bb_cfg['board_email']);
}
}
$emailer->set_subject($subject);
$emailer->extra_headers($email_headers);
$emailer->assign_vars(array(
'SITENAME' => $bb_cfg['sitename'],
'BOARD_EMAIL' => $bb_cfg['board_email'],
'MESSAGE' => $message)
);
// check if we should send it as html or plain text
if ( $email_format == '1' )
{
$emailer->send('html');
}
else
{
$emailer->send('text');
}
$emailer->reset();
if ($is_done =='')
{
$url= "admin_megamail.php?mail_id=".$mail_id."&mail_session_id=".$mail_session_id;
$template->assign_vars(array(
"META" => '<meta http-equiv="refresh" content="'.$mail_data['batch_wait'].';url=' . $url . '">')
);
$message = sprintf($lang['megamail_send_message'] ,$mail_data['batch_start'], ($mail_data['batch_start']+$mail_data['batch_size']), '<a href="' . $url . '">', '</a>', $mail_data['batch_wait']);
}
else
{
$url= append_sid("admin_megamail.php");
$template->assign_vars(array(
"META" => '<meta http-equiv="refresh" content="'.$mail_data['batch_wait'].';url=' . $url . '">')
);
$message = $lang['megamail_done']. '<br />' . sprintf($lang['megamail_proceed'], '<a href="' . $url . '">', '</a>');
}
message_die(GENERAL_MESSAGE, $message);
// message_die(GENERAL_MESSAGE, $lang['Email_sent'] . '<br /><br />' . sprintf($lang['Click_return_admin_index'], '<a href="' . append_sid("index.php?pane=right") . '">', '</a>'));
}
}
if ( @$error )
{
$template->set_filenames(array(
'reg_header' => 'error_body.tpl')
);
$template->assign_vars(array(
'ERROR_MESSAGE' => $error_msg)
);
$template->assign_var_from_handle('ERROR_BOX', 'reg_header');
}
//
// Initial selection
//
$sql = "SELECT m.*, u.username, g.group_name
FROM ". BB_MEGAMAIL ." m
LEFT JOIN ". BB_USERS ." u ON (m.user_id=u.user_id)
LEFT JOIN ". BB_GROUPS ." g ON (m.group_id=g.group_id)";
if ( !($result = DB()->sql_query($sql)) )
{
message_die(GENERAL_MESSAGE, sprintf('Could not obtain list of email-sessions. If you want to create the table, click <a href=%s>here to install</a>', "admin_megamail.php?mode=install"), '', __LINE__, __FILE__, $sql);
}
$row_class=0;
if ( $mail_data = DB()->sql_fetchrow($result) )
do
{
$url= "admin_megamail.php?mail_id=".$mail_data['mail_id']."&mail_session_id=".$mail_data['mailsession_id'];
$template->assign_block_vars('mail_sessions',array(
'ROW' => ($row_class % 2) ? 'row2' : 'row1',
'ID' => $mail_data['mail_id'],
'GROUP' => ($mail_data['group_id'] != -1) ? $mail_data['group_name'] : $lang['All_users'],
'SUBJECT' => $mail_data['email_subject'],
'BATCHSTART' => $mail_data['batch_start'],
'BATCHSIZE' => $mail_data['batch_size'],
'BATCHWAIT' => $mail_data['batch_wait'].' sec',
'EMAIL_INCLUDE_TO' => ( $mail_data['email_include_to'] == '1' ) ? $lang['YES'] : $lang['NO'] ,
'EMAIL_FORMAT' => ( $mail_data['email_format'] == '1' ) ? 'HTML' : 'TEXT',
'SENDER' => $mail_data['username'],
'STATUS' => ($mail_data['status'] == 0 ) ? sprintf($lang['megamail_proceed'], '<a href="' . $url . '">', '</a>') : $lang['FINISH'],
));
}
while( $mail_data = DB()->sql_fetchrow($result) );
else
$template->assign_block_vars('switch_no_sessions',array(
'EMPTY' => $lang['megamail_none'],
));
$sql = "SELECT group_id, group_name
FROM ". BB_GROUPS . "
WHERE group_single_user <> 1";
if ( !($result = DB()->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not obtain list of groups', '', __LINE__, __FILE__, $sql);
}
$select_list = '<select name = "' . POST_GROUPS_URL . '"><option value = "-1">' . $lang['ALL_USERS'] . '</option>';
if ( $row = DB()->sql_fetchrow($result) )
{
do
{
$select_list .= '<option value = "' . $row['group_id'] . '">' . $row['group_name'] . '</option>';
}
while ( $row = DB()->sql_fetchrow($result) );
}
$select_list .= '</select>';
$template->assign_vars(array(
'MESSAGE' => $message,
'SUBJECT' => $subject,
'L_EMAIL_TITLE' => $lang['EMAIL'],
'L_EMAIL_EXPLAIN' => $lang['Megamail_Explain'],
'L_COMPOSE' => $lang['COMPOSE'],
'L_RECIPIENTS' => $lang['RECIPIENTS'],
'L_EMAIL_SUBJECT' => $lang['SUBJECT'],
'L_EMAIL_MSG' => $lang['MESSAGE'],
'L_EMAIL' => $lang['EMAIL'],
'L_NOTICE' => @$notice,
'S_USER_ACTION' => "admin_megamail.php",
'S_GROUP_SELECT' => $select_list,
'L_EMAIL_INCLUDE_TO' => $lang['megamail_email_include_to'],
'S_EMAIL_INCLUDE_TO_YES' => $email_include_to_yes,
'S_EMAIL_INCLUDE_TO_NO' => $email_include_to_no,
'L_EMAIL_FORMAT' => $lang['megamail_email_format'],
'L_EMAIL_FORMAT_HTML' => $lang['megamail_email_format_html'],
'L_EMAIL_FORMAT_TEXT' => $lang['megamail_email_format_text'],
'S_EMAIL_FORMAT_HTML' => $email_format_html,
'S_EMAIL_FORMAT_TEXT' => $email_format_text,
'L_MAIL_SESSION_HEADER' => $lang['megamail_header'],
'L_ID' => 'Id',
'L_GROUP' => $lang['group_name'],
'L_BATCH_START' => $lang['megamail_batchstart'],
'L_BATCH_SIZE' => $lang['megamail_batchsize'],
'L_BATCH_WAIT' => $lang['megamail_batchwait'],
'L_SENDER' => $lang['AUTH_ADMIN'],
'L_STATUS' => $lang['megamail_status'],
'DEFAULT_SIZE' => $def_size,
'DEFAULT_WAIT' => $def_wait,
));
print_page('megamail.tpl', 'admin');