Updating version number to 4.2-dev after release of 4.2.0beta2a
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_formmail.php
index 992863c..6d44718 100644 (file)
@@ -2,7 +2,7 @@
 /***************************************************************
 *  Copyright notice
 *
-*  (c) 1999-2006 Kasper Skaarhoj (kasperYYYY@typo3.com)
+*  (c) 1999-2007 Kasper Skaarhoj (kasperYYYY@typo3.com)
 *  All rights reserved
 *
 *  This script is part of the TYPO3 project. The TYPO3 project is
@@ -68,6 +68,7 @@
  */
 class t3lib_formmail extends t3lib_htmlmail {
        var $reserved_names = 'recipient,recipient_copy,auto_respond_msg,redirect,subject,attachment,from_email,from_name,replyto_email,replyto_name,organisation,priority,html_enabled,quoted_printable,submit_x,submit_y';
+       var $dirtyHeaders = array();    // collection of suspicious header data, used for logging
 
 
        /**
@@ -75,13 +76,13 @@ class t3lib_formmail extends t3lib_htmlmail {
         * This class is able to generate a mail in formmail-style from the data in $V
         * Fields:
         *
-        * [recipient]:         email-adress of the one to receive the mail. If array, then all values are expected to be recipients
+        * [recipient]:                 email-adress of the one to receive the mail. If array, then all values are expected to be recipients
         * [attachment]:                ....
         *
         * [subject]:                   The subject of the mail
         * [from_email]:                Sender email. If not set, [email] is used
-        * [from_name]:         Sender name. If not set, [name] is used
-        * [replyto_email]:     Reply-to email. If not set [from_email] is used
+        * [from_name]:                 Sender name. If not set, [name] is used
+        * [replyto_email]:             Reply-to email. If not set [from_email] is used
         * [replyto_name]:              Reply-to name. If not set [from_name] is used
         * [organisation]:              Organisation (header)
         * [priority]:                  Priority, 1-5, default 3
@@ -95,14 +96,13 @@ class t3lib_formmail extends t3lib_htmlmail {
        function start($V,$base64=false)        {
                $convCharset = FALSE;   // do we need to convert form data?
 
-               if ($GLOBALS['TSFE']->metaCharset != $GLOBALS['TSFE']->renderCharset)   {       // Use metaCharset for mail if different from renderCharset
-                       $this->charset = $GLOBALS['TSFE']->metaCharset;
-                       $convCharset = TRUE;
-               }
-
                if ($GLOBALS['TSFE']->config['config']['formMailCharset'])      {       // Respect formMailCharset if it was set
                        $this->charset = $GLOBALS['TSFE']->csConvObj->parse_charset($GLOBALS['TSFE']->config['config']['formMailCharset']);
                        $convCharset = TRUE;
+
+               } elseif ($GLOBALS['TSFE']->metaCharset != $GLOBALS['TSFE']->renderCharset)     {       // Use metaCharset for mail if different from renderCharset
+                       $this->charset = $GLOBALS['TSFE']->metaCharset;
+                       $convCharset = TRUE;
                }
 
                parent::start();
@@ -113,19 +113,28 @@ class t3lib_formmail extends t3lib_htmlmail {
                                // convert form data from renderCharset to mail charset
                        $val = ($V['subject']) ? $V['subject'] : 'Formmail on '.t3lib_div::getIndpEnv('HTTP_HOST');
                        $this->subject = ($convCharset && strlen($val)) ? $GLOBALS['TSFE']->csConvObj->conv($val,$GLOBALS['TSFE']->renderCharset,$this->charset) : $val;
-                       $val = ($V['from_name']) ? $V['from_name'] : (($V['name'])?$V['name']:'');
+                       $this->subject = $this->sanitizeHeaderString($this->subject);
+                       $val = ($V['from_name']) ? $V['from_name'] : (($V['name'])?$V['name']:'');      // Be careful when changing $val! It is used again as the fallback value for replyto_name
                        $this->from_name = ($convCharset && strlen($val)) ? $GLOBALS['TSFE']->csConvObj->conv($val,$GLOBALS['TSFE']->renderCharset,$this->charset) : $val;
-                       $val = ($V['replyto_name']) ? $V['replyto_name'] : $this->from_name;
+                       $this->from_name = $this->sanitizeHeaderString($this->from_name);
+                       $this->from_name = preg_match( '/\s|,/', $this->from_name ) >= 1 ? '"'.$this->from_name.'"' : $this->from_name;
+                       $val = ($V['replyto_name']) ? $V['replyto_name'] : $val;
                        $this->replyto_name = ($convCharset && strlen($val)) ? $GLOBALS['TSFE']->csConvObj->conv($val,$GLOBALS['TSFE']->renderCharset,$this->charset) : $val;
+                       $this->replyto_name = $this->sanitizeHeaderString($this->replyto_name);
+                       $this->replyto_name = preg_match( '/\s|,/', $this->replyto_name ) >= 1 ? '"'.$this->replyto_name.'"' : $this->replyto_name;
                        $val = ($V['organisation']) ? $V['organisation'] : '';
                        $this->organisation = ($convCharset && strlen($val)) ? $GLOBALS['TSFE']->csConvObj->conv($val,$GLOBALS['TSFE']->renderCharset,$this->charset) : $val;
+                       $this->organisation = $this->sanitizeHeaderString($this->organisation);
 
                        $this->from_email = ($V['from_email']) ? $V['from_email'] : (($V['email'])?$V['email']:'');
+                       $this->from_email = t3lib_div::validEmail($this->from_email) ? $this->from_email : '';
                        $this->replyto_email = ($V['replyto_email']) ? $V['replyto_email'] : $this->from_email;
+                       $this->replyto_email = t3lib_div::validEmail($this->replyto_email) ? $this->replyto_email : '';
                        $this->priority = ($V['priority']) ? t3lib_div::intInRange($V['priority'],1,5) : 3;
 
                                // Auto responder.
                        $this->auto_respond_msg = (trim($V['auto_respond_msg']) && $this->from_email) ? trim($V['auto_respond_msg']) : '';
+                       $this->auto_respond_msg = $this->sanitizeHeaderString($this->auto_respond_msg);
 
                        $Plain_content = '';
                        $HTML_content = '<table border="0" cellpadding="2" cellspacing="2">';
@@ -143,7 +152,7 @@ class t3lib_formmail extends t3lib_htmlmail {
                                                $HTML_val = ($convCharset && strlen($val)) ? $GLOBALS['TSFE']->csConvObj->conv(htmlspecialchars($val),$GLOBALS['TSFE']->renderCharset,$this->charset,1) : htmlspecialchars($val);
 
                                                $Plain_content.= strtoupper($key).':  '.$space.$Plain_val."\n".$space;
-                                               $HTML_content.='<tr><td bgcolor="#eeeeee"><font face="Verdana" size="1"><b>'.strtoupper($key).'</b></font></td><td bgcolor="#eeeeee"><font face="Verdana" size="1">'.nl2br($HTML_val).'&nbsp;</font></td></tr>';
+                                               $HTML_content.= '<tr><td bgcolor="#eeeeee"><font face="Verdana" size="1"><b>'.strtoupper($key).'</b></font></td><td bgcolor="#eeeeee"><font face="Verdana" size="1">'.nl2br($HTML_val).'&nbsp;</font></td></tr>';
                                        }
                                }
                        }
@@ -156,11 +165,15 @@ class t3lib_formmail extends t3lib_htmlmail {
 
                        for ($a=0;$a<10;$a++)   {
                                $varname = 'attachment'.(($a)?$a:'');
+                               if (!is_uploaded_file($_FILES[$varname]['tmp_name']))   {
+                                       t3lib_div::sysLog('Possible abuse of t3lib_formmail: temporary file "'.$_FILES[$varname]['tmp_name'].'" ("'.$_FILES[$varname]['name'].'") was not an uploaded file.', 'Core', 3);
+                                       continue;
+                               }
                                $theFile = t3lib_div::upload_to_tempfile($_FILES[$varname]['tmp_name']);
                                $theName = $_FILES[$varname]['name'];
 
                                if ($theFile && @file_exists($theFile)) {
-                                       if (filesize($theFile) < 250000)        {
+                                       if (filesize($theFile) < $GLOBALS['TYPO3_CONF_VARS']['FE']['formmailMaxAttachmentSize'])        {
                                                $this->addAttachment($theFile, $theName);
                                        }
                                }
@@ -173,6 +186,13 @@ class t3lib_formmail extends t3lib_htmlmail {
                        if ($V['recipient_copy'])       {
                                $this->recipient_copy = trim($V['recipient_copy']);
                        }
+                               // log dirty header lines
+                       if ($this->dirtyHeaders)        {
+                               t3lib_div::sysLog( 'Possible misuse of t3lib_formmail: see TYPO3 devLog', 'Core', 3 );
+                               if ($GLOBALS['TYPO3_CONF_VARS']['SYS']['enable_DLOG'])  {
+                                       t3lib_div::devLog( 't3lib_formmail: '. t3lib_div::arrayToLogString($this->dirtyHeaders, '', 200 ), 'Core', 3 );
+                               }
+                       }
                }
        }
 
@@ -201,6 +221,22 @@ class t3lib_formmail extends t3lib_htmlmail {
                        return true;
                } else { return false;}
        }
+
+
+       /**
+        * Checks string for suspicious characters
+        *
+        * @param       string  String to check
+        * @return      string  Valid or empty string
+        */
+       function sanitizeHeaderString ($string) {
+               $pattern = '/[\r\n\f\e]/';
+               if (preg_match($pattern, $string) > 0)  {
+                       $this->dirtyHeaders[] = $string;
+                       $string = '';
+               }
+               return $string;
+       }
 }