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