Automatic installer.php lang files by installer_builder (20080430)
[moodle.git] / login / index.php
blob638bf70e9a9e7fcad725f373b01c6e8e77818827
1 <?php // $Id$
3 require_once("../config.php");
5 $loginguest = optional_param('loginguest', 0, PARAM_BOOL); // determines whether visitors are logged in as guest automatically
6 $testcookies = optional_param('testcookies', 0, PARAM_BOOL); // request cookie test
8 //initialize variables
9 $errormsg = '';
11 /// Check for timed out sessions
12 if (!empty($SESSION->has_timed_out)) {
13 $session_has_timed_out = true;
14 $SESSION->has_timed_out = false;
15 } else {
16 $session_has_timed_out = false;
19 //HTTPS is potentially required in this page
20 httpsrequired();
22 /// Check if the guest user exists. If not, create one.
23 if (! record_exists('user', 'username', 'guest')) {
24 $guest->auth = 'manual';
25 $guest->username = 'guest';
26 $guest->password = hash_internal_user_password('guest');
27 $guest->firstname = addslashes(get_string('guestuser'));
28 $guest->lastname = ' ';
29 $guest->email = 'root@localhost';
30 $guest->description = addslashes(get_string('guestuserinfo'));
31 $guest->confirmed = 1;
32 $guest->lang = $CFG->lang;
33 $guest->timemodified= time();
35 if (! $guest->id = insert_record('user', $guest)) {
36 notify('Could not create guest user record !!!');
40 /// Load alternative login screens if necessary
42 if ($CFG->auth == 'cas' && !empty($CFG->cas_enabled)) {
43 require($CFG->dirroot.'/auth/cas/login.php');
46 // See http://moodle.org/mod/forum/discuss.php?d=39918#187611
47 // if ($CFG->auth == 'shibboleth') {
48 // if (!empty($SESSION->shibboleth_checked) ) { // Just come from there
49 // unset($SESSION->shibboleth_checked);
50 // } else if (empty($_POST)) { // No incoming data, so redirect
51 // redirect($CFG->wwwroot.'/auth/shibboleth/index.php');
52 // }
53 // }
57 /// Define variables used in page
58 if (!$site = get_site()) {
59 error("No site found!");
62 if (empty($CFG->langmenu)) {
63 $langmenu = "";
64 } else {
65 $currlang = current_language();
66 $langs = get_list_of_languages();
67 $langmenu = popup_form ("$CFG->httpswwwroot/login/index.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
70 $loginsite = get_string("loginsite");
72 $loginurl = (!empty($CFG->alternateloginurl)) ? $CFG->alternateloginurl : '';
74 $frm = false;
75 $user = false;
78 if ((!empty($SESSION->wantsurl) and strstr($SESSION->wantsurl,'username=guest')) or $loginguest) {
79 /// Log in as guest automatically (idea from Zbigniew Fiedorowicz)
80 $frm->username = 'guest';
81 $frm->password = 'guest';
82 } else if (!empty($SESSION->wantsurl) && file_exists($CFG->dirroot.'/login/weblinkauth.php')) {
83 // Handles the case of another Moodle site linking into a page on this site
84 include($CFG->dirroot.'/login/weblinkauth.php');
85 if (function_exists(weblink_auth)) {
86 $user = weblink_auth($SESSION->wantsurl);
88 if ($user) {
89 $frm->username = $user->username;
90 } else {
91 $frm = data_submitted($loginurl);
93 } else {
94 $frm = data_submitted($loginurl);
97 /// Check if the user has actually submitted login data to us
99 if (empty($CFG->usesid) and $testcookies and (get_moodle_cookie() == '')) { // Login without cookie when test requested
101 $errormsg = get_string("cookiesnotenabled");
103 } else if ($frm) { // Login WITH cookies
105 $frm->username = trim(moodle_strtolower($frm->username));
107 if ($CFG->auth == 'none' && empty($CFG->extendedusernamechars)) {
108 $string = eregi_replace("[^(-\.[:alnum:])]", "", $frm->username);
109 if (strcmp($frm->username, $string)) {
110 $errormsg = get_string('username').': '.get_string("alphanumerical");
111 $user = null;
115 if (($frm->username == 'guest') and empty($CFG->guestloginbutton)) {
116 $user = false; /// Can't log in as guest if guest button is disabled
117 $frm = false;
118 } else if (!$user) {
119 if (empty($errormsg)) {
120 $user = authenticate_user_login($frm->username, $frm->password);
123 update_login_count();
125 if ($user) {
127 if (empty($user->confirmed)) { // This account was never confirmed
128 print_header(get_string("mustconfirm"), get_string("mustconfirm") );
129 print_heading(get_string("mustconfirm"));
130 print_simple_box(get_string("emailconfirmsent", "", $user->email), "center");
131 print_footer();
132 die;
135 // Let's get them all set up.
136 $USER = $user;
138 add_to_log(SITEID, 'user', 'login', "view.php?id=$USER->id&course=".SITEID, $USER->id, 0, $USER->id);
141 update_user_login_times();
142 set_moodle_cookie($USER->username);
143 set_login_session_preferences();
145 /// This is what lets the user do anything on the site :-)
146 load_all_capabilities();
148 //Select password change url
149 if (is_internal_auth($USER->auth) || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){
150 $passwordchangeurl=$CFG->wwwroot.'/login/change_password.php';
151 } elseif($CFG->changepassword) {
152 $passwordchangeurl=$CFG->changepassword;
153 } else {
154 $passwordchangeurl = '';
157 // check whether the user should be changing password
158 if (get_user_preferences('auth_forcepasswordchange', false) || $frm->password == 'changeme'){
159 if ($passwordchangeurl != '') {
160 redirect($passwordchangeurl);
161 } else {
162 error("You cannot proceed without changing your password.
163 However there is no available page for changing it.
164 Please contact your Moodle Administrator.");
169 /// Prepare redirection
170 if (user_not_fully_set_up($USER)) {
171 $urltogo = $CFG->wwwroot.'/user/edit.php?id='.$USER->id.'&amp;course='.SITEID;
172 // We don't delete $SESSION->wantsurl yet, so we get there later
174 } else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
175 $urltogo = $SESSION->wantsurl; /// Because it's an address in this site
176 unset($SESSION->wantsurl);
178 } else {
179 // no wantsurl stored or external - go to homepage
180 $urltogo = $CFG->wwwroot.'/';
181 unset($SESSION->wantsurl);
184 /// Go to my-moodle page instead of homepage if mymoodleredirect enabled
185 if (!has_capability('moodle/site:config',get_context_instance(CONTEXT_SYSTEM, SITEID)) and !empty($CFG->mymoodleredirect) and !isguest()) {
186 if ($urltogo == $CFG->wwwroot or $urltogo == $CFG->wwwroot.'/' or $urltogo == $CFG->wwwroot.'/index.php') {
187 $urltogo = $CFG->wwwroot.'/my/';
192 // check if user password has expired
193 // Currently supported only for ldap-authentication module
194 if (isset($CFG->ldap_expiration) && $CFG->ldap_expiration == 1 ) {
195 if (function_exists('auth_password_expire')){
196 $days2expire = auth_password_expire($USER->username);
197 if (intval($days2expire) > 0 && intval($days2expire) < intval($CFG->{$USER->auth.'_expiration_warning'})) {
198 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
199 notice_yesno(get_string('auth_passwordwillexpire', 'auth', $days2expire), $passwordchangeurl, $urltogo);
200 print_footer();
201 exit;
202 } elseif (intval($days2expire) < 0 ) {
203 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
204 notice_yesno(get_string('auth_passwordisexpired', 'auth'), $passwordchangeurl, $urltogo);
205 print_footer();
206 exit;
211 reset_login_count();
213 redirect($urltogo);
215 exit;
217 } else {
218 if (empty($errormsg)) {
219 $errormsg = get_string("invalidlogin");
225 /// We need to show a login form
227 /// First, let's remember where the user was trying to get to before they got here
229 if (empty($SESSION->wantsurl)) {
230 $SESSION->wantsurl = (array_key_exists('HTTP_REFERER',$_SERVER) &&
231 $_SERVER["HTTP_REFERER"] != $CFG->wwwroot &&
232 $_SERVER["HTTP_REFERER"] != $CFG->wwwroot.'/' &&
233 $_SERVER["HTTP_REFERER"] != $CFG->httpswwwroot.'/login/' &&
234 $_SERVER["HTTP_REFERER"] != $CFG->httpswwwroot.'/login/index.php')
235 ? $_SERVER["HTTP_REFERER"] : NULL;
238 if (!empty($loginurl)) { // We don't want the standard forms, go elsewhere
239 redirect($loginurl);
243 /// Generate the login page with forms
245 if ($session_has_timed_out) {
246 $errormsg = get_string('sessionerroruser', 'error');
249 if (get_moodle_cookie() == '') {
250 set_moodle_cookie('nobody'); // To help search for cookies
253 if (empty($frm->username) && $CFG->auth != 'shibboleth') { // See bug 5184
254 $frm->username = get_moodle_cookie() === 'nobody' ? '' : get_moodle_cookie();
255 $frm->password = "";
258 if (!empty($frm->username)) {
259 $focus = "login.password";
260 } else {
261 $focus = "login.username";
264 if (isset($CFG->auth_instructions)) {
265 $CFG->auth_instructions = trim($CFG->auth_instructions);
267 if ($CFG->auth == "email" or $CFG->auth == "none" or !empty($CFG->auth_instructions)) {
268 $show_instructions = true;
269 } else {
270 $show_instructions = false;
273 print_header("$site->fullname: $loginsite", $site->fullname, $loginsite, $focus,
274 '', true, '<div class="langmenu" align="right">'.$langmenu.'</div>');
276 include("index_form.html");
278 print_footer();