[ Index ]

MailPress 7.2

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

title

Body

[close]

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

   1  <?php
   2  class MP_Swift_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  
 118          $this->extract_headers( $this->message, $headers );
 119      }
 120  
 121  	function get_headers_deep( $id, $headers = array() )
 122      {
 123          $this->headers = array();
 124          $response = $this->get_response( "TOP $id 300" );
 125          if ( !$response ) return false;
 126  
 127          $this->message = $this->fetch_all();
 128  
 129          $this->extract_headers( $this->message, $headers );
 130      }
 131  
 132  	function extract_headers( $string, $headers = array() )
 133      {
 134          $raw_headers = preg_replace( "/\r\n[ \t]+/", ' ', $string ); // Unfold headers
 135          $raw_headers = explode( "\r\n", $raw_headers );
 136          $this->headers = array();
 137          foreach ( $raw_headers as $value ) 
 138          {
 139              if ( '' == $value ) continue;
 140              $k = substr( $value, 0, $pos = strpos( $value, ':' ) );
 141              $v = ltrim( substr( $value, $pos + 1 ) );
 142              if ( empty( $k ) ) continue;
 143              if ( '' == $v )  continue;
 144              if ( empty( $headers ) || in_array( $k, $headers ) ) $this->headers[$k][] = $v;
 145          }
 146          if ( !empty( $headers ) ) $this->sort_headers( $headers );
 147      }
 148  
 149  	function sort_headers( $headers )
 150      {
 151          $sort = array_flip( $headers );
 152          $keys = array_intersect_key( $sort, $this->headers );
 153          $this->headers = array_merge( $keys, array_intersect_key( $this->headers, $keys ), array_diff_key( $this->headers, $sort ) );
 154      }
 155  
 156  	function get_header( $header, $numeric = false, $item = 0 )
 157      {
 158          $this->header = false;
 159  
 160          if ( isset( $this->headers[$header][$item] ) )
 161          {
 162              if ( $numeric && is_numeric( $this->headers[$header][$item] ) ) 
 163              {
 164                  $this->header = trim( $this->headers[$header][$item] );
 165              }
 166              elseif ( !$numeric )
 167              {
 168                  $items = imap_mime_header_decode( $this->headers[$header][$item] );
 169                  $header = '';
 170                  foreach( $items as $item ) $header .= ' ' . $item->text;
 171                  $this->header = trim( $header );
 172              }
 173          }
 174  
 175          return $this->header;
 176      }
 177  
 178  	function get_body( $length = 8192 )
 179      {
 180          $body = ( $length ) ? substr( $this->message, 0, $length ) : $this->message;
 181                          
 182          // Microsoft Exchange Base 64 decoding
 183          if ( preg_match( '/\r?\n(.*?)\r?\nContent-Type\:\s*text\/plain.*?Content-Transfer-Encoding\:\sbase64\r?\n\r?\n(.*?)\1/is', $body, $matches ) )
 184              $body = str_replace( $matches[2], base64_decode( str_replace( array( "\n", "\r" ), '', $matches[2] ) ), $body );
 185                          
 186          // clean up
 187          $body = preg_replace( '%--- Below this line is a copy of the message.(.*)%is', '', $body );
 188          $body = preg_replace( '%------ This is a copy (.*)%is', '', $body );
 189          $body = preg_replace( '%----- Original message -----(.*)%is', '', $body ); 
 190          $body = preg_replace( '%Content-Type: message/rfc822.*%is', '', $body );
 191          $body = preg_replace( '%Content-Description: Delivery report.*\s*?%i', '', $body );
 192          $body = str_replace( "\r", "", $body );
 193          $body = str_replace( array( "\n", "\r" ), " ", $body );
 194          $body = preg_replace( '%\s+%', ' ', $body );
 195  
 196          return $body;
 197      }
 198  
 199  	function get_message( $id )
 200      {
 201          $response = $this->get_response( 'RETR ' . $id );
 202          if ( !$response ) return false;
 203  
 204          $this->message = $this->fetch_all();
 205      }
 206  
 207  	function delete( $id )
 208      {
 209          $response = $this->get_response( 'DELE ' . $id );
 210          if ( !$response ) return false;
 211      }
 212  
 213  	function disconnect()
 214      {
 215          $response = $this->get_response( 'QUIT' );
 216          fclose( $this->pop3 );
 217          if ( $this->trace )
 218          {
 219              $bm = "disconnected!";
 220              $this->trace->log( '!' . $bm . str_repeat( ' ', self::bt - strlen( $bm ) ) . '!' );
 221          }
 222          return true;
 223      }
 224  }


Generated: Tue May 19 15:55:14 2020 Cross-referenced by PHPXref 0.7.1