/library/Zend/Mail/Storage/Folder/Maildir.php

https://github.com/zucchi/zf2 · PHP · 217 lines · 151 code · 14 blank · 52 comment · 6 complexity · 1425ed15938f402f263d6997baee7f91 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. * @package Zend_Mail
  9. */
  10. namespace Zend\Mail\Storage\Folder;
  11. use Zend\Mail\Storage;
  12. use Zend\Mail\Storage\Exception;
  13. use Zend\Stdlib\ErrorHandler;
  14. /**
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage Storage
  18. */
  19. class Maildir extends Storage\Maildir implements FolderInterface
  20. {
  21. /**
  22. * root folder for folder structure
  23. * @var \Zend\Mail\Storage\Folder
  24. */
  25. protected $rootFolder;
  26. /**
  27. * rootdir of folder structure
  28. * @var string
  29. */
  30. protected $rootdir;
  31. /**
  32. * name of current folder
  33. * @var string
  34. */
  35. protected $currentFolder;
  36. /**
  37. * delim char for subfolders
  38. * @var string
  39. */
  40. protected $delim;
  41. /**
  42. * Create instance with parameters
  43. * Supported parameters are:
  44. * - dirname rootdir of maildir structure
  45. * - delim delim char for folder structure, default is '.'
  46. * - folder initial selected folder, default is 'INBOX'
  47. *
  48. * @param $params array mail reader specific parameters
  49. * @throws \Zend\Mail\Storage\Exception\InvalidArgumentException
  50. */
  51. public function __construct($params)
  52. {
  53. if (is_array($params)) {
  54. $params = (object)$params;
  55. }
  56. if (!isset($params->dirname) || !is_dir($params->dirname)) {
  57. throw new Exception\InvalidArgumentException('no valid dirname given in params');
  58. }
  59. $this->rootdir = rtrim($params->dirname, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
  60. $this->delim = isset($params->delim) ? $params->delim : '.';
  61. $this->_buildFolderTree();
  62. $this->selectFolder(!empty($params->folder) ? $params->folder : 'INBOX');
  63. $this->has['top'] = true;
  64. $this->has['flags'] = true;
  65. }
  66. /**
  67. * find all subfolders and mbox files for folder structure
  68. *
  69. * Result is save in \Zend\Mail\Storage\Folder instances with the root in $this->rootFolder.
  70. * $parentFolder and $parentGlobalName are only used internally for recursion.
  71. *
  72. * @throws \Zend\Mail\Storage\Exception\RuntimeException
  73. */
  74. protected function _buildFolderTree()
  75. {
  76. $this->rootFolder = new Storage\Folder('/', '/', false);
  77. $this->rootFolder->INBOX = new Storage\Folder('INBOX', 'INBOX', true);
  78. ErrorHandler::start(E_WARNING);
  79. $dh = opendir($this->rootdir);
  80. $error = ErrorHandler::stop();
  81. if (!$dh) {
  82. throw new Exception\RuntimeException("can't read folders in maildir", 0, $error);
  83. }
  84. $dirs = array();
  85. while (($entry = readdir($dh)) !== false) {
  86. // maildir++ defines folders must start with .
  87. if ($entry[0] != '.' || $entry == '.' || $entry == '..') {
  88. continue;
  89. }
  90. if ($this->_isMaildir($this->rootdir . $entry)) {
  91. $dirs[] = $entry;
  92. }
  93. }
  94. closedir($dh);
  95. sort($dirs);
  96. $stack = array(null);
  97. $folderStack = array(null);
  98. $parentFolder = $this->rootFolder;
  99. $parent = '.';
  100. foreach ($dirs as $dir) {
  101. do {
  102. if (strpos($dir, $parent) === 0) {
  103. $local = substr($dir, strlen($parent));
  104. if (strpos($local, $this->delim) !== false) {
  105. throw new Exception\RuntimeException('error while reading maildir');
  106. }
  107. array_push($stack, $parent);
  108. $parent = $dir . $this->delim;
  109. $folder = new Storage\Folder($local, substr($dir, 1), true);
  110. $parentFolder->$local = $folder;
  111. array_push($folderStack, $parentFolder);
  112. $parentFolder = $folder;
  113. break;
  114. } elseif ($stack) {
  115. $parent = array_pop($stack);
  116. $parentFolder = array_pop($folderStack);
  117. }
  118. } while ($stack);
  119. if (!$stack) {
  120. throw new Exception\RuntimeException('error while reading maildir');
  121. }
  122. }
  123. }
  124. /**
  125. * get root folder or given folder
  126. *
  127. * @param string $rootFolder get folder structure for given folder, else root
  128. * @throws \Zend\Mail\Storage\Exception\InvalidArgumentException
  129. * @return \Zend\Mail\Storage\Folder root or wanted folder
  130. */
  131. public function getFolders($rootFolder = null)
  132. {
  133. if (!$rootFolder || $rootFolder == 'INBOX') {
  134. return $this->rootFolder;
  135. }
  136. // rootdir is same as INBOX in maildir
  137. if (strpos($rootFolder, 'INBOX' . $this->delim) === 0) {
  138. $rootFolder = substr($rootFolder, 6);
  139. }
  140. $currentFolder = $this->rootFolder;
  141. $subname = trim($rootFolder, $this->delim);
  142. while ($currentFolder) {
  143. ErrorHandler::start(E_NOTICE);
  144. list($entry, $subname) = explode($this->delim, $subname, 2);
  145. ErrorHandler::stop();
  146. $currentFolder = $currentFolder->$entry;
  147. if (!$subname) {
  148. break;
  149. }
  150. }
  151. if ($currentFolder->getGlobalName() != rtrim($rootFolder, $this->delim)) {
  152. throw new Exception\InvalidArgumentException("folder $rootFolder not found");
  153. }
  154. return $currentFolder;
  155. }
  156. /**
  157. * select given folder
  158. *
  159. * folder must be selectable!
  160. *
  161. * @param \Zend\Mail\Storage\Folder|string $globalName global name of folder or instance for subfolder
  162. * @throws \Zend\Mail\Storage\Exception\RuntimeException
  163. */
  164. public function selectFolder($globalName)
  165. {
  166. $this->currentFolder = (string)$globalName;
  167. // getting folder from folder tree for validation
  168. $folder = $this->getFolders($this->currentFolder);
  169. try {
  170. $this->_openMaildir($this->rootdir . '.' . $folder->getGlobalName());
  171. } catch (Exception\ExceptionInterface $e) {
  172. // check what went wrong
  173. if (!$folder->isSelectable()) {
  174. throw new Exception\RuntimeException("{$this->currentFolder} is not selectable", 0, $e);
  175. }
  176. // seems like file has vanished; rebuilding folder tree - but it's still an exception
  177. $this->_buildFolderTree($this->rootdir);
  178. throw new Exception\RuntimeException('seems like the maildir has vanished, I\'ve rebuild the ' .
  179. 'folder tree, search for an other folder and try again', 0, $e);
  180. }
  181. }
  182. /**
  183. * get \Zend\Mail\Storage\Folder instance for current folder
  184. *
  185. * @return \Zend\Mail\Storage\Folder instance of current folder
  186. */
  187. public function getCurrentFolder()
  188. {
  189. return $this->currentFolder;
  190. }
  191. }