commented everything

git-svn-id: http://yolanda.mister-muffin.de/svn@17 7eef14d0-6ed0-489d-bf55-20463b2d70db
This commit is contained in:
josch 2007-10-10 18:51:01 +00:00
parent 13f42715e7
commit fd87013377
5 changed files with 70 additions and 5 deletions

View file

@ -1,24 +1,53 @@
require "/var/www/perl/include.pl"; require "/var/www/perl/include.pl";
#get tags from database and fill $page with xml
sub fill_tagcloud { sub fill_tagcloud {
#connect to db
$dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass); $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass);
#prepare query
my $sth = $dbh->prepare(qq{select text, count from tagcloud }); my $sth = $dbh->prepare(qq{select text, count from tagcloud });
#execute it
$sth->execute(); $sth->execute();
#get every returned value
while (my ($text, $count) = $sth->fetchrow_array()) while (my ($text, $count) = $sth->fetchrow_array())
{ {
#push the new value to the $page->tagcloud array
push @{ $page->{tagcloud}->{tag} }, { text => [$text], count => [$count] }; push @{ $page->{tagcloud}->{tag} }, { text => [$text], count => [$count] };
} }
#finish query
$sth->finish(); $sth->finish();
#close db
$dbh->disconnect(); $dbh->disconnect();
} }
#return a username from passed session id
sub get_username_from_sid { sub get_username_from_sid {
#get parameters
my ($sid) = @_; my ($sid) = @_;
#connect to db
$dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass); $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass);
#prepare query
my $sth = $dbh->prepare(qq{select username from users where sid = '$sid'}); my $sth = $dbh->prepare(qq{select username from users where sid = '$sid'});
#execute it
$sth->execute(); $sth->execute();
#save the resulting username
my ($username) = $sth->fetchrow_array(); my ($username) = $sth->fetchrow_array();
#finish query
$sth->finish(); $sth->finish();
#close db
$dbh->disconnect(); $dbh->disconnect();
#return username
return $username; return $username;
} }

View file

@ -3,6 +3,7 @@ use CGI::Session;
use DBI; use DBI;
use XML::Simple qw(:strict); use XML::Simple qw(:strict);
#set global variables
$database = 'gnutube'; $database = 'gnutube';
$dbhost = 'localhost'; $dbhost = 'localhost';
$dbuser = 'root'; $dbuser = 'root';

View file

@ -8,7 +8,7 @@ my $session = new CGI::Session;
#read xml #read xml
$page = XMLin('/var/www/perl/index.xml', ForceArray => 1, KeyAttr => {} ); $page = XMLin('/var/www/perl/index.xml', ForceArray => 1, KeyAttr => {} );
#fill tags #if a username is associated with session id, username is nonempty
$page->{username} = get_username_from_sid($session->id); $page->{username} = get_username_from_sid($session->id);
fill_tagcloud; fill_tagcloud;

View file

@ -1,34 +1,50 @@
require "/var/www/perl/include.pl"; require "/var/www/perl/include.pl";
#initialize session data
CGI::Session->name($session_name); CGI::Session->name($session_name);
$query = new CGI; $query = new CGI;
$session = new CGI::Session; $session = new CGI::Session;
#check if action is set
if($query->param('action')) { if($query->param('action')) {
#connect to db
$dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass); $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass);
#if login is requested
if($query->param('action') eq "login") { if($query->param('action') eq "login") {
#save POST data in local variables
my $user = $query->param('user'); my $user = $query->param('user');
my $pass = $query->param('pass'); my $pass = $query->param('pass');
#prepare query
my $sth = $dbh->prepare(qq{select username from users my $sth = $dbh->prepare(qq{select username from users
where password = password('$pass') where password = password('$pass')
and username = '$user' and username = '$user'
limit 1 }); limit 1 });
#execute query
$sth->execute(); $sth->execute();
#if something was returned username and password match
if($sth->fetchrow_array()) { if($sth->fetchrow_array()) {
#store session id in local variable
my $sid = $session->id; my $sid = $session->id;
#store session id in database
$sth = $dbh->prepare(qq{update users set sid = '$sid' where username = '$user'}); $sth = $dbh->prepare(qq{update users set sid = '$sid' where username = '$user'});
$sth->execute(); $sth->execute();
$sth->finish(); $sth->finish();
print $session->header(); print $session->header();
print "logged in"; print "logged in";
} else { } else {
#if not, print error
print $session->header(); print $session->header();
print "could not log you in"; print "could not log you in";
} }
} elsif($query->param('action') eq "logout") { } elsif($query->param('action') eq "logout") {
#if logout is requested
#remove sid from database
$sth = $dbh->prepare(qq{update users set sid = '' where username = '$user'}); $sth = $dbh->prepare(qq{update users set sid = '' where username = '$user'});
$sth->execute(); $sth->execute();
$sth->finish(); $sth->finish();
@ -36,12 +52,15 @@ if($query->param('action')) {
print $session->header(); print $session->header();
print "logged out"; print "logged out";
} else { } else {
#something ugly was passed
print $session->header(); print $session->header();
print "wtf?"; print "wtf?";
} }
#disconnect db
$dbh->disconnect(); $dbh->disconnect();
} else { } else {
#print login form
print $session->header(); print $session->header();
print '<form action="" method="POST"><p> print '<form action="" method="POST"><p>
<input name="action" type="hidden" value="login"> <input name="action" type="hidden" value="login">

View file

@ -1,20 +1,36 @@
require "/var/www/perl/include.pl"; require "/var/www/perl/include.pl";
#initialize session data
CGI::Session->name($session_name); CGI::Session->name($session_name);
$query = new CGI; $query = new CGI;
$session = new CGI::Session; $session = new CGI::Session;
#if username and password are passed put them into the database
if($query->param('user') and $query->param('pass')) { if($query->param('user') and $query->param('pass')) {
#connect to db
$dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass); $dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass);
#save POST data in local variables
my $user = $query->param("user"); my $user = $query->param("user");
my $pass = $query->param("pass"); my $pass = $query->param("pass");
#prepare query
$sth = $dbh->prepare(qq{insert into users (username, password) values ('$user', password('$pass'))}); $sth = $dbh->prepare(qq{insert into users (username, password) values ('$user', password('$pass'))});
#execute query
$sth->execute(); $sth->execute();
#finish query
$sth->finish(); $sth->finish();
#disconnect db
$dbh->disconnect(); $dbh->disconnect();
#print a little confirmation
print $session->header(); print $session->header();
print "done" . $query->param('pass'); print "done";
} else { } else {
#if not, print register form
print $session->header(); print $session->header();
print '<form action="" method="POST"><p> print '<form action="" method="POST"><p>
<input name="user" type="text" size="30" maxlength="30"> <input name="user" type="text" size="30" maxlength="30">