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