Additions to authentication scheme. Logs to /tmp/sessionlog. Will move this
[koha.git] / logout.pl
1 #!/usr/bin/perl
2
3 use CGI;
4 use C4::Database;
5
6 my $query=new CGI;
7
8 my $sessionID=$query->cookie('sessionID');
9
10 my $sessions;
11 open (S, "/tmp/sessions");
12 while (my ($sid, $u, $lasttime) = split(/:/, <S>)) {
13     chomp $lasttime;
14     (next) unless ($sid);
15     (next) if ($sid eq $sessionID);
16     $sessions->{$sid}->{'userid'}=$u;
17     $sessions->{$sid}->{'lasttime'}=$lasttime;
18 }
19 open (S, ">/tmp/sessions");
20 foreach (keys %$sessions) {
21     my $userid=$sessions->{$_}->{'userid'};
22     my $lasttime=$sessions->{$_}->{'lasttime'};
23     print S "$_:$userid:$lasttime\n";
24 }
25
26 my $dbh=C4Connect;
27
28 # Check that this is the ip that created the session before deleting it
29
30 my $sth=$dbh->prepare("select userid,ip from sessions where sessionID=?");
31 $sth->execute($sessionID);
32 my ($userid, $ip);
33 if ($sth->rows) {
34     ($userid,$ip) = $sth->fetchrow;
35     if ($ip ne $ENV{'REMOTE_ADDR'}) {
36        # attempt to logout from a different ip than cookie was created at
37        exit;
38     }
39 }
40
41 $sth=$dbh->prepare("delete from sessions where sessionID=?");
42 $sth->execute($sessionID);
43 open L, ">>/tmp/sessionlog";
44 print L "$userid from $ip logged out at ".localtime(time())." (manual log out).\n";
45 close L;
46
47 my $cookie=$query->cookie(-name => 'sessionID',
48                           -value => '',
49                           -expires => '+1y');
50
51 print $query->redirect("shelves.pl");
52
53 exit;
54 if ($sessionID) {
55     print "Logged out of $sessionID<br>\n";
56     print "<a href=shelves.pl>Login</a>";
57 } else {
58     print "Not logged in.<br>\n";
59     print "<a href=shelves.pl>Login</a>";
60 }
61
62
63