Adding Mail::_encodeHeaderValue()
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 7 Jul 2007 23:36:24 +0000 (23:36 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 7 Jul 2007 23:36:24 +0000 (23:36 +0000)
mail.php

index 07f5a115c1073badec59aa4c1f7c989236f6b1ae..a97b62403eefb7ef21f1dbacf6316edaf8ca4330 100644 (file)
--- a/mail.php
+++ b/mail.php
@@ -253,6 +253,7 @@ class Mail
                $address = trim($this->registry->unsanitize($address));
                $name = $this->_fetch_first_line($name);
                $name = trim($this->registry->unsanitize($name));
+               $name = $this->_encodeHeaderValue($name);
                $tostring = ($name == null ?  $address : "\"$name\" <$address>");
                
                // sanitize the from field
@@ -262,10 +263,12 @@ class Mail
                // sanitize the from name
                $this->fromname = $this->_fetch_first_line($this->fromname);
                $this->fromname = ($this->fromname == '' ? $this->from : trim($this->registry->unsanitize($this->fromname)));
+               $this->fromname = $this->_encodeHeaderValue($this->fromname);
                
                // sanitize the subject
                $this->subject = $this->_fetch_first_line($this->subject);
                $this->subject = trim($this->registry->unsanitize($this->subject));
+               $this->subject = $this->_encodeHeaderValue($this->subject);
                
                // sanitize the body
                $this->bodytext = $this->registry->modules['functions']->convert_line_breaks($this->bodytext, $this->delim);
@@ -339,6 +342,37 @@ class Mail
                $broken = explode("\n", $string);
                return $broken[0];
        }
+       
+       // ###################################################################
+       /**
+       * Encodes a header value (to name, fron name, subject, etc.) according
+       * to RFC 2047
+       *
+       * @param        string  The text to encode
+       *
+       * @return       string  Encoded text
+       */
+       function _encodeHeaderValue($text)
+       {
+               $enc = '';
+               
+               for ($i = 0; $i < strlen($text); $i++)
+               {
+                       $char = ord($text[$i]);
+                       // allowed characters: a-zA-Z0-9_\+\-\*_=
+                       if (($char >= 65 AND $char <= 90) OR ($char >= 97 AND $char <= 122) OR ($char >= 48 AND $char <= 57) OR $char == 33 OR $char == 42 OR $char == 43 OR $char == 45 OR $char == 47 OR $char == 61)
+                       {
+                               $enc .= $text[$i];
+                       }
+                       else
+                       {
+                               // this is a non-ASCII character, so encode it
+                               $enc .= '=' . strtoupper(dechex($char));
+                       }
+               }
+               
+               return '=?' . $this->charset . '?q?' . $enc . '?=';
+       }
 }
 
 /*=====================================================================*\