/framework/vendor/zend/Zend/Mail/Storage/Abstract.php

http://zoop.googlecode.com/ · PHP · 366 lines · 101 code · 53 blank · 212 comment · 6 complexity · 1ab551f4e641d852aab46e5faa8721f5 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage Storage
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Abstract.php 20096 2010-01-06 02:05:09Z bkarwin $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Mail
  25. * @subpackage Storage
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. abstract class Zend_Mail_Storage_Abstract implements Countable, ArrayAccess, SeekableIterator
  30. {
  31. /**
  32. * class capabilities with default values
  33. * @var array
  34. */
  35. protected $_has = array('uniqueid' => true,
  36. 'delete' => false,
  37. 'create' => false,
  38. 'top' => false,
  39. 'fetchPart' => true,
  40. 'flags' => false);
  41. /**
  42. * current iteration position
  43. * @var int
  44. */
  45. protected $_iterationPos = 0;
  46. /**
  47. * maximum iteration position (= message count)
  48. * @var null|int
  49. */
  50. protected $_iterationMax = null;
  51. /**
  52. * used message class, change it in an extened class to extend the returned message class
  53. * @var string
  54. */
  55. protected $_messageClass = 'Zend_Mail_Message';
  56. /**
  57. * Getter for has-properties. The standard has properties
  58. * are: hasFolder, hasUniqueid, hasDelete, hasCreate, hasTop
  59. *
  60. * The valid values for the has-properties are:
  61. * - true if a feature is supported
  62. * - false if a feature is not supported
  63. * - null is it's not yet known or it can't be know if a feature is supported
  64. *
  65. * @param string $var property name
  66. * @return bool supported or not
  67. * @throws Zend_Mail_Storage_Exception
  68. */
  69. public function __get($var)
  70. {
  71. if (strpos($var, 'has') === 0) {
  72. $var = strtolower(substr($var, 3));
  73. return isset($this->_has[$var]) ? $this->_has[$var] : null;
  74. }
  75. /**
  76. * @see Zend_Mail_Storage_Exception
  77. */
  78. require_once 'Zend/Mail/Storage/Exception.php';
  79. throw new Zend_Mail_Storage_Exception($var . ' not found');
  80. }
  81. /**
  82. * Get a full list of features supported by the specific mail lib and the server
  83. *
  84. * @return array list of features as array(featurename => true|false[|null])
  85. */
  86. public function getCapabilities()
  87. {
  88. return $this->_has;
  89. }
  90. /**
  91. * Count messages messages in current box/folder
  92. *
  93. * @return int number of messages
  94. * @throws Zend_Mail_Storage_Exception
  95. */
  96. abstract public function countMessages();
  97. /**
  98. * Get a list of messages with number and size
  99. *
  100. * @param int $id number of message
  101. * @return int|array size of given message of list with all messages as array(num => size)
  102. */
  103. abstract public function getSize($id = 0);
  104. /**
  105. * Get a message with headers and body
  106. *
  107. * @param $id int number of message
  108. * @return Zend_Mail_Message
  109. */
  110. abstract public function getMessage($id);
  111. /**
  112. * Get raw header of message or part
  113. *
  114. * @param int $id number of message
  115. * @param null|array|string $part path to part or null for messsage header
  116. * @param int $topLines include this many lines with header (after an empty line)
  117. * @return string raw header
  118. */
  119. abstract public function getRawHeader($id, $part = null, $topLines = 0);
  120. /**
  121. * Get raw content of message or part
  122. *
  123. * @param int $id number of message
  124. * @param null|array|string $part path to part or null for messsage content
  125. * @return string raw content
  126. */
  127. abstract public function getRawContent($id, $part = null);
  128. /**
  129. * Create instance with parameters
  130. *
  131. * @param array $params mail reader specific parameters
  132. * @throws Zend_Mail_Storage_Exception
  133. */
  134. abstract public function __construct($params);
  135. /**
  136. * Destructor calls close() and therefore closes the resource.
  137. */
  138. public function __destruct()
  139. {
  140. $this->close();
  141. }
  142. /**
  143. * Close resource for mail lib. If you need to control, when the resource
  144. * is closed. Otherwise the destructor would call this.
  145. *
  146. * @return null
  147. */
  148. abstract public function close();
  149. /**
  150. * Keep the resource alive.
  151. *
  152. * @return null
  153. */
  154. abstract public function noop();
  155. /**
  156. * delete a message from current box/folder
  157. *
  158. * @return null
  159. */
  160. abstract public function removeMessage($id);
  161. /**
  162. * get unique id for one or all messages
  163. *
  164. * if storage does not support unique ids it's the same as the message number
  165. *
  166. * @param int|null $id message number
  167. * @return array|string message number for given message or all messages as array
  168. * @throws Zend_Mail_Storage_Exception
  169. */
  170. abstract public function getUniqueId($id = null);
  171. /**
  172. * get a message number from a unique id
  173. *
  174. * I.e. if you have a webmailer that supports deleting messages you should use unique ids
  175. * as parameter and use this method to translate it to message number right before calling removeMessage()
  176. *
  177. * @param string $id unique id
  178. * @return int message number
  179. * @throws Zend_Mail_Storage_Exception
  180. */
  181. abstract public function getNumberByUniqueId($id);
  182. // interface implementations follows
  183. /**
  184. * Countable::count()
  185. *
  186. * @return int
  187. */
  188. public function count()
  189. {
  190. return $this->countMessages();
  191. }
  192. /**
  193. * ArrayAccess::offsetExists()
  194. *
  195. * @param int $id
  196. * @return boolean
  197. */
  198. public function offsetExists($id)
  199. {
  200. try {
  201. if ($this->getMessage($id)) {
  202. return true;
  203. }
  204. } catch(Zend_Mail_Storage_Exception $e) {}
  205. return false;
  206. }
  207. /**
  208. * ArrayAccess::offsetGet()
  209. *
  210. * @param int $id
  211. * @return Zend_Mail_Message message object
  212. */
  213. public function offsetGet($id)
  214. {
  215. return $this->getMessage($id);
  216. }
  217. /**
  218. * ArrayAccess::offsetSet()
  219. *
  220. * @param id $id
  221. * @param mixed $value
  222. * @throws Zend_Mail_Storage_Exception
  223. * @return void
  224. */
  225. public function offsetSet($id, $value)
  226. {
  227. /**
  228. * @see Zend_Mail_Storage_Exception
  229. */
  230. require_once 'Zend/Mail/Storage/Exception.php';
  231. throw new Zend_Mail_Storage_Exception('cannot write mail messages via array access');
  232. }
  233. /**
  234. * ArrayAccess::offsetUnset()
  235. *
  236. * @param int $id
  237. * @return boolean success
  238. */
  239. public function offsetUnset($id)
  240. {
  241. return $this->removeMessage($id);
  242. }
  243. /**
  244. * Iterator::rewind()
  245. *
  246. * Rewind always gets the new count from the storage. Thus if you use
  247. * the interfaces and your scripts take long you should use reset()
  248. * from time to time.
  249. *
  250. * @return void
  251. */
  252. public function rewind()
  253. {
  254. $this->_iterationMax = $this->countMessages();
  255. $this->_iterationPos = 1;
  256. }
  257. /**
  258. * Iterator::current()
  259. *
  260. * @return Zend_Mail_Message current message
  261. */
  262. public function current()
  263. {
  264. return $this->getMessage($this->_iterationPos);
  265. }
  266. /**
  267. * Iterator::key()
  268. *
  269. * @return int id of current position
  270. */
  271. public function key()
  272. {
  273. return $this->_iterationPos;
  274. }
  275. /**
  276. * Iterator::next()
  277. *
  278. * @return void
  279. */
  280. public function next()
  281. {
  282. ++$this->_iterationPos;
  283. }
  284. /**
  285. * Iterator::valid()
  286. *
  287. * @return boolean
  288. */
  289. public function valid()
  290. {
  291. if ($this->_iterationMax === null) {
  292. $this->_iterationMax = $this->countMessages();
  293. }
  294. return $this->_iterationPos && $this->_iterationPos <= $this->_iterationMax;
  295. }
  296. /**
  297. * SeekableIterator::seek()
  298. *
  299. * @param int $pos
  300. * @return void
  301. * @throws OutOfBoundsException
  302. */
  303. public function seek($pos)
  304. {
  305. if ($this->_iterationMax === null) {
  306. $this->_iterationMax = $this->countMessages();
  307. }
  308. if ($pos > $this->_iterationMax) {
  309. throw new OutOfBoundsException('this position does not exist');
  310. }
  311. $this->_iterationPos = $pos;
  312. }
  313. }