Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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   * Login renderable.
  19   *
  20   * @package    core_auth
  21   * @copyright  2016 Frédéric Massart - FMCorz.net
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_auth\output;
  26  
  27  use context_system;
  28  use help_icon;
  29  use moodle_url;
  30  use renderable;
  31  use renderer_base;
  32  use stdClass;
  33  use templatable;
  34  
  35  /**
  36   * Login renderable class.
  37   *
  38   * @package    core_auth
  39   * @copyright  2016 Frédéric Massart - FMCorz.net
  40   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class login implements renderable, templatable {
  43  
  44      /** @var bool Whether to auto focus the form fields. */
  45      public $autofocusform;
  46      /** @var bool Whether we can login as guest. */
  47      public $canloginasguest;
  48      /** @var bool Whether we can login by e-mail. */
  49      public $canloginbyemail;
  50      /** @var bool Whether we can sign-up. */
  51      public $cansignup;
  52      /** @var help_icon The cookies help icon. */
  53      public $cookieshelpicon;
  54      /** @var string The error message, if any. */
  55      public $error;
  56      /** @var string The info message, if any. */
  57      public $info;
  58      /** @var moodle_url Forgot password URL. */
  59      public $forgotpasswordurl;
  60      /** @var array Additional identify providers, contains the keys 'url', 'name' and 'icon'. */
  61      public $identityproviders;
  62      /** @var string Login instructions, if any. */
  63      public $instructions;
  64      /** @var moodle_url The form action login URL. */
  65      public $loginurl;
  66      /** @var moodle_url The sign-up URL. */
  67      public $signupurl;
  68      /** @var string The user name to pre-fill the form with. */
  69      public $username;
  70      /** @var string The language selector menu. */
  71      public $languagemenu;
  72      /** @var string The csrf token to limit login to requests that come from the login form. */
  73      public $logintoken;
  74      /** @var string Maintenance message, if Maintenance is enabled. */
  75      public $maintenance;
  76      /** @var string ReCaptcha element HTML. */
  77      public $recaptcha;
  78  
  79      /**
  80       * Constructor.
  81       *
  82       * @param array $authsequence The enabled sequence of authentication plugins.
  83       * @param string $username The username to display.
  84       */
  85      public function __construct(array $authsequence, $username = '') {
  86          global $CFG, $OUTPUT, $PAGE;
  87  
  88          $this->username = $username;
  89  
  90          $languagedata = new \core\output\language_menu($PAGE);
  91  
  92          $this->languagemenu = $languagedata->export_for_action_menu($OUTPUT);
  93          $this->canloginasguest = $CFG->guestloginbutton && !isguestuser();
  94          $this->canloginbyemail = !empty($CFG->authloginviaemail);
  95          $this->cansignup = $CFG->registerauth == 'email' || !empty($CFG->registerauth);
  96          if ($CFG->rememberusername == 0) {
  97              $this->cookieshelpicon = new help_icon('cookiesenabledonlysession', 'core');
  98          } else {
  99              $this->cookieshelpicon = new help_icon('cookiesenabled', 'core');
 100          }
 101  
 102          $this->autofocusform = !empty($CFG->loginpageautofocus);
 103  
 104          $this->forgotpasswordurl = new moodle_url('/login/forgot_password.php');
 105          $this->loginurl = new moodle_url('/login/index.php');
 106          $this->signupurl = new moodle_url('/login/signup.php');
 107  
 108          // Authentication instructions.
 109          $this->instructions = $CFG->auth_instructions;
 110          if (is_enabled_auth('none')) {
 111              $this->instructions = get_string('loginstepsnone');
 112          } else if ($CFG->registerauth == 'email' && empty($this->instructions)) {
 113              $this->instructions = get_string('loginsteps', 'core', 'signup.php');
 114          }
 115  
 116          if ($CFG->maintenance_enabled == true) {
 117              if (!empty($CFG->maintenance_message)) {
 118                  $this->maintenance = $CFG->maintenance_message;
 119              } else {
 120                  $this->maintenance = get_string('sitemaintenance', 'admin');
 121              }
 122          }
 123  
 124          // Identity providers.
 125          $this->identityproviders = \auth_plugin_base::get_identity_providers($authsequence);
 126          $this->logintoken = \core\session\manager::get_login_token();
 127  
 128          // ReCaptcha.
 129          if (login_captcha_enabled()) {
 130              require_once($CFG->libdir . '/recaptchalib_v2.php');
 131              $this->recaptcha = recaptcha_get_challenge_html(RECAPTCHA_API_URL, $CFG->recaptchapublickey);
 132          }
 133      }
 134  
 135      /**
 136       * Set the error message.
 137       *
 138       * @param string $error The error message.
 139       */
 140      public function set_error($error) {
 141          $this->error = $error;
 142      }
 143  
 144      /**
 145       * Set the info message.
 146       *
 147       * @param string $info The info message.
 148       */
 149      public function set_info(string $info): void {
 150          $this->info = $info;
 151      }
 152  
 153      public function export_for_template(renderer_base $output) {
 154  
 155          $identityproviders = \auth_plugin_base::prepare_identity_providers_for_output($this->identityproviders, $output);
 156  
 157          $data = new stdClass();
 158          $data->autofocusform = $this->autofocusform;
 159          $data->canloginasguest = $this->canloginasguest;
 160          $data->canloginbyemail = $this->canloginbyemail;
 161          $data->cansignup = $this->cansignup;
 162          $data->cookieshelpicon = $this->cookieshelpicon->export_for_template($output);
 163          $data->error = $this->error;
 164          $data->info = $this->info;
 165          $data->forgotpasswordurl = $this->forgotpasswordurl->out(false);
 166          $data->hasidentityproviders = !empty($this->identityproviders);
 167          $data->hasinstructions = !empty($this->instructions) || $this->cansignup;
 168          $data->identityproviders = $identityproviders;
 169          list($data->instructions, $data->instructionsformat) = \core_external\util::format_text($this->instructions, FORMAT_MOODLE,
 170              context_system::instance()->id);
 171          $data->loginurl = $this->loginurl->out(false);
 172          $data->signupurl = $this->signupurl->out(false);
 173          $data->username = $this->username;
 174          $data->logintoken = $this->logintoken;
 175          $data->maintenance = format_text($this->maintenance, FORMAT_MOODLE);
 176          $data->languagemenu = $this->languagemenu;
 177          $data->recaptcha = $this->recaptcha;
 178  
 179          return $data;
 180      }
 181  }