From 6c172e1057bf17b33c85c9add2d7d83cac1808fd Mon Sep 17 00:00:00 2001 From: josch Date: Sun, 27 Apr 2008 10:39:21 +0000 Subject: [PATCH] enabled xhtml comments git-svn-id: http://yolanda.mister-muffin.de/svn@321 7eef14d0-6ed0-489d-bf55-20463b2d70db --- trunk/functions.pl | 16 ++-- trunk/index.pl | 16 ++-- trunk/login.pl | 4 +- trunk/register.pl | 40 ++++----- trunk/search.pl | 8 +- trunk/settings.pl | 12 +-- trunk/video.pl | 166 ++++++++++++++++++++++---------------- trunk/xsl/xhtml/video.xsl | 6 +- 8 files changed, 145 insertions(+), 123 deletions(-) diff --git a/trunk/functions.pl b/trunk/functions.pl index 2f24f6d..9c8aee1 100644 --- a/trunk/functions.pl +++ b/trunk/functions.pl @@ -26,18 +26,18 @@ sub get_page_array #get parameters my (@userinfo) = @_; - my $root = XML::LibXML::Element->new( "page" ); + my $page = XML::LibXML::Element->new( "page" ); my ($locale) = $query->http('HTTP_ACCEPT_LANGUAGE') =~ /^([^,]+),.*$/; - $root->setAttribute( "locale", $locale ? $locale : "en_us" ); + $page->setAttribute( "locale", $locale ? $locale : "en_us" ); - $root->setAttribute( "username", $userinfo->{'username'} ); - $root->setNamespace("http://www.w3.org/1999/xhtml", "xhtml", 0); - $root->setNamespace("http://web.resource.org/cc/", "cc", 0); - $root->setNamespace("http://purl.org/dc/elements/1.1/", "dc", 0); - $root->setNamespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf", 0); + $page->setAttribute( "username", $userinfo->{'username'} ); + $page->setNamespace("http://www.w3.org/1999/xhtml", "xhtml", 0); + $page->setNamespace("http://web.resource.org/cc/", "cc", 0); + $page->setNamespace("http://purl.org/dc/elements/1.1/", "dc", 0); + $page->setNamespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf", 0); - return $root; + return $page; } # index.pl (display custom search) diff --git a/trunk/index.pl b/trunk/index.pl index 0dc4cd8..4a2440b 100644 --- a/trunk/index.pl +++ b/trunk/index.pl @@ -9,9 +9,9 @@ $session = new CGI::Session; my $doc = XML::LibXML::Document->new( "1.0", "UTF-8" ); -my $root = get_page_array(@userinfo); +my $page = get_page_array(@userinfo); -$root->appendChild(XML::LibXML::Element->new( "frontpage" )); +$page->appendChild(XML::LibXML::Element->new( "frontpage" )); if($query->param('information')) { @@ -19,7 +19,7 @@ if($query->param('information')) $message->setAttribute("type", "information"); $message->setAttribute("text", $query->param('information')); $message->setAttribute("value",$query->param('value')); - $root->appendChild($message); + $page->appendChild($message); } elsif($query->param('error')) { @@ -27,7 +27,7 @@ elsif($query->param('error')) $message->setAttribute("type", "error"); $message->setAttribute("text", $query->param('error')); $message->setAttribute("value",$query->param('value')); - $root->appendChild($message); + $page->appendChild($message); } elsif($query->param('warning')) { @@ -35,7 +35,7 @@ elsif($query->param('warning')) $message->setAttribute("type", "warning"); $message->setAttribute("text", $query->param('warning')); $message->setAttribute("value",$query->param('value')); - $root->appendChild($message); + $page->appendChild($message); } my $tagcloud = XML::LibXML::Element->new( "tagcloud" ); @@ -58,7 +58,7 @@ while (my ($text, $count) = $sth->fetchrow_array()) #finish query $sth->finish() or die $dbh->errstr; -$root->appendChild($tagcloud); +$page->appendChild($tagcloud); foreach $strquery ($config->{"search_frontpage_one_query"}, $config->{"search_frontpage_two_query"}, $config->{"search_frontpage_three_query"}) { @@ -163,9 +163,9 @@ foreach $strquery ($config->{"search_frontpage_one_query"}, $config->{"search_fr $results->appendChild($result); } - $root->appendChild($results); + $page->appendChild($results); } -$doc->setDocumentElement($root); +$doc->setDocumentElement($page); output_page($doc); diff --git a/trunk/login.pl b/trunk/login.pl index 7133cd6..02a101e 100644 --- a/trunk/login.pl +++ b/trunk/login.pl @@ -7,9 +7,7 @@ $session = new CGI::Session; @userinfo = get_userinfo_from_sid($session->id); -my $doc = XML::LibXML::Document->new( "1.0", "UTF-8" ); - -my $root = get_page_array(@userinfo); +#TODO: use $query->http("HTTP_REFERER"); to redirect to the site last visited if($query->param('action') eq "logout") { diff --git a/trunk/register.pl b/trunk/register.pl index ad39510..7dc3d32 100644 --- a/trunk/register.pl +++ b/trunk/register.pl @@ -9,7 +9,7 @@ $session = new CGI::Session; my $doc = XML::LibXML::Document->new( "1.0", "UTF-8" ); -my $root = get_page_array(@userinfo); +my $page = get_page_array(@userinfo); #check if user is logged in if($username) @@ -17,9 +17,9 @@ if($username) my $message = XML::LibXML::Element->new( "message" ); $message->setAttribute("type", "error"); $message->setAttribute("text", "error_already_registered"); - $root->appendChild($message); + $page->appendChild($message); - $doc->setDocumentElement($root); + $doc->setDocumentElement($page); output_page($doc); } @@ -36,14 +36,14 @@ elsif($query->param('user') and $query->param('pass') and $query->param('pass_re #if something was returned the selected username already exists if($sth->fetchrow_array()) { - $root->appendChild(XML::LibXML::Element->new( "registerform" )); + $page->appendChild(XML::LibXML::Element->new( "registerform" )); my $message = XML::LibXML::Element->new( "message" ); $message->setAttribute("type", "error"); $message->setAttribute("text", "error_username_already_registered"); - $root->appendChild($message); + $page->appendChild($message); - $doc->setDocumentElement($root); + $doc->setDocumentElement($page); output_page($doc); } @@ -58,62 +58,62 @@ elsif($query->param('user') and $query->param('pass') and $query->param('pass_re } else { - $root->appendChild(XML::LibXML::Element->new( "registerform" )); + $page->appendChild(XML::LibXML::Element->new( "registerform" )); my $message = XML::LibXML::Element->new( "message" ); $message->setAttribute("type", "error"); $message->setAttribute("text", "error_passwords_do_not_match"); - $root->appendChild($message); + $page->appendChild($message); - $doc->setDocumentElement($root); + $doc->setDocumentElement($page); output_page($doc); } } elsif(not $query->param('user') and ($query->param('pass') or $query->param('pass_repeat'))) { - $root->appendChild(XML::LibXML::Element->new( "registerform" )); + $page->appendChild(XML::LibXML::Element->new( "registerform" )); my $message = XML::LibXML::Element->new( "message" ); $message->setAttribute("type", "error"); $message->setAttribute("text", "error_insert_username"); - $root->appendChild($message); + $page->appendChild($message); - $doc->setDocumentElement($root); + $doc->setDocumentElement($page); output_page($doc); } elsif(not $query->param('pass') and ($query->param('user') or $query->param('pass_repeat'))) { - $root->appendChild(XML::LibXML::Element->new( "registerform" )); + $page->appendChild(XML::LibXML::Element->new( "registerform" )); my $message = XML::LibXML::Element->new( "message" ); $message->setAttribute("type", "error"); $message->setAttribute("text", "error_insert_password"); - $root->appendChild($message); + $page->appendChild($message); - $doc->setDocumentElement($root); + $doc->setDocumentElement($page); output_page($doc); } elsif(not $query->param('pass_repeat') and ($query->param('user') or $query->param('pass'))) { - $root->appendChild(XML::LibXML::Element->new( "registerform" )); + $page->appendChild(XML::LibXML::Element->new( "registerform" )); my $message = XML::LibXML::Element->new( "message" ); $message->setAttribute("type", "error"); $message->setAttribute("text", "error_repeat_password"); - $root->appendChild($message); + $page->appendChild($message); - $doc->setDocumentElement($root); + $doc->setDocumentElement($page); output_page($doc); } else { - $root->appendChild(XML::LibXML::Element->new( "registerform" )); + $page->appendChild(XML::LibXML::Element->new( "registerform" )); - $doc->setDocumentElement($root); + $doc->setDocumentElement($page); output_page($doc); } diff --git a/trunk/search.pl b/trunk/search.pl index d0946f4..b69f10a 100644 --- a/trunk/search.pl +++ b/trunk/search.pl @@ -9,7 +9,7 @@ $session = new CGI::Session; my $doc = XML::LibXML::Document->new( "1.0", "UTF-8" ); -my $root = get_page_array(@userinfo); +my $page = get_page_array(@userinfo); #check if query is set if($query->param('query')) @@ -18,10 +18,10 @@ if($query->param('query')) if($dbquery) { - $root->appendChild(fill_results($dbquery, @args)); - $root->setAttribute('query', $query->param('query')); + $page->appendChild(fill_results($dbquery, @args)); + $page->setAttribute('query', $query->param('query')); - $doc->setDocumentElement($root); + $doc->setDocumentElement($page); my @results = $doc->findnodes( "//results/result" ); diff --git a/trunk/settings.pl b/trunk/settings.pl index faa71c1..1972990 100644 --- a/trunk/settings.pl +++ b/trunk/settings.pl @@ -9,7 +9,7 @@ my @userinfo = get_userinfo_from_sid($session->id); my $doc = XML::LibXML::Document->new( "1.0", "UTF-8" ); -my $root = get_page_array(@userinfo); +my $page = get_page_array(@userinfo); if($userinfo->{'username'}) { @@ -19,12 +19,12 @@ if($userinfo->{'username'}) @userinfo = get_userinfo_from_sid($session->id); - $root = get_page_array(@userinfo); + $page = get_page_array(@userinfo); my $message = XML::LibXML::Element->new( "message" ); $message->setAttribute("type", "information"); $message->setAttribute("text", "information_settings_changed"); - $root->appendChild($message); + $page->appendChild($message); } } @@ -33,16 +33,16 @@ if($userinfo->{'username'}) { my $settings = XML::LibXML::Element->new( "settings" ); $settings->setAttribute("pagesize", $userinfo->{'pagesize'}); - $root->appendChild($settings); + $page->appendChild($settings); } else { my $message = XML::LibXML::Element->new( "message" ); $message->setAttribute("type", "error"); $message->setAttribute("text", "error_202c"); - $root->appendChild($message); + $page->appendChild($message); } -$doc->setDocumentElement($root); +$doc->setDocumentElement($page); output_page($doc); diff --git a/trunk/video.pl b/trunk/video.pl index c9b6eeb..64189ed 100644 --- a/trunk/video.pl +++ b/trunk/video.pl @@ -9,7 +9,7 @@ $session = new CGI::Session; my $doc = XML::LibXML::Document->new( "1.0", "UTF-8" ); -my $root = get_page_array(@userinfo); +my $page = get_page_array(@userinfo); #check if id or title is passed if($query->url_param('id')) @@ -54,11 +54,11 @@ if($query->url_param('id')) { if($query->param('embed') eq "video") { - $root->setAttribute( "embed", "video" ); + $page->setAttribute( "embed", "video" ); } elsif($query->param('embed') eq "preview") { - $root->setAttribute( "embed", "preview" ); + $page->setAttribute( "embed", "preview" ); } #if there was a single result, display the video @@ -75,78 +75,97 @@ if($query->url_param('id')) #check if a comment is about to be created if($query->param('comment')) { - #output infobox - my $message = XML::LibXML::Element->new( "message" ); - $message->setAttribute("type", "information"); - $message->setAttribute("text", "information_comment_created"); - $root->appendChild($message); - - #add to database - $dbh->do(qq{insert into comments (userid, videoid, text, timestamp) values (?, ?, ?, unix_timestamp())}, undef, - $userinfo->{'id'}, $id, $query->param('comment')) or die $dbh->errstr; - - #send pingbacks to every url in the comment - my (@matches) = $query->param('comment') =~ /(http:\/\/\S+)/gi; - foreach $match (@matches) + my $dtd = XML::LibXML::Dtd->new(0, "$root/site/comment.dtd"); + $dom = XML::LibXML->new; + $dom->clean_namespaces(1); + eval { $doc = $dom->parse_string("".$query->param('comment')."") }; + if ($@) { - #ask for http header only - if pingbacks are implemented right, then we wont need the full site - my $request = HTTP::Request->new(HEAD => $match); - my $ua = LWP::UserAgent->new; - - my $response = $ua->request($request); - - if ($response->is_success) + die $@; + } + else + { + eval { $doc->validate($dtd) }; + if ($@) { - my $pingbackurl = $response->header("x-pingback"); + die $@; + } + else + { + #output infobox + my $message = XML::LibXML::Element->new( "message" ); + $message->setAttribute("type", "information"); + $message->setAttribute("text", "information_comment_created"); + $page->appendChild($message); - #if there was no x-pingback header, fetch the website and search for link element - if (!$pingbackurl) + #add to database + $dbh->do(qq{insert into comments (userid, videoid, text, timestamp) values (?, ?, ?, unix_timestamp())}, undef, + $userinfo->{'id'}, $id, $query->param('comment')) or die $dbh->errstr; + + #send pingbacks to every url in the comment + my (@matches) = $query->param('comment') =~ /]+href="(http:\/\/\S+)"[^>]*>.+?<\/a>/gi; + foreach $match (@matches) { - $request = HTTP::Request->new(GET => $match); - $response = $ua->request($request); + #ask for http header only - if pingbacks are implemented right, then we wont need the full site + my $request = HTTP::Request->new(HEAD => $match); + my $ua = LWP::UserAgent->new; + + my $response = $ua->request($request); if ($response->is_success) { - ($pingbackurl) = $response->content =~ //; + my $pingbackurl = $response->header("x-pingback"); + + #if there was no x-pingback header, fetch the website and search for link element + if (!$pingbackurl) + { + $request = HTTP::Request->new(GET => $match); + $response = $ua->request($request); + + if ($response->is_success) + { + ($pingbackurl) = $response->content =~ //; + } + } + + #if requests were successful, send the pingbacks + if ($pingbackurl) + { + #TODO: expand xml entities < > & " in $pingbackurl + + #contruct the xml-rpc request + my $xmlpost = (); + $xmlpost->{"methodName"} = ["pingback.ping"]; + push @{$xmlpost->{'params'}->{'param'} }, + { + "value" => + { + "string" => [$config->{"url_root"}."/video/".urlencode($title)."/$id/"] + } + }; + push @{$xmlpost->{'params'}->{'param'} }, + { + "value" => + { + "string" => [$match] + } + }; + + #post a xml-rpc request + $request = HTTP::Request->new(POST => $pingbackurl); + $request->header('Content-Type' => "application/xml"); + $request->content(XMLout( + $xmlpost, + XMLDecl => 1, + KeyAttr => {}, + RootName => 'methodCall' + )); + $ua = LWP::UserAgent->new; + $response = $ua->request($request); + #TODO: maybe do something on success? + } } } - - #if requests were successful, send the pingbacks - if ($pingbackurl) - { - #TODO: expand xml entities < > & " in $pingbackurl - - #contruct the xml-rpc request - my $xmlpost = (); - $xmlpost->{"methodName"} = ["pingback.ping"]; - push @{$xmlpost->{'params'}->{'param'} }, - { - "value" => - { - "string" => [$config->{"url_root"}."/video/".urlencode($title)."/$id/"] - } - }; - push @{$xmlpost->{'params'}->{'param'} }, - { - "value" => - { - "string" => [$match] - } - }; - - #post a xml-rpc request - $request = HTTP::Request->new(POST => $pingbackurl); - $request->header('Content-Type' => "application/xml"); - $request->content(XMLout( - $xmlpost, - XMLDecl => 1, - KeyAttr => {}, - RootName => 'methodCall' - )); - $ua = LWP::UserAgent->new; - $response = $ua->request($request); - #TODO: maybe do something on success? - } } } } @@ -236,7 +255,7 @@ if($query->url_param('id')) $video->appendChild($rdf); - $root->appendChild($video); + $page->appendChild($video); #get comments my $comments = XML::LibXML::Element->new( "comments" ); @@ -247,17 +266,22 @@ if($query->url_param('id')) $sth->execute($id) or die $dbh->errstr; while (my ($commentid, $text, $username, $timestamp) = $sth->fetchrow_array()) { - my $comment = XML::LibXML::Element->new( "comment" ); + my $dom = XML::LibXML->new; + my $doc = $dom->parse_string("".$text.""); + my $comment = $doc->documentElement(); + foreach $node ($comment->getElementsByTagName("*")) + { + $node->setNamespace("http://www.w3.org/1999/xhtml", "xhtml"); + } $comment->setAttribute('username', $username); $comment->setAttribute('timestamp', $timestamp); $comment->setAttribute('id', $commentid); - $comment->appendTextChild("text", $text); $comments->appendChild($comment); } - $root->appendChild($comments); + $page->appendChild($comments); - $doc->setDocumentElement($root); + $doc->setDocumentElement($page); output_page($doc); } diff --git a/trunk/xsl/xhtml/video.xsl b/trunk/xsl/xhtml/video.xsl index 30e5c04..baf73a7 100644 --- a/trunk/xsl/xhtml/video.xsl +++ b/trunk/xsl/xhtml/video.xsl @@ -90,10 +90,10 @@ ---> + @@ -109,7 +109,7 @@ :
- +