Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 177 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
acp_email | |
0.00% |
0 / 175 |
|
0.00% |
0 / 1 |
2256 | |
0.00% |
0 / 1 |
main | |
0.00% |
0 / 175 |
|
0.00% |
0 / 1 |
2256 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | use phpbb\messenger\method\messenger_interface; |
15 | |
16 | /** |
17 | * @ignore |
18 | */ |
19 | if (!defined('IN_PHPBB')) |
20 | { |
21 | exit; |
22 | } |
23 | |
24 | class acp_email |
25 | { |
26 | var $u_action; |
27 | |
28 | function main($id, $mode) |
29 | { |
30 | global $config, $db, $user, $template, $phpbb_log, $request; |
31 | global $phpbb_root_path, $phpbb_admin_path, $phpEx, $phpbb_dispatcher, $phpbb_container; |
32 | |
33 | $user->add_lang('acp/email'); |
34 | $this->tpl_name = 'acp_email'; |
35 | $this->page_title = 'ACP_MASS_EMAIL'; |
36 | |
37 | $form_key = 'acp_email'; |
38 | add_form_key($form_key); |
39 | |
40 | // Set some vars |
41 | $submit = (isset($_POST['submit'])) ? true : false; |
42 | $error = array(); |
43 | |
44 | $usernames = $request->variable('usernames', '', true); |
45 | $usernames = (!empty($usernames)) ? explode("\n", $usernames) : array(); |
46 | $group_id = $request->variable('g', 0); |
47 | $subject = $request->variable('subject', '', true); |
48 | $message = $request->variable('message', '', true); |
49 | |
50 | // Do the job ... |
51 | if ($submit) |
52 | { |
53 | // Error checking needs to go here ... if no subject and/or no message then skip |
54 | // over the send and return to the form |
55 | $use_queue = (isset($_POST['send_immediately'])) ? false : true; |
56 | $priority = $request->variable('mail_priority_flag', MAIL_NORMAL_PRIORITY); |
57 | |
58 | if (!check_form_key($form_key)) |
59 | { |
60 | $error[] = $user->lang['FORM_INVALID']; |
61 | } |
62 | |
63 | if (!$subject) |
64 | { |
65 | $error[] = $user->lang['NO_EMAIL_SUBJECT']; |
66 | } |
67 | |
68 | if (!$message) |
69 | { |
70 | $error[] = $user->lang['NO_EMAIL_MESSAGE']; |
71 | } |
72 | |
73 | if (!count($error)) |
74 | { |
75 | if (!empty($usernames)) |
76 | { |
77 | // If giving usernames the admin is able to email inactive users too... |
78 | $sql_ary = array( |
79 | 'SELECT' => 'user_id, username, user_email, user_jabber, user_notify_type, user_lang', |
80 | 'FROM' => array( |
81 | USERS_TABLE => '', |
82 | ), |
83 | 'WHERE' => $db->sql_in_set('username_clean', array_map('utf8_clean_string', $usernames)) . ' |
84 | AND user_allow_massemail = 1', |
85 | 'ORDER_BY' => 'user_lang, user_notify_type', |
86 | ); |
87 | } |
88 | else |
89 | { |
90 | if ($group_id) |
91 | { |
92 | $sql_ary = array( |
93 | 'SELECT' => 'u.user_id, u.user_email, u.username, u.username_clean, u.user_lang, u.user_jabber, u.user_notify_type', |
94 | 'FROM' => array( |
95 | USERS_TABLE => 'u', |
96 | USER_GROUP_TABLE => 'ug', |
97 | ), |
98 | 'WHERE' => 'ug.group_id = ' . $group_id . ' |
99 | AND ug.user_pending = 0 |
100 | AND u.user_id = ug.user_id |
101 | AND u.user_allow_massemail = 1 |
102 | AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')', |
103 | 'ORDER_BY' => 'u.user_lang, u.user_notify_type', |
104 | ); |
105 | } |
106 | else |
107 | { |
108 | $sql_ary = array( |
109 | 'SELECT' => 'u.user_id, u.username, u.username_clean, u.user_email, u.user_jabber, u.user_lang, u.user_notify_type', |
110 | 'FROM' => array( |
111 | USERS_TABLE => 'u', |
112 | ), |
113 | 'WHERE' => 'u.user_allow_massemail = 1 |
114 | AND u.user_type IN (' . USER_NORMAL . ', ' . USER_FOUNDER . ')', |
115 | 'ORDER_BY' => 'u.user_lang, u.user_notify_type', |
116 | ); |
117 | } |
118 | } |
119 | /** |
120 | * Modify sql query to change the list of users the email is sent to |
121 | * |
122 | * @event core.acp_email_modify_sql |
123 | * @var array sql_ary Array which is used to build the sql query |
124 | * @since 3.1.2-RC1 |
125 | */ |
126 | $vars = array('sql_ary'); |
127 | extract($phpbb_dispatcher->trigger_event('core.acp_email_modify_sql', compact($vars))); |
128 | |
129 | $sql = $db->sql_build_query('SELECT', $sql_ary); |
130 | $result = $db->sql_query($sql); |
131 | $rows = $db->sql_fetchrowset($result); |
132 | $db->sql_freeresult($result); |
133 | |
134 | if (!empty($rows) && !$request->is_set('mail_banned_flag')) |
135 | { |
136 | /** @var \phpbb\ban\manager $ban_manager */ |
137 | $ban_manager = $phpbb_container->get('ban.manager'); |
138 | $banned_users = $ban_manager->get_banned_users(); |
139 | |
140 | $rows = array_filter($rows, function ($row) use ($banned_users) { |
141 | return !isset($banned_users[(int) $row['user_id']]); |
142 | }); |
143 | } |
144 | |
145 | if (empty($rows)) |
146 | { |
147 | trigger_error($user->lang['NO_USER'] . adm_back_link($this->u_action), E_USER_WARNING); |
148 | } |
149 | |
150 | $i = $j = 0; |
151 | |
152 | // Send with BCC |
153 | // Maximum number of bcc recipients |
154 | $max_chunk_size = (int) $config['email_max_chunk_size']; |
155 | $email_list = array(); |
156 | $old_lang = $rows[0]['user_lang']; |
157 | $old_notify_type = $rows[0]['user_notify_type']; |
158 | |
159 | foreach ($rows as $row) |
160 | { |
161 | if (($row['user_notify_type'] == messenger_interface::NOTIFY_EMAIL && $row['user_email']) || |
162 | ($row['user_notify_type'] == messenger_interface::NOTIFY_IM && $row['user_jabber']) || |
163 | ($row['user_notify_type'] == messenger_interface::NOTIFY_BOTH && ($row['user_email'] || $row['user_jabber']))) |
164 | { |
165 | if ($i == $max_chunk_size || $row['user_lang'] != $old_lang || $row['user_notify_type'] != $old_notify_type) |
166 | { |
167 | $i = 0; |
168 | |
169 | if (count($email_list)) |
170 | { |
171 | $j++; |
172 | } |
173 | |
174 | $old_lang = $row['user_lang']; |
175 | $old_notify_type = $row['user_notify_type']; |
176 | } |
177 | |
178 | $email_list[$j][$i]['lang'] = $row['user_lang']; |
179 | $email_list[$j][$i]['method'] = $row['user_notify_type']; |
180 | $email_list[$j][$i]['email'] = $row['user_email']; |
181 | $email_list[$j][$i]['name'] = $row['username']; |
182 | $email_list[$j][$i]['jabber'] = $row['user_jabber']; |
183 | $i++; |
184 | } |
185 | } |
186 | |
187 | $errored = false; |
188 | |
189 | $email_template = 'admin_send_email'; |
190 | $template_data = [ |
191 | 'CONTACT_EMAIL' => phpbb_get_board_contact($config, $phpEx), |
192 | 'MESSAGE' => html_entity_decode($message, ENT_COMPAT), |
193 | ]; |
194 | $generate_log_entry = true; |
195 | |
196 | /** |
197 | * Modify email template data before the emails are sent |
198 | * |
199 | * @event core.acp_email_send_before |
200 | * @var string email_template The template to be used for sending the email |
201 | * @var string subject The subject of the email |
202 | * @var array template_data Array with template data assigned to email template |
203 | * @var bool generate_log_entry If false, no log entry will be created |
204 | * @var array usernames Usernames which will be displayed in log entry, if it will be created |
205 | * @var int group_id The group this email will be sent to |
206 | * @var bool use_queue If true, email queue will be used for sending |
207 | * @var int priority Priority of sent emails |
208 | * @since 3.1.3-RC1 |
209 | */ |
210 | $vars = array( |
211 | 'email_template', |
212 | 'subject', |
213 | 'template_data', |
214 | 'generate_log_entry', |
215 | 'usernames', |
216 | 'group_id', |
217 | 'use_queue', |
218 | 'priority', |
219 | ); |
220 | extract($phpbb_dispatcher->trigger_event('core.acp_email_send_before', compact($vars))); |
221 | |
222 | /** @var \phpbb\di\service_collection */ |
223 | $messenger = $phpbb_container->get('messenger.method_collection'); |
224 | $messenger_collection_iterator = $messenger->getIterator(); |
225 | for ($i = 0, $size = count($email_list); $i < $size; $i++) |
226 | { |
227 | $used_lang = $email_list[$i][0]['lang']; |
228 | $used_method = $email_list[$i][0]['method']; |
229 | |
230 | foreach ($messenger_collection_iterator as $messenger_method) |
231 | { |
232 | $notify_method = $messenger_method->get_id(); |
233 | if ($notify_method == $used_method || $used_method == messenger_interface::NOTIFY_BOTH) |
234 | { |
235 | $messenger_method->set_use_queue($use_queue); |
236 | $messenger_method->template($email_template, $used_lang); |
237 | $messenger_method->subject(html_entity_decode($subject, ENT_COMPAT)); |
238 | $messenger_method->assign_vars($template_data); |
239 | |
240 | if ($notify_method == messenger_interface::NOTIFY_EMAIL) |
241 | { |
242 | for ($j = 0, $list_size = count($email_list[$i]); $j < $list_size; $j++) |
243 | { |
244 | $email_row = $email_list[$i][$j]; |
245 | if (count($email_list[$i]) == 1) |
246 | { |
247 | $messenger_method->to($email_row['email'], $email_row['name']); |
248 | } |
249 | else |
250 | { |
251 | $messenger_method->bcc($email_row['email'], $email_row['name']); |
252 | } |
253 | } |
254 | |
255 | $messenger_method->anti_abuse_headers($config, $user); |
256 | $messenger_method->set_mail_priority($priority); |
257 | } |
258 | else if ($notify_method == messenger_interface::NOTIFY_IM) |
259 | { |
260 | for ($j = 0, $list_size = count($email_list[$i]); $j < $list_size; $j++) |
261 | { |
262 | $email_row = $email_list[$i][$j]; |
263 | $messenger_method->to($email_row['jabber'], $email_row['name']); |
264 | } |
265 | } |
266 | |
267 | $errored = !$messenger_method->send() || $errored; |
268 | } |
269 | } |
270 | } |
271 | unset($email_list); |
272 | |
273 | if ($use_queue) |
274 | { |
275 | foreach ($messenger_collection_iterator as $messenger_method) |
276 | { |
277 | $messenger_method->save_queue(); |
278 | } |
279 | } |
280 | |
281 | if ($generate_log_entry) |
282 | { |
283 | if (!empty($usernames)) |
284 | { |
285 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MASS_EMAIL', false, array(implode(', ', utf8_normalize_nfc($usernames)))); |
286 | } |
287 | else |
288 | { |
289 | if ($group_id) |
290 | { |
291 | $group_name = get_group_name($group_id); |
292 | } |
293 | else |
294 | { |
295 | // Not great but the logging routine doesn't cope well with localising on the fly |
296 | $group_name = $user->lang['ALL_USERS']; |
297 | } |
298 | |
299 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MASS_EMAIL', false, array($group_name)); |
300 | } |
301 | } |
302 | |
303 | if (!$errored) |
304 | { |
305 | $message = ($use_queue) ? $user->lang['EMAIL_SENT_QUEUE'] : $user->lang['EMAIL_SENT']; |
306 | trigger_error($message . adm_back_link($this->u_action)); |
307 | } |
308 | else |
309 | { |
310 | $message = sprintf($user->lang['EMAIL_SEND_ERROR'], '<a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=logs&mode=critical') . '">', '</a>'); |
311 | trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING); |
312 | } |
313 | } |
314 | } |
315 | |
316 | // Exclude bots and guests... |
317 | $sql = 'SELECT group_id |
318 | FROM ' . GROUPS_TABLE . " |
319 | WHERE group_name IN ('BOTS', 'GUESTS')"; |
320 | $result = $db->sql_query($sql); |
321 | |
322 | $exclude = array(); |
323 | while ($row = $db->sql_fetchrow($result)) |
324 | { |
325 | $exclude[] = $row['group_id']; |
326 | } |
327 | $db->sql_freeresult($result); |
328 | |
329 | $select_list = '<option value="0"' . ((!$group_id) ? ' selected="selected"' : '') . '>' . $user->lang['ALL_USERS'] . '</option>'; |
330 | $select_list .= group_select_options($group_id, $exclude); |
331 | |
332 | $s_priority_options = '<option value="' . MAIL_LOW_PRIORITY . '">' . $user->lang['MAIL_LOW_PRIORITY'] . '</option>'; |
333 | $s_priority_options .= '<option value="' . MAIL_NORMAL_PRIORITY . '" selected="selected">' . $user->lang['MAIL_NORMAL_PRIORITY'] . '</option>'; |
334 | $s_priority_options .= '<option value="' . MAIL_HIGH_PRIORITY . '">' . $user->lang['MAIL_HIGH_PRIORITY'] . '</option>'; |
335 | |
336 | $template_data = array( |
337 | 'S_WARNING' => (count($error)) ? true : false, |
338 | 'WARNING_MSG' => (count($error)) ? implode('<br />', $error) : '', |
339 | 'U_ACTION' => $this->u_action, |
340 | 'S_GROUP_OPTIONS' => $select_list, |
341 | 'USERNAMES' => implode("\n", $usernames), |
342 | 'U_FIND_USERNAME' => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&form=acp_email&field=usernames'), |
343 | 'SUBJECT' => $subject, |
344 | 'MESSAGE' => $message, |
345 | 'S_PRIORITY_OPTIONS' => $s_priority_options, |
346 | ); |
347 | |
348 | /** |
349 | * Modify custom email template data before we display the form |
350 | * |
351 | * @event core.acp_email_display |
352 | * @var array template_data Array with template data assigned to email template |
353 | * @var array exclude Array with groups which are excluded from group selection |
354 | * @var array usernames Usernames which will be displayed in form |
355 | * |
356 | * @since 3.1.4-RC1 |
357 | */ |
358 | $vars = array('template_data', 'exclude', 'usernames'); |
359 | extract($phpbb_dispatcher->trigger_event('core.acp_email_display', compact($vars))); |
360 | |
361 | $template->assign_vars($template_data); |
362 | } |
363 | } |