PageRenderTime 64ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Mail/Protocol/Pop3.php

https://bitbucket.org/baruffaldi/cms-php-bfcms
PHP | 461 lines | 206 code | 64 blank | 191 comment | 33 complexity | 3a2ee85eda44ac359489d59757a17ce9 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 Protocol
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Pop3.php 9098 2008-03-30 19:29:10Z thomas $
  21. */
  22. /**
  23. * @category Zend
  24. * @package Zend_Mail
  25. * @subpackage Protocol
  26. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Mail_Protocol_Pop3
  30. {
  31. /**
  32. * saves if server supports top
  33. * @var null|bool
  34. */
  35. public $hasTop = null;
  36. /**
  37. * socket to pop3
  38. * @var null|resource
  39. */
  40. protected $_socket;
  41. /**
  42. * greeting timestamp for apop
  43. * @var null|string
  44. */
  45. protected $_timestamp;
  46. /**
  47. * Public constructor
  48. *
  49. * @param string $host hostname of IP address of POP3 server, if given connect() is called
  50. * @param int|null $port port of POP3 server, null for default (110 or 995 for ssl)
  51. * @param bool|string $ssl use ssl? 'SSL', 'TLS' or false
  52. * @throws Zend_Mail_Protocol_Exception
  53. */
  54. public function __construct($host = '', $port = null, $ssl = false)
  55. {
  56. if ($host) {
  57. $this->connect($host, $port, $ssl);
  58. }
  59. }
  60. /**
  61. * Public destructor
  62. */
  63. public function __destruct()
  64. {
  65. $this->logout();
  66. }
  67. /**
  68. * Open connection to POP3 server
  69. *
  70. * @param string $host hostname of IP address of POP3 server
  71. * @param int|null $port of POP3 server, default is 110 (995 for ssl)
  72. * @param string|bool $ssl use 'SSL', 'TLS' or false
  73. * @return string welcome message
  74. * @throws Zend_Mail_Protocol_Exception
  75. */
  76. public function connect($host, $port = null, $ssl = false)
  77. {
  78. if ($ssl == 'SSL') {
  79. $host = 'ssl://' . $host;
  80. }
  81. if ($port === null) {
  82. $port = $ssl == 'SSL' ? 995 : 110;
  83. }
  84. $this->_socket = @fsockopen($host, $port);
  85. if (!$this->_socket) {
  86. /**
  87. * @see Zend_Mail_Protocol_Exception
  88. */
  89. require_once 'Zend/Mail/Protocol/Exception.php';
  90. throw new Zend_Mail_Protocol_Exception('cannot connect to host');
  91. }
  92. $welcome = $this->readResponse();
  93. strtok($welcome, '<');
  94. $this->_timestamp = strtok('>');
  95. if (!strpos($this->_timestamp, '@')) {
  96. $this->_timestamp = null;
  97. } else {
  98. $this->_timestamp = '<' . $this->_timestamp . '>';
  99. }
  100. if ($ssl === 'TLS') {
  101. $this->request('STLS');
  102. $result = stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLS_CLIENT);
  103. if (!$result) {
  104. /**
  105. * @see Zend_Mail_Protocol_Exception
  106. */
  107. require_once 'Zend/Mail/Protocol/Exception.php';
  108. throw new Zend_Mail_Protocol_Exception('cannot enable TLS');
  109. }
  110. }
  111. return $welcome;
  112. }
  113. /**
  114. * Send a request
  115. *
  116. * @param string $request your request without newline
  117. * @return null
  118. * @throws Zend_Mail_Protocol_Exception
  119. */
  120. public function sendRequest($request)
  121. {
  122. $result = @fputs($this->_socket, $request . "\r\n");
  123. if (!$result) {
  124. /**
  125. * @see Zend_Mail_Protocol_Exception
  126. */
  127. require_once 'Zend/Mail/Protocol/Exception.php';
  128. throw new Zend_Mail_Protocol_Exception('send failed - connection closed?');
  129. }
  130. }
  131. /**
  132. * read a response
  133. *
  134. * @param boolean $multiline response has multiple lines and should be read until "<nl>.<nl>"
  135. * @return string response
  136. * @throws Zend_Mail_Protocol_Exception
  137. */
  138. public function readResponse($multiline = false)
  139. {
  140. $result = @fgets($this->_socket);
  141. if (!is_string($result)) {
  142. /**
  143. * @see Zend_Mail_Protocol_Exception
  144. */
  145. require_once 'Zend/Mail/Protocol/Exception.php';
  146. throw new Zend_Mail_Protocol_Exception('read failed - connection closed?');
  147. }
  148. $result = trim($result);
  149. if (strpos($result, ' ')) {
  150. list($status, $message) = explode(' ', $result, 2);
  151. } else {
  152. $status = $result;
  153. $message = '';
  154. }
  155. if ($status != '+OK') {
  156. /**
  157. * @see Zend_Mail_Protocol_Exception
  158. */
  159. require_once 'Zend/Mail/Protocol/Exception.php';
  160. throw new Zend_Mail_Protocol_Exception('last request failed');
  161. }
  162. if ($multiline) {
  163. $message = '';
  164. $line = fgets($this->_socket);
  165. while ($line && trim($line) != '.') {
  166. $message .= $line;
  167. $line = fgets($this->_socket);
  168. };
  169. }
  170. return $message;
  171. }
  172. /**
  173. * Send request and get resposne
  174. *
  175. * @see sendRequest(), readResponse()
  176. *
  177. * @param string $request request
  178. * @param bool $multiline multiline response?
  179. * @return string result from readResponse()
  180. * @throws Zend_Mail_Protocol_Exception
  181. */
  182. public function request($request, $multiline = false)
  183. {
  184. $this->sendRequest($request);
  185. return $this->readResponse($multiline);
  186. }
  187. /**
  188. * End communication with POP3 server (also closes socket)
  189. *
  190. * @return null
  191. */
  192. public function logout()
  193. {
  194. if (!$this->_socket) {
  195. return;
  196. }
  197. try {
  198. $this->request('QUIT');
  199. } catch (Zend_Mail_Protocol_Exception $e) {
  200. // ignore error - we're closing the socket anyway
  201. }
  202. fclose($this->_socket);
  203. $this->_socket = null;
  204. }
  205. /**
  206. * Get capabilities from POP3 server
  207. *
  208. * @return array list of capabilities
  209. * @throws Zend_Mail_Protocol_Exception
  210. */
  211. public function capa()
  212. {
  213. $result = $this->request('CAPA', true);
  214. return explode("\n", $result);
  215. }
  216. /**
  217. * Login to POP3 server. Can use APOP
  218. *
  219. * @param string $user username
  220. * @param string $password password
  221. * @param bool $try_apop should APOP be tried?
  222. * @return void
  223. * @throws Zend_Mail_Protocol_Exception
  224. */
  225. public function login($user, $password, $tryApop = true)
  226. {
  227. if ($tryApop && $this->_timestamp) {
  228. try {
  229. $this->request("APOP $user " . md5($this->_timestamp . $password));
  230. return;
  231. } catch (Zend_Mail_Protocol_Exception $e) {
  232. // ignore
  233. }
  234. }
  235. $result = $this->request("USER $user");
  236. $result = $this->request("PASS $password");
  237. }
  238. /**
  239. * Make STAT call for message count and size sum
  240. *
  241. * @param int $messages out parameter with count of messages
  242. * @param int $octets out parameter with size in octects of messages
  243. * @return void
  244. * @throws Zend_Mail_Protocol_Exception
  245. */
  246. public function status(&$messages, &$octets)
  247. {
  248. $messages = 0;
  249. $octets = 0;
  250. $result = $this->request('STAT');
  251. list($messages, $octets) = explode(' ', $result);
  252. }
  253. /**
  254. * Make LIST call for size of message(s)
  255. *
  256. * @param int|null $msgno number of message, null for all
  257. * @return int|array size of given message or list with array(num => size)
  258. * @throws Zend_Mail_Protocol_Exception
  259. */
  260. public function getList($msgno = null)
  261. {
  262. if ($msgno !== null) {
  263. $result = $this->request("LIST $msgno");
  264. list(, $result) = explode(' ', $result);
  265. return (int)$result;
  266. }
  267. $result = $this->request('LIST', true);
  268. $messages = array();
  269. $line = strtok($result, "\n");
  270. while ($line) {
  271. list($no, $size) = explode(' ', trim($line));
  272. $messages[(int)$no] = (int)$size;
  273. $line = strtok("\n");
  274. }
  275. return $messages;
  276. }
  277. /**
  278. * Make UIDL call for getting a uniqueid
  279. *
  280. * @param int|null $msgno number of message, null for all
  281. * @return string|array uniqueid of message or list with array(num => uniqueid)
  282. * @throws Zend_Mail_Protocol_Exception
  283. */
  284. public function uniqueid($msgno = null)
  285. {
  286. if ($msgno !== null) {
  287. $result = $this->request("UIDL $msgno");
  288. list(, $result) = explode(' ', $result);
  289. return $result;
  290. }
  291. $result = $this->request('UIDL', true);
  292. $result = explode("\n", $result);
  293. $messages = array();
  294. foreach ($result as $line) {
  295. if (!$line) {
  296. continue;
  297. }
  298. list($no, $id) = explode(' ', trim($line), 2);
  299. $messages[(int)$no] = $id;
  300. }
  301. return $messages;
  302. }
  303. /**
  304. * Make TOP call for getting headers and maybe some body lines
  305. * This method also sets hasTop - before it it's not known if top is supported
  306. *
  307. * The fallback makes normale RETR call, which retrieves the whole message. Additional
  308. * lines are not removed.
  309. *
  310. * @param int $msgno number of message
  311. * @param int $lines number of wanted body lines (empty line is inserted after header lines)
  312. * @param bool $fallback fallback with full retrieve if top is not supported
  313. * @return string message headers with wanted body lines
  314. * @throws Zend_Mail_Protocol_Exception
  315. */
  316. public function top($msgno, $lines = 0, $fallback = false)
  317. {
  318. if ($this->hasTop === false) {
  319. if ($fallback) {
  320. return $this->retrieve($msgno);
  321. } else {
  322. /**
  323. * @see Zend_Mail_Protocol_Exception
  324. */
  325. require_once 'Zend/Mail/Protocol/Exception.php';
  326. throw new Zend_Mail_Protocol_Exception('top not supported and no fallback wanted');
  327. }
  328. }
  329. $this->hasTop = true;
  330. $lines = (!$lines || $lines < 1) ? 0 : (int)$lines;
  331. try {
  332. $result = $this->request("TOP $msgno $lines", true);
  333. } catch (Zend_Mail_Protocol_Exception $e) {
  334. $this->hasTop = false;
  335. if ($fallback) {
  336. $result = $this->retrieve($msgno);
  337. } else {
  338. throw $e;
  339. }
  340. }
  341. return $result;
  342. }
  343. /**
  344. * Make a RETR call for retrieving a full message with headers and body
  345. *
  346. * @deprecated since 1.1.0; this method has a typo - please use retrieve()
  347. * @param int $msgno message number
  348. * @return string message
  349. * @throws Zend_Mail_Protocol_Exception
  350. */
  351. public function retrive($msgno)
  352. {
  353. return $this->retrieve($msgno);
  354. }
  355. /**
  356. * Make a RETR call for retrieving a full message with headers and body
  357. *
  358. * @param int $msgno message number
  359. * @return string message
  360. * @throws Zend_Mail_Protocol_Exception
  361. */
  362. public function retrieve($msgno)
  363. {
  364. $result = $this->request("RETR $msgno", true);
  365. return $result;
  366. }
  367. /**
  368. * Make a NOOP call, maybe needed for keeping the server happy
  369. *
  370. * @return null
  371. * @throws Zend_Mail_Protocol_Exception
  372. */
  373. public function noop()
  374. {
  375. $this->request('NOOP');
  376. }
  377. /**
  378. * Make a DELE count to remove a message
  379. *
  380. * @return null
  381. * @throws Zend_Mail_Protocol_Exception
  382. */
  383. public function delete($msgno)
  384. {
  385. $this->request("DELE $msgno");
  386. }
  387. /**
  388. * Make RSET call, which rollbacks delete requests
  389. *
  390. * @return null
  391. * @throws Zend_Mail_Protocol_Exception
  392. */
  393. public function undelete()
  394. {
  395. $this->request('RSET');
  396. }
  397. }