synch'ing head and rel_2_2 (from 2.2.5, including npl templates)
[koha.git] / opac / opac-logout.pl
1 #!/usr/bin/perl
2
3 use CGI;
4 use C4::Context;
5 use C4::Output;
6 use HTML::Template;
7
8 my $query=new CGI;
9
10 my $sessionID=$query->cookie('sessionID');
11
12
13 if ($ENV{'REMOTE_USER'}) {
14     print $query->header();
15     print startpage();
16     print startmenu('catalogue');
17     print qq|
18 <h1>Logout Feature Not Available</h1>
19 Your Koha server is configured to use a type of authentication called "Basic
20 Authentication" instead of using a cookies-based authentication system.  With
21 Basic Authentication, the only way to logout of Koha is by exiting your
22 browser.
23 |;
24     print endmenu('catalogue');
25     print endpage();
26     exit;
27 }
28
29 my $sessions;
30 open (S, "/tmp/sessions");
31         # FIXME - Come up with a better logging mechanism
32 while (my ($sid, $u, $lasttime) = split(/:/, <S>)) {
33     chomp $lasttime;
34     (next) unless ($sid);
35     (next) if ($sid eq $sessionID);
36     $sessions->{$sid}->{'userid'}=$u;
37     $sessions->{$sid}->{'lasttime'}=$lasttime;
38 }
39 open (S, ">/tmp/sessions");
40 foreach (keys %$sessions) {
41     my $userid=$sessions->{$_}->{'userid'};
42     my $lasttime=$sessions->{$_}->{'lasttime'};
43     print S "$_:$userid:$lasttime\n";
44 }
45
46 my $dbh = C4::Context->dbh;
47
48 # Check that this is the ip that created the session before deleting it
49
50 my $sth=$dbh->prepare("select userid,ip from sessions where sessionID=?");
51 $sth->execute($sessionID);
52 my ($userid, $ip);
53 if ($sth->rows) {
54     ($userid,$ip) = $sth->fetchrow;
55     if ($ip ne $ENV{'REMOTE_ADDR'}) {
56        # attempt to logout from a different ip than cookie was created at
57        exit;
58     }
59 }
60
61 my $sth=$dbh->prepare("delete from sessions where sessionID=?");
62 $sth->execute($sessionID);
63 open L, ">>/tmp/sessionlog";
64 my $time=localtime(time());
65 printf L "%20s from %16s logged out at %30s (manual log out).\n", $userid, $ip, $time;
66 close L;
67
68 my $cookie=$query->cookie(-name => 'sessionID',
69                           -value => '',
70                           -expires => '+1y');
71
72 # Should redirect to opac home page after logging out
73
74 print $query->redirect("/cgi-bin/koha/opac-main.pl");
75
76 exit;
77 if ($sessionID) {
78     print "Logged out of $sessionID<br>\n";
79     print "<a href=shelves.pl>Login</a>";
80 } else {
81     print "Not logged in.<br>\n";
82     print "<a href=shelves.pl>Login</a>";
83 }
84
85
86