Initial revision
[moodle.git] / login / index.php
CommitLineData
f9903ed0 1<?PHP // $Id$
2 require("../config.php");
3
4
5 if (match_referer() && isset($HTTP_POST_VARS)) { // form submitted
6
7 $frm = (object)$HTTP_POST_VARS;
8 $user = verify_login($frm->username, $frm->password);
9
10 update_login_count();
11
12 if ($user) {
13 if (! $user->confirmed ) { // they never confirmed via email
14 print_header("Need to confirm", "Not confirmed yet", "", "");
15 include("index_confirm.html");
16 die;
17 }
18
19 $USER = $user;
20 $USER->loggedin = true;
21
22 if (!update_user_in_db()) {
23 error("Weird error: User not found");
24 }
25
26 if (!update_user_login_times()) {
27 error("Wierd error: could not update login records");
28 }
29
30 set_moodle_cookie($USER->username);
31
32
33 if (empty($SESSION->wantsurl)) {
34 header("Location: $CFG->wwwroot");
35 } else {
36 header("Location: $SESSION->wantsurl");
37 unset($SESSION->wantsurl);
38 }
39
40 reset_login_count();
41 add_to_log("Logged in");
42
43 if ($CFG->smsnotify) {
44 $time = date("H:i D j M", time());
45 $smstring = "$time - $USER->firstname $USER->lastname logged in to $CFG->sitename";
46 system("echo \"$smstring \" | /opt/bin/sendsms &> /dev/null &");
47 }
48
49 die;
50
51 } else {
52 $errormsg = "Invalid login, please try again";
53 }
54 }
55
56 if (empty($SESSION->wantsurl)) {
57 $SESSION->wantsurl = $HTTP_REFERER;
58 }
59
60 if (!$frm->username)
61 $frm->username = get_moodle_cookie();
62
63 if ($frm->username) {
64 $focus = "form.password";
65 } else {
66 $focus = "form.username";
67 }
68
69 print_header("Login to the site", "Login to the site", "Login", $focus);
70
71 include("index_form.html");
72
73 exit;
74
75 // No footer on this page
76
77function update_user_login_times() {
78 global $db, $USER;
79
80 $USER->lastlogin = $USER->currentlogin;
81 $USER->currentlogin = time();
82
83 return $db->Execute("UPDATE user
84 SET lastlogin='$USER->lastlogin', currentlogin='$USER->currentlogin'
85 WHERE id = '$USER->id'");
86}
87?>