| 1 | package Silki::Role::Controller::WikitextHandler; | ||
| 2 | { | ||
| 3 | $Silki::Role::Controller::WikitextHandler::VERSION = '0.29'; | ||
| 4 | } | ||
| 5 | |||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | use Net::Akismet::Protocol; | ||
| 11 | use Silki::Config; | ||
| 12 | use Silki::I18N qw( loc ); | ||
| 13 | |||
| 14 | use Moose::Role; | ||
| 15 | |||
| 16 | sub _wikitext_from_form { | ||
| 17 | my $self = shift; | ||
| 18 | my $c = shift; | ||
| 19 | my $wiki = shift; | ||
| 20 | |||
| 21 | my $wikitext = $self->_get_wikitext( $c, $wiki ); | ||
| 22 | |||
| 23 | unless ( | ||
| 24 | $c->user()->has_permission_in_wiki( | ||
| 25 | wiki => $wiki, permission => Silki::Schema::Permission->Manage() | ||
| 26 | ) | ||
| 27 | && $c->request()->params()->{skip_spam_check} | ||
| 28 | ) { | ||
| 29 | |||
| 30 | $self->_check_for_link_spam( $c, $wiki, $wikitext ); | ||
| 31 | } | ||
| 32 | |||
| 33 | return $wikitext; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub _get_wikitext { | ||
| 37 | my $self = shift; | ||
| 38 | my $c = shift; | ||
| 39 | my $wiki = shift; | ||
| 40 | |||
| 41 | if ( $c->request()->params()->{format} | ||
| 42 | && $c->request()->params()->{format} eq 'html' ) { | ||
| 43 | my $formatter = Silki::Formatter::HTMLToWiki->new( wiki => $wiki ); | ||
| 44 | |||
| 45 | return $formatter->html_to_wikitext( | ||
| 46 | $c->request()->params()->{content} ); | ||
| 47 | |||
| 48 | } | ||
| 49 | else { | ||
| 50 | return $c->request()->params()->{content}; | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | sub _check_for_link_spam { | ||
| 55 | my $self = shift; | ||
| 56 | my $c = shift; | ||
| 57 | my $wiki = shift; | ||
| 58 | my $wikitext = shift; | ||
| 59 | |||
| 60 | my $config = Silki::Config->instance(); | ||
| 61 | |||
| 62 | my $key = $config->antispam_key(); | ||
| 63 | my $server = $config->antispam_server(); | ||
| 64 | |||
| 65 | return unless length $key && length $server; | ||
| 66 | |||
| 67 | my $ua = Net::Akismet::Protocol->new( | ||
| 68 | key => $key, | ||
| 69 | host => $server, | ||
| 70 | url => $wiki->uri( with_host => 1 ), | ||
| 71 | ); | ||
| 72 | |||
| 73 | my %user_info; | ||
| 74 | unless ( $c->user()->is_system_user() ) { | ||
| 75 | $user_info{comment_author} = $c->user()->best_name(); | ||
| 76 | $user_info{comment_author_email} = $c->user()->email_address(); | ||
| 77 | } | ||
| 78 | |||
| 79 | return | ||
| 80 | unless $ua->check( | ||
| 81 | user_ip => $c->request()->address(), | ||
| 82 | user_agent => $c->request()->user_agent(), | ||
| 83 | comment_content => $wikitext, | ||
| 84 | referer => $c->request()->referer(), | ||
| 85 | comment_type => 'wiki page', | ||
| 86 | %user_info, | ||
| 87 | ); | ||
| 88 | |||
| 89 | die loc( | ||
| 90 | 'Your submission was flagged as spam by our antispam system. Please check any external links in your text.' | ||
| 91 | ) . "\n"; | ||
| 92 | } | ||
| 93 | |||
| 94 | 1; | ||
| 95 | |||
| 96 | # ABSTRACT: Handles wikitext provided by the user, including spam checking | ||
| 97 | |||
| 98 | __END__ |