[FEATURE] Allow .ts file extension for static typoscript templates
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_formmail.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains a class for formmail
29 *
30 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj
31 *
32 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
33 */
34
35 /**
36 * Formmail class, used by the TYPO3 "cms" extension (default frontend) to send email forms.
37 *
38 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
39 * @package TYPO3
40 * @subpackage t3lib
41 * @see tslib_fe::sendFormmail(), t3lib/formmail.php
42 */
43 class t3lib_formmail {
44 protected $reserved_names = 'recipient,recipient_copy,auto_respond_msg,auto_respond_checksum,redirect,subject,attachment,from_email,from_name,replyto_email,replyto_name,organisation,priority,html_enabled,quoted_printable,submit_x,submit_y';
45 // Collection of suspicious header data, used for logging
46 protected $dirtyHeaders = array();
47
48 protected $characterSet;
49 protected $subject;
50 protected $fromName;
51 protected $replyToName;
52 protected $organisation;
53 protected $fromAddress;
54 protected $replyToAddress;
55 protected $priority;
56 protected $autoRespondMessage;
57 protected $encoding = 'quoted-printable';
58
59 /** @var t3lib_mail_Message */
60 protected $mailMessage;
61 protected $recipient;
62 protected $plainContent = '';
63
64 /** @var array Files to clean up at the end (attachments) */
65 protected $temporaryFiles = array();
66
67 /**
68 * Start function
69 * This class is able to generate a mail in formmail-style from the data in $V
70 * Fields:
71 *
72 * [recipient]: email-adress of the one to receive the mail. If array, then all values are expected to be recipients
73 * [attachment]: ....
74 *
75 * [subject]: The subject of the mail
76 * [from_email]: Sender email. If not set, [email] is used
77 * [from_name]: Sender name. If not set, [name] is used
78 * [replyto_email]: Reply-to email. If not set [from_email] is used
79 * [replyto_name]: Reply-to name. If not set [from_name] is used
80 * [organisation]: Organization (header)
81 * [priority]: Priority, 1-5, default 3
82 * [html_enabled]: If mail is sent as html
83 * [use_base64]: If set, base64 encoding will be used instead of quoted-printable
84 *
85 * @param array $valueList Contains values for the field names listed above (with slashes removed if from POST input)
86 * @param boolean $base64 Whether to base64 encode the mail content
87 * @return void
88 */
89 function start($valueList, $base64 = FALSE) {
90
91 $this->mailMessage = t3lib_div::makeInstance('t3lib_mail_Message');
92
93 if ($GLOBALS['TSFE']->config['config']['formMailCharset']) {
94 // Respect formMailCharset if it was set
95 $this->characterSet = $GLOBALS['TSFE']->csConvObj->parse_charset($GLOBALS['TSFE']->config['config']['formMailCharset']);
96 } elseif ($GLOBALS['TSFE']->metaCharset != $GLOBALS['TSFE']->renderCharset) {
97 // Use metaCharset for mail if different from renderCharset
98 $this->characterSet = $GLOBALS['TSFE']->metaCharset;
99 } else {
100 // Otherwise use renderCharset as default
101 $this->characterSet = $GLOBALS['TSFE']->renderCharset;
102 }
103
104 if ($base64 || $valueList['use_base64']) {
105 $this->encoding = 'base64';
106 }
107
108 if (isset($valueList['recipient'])) {
109 // Convert form data from renderCharset to mail charset
110 $this->subject = ($valueList['subject'])
111 ? $valueList['subject']
112 : 'Formmail on ' . t3lib_div::getIndpEnv('HTTP_HOST');
113 $this->subject = $this->sanitizeHeaderString($this->subject);
114
115 $this->fromName = ($valueList['from_name'])
116 ? $valueList['from_name']
117 : (($valueList['name']) ? $valueList['name'] : '');
118 $this->fromName = $this->sanitizeHeaderString($this->fromName);
119
120 $this->replyToName = ($valueList['replyto_name']) ? $valueList['replyto_name'] : $this->fromName;
121 $this->replyToName = $this->sanitizeHeaderString($this->replyToName);
122
123 $this->organisation = ($valueList['organisation']) ? $valueList['organisation'] : '';
124 $this->organisation = $this->sanitizeHeaderString($this->organisation);
125
126 $this->fromAddress = ($valueList['from_email']) ? $valueList['from_email'] : (
127 ($valueList['email']) ? $valueList['email'] : ''
128 );
129 if (!t3lib_div::validEmail($this->fromAddress)) {
130 $this->fromAddress = t3lib_utility_Mail::getSystemFromAddress();
131 $this->fromName = t3lib_utility_Mail::getSystemFromName();
132 }
133
134 $this->replyToAddress = ($valueList['replyto_email']) ? $valueList['replyto_email'] : $this->fromAddress;
135
136 $this->priority = ($valueList['priority']) ? t3lib_utility_Math::forceIntegerInRange($valueList['priority'], 1, 5) : 3;
137
138 // Auto responder
139 $this->autoRespondMessage = (trim($valueList['auto_respond_msg']) && $this->fromAddress)
140 ? trim($valueList['auto_respond_msg'])
141 : '';
142
143 if ($this->autoRespondMessage !== '') {
144 // Check if the value of the auto responder message has been modified with evil intentions
145 $autoRespondChecksum = $valueList['auto_respond_checksum'];
146 $correctHmacChecksum = t3lib_div::hmac($this->autoRespondMessage);
147 if ($autoRespondChecksum !== $correctHmacChecksum) {
148 t3lib_div::sysLog(
149 'Possible misuse of t3lib_formmail auto respond method. Subject: ' . $valueList['subject'],
150 'Core',
151 t3lib_div::SYSLOG_SEVERITY_ERROR
152 );
153 return;
154 } else {
155 $this->autoRespondMessage = $this->sanitizeHeaderString($this->autoRespondMessage);
156 }
157 }
158
159 $plainTextContent = '';
160 $htmlContent = '<table border="0" cellpadding="2" cellspacing="2">';
161
162 // Runs through $V and generates the mail
163 if (is_array($valueList)) {
164 foreach ($valueList as $key => $val) {
165 if (!t3lib_div::inList($this->reserved_names, $key)) {
166 $space = (strlen($val) > 60) ? LF : '';
167 $val = (is_array($val) ? implode($val, LF) : $val);
168
169 // Convert form data from renderCharset to mail charset (HTML may use entities)
170 $plainTextValue = $val;
171 $HtmlValue = htmlspecialchars($val);
172
173 $plainTextContent .= strtoupper($key) . ': ' . $space . $plainTextValue . LF . $space;
174 $htmlContent .= '<tr><td bgcolor="#eeeeee"><font face="Verdana" size="1"><strong>' . strtoupper($key)
175 . '</strong></font></td><td bgcolor="#eeeeee"><font face="Verdana" size="1">' . nl2br($HtmlValue)
176 . '&nbsp;</font></td></tr>';
177 }
178 }
179 }
180 $htmlContent .= '</table>';
181
182 $this->plainContent = $plainTextContent;
183
184 if ($valueList['html_enabled']) {
185 $this->mailMessage->setBody($htmlContent, 'text/html', $this->characterSet);
186 $this->mailMessage->addPart($plainTextContent, 'text/plain', $this->characterSet);
187 } else {
188 $this->mailMessage->setBody($plainTextContent, 'text/plain', $this->characterSet);
189 }
190
191 for ($a = 0; $a < 10; $a++) {
192 $variableName = 'attachment' . (($a) ? $a : '');
193 if (!isset($_FILES[$variableName])) {
194 continue;
195 }
196 if (!is_uploaded_file($_FILES[$variableName]['tmp_name'])) {
197 t3lib_div::sysLog(
198 'Possible abuse of t3lib_formmail: temporary file "' . $_FILES[$variableName]['tmp_name'] .
199 '" ("' . $_FILES[$variableName]['name'] . '") was not an uploaded file.',
200 'Core',
201 t3lib_div::SYSLOG_SEVERITY_ERROR
202 );
203 }
204 if ($_FILES[$variableName]['tmp_name']['error'] !== UPLOAD_ERR_OK) {
205 t3lib_div::sysLog(
206 'Error in uploaded file in t3lib_formmail: temporary file "' .
207 $_FILES[$variableName]['tmp_name'] . '" ("' . $_FILES[$variableName]['name'] . '") Error code: ' .
208 $_FILES[$variableName]['tmp_name']['error'],
209 'Core',
210 t3lib_div::SYSLOG_SEVERITY_ERROR
211 );
212 }
213 $theFile = t3lib_div::upload_to_tempfile($_FILES[$variableName]['tmp_name']);
214 $theName = $_FILES[$variableName]['name'];
215
216 if ($theFile && file_exists($theFile)) {
217 if (filesize($theFile) < $GLOBALS['TYPO3_CONF_VARS']['FE']['formmailMaxAttachmentSize']) {
218 $this->mailMessage->attach(Swift_Attachment::fromPath($theFile)->setFilename($theName));
219 }
220 }
221 $this->temporaryFiles[] = $theFile;
222 }
223
224 $from = $this->fromName ? array($this->fromAddress => $this->fromName) : array($this->fromAddress);
225 $this->recipient = $this->parseAddresses($valueList['recipient']);
226 $this->mailMessage->setSubject($this->subject)
227 ->setFrom($from)
228 ->setTo($this->recipient)
229 ->setPriority($this->priority);
230 $replyTo = $this->replyToName ? array($this->replyToAddress => $this->replyToName) : array($this->replyToAddress);
231 $this->mailMessage->setReplyTo($replyTo);
232 $this->mailMessage->getHeaders()->addTextHeader('Organization', $this->organisation);
233 if ($valueList['recipient_copy']) {
234 $this->mailMessage->setCc($this->parseAddresses($valueList['recipient_copy']));
235 }
236 $this->mailMessage->setCharset($this->characterSet);
237
238 // Ignore target encoding. This is handled automatically by Swift Mailer and overriding the defaults
239 // is not worth the trouble
240
241 // Log dirty header lines
242 if ($this->dirtyHeaders) {
243 t3lib_div::sysLog(
244 'Possible misuse of t3lib_formmail: see TYPO3 devLog',
245 'Core',
246 t3lib_div::SYSLOG_SEVERITY_ERROR
247 );
248 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['enable_DLOG']) {
249 t3lib_div::devLog('t3lib_formmail: ' . t3lib_div::arrayToLogString($this->dirtyHeaders, '', 200), 'Core', 3);
250 }
251 }
252 }
253 }
254
255 /**
256 * Checks string for suspicious characters
257 *
258 * @param string $string String to check
259 * @return string Valid or empty string
260 */
261 protected function sanitizeHeaderString($string) {
262 $pattern = '/[\r\n\f\e]/';
263 if (preg_match($pattern, $string) > 0) {
264 $this->dirtyHeaders[] = $string;
265 $string = '';
266 }
267 return $string;
268 }
269
270 /**
271 * Parses mailbox headers and turns them into an array.
272 *
273 * Mailbox headers are a comma separated list of 'name <email@example.org' combinations or plain email addresses (or a mix
274 * of these).
275 * The resulting array has key-value pairs where the key is either a number (no display name in the mailbox header) and the
276 * value is the email address, or the key is the email address and the value is the display name.
277 *
278 * @param string $rawAddresses Comma separated list of email addresses (optionally with display name)
279 * @return array Parsed list of addresses.
280 */
281 protected function parseAddresses($rawAddresses = '') {
282 /** @var $addressParser t3lib_mail_Rfc822AddressesParser */
283 $addressParser = t3lib_div::makeInstance('t3lib_mail_Rfc822AddressesParser', $rawAddresses);
284 $addresses = $addressParser->parseAddressList();
285 $addressList = array();
286 foreach ($addresses as $address) {
287 if ($address->personal) {
288 // Item with name found ( name <email@example.org> )
289 $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
290 } else {
291 // Item without name found ( email@example.org )
292 $addressList[] = $address->mailbox . '@' . $address->host;
293 }
294 }
295 return $addressList;
296 }
297
298 /**
299 * Sends the actual mail and handles autorespond message
300 *
301 * @return boolean
302 */
303 public function sendTheMail() {
304
305 // Sending the mail requires the recipient and message to be set.
306 if (!$this->mailMessage->getTo() || !trim($this->mailMessage->getBody())) {
307 return FALSE;
308 }
309
310 $this->mailMessage->send();
311
312 // Auto response
313 if ($this->autoRespondMessage) {
314 $theParts = explode('/', $this->autoRespondMessage, 2);
315 $theParts[0] = str_replace('###SUBJECT###', $this->subject, $theParts[0]);
316 $theParts[1] = str_replace('/', LF, $theParts[1]);
317 $theParts[1] = str_replace('###MESSAGE###', $this->plainContent, $theParts[1]);
318
319 /** @var $autoRespondMail t3lib_mail_Message */
320 $autoRespondMail = t3lib_div::makeInstance('t3lib_mail_Message');
321 $autoRespondMail->setTo($this->fromAddress)
322 ->setSubject($theParts[0])
323 ->setFrom($this->recipient)
324 ->setBody($theParts[1]);
325 $autoRespondMail->send();
326 }
327 return $this->mailMessage->isSent();
328 }
329
330 /**
331 * Do some cleanup at the end (deleting attachment files)
332 */
333 public function __destruct() {
334 foreach ($this->temporaryFiles as $file) {
335 if (t3lib_div::isAllowedAbsPath($file) && t3lib_div::isFirstPartOfStr($file, PATH_site . 'typo3temp/upload_temp_')) {
336 t3lib_div::unlink_tempfile($file);
337 }
338 }
339 }
340 }
341
342 ?>