Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 160
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 / 158
0.00% covered (danger)
0.00%
0 / 1
1056
0.00% covered (danger)
0.00%
0 / 1
 main
0.00% covered (danger)
0.00%
0 / 158
0.00% covered (danger)
0.00%
0 / 1
1056
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
14use phpbb\messenger\method\messenger_interface;
15
16/**
17* @ignore
18*/
19if (!defined('IN_PHPBB'))
20{
21    exit;
22}
23
24class 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_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',
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',
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',
104                        );
105                    }
106                    else
107                    {
108                        $sql_ary = array(
109                            'SELECT'    => 'u.user_id, u.username, u.username_clean, u.user_email, u.user_lang',
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',
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
158                foreach ($rows as $row)
159                {
160                    if ($row['user_email'])
161                    {
162                        if ($i == $max_chunk_size || $row['user_lang'] != $old_lang)
163                        {
164                            $i = 0;
165
166                            if (count($email_list))
167                            {
168                                $j++;
169                            }
170
171                            $old_lang = $row['user_lang'];
172                        }
173
174                        $email_list[$j][$i]['lang']        = $row['user_lang'];
175                        $email_list[$j][$i]['email']    = $row['user_email'];
176                        $email_list[$j][$i]['name']        = $row['username'];
177                        $i++;
178                    }
179                }
180
181                $errored = false;
182
183                $email_template = 'admin_send_email';
184                $template_data = [
185                    'CONTACT_EMAIL' => phpbb_get_board_contact($config, $phpEx),
186                    'MESSAGE'        => html_entity_decode($message, ENT_COMPAT),
187                ];
188                $generate_log_entry = true;
189
190                /**
191                * Modify email template data before the emails are sent
192                *
193                * @event core.acp_email_send_before
194                * @var    string    email_template        The template to be used for sending the email
195                * @var    string    subject                The subject of the email
196                * @var    array    template_data        Array with template data assigned to email template
197                * @var    bool    generate_log_entry    If false, no log entry will be created
198                * @var    array    usernames            Usernames which will be displayed in log entry, if it will be created
199                * @var    int        group_id            The group this email will be sent to
200                * @var    bool    use_queue            If true, email queue will be used for sending
201                * @var    int        priority            Priority of sent emails
202                * @since 3.1.3-RC1
203                */
204                $vars = array(
205                    'email_template',
206                    'subject',
207                    'template_data',
208                    'generate_log_entry',
209                    'usernames',
210                    'group_id',
211                    'use_queue',
212                    'priority',
213                );
214                extract($phpbb_dispatcher->trigger_event('core.acp_email_send_before', compact($vars)));
215
216                /** @var \phpbb\di\service_collection $messenger_collection */
217                $messenger_collection = $phpbb_container->get('messenger.method_collection');
218                /** @var \phpbb\messenger\method\messenger_interface $messenger_method */
219                $messenger_method = $messenger_collection->offsetGet('messenger.method.email');
220
221                for ($i = 0, $size = count($email_list); $i < $size; $i++)
222                {
223                    $used_lang = $email_list[$i][0]['lang'];
224
225                    $messenger_method->set_use_queue($use_queue);
226                    $messenger_method->template($email_template, $used_lang);
227                    $messenger_method->subject(html_entity_decode($subject, ENT_COMPAT));
228                    $messenger_method->assign_vars($template_data);
229
230                    for ($j = 0, $list_size = count($email_list[$i]); $j < $list_size; $j++)
231                    {
232                        $email_row = $email_list[$i][$j];
233                        if (count($email_list[$i]) == 1)
234                        {
235                            $messenger_method->to($email_row['email'], $email_row['name']);
236                        }
237                        else
238                        {
239                            $messenger_method->bcc($email_row['email'], $email_row['name']);
240                        }
241                    }
242
243                    $messenger_method->anti_abuse_headers($config, $user);
244                    $messenger_method->set_mail_priority($priority);
245
246                    $errored = !$messenger_method->send() || $errored;
247                    $messenger_method->save_queue();
248                }
249                unset($email_list);
250
251                if ($generate_log_entry)
252                {
253                    if (!empty($usernames))
254                    {
255                        $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MASS_EMAIL', false, array(implode(', ', utf8_normalize_nfc($usernames))));
256                    }
257                    else
258                    {
259                        if ($group_id)
260                        {
261                            $group_name = get_group_name($group_id);
262                        }
263                        else
264                        {
265                            // Not great but the logging routine doesn't cope well with localising on the fly
266                            $group_name = $user->lang['ALL_USERS'];
267                        }
268
269                        $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_MASS_EMAIL', false, array($group_name));
270                    }
271                }
272
273                if (!$errored)
274                {
275                    $message = ($use_queue) ? $user->lang['EMAIL_SENT_QUEUE'] : $user->lang['EMAIL_SENT'];
276                    trigger_error($message . adm_back_link($this->u_action));
277                }
278                else
279                {
280                    $message = sprintf($user->lang['EMAIL_SEND_ERROR'], '<a href="' . append_sid("{$phpbb_admin_path}index.$phpEx", 'i=logs&amp;mode=critical') . '">', '</a>');
281                    trigger_error($message . adm_back_link($this->u_action), E_USER_WARNING);
282                }
283            }
284        }
285
286        // Exclude bots and guests...
287        $sql = 'SELECT group_id
288            FROM ' . GROUPS_TABLE . "
289            WHERE group_name IN ('BOTS', 'GUESTS')";
290        $result = $db->sql_query($sql);
291
292        $exclude = array();
293        while ($row = $db->sql_fetchrow($result))
294        {
295            $exclude[] = $row['group_id'];
296        }
297        $db->sql_freeresult($result);
298
299        $select_list = '<option value="0"' . ((!$group_id) ? ' selected="selected"' : '') . '>' . $user->lang['ALL_USERS'] . '</option>';
300        $select_list .= group_select_options($group_id, $exclude);
301
302        $s_priority_options = '<option value="' . MAIL_LOW_PRIORITY . '">' . $user->lang['MAIL_LOW_PRIORITY'] . '</option>';
303        $s_priority_options .= '<option value="' . MAIL_NORMAL_PRIORITY . '" selected="selected">' . $user->lang['MAIL_NORMAL_PRIORITY'] . '</option>';
304        $s_priority_options .= '<option value="' . MAIL_HIGH_PRIORITY . '">' . $user->lang['MAIL_HIGH_PRIORITY'] . '</option>';
305
306        $template_data = array(
307            'S_WARNING'                => (count($error)) ? true : false,
308            'WARNING_MSG'            => (count($error)) ? implode('<br />', $error) : '',
309            'U_ACTION'                => $this->u_action,
310            'S_GROUP_OPTIONS'        => $select_list,
311            'USERNAMES'                => implode("\n", $usernames),
312            'U_FIND_USERNAME'        => append_sid("{$phpbb_root_path}memberlist.$phpEx", 'mode=searchuser&amp;form=acp_email&amp;field=usernames'),
313            'SUBJECT'                => $subject,
314            'MESSAGE'                => $message,
315            'S_PRIORITY_OPTIONS'    => $s_priority_options,
316        );
317
318        /**
319        * Modify custom email template data before we display the form
320        *
321        * @event core.acp_email_display
322        * @var    array    template_data        Array with template data assigned to email template
323        * @var    array    exclude                Array with groups which are excluded from group selection
324        * @var    array    usernames            Usernames which will be displayed in form
325        *
326        * @since 3.1.4-RC1
327        */
328        $vars = array('template_data', 'exclude', 'usernames');
329        extract($phpbb_dispatcher->trigger_event('core.acp_email_display', compact($vars)));
330
331        $template->assign_vars($template_data);
332    }
333}