connecting to db only once, fixed referer regex, added user preferences, information on empty results, correcting the spelling of my nick, fixed space bug with libxslt, sanitized settings interface, repaired results.xsl, videos and thumbnails now only have a max hight, videos get resized

git-svn-id: http://yolanda.mister-muffin.de/svn@201 7eef14d0-6ed0-489d-bf55-20463b2d70db
This commit is contained in:
josch 2007-12-17 12:38:02 +00:00
parent 666fa10be9
commit 52ba4dadb1
19 changed files with 187 additions and 104 deletions

View file

@ -1,2 +1,2 @@
Nils Dagsson Moskopp (erlehmann)
Johannes Schauer (Josch)
Johannes Schauer (josch)

View file

@ -7,6 +7,16 @@ $session = new CGI::Session;
@userinfo = get_userinfo_from_sid($session->id);
if($userinfo->{'username'})
{
if($query->param('show') eq 'settings' and $query->param('submit'))
{
$dbh->do(qq{update users set locale = ?, pagesize = ?, cortado = ? where id = ?}, undef, $query->param('locale'), $query->param('pagesize'), $query->param('cortado'), $userinfo->{'id'} ) or die $dbh->errstr;
}
}
@userinfo = get_userinfo_from_sid($session->id);
@page = get_page_array(@userinfo);
if($userinfo->{'username'})
@ -14,9 +24,9 @@ if($userinfo->{'username'})
if($query->param('show') eq 'settings')
{
$page->{'account'}->{'show'} = 'settings';
#results per page
#language
#cortado or plugin
$page->{'account'}->{'locale'} = $userinfo->{'locale'};
$page->{'account'}->{'pagesize'} = $userinfo->{'pagesize'};
$page->{'account'}->{'cortado'} = $userinfo->{'cortado'}
}
elsif($query->param('show') eq 'bookmarks')
{

View file

@ -130,19 +130,17 @@ while(1)
$thumbnailsec = int($duration/3 + .5);
#the width/height calculation could of course be much shorter but less readable then
$tnmaxwidth = 160;
$tnmaxheight = 120
$tnwidth = $tnmaxwidth;
$tnheight = int($tnwidth*($height/$width)/2 + .5)*2;
if($tnheight > $tnmaxheight)
{
$tnheight = $tnmaxheight;
$tnwidth = int($tnheight*($width/$height)/2 + .5)*2;
}
#all thumbs have equal height
$tnmaxheight = 120;
$tnheight = $tnmaxheight;
$tnwidth = int($tnheight*($width/$height)/2 + .5)*2;
system "ffmpeg -i $root/tmp/$id -vcodec mjpeg -vframes 1 -an -f rawvideo -ss $thumbnailsec -s ".$tnwidth."x$tnheight $root/video-stills/$id";
#check if the upload already is in the right format
if ($container eq 'ogg' and $video eq 'theora' and $audio eq 'vorbis')
$vmaxheight = 240;
#check if the upload already is in the right format and smaller/equal max-width/height
if ($container eq 'ogg' and $video eq 'theora' and $audio eq 'vorbis' and $height <= $vmaxheight)
{
appendlog $id, "file already is ogg-theora/vorbis";
@ -158,16 +156,20 @@ while(1)
}
else #encode video
{
#calculate video width
$vheight = $vmaxheight <= $height ? $vmaxheight : $height;
$vwidth = int($vheight*($width/$height)/2 + .5)*2;
#TODO: addmetadata information
system "ffmpeg2theora --optimize --videobitrate 1000 --audiobitrate 64 --sharpness 0 --output $root/videos/$id $root/tmp/$id 2>&1";
appendlog $id, $audio, $video, $width, $height, $fps, $duration, $sha;
system "ffmpeg2theora --optimize --videobitrate 1000 --audiobitrate 64 --sharpness 0 --width $vwidth --height $vheight --output $root/videos/$id $root/tmp/$id 2>&1";
appendlog $id, $audio, $video, $vwidth, $vheight, $fps, $duration, $sha;
#add video to videos table
$dbh->do(qq{insert into videos select id, title, description, userid, timestamp, creator,
subject, contributor, source, language, coverage, rights, license, notice,
derivativeworks, sharealike, commercialuse, ?, ?, ?, ?, ?, ?, 0, 0
from uploaded where id = ?}, undef, $filesize, $duration, $width,
$height, $fps, $sha, $id) or interrupt $dbh->errstr;
from uploaded where id = ?}, undef, $filesize, $duration, $vwidth,
$vheight, $fps, $sha, $id) or interrupt $dbh->errstr;
#delete temp file
unlink "$root/tmp/$id";

