includes/acp/acp_email.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Fri, 09 Mar 2012 16:11:37 +0400
changeset 12 898199d067e7
parent 0 ceef6d64c21d
permissions -rw-r--r--
Ignoring cache
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package acp
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* @package acp
indvdum@0
    21
*/
indvdum@0
    22
class acp_email
indvdum@0
    23
{
indvdum@0
    24
	var $u_action;
indvdum@0
    25
indvdum@0
    26
	function main($id, $mode)
indvdum@0
    27
	{
indvdum@0
    28
		global $config, $db, $user, $auth, $template, $cache;
indvdum@0
    29
		global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix;
indvdum@0
    30
indvdum@0
    31
		$user->add_lang('acp/email');
indvdum@0
    32
		$this->tpl_name = 'acp_email';
indvdum@0
    33
		$this->page_title = 'ACP_MASS_EMAIL';
indvdum@0
    34
indvdum@0
    35
		$form_key = 'acp_email';
indvdum@0
    36
		add_form_key($form_key);
indvdum@0
    37
indvdum@0
    38
		// Set some vars
indvdum@0
    39
		$submit = (isset($_POST['submit'])) ? true : false;
indvdum@0
    40
		$error = array();
indvdum@0
    41
indvdum@0
    42
		$usernames	= request_var('usernames', '', true);
indvdum@0
    43
		$group_id	= request_var('g', 0);
indvdum@0
    44
		$subject	= utf8_normalize_nfc(request_var('subject', '', true));
indvdum@0
    45
		$message	= utf8_normalize_nfc(request_var('message', '', true));
indvdum@0
    46
indvdum@0
    47
		// Do the job ...
indvdum@0
    48
		if ($submit)
indvdum@0
    49
		{
indvdum@0
    50
			// Error checking needs to go here ... if no subject and/or no message then skip
indvdum@0
    51
			// over the send and return to the form
indvdum@0
    52
			$use_queue		= (isset($_POST['send_immediately'])) ? false : true;
indvdum@0
    53
			$priority		= request_var('mail_priority_flag', MAIL_NORMAL_PRIORITY);
indvdum@0
    54
indvdum@0
    55
			if (!check_form_key($form_key))
indvdum@0
    56
			{
indvdum@0
    57
				$error[] = $user->lang['FORM_INVALID'];
indvdum@0
    58
			}
indvdum@0
    59
indvdum@0
    60
			if (!$subject)
indvdum@0
    61
			{
indvdum@0
    62
				$error[] = $user->lang['NO_EMAIL_SUBJECT'];
indvdum@0
    63
			}
indvdum@0
    64
indvdum@0
    65
			if (!$message)
indvdum@0
    66
			{
indvdum@0
    67
				$error[] = $user->lang['NO_EMAIL_MESSAGE'];
indvdum@0
    68
			}
indvdum@0
    69
indvdum@0
    70
			if (!sizeof($error))
indvdum@0
    71
			{
indvdum@0
    72
				if ($usernames)
indvdum@0
    73
				{
indvdum@0
    74
					// If giving usernames the admin is able to email inactive users too...
indvdum@0
    75
					$sql = 'SELECT username, user_email, user_jabber, user_notify_type, user_lang
indvdum@0
    76
						FROM ' . USERS_TABLE . '
indvdum@0
    77
						WHERE ' . $db->sql_in_set('username_clean', array_map('utf8_clean_string', explode("\n", $usernames))) . '
indvdum@0
    78
							AND user_allow_massemail = 1
indvdum@0
    79
						ORDER BY user_lang, user_notify_type'; // , SUBSTRING(user_email FROM INSTR(user_email, '@'))
indvdum@0
    80
				}
indvdum@0
    81
				else
indvdum@0
    82
				{
indvdum@0
    83
					if ($group_id)
indvdum@0
    84
					{
indvdum@0
    85
						$sql_ary = array(
indvdum@0
    86
							'SELECT'	=> 'u.user_email, u.username, u.username_clean, u.user_lang, u.user_jabber, u.user_notify_type',
indvdum@0
    87
							'FROM'		=> array(
indvdum@0
    88
								USERS_TABLE			=> 'u',
indvdum@0
    89
								USER_GROUP_TABLE	=> 'ug',
indvdum@0
    90
							),
indvdum@0
    91
							'WHERE'		=> 'ug.group_id = ' . $group_id . '
indvdum@0
    92
								AND ug.user_pending = 0
indvdum@0
    93
								AND u.user_id = ug.user_id
indvdum@0
    94
								AND u.user_allow_massemail = 1
indvdum@0
    95
								AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')',
indvdum@0
    96
							'ORDER_BY'	=> 'u.user_lang, u.user_notify_type',
indvdum@0
    97
						);
indvdum@0
    98
					}
indvdum@0
    99
					else
indvdum@0
   100
					{
indvdum@0
   101
						$sql_ary = array(
indvdum@0
   102
							'SELECT'	=> 'u.username, u.username_clean, u.user_email, u.user_jabber, u.user_lang, u.user_notify_type',
indvdum@0
   103
							'FROM'		=> array(
indvdum@0
   104
								USERS_TABLE	=> 'u',
indvdum@0
   105
							),
indvdum@0
   106
							'WHERE'		=> 'u.user_allow_massemail = 1
indvdum@0
   107
								AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')',
indvdum@0
   108
							'ORDER_BY'	=> 'u.user_lang, u.user_notify_type',
indvdum@0
   109
						);
indvdum@0
   110
					}
indvdum@0
   111
indvdum@0
   112
					// Mail banned or not
indvdum@0
   113
					if (!isset($_REQUEST['mail_banned_flag']))
indvdum@0
   114
					{
indvdum@0
   115
						$sql_ary['WHERE'] .= ' AND (b.ban_id IS NULL
indvdum@0
   116
						        OR b.ban_exclude = 1)';
indvdum@0
   117
						$sql_ary['LEFT_JOIN'] = array(
indvdum@0
   118
							array(
indvdum@0
   119
								'FROM'	=> array(
indvdum@0
   120
									BANLIST_TABLE	=> 'b',
indvdum@0
   121
								),
indvdum@0
   122
								'ON'	=> 'u.user_id = b.ban_userid',
indvdum@0
   123
							),
indvdum@0
   124
						);
indvdum@0
   125
					}
indvdum@0
   126
					$sql = $db->sql_build_query('SELECT', $sql_ary);
indvdum@0
   127
				}
indvdum@0
   128
				$result = $db->sql_query($sql);
indvdum@0
   129
				$row = $db->sql_fetchrow($result);
indvdum@0
   130
indvdum@0
   131
				if (!$row)
indvdum@0
   132
				{
indvdum@0
   133
					$db->sql_freeresult($result);
indvdum@0
   134
					trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
   135
				}
indvdum@0
   136
indvdum@0
   137
				$i = $j = 0;
indvdum@0
   138
indvdum@0
   139
				// Send with BCC
indvdum@0
   140
				// Maximum number of bcc recipients
indvdum@0
   141
				$max_chunk_size = (int) $config['email_max_chunk_size'];
indvdum@0
   142
				$email_list = array();
indvdum@0
   143
				$old_lang = $row['user_lang'];
indvdum@0
   144
				$old_notify_type = $row['user_notify_type'];
indvdum@0
   145
indvdum@0
   146
				do
indvdum@0
   147
				{
indvdum@0
   148
					if (($row['user_notify_type'] == NOTIFY_EMAIL && $row['user_email']) ||
indvdum@0
   149
						($row['user_notify_type'] == NOTIFY_IM && $row['user_jabber']) ||
indvdum@0
   150
						($row['user_notify_type'] == NOTIFY_BOTH && ($row['user_email'] || $row['user_jabber'])))
indvdum@0
   151
					{
indvdum@0
   152
						if ($i == $max_chunk_size || $row['user_lang'] != $old_lang || $row['user_notify_type'] != $old_notify_type)
indvdum@0
   153
						{
indvdum@0
   154
							$i = 0;
indvdum@0
   155
indvdum@0
   156
							if (sizeof($email_list))
indvdum@0
   157
							{
indvdum@0
   158
								$j++;
indvdum@0
   159
							}
indvdum@0
   160
indvdum@0
   161
							$old_lang = $row['user_lang'];
indvdum@0
   162
							$old_notify_type = $row['user_notify_type'];
indvdum@0
   163
						}
indvdum@0
   164
indvdum@0
   165
						$email_list[$j][$i]['lang']		= $row['user_lang'];
indvdum@0
   166
						$email_list[$j][$i]['method']	= $row['user_notify_type'];
indvdum@0
   167
						$email_list[$j][$i]['email']	= $row['user_email'];
indvdum@0
   168
						$email_list[$j][$i]['name']		= $row['username'];
indvdum@0
   169
						$email_list[$j][$i]['jabber']	= $row['user_jabber'];
indvdum@0
   170
						$i++;
indvdum@0
   171
					}
indvdum@0
   172
				}
indvdum@0
   173
				while ($row = $db->sql_fetchrow($result));
indvdum@0
   174
				$db->sql_freeresult($result);
indvdum@0
   175
indvdum@0
   176
				// Send the messages
indvdum@0
   177
				include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
indvdum@0
   178
				include_once($phpbb_root_path . 'includes/functions_user.' . $phpEx);
indvdum@0
   179
				$messenger = new messenger($use_queue);
indvdum@0
   180
indvdum@0
   181
				$errored = false;
indvdum@0
   182
indvdum@0
   183
				for ($i = 0, $size = sizeof($email_list); $i < $size; $i++)
indvdum@0
   184
				{
indvdum@0
   185
					$used_lang = $email_list[$i][0]['lang'];
indvdum@0
   186
					$used_method = $email_list[$i][0]['method'];
indvdum@0
   187
indvdum@0
   188
					for ($j = 0, $list_size = sizeof($email_list[$i]); $j < $list_size; $j++)
indvdum@0
   189
					{
indvdum@0
   190
						$email_row = $email_list[$i][$j];
indvdum@0
   191
indvdum@0
   192
						$messenger->{((sizeof($email_list[$i]) == 1) ? 'to' : 'bcc')}($email_row['email'], $email_row['name']);
indvdum@0
   193
						$messenger->im($email_row['jabber'], $email_row['name']);
indvdum@0
   194
					}
indvdum@0
   195
indvdum@0
   196
					$messenger->template('admin_send_email', $used_lang);
indvdum@0
   197
indvdum@0
   198
					$messenger->anti_abuse_headers($config, $user);
indvdum@0
   199
indvdum@0
   200
					$messenger->subject(htmlspecialchars_decode($subject));
indvdum@0
   201
					$messenger->set_mail_priority($priority);
indvdum@0
   202
indvdum@0
   203
					$messenger->assign_vars(array(
indvdum@0
   204
						'CONTACT_EMAIL' => $config['board_contact'],
indvdum@0
   205
						'MESSAGE'		=> htmlspecialchars_decode($message))
indvdum@0
   206
					);
indvdum@0
   207
indvdum@0
   208
					if (!($messenger->send($used_method)))
indvdum@0
   209
					{
indvdum@0
   210
						$errored = true;
indvdum@0
   211
					}
indvdum@0
   212
				}
indvdum@0
   213
				unset($email_list);
indvdum@0
   214
indvdum@0
   215
				$messenger->save_queue();
indvdum@0
   216
indvdum@0
   217
				if ($usernames)
indvdum@0
   218
				{
indvdum@0
   219
					$usernames = explode("\n", $usernames);
indvdum@0
   220
					add_log('admin', 'LOG_MASS_EMAIL', implode(', ', utf8_normalize_nfc($usernames)));
indvdum@0
   221
				}
indvdum@0
   222
				else
indvdum@0
   223
				{
indvdum@0
   224
					if ($group_id)
indvdum@0
   225
					{
indvdum@0
   226
						$group_name = get_group_name($group_id);
indvdum@0
   227
					}
indvdum@0
   228
					else
indvdum@0
   229
					{
indvdum@0
   230
						// Not great but the logging routine doesn't cope well with localising on the fly
indvdum@0
   231
						$group_name = $user->lang['ALL_USERS'];
indvdum@0
   232
					}
indvdum@0
   233
indvdum@0
   234
					add_log('admin', 'LOG_MASS_EMAIL', $group_name);
indvdum@0
   235
				}
indvdum@0
   236
indvdum@0
   237
				if (!$errored)
indvdum@0
   238
				{
indvdum@0
   239
					$message = ($use_queue) ? $user->lang['EMAIL_SENT_QUEUE'] : $user->lang['EMAIL_SENT'];
indvdum@0
   240
					trigger_error($message . adm_back_link($this->u_action));
indvdum@0
   241
				}
indvdum@0
   242
				else
indvdum@0
   243
				{
indvdum@0
   244
					$message = sprintf($user->lang['EMAIL_SEND_ERROR'], '<a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=logs&amp;mode=critical') . '">', '</a>');
indvdum@0
   245
					trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING);
indvdum@0
   246
				}
indvdum@0
   247
			}
