[TASK] Disable compression for inlineJs in Backend
[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 /**
37 * Formmail class, used by the TYPO3 "cms" extension (default frontend) to send email forms.
38 *
39 * @author Kasper Skårhøj <kasperYYYY@typo3.com>
40 * @package TYPO3
41 * @subpackage t3lib
42 * @see tslib_fe::sendFormmail(), t3lib/formmail.php
43 */
44 class t3lib_formmail {
45 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';
46 protected $dirtyHeaders = array(); // collection of suspicious header data, used for logging
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 Contains values for the field names listed above (with slashes removed if from POST input)
86 * @param boolean 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 }
100
101 if ($base64 || $valueList['use_base64']) {
102 $this->encoding = 'base64';
103 }
104
105 if (isset($valueList['recipient'])) {
106 // convert form data from renderCharset to mail charset
107 $this->subject = ($valueList['subject'])
108 ? $valueList['subject']
109 : 'Formmail on ' . t3lib_div::getIndpEnv('HTTP_HOST');
110 $this->subject = $this->sanitizeHeaderString($this->subject);
111
112 $this->fromName = ($valueList['from_name'])
113 ? $valueList['from_name']
114 : (($valueList['name']) ? $valueList['name'] : '');
115 $this->fromName = $this->sanitizeHeaderString($this->fromName);
116
117 $this->replyToName = ($valueList['replyto_name']) ? $valueList['replyto_name'] : $this->fromName;
118 $this->replyToName = $this->sanitizeHeaderString($this->replyToName);
119
120 $this->organisation = ($valueList['organisation']) ? $valueList['organisation'] : '';
121 $this->organisation = $this->sanitizeHeaderString($this->organisation);
122
123 $this->fromAddress = ($valueList['from_email']) ? $valueList['from_email'] : (
124 ($valueList['email']) ? $valueList['email'] : ''
125 );
126 if (!t3lib_div::validEmail($this->fromAddress)) {
127 $this->fromAddress = t3lib_utility_Mail::getSystemFromAddress();
128 $this->fromName = t3lib_utility_Mail::getSystemFromName();
129 }
130
131 $this->replyToAddress = ($valueList['replyto_email']) ? $valueList['replyto_email'] : $this->fromAddress;
132
133 $this->priority = ($valueList['priority']) ? t3lib_utility_Math::forceIntegerInRange($valueList['priority'], 1, 5) : 3;
134
135 // auto responder
136 $this->autoRespondMessage = (trim($valueList['auto_respond_msg']) && $this->fromAddress)
137 ? trim($valueList['auto_respond_msg'])
138 : '';
139
140 if ($this->autoRespondMessage !== '') {
141 // Check if the value of the auto responder message has been modified with evil intentions
142 $autoRespondChecksum = $valueList['auto_respond_checksum'];
143 $correctHmacChecksum = t3lib_div::hmac($this->autoRespondMessage);
144 if ($autoRespondChecksum !== $correctHmacChecksum) {
145 t3lib_div::sysLog('Possible misuse of t3lib_formmail auto respond method. Subject: ' . $valueList['subject'],
146 'Core',
147 3);
148 return;
149 } else {
150 $this->autoRespondMessage = $this->sanitizeHeaderString($this->autoRespondMessage);
151 }
152 }
153
154 $plainTextContent = '';
155 $htmlContent = '<table border="0" cellpadding="2" cellspacing="2">';
156
157 // Runs through $V and generates the mail
158 if (is_array($valueList)) {
159 foreach ($valueList as $key => $val) {
160 if (!t3lib_div::inList($this->reserved_names, $key)) {
161 $space = (strlen($val) > 60) ? LF : '';
162 $val = (is_array($val) ? implode($val, LF) : $val);
163
164 // convert form data from renderCharset to mail charset (HTML may use entities)
165 $plainTextValue = $val;
166 $HtmlValue = htmlspecialchars($val);
167
168 $plainTextContent .= strtoupper($key) . ': ' . $space . $plainTextValue . LF . $space;
169 $htmlContent .= '<tr><td bgcolor="#eeeeee"><font face="Verdana" size="1"><strong>' . strtoupper($key)
170 . '</strong></font></td><td bgcolor="#eeeeee"><font face="Verdana" size="1">' . nl2br($HtmlValue)
171 . '&nbsp;</font></td></tr>';
172 }
173 }
174 }
175 $htmlContent .= '</table>';
176
177 $this->plainContent = $plainTextContent;
178
179 if ($valueList['html_enabled']) {
180 $this->mailMessage->setBody($htmlContent, 'text/html');
181 $this->mailMessage->addPart($plainTextContent, 'text/plain');
182 } else {
183 $this->mailMessage->setBody($plainTextContent, 'text/plain');
184 }
185
186 for ($a = 0; $a < 10; $a++) {
187 $variableName = 'attachment' . (($a) ? $a : '');
188 if (!isset($_FILES[$variableName])) {
189 continue;
190 }
191 if (!is_uploaded_file($_FILES[$variableName]['tmp_name'])) {
192 t3lib_div::sysLog('Possible abuse of t3lib_formmail: temporary file "' . $_FILES[$variableName]['tmp_name']
193 . '" ("' . $_FILES[$variableName]['name'] . '") was not an uploaded file.', 'Core', 3);
194 }
195 if ($_FILES[$variableName]['tmp_name']['error'] !== UPLOAD_ERR_OK) {
196 t3lib_div::sysLog('Error in uploaded file in t3lib_formmail: temporary file "'
197 . $_FILES[$variableName]['tmp_name'] . '" ("' . $_FILES[$variableName]['name'] . '") Error code: '
198 . $_FILES[$variableName]['tmp_name']['error'], 'Core', 3);
199 }
200 $theFile = t3lib_div::upload_to_tempfile($_FILES[$variableName]['tmp_name']);
201 $theName = $_FILES[$variableName]['name'];
202
203 if ($theFile && file_exists($theFile)) {
204 if (filesize($theFile) < $GLOBALS['TYPO3_CONF_VARS']['FE']['formmailMaxAttachmentSize']) {
205 $this->mailMessage->attach(Swift_Attachment::fromPath($theFile)->setFilename($theName));
206 }
207 }
208 $this->temporaryFiles[] = $theFile;
209 }
210
211 $from = $this->fromName ? array($this->fromAddress => $this->fromName) : array($this->fromAddress);
212 $this->recipient = $this->parseAddresses($valueList['recipient']);
213 $this->mailMessage->setSubject($this->subject)
214 ->setFrom($from)
215 ->setTo($this->recipient)
216 ->setPriority($this->priority);
217 $replyTo = $this->replyToName ? array($this->replyToAddress => $this->replyToName) : array($this->replyToAddress);
218 $this->mailMessage->setReplyTo($replyTo);
219 $this->mailMessage->getHeaders()->addTextHeader('Organization', $this->organisation);
220 if ($valueList['recipient_copy']) {
221 $this->mailMessage->setCc($this->parseAddresses($valueList['recipient_copy']));
222 }
223 if ($this->characterSet) {
224 $this->mailMessage->setCharset($this->characterSet);
225 }
226 // Ignore target encoding. This is handled automatically by Swift Mailer and overriding the defaults
227 // is not worth the trouble
228
229 // log dirty header lines
230 if ($this->dirtyHeaders) {
231 t3lib_div::sysLog('Possible misuse of t3lib_formmail: see TYPO3 devLog', 'Core', 3);
232 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['enable_DLOG']) {
233 t3lib_div::devLog('t3lib_formmail: ' . t3lib_div::arrayToLogString($this->dirtyHeaders, '', 200), 'Core', 3);
234 }
235 }
236 }
237 }
238
239 /**
240 * Checks string for suspicious characters
241 *
242 * @param string String to check
243 * @return string Valid or empty string
244 */
245 protected function sanitizeHeaderString($string) {
246 $pattern = '/[\r\n\f\e]/';
247 if (preg_match($pattern, $string) > 0) {
248 $this->dirtyHeaders[] = $string;
249 $string = '';
250 }
251 return $string;
252 }
253
254 /**
255 * Parses mailbox headers and turns them into an array.
256 *
257 * Mailbox headers are a comma separated list of 'name <email@example.org' combinations or plain email addresses (or a mix
258 * of these).
259 * The resulting array has key-value pairs where the key is either a number (no display name in the mailbox header) and the
260 * value is the email address, or the key is the email address and the value is the display name.
261 *
262 * @param string $rawAddresses Comma separated list of email addresses (optionally with display name)
263 * @return array Parsed list of addresses.
264 */
265 protected function parseAddresses($rawAddresses = '') {
266 /** @var $addressParser t3lib_mail_Rfc822AddressesParser */
267 $addressParser = t3lib_div::makeInstance('t3lib_mail_Rfc822AddressesParser', $rawAddresses);
268 $addresses = $addressParser->parseAddressList();
269 $addressList = array();
270 foreach ($addresses as $address) {
271 if ($address->personal) {
272 // item with name found ( name <email@example.org> )
273 $addressList[$address->mailbox . '@' . $address->host] = $address->personal;
274 } else {
275 // item without name found ( email@example.org )
276 $addressList[] = $address->mailbox . '@' . $address->host;
277 }
278 }
279 return $addressList;
280 }
281
282 /**
283 * Sends the actual mail and handles autorespond message
284 *
285 * @return boolean
286 */
287 public function sendTheMail() {
288
289 // Sending the mail requires the recipient and message to be set.
290 if (!$this->mailMessage->getTo() || !trim($this->mailMessage->getBody())) {
291 return FALSE;
292 }
293
294 $this->mailMessage->send();
295
296 // Auto response
297 if ($this->autoRespondMessage) {
298 $theParts = explode('/', $this->autoRespondMessage, 2);
299 $theParts[0] = str_replace('###SUBJECT###', $this->subject, $theParts[0]);
300 $theParts[1] = str_replace("/", LF, $theParts[1]);
301 $theParts[1] = str_replace("###MESSAGE###", $this->plainContent, $theParts[1]);
302
303 /** @var $autoRespondMail t3lib_mail_Message */
304 $autoRespondMail = t3lib_div::makeInstance('t3lib_mail_Message');
305 $autoRespondMail->setTo($this->fromAddress)
306 ->setSubject($theParts[0])
307 ->setFrom($this->recipient)
308 ->setBody($theParts[1]);
309 $autoRespondMail->send();
310 }
311 return $this->mailMessage->isSent();
312 }
313
314 /**
315 * Do some cleanup at the end (deleting attachment files)
316 */
317 public function __destruct() {
318 foreach ($this->temporaryFiles as $file) {
319 if (t3lib_div::isAllowedAbsPath($file) && t3lib_div::isFirstPartOfStr($file, PATH_site . 'typo3temp/upload_temp_')) {
320 t3lib_div::unlink_tempfile($file);
321 }
322 }
323 }
324 }
325
326
327 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_formmail.php'])) {
328 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_formmail.php']);
329 }
330
331 ?>