Delete orphaned categories remaining in server. Bug 2140
[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;
fe657136 46 if ((!empty($SESSION->wantsurl) and strstr($SESSION->wantsurl,"username=guest")) or $loginguest) {
d00377f5 47 /// Log in as guest automatically (idea from Zbigniew Fiedorowicz)
48 $frm->username = "guest";
49 $frm->password = "guest";
50 } else {
51 $frm = data_submitted();
52 }
a9b07c52 53
d00377f5 54 if ($frm) {
1e22bc9c 55 $frm->username = trim(moodle_strtolower($frm->username));
cf5560fb 56
57 if (($frm->username == 'guest') and empty($CFG->guestloginbutton)) {
58 $user = false; /// Can't log in as guest if guest button is disabled
59 $frm = false;
60 } else {
61 $user = authenticate_user_login($frm->username, $frm->password);
62 }
bee00f48 63 update_login_count();
f9903ed0 64
65 if ($user) {
66 if (! $user->confirmed ) { // they never confirmed via email
fa0626c6 67 print_header(get_string("mustconfirm"), get_string("mustconfirm") );
6bebcf8d 68 print_heading(get_string("mustconfirm"));
69 print_simple_box(get_string("emailconfirmsent", "", $user->email), "center");
8553b700 70 print_footer();
f9903ed0 71 die;
72 }
c21c671d 73
f9903ed0 74 $USER = $user;
dfa1fbac 75 if (!empty($USER->description)) {
76 $USER->description = true; // No need to cart all of it around
77 }
f9903ed0 78 $USER->loggedin = true;
7228f796 79 $USER->site = $CFG->wwwroot; // for added security, store the site in the session
80 $USER->sesskey = random_string(10); // for added security, used to check script parameters
1924074c 81
fcf8e836 82 if ($USER->username == "guest") {
801e1f0a 83 $USER->lang = $CFG->lang; // Guest language always same as site
84 $USER->firstname = get_string("guestuser"); // Name always in current language
4f3d1a63 85 $USER->lastname = " ";
910d1731 86 }
f9903ed0 87
f9903ed0 88 if (!update_user_login_times()) {
89 error("Wierd error: could not update login records");
90 }
8223d271 91
bee00f48 92 set_moodle_cookie($USER->username);
f9903ed0 93
9698bf15 94 $wantsurl = $SESSION->wantsurl;
95
96 unset($SESSION->wantsurl);
28e30091 97 unset($SESSION->lang);
553201b5 98 $SESSION->justloggedin = true;
28e30091 99
089b19f6 100 //Select password change url
101 if (is_internal_auth() || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){
102 $passwordchangeurl=$CFG->wwwroot.'/login/change_password.php';
103 } elseif($CFG->changepassword) {
104 $passwordchangeurl=$CFG->changepassword;
105 }
106
107
d35757eb 108 // check whether the user should be changing password
7692b114 109 if (get_user_preferences('auth_forcepasswordchange', false)){
089b19f6 110 if (isset($passwordchangeurl)) {
111 redirect($passwordchangeurl);
d35757eb 112 } else {
113 error("You cannot proceed without changing your password.
114 However there is no available page for changing it.
115 Please contact your Moodle Administrator.");
116 }
117 }
118
089b19f6 119
bcdcbccc 120 add_to_log(SITEID, "user", "login", "view.php?id=$user->id&course=".SITEID, $user->id, 0, $user->id);
089b19f6 121
808a3baa 122 if (user_not_fully_set_up($USER)) {
089b19f6 123 $urltogo = $CFG->wwwroot.'/user/edit.php?id='.$USER->id.'&amp;course='.SITEID;
808a3baa 124
9698bf15 125 } else if (strpos($wantsurl, $CFG->wwwroot) === 0) { /// Matches site address
089b19f6 126 $urltogo = $wantsurl;
808a3baa 127
bee00f48 128 } else {
089b19f6 129 $urltogo = $CFG->wwwroot.'/'; /// Go to the standard home page
bee00f48 130 }
089b19f6 131
132 // check if user password has expired
133 // Currently supported only for ldap-authentication module
134 if (isset($CFG->ldap_expiration) && $CFG->ldap_expiration == 1 ) {
135 if (function_exists('auth_password_expire')){
136 $days2expire = auth_password_expire($USER->username);
137 if (intval($days2expire) > 0 && intval($days2expire) < intval($CFG->{$USER->auth.'_expiration_warning'})) {
138 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
139 notice_yesno(get_string('auth_passwordwillexpire', 'auth', $days2expire), $passwordchangeurl, $urltogo);
140 print_footer();
141 exit;
142 } elseif (intval($days2expire) < 0 ) {
143 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
144 notice_yesno(get_string('auth_passwordisexpired', 'auth'), $passwordchangeurl, $urltogo);
145 print_footer();
146 exit;
147 }
148 }
149 }
150
bee00f48 151 reset_login_count();
f9903ed0 152
bcdcbccc 153 redirect($urltogo);
154
f9903ed0 155 die;
156
157 } else {
fa0626c6 158 $errormsg = get_string("invalidlogin");
f9903ed0 159 }
160 }
8223d271 161
f9903ed0 162
9c9f7d77 163 if (empty($errormsg)) {
164 $errormsg = "";
165 }
166
f9903ed0 167 if (empty($SESSION->wantsurl)) {
ed90bc47 168 $SESSION->wantsurl = array_key_exists('HTTP_REFERER',$_SERVER) ? $_SERVER["HTTP_REFERER"] : $CFG->wwwroot;
f9903ed0 169 }
170
9c9f7d77 171 if (empty($frm->username)) {
f9903ed0 172 $frm->username = get_moodle_cookie();
9c9f7d77 173 $frm->password = "";
174 }
f9903ed0 175
9c9f7d77 176 if (!empty($frm->username)) {
a46b14ef 177 $focus = "login.password";
f9903ed0 178 } else {
a46b14ef 179 $focus = "login.username";
f9903ed0 180 }
35a48c9a 181
5f2c35db 182 if ($CFG->auth == "email" or $CFG->auth == "none" or chop($CFG->auth_instructions) <> "" ) {
35a48c9a 183 $show_instructions = true;
184 } else {
185 $show_instructions = false;
186 }
f9903ed0 187
9a58f7cb 188 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
f9903ed0 189 include("index_form.html");
8553b700 190 print_footer();
f9903ed0 191
192 exit;
193
194 // No footer on this page
195
f9903ed0 196?>