PageRenderTime 25ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/tags/release-0.8.0/library/Zend/Mail/Storage/Maildir.php

https://github.com/bhaumik25/zend-framework
PHP | 301 lines | 137 code | 40 blank | 124 comment | 23 complexity | 12a25acf0ebc9f7b73e89269ced7deee MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to version 1.0 of the Zend Framework
  8. * license, that is bundled with this package in the file LICENSE, and
  9. * is available through the world-wide-web at the following URL:
  10. * http://www.zend.com/license/framework/1_0.txt. If you did not receive
  11. * a copy of the Zend Framework license and are unable to obtain it
  12. * through the world-wide-web, please send a note to license@zend.com
  13. * so we can mail you a copy immediately.
  14. *
  15. * @package Zend_Mail
  16. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
  18. */
  19. /**
  20. * Zend_Mail_Storage_Abstract
  21. */
  22. require_once 'Zend/Mail/Storage/Abstract.php';
  23. /**
  24. * Zend_Mail_Message
  25. */
  26. require_once 'Zend/Mail/Message.php';
  27. /**
  28. * Zend_Mail_Storage_Exception
  29. */
  30. require_once 'Zend/Mail/Storage/Exception.php';
  31. /**
  32. * @package Zend_Mail
  33. * @copyright Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://www.zend.com/license/framework/1_0.txt Zend Framework License version 1.0
  35. */
  36. class Zend_Mail_Storage_Maildir extends Zend_Mail_Storage_Abstract
  37. {
  38. /**
  39. * data of found message files in maildir dir
  40. * @var array
  41. */
  42. protected $_files = array();
  43. /**
  44. * known flag chars in filenames
  45. * @var array
  46. */
  47. protected static $_knownFlags = array('P' => 'Passed',
  48. 'R' => 'Replied',
  49. 'S' => 'Seen',
  50. 'T' => 'Trashed',
  51. 'D' => 'Draft',
  52. 'F' => 'Flagged');
  53. /**
  54. * Count messages all messages in current box
  55. *
  56. * @return int number of messages
  57. * @throws Zend_Mail_Storage_Exception
  58. */
  59. public function countMessages()
  60. {
  61. return count($this->_files);
  62. }
  63. /**
  64. * Get one or all fields from file structure. Also checks if message is valid
  65. *
  66. * @param int $id message number
  67. * @param string|null $field wanted field
  68. * @return string|array wanted field or all fields as array
  69. * @throws Zend_Mail_Storage_Exception
  70. */
  71. protected function _getFileData($id, $field = null)
  72. {
  73. if (!isset($this->_files[$id - 1])) {
  74. throw new Zend_Mail_Storage_Exception('id does not exist');
  75. }
  76. if (!$field) {
  77. return $this->_files[$id - 1];
  78. }
  79. if (!isset($this->_files[$id - 1][$field])) {
  80. throw new Zend_Mail_Storage_Exception('field does not exist');
  81. }
  82. return $this->_files[$id - 1][$field];
  83. }
  84. /**
  85. * Get a list of messages with number and size
  86. *
  87. * @param int|null $id number of message or null for all messages
  88. * @return int|array size of given message of list with all messages as array(num => size)
  89. * @throws Zend_Mail_Storage_Exception
  90. */
  91. public function getSize($id = null)
  92. {
  93. if ($id !== null) {
  94. return filesize($this->_getFileData($id, 'filename'));
  95. }
  96. $result = array();
  97. foreach ($this->_files as $num => $pos) {
  98. $result[$num + 1] = filesize($this->_files[$num]['filename']);
  99. }
  100. return $result;
  101. }
  102. /**
  103. * Fetch a message
  104. *
  105. * @param int $id number of message
  106. * @return Zend_Mail_Message
  107. * @throws Zend_Mail_Storage_Exception
  108. */
  109. public function getMessage($id)
  110. {
  111. return new Zend_Mail_Message(array('handler' => $this, 'id' => $id, 'headers' => $this->getRawHeader($id)));
  112. }
  113. /*
  114. * Get raw header of message or part
  115. *
  116. * @param int $id number of message
  117. * @param null|array|string $part path to part or null for messsage header
  118. * @param int $topLines include this many lines with header (after an empty line)
  119. * @return string raw header
  120. * @throws Zend_Mail_Storage_Exception
  121. */
  122. public function getRawHeader($id, $part = null, $topLines = 0)
  123. {
  124. if ($part !== null) {
  125. // TODO: implement
  126. throw new Zend_Mail_Storage_Exception('not implemented');
  127. }
  128. $fh = fopen($this->_getFileData($id, 'filename'), 'r');
  129. $content = '';
  130. while (!feof($fh)) {
  131. $line = fgets($fh);
  132. if (!trim($line)) {
  133. break;
  134. }
  135. $content .= $line;
  136. }
  137. fclose($fh);
  138. return $content;
  139. }
  140. /*
  141. * Get raw content of message or part
  142. *
  143. * @param int $id number of message
  144. * @param null|array|string $part path to part or null for messsage content
  145. * @return string raw content
  146. * @throws Zend_Mail_Storage_Exception
  147. */
  148. public function getRawContent($id, $part = null)
  149. {
  150. if ($part !== null) {
  151. // TODO: implement
  152. throw new Zend_Mail_Storage_Exception('not implemented');
  153. }
  154. $fh = fopen($this->_getFileData($id, 'filename'), 'r');
  155. while (!feof($fh)) {
  156. $line = fgets($fh);
  157. if (!trim($line)) {
  158. break;
  159. }
  160. }
  161. $content = stream_get_contents($fh);
  162. fclose($fh);
  163. return $content;
  164. }
  165. /**
  166. * Create instance with parameters
  167. * Supported parameters are:
  168. * - dirname dirname of mbox file
  169. *
  170. * @param $params array mail reader specific parameters
  171. * @throws Zend_Mail_Storage_Exception
  172. */
  173. public function __construct($params)
  174. {
  175. if (!isset($params['dirname']) || !is_dir($params['dirname'])) {
  176. throw new Zend_Mail_Storage_Exception('no valid dirname given in params');
  177. }
  178. if (!$this->_isMaildir($params['dirname'])) {
  179. throw new Zend_Mail_Storage_Exception('invalid maildir given');
  180. }
  181. $this->_has['top'] = true;
  182. $this->_openMaildir($params['dirname']);
  183. }
  184. /**
  185. * check if a given dir is a valid maildir
  186. *
  187. * @param string $dirname name of dir
  188. * @return bool dir is valid maildir
  189. */
  190. protected function _isMaildir($dirname)
  191. {
  192. return is_dir($dirname . '/cur');
  193. }
  194. /**
  195. * open given dir as current maildir
  196. *
  197. * @param string $dirname name of maildir
  198. * @return null
  199. * @throws Zend_Mail_Storage_Exception
  200. */
  201. protected function _openMaildir($dirname)
  202. {
  203. if ($this->_files) {
  204. $this->close();
  205. }
  206. $dh = @opendir($dirname . '/cur/');
  207. if (!$dh) {
  208. throw new Zend_Mail_Storage_Exception('cannot open maildir');
  209. }
  210. while (($entry = readdir($dh)) !== false) {
  211. if ($entry[0] == '.' || !is_file($dirname . '/cur/' . $entry)) {
  212. continue;
  213. }
  214. list($uniq, $info) = explode(':', $entry, 2);
  215. list($version, $flags) = explode(',', $info, 2);
  216. if ($version != 2) {
  217. $flags = '';
  218. } else {
  219. $named_flags = array();
  220. $length = strlen($flags);
  221. for ($i = 0; $i < $length; ++$i) {
  222. $flag = $flags[$i];
  223. $named_flags[$flag] = isset(self::$_knownFlags[$flag]) ? self::$_knownFlags[$flag] : '';
  224. }
  225. }
  226. $this->_files[] = array('uniq' => $uniq,
  227. 'flags' => $named_flags,
  228. 'filename' => $dirname . '/cur/' . $entry);
  229. }
  230. closedir($dh);
  231. }
  232. /**
  233. * Close resource for mail lib. If you need to control, when the resource
  234. * is closed. Otherwise the destructor would call this.
  235. *
  236. * @return void
  237. */
  238. public function close()
  239. {
  240. $this->_files = array();
  241. }
  242. /**
  243. * Waste some CPU cycles doing nothing.
  244. *
  245. * @return void
  246. */
  247. public function noop()
  248. {
  249. return true;
  250. }
  251. /**
  252. * stub for not supported message deletion
  253. *
  254. * @return null
  255. * @throws Zend_Mail_Storage_Exception
  256. */
  257. public function removeMessage($id)
  258. {
  259. throw new Zend_Mail_Storage_Exception('maildir is (currently) read-only');
  260. }
  261. }