Updated formatting in getremoteaddr, and made add_to_log use it
[moodle.git] / login / index.php
CommitLineData
9a58f7cb 1<?php // $Id$
b0e3a925 2
3 require_once("../config.php");
fe657136 4 optional_variable($loginguest, false); // determines whether visitors are logged in as guest automatically
f9903ed0 5
a9b07c52 6 // Check if the guest user exists. If not, create one.
7 if (! record_exists("user", "username", "guest")) {
4e11ad4f 8 $guest->auth = "manual";
a9b07c52 9 $guest->username = "guest";
10 $guest->password = md5("guest");
b95c3ab8 11 $guest->firstname = addslashes(get_string("guestuser"));
cf38360f 12 $guest->lastname = " ";
a9b07c52 13 $guest->email = "root@localhost";
b95c3ab8 14 $guest->description = addslashes(get_string("guestuserinfo"));
a9b07c52 15 $guest->confirmed = 1;
ad6c33dc 16 $guest->lang = $CFG->lang;
a9b07c52 17 $guest->timemodified= time();
18
19 if (! $guest->id = insert_record("user", $guest)) {
20 notify("Could not create guest user record !!!");
21 }
22 }
da79d75d 23
24 if ($CFG->auth == 'cas' && !empty($CFG->cas_enabled)) {
25 require($CFG->dirroot.'/auth/cas/login.php');
26 }
089b19f6 27
28 //Define variables used in page
29 if (!$site = get_site()) {
30 error("No site found!");
31 }
32
33 if (empty($CFG->langmenu)) {
34 $langmenu = "";
35 } else {
36 $currlang = current_language();
37 $langs = get_list_of_languages();
38 if (empty($CFG->loginhttps)) {
39 $wwwroot = $CFG->wwwroot;
40 } else {
41 $wwwroot = str_replace('http','https',$CFG->wwwroot);
42 }
43 $langmenu = popup_form ("$wwwroot/login/index.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
44 }
45
46 $loginsite = get_string("loginsite");
47
a9b07c52 48
d00377f5 49 $frm = false;
0e8a3f25 50 $user = false;
e06f15ae 51 if ((!empty($SESSION->wantsurl) and strstr($SESSION->wantsurl,'username=guest')) or $loginguest) {
d00377f5 52 /// Log in as guest automatically (idea from Zbigniew Fiedorowicz)
e06f15ae 53 $frm->username = 'guest';
54 $frm->password = 'guest';
55 } else if (!empty($SESSION->wantsurl) && file_exists($CFG->dirroot.'/login/weblinkauth.php')) {
56 // Handles the case of another Moodle site linking into a page on this site
57 include($CFG->dirroot.'/login/weblinkauth.php');
0e8a3f25 58 if (function_exists(weblink_auth)) {
59 $user = weblink_auth($SESSION->wantsurl);
60 }
61 if ($user) {
62 $frm->username = $user->username;
63 } else {
5b2ae584 64 $frm = data_submitted($CFG->alternateloginurl);
0e8a3f25 65 }
d00377f5 66 } else {
5b2ae584 67 $frm = data_submitted($CFG->alternateloginurl);
d00377f5 68 }
a9b07c52 69
792197b0 70 if ($frm and (get_moodle_cookie() == '')) { // Login without cookie
71
72 $errormsg = get_string("cookiesnotenabled");
73
30b88cb7 74 } else if ($frm) { // Login WITH cookies
792197b0 75
1e22bc9c 76 $frm->username = trim(moodle_strtolower($frm->username));
cf5560fb 77
78 if (($frm->username == 'guest') and empty($CFG->guestloginbutton)) {
79 $user = false; /// Can't log in as guest if guest button is disabled
80 $frm = false;
0e8a3f25 81 } else if (!$user) {
cf5560fb 82 $user = authenticate_user_login($frm->username, $frm->password);
83 }
bee00f48 84 update_login_count();
f9903ed0 85
86 if ($user) {
87 if (! $user->confirmed ) { // they never confirmed via email
fa0626c6 88 print_header(get_string("mustconfirm"), get_string("mustconfirm") );
6bebcf8d 89 print_heading(get_string("mustconfirm"));
90 print_simple_box(get_string("emailconfirmsent", "", $user->email), "center");
8553b700 91 print_footer();
f9903ed0 92 die;
93 }
c21c671d 94
f9903ed0 95 $USER = $user;
dfa1fbac 96 if (!empty($USER->description)) {
1a33f699 97 $USER->description = true; // No need to cart all of it around
dfa1fbac 98 }
f9903ed0 99 $USER->loggedin = true;
1a33f699 100 $USER->site = $CFG->wwwroot; // for added security, store the site in the session
04280e85 101 sesskey(); // for added security, used to check script parameters
1a33f699 102
fcf8e836 103 if ($USER->username == "guest") {
801e1f0a 104 $USER->lang = $CFG->lang; // Guest language always same as site
105 $USER->firstname = get_string("guestuser"); // Name always in current language
4f3d1a63 106 $USER->lastname = " ";
910d1731 107 }
f9903ed0 108
f9903ed0 109 if (!update_user_login_times()) {
110 error("Wierd error: could not update login records");
111 }
8223d271 112
bee00f48 113 set_moodle_cookie($USER->username);
f9903ed0 114
28e30091 115 unset($SESSION->lang);
e1f96c23 116 unset($SESSION->encoding);
117
553201b5 118 $SESSION->justloggedin = true;
e1f96c23 119 $SESSION->encoding = get_string('thischarset');
28e30091 120
054193be 121 // Restore the calendar filters, if saved
122 if(intval(get_user_preferences('calendar_persistflt', 0))) {
123 include_once($CFG->dirroot.'/calendar/lib.php');
124 calendar_set_filters_status(get_user_preferences('calendar_savedflt', 0xff));
125 }
126
089b19f6 127 //Select password change url
128 if (is_internal_auth() || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){
129 $passwordchangeurl=$CFG->wwwroot.'/login/change_password.php';
130 } elseif($CFG->changepassword) {
131 $passwordchangeurl=$CFG->changepassword;
132 }
133
134
d35757eb 135 // check whether the user should be changing password
7692b114 136 if (get_user_preferences('auth_forcepasswordchange', false)){
089b19f6 137 if (isset($passwordchangeurl)) {
138 redirect($passwordchangeurl);
d35757eb 139 } else {
140 error("You cannot proceed without changing your password.
141 However there is no available page for changing it.
142 Please contact your Moodle Administrator.");
143 }
144 }
145
089b19f6 146
bcdcbccc 147 add_to_log(SITEID, "user", "login", "view.php?id=$user->id&course=".SITEID, $user->id, 0, $user->id);
e06f15ae 148
808a3baa 149 if (user_not_fully_set_up($USER)) {
089b19f6 150 $urltogo = $CFG->wwwroot.'/user/edit.php?id='.$USER->id.'&amp;course='.SITEID;
e06f15ae 151 // We don't delete $SESSION->wantsurl yet, so we get there later
808a3baa 152
e06f15ae 153 } else if (isset($SESSION->wantsurl) and (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
154 $urltogo = $SESSION->wantsurl; /// Because it's an address in this site
155 unset($SESSION->wantsurl);
808a3baa 156
bee00f48 157 } else {
089b19f6 158 $urltogo = $CFG->wwwroot.'/'; /// Go to the standard home page
e06f15ae 159 unset($SESSION->wantsurl); /// Just in case
bee00f48 160 }
089b19f6 161
162 // check if user password has expired
163 // Currently supported only for ldap-authentication module
164 if (isset($CFG->ldap_expiration) && $CFG->ldap_expiration == 1 ) {
165 if (function_exists('auth_password_expire')){
166 $days2expire = auth_password_expire($USER->username);
167 if (intval($days2expire) > 0 && intval($days2expire) < intval($CFG->{$USER->auth.'_expiration_warning'})) {
168 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
169 notice_yesno(get_string('auth_passwordwillexpire', 'auth', $days2expire), $passwordchangeurl, $urltogo);
170 print_footer();
171 exit;
172 } elseif (intval($days2expire) < 0 ) {
173 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
174 notice_yesno(get_string('auth_passwordisexpired', 'auth'), $passwordchangeurl, $urltogo);
175 print_footer();
176 exit;
177 }
178 }
179 }
180
bee00f48 181 reset_login_count();
f9903ed0 182
bcdcbccc 183 redirect($urltogo);
184
e06f15ae 185 exit;
f9903ed0 186
187 } else {
fa0626c6 188 $errormsg = get_string("invalidlogin");
f9903ed0 189 }
190 }
8223d271 191
f9903ed0 192
9c9f7d77 193 if (empty($errormsg)) {
194 $errormsg = "";
195 }
196
f9903ed0 197 if (empty($SESSION->wantsurl)) {
ed90bc47 198 $SESSION->wantsurl = array_key_exists('HTTP_REFERER',$_SERVER) ? $_SERVER["HTTP_REFERER"] : $CFG->wwwroot;
f9903ed0 199 }
792197b0 200
201 if (get_moodle_cookie() == '') {
202 set_moodle_cookie('nobody'); // To help search for cookies
203 }
f9903ed0 204
9c9f7d77 205 if (empty($frm->username)) {
792197b0 206 $frm->username = get_moodle_cookie() === 'nobody' ? '' : get_moodle_cookie();
9c9f7d77 207 $frm->password = "";
208 }
f9903ed0 209
9c9f7d77 210 if (!empty($frm->username)) {
a46b14ef 211 $focus = "login.password";
f9903ed0 212 } else {
a46b14ef 213 $focus = "login.username";
f9903ed0 214 }
35a48c9a 215
5f2c35db 216 if ($CFG->auth == "email" or $CFG->auth == "none" or chop($CFG->auth_instructions) <> "" ) {
35a48c9a 217 $show_instructions = true;
218 } else {
219 $show_instructions = false;
220 }
5b2ae584 221
222 if (!empty($CFG->alternateloginurl)) {
223 redirect($CFG->alternateloginurl);
224 exit;
225 }
f9903ed0 226
9a58f7cb 227 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
f9903ed0 228 include("index_form.html");
8553b700 229 print_footer();
f9903ed0 230
231 exit;
232
233 // No footer on this page
234
f9903ed0 235?>