MDL-29092 login/change password: backporting to 1.9 for MDL-26381 and set form action...
[moodle.git] / login / change_password.php
CommitLineData
d35757eb 1<?PHP // $Id$
f9903ed0 2
80d8a1b8 3 require_once('../config.php');
1437f0a5 4 require_once('change_password_form.php');
f9903ed0 5
6bc1e5d5 6 $id = optional_param('id', SITEID, PARAM_INT); // current course
01dd64a8 7 $return = optional_param('return', 0, PARAM_BOOL); // redirect after password change
269eed64 8
0be6f678 9 $strparticipants = get_string('participants');
10
dbe9e346 11 //HTTPS is potentially required in this page
12 httpsrequired();
13
6bc1e5d5 14 $systemcontext = get_context_instance(CONTEXT_SYSTEM);
dcf6d93c 15
01dd64a8
RW
16 if ($return) {
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";
21 } else {
22 $returnto = $SESSION->wantsurl;
23 }
24 unset($SESSION->wantsurl);
25
26 redirect($returnto);
27 }
28
80d8a1b8 29 if (!$course = get_record('course', 'id', $id)) {
30 error('No such course!');
269eed64 31 }
f9903ed0 32
1cb3da36 33 // require proper login; guest user can not change password
34 if (empty($USER->id) or isguestuser()) {
1437f0a5 35 if (empty($SESSION->wantsurl)) {
01dd64a8 36 $SESSION->wantsurl = $CFG->httpswwwroot.'/login/change_password.php?id=' . $id;
1437f0a5 37 }
38 redirect($CFG->httpswwwroot.'/login/index.php');
39 }
fc23641e 40
1cb3da36 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);
44 }
45
1437f0a5 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"!');
253e6474 49 }
f9903ed0 50
6bc1e5d5 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);
55 }
56 error($message);
57 }
58
210560e3 59 // load the appropriate auth plugin
60 $userauth = get_auth_plugin($USER->auth);
61
62 if (!$userauth->can_change_password()) {
dbb82a47 63 print_error('nopasswordchange', 'auth');
210560e3 64 }
65
80274abf 66 if ($changeurl = $userauth->change_password_url()) {
210560e3 67 // this internal scrip not used
80274abf 68 redirect($changeurl);
210560e3 69 }
70
01dd64a8 71 $mform = new login_change_password_form($CFG->httpswwwroot . '/login/change_password.php');
6bc1e5d5 72 $mform->set_data(array('id'=>$course->id));
f9903ed0 73
0be6f678 74 $navlinks = array();
75 $navlinks[] = array('name' => $strparticipants, 'link' => "$CFG->wwwroot/user/index.php?id=$course->id", 'type' => 'misc');
76
5d2b6c1c 77 if ($mform->is_cancelled()) {
78 redirect($CFG->wwwroot.'/user/view.php?id='.$USER->id.'&amp;course='.$course->id);
beac4717 79 } else if ($data = $mform->get_data()) {
0bb2c9f7 80
6bc1e5d5 81 if (!$userauth->user_update_password(addslashes_recursive($USER), $data->newpassword1)) {
dbb82a47 82 print_error('errorpasswordupdate', 'auth');
1437f0a5 83 }
0bb2c9f7 84
1437f0a5 85 // register success changing password
6bc1e5d5 86 unset_user_preference('auth_forcepasswordchange', $USER->id);
80d8a1b8 87
1437f0a5 88 $strpasswordchanged = get_string('passwordchanged');
80d8a1b8 89
6bc1e5d5 90 add_to_log($course->id, 'user', 'change password', "view.php?id=$USER->id&amp;course=$course->id", "$USER->id");
269eed64 91
1437f0a5 92 $fullname = fullname($USER, true);
80d8a1b8 93
0be6f678 94 $navlinks[] = array('name' => $fullname,
95 'link' => "$CFG->wwwroot/user/view.php?id=$USER->id&amp;course=$course->id",
96 'type' => 'misc');
97 $navlinks[] = array('name' => $strpasswordchanged, 'link' => null, 'type' => 'misc');
98 $navigation = build_navigation($navlinks);
80d8a1b8 99
0be6f678 100 print_header($strpasswordchanged, $strpasswordchanged, $navigation);
269eed64 101
5d2b6c1c 102 if (empty($SESSION->wantsurl) or $SESSION->wantsurl == $CFG->httpswwwroot.'/login/change_password.php') {
1437f0a5 103 $returnto = "$CFG->wwwroot/user/view.php?id=$USER->id&amp;course=$id";
104 } else {
105 $returnto = $SESSION->wantsurl;
e83c419d 106 }
f9903ed0 107
1437f0a5 108 notice($strpasswordchanged, $returnto);
269eed64 109
1437f0a5 110 print_footer();
111 exit;
14217044 112 }
f9903ed0 113
1437f0a5 114
80d8a1b8 115 $strchangepassword = get_string('changepassword');
116
117 $fullname = fullname($USER, true);
118
0be6f678 119 $navlinks[] = array('name' => $fullname, 'link' => "$CFG->wwwroot/user/view.php?id=$USER->id&amp;course=$course->id", 'type' => 'misc');
120 $navlinks[] = array('name' => $strchangepassword, 'link' => null, 'type' => 'misc');
121 $navigation = build_navigation($navlinks);
9cf85078
SH
122 // Turn off pop-up messaging window for this page
123 $CFG->messaging = 0;
0be6f678 124 print_header($strchangepassword, $strchangepassword, $navigation);
f2d042c4 125 if (get_user_preferences('auth_forcepasswordchange')) {
1437f0a5 126 notify(get_string('forcepasswordchangenotice'));
d35757eb 127 }
1437f0a5 128 $mform->display();
129 print_footer();
f9903ed0 130
131?>