diff --git a/functions.inc.php b/functions.inc.php index 0d0d254c..226eb7c4 100644 --- a/functions.inc.php +++ b/functions.inc.php @@ -219,19 +219,20 @@ function check_string ($var) { -// -// check_domain -// Action: Checks if domain is valid and returns TRUE if this is the case. -// Call: check_domain (string domain) -// -// TODO: make check_domain able to handle as example .local domains +/** + * Checks if a domain is valid + * @param string $domain + * @return empty string if the domain is valid, otherwise string with the errormessage + * + * TODO: make check_domain able to handle as example .local domains + * TODO: skip DNS check if the domain exists in PostfixAdmin? + */ function check_domain ($domain) { global $CONF; global $PALANG; if (!preg_match ('/^([-0-9A-Z]+\.)+' . '([0-9A-Z]){2,6}$/i', ($domain))) { - flash_error(sprintf($PALANG['pInvalidDomainRegex'], htmlentities($domain))); - return false; + return sprintf($PALANG['pInvalidDomainRegex'], htmlentities($domain)); } if (isset($CONF['emailcheck_resolve_domain']) && 'YES' == $CONF['emailcheck_resolve_domain'] && 'WINDOWS'!=(strtoupper(substr(php_uname('s'), 0, 7)))) { @@ -241,18 +242,17 @@ function check_domain ($domain) { if(function_exists('checkdnsrr')) { // AAAA (IPv6) is only available in PHP v. >= 5 if (version_compare(phpversion(), "5.0.0", ">=")) { - if (checkdnsrr($domain,'AAAA')) return true; + if (checkdnsrr($domain,'AAAA')) return ''; } - if (checkdnsrr($domain,'A')) return true; - if (checkdnsrr($domain,'MX')) return true; - flash_error(sprintf($PALANG['pInvalidDomainDNS'], htmlentities($domain))); - return false; + if (checkdnsrr($domain,'A')) return ''; + if (checkdnsrr($domain,'MX')) return ''; + return sprintf($PALANG['pInvalidDomainDNS'], htmlentities($domain)); } else { - flash_error("emailcheck_resolve_domain is enabled, but function (checkdnsrr) missing!"); + return 'emailcheck_resolve_domain is enabled, but function (checkdnsrr) missing!'; } } - return true; + return ''; } @@ -260,9 +260,8 @@ function check_domain ($domain) { * check_email * Checks if an email is valid - if it is, return true, else false. * @param String $email - a string that may be an email address. - * @return boolean true if it's an email address, else false. + * @return empty string if it's a valid email address, otherwise string with the errormessage * TODO: make check_email able to handle already added domains - * TODO: don't use flash_error, use return value instead */ function check_email ($email) { global $CONF; @@ -280,15 +279,13 @@ function check_email ($email) { // Perform non-domain-part sanity checks if (!preg_match ('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_{|}~]+' . '@' . '[^@]+$/i', $ce_email)) { - flash_error($PALANG['pInvalidMailRegex']); - return false; + return $PALANG['pInvalidMailRegex']; } // Determine domain name $matches=array(); if (!preg_match('|@(.+)$|',$ce_email,$matches)) { - flash_error($PALANG['pInvalidMailRegex']); - return false; + return $PALANG['pInvalidMailRegex']; } $domain=$matches[1]; diff --git a/model/AdminHandler.php b/model/AdminHandler.php index 83e7f507..3e1bdfb1 100644 --- a/model/AdminHandler.php +++ b/model/AdminHandler.php @@ -4,12 +4,13 @@ class AdminHandler extends PFAHandler { protected function validate_new_id() { - $valid = check_email($this->id); + $email_check = check_email($this->id); - if ($valid) { + if ($email_check == '') { return true; } else { - $this->errormsg[$this->id_field] = Lang::read('pAdminCreate_admin_username_text_error1'); # TODO: half of the errormsg is currently delivered via flash_error() in check_email / check_domain + $this->errormsg[] = $email_check; + $this->errormsg[$this->id_field] = Lang::read('pAdminCreate_admin_username_text_error1'); return false; } } diff --git a/model/AliasHandler.php b/model/AliasHandler.php index 0c4a8b6c..b71f10d2 100644 --- a/model/AliasHandler.php +++ b/model/AliasHandler.php @@ -158,7 +158,13 @@ class AliasHandler extends PFAHandler { if ($local_part == '') { # catchall $valid = true; } else { - $valid = check_email($this->id); # TODO: check_email should return error message instead of using flash_error itsself + $email_check = check_email($this->id); + if ($email_check == '') { + $valid = true; + } else { + $this->errormsg[$this->id_field] = $email_check; + $valid = false; + } } return $valid; @@ -297,11 +303,15 @@ class AliasHandler extends PFAHandler { # and because alias domains can't forward to external domains # TODO: allow this only if $this->id is a catchall? list (/*NULL*/, $domain) = explode('@', $singlegoto); - if (!check_domain($domain)) { - $errors[] = "invalid: $singlegoto"; # TODO: better error message + $domain_check = check_domain($domain); + if ($domain_check != '') { + $errors[] = "$singlegoto: $domain_check"; + } + } else { + $email_check = check_email($singlegoto); + if ($email_check != '') { + $errors[] = "$singlegoto: $email_check"; } - } elseif (!check_email($singlegoto)) { - $errors[] = "invalid: $singlegoto"; # TODO: better error message } } diff --git a/model/DomainHandler.php b/model/DomainHandler.php index 2cb69136..df9d8084 100644 --- a/model/DomainHandler.php +++ b/model/DomainHandler.php @@ -9,12 +9,12 @@ class DomainHandler extends PFAHandler { protected $domain_field = 'domain'; protected function validate_new_id() { - $valid = check_domain($this->id); + $domain_check = check_domain($this->id); - if ($valid) { + if ($domain_check == '') { return true; } else { - $this->errormsg[$this->id_field] = Lang::read('pAdminCreate_domain_domain_text_error2'); # TODO: half of the errormsg is currently delivered via flash_error() in check_domain + $this->errormsg[$this->id_field] = $domain_check; return false; } } diff --git a/model/MailboxHandler.php b/model/MailboxHandler.php index 19f75dbe..25546c51 100644 --- a/model/MailboxHandler.php +++ b/model/MailboxHandler.php @@ -125,7 +125,9 @@ class MailboxHandler extends PFAHandler { return false; } - if ( !check_email($this->id) ) { # TODO: check_email should return error message instead of using flash_error itsself + $email_check = check_email($this->id); + if ( $email_check != '' ) { + $this->errormsg[$this->id_field] = $email_check; return false; } diff --git a/sendmail.php b/sendmail.php index b89c9f6e..ad684bfd 100644 --- a/sendmail.php +++ b/sendmail.php @@ -50,12 +50,14 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") $tBody = stripslashes($tBody); # TODO: check for get_magic_quotes_gpc inside safepost/safeget } - if (empty ($fTo) or !check_email ($fTo)) + $email_check = check_email ($fTo); + if (empty ($fTo) or ($email_check != '')) { $error = 1; $tTo = escape_string ($_POST['fTo']); $tSubject = escape_string ($_POST['fSubject']); - flash_error($PALANG['pSendmail_to_text_error']); + flash_error($PALANG['pSendmail_to_text_error']); # TODO: superfluous? + flash_error($email_check); } if ($error != 1) diff --git a/users/edit-alias.php b/users/edit-alias.php index f56d22dc..ff8ff452 100644 --- a/users/edit-alias.php +++ b/users/edit-alias.php @@ -92,9 +92,10 @@ if ($_SERVER['REQUEST_METHOD'] == "POST") # - for example, $goto[] can contain an element with empty string. I added a # check for that in the 2.3 branch, but we should use a better solution # (avoid empty elements in $goto) in trunk ;-) - if(!check_email($address)) { + $email_check = check_email($address); + if($email_check != '') { $error += 1; - flash_error($PALANG['pEdit_alias_goto_text_error2'] . " $address"); + flash_error("$address: $email_check"); } else { $good_goto[] = $address;