2007-10-11 17:06:08 +00:00
|
|
|
require "include.pl";
|
2007-10-10 21:48:12 +00:00
|
|
|
|
2007-10-29 15:00:40 +00:00
|
|
|
sub get_userinfo_from_sid
|
2007-10-11 17:26:39 +00:00
|
|
|
{
|
2008-02-14 22:15:38 +00:00
|
|
|
#get parameters
|
|
|
|
my ($sid) = @_;
|
|
|
|
|
|
|
|
#prepare query
|
2008-04-23 07:08:14 +00:00
|
|
|
my $sth = $dbh->prepare(qq{select id, username, pagesize from users where sid = ?}) or die $dbh->errstr;
|
2008-02-14 22:15:38 +00:00
|
|
|
|
|
|
|
#execute it
|
|
|
|
$sth->execute($sid) or die $dbh->errstr;
|
|
|
|
|
|
|
|
#save the resulting username
|
2008-04-23 07:08:14 +00:00
|
|
|
($userinfo->{'id'}, $userinfo->{'username'}, $userinfo->{'pagesize'}) = $sth->fetchrow_array();
|
2008-02-14 22:15:38 +00:00
|
|
|
|
|
|
|
#finish query
|
|
|
|
$sth->finish() or die $dbh->errstr;
|
|
|
|
|
|
|
|
#return
|
|
|
|
return @userinfo;
|
2007-10-10 21:48:12 +00:00
|
|
|
}
|
2007-10-11 10:57:52 +00:00
|
|
|
|
2007-10-29 15:00:40 +00:00
|
|
|
sub get_page_array
|
2007-10-11 17:26:39 +00:00
|
|
|
{
|
2008-02-14 22:15:38 +00:00
|
|
|
#get parameters
|
|
|
|
my (@userinfo) = @_;
|
2008-04-27 00:42:15 +00:00
|
|
|
|
2008-04-27 10:39:21 +00:00
|
|
|
my $page = XML::LibXML::Element->new( "page" );
|
2008-02-14 22:15:38 +00:00
|
|
|
|
2008-04-29 07:02:07 +00:00
|
|
|
#get user language from browser http_accept string
|
2008-04-27 16:53:38 +00:00
|
|
|
my ($lang) = $query->http('HTTP_ACCEPT_LANGUAGE') =~ /^[^,]+,([^;]*);/;
|
|
|
|
$page->setAttribute( "lang", $lang ? $lang : "en" );
|
2008-02-14 22:15:38 +00:00
|
|
|
|
2008-04-29 07:02:07 +00:00
|
|
|
#TODO: set namespace for each site on its own
|
2008-04-27 10:39:21 +00:00
|
|
|
$page->setAttribute( "username", $userinfo->{'username'} );
|
2008-04-27 00:42:15 +00:00
|
|
|
|
2008-04-27 10:39:21 +00:00
|
|
|
return $page;
|
2007-10-11 10:57:52 +00:00
|
|
|
}
|
2007-10-29 16:47:16 +00:00
|
|
|
|
2008-04-16 21:07:07 +00:00
|
|
|
# index.pl (display custom search)
|
2007-11-26 14:25:31 +00:00
|
|
|
# search.pl (display search results)
|
2007-12-18 12:39:27 +00:00
|
|
|
# and upload.pl (display similar videos)
|
2007-10-29 16:47:16 +00:00
|
|
|
sub fill_results
|
|
|
|
{
|
2008-04-16 21:07:07 +00:00
|
|
|
my ($dbquery, @args) = @_;
|
2008-04-27 00:42:15 +00:00
|
|
|
|
|
|
|
my $results = XML::LibXML::Element->new( "results" );
|
|
|
|
|
2008-02-14 22:15:38 +00:00
|
|
|
#prepare query
|
|
|
|
my $sth = $dbh->prepare($dbquery) or die $dbh->errstr;
|
|
|
|
|
|
|
|
#execute it
|
2008-04-16 21:07:07 +00:00
|
|
|
$resultcount = $sth->execute(@args) or die $dbh->errstr;
|
2008-02-14 22:15:38 +00:00
|
|
|
|
|
|
|
#set pagesize by query or usersettings or default
|
2008-04-27 08:40:41 +00:00
|
|
|
$pagesize = $query->param('pagesize') or $pagesize = $userinfo->{'pagesize'} or $pagesize = $config->{"search_results_default"};
|
2008-02-14 22:15:38 +00:00
|
|
|
|
|
|
|
#if pagesize is more than maxpagesize reduce to maxpagesize
|
2008-04-27 08:40:41 +00:00
|
|
|
$pagesize = $pagesize > $config->{"search_results_max"} ? $config->{"search_results_max"} : $pagesize;
|
2008-02-14 22:15:38 +00:00
|
|
|
|
|
|
|
#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
|
|
|
|
$lastpage = int($resultcount/$pagesize+0.99999999999999);
|
|
|
|
|
2008-04-27 20:12:14 +00:00
|
|
|
$currentpage = int($query->param('page')) or $currentpage = 1;
|
2008-02-14 22:15:38 +00:00
|
|
|
|
|
|
|
$dbquery .= " limit ".($currentpage-1)*$pagesize.", ".$pagesize;
|
|
|
|
|
|
|
|
#prepare query
|
|
|
|
$sth = $dbh->prepare($dbquery) or die $dbh->errstr;
|
|
|
|
|
|
|
|
#execute it
|
2008-04-27 08:40:41 +00:00
|
|
|
$sth->execute(@args) or die $dbh->errstr;
|
2008-02-14 22:15:38 +00:00
|
|
|
|
2008-04-27 00:42:15 +00:00
|
|
|
$results->setAttribute('lastpage', $lastpage);
|
|
|
|
$results->setAttribute('currentpage', $currentpage);
|
|
|
|
$results->setAttribute('resultcount', $resultcount eq '0E0' ? 0 : $resultcount);
|
|
|
|
$results->setAttribute('pagesize', $pagesize);
|
2008-02-14 22:15:38 +00:00
|
|
|
|
|
|
|
#get every returned value
|
|
|
|
while (my ($id, $title, $description, $publisher, $timestamp, $creator,
|
|
|
|
$subject, $source, $language, $coverage, $rights,
|
|
|
|
$license, $filesize, $duration, $width, $height, $fps, $viewcount,
|
|
|
|
$downloadcount) = $sth->fetchrow_array())
|
|
|
|
{
|
2008-04-29 07:02:07 +00:00
|
|
|
#build xml node structure
|
2008-04-27 00:42:15 +00:00
|
|
|
my $result = XML::LibXML::Element->new( "result" );
|
|
|
|
$result->setAttribute( "thumbnail", $config->{"url_root"}."/video-stills/thumbnails/$id" );
|
|
|
|
$result->setAttribute( "preview", $config->{"url_root"}."/video-stills/previews/$id" );
|
|
|
|
$result->setAttribute( "duration", $duration );
|
|
|
|
$result->setAttribute( "viewcount", $viewcount );
|
|
|
|
|
|
|
|
my $rdf = XML::LibXML::Element->new( "RDF" );
|
|
|
|
$rdf->setNamespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf");
|
|
|
|
|
|
|
|
my $work = XML::LibXML::Element->new( "Work" );
|
|
|
|
$work->setNamespace( "http://web.resource.org/cc/", "cc");
|
|
|
|
$work->setNamespace( "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf", 0);
|
|
|
|
$work->setAttributeNS( "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "about", $config->{"url_root"}."/download/$id/" );
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "coverage" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($coverage);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "creator" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($creator);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "date" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($date);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "description" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($description);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "identifier" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($config->{"url_root"}."/video/".urlencode($title)."/$id/");
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "language" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($language);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "publisher" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($publisher);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "rights" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($rights);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "source" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($source);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "subject" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($subjcet);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
$node = XML::LibXML::Element->new( "title" );
|
|
|
|
$node->setNamespace( "http://purl.org/dc/elements/1.1/", "dc" );
|
|
|
|
$node->appendText($title);
|
|
|
|
$work->appendChild($node);
|
|
|
|
|
|
|
|
my $license = XML::LibXML::Element->new( "License" );
|
|
|
|
$license->setNamespace("http://web.resource.org/cc/", "cc");
|
|
|
|
$license->setNamespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf", 0);
|
|
|
|
$license->setAttributeNS( "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "about", "http://creativecommons.org/licenses/GPL/2.0/" );
|
|
|
|
|
|
|
|
$rdf->appendChild($work);
|
|
|
|
$rdf->appendChild($license);
|
|
|
|
|
|
|
|
$result->appendChild($rdf);
|
|
|
|
|
|
|
|
$results->appendChild($result);
|
2008-02-14 22:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#finish query
|
|
|
|
$sth->finish() or die $dbh->errstr;
|
2008-04-27 00:42:15 +00:00
|
|
|
|
|
|
|
return $results;
|
2007-10-29 16:47:16 +00:00
|
|
|
}
|
2007-12-01 18:04:28 +00:00
|
|
|
|
2008-04-16 21:07:07 +00:00
|
|
|
sub get_sqlquery
|
|
|
|
{
|
|
|
|
my $strquery = @_[0];
|
|
|
|
$strquery =~ s/%([0-9A-F]{2})/chr(hex($1))/eg;
|
|
|
|
(@tags) = $strquery =~ / tag:(\w+)/gi;
|
2008-04-27 08:40:41 +00:00
|
|
|
($order) = $strquery =~ / order:(\w+)/i;
|
2008-04-16 21:07:07 +00:00
|
|
|
($sort) = $strquery =~ / sort:(\w+)/i;
|
2008-04-29 07:02:07 +00:00
|
|
|
#TODO: add those options
|
2008-04-16 21:07:07 +00:00
|
|
|
#($title) = $strquery =~ /title:(\w+)/i;
|
|
|
|
#($description) = $strquery =~ /description:(\w+)/i;
|
|
|
|
#($creator) = $strquery =~ /creator:(\w+)/i;
|
|
|
|
#($language) = $strquery =~ /language:(\w+)/i;
|
|
|
|
#($coverage) = $strquery =~ /coverage:(\w+)/i;
|
|
|
|
#($rights) = $strquery =~ /rights:(\w+)/i;
|
|
|
|
#($license) = $strquery =~ /license:(\w+)/i;
|
|
|
|
#($filesize) = $strquery =~ /filesize:([<>]?\w+)/i;
|
|
|
|
#($duration) = $strquery =~ /duration:([<>]?\w+)/i;
|
|
|
|
#($timestamp) = $strquery =~ /timestamp:([<>]?\w+)/i;
|
2008-04-27 08:40:41 +00:00
|
|
|
$strquery =~ s/ (tag|order|sort):\w+//gi;
|
2008-04-16 21:07:07 +00:00
|
|
|
$strquery =~ s/^\s*(.*?)\s*$/$1/;
|
|
|
|
|
|
|
|
#build mysql query
|
|
|
|
my $dbquery = "select v.id, v.title, v.description, u.username,
|
|
|
|
from_unixtime( v.timestamp ), v.creator, v.subject,
|
|
|
|
v.source, v.language, v.coverage, v.rights, v.license, filesize,
|
|
|
|
duration, width, height, fps, viewcount, downloadcount";
|
2008-04-29 07:02:07 +00:00
|
|
|
|
|
|
|
#only continue with a valid query string
|
2008-04-16 21:07:07 +00:00
|
|
|
if($strquery)
|
|
|
|
{
|
2008-04-29 07:02:07 +00:00
|
|
|
#select all videos
|
2008-04-16 21:07:07 +00:00
|
|
|
if($strquery eq "*")
|
|
|
|
{
|
|
|
|
$dbquery .= " from videos as v, users as u where u.id = v.userid";
|
|
|
|
}
|
2008-04-29 07:02:07 +00:00
|
|
|
#search
|
2008-04-16 21:07:07 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$dbquery .= ", match(v.title, v.description, v.subject) against( ? in boolean mode) as relevance";
|
|
|
|
$dbquery .= " from videos as v, users as u where u.id = v.userid";
|
|
|
|
$dbquery .= " and match(v.title, v.description, v.subject) against( ? in boolean mode)";
|
|
|
|
push @args, $strquery, $strquery;
|
|
|
|
}
|
|
|
|
|
2008-04-29 07:02:07 +00:00
|
|
|
#match tags if present
|
2008-04-16 21:07:07 +00:00
|
|
|
if(@tags)
|
|
|
|
{
|
|
|
|
$dbquery .= " and match(v.subject) against (? in boolean mode)";
|
|
|
|
push @args, "@tags";
|
|
|
|
}
|
|
|
|
|
2008-04-29 07:02:07 +00:00
|
|
|
#match publisher
|
2008-04-16 21:07:07 +00:00
|
|
|
if($publisher)
|
|
|
|
{
|
|
|
|
$dbquery .= " and match(u.username) against (? in boolean mode)";
|
|
|
|
push @args, "$publisher";
|
|
|
|
}
|
|
|
|
|
2008-04-29 07:02:07 +00:00
|
|
|
#give results the right order
|
2008-04-27 08:40:41 +00:00
|
|
|
if($order)
|
2008-04-16 21:07:07 +00:00
|
|
|
{
|
2008-04-27 08:40:41 +00:00
|
|
|
if($order eq 'filesize')
|
2008-04-16 21:07:07 +00:00
|
|
|
{
|
|
|
|
$dbquery .= " order by v.filesize";
|
|
|
|
}
|
2008-04-27 08:40:41 +00:00
|
|
|
elsif($order eq 'duration')
|
2008-04-16 21:07:07 +00:00
|
|
|
{
|
|
|
|
$dbquery .= " order by v.duration";
|
|
|
|
}
|
2008-04-27 08:40:41 +00:00
|
|
|
elsif($order eq 'viewcount')
|
2008-04-16 21:07:07 +00:00
|
|
|
{
|
|
|
|
$dbquery .= " order by v.viewcount";
|
|
|
|
}
|
2008-04-27 08:40:41 +00:00
|
|
|
elsif($order eq 'downloadcount')
|
2008-04-16 21:07:07 +00:00
|
|
|
{
|
|
|
|
$dbquery .= " order by v.downloadcount";
|
|
|
|
}
|
2008-04-27 08:40:41 +00:00
|
|
|
elsif($order eq 'timestamp')
|
2008-04-16 21:07:07 +00:00
|
|
|
{
|
|
|
|
$dbquery .= " order by v.timestamp";
|
|
|
|
}
|
2008-04-27 08:40:41 +00:00
|
|
|
elsif($order eq 'relevance')
|
2008-04-16 21:07:07 +00:00
|
|
|
{
|
|
|
|
$dbquery .= " order by relevance";
|
|
|
|
}
|
2008-04-27 20:27:21 +00:00
|
|
|
elsif($order eq 'random')
|
|
|
|
{
|
|
|
|
$dbquery .= " order by rand()";
|
|
|
|
}
|
2008-04-16 21:07:07 +00:00
|
|
|
else
|
|
|
|
{
|
2008-04-27 08:40:41 +00:00
|
|
|
$dbquery .= " order by $config->{'search_order_default'}";
|
2008-04-16 21:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if($sort eq "ascending")
|
|
|
|
{
|
|
|
|
$dbquery .= " asc";
|
|
|
|
}
|
|
|
|
elsif($sort eq "descending")
|
|
|
|
{
|
|
|
|
$dbquery .= " desc";
|
|
|
|
}
|
2008-04-27 08:40:41 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
$dbquery .= " $config->{'search_sort_default'}";
|
|
|
|
}
|
2008-04-16 21:07:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $dbquery, @args;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-03-07 20:13:35 +00:00
|
|
|
#replace chars in url according to RFC 1738 <http://www.rfc-editor.org/rfc/rfc1738.txt>
|
2007-12-01 18:04:28 +00:00
|
|
|
sub urlencode
|
|
|
|
{
|
2008-02-14 22:15:38 +00:00
|
|
|
my ($url) = @_[0];
|
|
|
|
$url =~ s/([^A-Za-z0-9_\$\-.+!*'()])/sprintf("%%%02X", ord($1))/eg;
|
|
|
|
return $url;
|
2007-12-01 18:04:28 +00:00
|
|
|
}
|
2007-12-17 18:55:33 +00:00
|
|
|
|
|
|
|
sub output_page
|
|
|
|
{
|
2008-04-27 00:42:15 +00:00
|
|
|
my $doc = shift;
|
2008-02-14 22:15:38 +00:00
|
|
|
my $parser = XML::LibXML->new();
|
|
|
|
my $xslt = XML::LibXSLT->new();
|
2008-04-13 22:19:59 +00:00
|
|
|
|
|
|
|
# let the XSLT param choose other stylesheets or default to xhtml.xsl
|
2008-02-14 22:15:38 +00:00
|
|
|
my $param_xslt = $query->param('xslt');
|
2008-05-11 20:59:44 +00:00
|
|
|
$param_xslt =~ s/[^a-z0-9\[\]\$\-_.+!*'(),]//gi;
|
|
|
|
|
2008-04-27 00:42:15 +00:00
|
|
|
if( -f "$root/xsl/$param_xslt.xsl")
|
|
|
|
{
|
|
|
|
$xsltpath = "$root/xsl/$param_xslt.xsl"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$xsltpath = "$root/xsl/xhtml.xsl";
|
|
|
|
}
|
|
|
|
|
|
|
|
#TODO: preload xslt stylesheet
|
|
|
|
my $stylesheet = $xslt->parse_stylesheet($parser->parse_file($xsltpath));
|
|
|
|
|
|
|
|
$output = $stylesheet->transform($doc);
|
|
|
|
|
|
|
|
if($param_xslt eq "xspf")
|
2008-03-10 15:44:46 +00:00
|
|
|
{
|
2008-03-09 19:32:24 +00:00
|
|
|
return $session->header(
|
2008-04-27 00:42:15 +00:00
|
|
|
-type=>$stylesheet->media_type,
|
|
|
|
-charset=>$stylesheet->output_encoding,
|
|
|
|
-attachment=>$query->param('query').".xspf",
|
2008-03-09 19:32:24 +00:00
|
|
|
),
|
2008-04-27 00:42:15 +00:00
|
|
|
$output->toString;
|
|
|
|
#$stylesheet->output_as_bytes($output); <= for future use with XML::LibXSLT (>= 1.62)
|
|
|
|
}
|
|
|
|
elsif($param_xslt eq "pr0n")
|
|
|
|
{
|
|
|
|
return $session->header(
|
|
|
|
-status=>'402 Payment required',
|
|
|
|
-cost=>'$9001.00', # OVER NEIN THOUSAND
|
|
|
|
)
|
2008-02-14 22:15:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-04-27 00:42:15 +00:00
|
|
|
return $session->header(
|
|
|
|
-type=>$stylesheet->media_type,
|
|
|
|
-charset=>$stylesheet->output_encoding,
|
|
|
|
"x-pingback"=>$config->{"url_root"}."/pingback.pl"
|
|
|
|
),
|
|
|
|
print $output->toString;
|
|
|
|
#$stylesheet->output_as_bytes($output); <= for future use with XML::LibXSLT (>= 1.62)
|
2008-02-14 22:15:38 +00:00
|
|
|
}
|
2007-12-17 18:55:33 +00:00
|
|
|
}
|
2008-04-27 21:37:08 +00:00
|
|
|
|
2008-04-29 07:02:07 +00:00
|
|
|
#output info message
|
2008-04-27 21:37:08 +00:00
|
|
|
sub message
|
|
|
|
{
|
|
|
|
my ($type, $text, $value) = @_;
|
|
|
|
my $message = XML::LibXML::Element->new( "message" );
|
|
|
|
$message->setAttribute("type", $type);
|
|
|
|
$message->setAttribute("text", $text);
|
|
|
|
if ($value)
|
|
|
|
{
|
|
|
|
$message->setAttribute("value", $value);
|
|
|
|
}
|
|
|
|
return $message;
|
|
|
|
}
|