PageRenderTime 42ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/themes/kingsize/php/email-validator.php

https://bitbucket.org/geetharani/gordon
PHP | 181 lines | 95 code | 17 blank | 69 comment | 23 complexity | 217ddfde1e33ac0892a0b9705d9f9bb8 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. /*
  3. EmailAddressValidator Class
  4. http://code.google.com/p/php-email-address-validation/
  5. Released under New BSD license
  6. http://www.opensource.org/licenses/bsd-license.php
  7. Sample Code
  8. ----------------
  9. $validator = new EmailAddressValidator;
  10. if ($validator->check_email_address('test@example.org')) {
  11. // Email address is technically valid
  12. }
  13. */
  14. class EmailAddressValidator {
  15. /**
  16. * Check email address validity
  17. * @param strEmailAddress Email address to be checked
  18. * @return True if email is valid, false if not
  19. */
  20. public function check_email_address($strEmailAddress) {
  21. // If magic quotes is "on", email addresses with quote marks will
  22. // fail validation because of added escape characters. Uncommenting
  23. // the next three lines will allow for this issue.
  24. //if (get_magic_quotes_gpc()) {
  25. // $strEmailAddress = stripslashes($strEmailAddress);
  26. //}
  27. // Control characters are not allowed
  28. if (preg_match('/[\x00-\x1F\x7F-\xFF]/', $strEmailAddress)) {
  29. return false;
  30. }
  31. // Check email length - min 3 (a@a), max 256
  32. if (!$this->check_text_length($strEmailAddress, 3, 256)) {
  33. return false;
  34. }
  35. // Split it into sections using last instance of "@"
  36. $intAtSymbol = strrpos($strEmailAddress, '@');
  37. if ($intAtSymbol === false) {
  38. // No "@" symbol in email.
  39. return false;
  40. }
  41. $arrEmailAddress[0] = substr($strEmailAddress, 0, $intAtSymbol);
  42. $arrEmailAddress[1] = substr($strEmailAddress, $intAtSymbol + 1);
  43. // Count the "@" symbols. Only one is allowed, except where
  44. // contained in quote marks in the local part. Quickest way to
  45. // check this is to remove anything in quotes. We also remove
  46. // characters escaped with backslash, and the backslash
  47. // character.
  48. $arrTempAddress[0] = preg_replace('/\./'
  49. ,''
  50. ,$arrEmailAddress[0]);
  51. $arrTempAddress[0] = preg_replace('/"[^"]+"/'
  52. ,''
  53. ,$arrTempAddress[0]);
  54. $arrTempAddress[1] = $arrEmailAddress[1];
  55. $strTempAddress = $arrTempAddress[0] . $arrTempAddress[1];
  56. // Then check - should be no "@" symbols.
  57. if (strrpos($strTempAddress, '@') !== false) {
  58. // "@" symbol found
  59. return false;
  60. }
  61. // Check local portion
  62. if (!$this->check_local_portion($arrEmailAddress[0])) {
  63. return false;
  64. }
  65. // Check domain portion
  66. if (!$this->check_domain_portion($arrEmailAddress[1])) {
  67. return false;
  68. }
  69. // If we're still here, all checks above passed. Email is valid.
  70. return true;
  71. }
  72. /**
  73. * Checks email section before "@" symbol for validity
  74. * @param strLocalPortion Text to be checked
  75. * @return True if local portion is valid, false if not
  76. */
  77. protected function check_local_portion($strLocalPortion) {
  78. // Local portion can only be from 1 to 64 characters, inclusive.
  79. // Please note that servers are encouraged to accept longer local
  80. // parts than 64 characters.
  81. if (!$this->check_text_length($strLocalPortion, 1, 64)) {
  82. return false;
  83. }
  84. // Local portion must be:
  85. // 1) a dot-atom (strings separated by periods)
  86. // 2) a quoted string
  87. // 3) an obsolete format string (combination of the above)
  88. $arrLocalPortion = explode('.', $strLocalPortion);
  89. for ($i = 0, $max = sizeof($arrLocalPortion); $i < $max; $i++) {
  90. if (!preg_match('.^('
  91. . '([A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]'
  92. . '[A-Za-z0-9!#$%&\'*+/=?^_`{|}~-]{0,63})'
  93. .'|'
  94. . '("[^\\\"]{0,62}")'
  95. .')$.'
  96. ,$arrLocalPortion[$i])) {
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. /**
  103. * Checks email section after "@" symbol for validity
  104. * @param strDomainPortion Text to be checked
  105. * @return True if domain portion is valid, false if not
  106. */
  107. protected function check_domain_portion($strDomainPortion) {
  108. // Total domain can only be from 1 to 255 characters, inclusive
  109. if (!$this->check_text_length($strDomainPortion, 1, 255)) {
  110. return false;
  111. }
  112. // Check if domain is IP, possibly enclosed in square brackets.
  113. if (preg_match('/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
  114. .'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/'
  115. ,$strDomainPortion) ||
  116. preg_match('/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])'
  117. .'(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]$/'
  118. ,$strDomainPortion)) {
  119. return true;
  120. } else {
  121. $arrDomainPortion = explode('.', $strDomainPortion);
  122. if (sizeof($arrDomainPortion) < 2) {
  123. return false; // Not enough parts to domain
  124. }
  125. for ($i = 0, $max = sizeof($arrDomainPortion); $i < $max; $i++) {
  126. // Each portion must be between 1 and 63 characters, inclusive
  127. if (!$this->check_text_length($arrDomainPortion[$i], 1, 63)) {
  128. return false;
  129. }
  130. if (!preg_match('/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|'
  131. .'([A-Za-z0-9]+))$/', $arrDomainPortion[$i])) {
  132. return false;
  133. }
  134. if ($i == $max - 1) { // TLD cannot be only numbers
  135. if (strlen(preg_replace('/[0-9]/', '', $arrDomainPortion[$i])) <= 0) {
  136. return false;
  137. }
  138. }
  139. }
  140. }
  141. return true;
  142. }
  143. /**
  144. * Check given text length is between defined bounds
  145. * @param strText Text to be checked
  146. * @param intMinimum Minimum acceptable length
  147. * @param intMaximum Maximum acceptable length
  148. * @return True if string is within bounds (inclusive), false if not
  149. */
  150. protected function check_text_length($strText, $intMinimum, $intMaximum) {
  151. // Minimum and maximum are both inclusive
  152. $intTextLength = strlen($strText);
  153. if (($intTextLength < $intMinimum) || ($intTextLength > $intMaximum)) {
  154. return false;
  155. } else {
  156. return true;
  157. }
  158. }
  159. }
  160. ?>