3 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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
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
57 // login page requested session test
59 if ($testsession == $USER->id
) {
60 if (isset($SESSION->wantsurl
)) {
61 $urltogo = $SESSION->wantsurl
;
63 $urltogo = $CFG->wwwroot
.'/';
65 unset($SESSION->wantsurl
);
68 // TODO: try to find out what is the exact reason why sessions do not work
69 $errormsg = get_string("cookiesnotenabled");
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
);
79 $session_has_timed_out = false;
82 /// auth plugins may override these - SSO anyone?
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
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
);
110 $frm->username
= $user->username
;
112 $frm = data_submitted();
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");
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
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
));
154 echo $OUTPUT->footer();
161 if (isguestuser($user)) {
162 // no predefined language for guests - use existing session or default site 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();
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('');
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';
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();
219 } elseif (intval($days2expire) < 0 ) {
220 echo $OUTPUT->header();
221 echo $OUTPUT->confirm(get_string('auth_passwordisexpired', 'auth'), $passwordchangeurl, $urltogo);
222 echo $OUTPUT->footer();
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
)));
235 if (empty($errormsg)) {
236 if ($errorcode == AUTH_LOGIN_UNAUTHORISED
) {
237 $errormsg = get_string("unauthorisedlogin", "", $frm->username
);
239 $errormsg = get_string("invalidlogin");
246 /// Detect problems with timedout sessions
247 if ($session_has_timed_out and !data_submitted()) {
248 $errormsg = get_string('sessionerroruser', 'error');
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
= (array_key_exists('HTTP_REFERER',$_SERVER) &&
256 $_SERVER["HTTP_REFERER"] != $CFG->wwwroot
&&
257 $_SERVER["HTTP_REFERER"] != $CFG->wwwroot
.'/' &&
258 $_SERVER["HTTP_REFERER"] != $CFG->httpswwwroot
.'/login/' &&
259 strpos($_SERVER["HTTP_REFERER"], $CFG->httpswwwroot
.'/login/?') !== 0 &&
260 strpos($_SERVER["HTTP_REFERER"], $CFG->httpswwwroot
.'/login/index.php') !== 0) // There might be some extra params such as ?lang=.
261 ?
$_SERVER["HTTP_REFERER"] : NULL;
264 /// Redirect to alternative login URL if needed
265 if (!empty($CFG->alternateloginurl
)) {
266 $loginurl = $CFG->alternateloginurl
;
268 if (strpos($SESSION->wantsurl
, $loginurl) === 0) {
269 //we do not want to return to alternate url
270 $SESSION->wantsurl
= NULL;
274 if (strpos($loginurl, '?') === false) {
279 $loginurl .= 'errorcode='.$errorcode;
285 // make sure we really are on the https page when https login required
286 $PAGE->verify_https_required();
288 /// Generate the login page with forms
290 if (!isset($frm) or !is_object($frm)) {
291 $frm = new stdClass();
294 if (empty($frm->username
) && $authsequence[0] != 'shibboleth') { // See bug 5184
295 if (!empty($_GET["username"])) {
296 $frm->username
= clean_param($_GET["username"], PARAM_RAW
); // we do not want data from _POST here
298 $frm->username
= get_moodle_cookie();
304 if (!empty($frm->username
)) {
310 if (!empty($CFG->registerauth
) or is_enabled_auth('none') or !empty($CFG->auth_instructions
)) {
311 $show_instructions = true;
313 $show_instructions = false;
316 $potentialidps = array();
317 foreach($authsequence as $authname) {
318 $authplugin = get_auth_plugin($authname);
319 $potentialidps = array_merge($potentialidps, $authplugin->loginpage_idp_list($SESSION->wantsurl
));
322 if (!empty($SESSION->loginerrormsg
)) {
323 // We had some errors before redirect, show them now.
324 $errormsg = $SESSION->loginerrormsg
;
325 unset($SESSION->loginerrormsg
);
327 } else if ($testsession) {
328 // No need to redirect here.
329 unset($SESSION->loginerrormsg
);
331 } else if ($errormsg or !empty($frm->password
)) {
332 // We must redirect after every password submission.
334 $SESSION->loginerrormsg
= $errormsg;
336 redirect(new moodle_url('/login/index.php'));
339 $PAGE->set_title("$site->fullname: $loginsite");
340 $PAGE->set_heading("$site->fullname");
342 echo $OUTPUT->header();
344 if (isloggedin() and !isguestuser()) {
345 // prevent logging when already logged in, we do not want them to relogin by accident because sesskey would be changed
346 echo $OUTPUT->box_start();
347 $logout = new single_button(new moodle_url($CFG->httpswwwroot
.'/login/logout.php', array('sesskey'=>sesskey(),'loginpage'=>1)), get_string('logout'), 'post');
348 $continue = new single_button(new moodle_url($CFG->httpswwwroot
.'/login/index.php', array('cancel'=>1)), get_string('cancel'), 'get');
349 echo $OUTPUT->confirm(get_string('alreadyloggedin', 'error', fullname($USER)), $logout, $continue);
350 echo $OUTPUT->box_end();
352 include("index_form.html");
354 $PAGE->requires
->js_init_call('M.util.focus_login_error', null, true);
355 } else if (!empty($CFG->loginpageautofocus
)) {
356 //focus username or password
357 $PAGE->requires
->js_init_call('M.util.focus_login_form', null, true);
361 echo $OUTPUT->footer();