replace constants with class constants.
[phpbb.git] / phpBB / modules / acp / acp_email.php
blobc516c77f6cc41dc4762be899f44085cc64c9063c
1 <?php
2 /**
4 * @package acp
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * @package acp
22 class acp_email
24 var $u_action;
26 function main($id, $mode)
28 global $config, $db, $user, $auth, $template, $cache;
30 $user->add_lang('acp/email');
31 $this->tpl_name = 'acp_email';
32 $this->page_title = 'ACP_MASS_EMAIL';
34 $form_key = 'acp_email';
35 add_form_key($form_key);
37 // Set some vars
38 $submit = phpbb_request::is_set_post('submit');
39 $error = array();
41 $usernames = request_var('usernames', '', true);
42 $group_id = request_var('g', 0);
43 $subject = utf8_normalize_nfc(request_var('subject', '', true));
44 $message = utf8_normalize_nfc(request_var('message', '', true));
46 // Do the job ...
47 if ($submit)
49 // Error checking needs to go here ... if no subject and/or no message then skip
50 // over the send and return to the form
51 $use_queue = phpbb_request::is_set_post('send_immediately');
52 $priority = request_var('mail_priority_flag', MAIL_NORMAL_PRIORITY);
54 if (!check_form_key($form_key))
56 $error[] = $user->lang['FORM_INVALID'];
59 if (!$subject)
61 $error[] = $user->lang['NO_EMAIL_SUBJECT'];
64 if (!$message)
66 $error[] = $user->lang['NO_EMAIL_MESSAGE'];
69 if (!sizeof($error))
71 if ($usernames)
73 // If giving usernames the admin is able to email inactive users too...
74 $sql = 'SELECT username, user_email, user_jabber, user_notify_type, user_lang
75 FROM ' . USERS_TABLE . '
76 WHERE ' . $db->sql_in_set('username_clean', array_map('utf8_clean_string', explode("\n", $usernames))) . '
77 AND user_allow_massemail = 1
78 ORDER BY user_lang, user_notify_type'; // , SUBSTRING(user_email FROM INSTR(user_email, '@'))
80 else
82 if ($group_id)
84 $sql = 'SELECT u.user_email, u.username, u.username_clean, u.user_lang, u.user_jabber, u.user_notify_type
85 FROM ' . USERS_TABLE . ' u, ' . USER_GROUP_TABLE . ' ug
86 WHERE ug.group_id = ' . $group_id . '
87 AND ug.user_pending = 0
88 AND u.user_id = ug.user_id
89 AND u.user_allow_massemail = 1
90 AND u.user_type IN (' . phpbb::USER_NORMAL . ', ' . phpbb::USER_FOUNDER . ')
91 ORDER BY u.user_lang, u.user_notify_type';
93 else
95 $sql = 'SELECT username, username_clean, user_email, user_jabber, user_notify_type, user_lang
96 FROM ' . USERS_TABLE . '
97 WHERE user_allow_massemail = 1
98 AND user_type IN (' . phpbb::USER_NORMAL . ', ' . phpbb::USER_FOUNDER . ')
99 ORDER BY user_lang, user_notify_type';
102 $result = $db->sql_query($sql);
103 $row = $db->sql_fetchrow($result);
105 if (!$row)
107 $db->sql_freeresult($result);
108 trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING);
111 $i = $j = 0;
113 // Send with BCC, no more than 50 recipients for one mail (to not exceed the limit)
114 $max_chunk_size = 50;
115 $email_list = array();
116 $old_lang = $row['user_lang'];
117 $old_notify_type = $row['user_notify_type'];
121 if (($row['user_notify_type'] == NOTIFY_EMAIL && $row['user_email']) ||
122 ($row['user_notify_type'] == NOTIFY_IM && $row['user_jabber']) ||
123 ($row['user_notify_type'] == NOTIFY_BOTH && $row['user_email'] && $row['user_jabber']))
125 if ($i == $max_chunk_size || $row['user_lang'] != $old_lang || $row['user_notify_type'] != $old_notify_type)
127 $i = 0;
129 if (sizeof($email_list))
131 $j++;
134 $old_lang = $row['user_lang'];
135 $old_notify_type = $row['user_notify_type'];
138 $email_list[$j][$i]['lang'] = $row['user_lang'];
139 $email_list[$j][$i]['method'] = $row['user_notify_type'];
140 $email_list[$j][$i]['email'] = $row['user_email'];
141 $email_list[$j][$i]['name'] = $row['username'];
142 $email_list[$j][$i]['jabber'] = $row['user_jabber'];
143 $i++;
146 while ($row = $db->sql_fetchrow($result));
147 $db->sql_freeresult($result);
149 // Send the messages
150 include_once(PHPBB_ROOT_PATH . 'includes/functions_messenger.' . PHP_EXT);
151 include_once(PHPBB_ROOT_PATH . 'includes/functions_user.' . PHP_EXT);
152 $messenger = new messenger($use_queue);
154 $errored = false;
156 for ($i = 0, $size = sizeof($email_list); $i < $size; $i++)
158 $used_lang = $email_list[$i][0]['lang'];
159 $used_method = $email_list[$i][0]['method'];
161 for ($j = 0, $list_size = sizeof($email_list[$i]); $j < $list_size; $j++)
163 $email_row = $email_list[$i][$j];
165 $messenger->{((sizeof($email_list[$i]) == 1) ? 'to' : 'bcc')}($email_row['email'], $email_row['name']);
166 $messenger->im($email_row['jabber'], $email_row['name']);
169 $messenger->template('admin_send_email', $used_lang);
171 $messenger->headers('X-AntiAbuse: Board servername - ' . $config['server_name']);
172 $messenger->headers('X-AntiAbuse: User_id - ' . $user->data['user_id']);
173 $messenger->headers('X-AntiAbuse: Username - ' . $user->data['username']);
174 $messenger->headers('X-AntiAbuse: User IP - ' . $user->ip);
176 $messenger->subject(htmlspecialchars_decode($subject));
177 $messenger->set_mail_priority($priority);
179 $messenger->assign_vars(array(
180 'CONTACT_EMAIL' => $config['board_contact'],
181 'MESSAGE' => htmlspecialchars_decode($message))
184 if (!($messenger->send($used_method)))
186 $errored = true;
189 unset($email_list);
191 $messenger->save_queue();
193 if ($usernames)
195 $usernames = explode("\n", $usernames);
196 add_log('admin', 'LOG_MASS_EMAIL', implode(', ', utf8_normalize_nfc($usernames)));
198 else
200 if ($group_id)
202 $group_name = get_group_name($group_id);
204 else
206 // Not great but the logging routine doesn't cope well with localising on the fly
207 $group_name = $user->lang['ALL_USERS'];
210 add_log('admin', 'LOG_MASS_EMAIL', $group_name);
213 if (!$errored)
215 $message = ($use_queue) ? $user->lang['EMAIL_SENT_QUEUE'] : $user->lang['EMAIL_SENT'];
216 trigger_error($message . adm_back_link($this->u_action));
218 else
220 $message = sprintf($user->lang['EMAIL_SEND_ERROR'], '<a href="' . append_sid(PHPBB_ADMIN_PATH . 'index.' . PHP_EXT, 'i=logs&amp;mode=critical') . '">', '</a>');
221 trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING);
226 // Exclude bots and guests...
227 $sql = 'SELECT group_id
228 FROM ' . GROUPS_TABLE . "
229 WHERE group_name_clean IN ('bots', 'guests')";
230 $result = $db->sql_query($sql);
232 $exclude = array();
233 while ($row = $db->sql_fetchrow($result))
235 $exclude[] = $row['group_id'];
237 $db->sql_freeresult($result);
239 $select_list = '<option value="0"' . ((!$group_id) ? ' selected="selected"' : '') . '>' . $user->lang['ALL_USERS'] . '</option>';
240 $select_list .= group_select_options($group_id, $exclude);
242 $s_priority_options = '<option value="' . MAIL_LOW_PRIORITY . '">' . $user->lang['MAIL_LOW_PRIORITY'] . '</option>';
243 $s_priority_options .= '<option value="' . MAIL_NORMAL_PRIORITY . '" selected="selected">' . $user->lang['MAIL_NORMAL_PRIORITY'] . '</option>';
244 $s_priority_options .= '<option value="' . MAIL_HIGH_PRIORITY . '">' . $user->lang['MAIL_HIGH_PRIORITY'] . '</option>';
246 $template->assign_vars(array(
247 'S_WARNING' => (sizeof($error)) ? true : false,
248 'WARNING_MSG' => (sizeof($error)) ? implode('<br />', $error) : '',
249 'U_ACTION' => $this->u_action,
250 'S_GROUP_OPTIONS' => $select_list,
251 'USERNAMES' => $usernames,
252 'U_FIND_USERNAME' => append_sid('memberlist', 'mode=searchuser&amp;form=acp_email&amp;field=usernames'),
253 'SUBJECT' => $subject,
254 'MESSAGE' => $message,
255 'S_PRIORITY_OPTIONS' => $s_priority_options,