View file

@ -8,9 +8,6 @@ $session = new CGI::Session;
#do we have an id?
if($query->param('id'))
{
#connect to db
$dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass);
#check if video with requested id is in the database
my $sth = $dbh->prepare(qq{select title from videos where id = ? });
$sth->execute($query->param('id'));
@ -19,8 +16,7 @@ if($query->param('id'))
{
#if referer is not the local site update referer table
$referer = $query->referer() or $referer = '';
$server_name = $query->server_name();
if($referer !~ /^\w+:\/\/$server_name/)
if($referer !~ /^$domain/)
{
#check if already in database
$sth = $dbh->prepare(qq{select 1 from referer where videoid = ? and referer = ? }) or die $dbh->errstr;
@ -78,9 +74,6 @@ if($query->param('id'))
#print xml
print XMLout($page, KeyAttr => {}, XMLDecl => $XMLDecl, RootName => 'page');
}
#disconnect db
$dbh->disconnect();
}
else
{

View file

@ -5,9 +5,6 @@ sub get_userinfo_from_sid
#get parameters
my ($sid) = @_;
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass);
#prepare query
my $sth = $dbh->prepare(qq{select id, username, locale, pagesize, cortado from users where sid = ?}) or die $dbh->errstr;
@ -20,9 +17,6 @@ sub get_userinfo_from_sid
#finish query
$sth->finish() or die $dbh->errstr;
#close db
$dbh->disconnect() or die $dbh->errstr;
#return
return @userinfo;
}
@ -43,7 +37,7 @@ sub get_page_array
{
($page->{'locale'}) = $query->http('HTTP_ACCEPT_LANGUAGE') =~ /^([^,]+),.*$/;
}
$page->{stylesheet} = $stylesheet;
$page->{'stylesheet'} = $stylesheet;
$page->{'xmlns:dc'} = $xmlns_dc;
$page->{'xmlns:cc'} = $xmlns_cc;
$page->{'xmlns:rdf'} = $xmlns_rdf;
@ -54,16 +48,13 @@ sub get_page_array
# and account.pl (display own videos)
sub fill_results
{
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass);
#prepare query
my $sth = $dbh->prepare($dbquery) or die $dbh->errstr;
#execute it
$resultcount = $sth->execute(@_) or die $dbh->errstr;
$pagesize = $query->param('pagesize') or $pagesize = 5;
$pagesize = $query->param('pagesize') or $pagesize = $userinfo->{'pagesize'} or $pagesize = 5;
#rediculous but funny round up, will fail with 100000000000000 results per page
#on 0.0000000000001% of all queries - this is a risk we can handle
@ -84,6 +75,12 @@ sub fill_results
$page->{'results'}->{'resultcount'} = $resultcount eq '0E0' ? 0 : $resultcount;
$page->{'results'}->{'pagesize'} = $pagesize;
if($resultcount eq '0E0')
{
$page->{'message'}->{'type'} = "information";
$page->{'message'}->{'text'} = "information_no_results";
}
#get every returned value
while (my ($id, $title, $description, $publisher, $timestamp, $creator,
$subject, $contributor, $source, $language, $coverage, $rights,
@ -126,9 +123,6 @@ sub fill_results
#finish query
$sth->finish() or die $dbh->errstr;
#close db
$dbh->disconnect() or die $dbh->errstr;
}
#replace chars in url as said in this rfc: http://www.rfc-editor.org/rfc/rfc1738.txt

View file

@ -26,5 +26,5 @@ $stylesheet = "/style/default.css";
$xmlns_dc = "http://purl.org/dc/elements/1.1/";
$xmlns_cc = "http://web.resource.org/cc/";
$xmlns_rdf = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
$dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass);
1;

