PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 1ms app.codeStats 0ms

/standard/tags/release-0.2.0/incubator/library/Zend/Mail/Maildir.php

https://github.com/bhaumik25/zend-framework
PHP | 222 lines | 109 code | 33 blank | 80 comment | 22 complexity | 3637752e6e736ab819f69224c328ac3a 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-2006 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_Abstract
  21. */
  22. require_once 'Zend/Mail/Abstract.php';
  23. /**
  24. * Zend_Mail_Message
  25. */
  26. require_once 'Zend/Mail/Message.php';
  27. /**
  28. * Zend_Mail_Exception
  29. */
  30. require_once 'Zend/Mail/Exception.php';
  31. /**
  32. * @package Zend_Mail
  33. * @copyright Copyright (c) 2005-2006 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_Maildir extends Zend_Mail_Abstract
  37. {
  38. private $_files = array();
  39. private static $_knownFlags = array('P' => 'Passed',
  40. 'R' => 'Replied',
  41. 'S' => 'Seen',
  42. 'T' => 'Trashed',
  43. 'D' => 'Draft',
  44. 'F' => 'Flagged');
  45. /**
  46. * Count messages all messages in current box
  47. * Flags are not supported (exceptions is thrown)
  48. *
  49. * @param int $flags filter by flags
  50. * @throws Zend_Mail_Exception
  51. * @return int number of messages
  52. */
  53. public function countMessages($flags = null)
  54. {
  55. return count($this->_files);
  56. }
  57. /**
  58. * Get a list of messages with number and size
  59. *
  60. * @param int $id number of message
  61. * @return int|array size of given message of list with all messages as array(num => size)
  62. */
  63. public function getSize($id = 0)
  64. {
  65. if($id) {
  66. if(!isset($this->_files[$id - 1])) {
  67. throw new Zend_Mail_Exception('id does not exist');
  68. }
  69. return filesize($this->_files[$id - 1]['filename']);
  70. }
  71. $result = array();
  72. foreach($this->_files as $num => $pos) {
  73. $result[$num + 1] = filesize($this->_files[$num]['filename']);
  74. }
  75. return $result;
  76. }
  77. /**
  78. * Get a message with headers and body
  79. *
  80. * @param int $id number of message
  81. * @return Zend_Mail_Message
  82. */
  83. public function getMessage($id)
  84. {
  85. if(!isset($this->_files[$id - 1])) {
  86. throw new Zend_Mail_Exception('id does not exist');
  87. }
  88. return new Zend_Mail_Message(file_get_contents($this->_files[$id - 1]['filename']));
  89. }
  90. /**
  91. * Get a message with only header and $bodyLines lines of body
  92. *
  93. * @param int $id number of message
  94. * @param int $bodyLines also retrieve this number of body lines
  95. * @return Zend_Mail_Message
  96. */
  97. public function getHeader($id, $bodyLines = 0)
  98. {
  99. if(!isset($this->_files[$id - 1])) {
  100. throw new Zend_Mail_Exception('id does not exist');
  101. }
  102. $inHeader = true;
  103. $message = '';
  104. $fh = fopen($this->_files[$id - 1]['filename'], 'r');
  105. while(!feof($fh) && ($inHeader || $bodyLines--)) {
  106. $line = fgets($fh);
  107. if ($inHeader && !trim($line)) {
  108. if (!$bodyLines) {
  109. break;
  110. } else {
  111. $inHeader = false;
  112. }
  113. }
  114. $message .= $line;
  115. }
  116. fclose($fh);
  117. if (!$inHeader) {
  118. return new Zend_Mail_Message($message);
  119. } else {
  120. return new Zend_Mail_Message('', $message);
  121. }
  122. }
  123. /**
  124. * Create instance with parameters
  125. * Supported parameters are:
  126. * - filename filename of mbox file
  127. *
  128. * @param $params array mail reader specific parameters
  129. * @throws Zend_Mail_Exception
  130. */
  131. public function __construct($params)
  132. {
  133. if (!isset($params['dirname']) || !is_dir($params['dirname'])) {
  134. throw new Zend_Mail_Exception('no valid dirname given in params');
  135. }
  136. if(!is_dir($params['dirname'] . '/cur')) {
  137. throw new Zend_Mail_Exception('invalid maildir given');
  138. }
  139. $dh = opendir($params['dirname'] . '/cur/');
  140. while(($entry = readdir($dh)) !== false) {
  141. if($entry[0] == '.' || !is_file($params['dirname'] . '/cur/' . $entry)) {
  142. continue;
  143. }
  144. list($uniq, $info) = explode(':', $entry, 2);
  145. list($version, $flags) = explode(',', $info, 2);
  146. if($version != 2) {
  147. $flags = '';
  148. } else {
  149. $named_flags = array();
  150. $length = strlen($flags);
  151. for($i = 0; $i < $length; ++$i) {
  152. $flag = $flags[$i];
  153. $named_flags[$flag] = isset(self::$_knownFlags[$flag]) ? self::$_knownFlags[$flag] : '';
  154. }
  155. }
  156. $this->_files[] = array('uniq' => $uniq,
  157. 'flags' => $named_flags,
  158. 'filename' => $params['dirname'] . '/cur/' . $entry);
  159. }
  160. closedir($dh);
  161. $this->_has['top'] = true;
  162. }
  163. /**
  164. * Close resource for mail lib. If you need to control, when the resource
  165. * is closed. Otherwise the destructor would call this.
  166. *
  167. * @return void
  168. */
  169. public function close()
  170. {
  171. $this->_files = array();
  172. }
  173. /**
  174. * Waste some CPU cycles doing nothing.
  175. *
  176. * @return void
  177. */
  178. public function noop()
  179. {
  180. return true;
  181. }
  182. /**
  183. * stub for not supported message deletion
  184. */
  185. public function removeMessage($id)
  186. {
  187. throw new Zend_Mail_Exception('maildir is (currently) read-only');
  188. }
  189. }