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/Mbox.php';
00038
00039
00047 class Zend_Mail_Storage_Folder_Mbox extends Zend_Mail_Storage_Mbox implements Zend_Mail_Storage_Folder_Interface
00048 {
00053 protected $_rootFolder;
00054
00059 protected $_rootdir;
00060
00065 protected $_currentFolder;
00066
00079 public function __construct($params)
00080 {
00081 if (is_array($params)) {
00082 $params = (object)$params;
00083 }
00084
00085 if (isset($params->filename)) {
00089 require_once 'Zend/Mail/Storage/Exception.php';
00090 throw new Zend_Mail_Storage_Exception('use Zend_Mail_Storage_Mbox for a single file');
00091 }
00092
00093 if (!isset($params->dirname) || !is_dir($params->dirname)) {
00097 require_once 'Zend/Mail/Storage/Exception.php';
00098 throw new Zend_Mail_Storage_Exception('no valid dirname given in params');
00099 }
00100
00101 $this->_rootdir = rtrim($params->dirname, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
00102
00103 $this->_buildFolderTree($this->_rootdir);
00104 $this->selectFolder(!empty($params->folder) ? $params->folder : 'INBOX');
00105 $this->_has['top'] = true;
00106 $this->_has['uniqueid'] = false;
00107 }
00108
00121 protected function _buildFolderTree($currentDir, $parentFolder = null, $parentGlobalName = '')
00122 {
00123 if (!$parentFolder) {
00124 $this->_rootFolder = new Zend_Mail_Storage_Folder('/', '/', false);
00125 $parentFolder = $this->_rootFolder;
00126 }
00127
00128 $dh = @opendir($currentDir);
00129 if (!$dh) {
00133 require_once 'Zend/Mail/Storage/Exception.php';
00134 throw new Zend_Mail_Storage_Exception("can't read dir $currentDir");
00135 }
00136 while (($entry = readdir($dh)) !== false) {
00137
00138 if ($entry[0] == '.') {
00139 continue;
00140 }
00141 $absoluteEntry = $currentDir . $entry;
00142 $globalName = $parentGlobalName . DIRECTORY_SEPARATOR . $entry;
00143 if (is_file($absoluteEntry) && $this->_isMboxFile($absoluteEntry)) {
00144 $parentFolder->$entry = new Zend_Mail_Storage_Folder($entry, $globalName);
00145 continue;
00146 }
00147 if (!is_dir($absoluteEntry) ) {
00148 continue;
00149 }
00150 $folder = new Zend_Mail_Storage_Folder($entry, $globalName, false);
00151 $parentFolder->$entry = $folder;
00152 $this->_buildFolderTree($absoluteEntry . DIRECTORY_SEPARATOR, $folder, $globalName);
00153 }
00154
00155 closedir($dh);
00156 }
00157
00165 public function getFolders($rootFolder = null)
00166 {
00167 if (!$rootFolder) {
00168 return $this->_rootFolder;
00169 }
00170
00171 $currentFolder = $this->_rootFolder;
00172 $subname = trim($rootFolder, DIRECTORY_SEPARATOR);
00173 while ($currentFolder) {
00174 @list($entry, $subname) = @explode(DIRECTORY_SEPARATOR, $subname, 2);
00175 $currentFolder = $currentFolder->$entry;
00176 if (!$subname) {
00177 break;
00178 }
00179 }
00180
00181 if ($currentFolder->getGlobalName() != DIRECTORY_SEPARATOR . trim($rootFolder, DIRECTORY_SEPARATOR)) {
00185 require_once 'Zend/Mail/Storage/Exception.php';
00186 throw new Zend_Mail_Storage_Exception("folder $rootFolder not found");
00187 }
00188 return $currentFolder;
00189 }
00190
00200 public function selectFolder($globalName)
00201 {
00202 $this->_currentFolder = (string)$globalName;
00203
00204
00205 $folder = $this->getFolders($this->_currentFolder);
00206
00207 try {
00208 $this->_openMboxFile($this->_rootdir . $folder->getGlobalName());
00209 } catch(Zend_Mail_Storage_Exception $e) {
00210
00211 if (!$folder->isSelectable()) {
00215 require_once 'Zend/Mail/Storage/Exception.php';
00216 throw new Zend_Mail_Storage_Exception("{$this->_currentFolder} is not selectable");
00217 }
00218
00219 $this->_buildFolderTree($this->_rootdir);
00223 require_once 'Zend/Mail/Storage/Exception.php';
00224 throw new Zend_Mail_Storage_Exception('seems like the mbox file has vanished, I\'ve rebuild the ' .
00225 'folder tree, search for an other folder and try again');
00226 }
00227 }
00228
00235 public function getCurrentFolder()
00236 {
00237 return $this->_currentFolder;
00238 }
00239
00247 public function __sleep()
00248 {
00249 return array_merge(parent::__sleep(), array('_currentFolder', '_rootFolder', '_rootdir'));
00250 }
00251
00259 public function __wakeup()
00260 {
00261
00262 parent::__wakeup();
00263 }
00264 }