File: forgot_password.php

package info (click to toggle)
moodle 1.6.3-2%2Betch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 37,172 kB
  • ctags: 51,688
  • sloc: php: 231,916; sql: 5,631; xml: 2,688; sh: 1,185; perl: 638; makefile: 48; pascal: 36
file content (295 lines) | stat: -rw-r--r-- 9,938 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php  
// $Id: forgot_password.php,v 1.26.2.3 2006/09/07 05:50:05 skodak Exp $
// forgot password routine. 
// find the user and call the appropriate routine for their authentication
// type.

require_once('../config.php');
httpsrequired();


//******************************
// GET PARAMS AND STRINGS
//******************************

// parameters from form
$param = new StdClass;
$param->action = optional_param( 'action','',PARAM_ALPHA );
$param->email = optional_param( 'email','',PARAM_CLEAN );
$param->p = optional_param( 'p','',PARAM_CLEAN );
$param->s = optional_param( 's','',PARAM_CLEAN );
$param->username = optional_param( 'username','',PARAM_CLEAN );

// setup text strings
$txt = new StdClass;
$txt->cancel = get_string('cancel');
$txt->confirmednot = get_string('confirmednot');
$txt->email = get_string('email');
$txt->emailnotfound = get_string('emailnotfound');
$txt->forgotten = get_string('passwordforgotten');
$txt->forgottenduplicate = get_string('forgottenduplicate','moodle',get_admin() );
$txt->forgotteninstructions = get_string('passwordforgotteninstructions');
$txt->invalidemail = get_string('invalidemail');
$txt->login = get_string('login');
$txt->loginalready = get_string('loginalready');
$txt->ok = get_string('ok');
$txt->passwordextlink = get_string('passwordextlink');
$txt->passwordnohelp = get_string('passwordnohelp');
$txt->senddetails = get_string('senddetails');
$txt->username = get_string('username');
$txt->usernameemailmatch = get_string('usernameemailmatch');
$txt->usernamenotfound = get_string('usernamenotfound');

$sesskey = sesskey();
$errors = array();
$page = ''; // page to display


//******************************
// PROCESS ACTIONS
//******************************

// if you are logged in then you shouldn't be here!
if (isloggedin() && !isguest()) {
    redirect( $CFG->wwwroot.'/index.php', $txt->loginalready, 5 );
}

// changepassword link replaced by individual auth setting
$auth = $CFG->auth; // the 'default' authentication method
if (!empty($CFG->changepassword)) {
    if (empty($CFG->{'auth_'.$auth.'_changepasswordurl'})) {
       set_config('auth_'.$auth.'_changepasswordurl',$CFG->changepassword );
    }
    set_config('changepassword','');
}        
 
// ACTION = FIND
if ($param->action=='find' and confirm_sesskey()) {
    // find the user in the database

    // first try the username
    if (!empty($param->username)) {
        if (!$user=get_complete_user_data('username',$param->username)) {
            $errors[] = $txt->usernamenotfound;
        }
    }

    // now try email
    if (!empty($param->email)) {
        // validate email address 1st
        if (!validate_email( $param->email )) {
            $errors[] = $txt->invalidemail;
        }
        elseif (count_records('user','email',$param->email) > 1) {
            // (if there is more than one instance of the email then we
            // cannot complete automated recovery)
            $page = 'duplicateemail';

            // just clear everything - we drop through to message page
            unset( $user );
            unset( $email );
            $errors = array();
        }
        elseif (!$mailuser = get_complete_user_data('email',$param->email)) {
            $errors[] = $txt->emailnotfound;
        }

        // just in case they did specify both...
        // if $user exists then check they actually match (then just use $user)
        if (!empty($user) and !empty($mailuser)) {
            if ($user->id != $mailuser->id) {
                $errors[] = $txt->usernameemailmatch;
            }
        $user = $mailuser;
        }

        // use email user if username not used or located
        if (!empty($mailuser) and empty($user)) {
            $user = $mailuser;
        }
    }

    // if user located (and no errors) take the appropriate action
    if (!empty($user) and (count($errors)==0)) {
        // check this user isn't 'unconfirmed'
        if (empty($user->confirmed)) {
            $errors[] = $txt->confirmednot;
        }
        else {
            // what to do depends on the authentication method
            $authmethod = $user->auth;
            if (is_internal_auth( $authmethod ) or !empty($CFG->{'auth_'.$authmethod.'_stdchangepassword'})) {
                // handle internal authentication
                
                // set 'secret' string
                $user->secret = random_string( 15 );
                if (!set_field('user','secret',$user->secret,'id',$user->id)) {
                    error( 'error setting user secret string' );
                }

                // send email (make sure mail block is off)
                $user->mailstop = 0;
                if (!send_password_change_confirmation_email($user)) {
                    error( 'error sending password change confirmation email' );
                }
 
                // display confirm message
                $page = 'emailconfirm';
            }
            else {
                // handle some 'external' authentication
                // if help text defined then we are going to display another page
                $txt->extmessage = '';
                $continue = false;
                if (!empty( $CFG->{'auth_'.$authmethod.'_changepasswordhelp'} )) {
                    $txt->extmessage = $CFG->{'auth_'.$authmethod.'_changepasswordhelp'}.'<br /><br />';
                }
                // if url defined then add that to the message (with a standard message)
                if (!empty( $CFG->{'auth_'.$authmethod.'_changepasswordurl'} )) {
                    $txt->extmessage .= $txt->passwordextlink . '<br /><br />';
                    $link = $CFG->{'auth_'.$authmethod.'_changepasswordurl'};
                    $txt->extmessage .= "<a href=\"$link\">$link</a>";
                }
                // if nothing to display, just do message that we can't help
                if (empty($txt->extmessage)) {
                    $txt->extmessage = $txt->passwordextlink;
                    $continue = true;
                }
                $page = 'external';
            }
        }
    }

    // nothing supplied - error
    if (empty($param->username) and empty($param->email)) {
        $errors[] = 'no email or username';
    }

    if ($page != 'external' and !empty($CFG->protectusernames)) {
        // do not give any hints about usernames or email!
        $errors = array();
        $page = 'emailmaybeconfirmed';
    }
}

