polished.
[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 }
23
d00377f5 24 $frm = false;
fe657136 25 if ((!empty($SESSION->wantsurl) and strstr($SESSION->wantsurl,"username=guest")) or $loginguest) {
d00377f5 26 /// Log in as guest automatically (idea from Zbigniew Fiedorowicz)
27 $frm->username = "guest";
28 $frm->password = "guest";
29 } else {
30 $frm = data_submitted();
31 }
a9b07c52 32
d00377f5 33 if ($frm) {
1e22bc9c 34 $frm->username = trim(moodle_strtolower($frm->username));
faebaf0f 35 $user = authenticate_user_login($frm->username, $frm->password);
bee00f48 36 update_login_count();
f9903ed0 37
38 if ($user) {
39 if (! $user->confirmed ) { // they never confirmed via email
fa0626c6 40 print_header(get_string("mustconfirm"), get_string("mustconfirm") );
6bebcf8d 41 print_heading(get_string("mustconfirm"));
42 print_simple_box(get_string("emailconfirmsent", "", $user->email), "center");
8553b700 43 print_footer();
f9903ed0 44 die;
45 }
c21c671d 46
f9903ed0 47 $USER = $user;
dfa1fbac 48 if (!empty($USER->description)) {
49 $USER->description = true; // No need to cart all of it around
50 }
f9903ed0 51 $USER->loggedin = true;
c21c671d 52 $USER->site = $CFG->wwwroot; // for added security
1924074c 53
fcf8e836 54 if ($USER->username == "guest") {
801e1f0a 55 $USER->lang = $CFG->lang; // Guest language always same as site
56 $USER->firstname = get_string("guestuser"); // Name always in current language
4f3d1a63 57 $USER->lastname = " ";
910d1731 58 }
f9903ed0 59
f9903ed0 60 if (!update_user_login_times()) {
61 error("Wierd error: could not update login records");
62 }
8223d271 63
bee00f48 64 set_moodle_cookie($USER->username);
f9903ed0 65
9698bf15 66 $wantsurl = $SESSION->wantsurl;
67
68 unset($SESSION->wantsurl);
28e30091 69 unset($SESSION->lang);
553201b5 70 $SESSION->justloggedin = true;
28e30091 71
d35757eb 72 // check whether the user should be changing password
73 reload_user_preferences();
74 if ($USER->preference['auth_forcepasswordchange']){
75 if (is_internal_auth() || $CFG->{'auth_'.$USER->auth.'_stdchangepassword'}){
76 redirect("$CFG->wwwroot/login/change_password.php");
77 } elseif($CFG->changepassword) {
78 redirect($CFG->changepassword);
79 } else {
80 error("You cannot proceed without changing your password.
81 However there is no available page for changing it.
82 Please contact your Moodle Administrator.");
83 }
84 }
85
808a3baa 86 if (user_not_fully_set_up($USER)) {
839f2456 87 redirect("$CFG->wwwroot/user/edit.php?id=$USER->id&amp;course=".SITEID);
808a3baa 88
9698bf15 89 } else if (strpos($wantsurl, $CFG->wwwroot) === 0) { /// Matches site address
90 redirect($wantsurl);
808a3baa 91
bee00f48 92 } else {
9698bf15 93 redirect("$CFG->wwwroot/"); /// Go to the standard home page
bee00f48 94 }
f9903ed0 95
bee00f48 96 reset_login_count();
f9903ed0 97
f9903ed0 98 die;
99
100 } else {
fa0626c6 101 $errormsg = get_string("invalidlogin");
f9903ed0 102 }
103 }
8223d271 104
f9903ed0 105
9c9f7d77 106 if (empty($errormsg)) {
107 $errormsg = "";
108 }
109
f9903ed0 110 if (empty($SESSION->wantsurl)) {
ed90bc47 111 $SESSION->wantsurl = array_key_exists('HTTP_REFERER',$_SERVER) ? $_SERVER["HTTP_REFERER"] : $CFG->wwwroot;
f9903ed0 112 }
113
9c9f7d77 114 if (empty($frm->username)) {
f9903ed0 115 $frm->username = get_moodle_cookie();
9c9f7d77 116 $frm->password = "";
117 }
f9903ed0 118
9c9f7d77 119 if (!empty($frm->username)) {
a46b14ef 120 $focus = "login.password";
f9903ed0 121 } else {
a46b14ef 122 $focus = "login.username";
f9903ed0 123 }
35a48c9a 124
5f2c35db 125 if ($CFG->auth == "email" or $CFG->auth == "none" or chop($CFG->auth_instructions) <> "" ) {
35a48c9a 126 $show_instructions = true;
127 } else {
128 $show_instructions = false;
129 }
f9903ed0 130
35a48c9a 131 if (!$site = get_site()) {
132 error("No site found!");
133 }
2f05b46e 134
984a8bf3 135 if (empty($CFG->langmenu)) {
136 $langmenu = "";
137 } else {
138 $currlang = current_language();
139 $langs = get_list_of_languages();
8a33e371 140 if (empty($CFG->loginhttps)) {
141 $wwwroot = $CFG->wwwroot;
142 } else {
143 $wwwroot = str_replace('http','https',$CFG->wwwroot);
144 }
145 $langmenu = popup_form ("$wwwroot/login/index.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
984a8bf3 146 }
2f05b46e 147
fa0626c6 148 $loginsite = get_string("loginsite");
149
9a58f7cb 150 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
f9903ed0 151 include("index_form.html");
8553b700 152 print_footer();
f9903ed0 153
154 exit;
155
156 // No footer on this page
157
f9903ed0 158?>