[ Index ]

MailPress 7.1

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

title

Body

[close]

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

   1  <?php
   2  use Egulias\EmailValidator\EmailValidator;
   3  use Egulias\EmailValidator\Validation\RFCValidation;
   4  
   5  /**
   6   * A class to encapsulate Egulias Email Validator
   7   *
   8   * @author André Renaut
   9   */
  10  
  11  class MP_Swift_EmailValidator
  12  {
  13      /** Singleton instance */
  14      private static $instance = null;
  15  
  16      private $rfc2822_rules     = null;
  17  
  18      private $rfc653x_rules     = null;
  19      private $rfc653x_validator = null;
  20  
  21      public $rfc     = null;
  22      public $message = null;
  23  
  24  	function __construct()
  25      {
  26      }
  27  
  28  	public static function getInstance()
  29      {
  30          if (!isset(self::$instance)) self::$instance = new self();
  31          return self::$instance;
  32      }
  33  
  34      /**
  35      * @param string $email
  36      */
  37  	public function isValid( $email )
  38      {
  39          switch( true )
  40          {
  41              case ( !$this->basics( $email ) ) :
  42                  return false;
  43              break;
  44              case ( $this->rfc822(  $email ) ) :
  45              break;
  46              case ( $this->rfc2822( $email ) ) :
  47              break;
  48              case ( $this->rfc653x( $email ) ) :
  49              break;
  50              default :
  51                  return $this->rfc = false;
  52              break;
  53          }
  54          return true;
  55      }
  56  
  57      /**
  58      * basics
  59      * 
  60      * For the record (wikipedia) :
  61      *     the format of email addresses is : local-part@domain-part
  62      *         the local  part may be up to 64 characters long and the domain may have a maximum of 255 characters.
  63      *         the domain part is a list of dot-separated DNS labels, each label being limited to a length of 63 characters
  64      *     this rule is only applied by function rfc822 (so email gets validated by either two others) !
  65      *
  66      * @param string $email
  67      */
  68  	private function basics( $email )
  69      {
  70          $this->rfc = __FUNCTION__;
  71  
  72          $parts = explode( '@', $email );
  73  
  74          $domain_part = array_pop( $parts );
  75          if ( is_numeric( $domain_part )   ) return false;
  76          if ( strlen( $domain_part ) > 255 ) return false;
  77  
  78          $local_part  = implode( '@', $parts );
  79          if ( strlen( $local_part ) > 64 ) return false;
  80  
  81          $dots = explode( '.', $domain_part );
  82          foreach( $dots as $dot )
  83          {
  84              if ( strlen( $dot ) > 63 ) return false;
  85          }
  86  
  87          return true;
  88      }
  89  
  90      /**
  91      *    rfc822 - Standard for the Format of ARPA Internet Text Messages
  92      * 
  93      *    For the record (php.net) : 
  94      *      This validates e-mail addresses against the syntax in RFC 822, 
  95      *      with the exceptions that comments and whitespace folding
  96      *      and dotless domain names are not supported.
  97      *
  98      * @param string $email
  99      */
 100  	private function rfc822( $email )
 101      {
 102          $this->rfc = __FUNCTION__;
 103          return filter_var( $email, FILTER_VALIDATE_EMAIL );
 104      }
 105  
 106      /**
 107      *    rfc2822 - Internet Message Format
 108      *
 109      *   inspired by deprecated Swift_Mime_Grammar
 110      *
 111      * @param string $email
 112      */
 113  	private function rfc2822( $email )
 114      {
 115          $this->rfc = __FUNCTION__;
 116          if ( !isset( $this->rfc2822_rules ) )
 117          {
 118              $this->RFC_2822_Validation();
 119          }
 120          return $this->isValid_rfc2822( $email );
 121      }
 122  
 123      /**
 124      *    rfc5321 - Simple Mail Transfer Protocol
 125      *    rfc5322 - Internet Message Format
 126      */
 127  
 128      // nothing here
 129  
 130      /**
 131      *    rfc6530 - Overview and Framework for Internationalized Email
 132      *    rfc6531 - SMTP Extension for Internationalized Email
 133      *    rfc6532 - Internationalized Email Headers
 134      *
 135      * @param string $email
 136      */
 137  	private function rfc653x( $email )
 138      {
 139          $this->rfc = __FUNCTION__;
 140  
 141          if ( !( defined( 'SWIFT_ADDRESSENCODER' )
 142             && ( SWIFT_ADDRESSENCODER == 'utf8'  ) 
 143             && ( preg_match( '/[\\x80-\\xff]+/' , $email ) ) ) )
 144              return false; 
 145  
 146          if (    defined( 'FILTER_FLAG_EMAIL_UNICODE' ) 
 147             && ( filter_var( $email, FILTER_VALIDATE_EMAIL, array( 'flags' => FILTER_FLAG_EMAIL_UNICODE ) ) ) )
 148              return true;
 149  
 150          if ( !isset( $this->rfc653x_rules ) ) 
 151          {
 152              new MP_Swift();
 153              $this->rfc653x_validator = new EmailValidator;
 154              $this->rfc653x_rules     = new RFCValidation();
 155          }
 156          return $this->rfc653x_validator->isValid( $email, $this->rfc653x_rules );
 157      }
 158  
 159      //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\\
 160      //*   Defines the grammar to use for validation,
 161      //*   implements the RFC 2822 (and friends) ABNF grammar definitions.
 162  
 163      /**
 164      *
 165      * @param string $email
 166      */
 167  	private function isValid_rfc2822( $email )
 168      {
 169          return ( preg_match( '/^' . $this->rfc2822_rules . '$/D', $email ) );
 170      }
 171  
 172  	private function RFC_2822_Validation()
 173      {
 174          /*** Refer to RFC 2822 for ABNF grammar ***/
 175          
 176          //All basic building blocks
 177          $g['NO-WS-CTL'] = '[\x01-\x08\x0B\x0C\x0E-\x19\x7F]';
 178          $g['WSP'] = '[ \t]';
 179          $g['CRLF'] = '(?:\r\n)';
 180          $g['FWS'] = '(?:(?:' . $g['WSP'] . '*' . $g['CRLF'] . ')?' . $g['WSP'] . ')';
 181          $g['text'] = '[\x00-\x08\x0B\x0C\x0E-\x7F]';
 182          $g['quoted-pair'] = '(?:\\\\' . $g['text'] . ')';
 183          $g['ctext'] = '(?:' . $g['NO-WS-CTL'] . '|[\x21-\x27\x2A-\x5B\x5D-\x7E])';
 184          //Uses recursive PCRE (?1) -- 
 185          $g['ccontent'] = '(?:' . $g['ctext'] . '|' . $g['quoted-pair'] . '|(?1))';
 186          $g['comment'] = '(\((?:' . $g['FWS'] . '|' . $g['ccontent']. ')*' . $g['FWS'] . '?\))';
 187          $g['CFWS'] = '(?:(?:' . $g['FWS'] . '?' . $g['comment'] . ')*(?:(?:' . $g['FWS'] . '?' . $g['comment'] . ')|' . $g['FWS'] . '))';
 188          $g['qtext'] = '(?:' . $g['NO-WS-CTL'] . '|[\x21\x23-\x5B\x5D-\x7E])';
 189          $g['qcontent'] = '(?:' . $g['qtext'] . '|' . $g['quoted-pair'] . ')';
 190          $g['quoted-string'] = '(?:' . $g['CFWS'] . '?"' . '(' . $g['FWS'] . '?' . $g['qcontent'] . ')*' . $g['FWS'] . '?"' . $g['CFWS'] . '?)';
 191          $g['atext'] = '[a-zA-Z0-9!#\$%&\'\*\+\-\/=\?\^_`\{\}\|~]';
 192          $g['atom'] = '(?:' . $g['CFWS'] . '?' . $g['atext'] . '+' . $g['CFWS'] . '?)';
 193          $g['dot-atom-text'] = '(?:' . $g['atext'] . '+' . '(\.' . $g['atext'] . '+)*)';
 194          $g['dot-atom'] = '(?:' . $g['CFWS'] . '?' . $g['dot-atom-text'] . '+' . $g['CFWS'] . '?)';
 195          $g['word'] = '(?:' . $g['atom'] . '|' . $g['quoted-string'] . ')';
 196          $g['phrase'] = '(?:' . $g['word'] . '+?)';
 197          $g['no-fold-quote'] = '(?:"(?:' . $g['qtext'] . '|' . $g['quoted-pair'] . ')*")';
 198          $g['dtext'] = '(?:' . $g['NO-WS-CTL'] . '|[\x21-\x5A\x5E-\x7E])';
 199          $g['no-fold-literal'] = '(?:\[(?:' . $g['dtext'] . '|' . $g['quoted-pair'] . ')*\])';
 200  
 201          //Message IDs
 202          $g['id-left'] = '(?:' . $g['dot-atom-text'] . '|' . $g['no-fold-quote'] . ')';
 203          $g['id-right'] = '(?:' . $g['dot-atom-text'] . '|' . $g['no-fold-literal'] . ')';
 204  
 205          //Addresses, mailboxes and paths
 206          $g['local-part'] = '(?:' . $g['dot-atom'] . '|' . $g['quoted-string'] . ')';
 207          $g['dcontent'] = '(?:' . $g['dtext'] . '|' . $g['quoted-pair'] . ')';
 208          $g['domain-literal'] = '(?:' . $g['CFWS'] . '?\[(' . $g['FWS'] . '?' . $g['dcontent'] . ')*?' . $g['FWS'] . '?\]' . $g['CFWS'] . '?)';
 209          $g['domain'] = '(?:' . $g['dot-atom'] . '|' . $g['domain-literal'] . ')';
 210  
 211          $this->rfc2822_rules = '(?:' . $g['local-part'] . '@' . $g['domain'] . ')';
 212  
 213          unset( $g );
 214      }
 215  }


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