3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * Change password page.
23 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../config.php');
28 require_once('change_password_form.php');
29 require_once($CFG->libdir
.'/authlib.php');
31 $id = optional_param('id', SITEID
, PARAM_INT
); // current course
32 $return = optional_param('return', 0, PARAM_BOOL
); // redirect after password change
34 //HTTPS is required in this page when $CFG->loginhttps enabled
35 $PAGE->https_required();
37 $PAGE->set_url('/login/change_password.php', array('id'=>$id));
39 $PAGE->set_context(context_system
::instance());
42 // this redirect prevents security warning because https can not POST to http pages
43 if (empty($SESSION->wantsurl
)
44 or stripos(str_replace('https://', 'http://', $SESSION->wantsurl
), str_replace('https://', 'http://', $CFG->wwwroot
.'/login/change_password.php') === 0)) {
45 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$id";
47 $returnto = $SESSION->wantsurl
;
49 unset($SESSION->wantsurl
);
54 $strparticipants = get_string('participants');
56 $systemcontext = context_system
::instance();
58 if (!$course = $DB->get_record('course', array('id'=>$id))) {
59 print_error('invalidcourseid');
62 // require proper login; guest user can not change password
63 if (!isloggedin() or isguestuser()) {
64 if (empty($SESSION->wantsurl
)) {
65 $SESSION->wantsurl
= $CFG->httpswwwroot
.'/login/change_password.php';
67 redirect(get_login_url());
70 // do not require change own password cap if change forced
71 if (!get_user_preferences('auth_forcepasswordchange', false)) {
72 require_capability('moodle/user:changeownpassword', $systemcontext);
75 // do not allow "Logged in as" users to change any passwords
76 if (session_is_loggedinas()) {
77 print_error('cannotcallscript');
80 if (is_mnet_remote_user($USER)) {
81 $message = get_string('usercannotchangepassword', 'mnet');
82 if ($idprovider = $DB->get_record('mnet_host', array('id'=>$USER->mnethostid
))) {
83 $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
85 print_error('userchangepasswordlink', 'mnet', '', $message);
88 // load the appropriate auth plugin
89 $userauth = get_auth_plugin($USER->auth
);
91 if (!$userauth->can_change_password()) {
92 print_error('nopasswordchange', 'auth');
95 if ($changeurl = $userauth->change_password_url()) {
96 // this internal scrip not used
100 $mform = new login_change_password_form();
101 $mform->set_data(array('id'=>$course->id
));
104 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
106 if ($mform->is_cancelled()) {
107 redirect($CFG->wwwroot
.'/user/view.php?id='.$USER->id
.'&course='.$course->id
);
108 } else if ($data = $mform->get_data()) {
110 if (!$userauth->user_update_password($USER, $data->newpassword1
)) {
111 print_error('errorpasswordupdate', 'auth');
114 // Reset login lockout - we want to prevent any accidental confusion here.
115 login_unlock_account($user);
117 // register success changing password
118 unset_user_preference('auth_forcepasswordchange', $USER);
119 unset_user_preference('create_password', $USER);
121 $strpasswordchanged = get_string('passwordchanged');
123 add_to_log($course->id
, 'user', 'change password', "view.php?id=$USER->id&course=$course->id", "$USER->id");
125 $fullname = fullname($USER, true);
127 $PAGE->navbar
->add($fullname, new moodle_url('/user/view.php', array('id'=>$USER->id
, 'course'=>$course->id
)));
128 $PAGE->navbar
->add($strpasswordchanged);
129 $PAGE->set_title($strpasswordchanged);
130 $PAGE->set_heading($COURSE->fullname
);
131 echo $OUTPUT->header();
133 notice($strpasswordchanged, new moodle_url($PAGE->url
, array('return'=>1)));
135 echo $OUTPUT->footer();
139 // make sure we really are on the https page when https login required
140 $PAGE->verify_https_required();
142 $strchangepassword = get_string('changepassword');
144 $fullname = fullname($USER, true);
146 $PAGE->navbar
->add($fullname, new moodle_url('/user/view.php', array('id'=>$USER->id
, 'course'=>$course->id
)));
147 $PAGE->navbar
->add($strchangepassword);
148 $PAGE->set_title($strchangepassword);
149 $PAGE->set_heading($COURSE->fullname
);
150 echo $OUTPUT->header();
152 if (get_user_preferences('auth_forcepasswordchange')) {
153 echo $OUTPUT->notification(get_string('forcepasswordchangenotice'));
156 echo $OUTPUT->footer();