[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  abstract class MP_bounce_ extends MP_WP_db_connect_
   3  {
   4      public $bt = 132;
   5      public static $count = array();
   6  
   7  	function __construct()
   8      {
   9          $this->config = get_option( $this->option_name );
  10          if ( !$this->config ) return;
  11  
  12          $this->trace = new MP_Log( $this->log_name, array( 'option_name' => $this->log_option_name ) );
  13  
  14          $xmailboxstatus = array(     0    =>    'no changes',
  15                              1    =>    'mark as read',
  16                              2    =>    'delete' );
  17  
  18          $this->trace->log( '!' . str_repeat( '-', $this->bt ) . '!' );
  19          $bm = sprintf( $this->log_title, $xmailboxstatus[$this->config['mailbox_status']] );
  20          $this->trace->log( '!' . str_repeat( ' ', 5 ) . $bm . str_repeat( ' ', $this->bt - 5 - strlen( $bm ) ) . '!' );
  21          $this->trace->log( '!' . str_repeat( '-', $this->bt ) . '!' );
  22          $bm = " start      !";
  23          $this->trace->log( '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!' );
  24  
  25          $return = $this->process();
  26  
  27          do_action( $this->cron_name );
  28  
  29          $this->trace->log( '!' . str_repeat( '-', $this->bt ) . '!' );
  30          $this->trace->end( $return );
  31      }
  32  
  33  // process
  34  	function process()
  35      {
  36          $return = true;
  37          $pop3 = get_option( $this->option_name_pop3 );
  38  
  39          $this->pop3 = new MP_Swift_Pop3( $pop3['server'], $pop3['port'], $pop3['username'], $pop3['password'], $this->trace );
  40  
  41          $bm = ' connecting ! ' . $pop3['server'] . ':' . $pop3['port'];
  42          $this->trace->log( '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!' );
  43  
  44          if ( $this->pop3->connect() )
  45          {
  46              if ( $this->pop3->get_list() )
  47              {
  48                  foreach( $this->pop3->messages as $this->message_id ) $this->process_message();
  49              }
  50              else
  51              {
  52                  $v = ' *** ALL DONE ***       *** ALL DONE ***       *** ALL DONE *** '; 
  53                  $this->trace->log( '!' . str_repeat( '-', $this->bt ) . '!' );
  54                  $this->trace->log( '!' . str_repeat( ' ', 15 ) . $v . str_repeat( ' ', $this->bt -15 - strlen( $v ) ) . '!' );
  55                  $this->trace->log( '!' . str_repeat( '-', $this->bt ) . '!' );
  56                  $return = false;
  57              }
  58              if ( !$this->pop3->disconnect() ) $return = false;
  59          }
  60          else $return = false;
  61  
  62          if ( $return )
  63          {
  64              $bm = " end        !";
  65              $this->trace->log( '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!' );
  66          }
  67          return $return;
  68      }
  69  
  70  	function process_message()
  71      {
  72          if ( !list( $mail_id, $mp_user_id, $bounce_email ) = $this->is_bounce() ) return;
  73          if ( !isset( $mail_id, $mp_user_id, $bounce_email ) ) return;
  74  
  75          $this->mysql_disconnect( $this->class );
  76          $this->mysql_connect( $this->class );
  77  
  78          $this->trace->log( '!' . str_repeat( '-', $this->bt ) . '!' );
  79          $bm = '            ! id         ! bounces   ! ' . $bounce_email;
  80          $this->trace->log( '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!' );
  81  
  82          $user_logmess = $mail_logmess = '';
  83          $already_processed = $already_stored = false;
  84  
  85          if ( !$mp_user = MP_User::get( $mp_user_id ) )
  86          {
  87              $user_logmess = '** WARNING ** user not in database'; 
  88              $usermeta['bounce'] = 0;
  89          }
  90          else
  91          {
  92              $bounce = array( 'message' => $this->pop3->message );
  93  
  94              $usermeta = MP_User_meta::get( $mp_user_id, $this->meta_key );
  95              if ( !$usermeta )
  96              {
  97                  $usermeta = array();
  98                  $usermeta['bounce'] = 1;
  99                  $usermeta['bounces'][$mail_id][] = $bounce;    
 100                  MP_User_meta::add( $mp_user_id, $this->meta_key, $usermeta );
 101              }
 102              else
 103              {
 104                  if ( !is_array( $usermeta ) ) $usermeta = array();
 105  
 106                  if ( !isset( $usermeta['bounces'][$mail_id] ) ) 
 107                  {
 108                      $usermeta['bounces'][$mail_id] = array();
 109  
 110                      if ( !isset( $usermeta['bounce'] ) )         $usermeta['bounce'] = 1;
 111                      elseif ( !is_numeric( $usermeta['bounce'] ) ) $usermeta['bounce'] = 1;
 112                      else $usermeta['bounce']++;
 113                  }
 114                  else
 115                  {
 116                      $already_processed = true;
 117                      foreach( $usermeta['bounces'][$mail_id] as $bounces )
 118                      {
 119                          if ( $bounces['message'] == $bounce['message'] )
 120                          {
 121                              $already_stored = true;
 122                              break;
 123                          }
 124                      }
 125                  }
 126  
 127                  if ( !$already_stored ) $usermeta['bounces'][$mail_id][] = $bounce;
 128  
 129                  if ( !MP_User_meta::add(    $mp_user_id, $this->meta_key, $usermeta, true ) )
 130                      MP_User_meta::update( $mp_user_id, $this->meta_key, $usermeta );
 131              }
 132  
 133              switch ( true )
 134              {
 135                  case $already_processed :
 136                      $user_logmess = '-- notice -- bounce previously processed';
 137                  break;
 138                  case ( 'bounced' == $mp_user->status ) :
 139                      $user_logmess = ' <' . $mp_user->email . '> already ** BOUNCED **';
 140                  break;
 141                  case ( $usermeta['bounce'] >= $this->config['max_bounces'] ) :
 142                      MP_User::set_status( $mp_user_id, 'bounced' );
 143                      $user_logmess = '** BOUNCED ** <' . $mp_user->email . '>';
 144                  break;
 145                  default :
 146                      $user_logmess = 'new bounce for <' . $mp_user->email . '>';
 147                  break;
 148              }
 149          }
 150  
 151          $bm  = ' user       ! ';
 152          $bm .= str_repeat( ' ', 10 - strlen( $mp_user_id ) ) . $mp_user_id . ' !';
 153          $bm .= str_repeat( ' ', 10 - strlen( $usermeta['bounce'] ) ) . ( ( $usermeta['bounce'] ) ? $usermeta['bounce'] : '' ) . ' !';
 154          $bm .= " $user_logmess";
 155          $this->trace->log( '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!' );
 156  
 157          $mailmeta = '';
 158          if ( !$already_processed )
 159          {
 160              switch ( true )
 161              {
 162                  case ( -1 == $mail_id ) :
 163                      $mail_logmess = '** WARNING ** mail unknown';
 164                  break;
 165                  case ( !$mail = MP_Mail::get( $mail_id ) ) :
 166                      $mail_logmess = '** WARNING ** mail not in database';
 167                  break;
 168                  default :
 169                      if ( !isset( self::$count[$mail_id] ) ) self::$count[$mail_id] = MP_Mail_meta::get( $mail_id, $this->meta_key );
 170                      self::$count[$mail_id] = ( is_numeric( self::$count[$mail_id] ) ) ? ( self::$count[$mail_id] + 1 ) : 1;
 171                      if ( !MP_Mail_meta::add( $mail_id, $this->meta_key, self::$count[$mail_id] , true ) )
 172                          MP_Mail_meta::update( $mail_id, $this->meta_key, self::$count[$mail_id] );
 173                      $mailmeta = self::$count[$mail_id];
 174  
 175                      $metas = MP_Mail_meta::get( $mail_id, '_MailPress_replacements' );
 176                      $mail_logmess = $mail->subject;
 177                      if ( $metas ) foreach( $metas as $k => $v ) $mail_logmess = str_replace( $k, $v, $mail_logmess );
 178                      if ( strlen( $mail_logmess ) > 50 )    $mail_logmess = substr( $mail_logmess, 0, 49 ) . '...';
 179                  break;
 180              }
 181          }
 182          $bm  = ' mail       ! ';
 183          $bm .= str_repeat( ' ', 10 - strlen( $mail_id ) )  . $mail_id . ' !';
 184          $bm .= str_repeat( ' ', 10 - strlen( $mailmeta ) ) . $mailmeta . ' !';
 185          $bm .= " $mail_logmess";
 186          $this->trace->log( '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!' );
 187      
 188          $this->trace->log( '!' . str_repeat( '-', $this->bt ) . '!' );
 189      }
 190  
 191  	function process_mailbox_status()
 192      {
 193          switch ( $this->config['mailbox_status'] )
 194          {
 195              case 1 :
 196                  $this->pop3->get_message( $this->message_id );
 197              break;
 198              case 2 :
 199                  $this->pop3->delete( $this->message_id );
 200              break;
 201              default :
 202              break;
 203          }
 204      }
 205  
 206  	function get_tag( $tag, $numeric = false, $item = 0 )
 207      {
 208          if ( isset( $this->pop3->headers[$tag][$item] ) )
 209          {
 210              if ( $numeric && is_numeric( $this->pop3->headers[$tag][$item] ) ) 
 211              {
 212                  return $this->pop3->headers[$tag][$item];
 213              }
 214              elseif ( !$numeric )
 215              {
 216                  return $this->pop3->headers[$tag][$item];
 217              }
 218          }
 219          return false;
 220      }
 221  
 222  	function get_body( $length = 8192 )
 223      {
 224          $body = ( $length ) ? substr( $this->pop3->message, 0, $length ) : $this->pop3->message;
 225                          
 226          // Microsoft Exchange Base 64 decoding
 227          if ( preg_match( '/\r?\n(.*?)\r?\nContent-Type\:\s*text\/plain.*?Content-Transfer-Encoding\:\sbase64\r?\n\r?\n(.*?)\1/is', $body, $matches ) )
 228              $body = str_replace( $matches[2], base64_decode( str_replace( array( "\n", "\r" ), '', $matches[2] ) ), $body );
 229                          
 230          // clean up
 231          $body = preg_replace( '%--- Below this line is a copy of the message.(.*)%is', '', $body );
 232          $body = preg_replace( '%------ This is a copy (.*)%is', '', $body );
 233          $body = preg_replace( '%----- Original message -----(.*)%is', '', $body ); 
 234          $body = preg_replace( '%Content-Type: message/rfc822.*%is', '', $body );
 235          $body = preg_replace( '%Content-Description: Delivery report.*\s*?%i', '', $body );
 236          $body = str_replace( "\r", "", $body );
 237          $body = str_replace( array( "\n", "\r" ), " ", $body );
 238          $body = preg_replace( '%\s+%', ' ', $body );
 239  
 240          return $body;
 241      }
 242  }


Generated: Mon Mar 11 18:33:33 2019 Cross-referenced by PHPXref 0.7.1