| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Kwiki::URLBL; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 28353 | use strict; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 43 |  | 
| 4 | 1 |  |  | 1 |  | 6 | use vars qw($VERSION); | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 81 |  | 
| 5 |  |  |  |  |  |  | $VERSION = '0.02'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 406 | use Kwiki::Plugin -Base; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | use Kwiki::Installer -base; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our $VERSION = '0.05'; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | const class_id    => 'urlbl'; | 
| 13 |  |  |  |  |  |  | const class_title => 'URL Blacklist DNS'; | 
| 14 |  |  |  |  |  |  | const config_file => 'urlbl.yaml'; | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | sub register { | 
| 17 |  |  |  |  |  |  | require URI::Find; | 
| 18 |  |  |  |  |  |  | my $registry = shift; | 
| 19 |  |  |  |  |  |  | $registry->add(hook => 'edit:save', pre => 'urlbl_hook'); | 
| 20 |  |  |  |  |  |  | $registry->add(action => 'blacklisted_url'); | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub urlbl_hook { | 
| 24 |  |  |  |  |  |  | my $hook = pop; | 
| 25 |  |  |  |  |  |  | my $old_page = $self->hub->pages->new_page($self->pages->current->id); | 
| 26 |  |  |  |  |  |  | my $this     = $self->hub->urlbl; | 
| 27 |  |  |  |  |  |  | my @old_urls = $this->get_urls($old_page->content); | 
| 28 |  |  |  |  |  |  | my @urls     = $this->get_urls($self->cgi->page_content); | 
| 29 |  |  |  |  |  |  | my @new_urls = $this->get_new_urls(\@old_urls, \@urls); | 
| 30 |  |  |  |  |  |  | if (@new_urls && $this->is_blocked(\@new_urls)) { | 
| 31 |  |  |  |  |  |  | $hook->cancel(); | 
| 32 |  |  |  |  |  |  | return $self->redirect("action=blacklisted_url"); | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  | } | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | sub get_urls { | 
| 37 |  |  |  |  |  |  | require URI::Find; | 
| 38 |  |  |  |  |  |  | my ($content) = @_; | 
| 39 |  |  |  |  |  |  | my @list; | 
| 40 |  |  |  |  |  |  | my $finder = URI::Find->new( sub { | 
| 41 |  |  |  |  |  |  | my($uri, $orig_uri) = @_; | 
| 42 |  |  |  |  |  |  | push @list, $uri; | 
| 43 |  |  |  |  |  |  | return $orig_uri; | 
| 44 |  |  |  |  |  |  | }); | 
| 45 |  |  |  |  |  |  | $finder->find(\$content); | 
| 46 |  |  |  |  |  |  | return @list; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | sub get_new_urls { | 
| 50 |  |  |  |  |  |  | my ($old_urls, $urls) = @_; | 
| 51 |  |  |  |  |  |  | my @new_urls; | 
| 52 |  |  |  |  |  |  | my %old = map { $_ => 1 } @$old_urls; | 
| 53 |  |  |  |  |  |  | foreach my $url (@$urls) { | 
| 54 |  |  |  |  |  |  | push @new_urls, $url unless $old{$url}; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  | return @new_urls; | 
| 57 |  |  |  |  |  |  | } | 
| 58 |  |  |  |  |  |  |  | 
| 59 |  |  |  |  |  |  | sub is_blocked { | 
| 60 |  |  |  |  |  |  | require Net::DNS::Resolver; | 
| 61 |  |  |  |  |  |  | my ($new_urls) = @_; | 
| 62 |  |  |  |  |  |  | my @dnsbl = split /,\s*/, $self->config->urlbl_dns; | 
| 63 |  |  |  |  |  |  | my $res   = Net::DNS::Resolver->new; | 
| 64 |  |  |  |  |  |  | for my $url (@$new_urls) { | 
| 65 |  |  |  |  |  |  | my $uri = URI->new($url); | 
| 66 |  |  |  |  |  |  | my $domain = $uri->host; | 
| 67 |  |  |  |  |  |  | $domain =~ s/^www\.//; | 
| 68 |  |  |  |  |  |  | for my $dns (@dnsbl) { | 
| 69 |  |  |  |  |  |  | warn "looking up $domain.$dns"; | 
| 70 |  |  |  |  |  |  | my $q = $res->search("$domain.$dns"); | 
| 71 |  |  |  |  |  |  | return 1 if $q && $q->answer; | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  | return; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub blacklisted_url { | 
| 78 |  |  |  |  |  |  | return $self->render_screen( | 
| 79 |  |  |  |  |  |  | content_pane => 'blacklisted_url.html', | 
| 80 |  |  |  |  |  |  | ); | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | 1; | 
| 84 |  |  |  |  |  |  | __DATA__ |