[ Index ]

MailPress 7.2

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

title

Body

[close]

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

   1  <?php
   2  class MP_Bounce_II extends MP_bounce_
   3  {
   4      public $option_name    = MailPress_bounce_handling_II::option_name;
   5  
   6      public $class        = __CLASS__;
   7      public $log_name     = 'mp_process_bounce_handling_II';
   8      public $log_option_name = 'bounce_handling_II';
   9      public $log_title     = 'Bounce Handling II Report ( Bounce in mailbox : %1$s )';
  10  
  11      public $cron_name     = 'MailPress_schedule_bounce_handling_II';
  12  
  13      public $meta_key    = MailPress_bounce_handling_II::meta_key;
  14  
  15      private $subject_bounce= array();
  16      private $subject_regex = '';
  17  
  18      private $body_bounce   = array();
  19      private $body_regex    = '';
  20  
  21      const headers        = array( 'Subject', 'X-MailPress-blog-id', 'X-MailPress-mail-id', 'X-MailPress-user-id' );
  22  
  23  	function __construct()
  24      {
  25          $this->load_regex();
  26  
  27          parent::__construct();
  28      }
  29  
  30  	function is_bounce()
  31      {
  32          $this->pop3->get_headers_deep( $this->message_id, self::headers );
  33  
  34          $this->blog_id = false;
  35          $this->mail_id = false;
  36          $this->mp_user_id = false;
  37  
  38  // subject
  39          $subject = $this->pop3->get_header( 'Subject' );
  40  
  41          if ( empty( $subject ) )
  42          {
  43              $bm = '            ! ** ERROR ** empty header : Subject (?) ';
  44              $this->local_log[] = '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!';
  45              return false;
  46          }
  47  
  48          $bm = ' Subject    ! ==> ' . $subject;
  49          $this->local_log[] = '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!';
  50  
  51  /* looking for bounce in subject */
  52  
  53          if ( empty( $subject ) || !preg_match( $this->subject_regex, strtolower( $subject ) ) ) return false;
  54  
  55  /* looking for bounce in message */
  56  
  57          if ( !$this->parse_body( $this->pop3->get_body( 0 ) ) ) return false;
  58  
  59  /* looking for identifiers */
  60  
  61          $this->blog_id = $this->pop3->get_header( 'X-MailPress-blog-id', 1 );
  62          if ( $this->blog_id === false ) 
  63          {
  64              $bm = '            ! ** ERROR ** unknown blog';
  65              $this->local_log[] = '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!';
  66              return false;
  67          }
  68          if ( $this->blog_id != get_current_blog_id() )
  69          {
  70              $bm = '            ! ** ERROR ** current blog is #' . get_current_blog_id() . ', blog sending mail is #' . $this->blog_id;
  71              $this->local_log[] = '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!';
  72              $this->blog_id = false;
  73              return false;
  74          }
  75  
  76          $this->mail_id = $this->pop3->get_header( 'X-MailPress-mail-id', 1 );
  77          if ( !$this->mail_id ) 
  78          {
  79              $bm = '            ! ** ERROR ** unknown original mail';
  80              $this->local_log[] = '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!';
  81              return false;
  82          }
  83          $this->mail = MP_Mail::get( $this->mail_id );
  84          if ( !$this->mail ) 
  85          {
  86              $bm = '            ! ** ERROR ** mail unknown #' . $this->mail_id;
  87              $this->local_log[] = '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!';
  88              $this->mail_id = false;
  89              return false;
  90          }
  91  
  92          $this->mp_user_id = $this->pop3->get_header( 'X-MailPress-user-id', 1 );
  93          if ( !$this->mp_user_id && MailPress::is_email( $this->mail->toemail ) )
  94          {
  95              $this->mp_user_id = MP_User::get_id_by_email( $mail->toemail );
  96          }
  97          if ( !$this->mp_user_id )
  98          {
  99              $bm = '            ! ** ERROR ** unknown original recipient';
 100              $this->local_log[] = '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!';
 101              return false;
 102          }
 103          $this->mp_user = MP_User::get( $this->mp_user_id );
 104          if ( !$this->mp_user ) 
 105          {
 106              $bm = '            ! ** ERROR ** recipient unknown #' . $this->mp_user_id;
 107              $this->local_log[] = '!' . $bm . str_repeat( ' ', $this->bt - strlen( $bm ) ) . '!';
 108              $this->mp_user_id = false;
 109              return false;
 110          }
 111  
 112          return ( $this->mail_id && $this->mp_user_id && ( $this->blog_id !== false ) );
 113      }
 114  
 115  	function parse_body( $body )
 116      {
 117          $status = array();
 118          if ( preg_match( $this->body_regex, $body, $matches ) ) 
 119          {
 120              if ( 3 == count( $matches ) ) preg_match( '/Status\s*?\:\s*?([2|4|5]+)\.(\d{1,3}).(\d{1,3})/is', $matches[2], $status );
 121              unset( $matches );
 122          }
 123  
 124          if ( 4 == count( $status ) ) 
 125          {
 126              $bounce = false;
 127              switch ( $status[1] ) 
 128              {
 129                  case 2:
 130                      $bounce = true;
 131                      break;
 132                  case 4:
 133                      $bounce = $this->RFC_3463_4( $status[2], $status[3] );
 134                      break;
 135                  case 5:
 136                      if ( 5 == $status[2] && 0 == $status[3] ) break;
 137                      $bounce = $this->RFC_3463_5( $status[2], $status[3] );
 138                      break;
 139              }
 140              if ( $bounce ) return true;
 141          }
 142  
 143          foreach ( $this->body_bounce as $rule ) if ( preg_match( '%' . preg_quote( $rule ) . '%is', $body ) ) return true;
 144  
 145          return false;
 146      }
 147  
 148      // RFC 3463 - Enhanced Mail System Status Codes
 149  
 150  	function RFC_3463_4( $code, $subcode )
 151      {
 152          if ( ( 5 == $code ) && ( 3 == $subcode ) ) return true;
 153          return false;
 154      }
 155  
 156  	function RFC_3463_5( $code, $subcode )
 157      {
 158          switch ( $code )
 159          {
 160              case '1': 
 161                  if ( in_array( $subcode, array( '0','1','2','3','4','5','6' ) ) ) return true;
 162                  break;
 163              default :
 164                  if ( in_array( $code, array( '2','3','4','5','6','7' ) ) ) return true;
 165                  break;
 166          }
 167          return false;
 168      }
 169  
 170  	function load_regex()
 171      {
 172          $advanced_path = 'advanced/' . get_current_blog_id() . '/bounces';
 173  
 174          $root = MP_UPL_ABSPATH . $advanced_path;
 175          $root = apply_filters( 'MailPress_advanced_bounces_root', $root );
 176          $file    = "$root/II.xml";
 177  
 178          $y = '';
 179  
 180          if ( !is_file( $file ) ) return;
 181  
 182          $x = file_get_contents( $file );
 183          if ( $xml = simplexml_load_string( $x ) )
 184  
 185          foreach ( $xml->subject->text as $text ) $this->subject_bounce[] = preg_quote( ( string ) $text, '~' );
 186          $this->subject_regex = '~( ' . implode( '|', $this->subject_bounce ) . ' )~i';
 187  
 188          $this->body_regex = ( string ) $xml->body->regex;
 189          foreach ( $xml->body->text as $text ) $this->body_bounce[] = ( string ) $text;
 190      }
 191  }


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