Latest tags
[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
808a3baa 72 if (user_not_fully_set_up($USER)) {
839f2456 73 redirect("$CFG->wwwroot/user/edit.php?id=$USER->id&amp;course=".SITEID);
808a3baa 74
9698bf15 75 } else if (strpos($wantsurl, $CFG->wwwroot) === 0) { /// Matches site address
76 redirect($wantsurl);
808a3baa 77
bee00f48 78 } else {
9698bf15 79 redirect("$CFG->wwwroot/"); /// Go to the standard home page
bee00f48 80 }
f9903ed0 81
bee00f48 82 reset_login_count();
f9903ed0 83
f9903ed0 84 die;
85
86 } else {
fa0626c6 87 $errormsg = get_string("invalidlogin");
f9903ed0 88 }
89 }
8223d271 90
f9903ed0 91
9c9f7d77 92 if (empty($errormsg)) {
93 $errormsg = "";
94 }
95
f9903ed0 96 if (empty($SESSION->wantsurl)) {
ed90bc47 97 $SESSION->wantsurl = array_key_exists('HTTP_REFERER',$_SERVER) ? $_SERVER["HTTP_REFERER"] : $CFG->wwwroot;
f9903ed0 98 }
99
9c9f7d77 100 if (empty($frm->username)) {
f9903ed0 101 $frm->username = get_moodle_cookie();
9c9f7d77 102 $frm->password = "";
103 }
f9903ed0 104
9c9f7d77 105 if (!empty($frm->username)) {
a46b14ef 106 $focus = "login.password";
f9903ed0 107 } else {
a46b14ef 108 $focus = "login.username";
f9903ed0 109 }
35a48c9a 110
5f2c35db 111 if ($CFG->auth == "email" or $CFG->auth == "none" or chop($CFG->auth_instructions) <> "" ) {
35a48c9a 112 $show_instructions = true;
113 } else {
114 $show_instructions = false;
115 }
f9903ed0 116
35a48c9a 117 if (!$site = get_site()) {
118 error("No site found!");
119 }
2f05b46e 120
984a8bf3 121 if (empty($CFG->langmenu)) {
122 $langmenu = "";
123 } else {
124 $currlang = current_language();
125 $langs = get_list_of_languages();
8a33e371 126 if (empty($CFG->loginhttps)) {
127 $wwwroot = $CFG->wwwroot;
128 } else {
129 $wwwroot = str_replace('http','https',$CFG->wwwroot);
130 }
131 $langmenu = popup_form ("$wwwroot/login/index.php?lang=", $langs, "chooselang", $currlang, "", "", "", true);
984a8bf3 132 }
2f05b46e 133
fa0626c6 134 $loginsite = get_string("loginsite");
135
9a58f7cb 136 print_header("$site->fullname: $loginsite", "$site->fullname", $loginsite, $focus, "", true, "<div align=\"right\">$langmenu</div>");
f9903ed0 137 include("index_form.html");
8553b700 138 print_footer();
f9903ed0 139
140 exit;
141
142 // No footer on this page
143
f9903ed0 144?>