MDL-29092 login/change password: backporting to 1.9 for MDL-26381 and set form action...
[moodle.git] / login / change_password.php
1 <?PHP // $Id$
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
12     httpsrequired();
14     $systemcontext = get_context_instance(CONTEXT_SYSTEM);
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);
26         redirect($returnto);
27     }
29     if (!$course = get_record('course', 'id', $id)) {
30         error('No such course!');
31     }
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;
37         }
38         redirect($CFG->httpswwwroot.'/login/index.php');
39     }
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     }
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"!');
49     }
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     }
59     // load the appropriate auth plugin
60     $userauth = get_auth_plugin($USER->auth);
62     if (!$userauth->can_change_password()) {
63         print_error('nopasswordchange', 'auth');
64     }
66     if ($changeurl = $userauth->change_password_url()) {
67         // this internal scrip not used
68         redirect($changeurl);
69     }
71     $mform = new login_change_password_form($CFG->httpswwwroot . '/login/change_password.php');
72     $mform->set_data(array('id'=>$course->id));
74     $navlinks = array();
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.'&amp;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');
83         }
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&amp;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&amp;course=$course->id",
96                             'type' => 'misc');
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&amp;course=$id";
104         } else {
105             $returnto = $SESSION->wantsurl;
106         }
108         notice($strpasswordchanged, $returnto);
110         print_footer();
111         exit;
112     }
115     $strchangepassword = get_string('changepassword');
117     $fullname = fullname($USER, true);
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);
122     // Turn off pop-up messaging window for this page
123     $CFG->messaging = 0;
124     print_header($strchangepassword, $strchangepassword, $navigation);
125     if (get_user_preferences('auth_forcepasswordchange')) {
126         notify(get_string('forcepasswordchangenotice'));
127     }
128     $mform->display();
129     print_footer();
131 ?>