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