indvdum@0
   248
		}
indvdum@0
   249
indvdum@0
   250
		// Exclude bots and guests...
indvdum@0
   251
		$sql = 'SELECT group_id
indvdum@0
   252
			FROM ' . GROUPS_TABLE . "
indvdum@0
   253
			WHERE group_name IN ('BOTS', 'GUESTS')";
indvdum@0
   254
		$result = $db->sql_query($sql);
indvdum@0
   255
indvdum@0
   256
		$exclude = array();
indvdum@0
   257
		while ($row = $db->sql_fetchrow($result))
indvdum@0
   258
		{
indvdum@0
   259
			$exclude[] = $row['group_id'];
indvdum@0
   260
		}
indvdum@0
   261
		$db->sql_freeresult($result);
indvdum@0
   262
indvdum@0
   263
		$select_list = '<option value="0"' . ((!$group_id) ? ' selected="selected"' : '') . '>' . $user->lang['ALL_USERS'] . '</option>';
indvdum@0
   264
		$select_list .= group_select_options($group_id, $exclude);
indvdum@0
   265
indvdum@0
   266
		$s_priority_options = '<option value="' . MAIL_LOW_PRIORITY . '">' . $user->lang['MAIL_LOW_PRIORITY'] . '</option>';
indvdum@0
   267
		$s_priority_options .= '<option value="' . MAIL_NORMAL_PRIORITY . '" selected="selected">' . $user->lang['MAIL_NORMAL_PRIORITY'] . '</option>';
indvdum@0
   268
		$s_priority_options .= '<option value="' . MAIL_HIGH_PRIORITY . '">' . $user->lang['MAIL_HIGH_PRIORITY'] . '</option>';
indvdum@0
   269
indvdum@0
   270
		$template->assign_vars(array(
indvdum@0
   271
			'S_WARNING'				=> (sizeof($error)) ? true : false,
indvdum@0
   272
			'WARNING_MSG'			=> (sizeof($error)) ? implode('<br />', $error) : '',
indvdum@0
   273
			'U_ACTION'				=> $this->u_action,
indvdum@0
   274
			'S_GROUP_OPTIONS'		=> $select_list,
indvdum@0
   275
			'USERNAMES'				=> $usernames,
indvdum@0
   276
			'U_FIND_USERNAME'		=> append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=acp_email&amp;field=usernames'),
indvdum@0
   277
			'SUBJECT'				=> $subject,
indvdum@0
   278
			'MESSAGE'				=> $message,
indvdum@0
   279
			'S_PRIORITY_OPTIONS'	=> $s_priority_options)
indvdum@0
   280
		);
indvdum@0
   281
indvdum@0
   282
	}
indvdum@0
   283
}
indvdum@0
   284
indvdum@0
   285
?>