Initial revision
[moodle.git] / login / forgot_password.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2
3 include("../config.php");
4
5 if (match_referer() && isset($HTTP_POST_VARS)) {
6
7 $frm = (object)$HTTP_POST_VARS;
8
9 validate_form($frm, $err);
10
11 update_login_count();
12
13 if (count((array)$err) == 0) {
14
15 if (!$user = get_user_info_from_db("email", $frm->email)) {
16 error("No such user with this address: $frm->email");
17 }
18
19 if (! reset_password_and_mail($user)) {
20 error("Could not reset password and mail the new one to you");
21 }
22
23 reset_login_count();
24 print_header("Password has been sent", "Password has been sent", "Password Sent", "");
25 include("forgot_password_change.html");
26 exit;
27 }
28 }
29
30 if ( empty($frm->email) ) {
31 if ( $username = get_moodle_cookie() ) {
32 $frm->email = get_field("user", "email", "username", "$username");
33 }
34 }
35
36 print_header("Forgot password?", "Have a new password sent to you", "", "form.email");
37
38 include("forgot_password_form.html");
39
40
41/******************************************************************************
42 * FUNCTIONS
43 *****************************************************************************/
44
45function validate_form($frm, &$err) {
46
47 if (empty($frm->email))
48 $err->email = "Missing email address";
49
50 else if (! validate_email($frm->email))
51 $err->email = "Invalid email address";
52
53 else if (! record_exists("user", "email", $frm->email))
54 $err->email = "No such email address on file";
55
56}
57
58
59function reset_password_and_mail($user) {
60
61 global $CFG;
62
63 $site = get_site();
64 $from = get_admin();
65
66 $newpassword = generate_password();
67
68 if (! set_field("user", "password", md5($newpassword), "id", $user->id) ) {
69 error("Could not set user password!");
70 }
71
72 $message = "Hi $user->firstname,\n\n";
73
74 $message .= "Your account password at '$site->fullname' has been reset\n";
75 $message .= "and you have been issued with a new temporary password.\n\n";
76
77 $message .= "Your current login information is now:\n\n";
78
79 $message .= " username: $user->username\n";
80 $message .= " password: $newpassword\n\n";
81
82 $message .= "Please go to this page to change your password:\n\n";
83
84 $message .= "$CFG->wwwroot/login/change_password.php\n\n";
85
86 $message .= "In most mail programs, this should appear as a blue link\n";
87 $message .= "which you can just click on. If that doesn't work, \n";
88 $message .= "then cut and paste the address into the address\n";
89 $message .= "line at the top of your web browser window.\n\n";
90
91 $message .= "Cheers from the '$site->fullname' administrator,\n";
92 $message .= "$from->firstname $from->lastname ($from->email)\n";
93
94 $subject = "$site->fullname: Changed password";
95
96 return email_to_user($user, $from, $subject, $message);
97
98}
99
100
101
102?>