web/lib/Zend/Mime/Message.php
changeset 1230 68c69c656a2c
parent 807 877f952ae2bd
equal deleted inserted replaced
1229:5a6b6e770365 1230:68c69c656a2c
    12  * obtain it through the world-wide-web, please send an email
    12  * obtain it through the world-wide-web, please send an email
    13  * to license@zend.com so we can send you a copy immediately.
    13  * to license@zend.com so we can send you a copy immediately.
    14  *
    14  *
    15  * @category   Zend
    15  * @category   Zend
    16  * @package    Zend_Mime
    16  * @package    Zend_Mime
    17  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    19  * @version    $Id: Message.php 24593 2012-01-05 20:35:02Z matthew $
    19  * @version    $Id$
    20  */
    20  */
    21 
       
    22 
    21 
    23 /**
    22 /**
    24  * Zend_Mime
    23  * Zend_Mime
    25  */
    24  */
    26 require_once 'Zend/Mime.php';
    25 require_once 'Zend/Mime.php';
    28 /**
    27 /**
    29  * Zend_Mime_Part
    28  * Zend_Mime_Part
    30  */
    29  */
    31 require_once 'Zend/Mime/Part.php';
    30 require_once 'Zend/Mime/Part.php';
    32 
    31 
    33 
       
    34 /**
    32 /**
    35  * @category   Zend
    33  * @category   Zend
    36  * @package    Zend_Mime
    34  * @package    Zend_Mime
    37  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    35  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    36  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    39  */
    37  */
    40 class Zend_Mime_Message
    38 class Zend_Mime_Message
    41 {
    39 {
    42 
    40     /**
       
    41      * The Zend_Mime_Parts of the message
       
    42      *
       
    43      * @var array
       
    44      */
    43     protected $_parts = array();
    45     protected $_parts = array();
       
    46 
       
    47     /**
       
    48      * The Zend_Mime object for the message
       
    49      *
       
    50      * @var Zend_Mime|null
       
    51      */
    44     protected $_mime = null;
    52     protected $_mime = null;
    45 
    53 
    46     /**
    54     /**
    47      * Returns the list of all Zend_Mime_Parts in the message
    55      * Returns the list of all Zend_Mime_Parts in the message
    48      *
    56      *
   132      * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
   140      * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
   133      * @return string
   141      * @return string
   134      */
   142      */
   135     public function generateMessage($EOL = Zend_Mime::LINEEND)
   143     public function generateMessage($EOL = Zend_Mime::LINEEND)
   136     {
   144     {
   137         if (! $this->isMultiPart()) {
   145         if (!$this->isMultiPart()) {
   138             $body = array_shift($this->_parts);
   146             $body = array_shift($this->_parts);
   139             $body = $body->getContent($EOL);
   147             $body = $body->getContent($EOL);
   140         } else {
   148         } else {
   141             $mime = $this->getMime();
   149             $mime = $this->getMime();
   142 
   150 
   144             $body = 'This is a message in Mime Format.  If you see this, '
   152             $body = 'This is a message in Mime Format.  If you see this, '
   145                   . "your mail reader does not support this format." . $EOL;
   153                   . "your mail reader does not support this format." . $EOL;
   146 
   154 
   147             foreach (array_keys($this->_parts) as $p) {
   155             foreach (array_keys($this->_parts) as $p) {
   148                 $body .= $boundaryLine
   156                 $body .= $boundaryLine
   149                        . $this->getPartHeaders($p, $EOL)
   157                          . $this->getPartHeaders($p, $EOL)
   150                        . $EOL
   158                          . $EOL
   151                        . $this->getPartContent($p, $EOL);
   159                          . $this->getPartContent($p, $EOL);
   152             }
   160             }
   153 
   161 
   154             $body .= $mime->mimeEnd($EOL);
   162             $body .= $mime->mimeEnd($EOL);
   155         }
   163         }
   156 
   164 
   169     }
   177     }
   170 
   178 
   171     /**
   179     /**
   172      * Get the headers of a given part as a string
   180      * Get the headers of a given part as a string
   173      *
   181      *
   174      * @param int $partnum
   182      * @param  int    $partnum
       
   183      * @param  string $EOL
   175      * @return string
   184      * @return string
   176      */
   185      */
   177     public function getPartHeaders($partnum, $EOL = Zend_Mime::LINEEND)
   186     public function getPartHeaders($partnum, $EOL = Zend_Mime::LINEEND)
   178     {
   187     {
   179         return $this->_parts[$partnum]->getHeaders($EOL);
   188         return $this->_parts[$partnum]->getHeaders($EOL);
   180     }
   189     }
   181 
   190 
   182     /**
   191     /**
   183      * Get the (encoded) content of a given part as a string
   192      * Get the (encoded) content of a given part as a string
   184      *
   193      *
   185      * @param int $partnum
   194      * @param  int    $partnum
       
   195      * @param  string $EOL
   186      * @return string
   196      * @return string
   187      */
   197      */
   188     public function getPartContent($partnum, $EOL = Zend_Mime::LINEEND)
   198     public function getPartContent($partnum, $EOL = Zend_Mime::LINEEND)
   189     {
   199     {
   190         return $this->_parts[$partnum]->getContent($EOL);
   200         return $this->_parts[$partnum]->getContent($EOL);
   193     /**
   203     /**
   194      * Explode MIME multipart string into seperate parts
   204      * Explode MIME multipart string into seperate parts
   195      *
   205      *
   196      * Parts consist of the header and the body of each MIME part.
   206      * Parts consist of the header and the body of each MIME part.
   197      *
   207      *
   198      * @param string $body
   208      * @param  string $body
   199      * @param string $boundary
   209      * @param  string $boundary
       
   210      * @throws Zend_Exception
   200      * @return array
   211      * @return array
   201      */
   212      */
   202     protected static function _disassembleMime($body, $boundary)
   213     protected static function _disassembleMime($body, $boundary)
   203     {
   214     {
   204         $start = 0;
   215         $start = 0;
   205         $res = array();
   216         $res   = array();
   206         // find every mime part limiter and cut out the
   217         // find every mime part limiter and cut out the
   207         // string before it.
   218         // string before it.
   208         // the part before the first boundary string is discarded:
   219         // the part before the first boundary string is discarded:
   209         $p = strpos($body, '--'.$boundary."\n", $start);
   220         $p = strpos($body, '--' . $boundary . "\n", $start);
   210         if ($p === false) {
   221         if ($p === false) {
   211             // no parts found!
   222             // no parts found!
   212             return array();
   223             return array();
   213         }
   224         }
   214 
   225 
   215         // position after first boundary line
   226         // position after first boundary line
   216         $start = $p + 3 + strlen($boundary);
   227         $start = $p + 3 + strlen($boundary);
   217 
   228 
   218         while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
   229         while (($p = strpos($body, '--' . $boundary . "\n", $start))
   219             $res[] = substr($body, $start, $p-$start);
   230                !== false) {
       
   231             $res[] = substr($body, $start, $p - $start);
   220             $start = $p + 3 + strlen($boundary);
   232             $start = $p + 3 + strlen($boundary);
   221         }
   233         }
   222 
   234 
   223         // no more parts, find end boundary
   235         // no more parts, find end boundary
   224         $p = strpos($body, '--' . $boundary . '--', $start);
   236         $p = strpos($body, '--' . $boundary . '--', $start);
   225         if ($p===false) {
   237         if ($p === false) {
   226             throw new Zend_Exception('Not a valid Mime Message: End Missing');
   238             throw new Zend_Exception('Not a valid Mime Message: End Missing');
   227         }
   239         }
   228 
   240 
   229         // the remaining part also needs to be parsed:
   241         // the remaining part also needs to be parsed:
   230         $res[] = substr($body, $start, $p-$start);
   242         $res[] = substr($body, $start, $p - $start);
       
   243 
   231         return $res;
   244         return $res;
   232     }
   245     }
   233 
   246 
   234     /**
   247     /**
   235      * Decodes a MIME encoded string and returns a Zend_Mime_Message object with
   248      * Decodes a MIME encoded string and returns a Zend_Mime_Message object with
   236      * all the MIME parts set according to the given string
   249      * all the MIME parts set according to the given string
   237      *
   250      *
   238      * @param string $message
   251      * @param  string $message
   239      * @param string $boundary
   252      * @param  string $boundary
   240      * @param string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
   253      * @param  string $EOL EOL string; defaults to {@link Zend_Mime::LINEEND}
       
   254      * @throws Zend_Exception
   241      * @return Zend_Mime_Message
   255      * @return Zend_Mime_Message
   242      */
   256      */
   243     public static function createFromMessage($message, $boundary, $EOL = Zend_Mime::LINEEND)
   257     public static function createFromMessage(
       
   258         $message, $boundary, $EOL = Zend_Mime::LINEEND
       
   259     )
   244     {
   260     {
   245         require_once 'Zend/Mime/Decode.php';
   261         require_once 'Zend/Mime/Decode.php';
   246         $parts = Zend_Mime_Decode::splitMessageStruct($message, $boundary, $EOL);
   262         $parts = Zend_Mime_Decode::splitMessageStruct($message, $boundary, $EOL);
   247 
   263 
   248         $res = new self();
   264         $res = new self();
   251             $newPart = new Zend_Mime_Part($part['body']);
   267             $newPart = new Zend_Mime_Part($part['body']);
   252             foreach ($part['header'] as $key => $value) {
   268             foreach ($part['header'] as $key => $value) {
   253                 /**
   269                 /**
   254                  * @todo check for characterset and filename
   270                  * @todo check for characterset and filename
   255                  */
   271                  */
   256                 switch(strtolower($key)) {
   272                 switch (strtolower($key)) {
   257                     case 'content-type':
   273                     case 'content-type':
   258                         $newPart->type = $value;
   274                         $newPart->type = $value;
   259                         break;
   275                         break;
   260                     case 'content-transfer-encoding':
   276                     case 'content-transfer-encoding':
   261                         $newPart->encoding = $value;
   277                         $newPart->encoding = $value;
   262                         break;
   278                         break;
   263                     case 'content-id':
   279                     case 'content-id':
   264                         $newPart->id = trim($value,'<>');
   280                         $newPart->id = trim($value, '<>');
   265                         break;
   281                         break;
   266                     case 'content-disposition':
   282                     case 'content-disposition':
   267                         $newPart->disposition = $value;
   283                         $newPart->disposition = $value;
   268                         break;
   284                         break;
   269                     case 'content-description':
   285                     case 'content-description':
   274                         break;
   290                         break;
   275                     case 'content-language':
   291                     case 'content-language':
   276                         $newPart->language = $value;
   292                         $newPart->language = $value;
   277                         break;
   293                         break;
   278                     default:
   294                     default:
   279                         throw new Zend_Exception('Unknown header ignored for MimePart:' . $key);
   295                         throw new Zend_Exception(
       
   296                             'Unknown header ignored for MimePart:' . $key
       
   297                         );
   280                 }
   298                 }
   281             }
   299             }
   282             $res->addPart($newPart);
   300             $res->addPart($newPart);
   283         }
   301         }
       
   302 
   284         return $res;
   303         return $res;
   285     }
   304     }
   286 }
   305 }