Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]

   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.
  13  //
  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/>.
  16  
  17  /**
  18   * Forgot password routine.
  19   *
  20   * Finds the user and calls the appropriate routine for their authentication type.
  21   *
  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()'.
  29   *
  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
  34   */
  35  
  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');
  41  
  42  $token = optional_param('token', false, PARAM_ALPHANUM);
  43  
  44  $PAGE->set_url('/login/forgot_password.php');
  45  $systemcontext = context_system::instance();
  46  $PAGE->set_context($systemcontext);
  47  
  48  // setup text strings
  49  $strforgotten = get_string('passwordforgotten');
  50  
  51  $PAGE->set_pagelayout('login');
  52  $PAGE->set_title($strforgotten);
  53  $PAGE->set_heading($COURSE->fullname);
  54  
  55  // if alternatepasswordurl is defined, then we'll just head there
  56  if (!empty($CFG->forgottenpasswordurl)) {
  57      redirect($CFG->forgottenpasswordurl);
  58  }
  59  
  60  // if you are logged in then you shouldn't be here!
  61  if (isloggedin() and !isguestuser()) {
  62      redirect($CFG->wwwroot.'/index.php', get_string('loginalready'), 5);
  63  }
  64  
  65  // Fetch the token from the session, if present, and unset the session var immediately.
  66  $tokeninsession = false;
  67  if (!empty($SESSION->password_reset_token)) {
  68      $token = $SESSION->password_reset_token;
  69      unset($SESSION->password_reset_token);
  70      $tokeninsession = true;
  71  }
  72  
  73  if (empty($token)) {
  74      // This is a new password reset request.
  75      // Process the request; identify the user & send confirmation email.
  76      core_login_process_password_reset_request();
  77  } else {
  78      // A token has been found, but not in the session, and not from a form post.
  79      // This must be the user following the original rest link, so store the reset token in the session and redirect to self.
  80      // The session var is intentionally used only during the lifespan of one request (the redirect) and is unset above.
  81      if (!$tokeninsession && $_SERVER['REQUEST_METHOD'] === 'GET') {
  82          $SESSION->password_reset_token = $token;
  83          redirect($CFG->wwwroot . '/login/forgot_password.php');
  84      } else {
  85          // Continue with the password reset process.
  86          core_login_process_password_set($token);
  87      }
  88  }