| 1 | package AxKit::App::TABOO::XSP::Comment; | ||
| 2 | use 5.6.0; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Apache::AxKit::Language::XSP::SimpleTaglib; | ||
| 6 | use Apache::AxKit::Exception; | ||
| 7 | use AxKit; | ||
| 8 | use AxKit::App::TABOO::Data::Comment; | ||
| 9 | use AxKit::App::TABOO::Data::Plurals::Comments; | ||
| 10 | use AxKit::App::TABOO::Data::Story; | ||
| 11 | use AxKit::App::TABOO; | ||
| 12 | use Session; | ||
| 13 | use Time::Piece ':override'; | ||
| 14 | use XML::LibXML; | ||
| 15 | use Net::Akismet; | ||
| 16 | |||
| 17 | use vars qw/$NS/; | ||
| 18 | |||
| 19 | our $VERSION = '0.52'; | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | AxKit::App::TABOO::XSP::Comment - News comment management tag library for TABOO | ||
| 24 | |||
| 25 | =head1 SYNOPSIS | ||
| 26 | |||
| 27 | Add the comment: namespace to your XSP C<E<lt>xsp:pageE<gt>> tag, e.g.: | ||
| 28 | |||
| 29 | <xsp:page | ||
| 30 | language="Perl" | ||
| 31 | xmlns:xsp="http://apache.org/xsp/core/v1" | ||
| 32 | xmlns:comment="http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment" | ||
| 33 | > | ||
| 34 | |||
| 35 | Add this taglib to AxKit (via httpd.conf or .htaccess): | ||
| 36 | |||
| 37 | AxAddXSPTaglib AxKit::App::TABOO::XSP::Comment | ||
| 38 | |||
| 39 | |||
| 40 | =head1 DESCRIPTION | ||
| 41 | |||
| 42 | This XSP taglib provides a single (for now) tag to store information | ||
| 43 | related to news stories, as it communicates with TABOO Data objects, | ||
| 44 | particulary L<AxKit::App::TABOO::Data::Comment>. | ||
| 45 | |||
| 46 | L<Apache::AxKit::Language::XSP::SimpleTaglib> has been used to write | ||
| 47 | this taglib. | ||
| 48 | |||
| 49 | =cut | ||
| 50 | |||
| 51 | |||
| 52 | $NS = 'http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment'; | ||
| 53 | |||
| 54 | |||
| 55 | # Some constants | ||
| 56 | # TODO: This stuff should go somewhere else! | ||
| 57 | |||
| 58 | use constant GUEST => 0; | ||
| 59 | use constant NEWMEMBER => 1; | ||
| 60 | use constant MEMBER => 2; | ||
| 61 | use constant OLDTIMER => 3; | ||
| 62 | use constant ASSISTANT => 4; | ||
| 63 | use constant EDITOR => 5; | ||
| 64 | use constant ADMIN => 6; | ||
| 65 | use constant DIRECTOR => 7; | ||
| 66 | use constant GURU => 8; | ||
| 67 | use constant GOD => 9; | ||
| 68 | |||
| 69 | package AxKit::App::TABOO::XSP::Comment::Handlers; | ||
| 70 | |||
| 71 | =head1 Tag Reference | ||
| 72 | |||
| 73 | =head2 C<E<lt>store/E<gt>> | ||
| 74 | |||
| 75 | It will take whatever data it finds in the L<Apache::Request> object | ||
| 76 | held by AxKit, and hand it to a new | ||
| 77 | L<AxKit::App::TABOO::Data::Comment> object, which will use whatever | ||
| 78 | data it finds useful. It will not store anything unless the user is | ||
| 79 | logged in and authenticated with an authorization level. If an | ||
| 80 | authlevel is not found in the user's session object, it will throw an | ||
| 81 | exceptions with an C<AUTH_REQUIRED> code. | ||
| 82 | |||
| 83 | If TABOOAkismetKey is set (and spammers will make you want this really | ||
| 84 | fast), it will check the Akismet anti-spam system if the user has an | ||
| 85 | authlevel less than 2, and return a C<FORBIDDEN> if it is deemed to be | ||
| 86 | spam. | ||
| 87 | |||
| 88 | Finally, the Data object is instructed to save itself. | ||
| 89 | |||
| 90 | If successful, it will return a C<store> element in the output | ||
| 91 | namespace with the number 1. | ||
| 92 | |||
| 93 | =cut | ||
| 94 | |||
| 95 | |||
| 96 | sub store : node({http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment/Output}store) { | ||
| 97 | return << 'EOC' | ||
| 98 | my %args; | ||
| 99 | foreach my $name ($cgi->param) { | ||
| 100 | $args{$name} = $cgi->param($name); | ||
| 101 | } | ||
| 102 | my $session = AxKit::App::TABOO::session($r); | ||
| 103 | $args{'username'} = AxKit::App::TABOO::loggedin($session); | ||
| 104 | |||
| 105 | my $authlevel = AxKit::App::TABOO::authlevel($session); | ||
| 106 | AxKit::Debug(4, "Logged in as $args{'username'} at level $authlevel"); | ||
| 107 | unless (defined($authlevel)) { | ||
| 108 | throw Apache::AxKit::Exception::Retval( | ||
| 109 | return_code => AUTH_REQUIRED, | ||
| 110 | -text => "Not authenticated and authorized with an authlevel"); | ||
| 111 | } | ||
| 112 | my $check = AxKit::App::TABOO::Data::Comment->new(); | ||
| 113 | if (($args{'parentcpath'}) | ||
| 114 | && (!$check->exist(storyname => $args{'storyname'}, | ||
| 115 | sectionid => $args{'sectionid'}, | ||
| 116 | commentpath => $args{'parentcpath'}))) { | ||
| 117 | # This is actually bad, it shouldn't ever happen | ||
| 118 | throw Apache::AxKit::Exception::Retval( | ||
| 119 | return_code => FORBIDDEN, | ||
| 120 | -text => "The parent post of the submitter doesn't exist."); | ||
| 121 | } | ||
| 122 | my $timestamp = localtime; | ||
| 123 | unless ($args{'timestamp'}) { | ||
| 124 | $args{'timestamp'} = $timestamp->datetime; | ||
| 125 | } | ||
| 126 | $args{'commentpath'} = $args{'parentcpath'} . '/' . $args{'username'}; | ||
| 127 | my $check = AxKit::App::TABOO::Data::Plurals::Comments->new(); | ||
| 128 | my $exists = $check->exist(storyname => $args{'storyname'}, | ||
| 129 | sectionid => $args{'sectionid'}, | ||
| 130 | commentpath => $args{'commentpath'}); | ||
| 131 | if ($exists) { | ||
| 132 | $args{'commentpath'} .= '_' . ++$exists; | ||
| 133 | } | ||
| 134 | delete $args{'parentcpath'}; | ||
| 135 | |||
| 136 | if ($r->dir_config('TABOOAkismetKey')) { | ||
| 137 | AxKit::Debug(4, "Using Akismet"); | ||
| 138 | my $akismet = Net::Akismet->new( | ||
| 139 | KEY => $r->dir_config('TABOOAkismetKey'), | ||
| 140 | URL => 'http://'.$r->header_in('X-Forwarded-Host'), | ||
| 141 | ) or throw Apache::AxKit::Exception::Error(-text => "Akismet key verification failed."); | ||
| 142 | my %akismetstuff = (USER_IP => $r->header_in('X-Forwarded-For'), | ||
| 143 | COMMENT_CONTENT => $args{'content'}, | ||
| 144 | REFERRER => $r->header_in('Referer'), | ||
| 145 | COMMENT_TYPE => 'comment', | ||
| 146 | ); | ||
| 147 | if ($authlevel >= 2) { # Presumed ham | ||
| 148 | $akismet->ham(%akismetstuff); | ||
| 149 | } else { | ||
| 150 | AxKit::Debug(10, "Akismet check on: ".join(" ",values(%akismetstuff))); | ||
| 151 | if ($akismet->check(%akismetstuff) eq 'true') { | ||
| 152 | throw Apache::AxKit::Exception::Retval( | ||
| 153 | return_code => FORBIDDEN, | ||
| 154 | -text => "Akismet check says that your comment is spam. Please contact webmaster if you received this message in error."); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | my $comment = AxKit::App::TABOO::Data::Comment->new(); | ||
| 160 | $comment->populate(\%args); | ||
| 161 | $comment->save(); | ||
| 162 | # Modify the last timestamp of the parent story | ||
| 163 | my $story = AxKit::App::TABOO::Data::Story->new(); | ||
| 164 | $story->load(what => 'storyname,sectionid', | ||
| 165 | limit => {storyname => $args{'storyname'}, | ||
| 166 | sectionid => $args{'sectionid'}}); | ||
| 167 | $story->lasttimestamp($timestamp); | ||
| 168 | $story->save; | ||
| 169 | 1; | ||
| 170 | EOC | ||
| 171 | } | ||
| 172 | |||
| 173 | |||
| 174 | =head2 C<E<lt>this-comment/E<gt>> | ||
| 175 | |||
| 176 | Will return an XML representation of the data submitted in the last | ||
| 177 | request, enclosed in a C<comment-submission> element. Particularly | ||
| 178 | useful for previewing a submission. | ||
| 179 | |||
| 180 | =cut | ||
| 181 | |||
| 182 | sub this_comment : struct { | ||
| 183 | return << 'EOC' | ||
| 184 | my %args; | ||
| 185 | foreach my $name ($cgi->param) { | ||
| 186 | $args{$name} = $cgi->param($name); | ||
| 187 | } | ||
| 188 | $args{'username'} = AxKit::App::TABOO::loggedin(AxKit::App::TABOO::session($r)); | ||
| 189 | my $timestamp = localtime; | ||
| 190 | unless ($args{'timestamp'}) { | ||
| 191 | $args{'timestamp'} = $timestamp->datetime; | ||
| 192 | } | ||
| 193 | my $comment = AxKit::App::TABOO::Data::Comment->new(); | ||
| 194 | $comment->populate(\%args); | ||
| 195 | $comment->adduserinfo(); | ||
| 196 | # $comment->addcatinfo(); | ||
| 197 | |||
| 198 | my $doc = XML::LibXML::Document->new(); | ||
| 199 | my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment/Output', 'comm:comment-submission'); | ||
| 200 | $doc->setDocumentElement($root); | ||
| 201 | $comment->write_xml($doc, $root); # Return an XML representation | ||
| 202 | EOC | ||
| 203 | } | ||
| 204 | |||
| 205 | |||
| 206 | =head2 C<E<lt>get-comment/E<gt>> | ||
| 207 | |||
| 208 | Will return an XML representation of the data for a previously saved | ||
| 209 | comment, enclosed in a C<comment-loaded> element. It needs to get the | ||
| 210 | comment identified by C<storyname>, C<sectionid> and C<commentpath> | ||
| 211 | attributes or child elements. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | |||
| 216 | sub get_comment : struct attribOrChild(storyname,sectionid,commentpath) { | ||
| 217 | return << 'EOC' | ||
| 218 | my $comment = AxKit::App::TABOO::Data::Comment->new(); | ||
| 219 | unless ($comment->load(what => '*', limit => {sectionid => $attr_sectionid, | ||
| 220 | storyname=> $attr_storyname, | ||
| 221 | commentpath=> $attr_commentpath})) { | ||
| 222 | throw Apache::AxKit::Exception::Retval( | ||
| 223 | return_code => 404, | ||
| 224 | -text => "Requested comment $attr_commentpath not found."); | ||
| 225 | } | ||
| 226 | $comment->adduserinfo(); | ||
| 227 | my $doc = XML::LibXML::Document->new(); | ||
| 228 | my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment/Output', 'comm:comment-loaded'); | ||
| 229 | $doc->setDocumentElement($root); | ||
| 230 | $comment->write_xml($doc, $root); # Return an XML representation | ||
| 231 | EOC | ||
| 232 | } | ||
| 233 | |||
| 234 | |||
| 235 | 1; | ||
| 236 | |||
| 237 | |||
| 238 | |||
| 239 | =head1 FORMALITIES | ||
| 240 | |||
| 241 | See L<AxKit::App::TABOO>. | ||
| 242 | |||
| 243 | =cut |