| 1 |
<?php |
| 2 |
defined('ABSPATH') || die('Restricted Access'); |
| 3 |
|
| 4 |
function acym_getEmailRegex(bool $secureJS = false, bool $forceRegex = false): string |
| 5 |
{ |
| 6 |
$config = acym_config(); |
| 7 |
if ($forceRegex || $config->get('special_chars', 0) != 1) { |
| 8 |
$regex = '[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*\@([a-z0-9-]+\.)+[a-z0-9]{2,20}'; |
| 9 |
} else { |
| 10 |
$regex = '.+\@(.+\.)+.{2,20}'; |
| 11 |
} |
| 12 |
|
| 13 |
if ($secureJS) { |
| 14 |
$regex = str_replace(['"', "'"], ['\"', "\'"], $regex); |
| 15 |
} |
| 16 |
|
| 17 |
return $regex; |
| 18 |
} |
| 19 |
|
| 20 |
function acym_isValidEmail($email, bool $extended = false): bool |
| 21 |
{ |
| 22 |
if (empty($email) || !is_string($email)) { |
| 23 |
return false; |
| 24 |
} |
| 25 |
|
| 26 |
if (!preg_match('/^'.acym_getEmailRegex().'$/i', $email)) { |
| 27 |
return false; |
| 28 |
} |
| 29 |
|
| 30 |
if (!$extended) { |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
//Now we do an extended verification... |
| 35 |
|
| 36 |
$config = acym_config(); |
| 37 |
|
| 38 |
if ($config->get('email_checkdomain', false) && function_exists('getmxrr')) { |
| 39 |
$domain = substr($email, strrpos($email, '@') + 1); |
| 40 |
$mxhosts = []; |
| 41 |
//Check if the domain exists |
| 42 |
$checkDomain = getmxrr($domain, $mxhosts); |
| 43 |
//Sometimes the returned host is checkyouremailaddress-hostnamedoesnotexist262392208.com ... not sure why! |
| 44 |
//But we remove it if it's the case... |
| 45 |
if (!empty($mxhosts) && strpos($mxhosts[0], 'hostnamedoesnotexist')) { |
| 46 |
array_shift($mxhosts); |
| 47 |
} |
| 48 |
if (!$checkDomain || empty($mxhosts)) { |
| 49 |
//Lets check with another function in case of... |
| 50 |
$dns = @dns_get_record($domain, DNS_A); |
| 51 |
if (empty($dns)) { |
| 52 |
return false; |
| 53 |
} |
| 54 |
|
| 55 |
$domainChanged = true; |
| 56 |
foreach ($dns as $oneRes) { |
| 57 |
if (strtolower($oneRes['host']) == strtolower($domain)) { |
| 58 |
$domainChanged = false; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
if ($domainChanged) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
$ip = acym_getIP(); |
| 69 |
|
| 70 |
// Check IP to limit subscription to max 3 per 2 hours |
| 71 |
if (!empty($ip) && $config->get('email_iptimecheck', 0)) { |
| 72 |
$lapseTime = time() - 7200; |
| 73 |
$nbUsers = acym_loadResult('SELECT COUNT(*) FROM #__acym_user WHERE creation_date > '.intval($lapseTime).' AND ip = '.acym_escapeDB($ip)); |
| 74 |
if ($nbUsers >= 3) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
function acym_isPunycode(string $email): bool |
| 83 |
{ |
| 84 |
return strpos($email, '@xn--') !== false; |
| 85 |
} |
| 86 |
|
| 87 |
function acym_getDomain(string $email): string |
| 88 |
{ |
| 89 |
$aPos = strrpos($email, '@'); |
| 90 |
if (empty($aPos)) { |
| 91 |
return ''; |
| 92 |
} |
| 93 |
|
| 94 |
return substr($email, $aPos + 1); |
| 95 |
} |
| 96 |
|