Ticket #198: earle-banned-wordlist.patch

File earle-banned-wordlist.patch, 5.6 kB (added by dom, 20 months ago)

Patch against r994 for banned wordlist functionality

  • templates/banned_content.tt

     
     1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
     2 "http://www.w3.org/TR/html4/loose.dtd"> 
     3<html> 
     4<head> 
     5  <title>Edit Forbidden</title> 
     6  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
     7</head> 
     8<body> 
     9<h1>Edit Forbidden</h1> 
     10<p> 
     11Sorry. Somewhere in the edit you just tried to make, there was a word or phrase 
     12that the site's administrator has chosen to prohibit. Please contact the site 
     13administrator at <a href="mailto:[% contact_email %]">[% contact_email %]</a> 
     14with the <strong>complete</strong> text of your edit (including both the main  
     15content and any optional details you entered) for assistance. 
     16</p> 
     17</body> 
     18</html> 
     19 No newline at end of file 
  • lib/OpenGuides/Config.pm

     
    2020   centre_lat default_gmaps_zoom default_gmaps_search_zoom force_wgs84 
    2121   licence_name licence_url licence_info_url moderation_requires_password 
    2222   enable_node_image enable_common_categories enable_common_locales 
     23   banned_content 
    2324); 
    2425my @questions = map { $_ . "__qu" } @variables; 
    2526OpenGuides::Config->mk_accessors( @variables ); 
     
    180181        google_analytics_key => "Do you have a Google Analytics key to use with this guide? If you enter it here, then Google Analytics functionality will be automatically enabled.", 
    181182        licence_name => "What licence will you use for the guide?", 
    182183        licence_url => "What is the URL to your licence?", 
    183         licence_info_url => "What is the URL to your local page about your licensing policy?" 
     184        licence_info_url => "What is the URL to your local page about your licensing policy?", 
     185        banned_content => "What words or phrases are forbidden in edits? Space-separated list, use _ to represent a space in a phrase.", 
    184186    ); 
    185187 
    186188    foreach my $var ( keys %questions ) { 
     
    301303 
    302304=item * licence_info_url 
    303305 
     306=item * banned_content 
     307 
    304308=back 
    305309 
    306310=head1 AUTHOR 
  • lib/OpenGuides.pm

     
    11751175        cgi_obj => $q 
    11761176    ); 
    11771177 
     1178    foreach my $var ( qw( summary username comment edit_type ) ) { 
     1179        $new_metadata{$var} = $q->param($var) || ""; 
     1180    } 
     1181 
     1182    $new_metadata{host} = $ENV{REMOTE_ADDR}; 
     1183  
    11781184    delete $new_metadata{website} if $new_metadata{website} eq 'http://'; 
    11791185 
    11801186    $new_metadata{opening_hours_text} = $q->param("hours_text") || ""; 
     
    11861192    $new_metadata{longitude} = delete $new_metadata{longitude_unmunged} 
    11871193        if $new_metadata{longitude_unmunged}; 
    11881194 
     1195    # Here we examine content and metadata for banned strings. 
     1196    my $banned_content = $self->banned_content; 
     1197     
     1198    foreach my $banned_string (keys %$banned_content) { 
     1199        return $self->_banned_content_abort({ 
     1200            node          => $node, 
     1201            where         => 'content', 
     1202            string        => $banned_string, 
     1203            host          => $new_metadata{host}, 
     1204            return_output => $return_output, 
     1205        }) if $content =~ /$banned_string/im; 
     1206    } 
     1207     
     1208    while ( my ($md_type, $md_val) = each %new_metadata ) { 
     1209        foreach my $banned_string (keys %$banned_content) { 
     1210            return $self->_banned_content_abort({ 
     1211                node          => $node, 
     1212                where         => $md_type, 
     1213                string        => $banned_string, 
     1214                host          => $new_metadata{host}, 
     1215                return_output => $return_output, 
     1216            }) if $md_val =~ /$banned_string/im; 
     1217        } 
     1218    } 
     1219 
    11891220    # Check to make sure all the indexable nodes are created 
    11901221    # Skip this for nodes needing moderation - this occurs for them once 
    11911222    #  they've been moderated 
     
    11961227        ); 
    11971228    } 
    11981229     
    1199     foreach my $var ( qw( summary username comment edit_type ) ) { 
    1200         $new_metadata{$var} = $q->param($var) || ""; 
    1201     } 
    1202     $new_metadata{host} = $ENV{REMOTE_ADDR}; 
    1203  
    12041230    # Wiki::Toolkit::Plugin::RSS::ModWiki wants "major_change" to be set. 
    12051231    $new_metadata{major_change} = ( $new_metadata{edit_type} eq "Normal edit" ) 
    12061232                                    ? 1 
     
    19051931    return $cookie_data{$pref_name}; 
    19061932} 
    19071933 
     1934# Retrieve banned strings from config and convert them to unmunged form. 
     1935sub banned_content { 
     1936    my $self = shift; 
     1937     
     1938    my $banned_content; 
     1939     
     1940    # Strings have underscores as spaces due to Config::Tiny limitations. 
     1941    foreach (split(' ', $self->config->banned_content)) { 
     1942        $_ =~ s/_/ /g; 
     1943        $banned_content->{$_} = 1; 
     1944    } 
     1945     
     1946    $banned_content; 
     1947} 
    19081948 
     1949# Kill an edit if we detect banned content. 
     1950sub _banned_content_abort { 
     1951    my ($self, $args) = @_; 
     1952 
     1953    # Don't tell the editor what was banned; ask them to get in touch. Best not 
     1954    # to give spammers any ideas about how our spam detection works. 
     1955    my $output = $self->process_template( 
     1956        template => 'banned_content.tt', 
     1957    ); 
     1958 
     1959    # Put something in the logs.     
     1960    warn "Edit aborted, banned content detected.\nHost: $args->{host}; node: $args->{node}; where: $args->{where}; string: $args->{string}."; 
     1961 
     1962    return $output if $args->{return_output}; 
     1963    print $output; 
     1964} 
     1965 
    19091966=head1 BUGS AND CAVEATS 
    19101967 
    19111968UTF8 data are currently not handled correctly throughout.