Bootswatch, Summernote, and Captcheck mods for Mods for HESK (mods-for-hesk.com). In use at support.netsyms.com.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

119 lines
3.3 KiB

  1. <?php
  2. /**
  3. * @param string $address - the email address
  4. * @param array $multiple_emails - true if HESK (or custom field) supports multiple emails
  5. * @param bool $return_emails (def. true): return the email address(es). Otherwise a boolean is returned
  6. *
  7. * @return mixed|null|string - array if multiple valid emails, null if no email and not required, string if valid email
  8. */
  9. function hesk_validateEmail($address, $multiple_emails, $return_emails = true) {
  10. /* Allow multiple emails to be used? */
  11. if ($multiple_emails) {
  12. /* Make sure the format is correct */
  13. $address = preg_replace('/\s/', '', $address);
  14. $address = str_replace(';', ',', $address);
  15. /* Check if addresses are valid */
  16. $all = explode(',', $address);
  17. foreach ($all as $k => $v) {
  18. if (!hesk_isValidEmail($v)) {
  19. unset($all[$k]);
  20. }
  21. }
  22. /* If at least one is found return the value */
  23. if (count($all)) {
  24. if ($return_emails) {
  25. return implode(',', $all);
  26. }
  27. return true;
  28. } elseif (!$return_emails) {
  29. return false;
  30. }
  31. } else {
  32. /* Make sure people don't try to enter multiple addresses */
  33. $address = str_replace(strstr($address, ','), '', $address);
  34. $address = str_replace(strstr($address, ';'), '', $address);
  35. $address = trim($address);
  36. /* Valid address? */
  37. if (hesk_isValidEmail($address)) {
  38. if ($return_emails) {
  39. return $address;
  40. }
  41. return true;
  42. }
  43. }
  44. if ($return_emails) {
  45. return null;
  46. }
  47. return true;
  48. } // END hesk_validateEmail()
  49. /**
  50. * @param $email
  51. * @return bool
  52. */
  53. function hesk_isValidEmail($email) {
  54. /* Check for header injection attempts */
  55. if (preg_match("/\r|\n|%0a|%0d/i", $email)) {
  56. return false;
  57. }
  58. /* Does it contain an @? */
  59. $atIndex = strrpos($email, "@");
  60. if ($atIndex === false) {
  61. return false;
  62. }
  63. /* Get local and domain parts */
  64. $domain = substr($email, $atIndex + 1);
  65. $local = substr($email, 0, $atIndex);
  66. $localLen = strlen($local);
  67. $domainLen = strlen($domain);
  68. /* Check local part length */
  69. if ($localLen < 1 || $localLen > 64) {
  70. return false;
  71. }
  72. /* Check domain part length */
  73. if ($domainLen < 1 || $domainLen > 254) {
  74. return false;
  75. }
  76. /* Local part mustn't start or end with a dot */
  77. if ($local[0] == '.' || $local[$localLen - 1] == '.') {
  78. return false;
  79. }
  80. /* Local part mustn't have two consecutive dots*/
  81. if (strpos($local, '..') !== false) {
  82. return false;
  83. }
  84. /* Check domain part characters */
  85. if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain)) {
  86. return false;
  87. }
  88. /* Domain part mustn't have two consecutive dots */
  89. if (strpos($domain, '..') !== false) {
  90. return false;
  91. }
  92. /* Character not valid in local part unless local part is quoted */
  93. if (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\", "", $local))) /* " */ {
  94. if (!preg_match('/^"(\\\\"|[^"])+"$/', str_replace("\\\\", "", $local))) /* " */ {
  95. return false;
  96. }
  97. }
  98. /* All tests passed, email seems to be OK */
  99. return true;
  100. } // END hesk_isValidEmail()