From 1d881d926be22c80507a8b39cecb5b6bf629e54b Mon Sep 17 00:00:00 2001 From: moodler Date: Mon, 23 Dec 2002 03:07:01 +0000 Subject: [PATCH] More moving functions around and removal of hardcoded SQL --- lang/en/moodle.php | 3 +- lang/en_us/moodle.php | 1 + lib/moodlelib.php | 119 ++++++++++++++++++++++++++++++-------- login/confirm.php | 20 ++++--- login/forgot_password.php | 28 --------- login/index.php | 11 ---- login/signup.php | 35 ----------- 7 files changed, 110 insertions(+), 107 deletions(-) diff --git a/lang/en/moodle.php b/lang/en/moodle.php index 82b9803312..8d9c3aa2d5 100644 --- a/lang/en/moodle.php +++ b/lang/en/moodle.php @@ -163,9 +163,10 @@ $string['enrolmentkeyhint'] = "That enrolment key was incorrect, please try agai $string['entercourse'] = "Click to enter this course"; $string['enteremailaddress'] = "Enter in your email address to reset your password and have the new password sent to you via email."; +$string['error'] = "Error"; +$string['errortoomanylogins'] = "Sorry, you have exceeded the allowed number of login attempts. Restart your browser."; $string['existingadmins'] = "Existing admins"; $string['existingteachers'] = "Existing teachers"; -$string['error'] = "Error"; $string['feedback'] = "Feedback"; $string['filemissing'] = "\$a is missing"; $string['files'] = "Files"; diff --git a/lang/en_us/moodle.php b/lang/en_us/moodle.php index 0f69d8964c..f3d5dc56ab 100644 --- a/lang/en_us/moodle.php +++ b/lang/en_us/moodle.php @@ -160,6 +160,7 @@ $string['entercourse'] = "Click to enter this course"; $string['enteremailaddress'] = "Enter in your email address to reset your password and have the new password sent to you via email."; $string['error'] = "Error"; +$string['errortoomanylogins'] = "Sorry, you have exceeded the allowed number of login attempts. Restart your browser."; $string['existingadmins'] = "Existing admins"; $string['existingteachers'] = "Existing teachers"; $string['feedback'] = "Feedback"; diff --git a/lib/moodlelib.php b/lib/moodlelib.php index b72d0849fb..462b694eb9 100644 --- a/lib/moodlelib.php +++ b/lib/moodlelib.php @@ -310,6 +310,18 @@ function require_login($courseid=0) { } } +function update_user_login_times() { + global $USER; + + $USER->lastlogin = $user->lastlogin = $USER->currentlogin; + $USER->currentlogin = $user->currentlogin = time(); + save_session("USER"); + + $user->id = $USER->id; + + return update_record("user", $user); +} + function update_login_count() { /// Keeps track of login attempts @@ -328,7 +340,7 @@ function update_login_count() { if ($SESSION->logincount > $max_logins) { unset($SESSION->wantsurl); save_session("SESSION"); - error("Sorry, you have exceeded the allowed number of login attempts. Restart your browser."); + error(get_string("errortoomanylogins")); } } @@ -341,7 +353,6 @@ function reset_login_count() { } - function isadmin($userid=0) { /// Is the user an admin? global $USER; @@ -645,6 +656,54 @@ function email_to_user($user, $from, $subject, $messagetext, $messagehtml="", $a } } +function reset_password_and_mail($user) { + + global $CFG; + + $site = get_site(); + $from = get_admin(); + + $newpassword = generate_password(); + + if (! set_field("user", "password", md5($newpassword), "id", $user->id) ) { + error("Could not set user password!"); + } + + $a->firstname = $user->firstname; + $a->sitename = $site->fullname; + $a->username = $user->username; + $a->newpassword = $newpassword; + $a->link = "$CFG->wwwroot/login/change_password.php"; + $a->signoff = "$from->firstname $from->lastname ($from->email)"; + + $message = get_string("newpasswordtext", "", $a); + + $subject = "$site->fullname: ".get_string("changedpassword"); + + return email_to_user($user, $from, $subject, $message); + +} + +function send_confirmation_email($user) { + + global $CFG; + + $site = get_site(); + $from = get_admin(); + + $data->firstname = $user->firstname; + $data->sitename = $site->fullname; + $data->link = "$CFG->wwwroot/login/confirm.php?p=$user->secret&s=$user->username"; + $data->admin = "$from->firstname $from->lastname ($from->email)"; + + $message = get_string("emailconfirmation", "", $data); + $subject = "$site->fullname account confirmation"; + + return email_to_user($user, $from, $subject, $message); + +} + + /// FILE HANDLING ///////////////////////////////////////////// @@ -890,27 +949,6 @@ function get_list_of_languages() { return $languages; } -function get_list_of_plugins($plugin="mod") { -/// Lists plugin directories within some directory - - global $CFG; - - $basedir = opendir("$CFG->dirroot/$plugin"); - while ($dir = readdir($basedir)) { - if ($dir == "." || $dir == ".." || $dir == "CVS") { - continue; - } - if (filetype("$CFG->dirroot/$plugin/$dir") != "dir") { - continue; - } - $plugins[] = $dir; - } - if ($plugins) { - asort($plugins); - } - return $plugins; -} - /// ENCRYPTION //////////////////////////////////////////////// @@ -984,6 +1022,27 @@ function endecrypt ($pwd, $data, $case) { /// ENVIRONMENT CHECKING //////////////////////////////////////////////////////////// +function get_list_of_plugins($plugin="mod") { +/// Lists plugin directories within some directory + + global $CFG; + + $basedir = opendir("$CFG->dirroot/$plugin"); + while ($dir = readdir($basedir)) { + if ($dir == "." || $dir == ".." || $dir == "CVS") { + continue; + } + if (filetype("$CFG->dirroot/$plugin/$dir") != "dir") { + continue; + } + $plugins[] = $dir; + } + if ($plugins) { + asort($plugins); + } + return $plugins; +} + function check_php_version($version="4.1.0") { /// Returns true is the current version of PHP is greater that the specified one $minversion = intval(str_replace(".", "", $version)); @@ -1084,6 +1143,20 @@ function count_words($string) { return count(preg_split("/\w\b/", $string)) - 1; } +function random_string ($length=15) { + $pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + $pool .= "abcdefghijklmnopqrstuvwxyz"; + $pool .= "0123456789"; + $poollen = strlen($pool); + mt_srand ((double) microtime() * 1000000); + $string = ""; + for ($i = 0; $i < $length; $i++) { + $string .= substr($pool, (mt_rand()%($poollen)), 1); + } + return $string; +} + + function getweek ($startdate, $thedate) { /// Given dates in seconds, how many weeks is the date from startdate /// The first week is 1, the second 2 etc ... diff --git a/login/confirm.php b/login/confirm.php index b523917104..8091d83249 100644 --- a/login/confirm.php +++ b/login/confirm.php @@ -20,24 +20,26 @@ $USER = $user; - $timenow = time(); - - $rs = $db->Execute("UPDATE user SET confirmed=1, lastIP='$REMOTE_ADDR', - firstaccess='$timenow', lastaccess='$timenow' - WHERE id = '$USER->id' "); - if (!$rs) { - error("Could not update this user while confirming"); + if (!set_field("user", "confirmed", 1, "id", $USER->id)) { + error("Could not confirm this user!"); + } + if (!set_field("user", "firstaccess", time(), "id", $USER->id)) { + error("Could not set this user's first access date!"); + } + if (!update_user_in_db($USER->id)) { + error("Could not update this user's information"); } set_moodle_cookie($USER->username); + // The user has confirmed successfully, let's log them in + $USER->loggedin = true; $USER->confirmed = 1; $USER->site = $CFG->wwwroot; - save_session("USER"); - if ( ! empty($SESSION->wantsurl) ) { + if ( ! empty($SESSION->wantsurl) ) { // Send them where they were going $goto = $SESSION->wantsurl; unset($SESSION->wantsurl); save_session("SESSION"); diff --git a/login/forgot_password.php b/login/forgot_password.php index 187282dc73..8da98a7be3 100644 --- a/login/forgot_password.php +++ b/login/forgot_password.php @@ -67,32 +67,4 @@ function validate_form($frm, &$err) { } -function reset_password_and_mail($user) { - - global $CFG; - - $site = get_site(); - $from = get_admin(); - - $newpassword = generate_password(); - - if (! set_field("user", "password", md5($newpassword), "id", $user->id) ) { - error("Could not set user password!"); - } - - $a->firstname = $user->firstname; - $a->sitename = $site->fullname; - $a->username = $user->username; - $a->newpassword = $newpassword; - $a->link = "$CFG->wwwroot/login/change_password.php"; - $a->signoff = "$from->firstname $from->lastname ($from->email)"; - - $message = get_string("newpasswordtext", "", $a); - - $subject = "$site->fullname: ".get_string("changedpassword"); - - return email_to_user($user, $from, $subject, $message); - -} - ?> diff --git a/login/index.php b/login/index.php index b23d80940f..c41db4cd7c 100644 --- a/login/index.php +++ b/login/index.php @@ -104,15 +104,4 @@ // No footer on this page -function update_user_login_times() { - global $db, $USER; - - $USER->lastlogin = $USER->currentlogin; - $USER->currentlogin = time(); - save_session("USER"); - - return $db->Execute("UPDATE user - SET lastlogin='$USER->lastlogin', currentlogin='$USER->currentlogin' - WHERE id = '$USER->id'"); -} ?> diff --git a/login/signup.php b/login/signup.php index c3988fc368..6911550198 100644 --- a/login/signup.php +++ b/login/signup.php @@ -100,39 +100,4 @@ function validate_form($user, &$err) { } -function random_string ($length=15) { - $pool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; - $pool .= "abcdefghijklmnopqrstuvwxyz"; - $pool .= "0123456789"; - $poollen = strlen($pool); - mt_srand ((double) microtime() * 1000000); - $string = ""; - for ($i = 0; $i < $length; $i++) { - $string .= substr($pool, (mt_rand()%($poollen)), 1); - } - return $string; -} - - -function send_confirmation_email($user) { - - global $CFG; - - $site = get_site(); - $from = get_admin(); - - $data->firstname = $user->firstname; - $data->sitename = $site->fullname; - $data->link = "$CFG->wwwroot/login/confirm.php?p=$user->secret&s=$user->username"; - $data->admin = "$from->firstname $from->lastname ($from->email)"; - - $message = get_string("emailconfirmation", "", $data); - $subject = "$site->fullname account confirmation"; - - return email_to_user($user, $from, $subject, $message); - -} - - - ?> -- 2.39.5