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