View file

@ -24,8 +24,6 @@ elsif($query->param('error'))
$page->{'message'}->{'text'} = $query->param('error');
}
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass);
#prepare query
my $sth = $dbh->prepare(qq{select text, count from tagcloud }) or die $dbh->errstr;
@ -51,4 +49,3 @@ print $session->header(-type=>'text/xml', -charset=>'UTF-8');
#print xml
print XMLout($page, KeyAttr => {}, XMLDecl => $XMLDecl, RootName => 'page', AttrIndent => '1');

View file

@ -2,8 +2,6 @@
CGI::Session->name($session_name);
my $session = new CGI::Session;
my $dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass) or die $dbh->errstr;
$dbh->do(qq{drop table config});
$dbh->do(qq{drop table users});
@ -149,7 +147,5 @@ $dbh->do(qq{create table
)
}) or die $dbh->errstr;
$dbh->disconnect() or die $dbh->errstr;
print $session->header();
print "initiated database";

View file

@ -54,7 +54,6 @@
<string id="error_insert_username">Please insert a username.</string>
<string id="error_insert_password">Please insert a password.</string>
<string id="error_repeat_password">Please repeat your password.</string>
<string id="information_no_results">There are no results satisfying your query.</string>
<string id=""></string>
<!-- information -->
@ -65,6 +64,7 @@
<string id="information_comment_created">Your comment has been submitted.</string>
<string id="information_registered">Your account has been created.</string>
<string id="information_uploaded">Your file has been uploaded.</string>
<string id="information_no_results">There are no results satisfying your query.</string>
<!-- warnings -->
@ -181,6 +181,9 @@
<string id="account_uploads">my uploads</string>
<string id="account_settings">edit my settings</string>
<string id="account_bookmarks">my bookmarks</string>
<string id="account_locale">locale</string>
<string id="account_pagesize">pagesize</string>
<string id="account_cortado">cortado</string>
</strings>

View file

@ -12,9 +12,6 @@ $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($query->param('action') eq "logout")
{
#if logout is requested
@ -155,9 +152,6 @@ if($query->param('action'))
print XMLout($page, KeyAttr => {}, XMLDecl => $XMLDecl, RootName => 'page', AttrIndent => '1');
}
#disconnect db
$dbh->disconnect();
}
#check if user is logged in
elsif($userinfo->{'username'})

View file

