Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Imap.php
Go to the documentation of this file.
1 <?php
27 #require_once 'Zend/Mail/Storage/Abstract.php';
28 
32 #require_once 'Zend/Mail/Protocol/Imap.php';
33 
37 #require_once 'Zend/Mail/Storage/Writable/Interface.php';
38 
42 #require_once 'Zend/Mail/Storage/Folder/Interface.php';
43 
47 #require_once 'Zend/Mail/Storage/Folder.php';
48 
52 #require_once 'Zend/Mail/Message.php';
53 
57 #require_once 'Zend/Mail/Storage.php';
58 
68 {
69  // TODO: with an internal cache we could optimize this class, or create an extra class with
70  // such optimizations. Especially the various fetch calls could be combined to one cache call
71 
76  protected $_protocol;
77 
82  protected $_currentFolder = '';
83 
88  protected static $_knownFlags = array('\Passed' => Zend_Mail_Storage::FLAG_PASSED,
89  '\Answered' => Zend_Mail_Storage::FLAG_ANSWERED,
91  '\Unseen' => Zend_Mail_Storage::FLAG_UNSEEN,
92  '\Deleted' => Zend_Mail_Storage::FLAG_DELETED,
94  '\Flagged' => Zend_Mail_Storage::FLAG_FLAGGED);
95 
100  protected static $_searchFlags = array('\Recent' => 'RECENT',
101  '\Answered' => 'ANSWERED',
102  '\Seen' => 'SEEN',
103  '\Unseen' => 'UNSEEN',
104  '\Deleted' => 'DELETED',
105  '\Draft' => 'DRAFT',
106  '\Flagged' => 'FLAGGED');
107 
115  public function countMessages($flags = null)
116  {
117  if (!$this->_currentFolder) {
121  #require_once 'Zend/Mail/Storage/Exception.php';
122  throw new Zend_Mail_Storage_Exception('No selected folder to count');
123  }
124 
125  if ($flags === null) {
126  return count($this->_protocol->search(array('ALL')));
127  }
128 
129  $params = array();
130  foreach ((array)$flags as $flag) {
131  if (isset(self::$_searchFlags[$flag])) {
132  $params[] = self::$_searchFlags[$flag];
133  } else {
134  $params[] = 'KEYWORD';
135  $params[] = $this->_protocol->escapeString($flag);
136  }
137  }
138  return count($this->_protocol->search($params));
139  }
140 
148  public function getSize($id = 0)
149  {
150  if ($id) {
151  return $this->_protocol->fetch('RFC822.SIZE', $id);
152  }
153  return $this->_protocol->fetch('RFC822.SIZE', 1, INF);
154  }
155 
163  public function getMessage($id)
164  {
165  $data = $this->_protocol->fetch(array('FLAGS', 'RFC822.HEADER'), $id);
166  $header = $data['RFC822.HEADER'];
167 
168  $flags = array();
169  foreach ($data['FLAGS'] as $flag) {
170  $flags[] = isset(self::$_knownFlags[$flag]) ? self::$_knownFlags[$flag] : $flag;
171  }
172 
173  return new $this->_messageClass(array('handler' => $this, 'id' => $id, 'headers' => $header, 'flags' => $flags));
174  }
175 
176  /*
177  * Get raw header of message or part
178  *
179  * @param int $id number of message
180  * @param null|array|string $part path to part or null for messsage header
181  * @param int $topLines include this many lines with header (after an empty line)
182  * @param int $topLines include this many lines with header (after an empty line)
183  * @return string raw header
184  * @throws Zend_Mail_Protocol_Exception
185  * @throws Zend_Mail_Storage_Exception
186  */
187  public function getRawHeader($id, $part = null, $topLines = 0)
188  {
189  if ($part !== null) {
190  // TODO: implement
194  #require_once 'Zend/Mail/Storage/Exception.php';
195  throw new Zend_Mail_Storage_Exception('not implemented');
196  }
197 
198  // TODO: toplines
199  return $this->_protocol->fetch('RFC822.HEADER', $id);
200  }
201 
202  /*
203  * Get raw content of message or part
204  *
205  * @param int $id number of message
206  * @param null|array|string $part path to part or null for messsage content
207  * @return string raw content
208  * @throws Zend_Mail_Protocol_Exception
209  * @throws Zend_Mail_Storage_Exception
210  */
211  public function getRawContent($id, $part = null)
212  {
213  if ($part !== null) {
214  // TODO: implement
218  #require_once 'Zend/Mail/Storage/Exception.php';
219  throw new Zend_Mail_Storage_Exception('not implemented');
220  }
221 
222  return $this->_protocol->fetch('RFC822.TEXT', $id);
223  }
224 
239  public function __construct($params)
240  {
241  if (is_array($params)) {
242  $params = (object)$params;
243  }
244 
245  $this->_has['flags'] = true;
246 
247  if ($params instanceof Zend_Mail_Protocol_Imap) {
248  $this->_protocol = $params;
249  try {
250  $this->selectFolder('INBOX');
251  } catch(Zend_Mail_Storage_Exception $e) {
255  #require_once 'Zend/Mail/Storage/Exception.php';
256  throw new Zend_Mail_Storage_Exception('cannot select INBOX, is this a valid transport?', 0, $e);
257  }
258  return;
259  }
260 
261  if (!isset($params->user)) {
265  #require_once 'Zend/Mail/Storage/Exception.php';
266  throw new Zend_Mail_Storage_Exception('need at least user in params');
267  }
268 
269  $host = isset($params->host) ? $params->host : 'localhost';
270  $password = isset($params->password) ? $params->password : '';
271  $port = isset($params->port) ? $params->port : null;
272  $ssl = isset($params->ssl) ? $params->ssl : false;
273 
274  $this->_protocol = new Zend_Mail_Protocol_Imap();
275  $this->_protocol->connect($host, $port, $ssl);
276  if (!$this->_protocol->login($params->user, $password)) {
280  #require_once 'Zend/Mail/Storage/Exception.php';
281  throw new Zend_Mail_Storage_Exception('cannot login, user or password wrong');
282  }
283  $this->selectFolder(isset($params->folder) ? $params->folder : 'INBOX');
284  }
285 
292  public function close()
293  {
294  $this->_currentFolder = '';
295  $this->_protocol->logout();
296  }
297 
304  public function noop()
305  {
306  if (!$this->_protocol->noop()) {
310  #require_once 'Zend/Mail/Storage/Exception.php';
311  throw new Zend_Mail_Storage_Exception('could not do nothing');
312  }
313  }
314 
324  public function removeMessage($id)
325  {
326  if (!$this->_protocol->store(array(Zend_Mail_Storage::FLAG_DELETED), $id, null, '+')) {
330  #require_once 'Zend/Mail/Storage/Exception.php';
331  throw new Zend_Mail_Storage_Exception('cannot set deleted flag');
332  }
333  // TODO: expunge here or at close? we can handle an error here better and are more fail safe
334  if (!$this->_protocol->expunge()) {
338  #require_once 'Zend/Mail/Storage/Exception.php';
339  throw new Zend_Mail_Storage_Exception('message marked as deleted, but could not expunge');
340  }
341  }
342 
352  public function getUniqueId($id = null)
353  {
354  if ($id) {
355  return $this->_protocol->fetch('UID', $id);
356  }
357 
358  return $this->_protocol->fetch('UID', 1, INF);
359  }
360 
371  public function getNumberByUniqueId($id)
372  {
373  // TODO: use search to find number directly
374  $ids = $this->getUniqueId();
375  foreach ($ids as $k => $v) {
376  if ($v == $id) {
377  return $k;
378  }
379  }
380 
384  #require_once 'Zend/Mail/Storage/Exception.php';
385  throw new Zend_Mail_Storage_Exception('unique id not found');
386  }
387 
388 
397  public function getFolders($rootFolder = null)
398  {
399  $folders = $this->_protocol->listMailbox((string)$rootFolder);
400  if (!$folders) {
404  #require_once 'Zend/Mail/Storage/Exception.php';
405  throw new Zend_Mail_Storage_Exception('folder not found');
406  }
407 
408  ksort($folders, SORT_STRING);
409  $root = new Zend_Mail_Storage_Folder('/', '/', false);
410  $stack = array(null);
411  $folderStack = array(null);
412  $parentFolder = $root;
413  $parent = '';
414 
415  foreach ($folders as $globalName => $data) {
416  do {
417  if (!$parent || strpos($globalName, $parent) === 0) {
418  $pos = strrpos($globalName, $data['delim']);
419  if ($pos === false) {
420  $localName = $globalName;
421  } else {
422  $localName = substr($globalName, $pos + 1);
423  }
424  $selectable = !$data['flags'] || !in_array('\\Noselect', $data['flags']);
425 
426  array_push($stack, $parent);
427  $parent = $globalName . $data['delim'];
428  $folder = new Zend_Mail_Storage_Folder($localName, $globalName, $selectable);
429  $parentFolder->$localName = $folder;
430  array_push($folderStack, $parentFolder);
431  $parentFolder = $folder;
432  break;
433  } else if ($stack) {
434  $parent = array_pop($stack);
435  $parentFolder = array_pop($folderStack);
436  }
437  } while ($stack);
438  if (!$stack) {
442  #require_once 'Zend/Mail/Storage/Exception.php';
443  throw new Zend_Mail_Storage_Exception('error while constructing folder tree');
444  }
445  }
446 
447  return $root;
448  }
449 
460  public function selectFolder($globalName)
461  {
462  $this->_currentFolder = $globalName;
463  if (!$this->_protocol->select($this->_currentFolder)) {
464  $this->_currentFolder = '';
468  #require_once 'Zend/Mail/Storage/Exception.php';
469  throw new Zend_Mail_Storage_Exception('cannot change folder, maybe it does not exist');
470  }
471  }
472 
473 
480  public function getCurrentFolder()
481  {
482  return $this->_currentFolder;
483  }
484 
496  public function createFolder($name, $parentFolder = null)
497  {
498  // TODO: we assume / as the hierarchy delim - need to get that from the folder class!
499  if ($parentFolder instanceof Zend_Mail_Storage_Folder) {
500  $folder = $parentFolder->getGlobalName() . '/' . $name;
501  } else if ($parentFolder != null) {
502  $folder = $parentFolder . '/' . $name;
503  } else {
504  $folder = $name;
505  }
506 
507  if (!$this->_protocol->create($folder)) {
511  #require_once 'Zend/Mail/Storage/Exception.php';
512  throw new Zend_Mail_Storage_Exception('cannot create folder');
513  }
514  }
515 
523  public function removeFolder($name)
524  {
525  if ($name instanceof Zend_Mail_Storage_Folder) {
526  $name = $name->getGlobalName();
527  }
528 
529  if (!$this->_protocol->delete($name)) {
533  #require_once 'Zend/Mail/Storage/Exception.php';
534  throw new Zend_Mail_Storage_Exception('cannot delete folder');
535  }
536  }
537 
548  public function renameFolder($oldName, $newName)
549  {
550  if ($oldName instanceof Zend_Mail_Storage_Folder) {
551  $oldName = $oldName->getGlobalName();
552  }
553 
554  if (!$this->_protocol->rename($oldName, $newName)) {
558  #require_once 'Zend/Mail/Storage/Exception.php';
559  throw new Zend_Mail_Storage_Exception('cannot rename folder');
560  }
561  }
562 
571  // not yet * @param string|Zend_Mail_Message|Zend_Mime_Message $message message as string or instance of message class
572  public function appendMessage($message, $folder = null, $flags = null)
573  {
574  if ($folder === null) {
575  $folder = $this->_currentFolder;
576  }
577 
578  if ($flags === null) {
579  $flags = array(Zend_Mail_Storage::FLAG_SEEN);
580  }
581 
582  // TODO: handle class instances for $message
583  if (!$this->_protocol->append($folder, $message, $flags)) {
587  #require_once 'Zend/Mail/Storage/Exception.php';
588  throw new Zend_Mail_Storage_Exception('cannot create message, please check if the folder exists and your flags');
589  }
590  }
591 
600  public function copyMessage($id, $folder)
601  {
602  if (!$this->_protocol->copy($folder, $id)) {
606  #require_once 'Zend/Mail/Storage/Exception.php';
607  throw new Zend_Mail_Storage_Exception('cannot copy message, does the folder exist?');
608  }
609  }
610 
621  public function moveMessage($id, $folder) {
622  $this->copyMessage($id, $folder);
623  $this->removeMessage($id);
624  }
625 
635  public function setFlags($id, $flags)
636  {
637  if (!$this->_protocol->store($flags, $id)) {
641  #require_once 'Zend/Mail/Storage/Exception.php';
642  throw new Zend_Mail_Storage_Exception('cannot set flags, have you tried to set the recent flag or special chars?');
643  }
644  }
645 }
646 
getUniqueId($id=null)
Definition: Imap.php:352
copyMessage($id, $folder)
Definition: Imap.php:600
setFlags($id, $flags)
Definition: Imap.php:635
$id
Definition: fieldset.phtml:14
__construct($params)
Definition: Imap.php:239
const FLAG_FLAGGED
Definition: Storage.php:36
const FLAG_DELETED
Definition: Storage.php:37
const FLAG_PASSED
Definition: Storage.php:32
$message
selectFolder($globalName)
Definition: Imap.php:460
getNumberByUniqueId($id)
Definition: Imap.php:371
moveMessage($id, $folder)
Definition: Imap.php:621
const FLAG_DRAFT
Definition: Storage.php:38
$pos
Definition: list.phtml:42
getFolders($rootFolder=null)
Definition: Imap.php:397
const FLAG_UNSEEN
Definition: Storage.php:34
static $_knownFlags
Definition: Imap.php:88
appendMessage($message, $folder=null, $flags=null)
Definition: Imap.php:572
countMessages($flags=null)
Definition: Imap.php:115
const FLAG_ANSWERED
Definition: Storage.php:35
getRawHeader($id, $part=null, $topLines=0)
Definition: Imap.php:187
getRawContent($id, $part=null)
Definition: Imap.php:211
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
static $_searchFlags
Definition: Imap.php:100
createFolder($name, $parentFolder=null)
Definition: Imap.php:496
renameFolder($oldName, $newName)
Definition: Imap.php:548
if(!isset($_GET['name'])) $name
Definition: log.php:14