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

/library/Zend/Mail/Storage/Pop3.php

https://github.com/shevron/zf2
PHP | 286 lines | 136 code | 30 blank | 120 comment | 17 complexity | 1bbcb8131d34a5dfbf1a0a7862922358 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;
  11. use Zend\Mail\Exception as MailException;
  12. use Zend\Mail\Protocol;
  13. use Zend\Mime;
  14. /**
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage Storage
  18. */
  19. class Pop3 extends AbstractStorage
  20. {
  21. /**
  22. * protocol handler
  23. * @var null|\Zend\Mail\Protocol\Pop3
  24. */
  25. protected $_protocol;
  26. /**
  27. * Count messages all messages in current box
  28. *
  29. * @return int number of messages
  30. * @throws \Zend\Mail\Storage\Exception\ExceptionInterface
  31. * @throws \Zend\Mail\Protocol\Exception\ExceptionInterface
  32. */
  33. public function countMessages()
  34. {
  35. $count = 0; // "Declare" variable before first usage.
  36. $octets = 0; // "Declare" variable since it's passed by reference
  37. $this->_protocol->status($count, $octets);
  38. return (int)$count;
  39. }
  40. /**
  41. * get a list of messages with number and size
  42. *
  43. * @param int $id number of message
  44. * @return int|array size of given message of list with all messages as array(num => size)
  45. * @throws \Zend\Mail\Protocol\Exception\ExceptionInterface
  46. */
  47. public function getSize($id = 0)
  48. {
  49. $id = $id ? $id : null;
  50. return $this->_protocol->getList($id);
  51. }
  52. /**
  53. * Fetch a message
  54. *
  55. * @param int $id number of message
  56. * @return \Zend\Mail\Storage\Message
  57. * @throws \Zend\Mail\Protocol\Exception\ExceptionInterface
  58. */
  59. public function getMessage($id)
  60. {
  61. $bodyLines = 0;
  62. $message = $this->_protocol->top($id, $bodyLines, true);
  63. return new $this->_messageClass(array('handler' => $this, 'id' => $id, 'headers' => $message,
  64. 'noToplines' => $bodyLines < 1));
  65. }
  66. /*
  67. * Get raw header of message or part
  68. *
  69. * @param int $id number of message
  70. * @param null|array|string $part path to part or null for message header
  71. * @param int $topLines include this many lines with header (after an empty line)
  72. * @return string raw header
  73. * @throws \Zend\Mail\Protocol\Exception\ExceptionInterface
  74. * @throws \Zend\Mail\Storage\Exception\ExceptionInterface
  75. */
  76. public function getRawHeader($id, $part = null, $topLines = 0)
  77. {
  78. if ($part !== null) {
  79. // TODO: implement
  80. throw new Exception\RuntimeException('not implemented');
  81. }
  82. return $this->_protocol->top($id, 0, true);
  83. }
  84. /*
  85. * Get raw content of message or part
  86. *
  87. * @param int $id number of message
  88. * @param null|array|string $part path to part or null for message content
  89. * @return string raw content
  90. * @throws \Zend\Mail\Protocol\Exception\ExceptionInterface
  91. * @throws \Zend\Mail\Storage\Exception\ExceptionInterface
  92. */
  93. public function getRawContent($id, $part = null)
  94. {
  95. if ($part !== null) {
  96. // TODO: implement
  97. throw new Exception\RuntimeException('not implemented');
  98. }
  99. $content = $this->_protocol->retrieve($id);
  100. // TODO: find a way to avoid decoding the headers
  101. $headers = null; // "Declare" variable since it's passed by reference
  102. $body = null; // "Declare" variable before first usage.
  103. Mime\Decode::splitMessage($content, $headers, $body);
  104. return $body;
  105. }
  106. /**
  107. * create instance with parameters
  108. * Supported parameters are
  109. * - host hostname or ip address of POP3 server
  110. * - user username
  111. * - password password for user 'username' [optional, default = '']
  112. * - port port for POP3 server [optional, default = 110]
  113. * - ssl 'SSL' or 'TLS' for secure sockets
  114. *
  115. * @param $params array mail reader specific parameters
  116. * @throws \Zend\Mail\Storage\Exception\InvalidArgumentException
  117. * @throws \Zend\Mail\Protocol\Exception\RuntimeException
  118. */
  119. public function __construct($params)
  120. {
  121. if (is_array($params)) {
  122. $params = (object)$params;
  123. }
  124. $this->_has['fetchPart'] = false;
  125. $this->_has['top'] = null;
  126. $this->_has['uniqueid'] = null;
  127. if ($params instanceof Protocol\Pop3) {
  128. $this->_protocol = $params;
  129. return;
  130. }
  131. if (!isset($params->user)) {
  132. throw new Exception\InvalidArgumentException('need at least user in params');
  133. }
  134. $host = isset($params->host) ? $params->host : 'localhost';
  135. $password = isset($params->password) ? $params->password : '';
  136. $port = isset($params->port) ? $params->port : null;
  137. $ssl = isset($params->ssl) ? $params->ssl : false;
  138. $this->_protocol = new Protocol\Pop3();
  139. $this->_protocol->connect($host, $port, $ssl);
  140. $this->_protocol->login($params->user, $password);
  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. public function close()
  147. {
  148. $this->_protocol->logout();
  149. }
  150. /**
  151. * Keep the server busy.
  152. *
  153. * @throws \Zend\Mail\Protocol\Exception\RuntimeException
  154. */
  155. public function noop()
  156. {
  157. $this->_protocol->noop();
  158. }
  159. /**
  160. * Remove a message from server. If you're doing that from a web environment
  161. * you should be careful and use a uniqueid as parameter if possible to
  162. * identify the message.
  163. *
  164. * @param int $id number of message
  165. * @throws \Zend\Mail\Protocol\Exception\RuntimeException
  166. */
  167. public function removeMessage($id)
  168. {
  169. $this->_protocol->delete($id);
  170. }
  171. /**
  172. * get unique id for one or all messages
  173. *
  174. * if storage does not support unique ids it's the same as the message number
  175. *
  176. * @param int|null $id message number
  177. * @return array|string message number for given message or all messages as array
  178. * @throws \Zend\Mail\Storage\Exception\ExceptionInterface
  179. */
  180. public function getUniqueId($id = null)
  181. {
  182. if (!$this->hasUniqueid) {
  183. if ($id) {
  184. return $id;
  185. }
  186. $count = $this->countMessages();
  187. if ($count < 1) {
  188. return array();
  189. }
  190. $range = range(1, $count);
  191. return array_combine($range, $range);
  192. }
  193. return $this->_protocol->uniqueid($id);
  194. }
  195. /**
  196. * get a message number from a unique id
  197. *
  198. * I.e. if you have a webmailer that supports deleting messages you should use unique ids
  199. * as parameter and use this method to translate it to message number right before calling removeMessage()
  200. *
  201. * @param string $id unique id
  202. * @throws Exception\InvalidArgumentException
  203. * @return int message number
  204. */
  205. public function getNumberByUniqueId($id)
  206. {
  207. if (!$this->hasUniqueid) {
  208. return $id;
  209. }
  210. $ids = $this->getUniqueId();
  211. foreach ($ids as $k => $v) {
  212. if ($v == $id) {
  213. return $k;
  214. }
  215. }
  216. throw new Exception\InvalidArgumentException('unique id not found');
  217. }
  218. /**
  219. * Special handling for hasTop and hasUniqueid. The headers of the first message is
  220. * retrieved if Top wasn't needed/tried yet.
  221. *
  222. * @see AbstractStorage::__get()
  223. * @param string $var
  224. * @return string
  225. */
  226. public function __get($var)
  227. {
  228. $result = parent::__get($var);
  229. if ($result !== null) {
  230. return $result;
  231. }
  232. if (strtolower($var) == 'hastop') {
  233. if ($this->_protocol->hasTop === null) {
  234. // need to make a real call, because not all server are honest in their capas
  235. try {
  236. $this->_protocol->top(1, 0, false);
  237. } catch(MailException\ExceptionInterface $e) {
  238. // ignoring error
  239. }
  240. }
  241. $this->_has['top'] = $this->_protocol->hasTop;
  242. return $this->_protocol->hasTop;
  243. }
  244. if (strtolower($var) == 'hasuniqueid') {
  245. $id = null;
  246. try {
  247. $id = $this->_protocol->uniqueid(1);
  248. } catch(MailException\ExceptionInterface $e) {
  249. // ignoring error
  250. }
  251. $this->_has['uniqueid'] = $id ? true : false;
  252. return $this->_has['uniqueid'];
  253. }
  254. return $result;
  255. }
  256. }