3 require_once('../config.php');
4 require_once('change_password_form.php');
6 $id = optional_param('id', SITEID, PARAM_INT); // current course
7 $return = optional_param('return', 0, PARAM_BOOL); // redirect after password change
9 $strparticipants = get_string('participants');
11 //HTTPS is potentially required in this page
14 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
17 // this redirect prevents security warning because https can not POST to http pages
18 if (empty($SESSION->wantsurl)
19 or stripos(str_replace('https://', 'http://', $SESSION->wantsurl), str_replace('https://', 'http://', $CFG->wwwroot.'/login/change_password.php') === 0)) {
20 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$id";
22 $returnto = $SESSION->wantsurl;
24 unset($SESSION->wantsurl);
29 if (!$course = get_record('course', 'id', $id)) {
30 error('No such course!');
33 // require proper login; guest user can not change password
34 if (empty($USER->id) or isguestuser()) {
35 if (empty($SESSION->wantsurl)) {
36 $SESSION->wantsurl = $CFG->httpswwwroot.'/login/change_password.php?id=' . $id;
38 redirect($CFG->httpswwwroot.'/login/index.php');
41 // do not require change own password cap if change forced
42 if (!get_user_preferences('auth_forcepasswordchange', false)) {
43 require_capability('moodle/user:changeownpassword', $systemcontext);
46 // do not allow "Logged in as" users to change any passwords
47 if (!empty($USER->realuser)) {
48 error('Can not use this script when "Logged in as"!');
51 if (is_mnet_remote_user($USER)) {
52 $message = get_string('usercannotchangepassword', 'mnet');
53 if ($idprovider = get_record('mnet_host', 'id', $USER->mnethostid)) {
54 $message .= get_string('userchangepasswordlink', 'mnet', $idprovider);
59 // load the appropriate auth plugin
60 $userauth = get_auth_plugin($USER->auth);
62 if (!$userauth->can_change_password()) {
63 print_error('nopasswordchange', 'auth');
66 if ($changeurl = $userauth->change_password_url()) {
67 // this internal scrip not used
71 $mform = new login_change_password_form($CFG->httpswwwroot . '/login/change_password.php');
72 $mform->set_data(array('id'=>$course->id));
75 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
77 if ($mform->is_cancelled()) {
78 redirect($CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$course->id);
79 } else if ($data = $mform->get_data()) {
81 if (!$userauth->user_update_password(addslashes_recursive($USER), $data->newpassword1)) {
82 print_error('errorpasswordupdate', 'auth');
85 // register success changing password
86 unset_user_preference('auth_forcepasswordchange', $USER->id);
88 $strpasswordchanged = get_string('passwordchanged');
90 add_to_log($course->id, 'user', 'change password', "view.php?id=$USER->id&course=$course->id", "$USER->id");
92 $fullname = fullname($USER, true);
94 $navlinks[] = array('name' => $fullname,
95 'link' => "$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id",
97 $navlinks[] = array('name' => $strpasswordchanged, 'link' => null, 'type' => 'misc');
98 $navigation = build_navigation($navlinks);
100 print_header($strpasswordchanged, $strpasswordchanged, $navigation);
102 if (empty($SESSION->wantsurl) or $SESSION->wantsurl == $CFG->httpswwwroot.'/login/change_password.php') {
103 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&course=$id";
105 $returnto = $SESSION->wantsurl;
108 notice($strpasswordchanged, $returnto);
115 $strchangepassword = get_string('changepassword');
117 $fullname = fullname($USER, true);
119 $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$USER->id&course=$course->id", 'type' => 'misc');
120 $navlinks[] = array('name' => $strchangepassword, 'link' => null, 'type' => 'misc');
121 $navigation = build_navigation($navlinks);
122 // Turn off pop-up messaging window for this page
124 print_header($strchangepassword, $strchangepassword, $navigation);
125 if (get_user_preferences('auth_forcepasswordchange')) {
126 notify(get_string('forcepasswordchangenotice'));