LocalPart.php 4.4 KB

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