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
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/Mbox.php';
38 
39 
48 {
53  protected $_rootFolder;
54 
59  protected $_rootdir;
60 
65  protected $_currentFolder;
66 
79  public function __construct($params)
80  {
81  if (is_array($params)) {
82  $params = (object)$params;
83  }
84 
85  if (isset($params->filename)) {
89  #require_once 'Zend/Mail/Storage/Exception.php';
90  throw new Zend_Mail_Storage_Exception('use Zend_Mail_Storage_Mbox for a single file');
91  }
92 
93  if (!isset($params->dirname) || !is_dir($params->dirname)) {
97  #require_once 'Zend/Mail/Storage/Exception.php';
98  throw new Zend_Mail_Storage_Exception('no valid dirname given in params');
99  }
100 
101  $this->_rootdir = rtrim($params->dirname, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
102 
103  $this->_buildFolderTree($this->_rootdir);
104  $this->selectFolder(!empty($params->folder) ? $params->folder : 'INBOX');
105  $this->_has['top'] = true;
106  $this->_has['uniqueid'] = false;
107  }
108 
121  protected function _buildFolderTree($currentDir, $parentFolder = null, $parentGlobalName = '')
122  {
123  if (!$parentFolder) {
124  $this->_rootFolder = new Zend_Mail_Storage_Folder('/', '/', false);
125  $parentFolder = $this->_rootFolder;
126  }
127 
128  $dh = @opendir($currentDir);
129  if (!$dh) {
133  #require_once 'Zend/Mail/Storage/Exception.php';
134  throw new Zend_Mail_Storage_Exception("can't read dir $currentDir");
135  }
136  while (($entry = readdir($dh)) !== false) {
137  // ignore hidden files for mbox
138  if ($entry[0] == '.') {
139  continue;
140  }
141  $absoluteEntry = $currentDir . $entry;
142  $globalName = $parentGlobalName . DIRECTORY_SEPARATOR . $entry;
143  if (is_file($absoluteEntry) && $this->_isMboxFile($absoluteEntry)) {
144  $parentFolder->$entry = new Zend_Mail_Storage_Folder($entry, $globalName);
145  continue;
146  }
147  if (!is_dir($absoluteEntry) /* || $entry == '.' || $entry == '..' */) {
148  continue;
149  }
150  $folder = new Zend_Mail_Storage_Folder($entry, $globalName, false);
151  $parentFolder->$entry = $folder;
152  $this->_buildFolderTree($absoluteEntry . DIRECTORY_SEPARATOR, $folder, $globalName);
153  }
154 
155  closedir($dh);
156  }
157 
165  public function getFolders($rootFolder = null)
166  {
167  if (!$rootFolder) {
168  return $this->_rootFolder;
169  }
170 
171  $currentFolder = $this->_rootFolder;
172  $subname = trim($rootFolder, DIRECTORY_SEPARATOR);
173  while ($currentFolder) {
174  @list($entry, $subname) = @explode(DIRECTORY_SEPARATOR, $subname, 2);
175  $currentFolder = $currentFolder->$entry;
176  if (!$subname) {
177  break;
178  }
179  }
180 
181  if ($currentFolder->getGlobalName() != DIRECTORY_SEPARATOR . trim($rootFolder, DIRECTORY_SEPARATOR)) {
185  #require_once 'Zend/Mail/Storage/Exception.php';
186  throw new Zend_Mail_Storage_Exception("folder $rootFolder not found");
187  }
188  return $currentFolder;
189  }
190 
200  public function selectFolder($globalName)
201  {
202  $this->_currentFolder = (string)$globalName;
203 
204  // getting folder from folder tree for validation
205  $folder = $this->getFolders($this->_currentFolder);
206 
207  try {
208  $this->_openMboxFile($this->_rootdir . $folder->getGlobalName());
209  } catch(Zend_Mail_Storage_Exception $e) {
210  // check what went wrong
211  if (!$folder->isSelectable()) {
215  #require_once 'Zend/Mail/Storage/Exception.php';
216  throw new Zend_Mail_Storage_Exception("{$this->_currentFolder} is not selectable", 0, $e);
217  }
218  // seems like file has vanished; rebuilding folder tree - but it's still an exception
219  $this->_buildFolderTree($this->_rootdir);
223  #require_once 'Zend/Mail/Storage/Exception.php';
224  throw new Zend_Mail_Storage_Exception('seems like the mbox file has vanished, I\'ve rebuild the ' .
225  'folder tree, search for an other folder and try again', 0, $e);
226  }
227  }
228 
235  public function getCurrentFolder()
236  {
237  return $this->_currentFolder;
238  }
239 
247  public function __sleep()
248  {
249  return array_merge(parent::__sleep(), array('_currentFolder', '_rootFolder', '_rootdir'));
250  }
251 
259  public function __wakeup()
260  {
261  // if cache is stall selectFolder() rebuilds the tree on error
263  }
264 }
_buildFolderTree($currentDir, $parentFolder=null, $parentGlobalName='')
Definition: Mbox.php:121
getFolders($rootFolder=null)
Definition: Mbox.php:165
_openMboxFile($filename)
Definition: Mbox.php:282
_isMboxFile($file, $fileIsString=true)
Definition: Mbox.php:250
selectFolder($globalName)
Definition: Mbox.php:200
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18