00001 <?php
00026 require_once 'Zend/Mime.php';
00027
00031 require_once 'Zend/Mime/Part.php';
00032
00033
00040 class Zend_Mime_Message
00041 {
00042
00043 protected $_parts = array();
00044 protected $_mime = null;
00045
00051 public function getParts()
00052 {
00053 return $this->_parts;
00054 }
00055
00061 public function setParts($parts)
00062 {
00063 $this->_parts = $parts;
00064 }
00065
00071 public function addPart(Zend_Mime_Part $part)
00072 {
00076 $this->_parts[] = $part;
00077 }
00078
00085 public function isMultiPart()
00086 {
00087 return (count($this->_parts) > 1);
00088 }
00089
00098 public function setMime(Zend_Mime $mime)
00099 {
00100 $this->_mime = $mime;
00101 }
00102
00111 public function getMime()
00112 {
00113 if ($this->_mime === null) {
00114 $this->_mime = new Zend_Mime();
00115 }
00116
00117 return $this->_mime;
00118 }
00119
00135 public function generateMessage($EOL = Zend_Mime::LINEEND)
00136 {
00137 if (! $this->isMultiPart()) {
00138 $body = array_shift($this->_parts);
00139 $body = $body->getContent($EOL);
00140 } else {
00141 $mime = $this->getMime();
00142
00143 $boundaryLine = $mime->boundaryLine($EOL);
00144 $body = 'This is a message in Mime Format. If you see this, '
00145 . "your mail reader does not support this format." . $EOL;
00146
00147 foreach (array_keys($this->_parts) as $p) {
00148 $body .= $boundaryLine
00149 . $this->getPartHeaders($p, $EOL)
00150 . $EOL
00151 . $this->getPartContent($p, $EOL);
00152 }
00153
00154 $body .= $mime->mimeEnd($EOL);
00155 }
00156
00157 return trim($body);
00158 }
00159
00166 public function getPartHeadersArray($partnum)
00167 {
00168 return $this->_parts[$partnum]->getHeadersArray();
00169 }
00170
00177 public function getPartHeaders($partnum, $EOL = Zend_Mime::LINEEND)
00178 {
00179 return $this->_parts[$partnum]->getHeaders($EOL);
00180 }
00181
00188 public function getPartContent($partnum, $EOL = Zend_Mime::LINEEND)
00189 {
00190 return $this->_parts[$partnum]->getContent($EOL);
00191 }
00192
00202 protected static function _disassembleMime($body, $boundary)
00203 {
00204 $start = 0;
00205 $res = array();
00206
00207
00208
00209 $p = strpos($body, '--'.$boundary."\n", $start);
00210 if ($p === false) {
00211
00212 return array();
00213 }
00214
00215
00216 $start = $p + 3 + strlen($boundary);
00217
00218 while (($p = strpos($body, '--' . $boundary . "\n", $start)) !== false) {
00219 $res[] = substr($body, $start, $p-$start);
00220 $start = $p + 3 + strlen($boundary);
00221 }
00222
00223
00224 $p = strpos($body, '--' . $boundary . '--', $start);
00225 if ($p===false) {
00226 throw new Zend_Exception('Not a valid Mime Message: End Missing');
00227 }
00228
00229
00230 $res[] = substr($body, $start, $p-$start);
00231 return $res;
00232 }
00233
00243 public static function createFromMessage($message, $boundary, $EOL = Zend_Mime::LINEEND)
00244 {
00245 require_once 'Zend/Mime/Decode.php';
00246 $parts = Zend_Mime_Decode::splitMessageStruct($message, $boundary, $EOL);
00247
00248 $res = new self();
00249 foreach ($parts as $part) {
00250
00251 $newPart = new Zend_Mime_Part($part['body']);
00252 foreach ($part['header'] as $key => $value) {
00256 switch(strtolower($key)) {
00257 case 'content-type':
00258 $newPart->type = $value;
00259 break;
00260 case 'content-transfer-encoding':
00261 $newPart->encoding = $value;
00262 break;
00263 case 'content-id':
00264 $newPart->id = trim($value,'<>');
00265 break;
00266 case 'content-disposition':
00267 $newPart->disposition = $value;
00268 break;
00269 case 'content-description':
00270 $newPart->description = $value;
00271 break;
00272 case 'content-location':
00273 $newPart->location = $value;
00274 break;
00275 case 'content-language':
00276 $newPart->language = $value;
00277 break;
00278 default:
00279 throw new Zend_Exception('Unknown header ignored for MimePart:' . $key);
00280 }
00281 }
00282 $res->addPart($newPart);
00283 }
00284 return $res;
00285 }
00286 }