From 85a1d4c9bc17262491fb5791742b604f00ff25ff Mon Sep 17 00:00:00 2001 From: moodler Date: Mon, 6 Sep 2004 15:21:22 +0000 Subject: [PATCH] Made the check for allowable emails into a new function --- lib/moodlelib.php | 40 ++++++++++++++++++++++++++++++++++++++++ login/signup.php | 25 ++----------------------- user/edit.php | 25 ++----------------------- 3 files changed, 44 insertions(+), 46 deletions(-) diff --git a/lib/moodlelib.php b/lib/moodlelib.php index eeaa833889..373264df56 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -747,6 +747,12 @@ function create_user_record($username, $password, $auth='') { } } + if (!empty($newuser->email)) { + if (email_is_not_allowed($newuser->email)) { + unset($newuser->email); + } + } + $newuser->auth = (empty($auth)) ? $CFG->auth : $auth; $newuser->username = $username; $newuser->password = md5($password); @@ -1640,6 +1646,40 @@ function send_password_change_confirmation_email($user) { } +function email_is_not_allowed($email) { +/// Check that an email is allowed. It returns an error message if there +/// was a problem. + + global $CFG; + + if (!empty($CFG->allowemailaddresses)) { + $allowed = explode(' ', $CFG->allowemailaddresses); + foreach ($allowed as $allowedpattern) { + $allowedpattern = trim($allowedpattern); + if (!$allowedpattern) { + continue; + } + if (strpos($email, $allowedpattern) !== false) { // Match! + return false; + } + } + return get_string("emailonlyallowed", '', $CFG->allowemailaddresses); + + } else if (!empty($CFG->denyemailaddresses)) { + $denied = explode(' ', $CFG->denyemailaddresses); + foreach ($denied as $deniedpattern) { + $deniedpattern = trim($deniedpattern); + if (!$deniedpattern) { + continue; + } + if (strpos($email, $deniedpattern) !== false) { // Match! + return get_string("emailnotallowed", '', $CFG->denyemailaddresses); + } + } + } + + return false; +} /// FILE HANDLING ///////////////////////////////////////////// diff --git a/login/signup.php b/login/signup.php index ba48373a70..5b06e1bce0 100644 --- a/login/signup.php +++ b/login/signup.php @@ -147,29 +147,8 @@ function validate_form($user, &$err) { } if (empty($err->email)) { - if (!empty($CFG->allowemailaddresses)) { - $allowed = explode(' ', $CFG->allowemailaddresses); - $err->email = get_string("emailonlyallowed", '', $CFG->allowemailaddresses); // Default - foreach ($allowed as $allowedpattern) { - $allowedpattern = trim($allowedpattern); - if (!$allowedpattern) { - continue; - } - if (strpos($user->email, $allowedpattern) !== false) { - unset($err->email); - } - } - } else if (!empty($CFG->denyemailaddresses)) { - $denied = explode(' ', $CFG->denyemailaddresses); - foreach ($denied as $deniedpattern) { - $deniedpattern = trim($deniedpattern); - if (!$deniedpattern) { - continue; - } - if (strpos($user->email, $deniedpattern) !== false) { - $err->email = get_string("emailnotallowed", '', $CFG->denyemailaddresses); - } - } + if ($error = email_is_not_allowed($user->email)) { + $err->email = $error; } } diff --git a/user/edit.php b/user/edit.php index 4a1db9b664..6c9ace7071 100644 --- a/user/edit.php +++ b/user/edit.php @@ -256,29 +256,8 @@ function find_form_errors(&$user, &$usernew, &$err) { } if (empty($err["email"]) and !isadmin()) { - if (!empty($CFG->allowemailaddresses)) { - $allowed = explode(' ', $CFG->allowemailaddresses); - $err["email"] = get_string("emailonlyallowed", '', $CFG->allowemailaddresses); // Default - foreach ($allowed as $allowedpattern) { - $allowedpattern = trim($allowedpattern); - if (!$allowedpattern) { - continue; - } - if (strpos($usernew->email, $allowedpattern) !== false) { - unset($err["email"]); - } - } - } else if (!empty($CFG->denyemailaddresses)) { - $denied = explode(' ', $CFG->denyemailaddresses); - foreach ($denied as $deniedpattern) { - $deniedpattern = trim($deniedpattern); - if (!$deniedpattern) { - continue; - } - if (strpos($usernew->email, $deniedpattern) !== false) { - $err->email = get_string("emailnotallowed", '', $CFG->denyemailaddresses); - } - } + if ($error = email_is_not_allowed($usernew->email)) { + $err["email"] = $error; } } -- 2.39.5