Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Pop3.php
Go to the documentation of this file.
1 <?php
32 {
36  const TIMEOUT_CONNECTION = 30;
37 
42  public $hasTop = null;
43 
48  protected $_socket;
49 
54  protected $_timestamp;
55 
56 
65  public function __construct($host = '', $port = null, $ssl = false)
66  {
67  if ($host) {
68  $this->connect($host, $port, $ssl);
69  }
70  }
71 
72 
76  public function __destruct()
77  {
78  $this->logout();
79  }
80 
81 
91  public function connect($host, $port = null, $ssl = false)
92  {
93  if ($ssl == 'SSL') {
94  $host = 'ssl://' . $host;
95  }
96 
97  if ($port === null) {
98  $port = $ssl == 'SSL' ? 995 : 110;
99  }
100 
101  $errno = 0;
102  $errstr = '';
103  $this->_socket = @fsockopen($host, $port, $errno, $errstr, self::TIMEOUT_CONNECTION);
104  if (!$this->_socket) {
108  #require_once 'Zend/Mail/Protocol/Exception.php';
109  throw new Zend_Mail_Protocol_Exception('cannot connect to host; error = ' . $errstr .
110  ' (errno = ' . $errno . ' )');
111  }
112 
113  $welcome = $this->readResponse();
114 
115  strtok($welcome, '<');
116  $this->_timestamp = strtok('>');
117  if (!strpos($this->_timestamp, '@')) {
118  $this->_timestamp = null;
119  } else {
120  $this->_timestamp = '<' . $this->_timestamp . '>';
121  }
122 
123  if ($ssl === 'TLS') {
124  $this->request('STLS');
125  // TODO: Add STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT in the future when it is supported by PHP
126  $result = stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT);
127  if (!$result) {
131  #require_once 'Zend/Mail/Protocol/Exception.php';
132  throw new Zend_Mail_Protocol_Exception('cannot enable TLS');
133  }
134  }
135 
136  return $welcome;
137  }
138 
139 
147  public function sendRequest($request)
148  {
149  $result = @fputs($this->_socket, $request . "\r\n");
150  if (!$result) {
154  #require_once 'Zend/Mail/Protocol/Exception.php';
155  throw new Zend_Mail_Protocol_Exception('send failed - connection closed?');
156  }
157  }
158 
159 
167  public function readResponse($multiline = false)
168  {
169  $result = @fgets($this->_socket);
170  if (!is_string($result)) {
174  #require_once 'Zend/Mail/Protocol/Exception.php';
175  throw new Zend_Mail_Protocol_Exception('read failed - connection closed?');
176  }
177 
178  $result = trim($result);
179  if (strpos($result, ' ')) {
180  list($status, $message) = explode(' ', $result, 2);
181  } else {
182  $status = $result;
183  $message = '';
184  }
185 
186  if ($status != '+OK') {
190  #require_once 'Zend/Mail/Protocol/Exception.php';
191  throw new Zend_Mail_Protocol_Exception('last request failed');
192  }
193 
194  if ($multiline) {
195  $message = '';
196  $line = fgets($this->_socket);
197  while ($line && rtrim($line, "\r\n") != '.') {
198  if ($line[0] == '.') {
199  $line = substr($line, 1);
200  }
201  $message .= $line;
202  $line = fgets($this->_socket);
203  };
204  }
205 
206  return $message;
207  }
208 
209 
220  public function request($request, $multiline = false)
221  {
222  $this->sendRequest($request);
223  return $this->readResponse($multiline);
224  }
225 
226 
232  public function logout()
233  {
234  if (!$this->_socket) {
235  return;
236  }
237 
238  try {
239  $this->request('QUIT');
240  } catch (Zend_Mail_Protocol_Exception $e) {
241  // ignore error - we're closing the socket anyway
242  }
243 
244  fclose($this->_socket);
245  $this->_socket = null;
246  }
247 
248 
255  public function capa()
256  {
257  $result = $this->request('CAPA', true);
258  return explode("\n", $result);
259  }
260 
261 
271  public function login($user, $password, $tryApop = true)
272  {
273  if ($tryApop && $this->_timestamp) {
274  try {
275  $this->request("APOP $user " . md5($this->_timestamp . $password));
276  return;
277  } catch (Zend_Mail_Protocol_Exception $e) {
278  // ignore
279  }
280  }
281 
282  $result = $this->request("USER $user");
283  $result = $this->request("PASS $password");
284  }
285 
286 
295  public function status(&$messages, &$octets)
296  {
297  $messages = 0;
298  $octets = 0;
299  $result = $this->request('STAT');
300 
301  list($messages, $octets) = explode(' ', $result);
302  }
303 
304 
312  public function getList($msgno = null)
313  {
314  if ($msgno !== null) {
315  $result = $this->request("LIST $msgno");
316 
317  list(, $result) = explode(' ', $result);
318  return (int)$result;
319  }
320 
321  $result = $this->request('LIST', true);
322  $messages = array();
323  $line = strtok($result, "\n");
324  while ($line) {
325  list($no, $size) = explode(' ', trim($line));
326  $messages[(int)$no] = (int)$size;
327  $line = strtok("\n");
328  }
329 
330  return $messages;
331  }
332 
333 
341  public function uniqueid($msgno = null)
342  {
343  if ($msgno !== null) {
344  $result = $this->request("UIDL $msgno");
345 
346  list(, $result) = explode(' ', $result);
347  return $result;
348  }
349 
350  $result = $this->request('UIDL', true);
351 
352  $result = explode("\n", $result);
353  $messages = array();
354  foreach ($result as $line) {
355  if (!$line) {
356  continue;
357  }
358  list($no, $id) = explode(' ', trim($line), 2);
359  $messages[(int)$no] = $id;
360  }
361 
362  return $messages;
363 
364  }
365 
366 
380  public function top($msgno, $lines = 0, $fallback = false)
381  {
382  if ($this->hasTop === false) {
383  if ($fallback) {
384  return $this->retrieve($msgno);
385  } else {
389  #require_once 'Zend/Mail/Protocol/Exception.php';
390  throw new Zend_Mail_Protocol_Exception('top not supported and no fallback wanted');
391  }
392  }
393  $this->hasTop = true;
394 
395  $lines = (!$lines || $lines < 1) ? 0 : (int)$lines;
396 
397  try {
398  $result = $this->request("TOP $msgno $lines", true);
399  } catch (Zend_Mail_Protocol_Exception $e) {
400  $this->hasTop = false;
401  if ($fallback) {
402  $result = $this->retrieve($msgno);
403  } else {
404  throw $e;
405  }
406  }
407 
408  return $result;
409  }
410 
411 
420  public function retrive($msgno)
421  {
422  return $this->retrieve($msgno);
423  }
424 
425 
433  public function retrieve($msgno)
434  {
435  $result = $this->request("RETR $msgno", true);
436  return $result;
437  }
438 
445  public function noop()
446  {
447  $this->request('NOOP');
448  }
449 
450 
457  public function delete($msgno)
458  {
459  $this->request("DELE $msgno");
460  }
461 
462 
469  public function undelete()
470  {
471  $this->request('RSET');
472  }
473 }
connect($host, $port=null, $ssl=false)
Definition: Pop3.php:91
const TIMEOUT_CONNECTION
Definition: Pop3.php:36
status(&$messages, &$octets)
Definition: Pop3.php:295
fsockopen(&$errorNumber, &$errorMessage)
Definition: http_mock.php:37
$id
Definition: fieldset.phtml:14
request($request, $multiline=false)
Definition: Pop3.php:220
$message
uniqueid($msgno=null)
Definition: Pop3.php:341
$user
Definition: dummy_user.php:13
$status
Definition: order_status.php:8
top($msgno, $lines=0, $fallback=false)
Definition: Pop3.php:380
sendRequest($request)
Definition: Pop3.php:147
__construct($host='', $port=null, $ssl=false)
Definition: Pop3.php:65
getList($msgno=null)
Definition: Pop3.php:312
login($user, $password, $tryApop=true)
Definition: Pop3.php:271
readResponse($multiline=false)
Definition: Pop3.php:167