Passed
Push — development ( 99aa7b...924c99 )
by Spuds
01:08 queued 20s
created

BaseMail::setLanguage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Base abstract class for mail functions
5
 *
6
 * @package   ElkArte Forum
7
 * @copyright ElkArte Forum contributors
8
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
9
 *
10
 * @version 2.0 Beta 1
11
 *
12
 */
13
14
namespace ElkArte\Mail;
15
16
/**
17
 * BaseMail class provides constructor and common code for other mail functions
18
 */
19
abstract class BaseMail
20
{
21
	/** @var bool If to use PBE/Mailist processing */
22
	public $mailList = false;
23
24
	/** @var bool If to use mail or SMTP to send */
25
	public $useSendmail = true;
26
27
	/** @var string \r\n or \n based on transport and OS */
28
	public $lineBreak = "\n";
29
30
	/** @var string m, p or t */
31
	public $messageType;
32
33
	/** @var array collection of data for saving in DB to allow reply to email */
34
	public $unqPBEHead = [];
35
36
	/** @var string Used to help bounce detection */
37
	public $returnPath;
38
39
	/** @var string The language to use for the email templates */
40
	public $language;
41
42
	/**
43
	 * Constructor, use to set the transport and linebreak
44
	 */
45
	public function __construct()
46
	{
47
		$this->setMailTransport();
48
		$this->setLineBreak();
49
		$this->setLanguage();
50
51
		require_once(SUBSDIR . '/Mail.subs.php');
52
	}
53
54
	/**
55
	 * Sets the language for the current instance. If no language is provided, the global default language is used.
56
	 *
57
	 * @param string $language The language to set. If empty, the global $language will be used.
58
	 * @return void
59
	 */
60
	public function setLanguage($language = ''): void
61
	{
62
		if (empty($language))
63
		{
64
			$language = $GLOBALS['language'];
65
		}
66
67
		$this->language = $language;
68
	}
69
70
	/**
71
	 * Sets if we use php mail or smtp mail transport
72
	 */
73
	public function setMailTransport(): void
74
	{
75
		global $modSettings;
76
77
		$this->useSendmail = empty($modSettings['mail_type']) || $modSettings['smtp_host'] === '';
78
	}
79
80
	/**
81
	 * Based on OS or mail transport, sets the needed linebreak value
82
	 */
83
	public function setLineBreak(): void
84
	{
85
		// If messages are not received while not using SMTP, then try using a LF (\n) only. Some Unix
86
		// mail transfer agents (notably qmail) replace LF by CRLF automatically (which leads to doubling
87
		// CR if CRLF is used). That should be a last resort.
88
		$this->lineBreak = "\r\n";
89
90
		// Line breaks need to be \r\n only in windows or for SMTP.
91
		// $this->lineBreak = detectServer()->is('windows') || !$this->useSendmail ? "\r\n" : "\n";
92
	}
93
94
	/**
95
	 * Sets flag if we are using maillist functionality
96
	 *
97
	 * @param string $from_wrapper
98
	 * @param string $message_id
99
	 * @param int $priority
100
	 */
101
	public function setMailList($from_wrapper, $message_id, $priority): void
102
	{
103
		global $modSettings;
104
105
		// Using maillist styles and this message qualifies (priority 3 and below only (4 = digest, 5 = newsletter))
106
		$this->mailList = !empty($modSettings['maillist_enabled'])
107
			&& $from_wrapper !== null
108
			&& $message_id !== null
109
			&& $priority < 4
110
			&& empty($modSettings['mail_no_message_id']);
111
	}
112
113
	/**
114
	 * Message type is one of m = message, t = topic, p = private
115
	 *
116
	 * @param string $message_id
117
	 * @return string|null cleaned message id
118
	 */
119
	public function setMessageType($message_id): ?string
120
	{
121
		$this->messageType = 'm';
122
		if ($message_id !== null && isset($message_id[0]) && in_array($message_id[0], ['m', 'p', 't']))
123
		{
124
			$this->messageType = $message_id[0];
125
			$message_id = substr($message_id, 1);
126
		}
127
128
		return $message_id;
129
	}
130
131
	/**
132
	 * Sets the unique ID for the message id header and PBE emails
133
	 *
134
	 * If using maillist functions it will also insert the ID into the message body's as
135
	 * some email clients strip, or do not return, proper headers to show what they are in replying to.
136
	 * PBE functions depend on finding this key to match up reply's to a message and ensure the reply
137
	 * was from a valid recipient.
138
	 *
139
	 * @param $message_id
140
	 * @return string
141
	 */
142
	public function getUniqueMessageID($message_id): string
143
	{
144
		global $boardurl, $modSettings;
145
146
		$unq_head = '';
147
148
		// If we are using the post by email functions, then we generate "reply to mail" security keys
149
		if ($this->mailList)
150
		{
151
			$this->unqPBEHead[0] = md5($boardurl . microtime() . mt_rand());
152
			$this->unqPBEHead[1] = $this->messageType;
153
			$this->unqPBEHead[2] = $message_id;
154
155
			$unq_head = $this->unqPBEHead[0] . '-' . $this->unqPBEHead[1] . $this->unqPBEHead[2];
156
		}
157
		elseif (empty($modSettings['mail_no_message_id']))
158
		{
159
			$unq_head = md5($boardurl . microtime()) . '-' . $message_id;
160
		}
161
162
		return $unq_head;
163
	}
164
165
	/**
166
	 * Sets a return path, mainly used in PHP mail() function to help in bounce detection
167
	 *
168
	 * @return void
169
	 */
170
	public function setReturnPath(): void
171
	{
172
		global $modSettings, $webmaster_email;
173
174
		$this->returnPath = '';
175
		if ($this->mailList === true)
176
		{
177
			$this->returnPath = empty($modSettings['maillist_sitename_address']) ? '' : $modSettings['maillist_sitename_address'];
178
		}
179
180
		if ($this->returnPath === '')
181
		{
182
			$this->returnPath = empty($modSettings['maillist_mail_from']) ? $webmaster_email : $modSettings['maillist_mail_from'];
183
		}
184
	}
185
}
186