Merge branch 'MDL-61801-33' of git://github.com/andrewnicols/moodle into MOODLE_33_STABLE
[moodle.git] / login / forgot_password.php
blob70f10af50ad54307b65f020c6232980b64b13d12
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Forgot password routine.
20 * Finds the user and calls the appropriate routine for their authentication type.
22 * There are several pathways to/through this page, summarised below:
23 * 1. User clicks the 'forgotten your username or password?' link on the login page.
24 * - No token is received, render the username/email search form.
25 * 2. User clicks the link in the forgot password email
26 * - Token received as GET param, store the token in session, redirect to self
27 * 3. Redirected from (2)
28 * - Fetch token from session, and continue to run the reset routine defined in 'core_login_process_password_set()'.
30 * @package core
31 * @subpackage auth
32 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 require('../config.php');
37 require_once($CFG->libdir.'/authlib.php');
38 require_once(__DIR__ . '/lib.php');
39 require_once('forgot_password_form.php');
40 require_once('set_password_form.php');
42 $token = optional_param('token', false, PARAM_ALPHANUM);
44 //HTTPS is required in this page when $CFG->loginhttps enabled
45 $PAGE->https_required();
47 $PAGE->set_url('/login/forgot_password.php');
48 $systemcontext = context_system::instance();
49 $PAGE->set_context($systemcontext);
51 // setup text strings
52 $strforgotten = get_string('passwordforgotten');
53 $strlogin = get_string('login');
55 $PAGE->navbar->add($strlogin, get_login_url());
56 $PAGE->navbar->add($strforgotten);
57 $PAGE->set_title($strforgotten);
58 $PAGE->set_heading($COURSE->fullname);
60 // if alternatepasswordurl is defined, then we'll just head there
61 if (!empty($CFG->forgottenpasswordurl)) {
62 redirect($CFG->forgottenpasswordurl);
65 // if you are logged in then you shouldn't be here!
66 if (isloggedin() and !isguestuser()) {
67 redirect($CFG->wwwroot.'/index.php', get_string('loginalready'), 5);
70 // Fetch the token from the session, if present, and unset the session var immediately.
71 $tokeninsession = false;
72 if (!empty($SESSION->password_reset_token)) {
73 $token = $SESSION->password_reset_token;
74 unset($SESSION->password_reset_token);
75 $tokeninsession = true;
78 if (empty($token)) {
79 // This is a new password reset request.
80 // Process the request; identify the user & send confirmation email.
81 core_login_process_password_reset_request();
82 } else {
83 // A token has been found, but not in the session, and not from a form post.
84 // This must be the user following the original rest link, so store the reset token in the session and redirect to self.
85 // The session var is intentionally used only during the lifespan of one request (the redirect) and is unset above.
86 if (!$tokeninsession && $_SERVER['REQUEST_METHOD'] === 'GET') {
87 $SESSION->password_reset_token = $token;
88 redirect($CFG->httpswwwroot . '/login/forgot_password.php');
89 } else {
90 // Continue with the password reset process.
91 core_login_process_password_set($token);