PageRenderTime 21ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/system/Zend/Mail/Storage/Pop3.php

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