MDL-62134 tool_dataprivacy: Add a manager_observer
[moodle.git] / login / index.php
blob9ab9abb7813da539722c187ec54c3d0632ad92e1
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Main login page.
21 * @package core
22 * @subpackage auth
23 * @copyright 1999 onwards Martin Dougiamas http://dougiamas.com
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../config.php');
28 require_once('lib.php');
30 redirect_if_major_upgrade_required();
32 $testsession = optional_param('testsession', 0, PARAM_INT); // test session works properly
33 $anchor = optional_param('anchor', '', PARAM_RAW); // Used to restore hash anchor to wantsurl.
35 $context = context_system::instance();
36 $PAGE->set_url("$CFG->wwwroot/login/index.php");
37 $PAGE->set_context($context);
38 $PAGE->set_pagelayout('login');
40 /// Initialize variables
41 $errormsg = '';
42 $errorcode = 0;
44 // login page requested session test
45 if ($testsession) {
46 if ($testsession == $USER->id) {
47 if (isset($SESSION->wantsurl)) {
48 $urltogo = $SESSION->wantsurl;
49 } else {
50 $urltogo = $CFG->wwwroot.'/';
52 unset($SESSION->wantsurl);
53 redirect($urltogo);
54 } else {
55 // TODO: try to find out what is the exact reason why sessions do not work
56 $errormsg = get_string("cookiesnotenabled");
57 $errorcode = 1;
61 /// Check for timed out sessions
62 if (!empty($SESSION->has_timed_out)) {
63 $session_has_timed_out = true;
64 unset($SESSION->has_timed_out);
65 } else {
66 $session_has_timed_out = false;
69 /// auth plugins may override these - SSO anyone?
70 $frm = false;
71 $user = false;
73 $authsequence = get_enabled_auth_plugins(true); // auths, in sequence
74 foreach($authsequence as $authname) {
75 $authplugin = get_auth_plugin($authname);
76 $authplugin->loginpage_hook();
80 /// Define variables used in page
81 $site = get_site();
83 // Ignore any active pages in the navigation/settings.
84 // We do this because there won't be an active page there, and by ignoring the active pages the
85 // navigation and settings won't be initialised unless something else needs them.
86 $PAGE->navbar->ignore_active();
87 $loginsite = get_string("loginsite");
88 $PAGE->navbar->add($loginsite);
90 if ($user !== false or $frm !== false or $errormsg !== '') {
91 // some auth plugin already supplied full user, fake form data or prevented user login with error message
93 } else if (!empty($SESSION->wantsurl) && file_exists($CFG->dirroot.'/login/weblinkauth.php')) {
94 // Handles the case of another Moodle site linking into a page on this site
95 //TODO: move weblink into own auth plugin
96 include($CFG->dirroot.'/login/weblinkauth.php');
97 if (function_exists('weblink_auth')) {
98 $user = weblink_auth($SESSION->wantsurl);
100 if ($user) {
101 $frm->username = $user->username;
102 } else {
103 $frm = data_submitted();
106 } else {
107 $frm = data_submitted();
110 // Restore the #anchor to the original wantsurl. Note that this
111 // will only work for internal auth plugins, SSO plugins such as
112 // SAML / CAS / OIDC will have to handle this correctly directly.
113 if ($anchor && isset($SESSION->wantsurl) && strpos($SESSION->wantsurl, '#') === false) {
114 $wantsurl = new moodle_url($SESSION->wantsurl);
115 $wantsurl->set_anchor(substr($anchor, 1));
116 $SESSION->wantsurl = $wantsurl->out();
119 /// Check if the user has actually submitted login data to us
121 if ($frm and isset($frm->username)) { // Login WITH cookies
123 $frm->username = trim(core_text::strtolower($frm->username));
125 if (is_enabled_auth('none') ) {
126 if ($frm->username !== core_user::clean_field($frm->username, 'username')) {
127 $errormsg = get_string('username').': '.get_string("invalidusername");
128 $errorcode = 2;
129 $user = null;
133 if ($user) {
134 //user already supplied by aut plugin prelogin hook
135 } else if (($frm->username == 'guest') and empty($CFG->guestloginbutton)) {
136 $user = false; /// Can't log in as guest if guest button is disabled
137 $frm = false;
138 } else {
139 if (empty($errormsg)) {
140 $user = authenticate_user_login($frm->username, $frm->password, false, $errorcode);
144 // Intercept 'restored' users to provide them with info & reset password
145 if (!$user and $frm and is_restored_user($frm->username)) {
146 $PAGE->set_title(get_string('restoredaccount'));
147 $PAGE->set_heading($site->fullname);
148 echo $OUTPUT->header();
149 echo $OUTPUT->heading(get_string('restoredaccount'));
150 echo $OUTPUT->box(get_string('restoredaccountinfo'), 'generalbox boxaligncenter');
151 require_once('restored_password_form.php'); // Use our "supplanter" login_forgot_password_form. MDL-20846
152 $form = new login_forgot_password_form('forgot_password.php', array('username' => $frm->username));
153 $form->display();
154 echo $OUTPUT->footer();
155 die;
158 if ($user) {
160 // language setup
161 if (isguestuser($user)) {
162 // no predefined language for guests - use existing session or default site lang
163 unset($user->lang);
165 } else if (!empty($user->lang)) {
166 // unset previous session language - use user preference instead
167 unset($SESSION->lang);
170 if (empty($user->confirmed)) { // This account was never confirmed
171 $PAGE->set_title(get_string("mustconfirm"));
172 $PAGE->set_heading($site->fullname);
173 echo $OUTPUT->header();
174 echo $OUTPUT->heading(get_string("mustconfirm"));
175 echo $OUTPUT->box(get_string("emailconfirmsent", "", $user->email), "generalbox boxaligncenter");
176 echo $OUTPUT->footer();
177 die;
180 /// Let's get them all set up.
181 complete_user_login($user);
183 \core\session\manager::apply_concurrent_login_limit($user->id, session_id());
185 // sets the username cookie
186 if (!empty($CFG->nolastloggedin)) {
187 // do not store last logged in user in cookie
188 // auth plugins can temporarily override this from loginpage_hook()
189 // do not save $CFG->nolastloggedin in database!
191 } else if (empty($CFG->rememberusername) or ($CFG->rememberusername == 2 and empty($frm->rememberusername))) {
192 // no permanent cookies, delete old one if exists
193 set_moodle_cookie('');
195 } else {
196 set_moodle_cookie($USER->username);
199 $urltogo = core_login_get_return_url();
201 /// check if user password has expired
202 /// Currently supported only for ldap-authentication module
203 $userauth = get_auth_plugin($USER->auth);
204 if (!isguestuser() and !empty($userauth->config->expiration) and $userauth->config->expiration == 1) {
205 $externalchangepassword = false;
206 if ($userauth->can_change_password()) {
207 $passwordchangeurl = $userauth->change_password_url();
208 if (!$passwordchangeurl) {
209 $passwordchangeurl = $CFG->wwwroot.'/login/change_password.php';
210 } else {
211 $externalchangepassword = true;
213 } else {
214 $passwordchangeurl = $CFG->wwwroot.'/login/change_password.php';
216 $days2expire = $userauth->password_expire($USER->username);
217 $PAGE->set_title("$site->fullname: $loginsite");
218 $PAGE->set_heading("$site->fullname");
219 if (intval($days2expire) > 0 && intval($days2expire) < intval($userauth->config->expiration_warning)) {
220 echo $OUTPUT->header();
221 echo $OUTPUT->confirm(get_string('auth_passwordwillexpire', 'auth', $days2expire), $passwordchangeurl, $urltogo);
222 echo $OUTPUT->footer();
223 exit;
224 } elseif (intval($days2expire) < 0 ) {
225 if ($externalchangepassword) {
226 // We end the session if the change password form is external. This prevents access to the site
227 // until the password is correctly changed.
228 require_logout();
229 } else {
230 // If we use the standard change password form, this user preference will be reset when the password
231 // is changed. Until then it will prevent access to the site.
232 set_user_preference('auth_forcepasswordchange', 1, $USER);
234 echo $OUTPUT->header();
235 echo $OUTPUT->confirm(get_string('auth_passwordisexpired', 'auth'), $passwordchangeurl, $urltogo);
236 echo $OUTPUT->footer();
237 exit;
241 // Discard any errors before the last redirect.
242 unset($SESSION->loginerrormsg);
244 // test the session actually works by redirecting to self
245 $SESSION->wantsurl = $urltogo;
246 redirect(new moodle_url(get_login_url(), array('testsession'=>$USER->id)));
248 } else {
249 if (empty($errormsg)) {
250 if ($errorcode == AUTH_LOGIN_UNAUTHORISED) {
251 $errormsg = get_string("unauthorisedlogin", "", $frm->username);
252 } else {
253 $errormsg = get_string("invalidlogin");
254 $errorcode = 3;
260 /// Detect problems with timedout sessions
261 if ($session_has_timed_out and !data_submitted()) {
262 $errormsg = get_string('sessionerroruser', 'error');
263 $errorcode = 4;
266 /// First, let's remember where the user was trying to get to before they got here
268 if (empty($SESSION->wantsurl)) {
269 $SESSION->wantsurl = null;
270 $referer = get_local_referer(false);
271 if ($referer &&
272 $referer != $CFG->wwwroot &&
273 $referer != $CFG->wwwroot . '/' &&
274 $referer != $CFG->wwwroot . '/login/' &&
275 strpos($referer, $CFG->wwwroot . '/login/?') !== 0 &&
276 strpos($referer, $CFG->wwwroot . '/login/index.php') !== 0) { // There might be some extra params such as ?lang=.
277 $SESSION->wantsurl = $referer;
281 /// Redirect to alternative login URL if needed
282 if (!empty($CFG->alternateloginurl)) {
283 $loginurl = new moodle_url($CFG->alternateloginurl);
285 $loginurlstr = $loginurl->out(false);
287 if (strpos($SESSION->wantsurl, $loginurlstr) === 0) {
288 // We do not want to return to alternate url.
289 $SESSION->wantsurl = null;
292 // If error code then add that to url.
293 if ($errorcode) {
294 $loginurl->param('errorcode', $errorcode);
297 redirect($loginurl->out(false));
300 /// Generate the login page with forms
302 if (!isset($frm) or !is_object($frm)) {
303 $frm = new stdClass();
306 if (empty($frm->username) && $authsequence[0] != 'shibboleth') { // See bug 5184
307 if (!empty($_GET["username"])) {
308 // we do not want data from _POST here
309 $frm->username = clean_param($_GET["username"], PARAM_RAW); // we do not want data from _POST here
310 } else {
311 $frm->username = get_moodle_cookie();
314 $frm->password = "";
317 if (!empty($SESSION->loginerrormsg)) {
318 // We had some errors before redirect, show them now.
319 $errormsg = $SESSION->loginerrormsg;
320 unset($SESSION->loginerrormsg);
322 } else if ($testsession) {
323 // No need to redirect here.
324 unset($SESSION->loginerrormsg);
326 } else if ($errormsg or !empty($frm->password)) {
327 // We must redirect after every password submission.
328 if ($errormsg) {
329 $SESSION->loginerrormsg = $errormsg;
331 redirect(new moodle_url('/login/index.php'));
334 $PAGE->set_title("$site->fullname: $loginsite");
335 $PAGE->set_heading("$site->fullname");
337 echo $OUTPUT->header();
339 if (isloggedin() and !isguestuser()) {
340 // prevent logging when already logged in, we do not want them to relogin by accident because sesskey would be changed
341 echo $OUTPUT->box_start();
342 $logout = new single_button(new moodle_url('/login/logout.php', array('sesskey'=>sesskey(),'loginpage'=>1)), get_string('logout'), 'post');
343 $continue = new single_button(new moodle_url('/'), get_string('cancel'), 'get');
344 echo $OUTPUT->confirm(get_string('alreadyloggedin', 'error', fullname($USER)), $logout, $continue);
345 echo $OUTPUT->box_end();
346 } else {
347 $loginform = new \core_auth\output\login($authsequence, $frm->username);
348 $loginform->set_error($errormsg);
349 echo $OUTPUT->render($loginform);
352 echo $OUTPUT->footer();