|
TYPO3 API
SVNRelease
|
00001 <?php 00002 /*************************************************************** 00003 * Copyright notice 00004 * 00005 * (c) 1999-2011 Kasper Skårhøj (kasperYYYY@typo3.com) 00006 * All rights reserved 00007 * 00008 * This script is part of the TYPO3 project. The TYPO3 project is 00009 * free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * The GNU General Public License can be found at 00015 * http://www.gnu.org/copyleft/gpl.html. 00016 * A copy is found in the textfile GPL.txt and important notices to the license 00017 * from the author is found in LICENSE.txt distributed with these scripts. 00018 * 00019 * 00020 * This script is distributed in the hope that it will be useful, 00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00023 * GNU General Public License for more details. 00024 * 00025 * This copyright notice MUST APPEAR in all copies of the script! 00026 ***************************************************************/ 00027 /** 00028 * Contains a class for formmail 00029 * 00030 * $Id: class.t3lib_formmail.php 10601 2011-02-23 14:12:58Z baschny $ 00031 * Revised for TYPO3 3.6 July/2003 by Kasper Skårhøj 00032 * 00033 * @author Kasper Skårhøj <kasperYYYY@typo3.com> 00034 */ 00035 /** 00036 * [CLASS/FUNCTION INDEX of SCRIPT] 00037 * 00038 * 00039 * 00040 * 69: class t3lib_formmail 00041 * 95: function start($V,$base64=false) 00042 * 172: function addAttachment($file, $filename) 00043 * 00044 * TOTAL FUNCTIONS: 2 00045 * (This index is automatically created/updated by the extension "extdeveval") 00046 * 00047 */ 00048 00049 00050 /** 00051 * Formmail class, used by the TYPO3 "cms" extension (default frontend) to send email forms. 00052 * 00053 * @author Kasper Skårhøj <kasperYYYY@typo3.com> 00054 * @package TYPO3 00055 * @subpackage t3lib 00056 * @see tslib_fe::sendFormmail(), t3lib/formmail.php 00057 */ 00058 class t3lib_formmail { 00059 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'; 00060 protected $dirtyHeaders = array(); // collection of suspicious header data, used for logging 00061 00062 protected $characterSet; 00063 protected $subject; 00064 protected $fromName; 00065 protected $replyToName; 00066 protected $organisation; 00067 protected $fromAddress; 00068 protected $replyToAddress; 00069 protected $priority; 00070 protected $autoRespondMessage; 00071 protected $encoding = 'quoted-printable'; 00072 00073 /** @var t3lib_mail_Message */ 00074 protected $mailMessage; 00075 protected $recipient; 00076 protected $plainContent = ''; 00077 00078 /** @var array Files to clean up at the end (attachments) */ 00079 protected $temporaryFiles = array(); 00080 00081 /** 00082 * Start function 00083 * This class is able to generate a mail in formmail-style from the data in $V 00084 * Fields: 00085 * 00086 * [recipient]: email-adress of the one to receive the mail. If array, then all values are expected to be recipients 00087 * [attachment]: .... 00088 * 00089 * [subject]: The subject of the mail 00090 * [from_email]: Sender email. If not set, [email] is used 00091 * [from_name]: Sender name. If not set, [name] is used 00092 * [replyto_email]: Reply-to email. If not set [from_email] is used 00093 * [replyto_name]: Reply-to name. If not set [from_name] is used 00094 * [organisation]: Organization (header) 00095 * [priority]: Priority, 1-5, default 3 00096 * [html_enabled]: If mail is sent as html 00097 * [use_base64]: If set, base64 encoding will be used instead of quoted-printable 00098 * 00099 * @param array Contains values for the field names listed above (with slashes removed if from POST input) 00100 * @param boolean Whether to base64 encode the mail content 00101 * @return void 00102 */ 00103 function start($valueList, $base64 = false) { 00104 00105 $this->mailMessage = t3lib_div::makeInstance('t3lib_mail_Message'); 00106 00107 if ($GLOBALS['TSFE']->config['config']['formMailCharset']) { 00108 // Respect formMailCharset if it was set 00109 $this->characterSet = $GLOBALS['TSFE']->csConvObj->parse_charset($GLOBALS['TSFE']->config['config']['formMailCharset']); 00110 } elseif ($GLOBALS['TSFE']->metaCharset != $GLOBALS['TSFE']->renderCharset) { 00111 // Use metaCharset for mail if different from renderCharset 00112 $this->characterSet = $GLOBALS['TSFE']->metaCharset; 00113 } 00114 00115 if ($base64 || $valueList['use_base64']) { 00116 $this->encoding = 'base64'; 00117 } 00118 00119 if (isset($valueList['recipient'])) { 00120 // convert form data from renderCharset to mail charset 00121 $this->subject = ($valueList['subject']) 00122 ? $valueList['subject'] 00123 : 'Formmail on ' . t3lib_div::getIndpEnv('HTTP_HOST'); 00124 $this->subject = $this->sanitizeHeaderString($this->subject); 00125 00126 $this->fromName = ($valueList['from_name']) 00127 ? $valueList['from_name'] 00128 : (($valueList['name']) ? $valueList['name'] : ''); 00129 $this->fromName = $this->sanitizeHeaderString($this->fromName); 00130 00131 $this->replyToName = ($valueList['replyto_name']) ? $valueList['replyto_name'] : $this->fromName; 00132 $this->replyToName = $this->sanitizeHeaderString($this->replyToName); 00133 00134 $this->organisation = ($valueList['organisation']) ? $valueList['organisation'] : ''; 00135 $this->organisation = $this->sanitizeHeaderString($this->organisation); 00136 00137 $this->fromAddress = ($valueList['from_email']) ? $valueList['from_email'] : ( 00138 ($valueList['email']) ? $valueList['email'] : '' 00139 ); 00140 if (!t3lib_div::validEmail($this->fromAddress)) { 00141 $this->fromAddress = t3lib_utility_Mail::getSystemFromAddress(); 00142 $this->fromName = t3lib_utility_Mail::getSystemFromName(); 00143 } 00144 00145 $this->replyToAddress = ($valueList['replyto_email']) ? $valueList['replyto_email'] : $this->fromAddress; 00146 00147 $this->priority = ($valueList['priority']) ? t3lib_div::intInRange($valueList['priority'], 1, 5) : 3; 00148 00149 // auto responder 00150 $this->autoRespondMessage = (trim($valueList['auto_respond_msg']) && $this->fromAddress) 00151 ? trim($valueList['auto_respond_msg']) 00152 : ''; 00153 00154 if ($this->autoRespondMessage !== '') { 00155 // Check if the value of the auto responder message has been modified with evil intentions 00156 $autoRespondChecksum = $valueList['auto_respond_checksum']; 00157 $correctHmacChecksum = t3lib_div::hmac($this->autoRespondMessage); 00158 if ($autoRespondChecksum !== $correctHmacChecksum) { 00159 t3lib_div::sysLog('Possible misuse of t3lib_formmail auto respond method. Subject: ' . $valueList['subject'], 00160 'Core', 00161 3); 00162 return; 00163 } else { 00164 $this->autoRespondMessage = $this->sanitizeHeaderString($this->autoRespondMessage); 00165 } 00166 } 00167 00168 $plainTextContent = ''; 00169 $htmlContent = '<table border="0" cellpadding="2" cellspacing="2">'; 00170 00171 // Runs through $V and generates the mail 00172 if (is_array($valueList)) { 00173 foreach ($valueList as $key => $val) { 00174 if (!t3lib_div::inList($this->reserved_names, $key)) { 00175 $space = (strlen($val) > 60) ? LF : ''; 00176 $val = (is_array($val) ? implode($val, LF) : $val); 00177 00178 // convert form data from renderCharset to mail charset (HTML may use entities) 00179 $plainTextValue = $val; 00180 $HtmlValue = htmlspecialchars($val); 00181 00182 $plainTextContent .= strtoupper($key) . ': ' . $space . $plainTextValue . LF . $space; 00183 $htmlContent .= '<tr><td bgcolor="#eeeeee"><font face="Verdana" size="1"><strong>' . strtoupper($key) 00184 . '</strong></font></td><td bgcolor="#eeeeee"><font face="Verdana" size="1">' . nl2br($HtmlValue) 00185 . ' </font></td></tr>'; 00186 } 00187 } 00188 } 00189 $htmlContent .= '</table>'; 00190 00191 $this->plainContent = $plainTextContent; 00192 00193 if ($valueList['html_enabled']) { 00194 $this->mailMessage->setBody($htmlContent, 'text/html'); 00195 $this->mailMessage->addPart($plainTextContent, 'text/plain'); 00196 } else { 00197 $this->mailMessage->setBody($plainTextContent, 'text/plain'); 00198 } 00199 00200 for ($a = 0; $a < 10; $a++) { 00201 $variableName = 'attachment' . (($a) ? $a : ''); 00202 if (!isset($_FILES[$variableName])) { 00203 continue; 00204 } 00205 if (!is_uploaded_file($_FILES[$variableName]['tmp_name'])) { 00206 t3lib_div::sysLog('Possible abuse of t3lib_formmail: temporary file "' . $_FILES[$variableName]['tmp_name'] 00207 . '" ("' . $_FILES[$variableName]['name'] . '") was not an uploaded file.', 'Core', 3); 00208 } 00209 if ($_FILES[$variableName]['tmp_name']['error'] !== UPLOAD_ERR_OK) { 00210 t3lib_div::sysLog('Error in uploaded file in t3lib_formmail: temporary file "' 00211 . $_FILES[$variableName]['tmp_name'] . '" ("' . $_FILES[$variableName]['name'] . '") Error code: ' 00212 . $_FILES[$variableName]['tmp_name']['error'], 'Core', 3); 00213 } 00214 $theFile = t3lib_div::upload_to_tempfile($_FILES[$variableName]['tmp_name']); 00215 $theName = $_FILES[$variableName]['name']; 00216 00217 if ($theFile && file_exists($theFile)) { 00218 if (filesize($theFile) < $GLOBALS['TYPO3_CONF_VARS']['FE']['formmailMaxAttachmentSize']) { 00219 $this->mailMessage->attach(Swift_Attachment::fromPath($theFile)->setFilename($theName)); 00220 } 00221 } 00222 $this->temporaryFiles[] = $theFile; 00223 } 00224 00225 $from = $this->fromName ? array($this->fromAddress => $this->fromName) : array($this->fromAddress); 00226 $this->recipient = $this->parseAddresses($valueList['recipient']); 00227 $this->mailMessage->setSubject($this->subject) 00228 ->setFrom($from) 00229 ->setTo($this->recipient) 00230 ->setPriority($this->priority); 00231 $replyTo = $this->replyToName ? array($this->replyToAddress => $this->replyToName) : array($this->replyToAddress); 00232 $this->mailMessage->addReplyTo($replyTo); 00233 $this->mailMessage->getHeaders()->addTextHeader('Organization', $this->organisation); 00234 if ($valueList['recipient_copy']) { 00235 $this->mailMessage->addCc($this->parseAddresses($valueList['recipient_copy'])); 00236 } 00237 if ($this->characterSet) { 00238 $this->mailMessage->setCharset($this->characterSet); 00239 } 00240 // Ignore target encoding. This is handled automatically by Swift Mailer and overriding the defaults 00241 // is not worth the trouble 00242 00243 // log dirty header lines 00244 if ($this->dirtyHeaders) { 00245 t3lib_div::sysLog('Possible misuse of t3lib_formmail: see TYPO3 devLog', 'Core', 3); 00246 if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['enable_DLOG']) { 00247 t3lib_div::devLog('t3lib_formmail: ' . t3lib_div::arrayToLogString($this->dirtyHeaders, '', 200), 'Core', 3); 00248 } 00249 } 00250 } 00251 } 00252 00253 /** 00254 * Checks string for suspicious characters 00255 * 00256 * @param string String to check 00257 * @return string Valid or empty string 00258 */ 00259 protected function sanitizeHeaderString($string) { 00260 $pattern = '/[\r\n\f\e]/'; 00261 if (preg_match($pattern, $string) > 0) { 00262 $this->dirtyHeaders[] = $string; 00263 $string = ''; 00264 } 00265 return $string; 00266 } 00267 00268 /** 00269 * Parses mailbox headers and turns them into an array. 00270 * 00271 * Mailbox headers are a comma separated list of 'name <email@example.org' combinations or plain email addresses (or a mix 00272 * of these). 00273 * The resulting array has key-value pairs where the key is either a number (no display name in the mailbox header) and the 00274 * value is the email address, or the key is the email address and the value is the display name. 00275 * 00276 * @param string $rawAddresses Comma separated list of email addresses (optionally with display name) 00277 * @return array Parsed list of addresses. 00278 */ 00279 protected function parseAddresses($rawAddresses = '') { 00280 /** @var $addressParser t3lib_mail_Rfc822AddressesParser */ 00281 $addressParser = t3lib_div::makeInstance('t3lib_mail_Rfc822AddressesParser', $rawAddresses); 00282 $addresses = $addressParser->parseAddressList(); 00283 $addressList = array(); 00284 foreach ($addresses as $address) { 00285 if ($address->personal) { 00286 // item with name found ( name <email@example.org> ) 00287 $addressList[$address->mailbox . '@' . $address->host] = $address->personal; 00288 } else { 00289 // item without name found ( email@example.org ) 00290 $addressList[] = $address->mailbox . '@' . $address->host; 00291 } 00292 } 00293 return $addressList; 00294 } 00295 00296 /** 00297 * Sends the actual mail and handles autorespond message 00298 * 00299 * @return boolean 00300 */ 00301 public function sendTheMail() { 00302 00303 // Sending the mail requires the recipient and message to be set. 00304 if (!$this->mailMessage->getTo() || !trim($this->mailMessage->getBody())) { 00305 return FALSE; 00306 } 00307 00308 $this->mailMessage->send(); 00309 00310 // Auto response 00311 if ($this->autoRespondMessage) { 00312 $theParts = explode('/', $this->autoRespondMessage, 2); 00313 $theParts[0] = str_replace('###SUBJECT###', $this->subject, $theParts[0]); 00314 $theParts[1] = str_replace("/", LF, $theParts[1]); 00315 $theParts[1] = str_replace("###MESSAGE###", $this->plainContent, $theParts[1]); 00316 00317 /** @var $autoRespondMail t3lib_mail_Message */ 00318 $autoRespondMail = t3lib_div::makeInstance('t3lib_mail_Message'); 00319 $autoRespondMail->setTo($this->fromAddress) 00320 ->setSubject($theParts[0]) 00321 ->setFrom($this->recipient) 00322 ->setBody($theParts[1]); 00323 $autoRespondMail->send(); 00324 } 00325 return $this->mailMessage->isSent(); 00326 } 00327 00328 /** 00329 * Do some cleanup at the end (deleting attachment files) 00330 */ 00331 public function __destruct() { 00332 foreach ($this->temporaryFiles as $file) { 00333 t3lib_div::unlink_tempfile($file); 00334 } 00335 } 00336 } 00337 00338 00339 if (defined('TYPO3_MODE') && isset($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_formmail.php'])) { 00340 include_once($GLOBALS['TYPO3_CONF_VARS'][TYPO3_MODE]['XCLASS']['t3lib/class.t3lib_formmail.php']); 00341 } 00342 00343 ?>
1.8.0