includes/acp/acp_email.php
changeset 0 ceef6d64c21d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/includes/acp/acp_email.php	Wed Mar 07 16:41:10 2012 +0400
     1.3 @@ -0,0 +1,285 @@
     1.4 +<?php
     1.5 +/**
     1.6 +*
     1.7 +* @package acp
     1.8 +* @version $Id$
     1.9 +* @copyright (c) 2005 phpBB Group
    1.10 +* @license http://opensource.org/licenses/gpl-license.php GNU Public License
    1.11 +*
    1.12 +*/
    1.13 +
    1.14 +/**
    1.15 +* @ignore
    1.16 +*/
    1.17 +if (!defined('IN_PHPBB'))
    1.18 +{
    1.19 +	exit;
    1.20 +}
    1.21 +
    1.22 +/**
    1.23 +* @package acp
    1.24 +*/
    1.25 +class acp_email
    1.26 +{
    1.27 +	var $u_action;
    1.28 +
    1.29 +	function main($id, $mode)
    1.30 +	{
    1.31 +		global $config, $db, $user, $auth, $template, $cache;
    1.32 +		global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix;
    1.33 +
    1.34 +		$user->add_lang('acp/email');
    1.35 +		$this->tpl_name = 'acp_email';
    1.36 +		$this->page_title = 'ACP_MASS_EMAIL';
    1.37 +
    1.38 +		$form_key = 'acp_email';
    1.39 +		add_form_key($form_key);
    1.40 +
    1.41 +		// Set some vars
    1.42 +		$submit = (isset($_POST['submit'])) ? true : false;
    1.43 +		$error = array();
    1.44 +
    1.45 +		$usernames	= request_var('usernames', '', true);
    1.46 +		$group_id	= request_var('g', 0);
    1.47 +		$subject	= utf8_normalize_nfc(request_var('subject', '', true));
    1.48 +		$message	= utf8_normalize_nfc(request_var('message', '', true));
    1.49 +
    1.50 +		// Do the job ...
    1.51 +		if ($submit)
    1.52 +		{
    1.53 +			// Error checking needs to go here ... if no subject and/or no message then skip
    1.54 +			// over the send and return to the form
    1.55 +			$use_queue		= (isset($_POST['send_immediately'])) ? false : true;
    1.56 +			$priority		= request_var('mail_priority_flag', MAIL_NORMAL_PRIORITY);
    1.57 +
    1.58 +			if (!check_form_key($form_key))
    1.59 +			{
    1.60 +				$error[] = $user->lang['FORM_INVALID'];
    1.61 +			}
    1.62 +
    1.63 +			if (!$subject)
    1.64 +			{
    1.65 +				$error[] = $user->lang['NO_EMAIL_SUBJECT'];
    1.66 +			}
    1.67 +
    1.68 +			if (!$message)
    1.69 +			{
    1.70 +				$error[] = $user->lang['NO_EMAIL_MESSAGE'];
    1.71 +			}
    1.72 +
    1.73 +			if (!sizeof($error))
    1.74 +			{
    1.75 +				if ($usernames)
    1.76 +				{
    1.77 +					// If giving usernames the admin is able to email inactive users too...
    1.78 +					$sql = 'SELECT username, user_email, user_jabber, user_notify_type, user_lang
    1.79 +						FROM ' . USERS_TABLE . '
    1.80 +						WHERE ' . $db->sql_in_set('username_clean', array_map('utf8_clean_string', explode("\n", $usernames))) . '
    1.81 +							AND user_allow_massemail = 1
    1.82 +						ORDER BY user_lang, user_notify_type'; // , SUBSTRING(user_email FROM INSTR(user_email, '@'))
    1.83 +				}
    1.84 +				else
    1.85 +				{
    1.86 +					if ($group_id)
    1.87 +					{
    1.88 +						$sql_ary = array(
    1.89 +							'SELECT'	=> 'u.user_email, u.username, u.username_clean, u.user_lang, u.user_jabber, u.user_notify_type',
    1.90 +							'FROM'		=> array(
    1.91 +								USERS_TABLE			=> 'u',
    1.92 +								USER_GROUP_TABLE	=> 'ug',
    1.93 +							),
    1.94 +							'WHERE'		=> 'ug.group_id = ' . $group_id . '
    1.95 +								AND ug.user_pending = 0
    1.96 +								AND u.user_id = ug.user_id
    1.97 +								AND u.user_allow_massemail = 1
    1.98 +								AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')',
    1.99 +							'ORDER_BY'	=> 'u.user_lang, u.user_notify_type',
   1.100 +						);
   1.101 +					}
   1.102 +					else
   1.103 +					{
   1.104 +						$sql_ary = array(
   1.105 +							'SELECT'	=> 'u.username, u.username_clean, u.user_email, u.user_jabber, u.user_lang, u.user_notify_type',
   1.106 +							'FROM'		=> array(
   1.107 +								USERS_TABLE	=> 'u',
   1.108 +							),
   1.109 +							'WHERE'		=> 'u.user_allow_massemail = 1
   1.110 +								AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')',
   1.111 +							'ORDER_BY'	=> 'u.user_lang, u.user_notify_type',
   1.112 +						);
   1.113 +					}
   1.114 +
   1.115 +					// Mail banned or not
   1.116 +					if (!isset($_REQUEST['mail_banned_flag']))
   1.117 +					{
   1.118 +						$sql_ary['WHERE'] .= ' AND (b.ban_id IS NULL
   1.119 +						        OR b.ban_exclude = 1)';
   1.120 +						$sql_ary['LEFT_JOIN'] = array(
   1.121 +							array(
   1.122 +								'FROM'	=> array(
   1.123 +									BANLIST_TABLE	=> 'b',
   1.124 +								),
   1.125 +								'ON'	=> 'u.user_id = b.ban_userid',
   1.126 +							),
   1.127 +						);
   1.128 +					}
   1.129 +					$sql = $db->sql_build_query('SELECT', $sql_ary);
   1.130 +				}
   1.131 +				$result = $db->sql_query($sql);
   1.132 +				$row = $db->sql_fetchrow($result);
   1.133 +
   1.134 +				if (!$row)
   1.135 +				{
   1.136 +					$db->sql_freeresult($result);
   1.137 +					trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
   1.138 +				}
   1.139 +
   1.140 +				$i = $j = 0;
   1.141 +
   1.142 +				// Send with BCC
   1.143 +				// Maximum number of bcc recipients
   1.144 +				$max_chunk_size = (int) $config['email_max_chunk_size'];
   1.145 +				$email_list = array();
   1.146 +				$old_lang = $row['user_lang'];
   1.147 +				$old_notify_type = $row['user_notify_type'];
   1.148 +
   1.149 +				do
   1.150 +				{
   1.151 +					if (($row['user_notify_type'] == NOTIFY_EMAIL && $row['user_email']) ||
   1.152 +						($row['user_notify_type'] == NOTIFY_IM && $row['user_jabber']) ||
   1.153 +						($row['user_notify_type'] == NOTIFY_BOTH && ($row['user_email'] || $row['user_jabber'])))
   1.154 +					{
   1.155 +						if ($i == $max_chunk_size || $row['user_lang'] != $old_lang || $row['user_notify_type'] != $old_notify_type)
   1.156 +						{
   1.157 +							$i = 0;
   1.158 +
   1.159 +							if (sizeof($email_list))
   1.160 +							{
   1.161 +								$j++;
   1.162 +							}
   1.163 +
   1.164 +							$old_lang = $row['user_lang'];
   1.165 +							$old_notify_type = $row['user_notify_type'];
   1.166 +						}
   1.167 +
   1.168 +						$email_list[$j][$i]['lang']		= $row['user_lang'];
   1.169 +						$email_list[$j][$i]['method']	= $row['user_notify_type'];
   1.170 +						$email_list[$j][$i]['email']	= $row['user_email'];
   1.171 +						$email_list[$j][$i]['name']		= $row['username'];
   1.172 +						$email_list[$j][$i]['jabber']	= $row['user_jabber'];
   1.173 +						$i++;
   1.174 +					}
   1.175 +				}
   1.176 +				while ($row = $db->sql_fetchrow($result));
   1.177 +				$db->sql_freeresult($result);
   1.178 +
   1.179 +				// Send the messages
   1.180 +				include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
   1.181 +				include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
   1.182 +				$messenger = new messenger($use_queue);
   1.183 +
   1.184 +				$errored = false;
   1.185 +
   1.186 +				for ($i = 0, $size = sizeof($email_list); $i < $size; $i++)
   1.187 +				{
   1.188 +					$used_lang = $email_list[$i][0]['lang'];
   1.189 +					$used_method = $email_list[$i][0]['method'];
   1.190 +
   1.191 +					for ($j = 0, $list_size = sizeof($email_list[$i]); $j < $list_size; $j++)
   1.192 +					{
   1.193 +						$email_row = $email_list[$i][$j];
   1.194 +
   1.195 +						$messenger->{((sizeof($email_list[$i]) == 1) ? 'to' : 'bcc')}($email_row['email'], $email_row['name']);
   1.196 +						$messenger->im($email_row['jabber'], $email_row['name']);
   1.197 +					}
   1.198 +
   1.199 +					$messenger->template('admin_send_email', $used_lang);
   1.200 +
   1.201 +					$messenger->anti_abuse_headers($config, $user);
   1.202 +
   1.203 +					$messenger->subject(htmlspecialchars_decode($subject));
   1.204 +					$messenger->set_mail_priority($priority);
   1.205 +
   1.206 +					$messenger->assign_vars(array(
   1.207 +						'CONTACT_EMAIL' => $config['board_contact'],
   1.208 +						'MESSAGE'		=> htmlspecialchars_decode($message))
   1.209 +					);
   1.210 +
   1.211 +					if (!($messenger->send($used_method)))
   1.212 +					{
   1.213 +						$errored = true;
   1.214 +					}
   1.215 +				}
   1.216 +				unset($email_list);
   1.217 +
   1.218 +				$messenger->save_queue();
   1.219 +
   1.220 +				if ($usernames)
   1.221 +				{
   1.222 +					$usernames = explode("\n", $usernames);
   1.223 +					add_log('admin', 'LOG_MASS_EMAIL', implode(', ', utf8_normalize_nfc($usernames)));
   1.224 +				}
   1.225 +				else
   1.226 +				{
   1.227 +					if ($group_id)
   1.228 +					{
   1.229 +						$group_name = get_group_name($group_id);
   1.230 +					}
   1.231 +					else
   1.232 +					{
   1.233 +						// Not great but the logging routine doesn't cope well with localising on the fly
   1.234 +						$group_name = $user->lang['ALL_USERS'];
   1.235 +					}
   1.236 +
   1.237 +					add_log('admin', 'LOG_MASS_EMAIL', $group_name);
   1.238 +				}
   1.239 +
   1.240 +				if (!$errored)
   1.241 +				{
   1.242 +					$message = ($use_queue) ? $user->lang['EMAIL_SENT_QUEUE'] : $user->lang['EMAIL_SENT'];
   1.243 +					trigger_error($message . adm_back_link($this->u_action));
   1.244 +				}
   1.245 +				else
   1.246 +				{
   1.247 +					$message = sprintf($user->lang['EMAIL_SEND_ERROR'], '<a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=logs&amp;mode=critical') . '">', '</a>');
   1.248 +					trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING);
   1.249 +				}
   1.250 +			}
   1.251 +		}
   1.252 +
   1.253 +		// Exclude bots and guests...
   1.254 +		$sql = 'SELECT group_id
   1.255 +			FROM ' . GROUPS_TABLE . "
   1.256 +			WHERE group_name IN ('BOTS', 'GUESTS')";
   1.257 +		$result = $db->sql_query($sql);
   1.258 +
   1.259 +		$exclude = array();
   1.260 +		while ($row = $db->sql_fetchrow($result))
   1.261 +		{
   1.262 +			$exclude[] = $row['group_id'];
   1.263 +		}
   1.264 +		$db->sql_freeresult($result);
   1.265 +
   1.266 +		$select_list = '<option value="0"' . ((!$group_id) ? ' selected="selected"' : '') . '>' . $user->lang['ALL_USERS'] . '</option>';
   1.267 +		$select_list .= group_select_options($group_id, $exclude);
   1.268 +
   1.269 +		$s_priority_options = '<option value="' . MAIL_LOW_PRIORITY . '">' . $user->lang['MAIL_LOW_PRIORITY'] . '</option>';
   1.270 +		$s_priority_options .= '<option value="' . MAIL_NORMAL_PRIORITY . '" selected="selected">' . $user->lang['MAIL_NORMAL_PRIORITY'] . '</option>';
   1.271 +		$s_priority_options .= '<option value="' . MAIL_HIGH_PRIORITY . '">' . $user->lang['MAIL_HIGH_PRIORITY'] . '</option>';
   1.272 +
   1.273 +		$template->assign_vars(array(
   1.274 +			'S_WARNING'				=> (sizeof($error)) ? true : false,
   1.275 +			'WARNING_MSG'			=> (sizeof($error)) ? implode('<br />', $error) : '',
   1.276 +			'U_ACTION'				=> $this->u_action,
   1.277 +			'S_GROUP_OPTIONS'		=> $select_list,
   1.278 +			'USERNAMES'				=> $usernames,
   1.279 +			'U_FIND_USERNAME'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=acp_email&amp;field=usernames'),
   1.280 +			'SUBJECT'				=> $subject,
   1.281 +			'MESSAGE'				=> $message,
   1.282 +			'S_PRIORITY_OPTIONS'	=> $s_priority_options)
   1.283 +		);
   1.284 +
   1.285 +	}
   1.286 +}
   1.287 +
   1.288 +?>
   1.289 \ No newline at end of file