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/Folder.php';
28 
32 #require_once 'Zend/Mail/Storage/Folder/Interface.php';
33 
37 #require_once 'Zend/Mail/Storage/Maildir.php';
38 
39 
48 {
53  protected $_rootFolder;
54 
59  protected $_rootdir;
60 
65  protected $_currentFolder;
66 
71  protected $_delim;
72 
83  public function __construct($params)
84  {
85  if (is_array($params)) {
86  $params = (object)$params;
87  }
88 
89  if (!isset($params->dirname) || !is_dir($params->dirname)) {
93  #require_once 'Zend/Mail/Storage/Exception.php';
94  throw new Zend_Mail_Storage_Exception('no valid dirname given in params');
95  }
96 
97  $this->_rootdir = rtrim($params->dirname, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
98 
99  $this->_delim = isset($params->delim) ? $params->delim : '.';
100 
101  $this->_buildFolderTree();
102  $this->selectFolder(!empty($params->folder) ? $params->folder : 'INBOX');
103  $this->_has['top'] = true;
104  $this->_has['flags'] = true;
105  }
106 
116  protected function _buildFolderTree()
117  {
118  $this->_rootFolder = new Zend_Mail_Storage_Folder('/', '/', false);
119  $this->_rootFolder->INBOX = new Zend_Mail_Storage_Folder('INBOX', 'INBOX', true);
120 
121  $dh = @opendir($this->_rootdir);
122  if (!$dh) {
126  #require_once 'Zend/Mail/Storage/Exception.php';
127  throw new Zend_Mail_Storage_Exception("can't read folders in maildir");
128  }
129  $dirs = array();
130  while (($entry = readdir($dh)) !== false) {
131  // maildir++ defines folders must start with .
132  if ($entry[0] != '.' || $entry == '.' || $entry == '..') {
133  continue;
134  }
135  if ($this->_isMaildir($this->_rootdir . $entry)) {
136  $dirs[] = $entry;
137  }
138  }
139  closedir($dh);
140 
141  sort($dirs);
142  $stack = array(null);
143  $folderStack = array(null);
144  $parentFolder = $this->_rootFolder;
145  $parent = '.';
146 
147  foreach ($dirs as $dir) {
148  do {
149  if (strpos($dir, $parent) === 0) {
150  $local = substr($dir, strlen($parent));
151  if (strpos($local, $this->_delim) !== false) {
155  #require_once 'Zend/Mail/Storage/Exception.php';
156  throw new Zend_Mail_Storage_Exception('error while reading maildir');
157  }
158  array_push($stack, $parent);
159  $parent = $dir . $this->_delim;
160  $folder = new Zend_Mail_Storage_Folder($local, substr($dir, 1), true);
161  $parentFolder->$local = $folder;
162  array_push($folderStack, $parentFolder);
163  $parentFolder = $folder;
164  break;
165  } else if ($stack) {
166  $parent = array_pop($stack);
167  $parentFolder = array_pop($folderStack);
168  }
169  } while ($stack);
170  if (!$stack) {
174  #require_once 'Zend/Mail/Storage/Exception.php';
175  throw new Zend_Mail_Storage_Exception('error while reading maildir');
176  }
177  }
178  }
179 
187  public function getFolders($rootFolder = null)
188  {
189  if (!$rootFolder || $rootFolder == 'INBOX') {
190  return $this->_rootFolder;
191  }
192 
193  // rootdir is same as INBOX in maildir
194  if (strpos($rootFolder, 'INBOX' . $this->_delim) === 0) {
195  $rootFolder = substr($rootFolder, 6);
196  }
197  $currentFolder = $this->_rootFolder;
198  $subname = trim($rootFolder, $this->_delim);
199  while ($currentFolder) {
200  @list($entry, $subname) = @explode($this->_delim, $subname, 2);
201  $currentFolder = $currentFolder->$entry;
202  if (!$subname) {
203  break;
204  }
205  }
206 
207  if ($currentFolder->getGlobalName() != rtrim($rootFolder, $this->_delim)) {
211  #require_once 'Zend/Mail/Storage/Exception.php';
212  throw new Zend_Mail_Storage_Exception("folder $rootFolder not found");
213  }
214  return $currentFolder;
215  }
216 
226  public function selectFolder($globalName)
227  {
228  $this->_currentFolder = (string)$globalName;
229 
230  // getting folder from folder tree for validation
231  $folder = $this->getFolders($this->_currentFolder);
232 
233  try {
234  $this->_openMaildir($this->_rootdir . '.' . $folder->getGlobalName());
235  } catch(Zend_Mail_Storage_Exception $e) {
236  // check what went wrong
237  if (!$folder->isSelectable()) {
241  #require_once 'Zend/Mail/Storage/Exception.php';
242  throw new Zend_Mail_Storage_Exception("{$this->_currentFolder} is not selectable", 0, $e);
243  }
244  // seems like file has vanished; rebuilding folder tree - but it's still an exception
245  $this->_buildFolderTree($this->_rootdir);
249  #require_once 'Zend/Mail/Storage/Exception.php';
250  throw new Zend_Mail_Storage_Exception('seems like the maildir has vanished, I\'ve rebuild the ' .
251  'folder tree, search for an other folder and try again', 0, $e);
252  }
253  }
254 
261  public function getCurrentFolder()
262  {
263  return $this->_currentFolder;
264  }
265 }
getFolders($rootFolder=null)
Definition: Maildir.php:187
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18