File: change_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 (177 lines) | stat: -rw-r--r-- 6,392 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
<?PHP // $Id: change_password.php,v 1.41.2.2 2006/10/03 07:30:23 moodler Exp $

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

    $id = optional_param('id', SITEID, PARAM_INT);

    //HTTPS is potentially required in this page
    httpsrequired();

    if (!$course = get_record('course', 'id', $id)) {
        error('No such course!');
    }

    // did we get here because of a force password change
    $forcepassword = !empty($USER->preference['auth_forcepasswordchange']);

    if (!$forcepassword) {  // Don't redirect if they just got sent here
        require_login($id);
    }

    if ($frm = data_submitted()) {
        validate_form($frm, $err);

        check_for_restricted_user($frm->username);

        update_login_count();

        if (!count((array)$err)) {
            $user = get_complete_user_data('username', $frm->username);

            if (isguest($user->id)) {
                error('Can\'t change guest password!');
            }
            
            if (is_internal_auth($user->auth)){
                if (!update_internal_user_password($user, $frm->newpassword1)) {
                    error('Could not set the new password');
                }
            } else { // external users
                // the relevant auth libs should be loaded already 
                // as validate_form() calls authenticate_user_login()
                // check that we allow changes through moodle
                if (!empty($CFG->{'auth_'. $user->auth.'_stdchangepassword'})) {
                    if (function_exists('auth_user_update_password')){
                        // note that we pass cleartext password 
                        if (auth_user_update_password($user->username, $frm->newpassword1)){
                            update_internal_user_password($user, $frm->newpassword1, false);
                        } else {
                            error('Could not set the new password');
                        }
                    } else {
                        error('The authentication module is misconfigured (missing auth_user_update_password)'); 
                    } 
                } else {
                    error('You cannot change your password this way.');
                }
            }
            
            /// Are we admin logged in as someone else? If yes then we need to retain our real identity.
            if (!empty($USER->realuser)) {
                $realuser = $USER->realuser;
            }
            
            $USER = clone($user); // Get a fresh copy

            if (!empty($realuser)) {
                $USER->realuser = $realuser;
            }

            // register success changing password
            unset_user_preference('auth_forcepasswordchange', $user->id);

            set_moodle_cookie($USER->username);

            reset_login_count();

            $strpasswordchanged = get_string('passwordchanged');

            add_to_log($course->id, 'user', 'change password', "view.php?id=$user->id&amp;course=$course->id", "$user->id");

            $fullname = fullname($USER, true);

            if ($course->id != SITEID) {
                $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
            } else {
                $navstr = '';
            }
            $navstr .= "<a href=\"$CFG->wwwroot/user/index.php?id=$course->id\">".get_string("participants")."</a> -> <a href=\"$CFG->wwwroot/user/view.php?id=$USER->id&amp;course=$course->id\">$fullname</a> -> $strpasswordchanged";

            print_header($strpasswordchanged, $strpasswordchanged, $navstr);

            notice($strpasswordchanged, "$CFG->wwwroot/user/view.php?id=$USER->id&amp;course=$id");

            print_footer();
            exit;
        }
    }

    // We NEED to set this, because the form assumes it has a value!
    $frm->id = empty($course->id) ? 0 : $course->id;

    if (empty($frm->username) && !isguest()) {
        $frm->username = $USER->username;
    }

    $strchangepassword = get_string('changepassword');

    $fullname = fullname($USER, true);

    if ($course->id != SITEID) {
        $navstr = "<a href=\"$CFG->wwwroot/course/view.php?id=$course->id\">$course->shortname</a> -> ";
    } else {
        $navstr = '';
    }
    $navstr .= "<a href=\"$CFG->wwwroot/user/index.php?id=$course->id\">".get_string('participants')."</a> -> <a href=\"$CFG->wwwroot/user/view.php?id=$USER->id&amp;course=$course->id\">$fullname</a> -> $strchangepassword";

    print_header($strchangepassword, $strchangepassword, $navstr);

    print_simple_box_start('center');
    include('change_password_form.html');
    print_simple_box_end();
    print_footer();




/******************************************************************************
 * FUNCTIONS
 *****************************************************************************/
function validate_form($frm, &$err) {

    global $USER;

    $validpw = authenticate_user_login($frm->username, $frm->password);

    if (empty($frm->username)){
        $err->username = get_string('missingusername');
    } else {
        if (!isadmin() and empty($frm->password)){
            $err->password = get_string('missingpassword');
        } else {  
            if (!isadmin()) {
                //require non adminusers to give valid password
                if(!$validpw) {
                    $err->password = get_string('wrongpassword');
                }
            }
            else {
                // don't allow anyone to change the primary admin's password
                $mainadmin = get_admin();
                if($frm->username == $mainadmin->username && $mainadmin->id != $USER->id) { // the primary admin can change their own password!
                    $err->username = get_string('adminprimarynoedit');
                }
            }
        }
    }

    if (empty($frm->newpassword1)){
        $err->newpassword1 = get_string('missingnewpassword');
    }

    if (empty($frm->newpassword2)){
        $err->newpassword2 = get_string('missingnewpassword');
    } else {
        if ($frm->newpassword1 <> $frm->newpassword2) {
            $err->newpassword2 = get_string('passwordsdiffer');
        } else {
            if(!isadmin() and ($frm->password === $frm->newpassword1)){
                $err->newpassword1 = get_string('mustchangepassword');
            }
        }
    }
    
    return;
}

?>