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