new upload functionalities

git-svn-id: http://yolanda.mister-muffin.de/svn@19 7eef14d0-6ed0-489d-bf55-20463b2d70db
This commit is contained in:
josch 2007-10-10 21:48:12 +00:00
parent 3016c3a294
commit a1bf6a20c2
7 changed files with 251 additions and 0 deletions

53
trunk/functions.pl Normal file
View file

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

14
trunk/include.pl Normal file
View file

@ -0,0 +1,14 @@
use CGI qw(:standard);
use CGI::Session;
use DBI;
use XML::Simple qw(:strict);
use Digest::SHA qw(sha256_hex);
#set global variables
$database = 'gnutube';
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$session_name = 'sid';
$cwd = $ENV{PWD};
1;

20
trunk/index.pl Normal file
View file

@ -0,0 +1,20 @@
require "/var/www/perl/include.pl";
require "/var/www/perl/functions.pl";
#create or resume session
CGI::Session->name($session_name);
my $session = new CGI::Session;
#read xml
$page = XMLin('/var/www/perl/index.xml', ForceArray => 1, KeyAttr => {} );
#if a username is associated with session id, username is nonempty
$page->{username} = get_username_from_sid($session->id);
fill_tagcloud;
#print xml http header along with session cookie
print $session->header(-type=>'text/xml');
#print xml
print XMLout($page, KeyAttr => {}, XMLDecl => '<?xml version="1.0" encoding="ISO-8859-1" ?><?xml-stylesheet type="text/xsl" href="./xsl/xhtml.xsl" ?>', RootName => 'page');

71
trunk/login.pl Normal file
View file

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

40
trunk/register.pl Normal file
View file

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

22
trunk/upload.pl Normal file
View file

@ -0,0 +1,22 @@
require "/var/www/perl/include.pl";
require "/var/www/perl/functions.pl";
#create or resume session
CGI::Session->name($session_name);
my $session = new CGI::Session;
my $username = get_username_from_sid($session->id);
if($username) {
print $session->header();
print '<form action="uploader.pl" method="post" enctype="multipart/form-data">
Upload: <input type="file" name="file">
<br><br>
Title: <input type="text" name="title">
<br><br>
<input type="submit" name="submit" value=" upload ">
</form>';
} else {
print $session->header();
print "nope...";
}

31
trunk/uploader.pl Normal file
View file

@ -0,0 +1,31 @@
require "/var/www/perl/include.pl";
require "/var/www/perl/functions.pl";
CGI::Session->name($session_name);
$query = CGI->new(\&hook);
$session = new CGI::Session;
sub hook {
#this is going to become an ajax progress bar
my ($filename, $buffer, $bytes_read, $data) = @_;
print sha256_hex($buffer);
#open(TEMP, ">>/var/www/perl/videos/temp.temp") or die "cannot open";
print "Read $bytes_read bytes of $filename\n";
#close TEMP;
}
my $username = get_username_from_sid($session->id);
if($username) {
my $filename = $query->param("file");
my $title = $query->param("title");
$upload_filehandle = $query->upload("file");
print $session->header();
while ( <$upload_filehandle> )
{
print;
}
} else {
print $session->header();
print "nope...";
}