Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Mbox.php
Go to the documentation of this file.
1 <?php
28 // #require_once 'Zend/Loader.php';
29 
33 #require_once 'Zend/Mail/Storage/Abstract.php';
34 
38 #require_once 'Zend/Mail/Message/File.php';
39 
40 
49 {
54  protected $_fh;
55 
60  protected $_filename;
61 
66  protected $_filemtime;
67 
72  protected $_positions;
73 
78  protected $_messageClass = 'Zend_Mail_Message_File';
79 
86  public function countMessages()
87  {
88  return count($this->_positions);
89  }
90 
91 
98  public function getSize($id = 0)
99  {
100  if ($id) {
101  $pos = $this->_positions[$id - 1];
102  return $pos['end'] - $pos['start'];
103  }
104 
105  $result = array();
106  foreach ($this->_positions as $num => $pos) {
107  $result[$num + 1] = $pos['end'] - $pos['start'];
108  }
109 
110  return $result;
111  }
112 
113 
121  protected function _getPos($id)
122  {
123  if (!isset($this->_positions[$id - 1])) {
127  #require_once 'Zend/Mail/Storage/Exception.php';
128  throw new Zend_Mail_Storage_Exception('id does not exist');
129  }
130 
131  return $this->_positions[$id - 1];
132  }
133 
134 
142  public function getMessage($id)
143  {
144  // TODO that's ugly, would be better to let the message class decide
145  if (strtolower($this->_messageClass) == 'zend_mail_message_file' || is_subclass_of($this->_messageClass, 'zend_mail_message_file')) {
146  // TODO top/body lines
147  $messagePos = $this->_getPos($id);
148  return new $this->_messageClass(array('file' => $this->_fh, 'startPos' => $messagePos['start'],
149  'endPos' => $messagePos['end']));
150  }
151 
152  $bodyLines = 0; // TODO: need a way to change that
153 
154  $message = $this->getRawHeader($id);
155  // file pointer is after headers now
156  if ($bodyLines) {
157  $message .= "\n";
158  while ($bodyLines-- && ftell($this->_fh) < $this->_positions[$id - 1]['end']) {
159  $message .= fgets($this->_fh);
160  }
161  }
162 
163  return new $this->_messageClass(array('handler' => $this, 'id' => $id, 'headers' => $message));
164  }
165 
166  /*
167  * Get raw header of message or part
168  *
169  * @param int $id number of message
170  * @param null|array|string $part path to part or null for messsage header
171  * @param int $topLines include this many lines with header (after an empty line)
172  * @return string raw header
173  * @throws Zend_Mail_Protocol_Exception
174  * @throws Zend_Mail_Storage_Exception
175  */
176  public function getRawHeader($id, $part = null, $topLines = 0)
177  {
178  if ($part !== null) {
179  // TODO: implement
183  #require_once 'Zend/Mail/Storage/Exception.php';
184  throw new Zend_Mail_Storage_Exception('not implemented');
185  }
186  $messagePos = $this->_getPos($id);
187  // TODO: toplines
188  return stream_get_contents($this->_fh, $messagePos['separator'] - $messagePos['start'], $messagePos['start']);
189  }
190 
191  /*
192  * Get raw content of message or part
193  *
194  * @param int $id number of message
195  * @param null|array|string $part path to part or null for messsage content
196  * @return string raw content
197  * @throws Zend_Mail_Protocol_Exception
198  * @throws Zend_Mail_Storage_Exception
199  */
200  public function getRawContent($id, $part = null)
201  {
202  if ($part !== null) {
203  // TODO: implement
207  #require_once 'Zend/Mail/Storage/Exception.php';
208  throw new Zend_Mail_Storage_Exception('not implemented');
209  }
210  $messagePos = $this->_getPos($id);
211  return stream_get_contents($this->_fh, $messagePos['end'] - $messagePos['separator'], $messagePos['separator']);
212  }
213 
222  public function __construct($params)
223  {
224  if (is_array($params)) {
225  $params = (object)$params;
226  }
227 
228  if (!isset($params->filename) /* || Zend_Loader::isReadable($params['filename']) */) {
232  #require_once 'Zend/Mail/Storage/Exception.php';
233  throw new Zend_Mail_Storage_Exception('no valid filename given in params');
234  }
235 
236  $this->_openMboxFile($params->filename);
237  $this->_has['top'] = true;
238  $this->_has['uniqueid'] = false;
239  }
240 
250  protected function _isMboxFile($file, $fileIsString = true)
251  {
252  if ($fileIsString) {
253  $file = @fopen($file, 'r');
254  if (!$file) {
255  return false;
256  }
257  } else {
258  fseek($file, 0);
259  }
260 
261  $result = false;
262 
263  $line = fgets($file);
264  if (strpos($line, 'From ') === 0) {
265  $result = true;
266  }
267 
268  if ($fileIsString) {
269  @fclose($file);
270  }
271 
272  return $result;
273  }
274 
282  protected function _openMboxFile($filename)
283  {
284  if ($this->_fh) {
285  $this->close();
286  }
287 
288  $this->_fh = @fopen($filename, 'r');
289  if (!$this->_fh) {
293  #require_once 'Zend/Mail/Storage/Exception.php';
294  throw new Zend_Mail_Storage_Exception('cannot open mbox file');
295  }
296  $this->_filename = $filename;
297  $this->_filemtime = filemtime($this->_filename);
298 
299  if (!$this->_isMboxFile($this->_fh, false)) {
300  @fclose($this->_fh);
304  #require_once 'Zend/Mail/Storage/Exception.php';
305  throw new Zend_Mail_Storage_Exception('file is not a valid mbox format');
306  }
307 
308  $messagePos = array('start' => ftell($this->_fh), 'separator' => 0, 'end' => 0);
309  while (($line = fgets($this->_fh)) !== false) {
310  if (strpos($line, 'From ') === 0) {
311  $messagePos['end'] = ftell($this->_fh) - strlen($line) - 2; // + newline
312  if (!$messagePos['separator']) {
313  $messagePos['separator'] = $messagePos['end'];
314  }
315  $this->_positions[] = $messagePos;
316  $messagePos = array('start' => ftell($this->_fh), 'separator' => 0, 'end' => 0);
317  }
318  if (!$messagePos['separator'] && !trim($line)) {
319  $messagePos['separator'] = ftell($this->_fh);
320  }
321  }
322 
323  $messagePos['end'] = ftell($this->_fh);
324  if (!$messagePos['separator']) {
325  $messagePos['separator'] = $messagePos['end'];
326  }
327  $this->_positions[] = $messagePos;
328  }
329 
336  public function close()
337  {
338  @fclose($this->_fh);
339  $this->_positions = array();
340  }
341 
342 
348  public function noop()
349  {
350  return true;
351  }
352 
353 
360  public function removeMessage($id)
361  {
365  #require_once 'Zend/Mail/Storage/Exception.php';
366  throw new Zend_Mail_Storage_Exception('mbox is read-only');
367  }
368 
380  public function getUniqueId($id = null)
381  {
382  if ($id) {
383  // check if id exists
384  $this->_getPos($id);
385  return $id;
386  }
387 
388  $range = range(1, $this->countMessages());
389  return array_combine($range, $range);
390  }
391 
402  public function getNumberByUniqueId($id)
403  {
404  // check if id exists
405  $this->_getPos($id);
406  return $id;
407  }
408 
416  public function __sleep()
417  {
418  return array('_filename', '_positions', '_filemtime');
419  }
420 
430  public function __wakeup()
431  {
432  if ($this->_filemtime != @filemtime($this->_filename)) {
433  $this->close();
434  $this->_openMboxFile($this->_filename);
435  } else {
436  $this->_fh = @fopen($this->_filename, 'r');
437  if (!$this->_fh) {
441  #require_once 'Zend/Mail/Storage/Exception.php';
442  throw new Zend_Mail_Storage_Exception('cannot open mbox file');
443  }
444  }
445  }
446 
447 }
is_subclass_of($obj, $className)
__construct($params)
Definition: Mbox.php:222
$id
Definition: fieldset.phtml:14
getRawContent($id, $part=null)
Definition: Mbox.php:200
getNumberByUniqueId($id)
Definition: Mbox.php:402
getUniqueId($id=null)
Definition: Mbox.php:380
_openMboxFile($filename)
Definition: Mbox.php:282
$message
$pos
Definition: list.phtml:42
_isMboxFile($file, $fileIsString=true)
Definition: Mbox.php:250
getRawHeader($id, $part=null, $topLines=0)
Definition: Mbox.php:176
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18