# $Id: better_spam_protect.pl,v 1.3 2004/05/06 00:25:51 chip Exp $ # $Source: /home/chip/src/better_spam_protect/RCS/better_spam_protect.pl,v $ # # better_spam_protect.pl - Better spam protection plug-in for Movable Type. # # Chip Rosenthal # Unicom Systems Development # # # For documentation, see: http://www.unicom.com/chrome/a/000532.html # package plugins::better_spam_protect; use strict; use MT::Template::Context; use MT::Util qw( remove_html ); # # email_link() - Generate a mailto: hyperlink. # sub email_link { my($addr, $name, $spam_protect) = @_; if (!$spam_protect) { return qq[${name}]; } my @a = map { "'&#" . ord($_) . ";'" } split(//, "mailto:$addr"); my @n = map { "'&#" . ord($_) . ";'" } split(//, $name); (my $n1 = $name) =~ s!@! (at) !g; return q[]; } # # _hdlr_entry_author_link() - Override handler for <$MTEntryAuthorLink$> tag. # sub _hdlr_entry_author_link { my($ctx, $args) = @_; my $e = $ctx->stash('entry') or return $ctx->_no_entry_error('MT' . $ctx->stash('tag')); my $a = $e->author; return '' unless $a; my $name = $a->nickname || $a->name || ''; my $show_email = 1 unless exists $args->{show_email}; my $show_url = 1 unless exists $args->{show_url}; if ($show_url && $a->url) { return sprintf qq(%s), $a->url, $name; } elsif ($show_email && $a->email) { my $sp = ($args->{'spam_protect'} ne "0"); if ($sp) { # force sanitize off, it breaks javascript obfuscation $_[1]->{'sanitize'} = 0; } return email_link($a->email, $name, $sp); } else { return $name; } } # # _hdlr_comment_author_link() - Override handler for <$MTCommentAuthorLink$> # and <$MTCommentPreviewAuthorLink$> tags. # sub _hdlr_comment_author_link { my($ctx, $args) = @_; my $tag = $ctx->stash('tag'); my $c = $ctx->stash($tag =~ /Preview/ ? 'comment_preview' : 'comment'); my $show_email = 1 unless exists $args->{show_email}; my $show_url = 1 unless exists $args->{show_url}; if ($c && $show_email && $c->email && !($show_url && $c->url)) { my $sp = ($args->{'spam_protect'} ne "0"); if ($sp) { # force sanitize off, it breaks javascript obfuscation # we'll strip off HTML so the strings should be safe $_[1]->{'sanitize'} = 0; } return email_link(remove_html($c->email), remove_html($c->author), $sp); } # pass to standard handler for processing return MT::Template::Context::_hdlr_comment_author_link(@_); } # # _hdlr_email_link() - Handler for <$MTEmailLink$>. # sub _hdlr_email_link { my($ctx, $args) = @_; my $addr = $args->{'address'} or return $ctx->error('required "address" parameter is missing'); my $name = $args->{'name'} || $addr; my $sp = ($args->{'spam_protect'} ne "0"); return email_link(remove_html($addr), remove_html($name), $sp); } # # Install handlers that override the MT defaults. # MT::Template::Context->add_tag(EntryAuthorLink => \&_hdlr_entry_author_link); MT::Template::Context->add_tag(CommentAuthorLink => \&_hdlr_comment_author_link); MT::Template::Context->add_tag(CommentPreviewAuthorLink => \&_hdlr_comment_author_link); # # Create <$MTEmailLink$> tag. # MT::Template::Context->add_tag(EmailLink => \&_hdlr_email_link); 1;