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