[ Index ]

MailPress 7.2

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

title

Body

[close]

/mp-includes/composer/vendor/egulias/email-validator/EmailValidator/Parser/ -> LocalPart.php (source)

   1  <?php
   2  
   3  namespace Egulias\EmailValidator\Parser;
   4  
   5  use Egulias\EmailValidator\Exception\DotAtEnd;
   6  use Egulias\EmailValidator\Exception\DotAtStart;
   7  use Egulias\EmailValidator\EmailLexer;
   8  use Egulias\EmailValidator\EmailValidator;
   9  use Egulias\EmailValidator\Exception\ExpectingAT;
  10  use Egulias\EmailValidator\Exception\ExpectingATEXT;
  11  use Egulias\EmailValidator\Exception\UnclosedQuotedString;
  12  use Egulias\EmailValidator\Exception\UnopenedComment;
  13  use Egulias\EmailValidator\Warning\CFWSWithFWS;
  14  use Egulias\EmailValidator\Warning\LocalTooLong;
  15  
  16  class LocalPart extends Parser
  17  {
  18      public function parse($localPart)
  19      {
  20          $parseDQuote = true;
  21          $closingQuote = false;
  22          $openedParenthesis = 0;
  23  
  24          while ($this->lexer->token['type'] !== EmailLexer::S_AT && $this->lexer->token) {
  25              if ($this->lexer->token['type'] === EmailLexer::S_DOT && !$this->lexer->getPrevious()) {
  26                  throw new DotAtStart();
  27              }
  28  
  29              $closingQuote = $this->checkDQUOTE($closingQuote);
  30              if ($closingQuote && $parseDQuote) {
  31                  $parseDQuote = $this->parseDoubleQuote();
  32              }
  33  
  34              if ($this->lexer->token['type'] === EmailLexer::S_OPENPARENTHESIS) {
  35                  $this->parseComments();
  36                  $openedParenthesis += $this->getOpenedParenthesis();
  37              }
  38              if ($this->lexer->token['type'] === EmailLexer::S_CLOSEPARENTHESIS) {
  39                  if ($openedParenthesis === 0) {
  40                      throw new UnopenedComment();
  41                  } else {
  42                      $openedParenthesis--;
  43                  }
  44              }
  45  
  46              $this->checkConsecutiveDots();
  47  
  48              if ($this->lexer->token['type'] === EmailLexer::S_DOT &&
  49                  $this->lexer->isNextToken(EmailLexer::S_AT)
  50              ) {
  51                  throw new DotAtEnd();
  52              }
  53  
  54              $this->warnEscaping();
  55              $this->isInvalidToken($this->lexer->token, $closingQuote);
  56  
  57              if ($this->isFWS()) {
  58                  $this->parseFWS();
  59              }
  60  
  61              $this->lexer->moveNext();
  62          }
  63  
  64          $prev = $this->lexer->getPrevious();
  65          if (strlen($prev['value']) > LocalTooLong::LOCAL_PART_LENGTH) {
  66              $this->warnings[LocalTooLong::CODE] = new LocalTooLong();
  67          }
  68      }
  69  
  70      protected function parseDoubleQuote()
  71      {
  72          $parseAgain = true;
  73          $special = array(
  74              EmailLexer::S_CR => true,
  75              EmailLexer::S_HTAB => true,
  76              EmailLexer::S_LF => true
  77          );
  78  
  79          $invalid = array(
  80              EmailLexer::C_NUL => true,
  81              EmailLexer::S_HTAB => true,
  82              EmailLexer::S_CR => true,
  83              EmailLexer::S_LF => true
  84          );
  85          $setSpecialsWarning = true;
  86  
  87          $this->lexer->moveNext();
  88  
  89          while ($this->lexer->token['type'] !== EmailLexer::S_DQUOTE && $this->lexer->token) {
  90              $parseAgain = false;
  91              if (isset($special[$this->lexer->token['type']]) && $setSpecialsWarning) {
  92                  $this->warnings[CFWSWithFWS::CODE] = new CFWSWithFWS();
  93                  $setSpecialsWarning = false;
  94              }
  95              if ($this->lexer->token['type'] === EmailLexer::S_BACKSLASH && $this->lexer->isNextToken(EmailLexer::S_DQUOTE)) {
  96                  $this->lexer->moveNext();
  97              }
  98  
  99              $this->lexer->moveNext();
 100  
 101              if (!$this->escaped() && isset($invalid[$this->lexer->token['type']])) {
 102                  throw new ExpectingATEXT();
 103              }
 104          }
 105  
 106          $prev = $this->lexer->getPrevious();
 107  
 108          if ($prev['type'] === EmailLexer::S_BACKSLASH) {
 109              if (!$this->checkDQUOTE(false)) {
 110                  throw new UnclosedQuotedString();
 111              }
 112          }
 113  
 114          if (!$this->lexer->isNextToken(EmailLexer::S_AT) && $prev['type'] !== EmailLexer::S_BACKSLASH) {
 115              throw new ExpectingAT();
 116          }
 117  
 118          return $parseAgain;
 119      }
 120  
 121      protected function isInvalidToken($token, $closingQuote)
 122      {
 123          $forbidden = array(
 124              EmailLexer::S_COMMA,
 125              EmailLexer::S_CLOSEBRACKET,
 126              EmailLexer::S_OPENBRACKET,
 127              EmailLexer::S_GREATERTHAN,
 128              EmailLexer::S_LOWERTHAN,
 129              EmailLexer::S_COLON,
 130              EmailLexer::S_SEMICOLON,
 131              EmailLexer::INVALID
 132          );
 133  
 134          if (in_array($token['type'], $forbidden) && !$closingQuote) {
 135              throw new ExpectingATEXT();
 136          }
 137      }
 138  }


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