# Copyright (C) 2004 Kenta Yoshimura. All Rights Reserved.
# http://yoffy.dyndns.org/
package MT::Plugin::MT2ch;
use MT::Template::Context;
use MT::Util qw( remove_html spam_protect );
use MT::Comment;
use MT::ConfigMgr;
use MT::ErrorHandler;
MT::Template::Context->add_tag( EntryCommentCount2ch => \&entry_comment_count_2ch );
MT::Template::Context->add_tag( CommentOrderNumber2ch => \&comment_order_number_2ch );
MT::Template::Context->add_tag( CommentAuthor2ch => \&comment_author_2ch );
MT::Template::Context->add_tag( CommentAuthorLink2ch => \&comment_author_link_2ch );
MT::Template::Context->add_tag( CommentPreviewAuthor2ch => \&comment_author_2ch );
MT::Template::Context->add_tag( CommentPreviewAuthorLink2ch => \&comment_author_link_2ch );
MT::Template::Context->add_tag( CommentID2ch => \&comment_id_2ch );
MT::Template::Context->add_tag( CommentPreviewID2ch => \&comment_id_2ch );
MT::Template::Context->add_global_filter(no_return => sub {
my $str = shift;
$str =~ s/[\n\r]//g;
return $str;
});
sub entry_comment_count_2ch {
my $e = $_[0]->stash('entry')
or return $_[0]->_no_entry_error('MTEntryCommentCount');
($e->comment_count) + 1;
}
sub comment_order_number_2ch { ($_[0]->stash('comment_order_num')) + 1 }
sub sanitize_on {
$_[0]->{'sanitize'} = 1 unless exists $_[0]->{'sanitize'};
}
sub _name2ch {
# my $ctx = shift;
# my $comment = $ctx->stash( 'comment' );
my $comment = $_[ 0 ];
my $gen = "GHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./0123456789ABCDEFGABCDEFGHIJKLMNOPQRSTUVWXYZabcdefabcdefghijklmnopqrstuvwxyz./012";
my $name = $comment->author;
my $found = index( $name, '#' );
my $i;
if ( $found < 0 )
{
return $name;
}
my $b = length( $name );
my $key = '';
my $salt = '';
my $pass;
for ( $i = $found + 1; $i < $b; $i++ )
{
my $ch = ord( substr( $name, $i, 1 ) ) & 0x7f;
$key = $key . substr( $gen, $ch, 1 );
}
$key = $key . "H.";
$salt = substr( $key, 1, 2 );
$pass = crypt( $key, $salt );
$b = length( $pass );
return substr( $name, 0, $found + 1 ) . substr( $pass, $b - 10 );
}
# from lib/MT/Template/Context.pm _hdlr_comment_author
sub comment_author_2ch {
sanitize_on($_[1]);
my $tag = $_[0]->stash('tag');
my $c = $_[0]->stash($tag =~ /Preview/ ? 'comment_preview' : 'comment')
or return $_[0]->_no_comment_error('MT' . $tag);
# my $a = defined $c->author ? $c->author : '';
my $a = _name2ch( $c );
$a = '' unless defined $a;
remove_html($a);
}
# from lib/MT/Template/Context.pm _hdlr_comment_author_link
sub comment_author_link_2ch {
sanitize_on($_[1]);
my($ctx, $args) = @_;
my $tag = $ctx->stash('tag');
my $c = $ctx->stash($tag =~ /Preview/ ? 'comment_preview' : 'comment')
or return $ctx->_no_comment_error('MT' . $tag);
# my $name = $c->author;
my $name = _name2ch( $c );
$name = '' unless defined $name;
my $show_email = 1 unless exists $args->{show_email};
my $show_url = 1 unless exists $args->{show_url};
if ($show_url && $c->url) {
my $cgi_path = MT::ConfigMgr->instance->CGIPath;
$cgi_path =~ s#([^/])$#$1/#;
my $comment_script = MT::ConfigMgr->instance->CommentScript;
return sprintf(qq(%s),
$cgi_path, $comment_script, $c->id, $name);
} elsif ($show_email && $c->email) {
my $email = remove_html($c->email);
my $str = "mailto:" . $email;
$str = spam_protect($str) if $args->{'spam_protect'};
return sprintf qq(%s), $str, $name;
} else {
return $name;
}
}
sub comment_id_2ch {
my $ctx = shift;
my $tag = $ctx->stash( 'tag' );
my $comment = $ctx->stash( $tag =~ /Preview/ ? 'comment_preview' : 'comment' );
my $stamp = $comment->created_on;
my $gen = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
my $year = substr( $stamp, 0, 4 );
my $mon = substr( $stamp, 4, 2 );
my $mday = substr( $stamp, 6, 2 );
my $i = ($year * 365 + $mon * 30 + $mday) % 4096;
my $salt = substr($gen, $i / 64, 1) . substr($gen, $i % 64, 1);
my @ip = split( /\./, $comment->ip );
my $key = 0;
my $pass = '';
for ( $i = 0; $i < 4; $i++ )
{ $key = $key * 256 + $ip[ $i ]; }
$key = sprintf( "%x", $key );
$pass = crypt( $key, $salt );
$i = length( $pass );
return substr( $pass, $i - 8 );
}
1;