@ -25,16 +25,10 @@ elsif($query->param('user') and $query->param('pass') and $query->param('pass_re
{
if($query->param('pass') eq $query->param('pass_repeat'))
{
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass) or die $dbh->errstr;
#do query
$dbh->do(qq{insert into users (username, password, timestamp, locale) values ( ?, password( ? ), unix_timestamp(), ?)}, undef,
$query->param("user"), $query->param("pass"), $page->{'locale'}) or die $dbh->errstr;
#disconnect db
$dbh->disconnect() or die $dbh->errstr;
print $query->redirect("index.pl?information=information_registered");
}
else

View file

@ -12,6 +12,8 @@ $session = new CGI::Session;
#check if query is set
if($query->param('query') or $query->param('orderby'))
{
#TODO: clean up scriptname, argument, value only being there because of
#TODO: account.pl also calling fill_results() which will be changed
$page->{'search'} = [''];
$page->{'results'}->{'scriptname'} = 'search.pl';
$page->{'results'}->{'argument'} = 'query';

View file

@ -12,7 +12,6 @@
<str id="page_account_settings">/account.pl?show=settings</str>
<str id="page_account_uploads">/account.pl?show=uploads</str>
<str id="page_account_bookmarks">/account.pl?show=bookmarks</str>
<str id="page_bookmarks"></str>
<str id="page_login">/login.pl</str>
<str id="page_login-openid">/login.pl?action=openid</str>
<str id="page_logout">/login.pl?action=logout</str>

View file

@ -22,9 +22,6 @@ sub hook
if($userinfo->{'id'}&&$query->param("DC.Title") && $query->param("DC.Description") && $query->param("DC.Subject"))
{
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$host", $dbuser, $dbpass) or die $dbh->errstr;
#make new entry for video into the databse
#FIXME: contributor, rights
$dbh->do(qq{insert into uploaded (title, description, userid, timestamp,
@ -51,9 +48,6 @@ if($userinfo->{'id'}&&$query->param("DC.Title") && $query->param("DC.Description
$page->{'message'}->{'type'} = "information";
$page->{'message'}->{'text'} = "information_uploaded";
#disconnect db
$dbh->disconnect() or die $dbh->errstr;
}
else
{

View file

@ -20,9 +20,6 @@ if($query->url_param('action') eq 'bookmark' and $query->url_param('id'))
#check if id or title is passed
elsif($query->url_param('title') or $query->url_param('id'))
{
#connect to db
my $dbh = DBI->connect("DBI:mysql:$database:$dbhost", $dbuser, $dbpass) or die $dbh->errstr;
if($query->url_param('id'))
{
#if id is passed ignore title and check for the id
@ -103,8 +100,7 @@ elsif($query->url_param('title') or $query->url_param('id'))
#if referer is not the local site update referer table
$referer = $query->referer() or $referer = '';
$server_name = $query->server_name();
if($referer !~ /^\w+:\/\/$server_name/)
if($referer !~ /^$domain/)
{
#check if already in database
$sth = $dbh->prepare(qq{select 1 from referer where videoid = ? and referer = ? }) or die $dbh->errstr;
@ -123,11 +119,32 @@ elsif($query->url_param('title') or $query->url_param('id'))
}
}
if($query->param('cortado') eq 'true')
{
$cortado = 'true';
}
elsif($query->param('cortado') eq 'false')
{
$cortado = 'false'
}
elsif($userinfo->{'cortado'} = 1)
{
$cortado = 'true';
}
elsif($userinfo->{'cortado'} = 0)
{
$cortado = 'false';
}
else
{
$cortado = 'true';
}
#before code cleanup, this was a really obfuscated array/hash creation
push @{ $page->{'video'} },
{
'thumbnail' => "$domain/video-stills/$id",
'cortado' => $query->param('cortado') eq 'false' ? "false" : "true",
'cortado' => $cortado,
'filesize' => $filesize,
'duration' => $duration,
'width' => $width,
@ -202,9 +219,6 @@ elsif($query->url_param('title') or $query->url_param('id'))
fill_results(@args);
}
#close db
$dbh->disconnect() or die $dbh->errstr;
}
else
{

View file

@ -30,9 +30,7 @@
<xsl:when test="document(concat('../locale/', //@locale, '.xml'))">
<xsl:value-of select="//@locale" />
</xsl:when>
<xsl:otherwise>
en-us
</xsl:otherwise>
<xsl:otherwise>en-us</xsl:otherwise>
</xsl:choose>
</xsl:variable>
@ -147,7 +145,7 @@
<xsl:value-of select="$locale_strings[@id='separator']" />
<a>
<xsl:attribute name="href">
<xsl:value-of select="$site_strings[@id='page_bookmarks']" />
<xsl:value-of select="$site_strings[@id='page_account_bookmarks']" />
</xsl:attribute>
<xsl:value-of select="$locale_strings[@id='bookmarks']" />
</a>

View file

@ -54,45 +54,132 @@
<xsl:choose>
<xsl:when test="//account/@show='uploads'">
<h2>results</h2>
<h2><xsl:value-of select="$locale_strings[@id='account_uploads']" /></h2>
<xsl:call-template name="results"/>
</xsl:when>
<xsl:when test="//account/@show='settings'">
<h2>settings</h2>
<form>
<h2><xsl:value-of select="$locale_strings[@id='account_settings']" /></h2>
<form method="POST">
<xsl:attribute name="action">
<xsl:value-of select="$site_strings[@id='page_account_settings']" />
</xsl:attribute>
<input type="hidden" name="show" value="settings" />
<div>
locale
<h3><xsl:value-of select="$locale_strings[@id='account_locale']" /></h3>
<br />
<select name="DC.Language">
<option>English</option>
<option>German</option>
<select name="locale" size="2">
<option value="en-us">
<xsl:if test="//account/@locale='en-us'">
<xsl:attribute name="selected">
selected
</xsl:attribute>
</xsl:if>
<xsl:value-of select="$locale_strings[@id='language_en-us']" />
</option>
<option value="de-de">
<xsl:if test="//account/@locale='de-de'">
<xsl:attribute name="selected">
selected
</xsl:attribute>
</xsl:if>
<xsl:value-of select="$locale_strings[@id='language_de-de']" />
</option>
</select>
</div>
<div>
pagesize
<h3><xsl:value-of select="$locale_strings[@id='account_pagesize']" /></h3>
<br />
<select>
<option>1</option>
<option>2</option>
<option>5</option>
<option>10</option>
<option>20</option>
<option>50</option>
<option>100</option>
<select name="pagesize" size="7">
<option>
<xsl:if test="//account/@pagesize=1">
<xsl:attribute name="selected">
selected
</xsl:attribute>
</xsl:if>
1
</option>
<option>
<xsl:if test="//account/@pagesize=2">
<xsl:attribute name="selected">
selected
</xsl:attribute>
</xsl:if>
2
</option>
<option>
<xsl:if test="//account/@pagesize=5">
<xsl:attribute name="selected">
selected
</xsl:attribute>
</xsl:if>
5
</option>
<option>
<xsl:if test="//account/@pagesize=10">
<xsl:attribute name="selected">
selected
</xsl:attribute>
</xsl:if>
10
</option>
<option>
<xsl:if test="//account/@pagesize=20">
<xsl:attribute name="selected">
selected
</xsl:attribute>
</xsl:if>
20
</option>
<option>
<xsl:if test="//account/@pagesize=50">
<xsl:attribute name="selected">
selected
</xsl:attribute>
</xsl:if>
50
</option>
<option>
<xsl:if test="//account/@pagesize=100">
<xsl:attribute name="selected">
selected
</xsl:attribute>
</xsl:if>
100
</option>
</select>
</div>
<div>
<input type="radio" name="cortado" value="true" />cortado
<input type="radio" name="cortado" value="false" />video plugin
<h3><xsl:value-of select="$locale_strings[@id='account_cortado']" /></h3>
<input type="radio" name="cortado" value="1">
<xsl:if test="//account/@cortado=1">
<xsl:attribute name="checked">
checked
</xsl:attribute>
</xsl:if>
</input>
<xsl:value-of select="$locale_strings[@id='watch_cortadoapplet']" />
<br />
<input type="radio" name="cortado" value="0">
<xsl:if test="//account/@cortado=0">
<xsl:attribute name="checked">
checked
</xsl:attribute>
</xsl:if>
</input>
<xsl:value-of select="$locale_strings[@id='watch_browserplugin']" />
</div>
<div>
<input type="submit" />
<input name="submit" type="submit" />
</div>
</form>
</xsl:when>
<xsl:when test="//account/@show='bookmarks'">
<h2>bookmarks</h2>
<h2><xsl:value-of select="$locale_strings[@id='account_bookmarks']" /></h2>
<p>coming soon...</p>
</xsl:when>
<xsl:otherwise>
<p></p>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

View file

@ -47,6 +47,11 @@
</xsl:for-each>
</xsl:template>
<xsl:template name="results-heading">
<!--
this is deprecated - dont use it.
-->
<div>
<xsl:choose>
<xsl:when test="//results/@value!=''">
@ -142,6 +147,7 @@
</xsl:when>
</xsl:choose>
</div>
</xsl:template>
<xsl:template name="results">

View file

@ -90,10 +90,10 @@
<xsl:attribute name="href">
<xsl:choose>
<xsl:when test="//@embed='true'">
<xsl:value-of select="concat(//video/rdf:RDF/cc:Work/dc:identifier, 'embed=true')" />
<xsl:value-of select="concat(//video/rdf:RDF/cc:Work/dc:identifier, 'cortado=true&amp;embed=true')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="//video/rdf:RDF/cc:Work/dc:identifier" />
<xsl:value-of select="concat(//video/rdf:RDF/cc:Work/dc:identifier, 'cortado=true')" />
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>