AxKit-App-TABOO

 view release on metacpan or  search on metacpan

lib/AxKit/App/TABOO/XSP/Comment.pm  view on Meta::CPAN


If TABOOAkismetKey is set (and spammers will make you want this really
fast), it will check the Akismet anti-spam system if the user has an
authlevel less than 2, and return a C<FORBIDDEN> if it is deemed to be
spam.

Finally, the Data object is instructed to save itself.

If successful, it will return a C<store> element in the output
namespace with the number 1.

=cut


sub store : node({http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment/Output}store) {
    return << 'EOC'
    my %args;
    foreach my $name ($cgi->param) {
      $args{$name} = $cgi->param($name);
    }
    my $session = AxKit::App::TABOO::session($r);
    $args{'username'} = AxKit::App::TABOO::loggedin($session);

    my $authlevel = AxKit::App::TABOO::authlevel($session);
    AxKit::Debug(4, "Logged in as $args{'username'} at level $authlevel");
    unless (defined($authlevel)) {
	throw Apache::AxKit::Exception::Retval(
					       return_code => AUTH_REQUIRED,
					       -text => "Not authenticated and authorized with an authlevel");
    }
    my $check = AxKit::App::TABOO::Data::Comment->new();
    if (($args{'parentcpath'})
	 && (!$check->exist(storyname => $args{'storyname'}, 
			    sectionid => $args{'sectionid'},
			    commentpath => $args{'parentcpath'}))) {
      # This is actually bad, it shouldn't ever happen
      throw Apache::AxKit::Exception::Retval(
					     return_code => FORBIDDEN,
					     -text => "The parent post of the submitter doesn't exist.");
    }
    my $timestamp = localtime;
    unless ($args{'timestamp'}) {
	$args{'timestamp'} = $timestamp->datetime;
    }
    $args{'commentpath'} = $args{'parentcpath'} . '/' . $args{'username'};
    my $check = AxKit::App::TABOO::Data::Plurals::Comments->new();
    my $exists = $check->exist(storyname => $args{'storyname'}, 
			       sectionid => $args{'sectionid'},
			       commentpath => $args{'commentpath'});
    if ($exists) {
      $args{'commentpath'} .= '_' . ++$exists;
    }
    delete $args{'parentcpath'};

    if ($r->dir_config('TABOOAkismetKey')) {
      AxKit::Debug(4, "Using Akismet");
      my $akismet = Net::Akismet->new(
                        KEY => $r->dir_config('TABOOAkismetKey'),
                        URL => 'http://'.$r->header_in('X-Forwarded-Host'),
                ) or throw Apache::AxKit::Exception::Error(-text => "Akismet key verification failed.");
      my %akismetstuff = (USER_IP => $r->header_in('X-Forwarded-For'),
			  COMMENT_CONTENT => $args{'content'},
			  REFERRER => $r->header_in('Referer'),
			  COMMENT_TYPE => 'comment',
			 );
      if ($authlevel >= 2) { # Presumed ham
	$akismet->ham(%akismetstuff);
      } else {
	AxKit::Debug(10, "Akismet check on: ".join("    ",values(%akismetstuff)));
	if ($akismet->check(%akismetstuff) eq 'true') {
	  throw Apache::AxKit::Exception::Retval(
						 return_code => FORBIDDEN,
						 -text => "Akismet check says that your comment is spam. Please contact webmaster if you received this message in error.");
	}
      }
    }

    my $comment = AxKit::App::TABOO::Data::Comment->new();
    $comment->populate(\%args);
    $comment->save();
    # Modify the last timestamp of the parent story
    my $story = AxKit::App::TABOO::Data::Story->new();
    $story->load(what => 'storyname,sectionid',
		 limit => {storyname => $args{'storyname'},
			   sectionid => $args{'sectionid'}});
    $story->lasttimestamp($timestamp);
    $story->save;
    1;
EOC
}


=head2 C<E<lt>this-comment/E<gt>>

Will return an XML representation of the data submitted in the last
request, enclosed in a C<comment-submission> element. Particularly
useful for previewing a submission.

=cut

sub this_comment : struct {
    return << 'EOC'
    my %args;
    foreach my $name ($cgi->param) {
      $args{$name} = $cgi->param($name);
    }
    $args{'username'} = AxKit::App::TABOO::loggedin(AxKit::App::TABOO::session($r));
    my $timestamp = localtime;
    unless ($args{'timestamp'}) {
	$args{'timestamp'} = $timestamp->datetime;
    }
    my $comment = AxKit::App::TABOO::Data::Comment->new();
    $comment->populate(\%args);
    $comment->adduserinfo();
#    $comment->addcatinfo();
    
    my $doc = XML::LibXML::Document->new();
    my $root = $doc->createElementNS('http://www.kjetil.kjernsmo.net/software/TABOO/NS/Comment/Output', 'comm:comment-submission');
    $doc->setDocumentElement($root);
    $comment->write_xml($doc, $root); # Return an XML representation
EOC



( run in 1.577 second using v1.01-cache-2.11-cpan-39bf76dae61 )