Changeset 203 for AutoIPBan/trunk

Show
Ignore:
Timestamp:
05/14/06 02:55:15 (3 years ago)
Author:
ogawa
Message:

Add MTIPBanList container tag and MTIPBanListIP tag to be able to access IPBanList from MT Templates.
Add context menu for adding hosts to IPBanList.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • AutoIPBan/trunk/AutoIPBan.pl

    r201 r203  
    1212use strict; 
    1313use MT; 
     14use MT::Template::Context; 
    1415use base 'MT::Plugin'; 
    1516 
     
    1718 
    1819sub BEGIN { 
    19     $VERSION = 0.01; 
     20    $VERSION = 0.02; 
    2021    my $plugin = __PACKAGE__->new({ 
    2122        name => 'AutoIPBan', 
     
    2829    MT->add_plugin($plugin); 
    2930    MT->add_callback('TBPingThrottleFilter', 1, $plugin, \&tbping_auto_ipban); 
     31    MT::Template::Context->add_container_tag(IPBanList => \&ipbanlist); 
     32    MT::Template::Context->add_tag(IPBanListIP => \&ipbanlist_ip); 
     33} 
     34 
     35sub init_app { 
     36    my ($plugin, $app) = @_; 
     37    return unless $app->isa('MT::App::CMS'); 
     38    $app->add_itemset_action({ 
     39        type => 'ping', 
     40        key => 'add_to_ipbanlist_ping', 
     41        label => 'Add To IPBanList', 
     42        code => \&add_to_ipbanlist_ping 
     43        }); 
     44    $app->add_itemset_action({ 
     45        type => 'comment', 
     46        key => 'add_to_ipbanlist_comment', 
     47        label => 'Add To IPBanList', 
     48        code => \&add_to_ipbanlist_comment 
     49        }); 
    3050} 
    3151 
     
    4868                                  { range => { created_on => 1 } }); 
    4969    if ($count >= $app->config('OneHourMaxPings')) { 
    50         _add_ipbanlist($blog_id, $ip); 
     70        _add_to_ipbanlist($blog_id, $ip); 
    5171        return 0; 
    5272    } 
     
    6080                               { range => { created_on => 1 } }); 
    6181    if ($count >= $app->config('OneDayMaxPings')) { 
    62         _add_ipbanlist($blog_id, $ip); 
     82        _add_to_ipbanlist($blog_id, $ip); 
    6383        return 0; 
    6484    } 
     
    6686} 
    6787 
    68 sub _add_ipbanlist { 
     88sub add_to_ipbanlist_ping { 
     89    my ($app) = @_; 
     90    my @ids = $app->param('id') 
     91        or return $app->error("Need pings to add to IPBanList"); 
     92    for my $id (@ids) { 
     93        my $ping = MT::TBPing->load($id, { cache_ok => 1 }); 
     94        _add_to_ipbanlist($ping->blog_id, $ping->ip); 
     95    } 
     96    $app->call_return; 
     97} 
     98 
     99sub add_to_ipbanlist_comment { 
     100    my ($app) = @_; 
     101    my @ids = $app->param('id') 
     102        or return $app->error("Need comments to add to IPBanList"); 
     103    for my $id (@ids) { 
     104        my $comment = MT::Comment->load($id, { cache_ok => 1 }); 
     105        _add_to_ipbanlist($comment->blog_id, $comment->ip); 
     106    } 
     107    $app->call_return; 
     108} 
     109 
     110sub _add_to_ipbanlist { 
    69111    my ($blog_id, $ip) = @_; 
    70112    unless (MT::IPBanList->load({ blog_id => $blog_id, ip => $ip })) { 
     
    76118} 
    77119 
     120sub ipbanlist { 
     121    my ($ctx, $args) = @_; 
     122    my @blog_ids = defined $args->{blog_id} ? 
     123        split /\s*,\s*/, $args->{blog_id} : [ $ctx->stash('blog_id') ]; 
     124    my %ips; 
     125    for my $blog_id (@blog_ids) { 
     126        my @list = MT::IPBanList->load({ blog_id => $blog_id }); 
     127        %ips = map { $_->ip => 1 } @list; 
     128    } 
     129    my @res; 
     130    my $builder = $ctx->stash('builder'); 
     131    my $tokens = $ctx->stash('tokens'); 
     132    for my $ip (keys %ips) { 
     133        local $ctx->{__stash}{'ipbanlist_ip'} = $ip; 
     134        defined(my $out = $builder->build($ctx, $tokens)) 
     135            or return $ctx->error($ctx->errstr); 
     136        push @res, $out; 
     137    } 
     138    my $glue = $args->{glue} || ''; 
     139    join $glue, @res; 
     140} 
     141 
     142sub ipbanlist_ip { 
     143    $_[0]->stash('ipbanlist_ip') || ''; 
     144} 
     145 
    781461;