[ Index ]

MailPress 544

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]     [ Statistics ]    

title

Body

[close]

/mp-includes/class/ -> MP_Pop3.class.php (source)

   1  <?php
   2  class MP_Pop3
   3  {
   4      const bt = 132;
   5  
   6      var $pop3 = null;
   7  
   8  	function __construct($server, $port, $username, $password, $trace = false)
   9      {
  10          $this->server     = $server;
  11          $this->port     = (int) $port;
  12          $this->username     = $username;
  13          $this->password     = $password;
  14          $this->trace    = $trace;
  15      }
  16  
  17  	function fetch() { return fgets( $this->pop3, 1024 ); }
  18  
  19  	function fetch_all()
  20      {
  21          $response = $f = '';
  22  
  23          $f = $this->fetch();
  24          if (!empty($f))
  25          {
  26              while ( !preg_match("#^\.\r\n#", $f) )
  27              {
  28                  if ( $f[0] == '.' ) $f = substr($f,1);
  29                  $response .= $f;
  30                  $f = $this->fetch();
  31                      if (empty($f)) break;
  32              }
  33          }
  34          return $response;
  35      }
  36  
  37  	function get_response($cmd = false)
  38      {
  39          if ($cmd)
  40          {
  41              fwrite($this->pop3, "$cmd\r\n");
  42              if ($this->trace)
  43              {
  44                  if ('PASS ' == substr($cmd, 0, 5)) $cmd = 'PASS ' . str_repeat('*', strlen($cmd) - 5);
  45                  $bm = " POP cmd    ! $cmd";
  46                  $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
  47              }
  48          }
  49          
  50          $response = '';
  51          do { $response .= $this->fetch(); } while("\n" != substr($response, -1 ));
  52          if ($this->trace)
  53          {
  54              $bm = " response   ! " . str_replace("\r\n", '', $response);
  55              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
  56          }
  57  
  58          if ( ('QUIT' == $cmd) && ('.' == $response[0]) )     return $response;
  59          if ( '+' == $response[0] )                     return $response;
  60  
  61          $bm = " end        ! Abort";
  62          $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
  63          return false;
  64      }
  65  
  66  	function connect()
  67      {
  68      // fsockopen 
  69          $this->pop3 = fsockopen($this->server, $this->port, $errno, $errstr);
  70  
  71          if (false === $this->pop3) 
  72          {
  73              if ($this->trace)
  74              {
  75                  if (empty($errstr)) { $errno = '*'; $errstr = 'Unable to connect to ' . $this->server .  ':' . $this->port; }
  76                  $bm = "*** ERROR **! $errno $errstr";
  77                  $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
  78                  $bm = " end        ! Abort";
  79                  $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
  80              }
  81              return false;
  82          }
  83  
  84          $response = $this->get_response();
  85          if (!$response) return false;
  86      // USER 
  87          $response = $this->get_response('USER ' . $this->username);
  88          if (!$response) return false;
  89      // PASS 
  90          $response = $this->get_response('PASS ' . $this->password);
  91          if (!$response) return false;
  92  
  93          return true;
  94      }
  95  
  96  	function get_list()
  97      {
  98          $this->messages = array();
  99      
 100          $response = $this->get_response('LIST');
 101          if (!$response) return false;
 102  
 103          $r = explode(' ', $response);
 104          if (!$r[1])    return false; // list is empty
 105  
 106          if ($string = $this->fetch()) do { $datas = explode(' ', $string); $this->messages[] = $datas[0]; $string = $this->fetch(); } while(".\r\n" != substr($string, -3 ));
 107          return true;
 108      }
 109  
 110  	function get_headers($id, $headers = array())
 111      {
 112          $this->headers = array();
 113          $response = $this->get_response("TOP $id 0");
 114          if (!$response) return false;
 115  
 116          $this->message = $this->fetch_all();
 117          $this->extract_headers($this->message, $headers);
 118      }
 119  
 120  	function get_headers_deep($id, $headers = array())
 121      {
 122          $this->headers = array();
 123          $response = $this->get_response("TOP $id 100");
 124          if (!$response) return false;
 125  
 126          $this->message = $this->fetch_all();
 127  
 128          $this->extract_headers($this->message, $headers);
 129      }
 130  
 131  	function extract_headers($string, $headers = array())
 132      {
 133          $raw_headers = preg_replace("/\r\n[ \t]+/", ' ', $string); // Unfold headers
 134          $raw_headers = explode("\r\n", $raw_headers);
 135          $this->headers = array();
 136          foreach ($raw_headers as $value) 
 137          {
 138              if ('' == $value) continue;
 139              $k = substr($value, 0, $pos = strpos($value, ':'));
 140              $v = ltrim(substr($value, $pos + 1));
 141              if (empty($k)) continue;
 142              if ('' == $v)  continue;
 143              if (empty($headers) || in_array($k, $headers)) $this->headers[$k][] = $v;
 144          }
 145          if (!empty($headers)) $this->sort_headers($headers);
 146      }
 147  
 148  	function sort_headers($headers)
 149      {
 150          $sort = array_flip($headers);
 151          $keys = array_intersect_key($sort, $this->headers);
 152          $this->headers = array_merge($keys, array_intersect_key($this->headers, $keys), array_diff_key($this->headers, $sort));
 153      }
 154  
 155  	function get_message($id)
 156      {
 157          $response = $this->get_response('RETR ' . $id);
 158          if (!$response) return false;
 159  
 160          $this->message = $this->fetch_all();
 161      }
 162  
 163  	function delete($id)
 164      {
 165          $response = $this->get_response('DELE ' . $id);
 166          if (!$response) return false;
 167      }
 168  
 169  	function disconnect()
 170      {
 171          $response = $this->get_response('QUIT');
 172          fclose($this->pop3);
 173          if ($this->trace)
 174          {
 175              $bm = "disconnected!";
 176              $this->trace->log('!' . $bm . str_repeat( ' ', self::bt - strlen($bm)) . '!');
 177          }
 178          return true;
 179      }
 180  }


Generated: Thu Apr 28 18:38:52 2016 Cross-referenced by PHPXref 0.7.1