// ACTION = AUTHENTICATE
if (!empty($param->p) and !empty($param->s)) {

    update_login_count();
    $user = get_complete_user_data('username',$param->s);

    // make sure that url relates to a valid user
    if (!empty($user)) {
        // check this isn't guest user
        if (isguest( $user->id )) {
            error('You cannot change the guest password');
        }

        // override email stop and mail new password
        $user->emailstop = 0;
        if (!reset_password_and_mail($user)) {
            error( 'Error resetting password and mailing you' );
        }

        reset_login_count();
        $page = 'emailsent';
       
        $changepasswordurl = "{$CFG->httpswwwroot}/login/change_password.php?action=forgot";
        $a->email = $user->email;
        $a->link = $changepasswordurl;
        $txt->emailpasswordsent = get_string( 'emailpasswordsent', '', $a );
    }

}


//******************************
// DISPLAY PART
//******************************
 
print_header( $txt->forgotten, $txt->forgotten,
    "<a href=\"{$CFG->wwwroot}/login/index.php\">{$txt->login}</a>->{$txt->forgotten}",
    'form.email' );

if ($page=='emailmaybeconfirmed') {
    // Print general confirmation message
    notice(get_string('emailpasswordconfirmmaybesent'),$CFG->wwwroot.'/index.php'); 
}

// check $page for appropriate page to display
if ($page=='emailconfirm') {
    // Confirm (internal method) email sent
    $protectedemail = preg_replace('/([^@]*)@(.*)/', '???????@$2', $user->email); // obfuscate the email address to protect privacy
    $txt->emailpasswordconfirmsent = get_string( 'emailpasswordconfirmsent','',$protectedemail );
    notice( $txt->emailpasswordconfirmsent,$CFG->wwwroot.'/index.php'); 
}

elseif ($page=='external') { 
    // display change password help text
    print_simple_box( $txt->extmessage, 'center', '50%','','20','noticebox' );

    // only print continue button if it makes sense
    if ($continue) {
        print_continue($CFG->wwwroot.'/index.php');
    }
}

elseif ($page=='emailsent') {
    // mail sent with new password
    notice( $txt->emailpasswordsent, $changepasswordurl );
}

elseif ($page=='duplicateemail') {
    // email address appears more than once
    notice( $txt->forgottenduplicate, $CFG->wwwroot.'/index.php');
}

else {
    echo '<br />';
    print_simple_box_start('center','50%','','20');

    // display any errors
    if (count($errors)) {
        echo "<ul class=\"errors\">\n";
        foreach ($errors as $error) {
            echo "    <li>$error</li>\n";
        }
        echo "</ul>\n";
    }

?>

<p><?php echo $txt->forgotteninstructions; ?></p>

<form action="forgot_password.php" method="post">
    <input type="hidden" name="sesskey" value="<?php echo $sesskey; ?>" />
    <input type="hidden" name="action" value="find" />
    <table id="forgottenpassword">
        <tr>
            <td><?php echo $txt->username; ?></td>
            <td><input type="text" name="username" size="25" /></td>
        </tr>
        <tr>
            <td><?php echo $txt->email; ?></td>
            <td><input type="text" name="email" size="25" /></td>
        </tr>
        <tr>
             <td>&nbsp;</td>
             <td><input type="submit" value="<?php echo $txt->ok; ?>" />
                 <input type="button" value="<?php echo $txt->cancel; ?>" 
                 onclick="javascript: history.go(-1)" /></td>
        </tr>
    </table>   
    

</form>

<?php
}

print_simple_box_end();
print_footer();
?>