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

/lib/Zend/Mail/Protocol/Pop3.php

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