| 1 | package Perlanet::Trait::Scrubber; | ||||||||||
| 2 | |||||||||||
| 3 | 11 | ▶ | use 5.10.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 11 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 11 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 11 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 11 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 11 | ▶ | use HTML::Scrubber; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | =head1 NAME | ||||||||||
| 13 | |||||||||||
| 14 | Perlanet::Trait::Scrubber - clean posts with HTML::Scrubber before aggregating | ||||||||||
| 15 | |||||||||||
| 16 | =head1 DESCRIPTION | ||||||||||
| 17 | |||||||||||
| 18 | Before adding a post to the aggregated feed, it will first be cleaned with | ||||||||||
| 19 | L<HTML::Scrubber>. | ||||||||||
| 20 | |||||||||||
| 21 | =head1 ATTRIBUTES | ||||||||||
| 22 | |||||||||||
| 23 | =head1 scrubber | ||||||||||
| 24 | |||||||||||
| 25 | An instance of L<HTML::Scrubber> used to remove unwanted content from | ||||||||||
| 26 | the feed entries. For default settings see source of Perlanet.pm. | ||||||||||
| 27 | |||||||||||
| 28 | =cut | ||||||||||
| 29 | |||||||||||
| 30 | 11 | has 'scrubber' => ( | |||||||||
| 31 | is => 'ro', | ||||||||||
| 32 | lazy_build => 1 | ||||||||||
| 33 | ); | ||||||||||
| 34 | |||||||||||
| 35 | 4 | ▶ | sub _build_scrubber { | ||||||||
|
Subroutine
_build_scrubber: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 4 | my $self = shift; | |||||||||
| 37 | |||||||||||
| 38 | 4 | my %scrub_rules = ( | |||||||||
| 39 | img => { | ||||||||||
| 40 | src => qr{^https?://}, # only URL with http:// | ||||||||||
| 41 | alt => 1, # alt attributes allowed | ||||||||||
| 42 | align => 1, # allow align on images | ||||||||||
| 43 | style => 1, | ||||||||||
| 44 | width => 1, | ||||||||||
| 45 | height => 1, | ||||||||||
| 46 | '*' => 0, # deny all others | ||||||||||
| 47 | }, | ||||||||||
| 48 | style => 0, | ||||||||||
| 49 | script => 0, | ||||||||||
| 50 | span => { | ||||||||||
| 51 | id => 0, # blogger(?) includes spans with id attribute | ||||||||||
| 52 | }, | ||||||||||
| 53 | a => { | ||||||||||
| 54 | href => 1, | ||||||||||
| 55 | '*' => 0, | ||||||||||
| 56 | }, | ||||||||||
| 57 | iframe => { | ||||||||||
| 58 | frameborder => 0, | ||||||||||
| 59 | scrolling => 0, | ||||||||||
| 60 | seamless => 0, | ||||||||||
| 61 | '*' => 1, | ||||||||||
| 62 | }, | ||||||||||
| 63 | centre => 0, | ||||||||||
| 64 | ); | ||||||||||
| 65 | |||||||||||
| 66 | # Definitions for HTML::Scrub | ||||||||||
| 67 | 4 | my %scrub_def = ( | |||||||||
| 68 | '*' => 1, | ||||||||||
| 69 | 'href' => qr{^(?!(?:java)?script)}i, | ||||||||||
| 70 | 'src' => qr{^(?!(?:java)?script)}i, | ||||||||||
| 71 | 'cite' => '(?i-xsm:^(?!(?:java)?script))', | ||||||||||
| 72 | 'language' => 0, | ||||||||||
| 73 | 'name' => 1, | ||||||||||
| 74 | 'value' => 1, | ||||||||||
| 75 | 'onblur' => 0, | ||||||||||
| 76 | 'onchange' => 0, | ||||||||||
| 77 | 'onclick' => 0, | ||||||||||
| 78 | 'ondblclick' => 0, | ||||||||||
| 79 | 'onerror' => 0, | ||||||||||
| 80 | 'onfocus' => 0, | ||||||||||
| 81 | 'onkeydown' => 0, | ||||||||||
| 82 | 'onkeypress' => 0, | ||||||||||
| 83 | 'onkeyup' => 0, | ||||||||||
| 84 | 'onload' => 0, | ||||||||||
| 85 | 'onmousedown' => 0, | ||||||||||
| 86 | 'onmousemove' => 0, | ||||||||||
| 87 | 'onmouseout' => 0, | ||||||||||
| 88 | 'onmouseover' => 0, | ||||||||||
| 89 | 'onmouseup' => 0, | ||||||||||
| 90 | 'onreset' => 0, | ||||||||||
| 91 | 'onselect' => 0, | ||||||||||
| 92 | 'onsubmit' => 0, | ||||||||||
| 93 | 'onunload' => 0, | ||||||||||
| 94 | 'src' => 1, | ||||||||||
| 95 | 'type' => 1, | ||||||||||
| 96 | 'style' => 1, | ||||||||||
| 97 | 'class' => 0, | ||||||||||
| 98 | 'id' => 0, | ||||||||||
| 99 | 'frameborder' => 0, | ||||||||||
| 100 | 'border' => 0, | ||||||||||
| 101 | ); | ||||||||||
| 102 | |||||||||||
| 103 | 4 | my $scrub = HTML::Scrubber->new; | |||||||||
| 104 | 4 | $scrub->rules(%scrub_rules); | |||||||||
| 105 | 4 | $scrub->default(1, \%scrub_def); | |||||||||
| 106 | 4 | $scrub->script(0); | |||||||||
| 107 | |||||||||||
| 108 | 4 | return $scrub; | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | 7 | ▶ | around 'clean_html' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||
| 112 | 7 | my $orig = shift; | |||||||||
| 113 | 7 | my $self = shift; | |||||||||
| 114 | 7 | my ($html) = @_; | |||||||||
| 115 | |||||||||||
| 116 | 7 | ▶ | warn __PACKAGE__, '::clean_html' if $ENV{PERLANET_DEBUG}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | |||||||||||
| 118 | 7 | $html = $self->$orig($html); | |||||||||
| 119 | 7 | my $scrubbed = $self->scrubber->scrub($html); | |||||||||
| 120 | 7 | return $scrubbed; | |||||||||
| 121 | 11 | }; | |||||||||
| 122 | |||||||||||
| 123 | =head1 AUTHOR | ||||||||||
| 124 | |||||||||||
| 125 | Dave Cross, <dave@perlhacks.com> | ||||||||||
| 126 | |||||||||||
| 127 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 128 | |||||||||||
| 129 | Copyright (c) 2010 by Magnum Solutions Ltd. | ||||||||||
| 130 | |||||||||||
| 131 | This library is free software; you can redistribute it and/or modify | ||||||||||
| 132 | it under the same terms as Perl itself, either Perl version 5.10.0 or, | ||||||||||
| 133 | at your option, any later version of Perl 5 you may have available. | ||||||||||
| 134 | |||||||||||
| 135 | =cut | ||||||||||
| 136 | |||||||||||
| 137 | 11 | 1; | |||||||||