Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Message.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Mime.php';
26 
30 #require_once 'Zend/Mime/Part.php';
31 
39 {
45  protected $_parts = array();
46 
52  protected $_mime = null;
53 
59  public function getParts()
60  {
61  return $this->_parts;
62  }
63 
69  public function setParts($parts)
70  {
71  $this->_parts = $parts;
72  }
73 
79  public function addPart(Zend_Mime_Part $part)
80  {
84  $this->_parts[] = $part;
85  }
86 
93  public function isMultiPart()
94  {
95  return (count($this->_parts) > 1);
96  }
97 
106  public function setMime(Zend_Mime $mime)
107  {
108  $this->_mime = $mime;
109  }
110 
119  public function getMime()
120  {
121  if ($this->_mime === null) {
122  $this->_mime = new Zend_Mime();
123  }
124 
125  return $this->_mime;
126  }
127 
143  public function generateMessage($EOL = Zend_Mime::LINEEND)
144  {
145  if (!$this->isMultiPart()) {
146  $body = array_shift($this->_parts);
147  $body = $body->getContent($EOL);
148  } else {
149  $mime = $this->getMime();
150 
151  $boundaryLine = $mime->boundaryLine($EOL);
152  $body = 'This is a message in Mime Format. If you see this, '
153  . "your mail reader does not support this format." . $EOL;
154 
155  foreach (array_keys($this->_parts) as $p) {
156  $body .= $boundaryLine
157  . $this->getPartHeaders($p, $EOL)
158  . $EOL
159  . $this->getPartContent($p, $EOL);
160  }
161 
162  $body .= $mime->mimeEnd($EOL);
163  }
164 
165  return trim($body);
166  }
167 
174  public function getPartHeadersArray($partnum)
175  {
176  return $this->_parts[$partnum]->getHeadersArray();
177  }
178 
186  public function getPartHeaders($partnum, $EOL = Zend_Mime::LINEEND)
187  {
188  return $this->_parts[$partnum]->getHeaders($EOL);
189  }
190 
198  public function getPartContent($partnum, $EOL = Zend_Mime::LINEEND)
199  {
200  return $this->_parts[$partnum]->getContent($EOL);
201  }
202 
213  protected static function _disassembleMime($body, $boundary)
214  {
215  $start = 0;
216  $res = array();
217  // find every mime part limiter and cut out the
218  // string before it.
219  // the part before the first boundary string is discarded:
220  $p = strpos($body, '--' . $boundary . "\n", $start);
221  if ($p === false) {
222  // no parts found!
223  return array();
224  }
225 
226  // position after first boundary line
227  $start = $p + 3 + strlen($boundary);
228 
229  while (($p = strpos($body, '--' . $boundary . "\n", $start))
230  !== false) {
231  $res[] = substr($body, $start, $p - $start);
232  $start = $p + 3 + strlen($boundary);
233  }
234 
235  // no more parts, find end boundary
236  $p = strpos($body, '--' . $boundary . '--', $start);
237  if ($p === false) {
238  throw new Zend_Exception('Not a valid Mime Message: End Missing');
239  }
240 
241  // the remaining part also needs to be parsed:
242  $res[] = substr($body, $start, $p - $start);
243 
244  return $res;
245  }
246 
257  public static function createFromMessage(
258  $message, $boundary, $EOL = Zend_Mime::LINEEND
259  )
260  {
261  #require_once 'Zend/Mime/Decode.php';
262  $parts = Zend_Mime_Decode::splitMessageStruct($message, $boundary, $EOL);
263 
264  $res = new self();
265  foreach ($parts as $part) {
266  // now we build a new MimePart for the current Message Part:
267  $newPart = new Zend_Mime_Part($part['body']);
268  foreach ($part['header'] as $key => $value) {
272  switch (strtolower($key)) {
273  case 'content-type':
274  $newPart->type = $value;
275  break;
276  case 'content-transfer-encoding':
277  $newPart->encoding = $value;
278  break;
279  case 'content-id':
280  $newPart->id = trim($value, '<>');
281  break;
282  case 'content-disposition':
283  $newPart->disposition = $value;
284  break;
285  case 'content-description':
286  $newPart->description = $value;
287  break;
288  case 'content-location':
289  $newPart->location = $value;
290  break;
291  case 'content-language':
292  $newPart->language = $value;
293  break;
294  default:
295  throw new Zend_Exception(
296  'Unknown header ignored for MimePart:' . $key
297  );
298  }
299  }
300  $res->addPart($newPart);
301  }
302 
303  return $res;
304  }
305 }
setMime(Zend_Mime $mime)
Definition: Message.php:106
addPart(Zend_Mime_Part $part)
Definition: Message.php:79
$start
Definition: listing.phtml:18
getPartHeadersArray($partnum)
Definition: Message.php:174
$message
getPartHeaders($partnum, $EOL=Zend_Mime::LINEEND)
Definition: Message.php:186
const LINEEND
Definition: Mime.php:42
static splitMessageStruct( $message, $boundary, $EOL=Zend_Mime::LINEEND)
Definition: Decode.php:91
setParts($parts)
Definition: Message.php:69
$value
Definition: gender.phtml:16
getPartContent($partnum, $EOL=Zend_Mime::LINEEND)
Definition: Message.php:198
static _disassembleMime($body, $boundary)
Definition: Message.php:213
generateMessage($EOL=Zend_Mime::LINEEND)
Definition: Message.php:143
static createFromMessage( $message, $boundary, $EOL=Zend_Mime::LINEEND)
Definition: Message.php:257