00001 <?php
00027 require_once 'Zend/Mail/Storage/Folder.php';
00028
00032 require_once 'Zend/Mail/Storage/Folder/Interface.php';
00033
00037 require_once 'Zend/Mail/Storage/Maildir.php';
00038
00039
00047 class Zend_Mail_Storage_Folder_Maildir extends Zend_Mail_Storage_Maildir implements Zend_Mail_Storage_Folder_Interface
00048 {
00053 protected $_rootFolder;
00054
00059 protected $_rootdir;
00060
00065 protected $_currentFolder;
00066
00071 protected $_delim;
00072
00083 public function __construct($params)
00084 {
00085 if (is_array($params)) {
00086 $params = (object)$params;
00087 }
00088
00089 if (!isset($params->dirname) || !is_dir($params->dirname)) {
00093 require_once 'Zend/Mail/Storage/Exception.php';
00094 throw new Zend_Mail_Storage_Exception('no valid dirname given in params');
00095 }
00096
00097 $this->_rootdir = rtrim($params->dirname, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
00098
00099 $this->_delim = isset($params->delim) ? $params->delim : '.';
00100
00101 $this->_buildFolderTree();
00102 $this->selectFolder(!empty($params->folder) ? $params->folder : 'INBOX');
00103 $this->_has['top'] = true;
00104 $this->_has['flags'] = true;
00105 }
00106
00116 protected function _buildFolderTree()
00117 {
00118 $this->_rootFolder = new Zend_Mail_Storage_Folder('/', '/', false);
00119 $this->_rootFolder->INBOX = new Zend_Mail_Storage_Folder('INBOX', 'INBOX', true);
00120
00121 $dh = @opendir($this->_rootdir);
00122 if (!$dh) {
00126 require_once 'Zend/Mail/Storage/Exception.php';
00127 throw new Zend_Mail_Storage_Exception("can't read folders in maildir");
00128 }
00129 $dirs = array();
00130 while (($entry = readdir($dh)) !== false) {
00131
00132 if ($entry[0] != '.' || $entry == '.' || $entry == '..') {
00133 continue;
00134 }
00135 if ($this->_isMaildir($this->_rootdir . $entry)) {
00136 $dirs[] = $entry;
00137 }
00138 }
00139 closedir($dh);
00140
00141 sort($dirs);
00142 $stack = array(null);
00143 $folderStack = array(null);
00144 $parentFolder = $this->_rootFolder;
00145 $parent = '.';
00146
00147 foreach ($dirs as $dir) {
00148 do {
00149 if (strpos($dir, $parent) === 0) {
00150 $local = substr($dir, strlen($parent));
00151 if (strpos($local, $this->_delim) !== false) {
00155 require_once 'Zend/Mail/Storage/Exception.php';
00156 throw new Zend_Mail_Storage_Exception('error while reading maildir');
00157 }
00158 array_push($stack, $parent);
00159 $parent = $dir . $this->_delim;
00160 $folder = new Zend_Mail_Storage_Folder($local, substr($dir, 1), true);
00161 $parentFolder->$local = $folder;
00162 array_push($folderStack, $parentFolder);
00163 $parentFolder = $folder;
00164 break;
00165 } else if ($stack) {
00166 $parent = array_pop($stack);
00167 $parentFolder = array_pop($folderStack);
00168 }
00169 } while ($stack);
00170 if (!$stack) {
00174 require_once 'Zend/Mail/Storage/Exception.php';
00175 throw new Zend_Mail_Storage_Exception('error while reading maildir');
00176 }
00177 }
00178 }
00179
00187 public function getFolders($rootFolder = null)
00188 {
00189 if (!$rootFolder || $rootFolder == 'INBOX') {
00190 return $this->_rootFolder;
00191 }
00192
00193
00194 if (strpos($rootFolder, 'INBOX' . $this->_delim) === 0) {
00195 $rootFolder = substr($rootFolder, 6);
00196 }
00197 $currentFolder = $this->_rootFolder;
00198 $subname = trim($rootFolder, $this->_delim);
00199 while ($currentFolder) {
00200 @list($entry, $subname) = @explode($this->_delim, $subname, 2);
00201 $currentFolder = $currentFolder->$entry;
00202 if (!$subname) {
00203 break;
00204 }
00205 }
00206
00207 if ($currentFolder->getGlobalName() != rtrim($rootFolder, $this->_delim)) {
00211 require_once 'Zend/Mail/Storage/Exception.php';
00212 throw new Zend_Mail_Storage_Exception("folder $rootFolder not found");
00213 }
00214 return $currentFolder;
00215 }
00216
00226 public function selectFolder($globalName)
00227 {
00228 $this->_currentFolder = (string)$globalName;
00229
00230
00231 $folder = $this->getFolders($this->_currentFolder);
00232
00233 try {
00234 $this->_openMaildir($this->_rootdir . '.' . $folder->getGlobalName());
00235 } catch(Zend_Mail_Storage_Exception $e) {
00236
00237 if (!$folder->isSelectable()) {
00241 require_once 'Zend/Mail/Storage/Exception.php';
00242 throw new Zend_Mail_Storage_Exception("{$this->_currentFolder} is not selectable");
00243 }
00244
00245 $this->_buildFolderTree($this->_rootdir);
00249 require_once 'Zend/Mail/Storage/Exception.php';
00250 throw new Zend_Mail_Storage_Exception('seems like the maildir has vanished, I\'ve rebuild the ' .
00251 'folder tree, search for an other folder and try again');
00252 }
00253 }
00254
00261 public function getCurrentFolder()
00262 {
00263 return $this->_currentFolder;
00264 }
00265 }