Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
message
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 12
506
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_subject
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_body
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_template
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 set_template_vars
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add_recipient_from_user_row
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 add_recipient
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 set_sender_from_user
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 set_sender
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 set_sender_notify_type
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 cc_sender
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 send
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 1
90
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
14namespace phpbb\message;
15
16use phpbb\messenger\method\messenger_interface;
17
18/**
19* Class message
20* Holds all information for an email and sends it in the end
21*/
22class message
23{
24    /** @var string */
25    protected $server_name;
26
27    /** @var string */
28    protected $subject = '';
29    /** @var string */
30    protected $body = '';
31    /** @var string */
32    protected $template = '';
33    /** @var array */
34    protected $template_vars = array();
35
36    /** @var string */
37    protected $sender_ip = '';
38    /** @var string */
39    protected $sender_name = '';
40    /** @var string */
41    protected $sender_address = '';
42    /** @var string */
43    protected $sender_lang = '';
44    /** @var string|int */
45    protected $sender_id = '';
46    /** @var string */
47    protected $sender_username = '';
48    /** @var string */
49    protected $sender_jabber = '';
50    /** @var int */
51    protected $sender_notify_type = messenger_interface::NOTIFY_EMAIL;
52
53    /** @var array */
54    protected $recipients;
55
56    /**
57    * Construct
58    *
59    * @param string $server_name    Used for AntiAbuse header
60    */
61    public function __construct($server_name)
62    {
63        $this->server_name = $server_name;
64    }
65
66    /**
67    * Set the subject of the email
68    *
69    * @param string $subject
70    * @return void
71    */
72    public function set_subject($subject)
73    {
74        $this->subject = $subject;
75    }
76
77    /**
78    * Set the body of the email text
79    *
80    * @param string $body
81    * @return void
82    */
83    public function set_body($body)
84    {
85        $this->body = $body;
86    }
87
88    /**
89    * Set the name of the email template to use
90    *
91    * @param string $template
92    * @return void
93    */
94    public function set_template($template)
95    {
96        $this->template = $template;
97    }
98
99    /**
100    * Set the array with the "template" data for the email
101    *
102    * @param array $template_vars
103    * @return void
104    */
105    public function set_template_vars($template_vars)
106    {
107        $this->template_vars = $template_vars;
108    }
109
110    /**
111    * Add a recipient from \phpbb\user
112    *
113    * @param array $user
114    * @return void
115    */
116    public function add_recipient_from_user_row(array $user)
117    {
118        $this->add_recipient(
119            $user['username'],
120            $user['user_email'],
121            $user['user_lang'],
122            $user['user_notify_type'],
123            $user['username'],
124            $user['user_jabber']
125        );
126    }
127
128    /**
129    * Add a recipient
130    *
131    * @param string $recipient_name        Displayed sender name
132    * @param string $recipient_address    Email address
133    * @param string $recipient_lang
134    * @param int $recipient_notify_type    Used notification methods (Jabber, Email, ...)
135    * @param string $recipient_username    User Name (used for AntiAbuse header)
136    * @param string $recipient_jabber
137    * @return void
138    */
139    public function add_recipient($recipient_name, $recipient_address, $recipient_lang, $recipient_notify_type = messenger_interface::NOTIFY_EMAIL, $recipient_username = '', $recipient_jabber = '')
140    {
141        $this->recipients[] = array(
142            'name'            => $recipient_name,
143            'user_email'    => $recipient_address,
144            'lang'            => $recipient_lang,
145            'username'        => $recipient_username,
146            'user_jabber'    => $recipient_jabber,
147            'notify_type'    => $recipient_notify_type,
148            'to_name'        => $recipient_name,
149        );
150    }
151
152    /**
153    * Set the senders data from \phpbb\user object
154    *
155    * @param \phpbb\user $user
156    * @return void
157    */
158    public function set_sender_from_user($user)
159    {
160        $this->set_sender(
161            $user->ip,
162            $user->data['username'],
163            $user->data['user_email'],
164            $user->lang_name,
165            $user->data['user_id'],
166            $user->data['username'],
167            $user->data['user_jabber']
168        );
169
170        $this->set_sender_notify_type($user->data['user_notify_type']);
171    }
172
173    /**
174    * Set the senders data
175    *
176    * @param string $sender_ip
177    * @param string $sender_name        Displayed sender name
178    * @param string $sender_address        Email address
179    * @param string $sender_lang
180    * @param int $sender_id                User ID
181    * @param string $sender_username    User Name (used for AntiAbuse header)
182    * @param string $sender_jabber
183    * @return void
184    */
185    public function set_sender($sender_ip, $sender_name, $sender_address, $sender_lang = '', $sender_id = 0, $sender_username = '', $sender_jabber = '')
186    {
187        $this->sender_ip = $sender_ip;
188        $this->sender_name = $sender_name;
189        $this->sender_address = $sender_address;
190        $this->sender_lang = $sender_lang;
191        $this->sender_id = $sender_id;
192        $this->sender_username = $sender_username;
193        $this->sender_jabber = $sender_jabber;
194    }
195
196    /**
197    * Which notification type should be used? Jabber, Email, ...?
198    *
199    * @param int $sender_notify_type
200    * @return void
201    */
202    public function set_sender_notify_type($sender_notify_type)
203    {
204        $this->sender_notify_type = $sender_notify_type;
205    }
206
207    /**
208    * Ok, now the same email if CC specified, but without exposing the user's email address
209    *
210    * @return void
211    */
212    public function cc_sender()
213    {
214        if (!count($this->recipients))
215        {
216            trigger_error('No email recipients specified');
217        }
218        if (!$this->sender_address)
219        {
220            trigger_error('No email sender specified');
221        }
222
223        $this->recipients[] = array(
224            'lang'            => $this->sender_lang,
225            'user_email'    => $this->sender_address,
226            'name'            => $this->sender_name,
227            'username'        => $this->sender_username,
228            'user_jabber'    => $this->sender_jabber,
229            'notify_type'    => $this->sender_notify_type,
230            'to_name'        => $this->recipients[0]['to_name'],
231        );
232    }
233
234    /**
235    * Send the email
236    *
237    * @param \phpbb\di\service_collection $messenger
238    * @param string $contact
239    * @return void
240    */
241    public function send(\phpbb\di\service_collection $messenger, $contact)
242    {
243        if (!count($this->recipients))
244        {
245            return;
246        }
247
248        foreach ($this->recipients as $recipient)
249        {
250            /** @psalm-suppress InvalidTemplateParam */
251            $messenger_collection_iterator = $messenger->getIterator();
252            foreach ($messenger_collection_iterator as $messenger_method)
253            {
254                $messenger_method->set_use_queue(false);
255                if ($messenger_method->get_id() == $recipient['notify_type'] || $recipient['notify_type'] == $messenger_method::NOTIFY_BOTH)
256                {
257                    $messenger_method->template($this->template, $recipient['lang']);
258                    $messenger_method->set_addresses($recipient);
259                    $messenger_method->reply_to($this->sender_address);
260
261                    $messenger_method->header('X-AntiAbuse', 'Board servername - ' . $this->server_name);
262                    $messenger_method->header('X-AntiAbuse', 'User IP - ' . $this->sender_ip);
263                    if ($this->sender_id)
264                    {
265                        $messenger_method->header('X-AntiAbuse', 'User_id - ' . $this->sender_id);
266                    }
267                    if ($this->sender_username)
268                    {
269                        $messenger_method->header('X-AntiAbuse', 'Username - ' . $this->sender_username);
270                    }
271
272                    $messenger_method->subject(html_entity_decode($this->subject, ENT_COMPAT));
273
274                    $messenger_method->assign_vars([
275                        'BOARD_CONTACT'    => $contact,
276                        'TO_USERNAME'    => html_entity_decode($recipient['to_name'], ENT_COMPAT),
277                        'FROM_USERNAME'    => html_entity_decode($this->sender_name, ENT_COMPAT),
278                        'MESSAGE'        => html_entity_decode($this->body, ENT_COMPAT),
279                    ]);
280
281                    if (count($this->template_vars))
282                    {
283                        $messenger_method->assign_vars($this->template_vars);
284                    }
285
286                    $messenger_method->send();
287                }
288            }
289        }
290    }
291}