aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/message/message.php
blob: 5fd24b542e459d57192a7409b483bbabad474728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\message;

/**
* Class message
* Holds all information for an email and sends it in the end
*/
class message
{
	/** @var string */
	protected $server_name;

	/** @var string */
	protected $subject = '';
	/** @var string */
	protected $body = '';
	/** @var string */
	protected $template = '';
	/** @var array */
	protected $template_vars = array();

	/** @var string */
	protected $sender_ip = '';
	/** @var string */
	protected $sender_name = '';
	/** @var string */
	protected $sender_address = '';
	/** @var string */
	protected $sender_lang = '';
	/** @var string */
	protected $sender_id = '';
	/** @var string */
	protected $sender_username = '';
	/** @var string */
	protected $sender_jabber = '';
	/** @var int */
	protected $sender_notify_type = NOTIFY_EMAIL;

	/** @var array */
	protected $recipients;

	/**
	* Construct
	*
	* @param string $server_name	Used for AntiAbuse header
	*/
	public function __construct($server_name)
	{
		$this->server_name = $server_name;
	}

	/**
	* Set the subject of the email
	*
	* @param string $subject
	* @return null
	*/
	public function set_subject($subject)
	{
		$this->subject = $subject;
	}

	/**
	* Set the body of the email text
	*
	* @param string $body
	* @return null
	*/
	public function set_body($body)
	{
		$this->body = $body;
	}

	/**
	* Set the name of the email template to use
	*
	* @param string $template
	* @return null
	*/
	public function set_template($template)
	{
		$this->template = $template;
	}

	/**
	* Set the array with the "template" data for the email
	*
	* @param array $template_vars
	* @return null
	*/
	public function set_template_vars($template_vars)
	{
		$this->template_vars = $template_vars;
	}

	/**
	* Add a recipient from \phpbb\user
	*
	* @param array $user
	* @return null
	*/
	public function add_recipient_from_user_row(array $user)
	{
		$this->add_recipient(
			$user['username'],
			$user['user_email'],
			$user['user_lang'],
			$user['user_notify_type'],
			$user['username'],
			$user['user_jabber']
		);
	}

	/**
	* Add a recipient
	*
	* @param string $recipient_name		Displayed sender name
	* @param string $recipient_address	Email address
	* @param string $recipient_lang
	* @param int $recipient_notify_type	Used notification methods (Jabber, Email, ...)
	* @param string $recipient_username	User Name (used for AntiAbuse header)
	* @param string $recipient_jabber
	* @return null
	*/
	public function add_recipient($recipient_name, $recipient_address, $recipient_lang, $recipient_notify_type = NOTIFY_EMAIL, $recipient_username = '', $recipient_jabber = '')
	{
		$this->recipients[] = array(
			'name'			=> $recipient_name,
			'address'		=> $recipient_address,
			'lang'			=> $recipient_lang,
			'username'		=> $recipient_username,
			'jabber'		=> $recipient_jabber,
			'notify_type'	=> $recipient_notify_type,
			'to_name'		=> $recipient_name,
		);
	}

	/**
	* Set the senders data from \phpbb\user object
	*
	* @param \phpbb\user $user
	* @return null
	*/
	public function set_sender_from_user($user)
	{
		$this->set_sender(
			$user->ip,
			$user->data['username'],
			$user->data['user_email'],
			$user->lang_name,
			$user->data['user_id'],
			$user->data['username'],
			$user->data['user_jabber']
		);

		$this->set_sender_notify_type($user->data['user_notify_type']);
	}

	/**
	* Set the senders data
	*
	* @param string $sender_ip
	* @param string $sender_name		Displayed sender name
	* @param string $sender_address		Email address
	* @param string $sender_lang
	* @param int $sender_id				User ID
	* @param string $sender_username	User Name (used for AntiAbuse header)
	* @param string $sender_jabber
	* @return null
	*/
	public function set_sender($sender_ip, $sender_name, $sender_address, $sender_lang = '', $sender_id = 0, $sender_username = '', $sender_jabber = '')
	{
		$this->sender_ip = $sender_ip;
		$this->sender_name = $sender_name;
		$this->sender_address = $sender_address;
		$this->sender_lang = $sender_lang;
		$this->sender_id = $sender_id;
		$this->sender_username = $sender_username;
		$this->sender_jabber = $sender_jabber;
	}

	/**
	* Which notification type should be used? Jabber, Email, ...?
	*
	* @param int $sender_notify_type
	* @return null
	*/
	public function set_sender_notify_type($sender_notify_type)
	{
		$this->sender_notify_type = $sender_notify_type;
	}

	/**
	* Ok, now the same email if CC specified, but without exposing the user's email address
	*
	* @return null
	*/
	public function cc_sender()
	{
		if (!sizeof($this->recipients))
		{
			trigger_error('No email recipients specified');
		}
		if (!$this->sender_address)
		{
			trigger_error('No email sender specified');
		}

		$this->recipients[] = array(
			'lang'			=> $this->sender_lang,
			'address'		=> $this->sender_address,
			'name'			=> $this->sender_name,
			'username'		=> $this->sender_username,
			'jabber'		=> $this->sender_jabber,
			'notify_type'	=> $this->sender_notify_type,
			'to_name'		=> $this->recipients[0]['to_name'],
		);
	}

	/**
	* Send the email
	*
	* @param \messenger $messenger
	* @param string $contact
	* @return null
	*/
	public function send(\messenger $messenger, $contact)
	{
		if (!sizeof($this->recipients))
		{
			return;
		}

		foreach ($this->recipients as $recipient)
		{
			$messenger->template($this->template, $recipient['lang']);
			$messenger->replyto($this->sender_address);
			$messenger->to($recipient['address'], $recipient['name']);
			$messenger->im($recipient['jabber'], $recipient['username']);

			$messenger->headers('X-AntiAbuse: Board servername - ' . $this->server_name);
			$messenger->headers('X-AntiAbuse: User IP - ' . $this->sender_ip);

			if ($this->sender_id)
			{
				$messenger->headers('X-AntiAbuse: User_id - ' . $this->sender_id);
			}
			if ($this->sender_username)
			{
				$messenger->headers('X-AntiAbuse: Username - ' . $this->sender_username);
			}

			$messenger->subject(htmlspecialchars_decode($this->subject));

			$messenger->assign_vars(array(
				'BOARD_CONTACT'	=> $contact,
				'TO_USERNAME'	=> htmlspecialchars_decode($recipient['to_name']),
				'FROM_USERNAME'	=> htmlspecialchars_decode($this->sender_name),
				'MESSAGE'		=> htmlspecialchars_decode($this->body))
			);

			if (sizeof($this->template_vars))
			{
				$messenger->assign_vars($this->template_vars);
			}

			$messenger->send($recipient['notify_type']);
		}
	}
}