Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Maildir.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Mail/Storage/Abstract.php';
28 
32 #require_once 'Zend/Mail/Message/File.php';
33 
37 #require_once 'Zend/Mail/Storage.php';
38 
39 
48 {
53  protected $_messageClass = 'Zend_Mail_Message_File';
54 
59  protected $_files = array();
60 
68  protected static $_knownFlags = array('D' => Zend_Mail_Storage::FLAG_DRAFT,
74 
75  // TODO: getFlags($id) for fast access if headers are not needed (i.e. just setting flags)?
76 
83  public function countMessages($flags = null)
84  {
85  if ($flags === null) {
86  return count($this->_files);
87  }
88 
89  $count = 0;
90  if (!is_array($flags)) {
91  foreach ($this->_files as $file) {
92  if (isset($file['flaglookup'][$flags])) {
93  ++$count;
94  }
95  }
96  return $count;
97  }
98 
99  $flags = array_flip($flags);
100  foreach ($this->_files as $file) {
101  foreach ($flags as $flag => $v) {
102  if (!isset($file['flaglookup'][$flag])) {
103  continue 2;
104  }
105  }
106  ++$count;
107  }
108  return $count;
109  }
110 
119  protected function _getFileData($id, $field = null)
120  {
121  if (!isset($this->_files[$id - 1])) {
125  #require_once 'Zend/Mail/Storage/Exception.php';
126  throw new Zend_Mail_Storage_Exception('id does not exist');
127  }
128 
129  if (!$field) {
130  return $this->_files[$id - 1];
131  }
132 
133  if (!isset($this->_files[$id - 1][$field])) {
137  #require_once 'Zend/Mail/Storage/Exception.php';
138  throw new Zend_Mail_Storage_Exception('field does not exist');
139  }
140 
141  return $this->_files[$id - 1][$field];
142  }
143 
151  public function getSize($id = null)
152  {
153  if ($id !== null) {
154  $filedata = $this->_getFileData($id);
155  return isset($filedata['size']) ? $filedata['size'] : filesize($filedata['filename']);
156  }
157 
158  $result = array();
159  foreach ($this->_files as $num => $data) {
160  $result[$num + 1] = isset($data['size']) ? $data['size'] : filesize($data['filename']);
161  }
162 
163  return $result;
164  }
165 
166 
167 
175  public function getMessage($id)
176  {
177  // TODO that's ugly, would be better to let the message class decide
178  if (strtolower($this->_messageClass) == 'zend_mail_message_file' || is_subclass_of($this->_messageClass, 'zend_mail_message_file')) {
179  return new $this->_messageClass(array('file' => $this->_getFileData($id, 'filename'),
180  'flags' => $this->_getFileData($id, 'flags')));
181  }
182 
183  return new $this->_messageClass(array('handler' => $this, 'id' => $id, 'headers' => $this->getRawHeader($id),
184  'flags' => $this->_getFileData($id, 'flags')));
185  }
186 
187  /*
188  * Get raw header of message or part
189  *
190  * @param int $id number of message
191  * @param null|array|string $part path to part or null for messsage header
192  * @param int $topLines include this many lines with header (after an empty line)
193  * @return string raw header
194  * @throws Zend_Mail_Storage_Exception
195  */
196  public function getRawHeader($id, $part = null, $topLines = 0)
197  {
198  if ($part !== null) {
199  // TODO: implement
203  #require_once 'Zend/Mail/Storage/Exception.php';
204  throw new Zend_Mail_Storage_Exception('not implemented');
205  }
206 
207  $fh = fopen($this->_getFileData($id, 'filename'), 'r');
208 
209  $content = '';
210  while (!feof($fh)) {
211  $line = fgets($fh);
212  if (!trim($line)) {
213  break;
214  }
215  $content .= $line;
216  }
217 
218  fclose($fh);
219  return $content;
220  }
221 
222  /*
223  * Get raw content of message or part
224  *
225  * @param int $id number of message
226  * @param null|array|string $part path to part or null for messsage content
227  * @return string raw content
228  * @throws Zend_Mail_Storage_Exception
229  */
230  public function getRawContent($id, $part = null)
231  {
232  if ($part !== null) {
233  // TODO: implement
237  #require_once 'Zend/Mail/Storage/Exception.php';
238  throw new Zend_Mail_Storage_Exception('not implemented');
239  }
240 
241  $fh = fopen($this->_getFileData($id, 'filename'), 'r');
242 
243  while (!feof($fh)) {
244  $line = fgets($fh);
245  if (!trim($line)) {
246  break;
247  }
248  }
249 
250  $content = stream_get_contents($fh);
251  fclose($fh);
252  return $content;
253  }
254 
263  public function __construct($params)
264  {
265  if (is_array($params)) {
266  $params = (object)$params;
267  }
268 
269  if (!isset($params->dirname) || !is_dir($params->dirname)) {
273  #require_once 'Zend/Mail/Storage/Exception.php';
274  throw new Zend_Mail_Storage_Exception('no valid dirname given in params');
275  }
276 
277  if (!$this->_isMaildir($params->dirname)) {
281  #require_once 'Zend/Mail/Storage/Exception.php';
282  throw new Zend_Mail_Storage_Exception('invalid maildir given');
283  }
284 
285  $this->_has['top'] = true;
286  $this->_has['flags'] = true;
287  $this->_openMaildir($params->dirname);
288  }
289 
296  protected function _isMaildir($dirname)
297  {
298  if (file_exists($dirname . '/new') && !is_dir($dirname . '/new')) {
299  return false;
300  }
301  if (file_exists($dirname . '/tmp') && !is_dir($dirname . '/tmp')) {
302  return false;
303  }
304  return is_dir($dirname . '/cur');
305  }
306 
314  protected function _openMaildir($dirname)
315  {
316  if ($this->_files) {
317  $this->close();
318  }
319 
320  $dh = @opendir($dirname . '/cur/');
321  if (!$dh) {
325  #require_once 'Zend/Mail/Storage/Exception.php';
326  throw new Zend_Mail_Storage_Exception('cannot open maildir');
327  }
328  $this->_getMaildirFiles($dh, $dirname . '/cur/');
329  closedir($dh);
330 
331  $dh = @opendir($dirname . '/new/');
332  if ($dh) {
333  $this->_getMaildirFiles($dh, $dirname . '/new/', array(Zend_Mail_Storage::FLAG_RECENT));
334  closedir($dh);
335  } else if (file_exists($dirname . '/new/')) {
339  #require_once 'Zend/Mail/Storage/Exception.php';
340  throw new Zend_Mail_Storage_Exception('cannot read recent mails in maildir');
341  }
342  }
343 
352  protected function _getMaildirFiles($dh, $dirname, $default_flags = array())
353  {
354  while (($entry = readdir($dh)) !== false) {
355  if ($entry[0] == '.' || !is_file($dirname . $entry)) {
356  continue;
357  }
358 
359  @list($uniq, $info) = explode(':', $entry, 2);
360  @list(,$size) = explode(',', $uniq, 2);
361  if ($size && $size[0] == 'S' && $size[1] == '=') {
362  $size = substr($size, 2);
363  }
364  if (!ctype_digit($size)) {
365  $size = null;
366  }
367  @list($version, $flags) = explode(',', $info, 2);
368  if ($version != 2) {
369  $flags = '';
370  }
371 
372  $named_flags = $default_flags;
373  $length = strlen($flags);
374  for ($i = 0; $i < $length; ++$i) {
375  $flag = $flags[$i];
376  $named_flags[$flag] = isset(self::$_knownFlags[$flag]) ? self::$_knownFlags[$flag] : $flag;
377  }
378 
379  $data = array('uniq' => $uniq,
380  'flags' => $named_flags,
381  'flaglookup' => array_flip($named_flags),
382  'filename' => $dirname . $entry);
383  if ($size !== null) {
384  $data['size'] = (int)$size;
385  }
386  $this->_files[] = $data;
387  }
388  }
389 
390 
397  public function close()
398  {
399  $this->_files = array();
400  }
401 
402 
408  public function noop()
409  {
410  return true;
411  }
412 
413 
420  public function removeMessage($id)
421  {
425  #require_once 'Zend/Mail/Storage/Exception.php';
426  throw new Zend_Mail_Storage_Exception('maildir is (currently) read-only');
427  }
428 
438  public function getUniqueId($id = null)
439  {
440  if ($id) {
441  return $this->_getFileData($id, 'uniq');
442  }
443 
444  $ids = array();
445  foreach ($this->_files as $num => $file) {
446  $ids[$num + 1] = $file['uniq'];
447  }
448  return $ids;
449  }
450 
461  public function getNumberByUniqueId($id)
462  {
463  foreach ($this->_files as $num => $file) {
464  if ($file['uniq'] == $id) {
465  return $num + 1;
466  }
467  }
468 
472  #require_once 'Zend/Mail/Storage/Exception.php';
473  throw new Zend_Mail_Storage_Exception('unique id not found');
474  }
475 }
const FLAG_RECENT
Definition: Storage.php:39
is_subclass_of($obj, $className)
_getMaildirFiles($dh, $dirname, $default_flags=array())
Definition: Maildir.php:352
$id
Definition: fieldset.phtml:14
getRawHeader($id, $part=null, $topLines=0)
Definition: Maildir.php:196
const FLAG_FLAGGED
Definition: Storage.php:36
$count
Definition: recent.phtml:13
const FLAG_DELETED
Definition: Storage.php:37
const FLAG_PASSED
Definition: Storage.php:32
countMessages($flags=null)
Definition: Maildir.php:83
_getFileData($id, $field=null)
Definition: Maildir.php:119
const FLAG_DRAFT
Definition: Storage.php:38
const FLAG_ANSWERED
Definition: Storage.php:35
foreach( $_productCollection as $_product)() ?>" class $info
Definition: listing.phtml:52
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
getRawContent($id, $part=null)
Definition: Maildir.php:230
$i
Definition: gallery.phtml:31