new session timeout handling
[moodle.git] / login / index.php
CommitLineData
9a58f7cb 1<?php // $Id$
b0e3a925 2
3 require_once("../config.php");
a718d872 4
e9f7ee0e 5 $loginguest = optional_param('loginguest', false); // determines whether visitors are logged in as guest automatically
f9903ed0 6
90b84141 7 //HTTPS is potentially required in this page
8 httpsrequired();
9
a718d872 10/// Check if the guest user exists. If not, create one.
a9b07c52 11 if (! record_exists("user", "username", "guest")) {
4e11ad4f 12 $guest->auth = "manual";
a9b07c52 13 $guest->username = "guest";
14 $guest->password = md5("guest");
b95c3ab8 15 $guest->firstname = addslashes(get_string("guestuser"));
cf38360f 16 $guest->lastname = " ";
a9b07c52 17 $guest->email = "root@localhost";
b95c3ab8 18 $guest->description = addslashes(get_string("guestuserinfo"));
a9b07c52 19 $guest->confirmed = 1;
ad6c33dc 20 $guest->lang = $CFG->lang;
a9b07c52 21 $guest->timemodified= time();
22
23 if (! $guest->id = insert_record("user", $guest)) {
24 notify("Could not create guest user record !!!");
25 }
26 }
da79d75d 27
a718d872 28/// Load alternative login screens if necessary
29
da79d75d 30 if ($CFG->auth == 'cas' && !empty($CFG->cas_enabled)) {
31 require($CFG->dirroot.'/auth/cas/login.php');
32 }
a718d872 33
34 if ($CFG->auth == 'shibboleth') {
e9f7ee0e 35 if (!empty($SESSION->shibboleth_checked) ) { // Just come from there
a718d872 36 unset($SESSION->shibboleth_checked);
e9f7ee0e 37 } else if (empty($_POST)) { // No incoming data, so redirect
565809c6 38 redirect($CFG->wwwroot.'/auth/shibboleth/index.php');
a718d872 39 }
40 }
089b19f6 41
a718d872 42
43
44/// Define variables used in page
089b19f6 45 if (!$site = get_site()) {
46 error("No site found!");
47 }
48
49 if (empty($CFG->langmenu)) {
50 $langmenu = "";
51 } else {
52 $currlang = current_language();
53 $langs = get_list_of_languages();
90b84141 54 $langmenu = popup_form ("$CFG->httpswwwroot/login/index.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
089b19f6 55 }
56
57 $loginsite = get_string("loginsite");
58
d9969553 59 $loginurl = (!empty($CFG->alternateloginurl)) ? $CFG->alternateloginurl : '';
a9b07c52 60
d00377f5 61 $frm = false;
0e8a3f25 62 $user = false;
a718d872 63
64
e06f15ae 65 if ((!empty($SESSION->wantsurl) and strstr($SESSION->wantsurl,'username=guest')) or $loginguest) {
d00377f5 66 /// Log in as guest automatically (idea from Zbigniew Fiedorowicz)
e06f15ae 67 $frm->username = 'guest';
68 $frm->password = 'guest';
69 } else if (!empty($SESSION->wantsurl) && file_exists($CFG->dirroot.'/login/weblinkauth.php')) {
70 // Handles the case of another Moodle site linking into a page on this site
71 include($CFG->dirroot.'/login/weblinkauth.php');
0e8a3f25 72 if (function_exists(weblink_auth)) {
73 $user = weblink_auth($SESSION->wantsurl);
74 }
75 if ($user) {
76 $frm->username = $user->username;
77 } else {
d9969553 78 $frm = data_submitted($loginurl);
0e8a3f25 79 }
d00377f5 80 } else {
d9969553 81 $frm = data_submitted($loginurl);
d00377f5 82 }
a9b07c52 83
a718d872 84/// Check if the user has actually submitted login data to us
85
96b56581 86 if ($frm and (get_moodle_cookie() == '') and ($frm->username!='guest') and !$user and empty($CFG->alternateloginurl)) { // Login without cookie
792197b0 87
88 $errormsg = get_string("cookiesnotenabled");
89
30b88cb7 90 } else if ($frm) { // Login WITH cookies
792197b0 91
1e22bc9c 92 $frm->username = trim(moodle_strtolower($frm->username));
cf5560fb 93
94 if (($frm->username == 'guest') and empty($CFG->guestloginbutton)) {
95 $user = false; /// Can't log in as guest if guest button is disabled
96 $frm = false;
0e8a3f25 97 } else if (!$user) {
cf5560fb 98 $user = authenticate_user_login($frm->username, $frm->password);
99 }
bee00f48 100 update_login_count();
f9903ed0 101
102 if ($user) {
a718d872 103
104 if (empty($user->confirmed)) { // This account was never confirmed
fa0626c6 105 print_header(get_string("mustconfirm"), get_string("mustconfirm") );
6bebcf8d 106 print_heading(get_string("mustconfirm"));
107 print_simple_box(get_string("emailconfirmsent", "", $user->email), "center");
8553b700 108 print_footer();
f9903ed0 109 die;
110 }
c21c671d 111
a718d872 112 // Let's get them all set up.
f9903ed0 113 $USER = $user;
f9903ed0 114
a718d872 115 add_to_log(SITEID, 'user', 'login', "view.php?id=$USER->id&course=".SITEID, $USER->id, 0, $USER->id);
e1f96c23 116
054193be 117
a718d872 118 update_user_login_times();
119 set_moodle_cookie($USER->username);
120 set_login_session_preferences();
121
122
089b19f6 123 //Select password change url
3f11ce14 124 if (is_internal_auth($USER->auth) || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){
089b19f6 125 $passwordchangeurl=$CFG->wwwroot.'/login/change_password.php';
126 } elseif($CFG->changepassword) {
127 $passwordchangeurl=$CFG->changepassword;
128 }
129
d35757eb 130 // check whether the user should be changing password
b1ace59a 131 if (get_user_preferences('auth_forcepasswordchange', false) || $frm->password == 'changeme'){
089b19f6 132 if (isset($passwordchangeurl)) {
133 redirect($passwordchangeurl);
d35757eb 134 } else {
135 error("You cannot proceed without changing your password.
136 However there is no available page for changing it.
137 Please contact your Moodle Administrator.");
138 }
139 }
140
e06f15ae 141
808a3baa 142 if (user_not_fully_set_up($USER)) {
089b19f6 143 $urltogo = $CFG->wwwroot.'/user/edit.php?id='.$USER->id.'&amp;course='.SITEID;
e06f15ae 144 // We don't delete $SESSION->wantsurl yet, so we get there later
808a3baa 145
e06f15ae 146 } else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
147 $urltogo = $SESSION->wantsurl; /// Because it's an address in this site
148 unset($SESSION->wantsurl);
808a3baa 149
bee00f48 150 } else {
0d6b9d4f 151 $urltogo = $CFG->wwwroot.'/my'; /// Go to the standard home page
62e4c095 152 if (isadmin() || empty($CFG->mymoodleredirect)) {
153 $urltogo = $CFG->wwwroot; /// not needed by admins or when it's turned off
0d6b9d4f 154 }
e06f15ae 155 unset($SESSION->wantsurl); /// Just in case
bee00f48 156 }
089b19f6 157
158 // check if user password has expired
159 // Currently supported only for ldap-authentication module
160 if (isset($CFG->ldap_expiration) && $CFG->ldap_expiration == 1 ) {
161 if (function_exists('auth_password_expire')){
162 $days2expire = auth_password_expire($USER->username);
163 if (intval($days2expire) > 0 && intval($days2expire) < intval($CFG->{$USER->auth.'_expiration_warning'})) {
164 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
165 notice_yesno(get_string('auth_passwordwillexpire', 'auth', $days2expire), $passwordchangeurl, $urltogo);
166 print_footer();
167 exit;
168 } elseif (intval($days2expire) < 0 ) {
169 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
170 notice_yesno(get_string('auth_passwordisexpired', 'auth'), $passwordchangeurl, $urltogo);
171 print_footer();
172 exit;
173 }
174 }
175 }
176
bee00f48 177 reset_login_count();
f9903ed0 178
bcdcbccc 179 redirect($urltogo);
180
e06f15ae 181 exit;
f9903ed0 182
183 } else {
fa0626c6 184 $errormsg = get_string("invalidlogin");
f9903ed0 185 }
186 }
8223d271 187
f9903ed0 188
d9969553 189/// We need to show a login form
190
191/// First, let's remember where the user was trying to get to before they got here
9c9f7d77 192
f9903ed0 193 if (empty($SESSION->wantsurl)) {
0d6b9d4f 194 $SESSION->wantsurl = (array_key_exists('HTTP_REFERER',$_SERVER) &&
195 $_SERVER["HTTP_REFERER"] != $CFG->wwwroot &&
196 $_SERVER["HTTP_REFERER"] != $CFG->wwwroot.'/')
197 ? $_SERVER["HTTP_REFERER"] : NULL;
f9903ed0 198 }
792197b0 199
d9969553 200 if (!empty($loginurl)) { // We don't want the standard forms, go elsewhere
201 redirect($loginurl);
202 }
203
204
a718d872 205/// Generate the login page with forms
d9969553 206
207 if (empty($errormsg)) {
208 $errormsg = '';
209 }
210
792197b0 211 if (get_moodle_cookie() == '') {
212 set_moodle_cookie('nobody'); // To help search for cookies
213 }
f9903ed0 214
9c9f7d77 215 if (empty($frm->username)) {
792197b0 216 $frm->username = get_moodle_cookie() === 'nobody' ? '' : get_moodle_cookie();
9c9f7d77 217 $frm->password = "";
218 }
f9903ed0 219
9c9f7d77 220 if (!empty($frm->username)) {
a46b14ef 221 $focus = "login.password";
f9903ed0 222 } else {
a46b14ef 223 $focus = "login.username";
f9903ed0 224 }
35a48c9a 225
5f2c35db 226 if ($CFG->auth == "email" or $CFG->auth == "none" or chop($CFG->auth_instructions) <> "" ) {
35a48c9a 227 $show_instructions = true;
228 } else {
229 $show_instructions = false;
230 }
5b2ae584 231
d9969553 232 print_header("$site->fullname: $loginsite", $site->fullname, $loginsite, $focus,
233 '', true, '<div class="langmenu" align="right">'.$langmenu.'</div>');
234
f9903ed0 235 include("index_form.html");
236
d9969553 237 print_footer();
f9903ed0 238
f9903ed0 239
f9903ed0 240?>