Merge branch 'MDL-46239-28-2' of git://github.com/xow/moodle into MOODLE_28_STABLE
[moodle.git] / login / index.php
blobdab9cb1d19bf5ce16f09aeb5b446031de6214df4
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 // Try to prevent searching for sites that allow sign-up.
31 if (!isset($CFG->additionalhtmlhead)) {
32 $CFG->additionalhtmlhead = '';
34 $CFG->additionalhtmlhead .= '<meta name="robots" content="noindex" />';
36 redirect_if_major_upgrade_required();
38 $testsession = optional_param('testsession', 0, PARAM_INT); // test session works properly
39 $cancel = optional_param('cancel', 0, PARAM_BOOL); // redirect to frontpage, needed for loginhttps
41 if ($cancel) {
42 redirect(new moodle_url('/'));
45 //HTTPS is required in this page when $CFG->loginhttps enabled
46 $PAGE->https_required();
48 $context = context_system::instance();
49 $PAGE->set_url("$CFG->httpswwwroot/login/index.php");
50 $PAGE->set_context($context);
51 $PAGE->set_pagelayout('login');
53 /// Initialize variables
54 $errormsg = '';
55 $errorcode = 0;
57 // login page requested session test
58 if ($testsession) {
59 if ($testsession == $USER->id) {
60 if (isset($SESSION->wantsurl)) {
61 $urltogo = $SESSION->wantsurl;
62 } else {
63 $urltogo = $CFG->wwwroot.'/';
65 unset($SESSION->wantsurl);
66 redirect($urltogo);
67 } else {
68 // TODO: try to find out what is the exact reason why sessions do not work
69 $errormsg = get_string("cookiesnotenabled");
70 $errorcode = 1;
74 /// Check for timed out sessions
75 if (!empty($SESSION->has_timed_out)) {
76 $session_has_timed_out = true;
77 unset($SESSION->has_timed_out);
78 } else {
79 $session_has_timed_out = false;
82 /// auth plugins may override these - SSO anyone?
83 $frm = false;
84 $user = false;
86 $authsequence = get_enabled_auth_plugins(true); // auths, in sequence
87 foreach($authsequence as $authname) {
88 $authplugin = get_auth_plugin($authname);
89 $authplugin->loginpage_hook();
93 /// Define variables used in page
94 $site = get_site();
96 $loginsite = get_string("loginsite");
97 $PAGE->navbar->add($loginsite);
99 if ($user !== false or $frm !== false or $errormsg !== '') {
100 // some auth plugin already supplied full user, fake form data or prevented user login with error message
102 } else if (!empty($SESSION->wantsurl) && file_exists($CFG->dirroot.'/login/weblinkauth.php')) {
103 // Handles the case of another Moodle site linking into a page on this site
104 //TODO: move weblink into own auth plugin
105 include($CFG->dirroot.'/login/weblinkauth.php');
106 if (function_exists('weblink_auth')) {
107 $user = weblink_auth($SESSION->wantsurl);
109 if ($user) {
110 $frm->username = $user->username;
111 } else {
112 $frm = data_submitted();
115 } else {
116 $frm = data_submitted();
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 !== clean_param($frm->username, PARAM_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 // sets the username cookie
184 if (!empty($CFG->nolastloggedin)) {
185 // do not store last logged in user in cookie
186 // auth plugins can temporarily override this from loginpage_hook()
187 // do not save $CFG->nolastloggedin in database!
189 } else if (empty($CFG->rememberusername) or ($CFG->rememberusername == 2 and empty($frm->rememberusername))) {
190 // no permanent cookies, delete old one if exists
191 set_moodle_cookie('');
193 } else {
194 set_moodle_cookie($USER->username);
197 $urltogo = core_login_get_return_url();
199 /// check if user password has expired
200 /// Currently supported only for ldap-authentication module
201 $userauth = get_auth_plugin($USER->auth);
202 if (!empty($userauth->config->expiration) and $userauth->config->expiration == 1) {
203 if ($userauth->can_change_password()) {
204 $passwordchangeurl = $userauth->change_password_url();
205 if (!$passwordchangeurl) {
206 $passwordchangeurl = $CFG->httpswwwroot.'/login/change_password.php';
208 } else {
209 $passwordchangeurl = $CFG->httpswwwroot.'/login/change_password.php';
211 $days2expire = $userauth->password_expire($USER->username);
212 $PAGE->set_title("$site->fullname: $loginsite");
213 $PAGE->set_heading("$site->fullname");
214 if (intval($days2expire) > 0 && intval($days2expire) < intval($userauth->config->expiration_warning)) {
215 echo $OUTPUT->header();
216 echo $OUTPUT->confirm(get_string('auth_passwordwillexpire', 'auth', $days2expire), $passwordchangeurl, $urltogo);
217 echo $OUTPUT->footer();
218 exit;
219 } elseif (intval($days2expire) < 0 ) {
220 echo $OUTPUT->header();
221 echo $OUTPUT->confirm(get_string('auth_passwordisexpired', 'auth'), $passwordchangeurl, $urltogo);
222 echo $OUTPUT->footer();
223 exit;
227 // Discard any errors before the last redirect.
228 unset($SESSION->loginerrormsg);
230 // test the session actually works by redirecting to self
231 $SESSION->wantsurl = $urltogo;
232 redirect(new moodle_url(get_login_url(), array('testsession'=>$USER->id)));
234 } else {
235 if (empty($errormsg)) {
236 if ($errorcode == AUTH_LOGIN_UNAUTHORISED) {
237 $errormsg = get_string("unauthorisedlogin", "", $frm->username);
238 } else {
239 $errormsg = get_string("invalidlogin");
240 $errorcode = 3;
246 /// Detect problems with timedout sessions
247 if ($session_has_timed_out and !data_submitted()) {
248 $errormsg = get_string('sessionerroruser', 'error');
249 $errorcode = 4;
252 /// First, let's remember where the user was trying to get to before they got here
254 if (empty($SESSION->wantsurl)) {
255 $SESSION->wantsurl = null;
256 $referer = get_local_referer(false);
257 if ($referer &&
258 $referer != $CFG->wwwroot &&
259 $referer != $CFG->wwwroot . '/' &&
260 $referer != $CFG->httpswwwroot . '/login/' &&
261 strpos($referer, $CFG->httpswwwroot . '/login/?') !== 0 &&
262 strpos($referer, $CFG->httpswwwroot . '/login/index.php') !== 0) { // There might be some extra params such as ?lang=.
263 $SESSION->wantsurl = $referer;
267 /// Redirect to alternative login URL if needed
268 if (!empty($CFG->alternateloginurl)) {
269 $loginurl = $CFG->alternateloginurl;
271 if (strpos($SESSION->wantsurl, $loginurl) === 0) {
272 //we do not want to return to alternate url
273 $SESSION->wantsurl = NULL;
276 if ($errorcode) {
277 if (strpos($loginurl, '?') === false) {
278 $loginurl .= '?';
279 } else {
280 $loginurl .= '&';
282 $loginurl .= 'errorcode='.$errorcode;
285 redirect($loginurl);
288 // make sure we really are on the https page when https login required
289 $PAGE->verify_https_required();
291 /// Generate the login page with forms
293 if (!isset($frm) or !is_object($frm)) {
294 $frm = new stdClass();
297 if (empty($frm->username) && $authsequence[0] != 'shibboleth') { // See bug 5184
298 if (!empty($_GET["username"])) {
299 $frm->username = clean_param($_GET["username"], PARAM_RAW); // we do not want data from _POST here
300 } else {
301 $frm->username = get_moodle_cookie();
304 $frm->password = "";
307 if (!empty($frm->username)) {
308 $focus = "password";
309 } else {
310 $focus = "username";
313 if (!empty($CFG->registerauth) or is_enabled_auth('none') or !empty($CFG->auth_instructions)) {
314 $show_instructions = true;
315 } else {
316 $show_instructions = false;
319 $potentialidps = array();
320 foreach($authsequence as $authname) {
321 $authplugin = get_auth_plugin($authname);
322 $potentialidps = array_merge($potentialidps, $authplugin->loginpage_idp_list($SESSION->wantsurl));
325 if (!empty($SESSION->loginerrormsg)) {
326 // We had some errors before redirect, show them now.
327 $errormsg = $SESSION->loginerrormsg;
328 unset($SESSION->loginerrormsg);
330 } else if ($testsession) {
331 // No need to redirect here.
332 unset($SESSION->loginerrormsg);
334 } else if ($errormsg or !empty($frm->password)) {
335 // We must redirect after every password submission.
336 if ($errormsg) {
337 $SESSION->loginerrormsg = $errormsg;
339 redirect(new moodle_url('/login/index.php'));
342 $PAGE->set_title("$site->fullname: $loginsite");
343 $PAGE->set_heading("$site->fullname");
345 echo $OUTPUT->header();
347 if (isloggedin() and !isguestuser()) {
348 // prevent logging when already logged in, we do not want them to relogin by accident because sesskey would be changed
349 echo $OUTPUT->box_start();
350 $logout = new single_button(new moodle_url($CFG->httpswwwroot.'/login/logout.php', array('sesskey'=>sesskey(),'loginpage'=>1)), get_string('logout'), 'post');
351 $continue = new single_button(new moodle_url($CFG->httpswwwroot.'/login/index.php', array('cancel'=>1)), get_string('cancel'), 'get');
352 echo $OUTPUT->confirm(get_string('alreadyloggedin', 'error', fullname($USER)), $logout, $continue);
353 echo $OUTPUT->box_end();
354 } else {
355 include("index_form.html");
356 if ($errormsg) {
357 $PAGE->requires->js_init_call('M.util.focus_login_error', null, true);
358 } else if (!empty($CFG->loginpageautofocus)) {
359 //focus username or password
360 $PAGE->requires->js_init_call('M.util.focus_login_form', null, true);
364 echo $OUTPUT->footer();