Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
File.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Mime/Decode.php';
27 
31 #require_once 'Zend/Mail/Part.php';
32 
33 
41 {
42  protected $_contentPos = array();
43  protected $_partPos = array();
44  protected $_fh;
45 
57  public function __construct(array $params)
58  {
59  if (empty($params['file'])) {
63  #require_once 'Zend/Mail/Exception.php';
64  throw new Zend_Mail_Exception('no file given in params');
65  }
66 
67  if (!is_resource($params['file'])) {
68  $this->_fh = fopen($params['file'], 'r');
69  } else {
70  $this->_fh = $params['file'];
71  }
72  if (!$this->_fh) {
76  #require_once 'Zend/Mail/Exception.php';
77  throw new Zend_Mail_Exception('could not open file');
78  }
79  if (isset($params['startPos'])) {
80  fseek($this->_fh, $params['startPos']);
81  }
82  $header = '';
83  $endPos = isset($params['endPos']) ? $params['endPos'] : null;
84  while (($endPos === null || ftell($this->_fh) < $endPos) && trim($line = fgets($this->_fh))) {
85  $header .= $line;
86  }
87 
88  Zend_Mime_Decode::splitMessage($header, $this->_headers, $null);
89 
90  $this->_contentPos[0] = ftell($this->_fh);
91  if ($endPos !== null) {
92  $this->_contentPos[1] = $endPos;
93  } else {
94  fseek($this->_fh, 0, SEEK_END);
95  $this->_contentPos[1] = ftell($this->_fh);
96  }
97  if (!$this->isMultipart()) {
98  return;
99  }
100 
101  $boundary = $this->getHeaderField('content-type', 'boundary');
102  if (!$boundary) {
106  #require_once 'Zend/Mail/Exception.php';
107  throw new Zend_Mail_Exception('no boundary found in content type to split message');
108  }
109 
110  $part = array();
111  $pos = $this->_contentPos[0];
112  fseek($this->_fh, $pos);
113  while (!feof($this->_fh) && ($endPos === null || $pos < $endPos)) {
114  $line = fgets($this->_fh);
115  if ($line === false) {
116  if (feof($this->_fh)) {
117  break;
118  }
122  #require_once 'Zend/Mail/Exception.php';
123  throw new Zend_Mail_Exception('error reading file');
124  }
125 
126  $lastPos = $pos;
127  $pos = ftell($this->_fh);
128  $line = trim($line);
129 
130  if ($line == '--' . $boundary) {
131  if ($part) {
132  // not first part
133  $part[1] = $lastPos;
134  $this->_partPos[] = $part;
135  }
136  $part = array($pos);
137  } else if ($line == '--' . $boundary . '--') {
138  $part[1] = $lastPos;
139  $this->_partPos[] = $part;
140  break;
141  }
142  }
143  $this->_countParts = count($this->_partPos);
144 
145  }
146 
147 
156  public function getContent($stream = null)
157  {
158  fseek($this->_fh, $this->_contentPos[0]);
159  if ($stream !== null) {
160  return stream_copy_to_stream($this->_fh, $stream, $this->_contentPos[1] - $this->_contentPos[0]);
161  }
162  $length = $this->_contentPos[1] - $this->_contentPos[0];
163  return $length < 1 ? '' : fread($this->_fh, $length);
164  }
165 
173  public function getSize() {
174  return $this->_contentPos[1] - $this->_contentPos[0];
175  }
176 
184  public function getPart($num)
185  {
186  --$num;
187  if (!isset($this->_partPos[$num])) {
191  #require_once 'Zend/Mail/Exception.php';
192  throw new Zend_Mail_Exception('part not found');
193  }
194 
195  return new self(array('file' => $this->_fh, 'startPos' => $this->_partPos[$num][0],
196  'endPos' => $this->_partPos[$num][1]));
197  }
198 }
getHeaderField($name, $wantedPart=0, $firstName=0)
Definition: Part.php:461
static splitMessage( $message, &$headers, &$body, $EOL=Zend_Mime::LINEEND)
Definition: Decode.php:123
getContent($stream=null)
Definition: File.php:156
__construct(array $params)
Definition: File.php:57
$pos
Definition: list.phtml:42
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18