2007-10-11 16:25:56 +00:00
|
|
|
#!/usr/bin/perl
|
2007-10-11 17:06:08 +00:00
|
|
|
require "include.pl";
|
2007-10-12 00:44:24 +00:00
|
|
|
require "functions.pl";
|
2007-10-10 21:48:12 +00:00
|
|
|
|
|
|
|
#initialize session data
|
|
|
|
CGI::Session->name($session_name);
|
|
|
|
$query = new CGI;
|
|
|
|
$session = new CGI::Session;
|
|
|
|
|
|
|
|
#check if action is set
|
2007-10-11 17:26:39 +00:00
|
|
|
if($query->param('action'))
|
|
|
|
{
|
2007-10-10 21:48:12 +00:00
|
|
|
#connect to db
|
|
|
|
$dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass);
|
|
|
|
|
|
|
|
#if login is requested
|
2007-10-11 17:26:39 +00:00
|
|
|
if($query->param('action') eq "login")
|
|
|
|
{
|
2007-10-10 21:48:12 +00:00
|
|
|
#prepare query
|
|
|
|
my $sth = $dbh->prepare(qq{select username from users
|
2007-10-12 00:34:32 +00:00
|
|
|
where password = password( ? )
|
|
|
|
and username = ?
|
2007-10-10 21:48:12 +00:00
|
|
|
limit 1 });
|
|
|
|
|
|
|
|
#execute query
|
2007-10-12 00:34:32 +00:00
|
|
|
$sth->execute($query->param('pass'), $query->param('user'));
|
2007-10-10 21:48:12 +00:00
|
|
|
|
|
|
|
#if something was returned username and password match
|
2007-10-11 17:26:39 +00:00
|
|
|
if($sth->fetchrow_array())
|
|
|
|
{
|
2007-10-10 21:48:12 +00:00
|
|
|
#store session id in database
|
2007-10-12 00:34:32 +00:00
|
|
|
$sth = $dbh->prepare(qq{update users set sid = ? where username = ? });
|
|
|
|
$sth->execute($session->id, $query->param('user'));
|
2007-10-10 21:48:12 +00:00
|
|
|
$sth->finish();
|
|
|
|
print $session->header();
|
|
|
|
print "logged in";
|
2007-10-11 17:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-10 21:48:12 +00:00
|
|
|
#if not, print error
|
|
|
|
print $session->header();
|
|
|
|
print "could not log you in";
|
|
|
|
}
|
|
|
|
|
2007-10-11 17:26:39 +00:00
|
|
|
}
|
|
|
|
elsif($query->param('action') eq "logout")
|
|
|
|
{
|
2007-10-10 21:48:12 +00:00
|
|
|
#if logout is requested
|
|
|
|
#remove sid from database
|
2007-10-12 00:34:32 +00:00
|
|
|
$sth = $dbh->prepare(qq{update users set sid = '' where username = ?});
|
|
|
|
$sth->execute(get_username_from_sid($session->id));
|
2007-10-10 21:48:12 +00:00
|
|
|
$sth->finish();
|
|
|
|
$session->delete();
|
|
|
|
print $session->header();
|
|
|
|
print "logged out";
|
2007-10-11 17:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-10 21:48:12 +00:00
|
|
|
#something ugly was passed
|
|
|
|
print $session->header();
|
|
|
|
print "wtf?";
|
|
|
|
}
|
|
|
|
|
|
|
|
#disconnect db
|
|
|
|
$dbh->disconnect();
|
2007-10-11 17:26:39 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-12 00:44:24 +00:00
|
|
|
#if not, print login form
|
|
|
|
|
|
|
|
$page = XMLin("$gnutube_root/login.xml", ForceArray => 1, KeyAttr => {} );
|
|
|
|
|
|
|
|
#if a username is associated with session id, username is nonempty
|
|
|
|
$page->{username} = get_username_from_sid($session->id);
|
|
|
|
|
|
|
|
#print xml http header along with session cookie
|
|
|
|
print $session->header(-type=>'text/xml');
|
|
|
|
|
|
|
|
print XMLout($page, KeyAttr => {}, XMLDecl => $XMLDecl, RootName => 'page');
|
2007-10-10 21:48:12 +00:00
|
|
|
}
|