Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Part.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Mime/Decode.php';
27 
31 #require_once 'Zend/Mail/Header/HeaderName.php';
32 
36 #require_once 'Zend/Mail/Header/HeaderValue.php';
37 
41 #require_once 'Zend/Mail/Part/Interface.php';
42 
43 
50 class Zend_Mail_Part implements RecursiveIterator, Zend_Mail_Part_Interface
51 {
56  protected $_headers;
57 
62  protected $_content;
63 
68  protected $_topLines = '';
69 
74  protected $_parts = array();
75 
80  protected $_countParts;
81 
86  protected $_iterationPos = 1;
87 
92  protected $_mail;
93 
98  protected $_messageNum = 0;
99 
104  protected $_partClass;
105 
120  public function __construct(array $params)
121  {
122  if (isset($params['handler'])) {
123  if (!$params['handler'] instanceof Zend_Mail_Storage_Abstract) {
127  #require_once 'Zend/Mail/Exception.php';
128  throw new Zend_Mail_Exception('handler is not a valid mail handler');
129  }
130  if (!isset($params['id'])) {
134  #require_once 'Zend/Mail/Exception.php';
135  throw new Zend_Mail_Exception('need a message id with a handler');
136  }
137 
138  $this->_mail = $params['handler'];
139  $this->_messageNum = $params['id'];
140  }
141 
142  if (isset($params['partclass'])) {
143  $this->setPartClass($params['partclass']);
144  }
145 
146  if (isset($params['raw'])) {
147  Zend_Mime_Decode::splitMessage($params['raw'], $this->_headers, $this->_content, "\r\n");
148  } else if (isset($params['headers'])) {
149  if (is_array($params['headers'])) {
150  $this->_headers = $params['headers'];
151  $this->_validateHeaders($this->_headers);
152  } else {
153  if (!empty($params['noToplines'])) {
154  Zend_Mime_Decode::splitMessage($params['headers'], $this->_headers, $null, "\r\n");
155  } else {
156  Zend_Mime_Decode::splitMessage($params['headers'], $this->_headers, $this->_topLines, "\r\n");
157  }
158  }
159 
160  if (isset($params['content'])) {
161  $this->_content = $params['content'];
162  }
163  }
164  }
165 
171  public function setPartClass($class)
172  {
173  if ( !class_exists($class) ) {
177  #require_once 'Zend/Mail/Exception.php';
178  throw new Zend_Mail_Exception("Class '{$class}' does not exist");
179  }
180  if ( !is_subclass_of($class, 'Zend_Mail_Part_Interface') ) {
184  #require_once 'Zend/Mail/Exception.php';
185  throw new Zend_Mail_Exception("Class '{$class}' must implement Zend_Mail_Part_Interface");
186  }
187 
188  $this->_partClass = $class;
189  return $this;
190  }
191 
196  public function getPartClass()
197  {
198  if ( !$this->_partClass ) {
199  $this->_partClass = __CLASS__;
200  }
201  return $this->_partClass;
202  }
203 
209  public function isMultipart()
210  {
211  try {
212  return stripos($this->contentType, 'multipart/') === 0;
213  } catch(Zend_Mail_Exception $e) {
214  return false;
215  }
216  }
217 
218 
227  public function getContent()
228  {
229  if ($this->_content !== null) {
230  return $this->_content;
231  }
232 
233  if ($this->_mail) {
234  return $this->_mail->getRawContent($this->_messageNum);
235  } else {
239  #require_once 'Zend/Mail/Exception.php';
240  throw new Zend_Mail_Exception('no content');
241  }
242  }
243 
251  public function getSize() {
252  return strlen($this->getContent());
253  }
254 
255 
262  protected function _cacheContent()
263  {
264  // caching content if we can't fetch parts
265  if ($this->_content === null && $this->_mail) {
266  $this->_content = $this->_mail->getRawContent($this->_messageNum);
267  }
268 
269  if (!$this->isMultipart()) {
270  return;
271  }
272 
273  // split content in parts
274  $boundary = $this->getHeaderField('content-type', 'boundary');
275  if (!$boundary) {
279  #require_once 'Zend/Mail/Exception.php';
280  throw new Zend_Mail_Exception('no boundary found in content type to split message');
281  }
282  $parts = Zend_Mime_Decode::splitMessageStruct($this->_content, $boundary);
283  if ($parts === null) {
284  return;
285  }
286  $partClass = $this->getPartClass();
287  $counter = 1;
288  foreach ($parts as $part) {
289  $this->_parts[$counter++] = new $partClass(array('headers' => $part['header'], 'content' => $part['body']));
290  }
291  }
292 
300  public function getPart($num)
301  {
302  if (isset($this->_parts[$num])) {
303  return $this->_parts[$num];
304  }
305 
306  if (!$this->_mail && $this->_content === null) {
310  #require_once 'Zend/Mail/Exception.php';
311  throw new Zend_Mail_Exception('part not found');
312  }
313 
314  if ($this->_mail && $this->_mail->hasFetchPart) {
315  // TODO: fetch part
316  // return
317  }
318 
319  $this->_cacheContent();
320 
321  if (!isset($this->_parts[$num])) {
325  #require_once 'Zend/Mail/Exception.php';
326  throw new Zend_Mail_Exception('part not found');
327  }
328 
329  return $this->_parts[$num];
330  }
331 
337  public function countParts()
338  {
339  if ($this->_countParts) {
340  return $this->_countParts;
341  }
342 
343  $this->_countParts = count($this->_parts);
344  if ($this->_countParts) {
345  return $this->_countParts;
346  }
347 
348  if ($this->_mail && $this->_mail->hasFetchPart) {
349  // TODO: fetch part
350  // return
351  }
352 
353  $this->_cacheContent();
354 
355  $this->_countParts = count($this->_parts);
356  return $this->_countParts;
357  }
358 
359 
368  public function getHeaders()
369  {
370  if ($this->_headers === null) {
371  if (!$this->_mail) {
372  $this->_headers = array();
373  } else {
374  $part = $this->_mail->getRawHeader($this->_messageNum);
375  Zend_Mime_Decode::splitMessage($part, $this->_headers, $null);
376  }
377  }
378 
379  return $this->_headers;
380  }
381 
393  public function getHeader($name, $format = null)
394  {
395  if ($this->_headers === null) {
396  $this->getHeaders();
397  }
398 
399  $lowerName = strtolower($name);
400 
401  if ($this->headerExists($name) == false) {
402  $lowerName = strtolower(preg_replace('%([a-z])([A-Z])%', '\1-\2', $name));
403  if($this->headerExists($lowerName) == false) {
407  #require_once 'Zend/Mail/Exception.php';
408  throw new Zend_Mail_Exception("no Header with Name $name or $lowerName found");
409  }
410  }
411  $name = $lowerName;
412 
413  $header = $this->_headers[$name];
414 
415  switch ($format) {
416  case 'string':
417  if (is_array($header)) {
418  $header = implode(Zend_Mime::LINEEND, $header);
419  }
420  break;
421  case 'array':
422  $header = (array)$header;
423  default:
424  // do nothing
425  }
426 
427  return $header;
428  }
429 
436  public function headerExists($name)
437  {
438  $name = strtolower($name);
439  if(isset($this->_headers[$name])) {
440  return true;
441  } else {
442  return false;
443  }
444  }
445 
461  public function getHeaderField($name, $wantedPart = 0, $firstName = 0) {
462  return Zend_Mime_Decode::splitHeaderField(current($this->getHeader($name, 'array')), $wantedPart, $firstName);
463  }
464 
465 
477  public function __get($name)
478  {
479  return $this->getHeader($name, 'string');
480  }
481 
492  public function __isset($name)
493  {
494  return $this->headerExists($name);
495  }
496 
502  public function __toString()
503  {
504  return $this->getContent();
505  }
506 
512  public function hasChildren()
513  {
514  $current = $this->current();
515  return $current && $current instanceof Zend_Mail_Part && $current->isMultipart();
516  }
517 
523  public function getChildren()
524  {
525  return $this->current();
526  }
527 
533  public function valid()
534  {
535  if ($this->_countParts === null) {
536  $this->countParts();
537  }
538  return $this->_iterationPos && $this->_iterationPos <= $this->_countParts;
539  }
540 
546  public function next()
547  {
549  }
550 
556  public function key()
557  {
558  return $this->_iterationPos;
559  }
560 
566  public function current()
567  {
568  return $this->getPart($this->_iterationPos);
569  }
570 
576  public function rewind()
577  {
578  $this->countParts();
579  $this->_iterationPos = 1;
580  }
581 
588  protected function _validateHeaders(array $headers, $assertNames = true)
589  {
590  foreach ($headers as $name => $value) {
591  if ($assertNames) {
593  }
594 
595  if (is_array($value)) {
596  $this->_validateHeaders($value, false);
597  continue;
598  }
599 
601  }
602  }
603 }
__construct(array $params)
Definition: Part.php:120
is_subclass_of($obj, $className)
getHeaderField($name, $wantedPart=0, $firstName=0)
Definition: Part.php:461
static splitMessage( $message, &$headers, &$body, $EOL=Zend_Mime::LINEEND)
Definition: Decode.php:123
headerExists($name)
Definition: Part.php:436
_cacheContent()
Definition: Part.php:262
_validateHeaders(array $headers, $assertNames=true)
Definition: Part.php:588
const LINEEND
Definition: Mime.php:42
static splitMessageStruct( $message, $boundary, $EOL=Zend_Mime::LINEEND)
Definition: Decode.php:91
$_option $_optionId $class
Definition: date.phtml:13
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
__isset($name)
Definition: Part.php:492
static splitHeaderField( $field, $wantedPart=null, $firstName=0)
Definition: Decode.php:217
__get($name)
Definition: Part.php:477
setPartClass($class)
Definition: Part.php:171
getHeader($name, $format=null)
Definition: Part.php:393
getPartClass()
Definition: Part.php:196
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
getPart($num)
Definition: Part.php:300
if(!isset($_GET['name'])) $name
Definition: log.php:14