⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 comments.pm

📁 1. 记录每个帖子的访问人情况
💻 PM
📖 第 1 页 / 共 3 页
字号:
                      -expires => '+1h');        $app->bake_cookie(%kookee);        my %name_kookee = (-name => 'commenter_name',                           -value => '',                           -path => '/',                           -expires => '+1h');        $app->bake_cookie(%name_kookee);        return 1;    } elsif ($q->param('sig')) {        my $session = undef;        my ($email, $name, $nick);        my $ts = $q->param('ts') || "";        $email = $q->param('email') || "";        $name = $q->param('name') || "";        $nick = $q->param('nick') || "";        my $sig_str = $q->param('sig');        my $cmntr;        if ($sig_str) {            if (!$app->_validate_signature($sig_str,                                            token => $weblog->effective_remote_auth_token,                                           email => decode_url($email),                                           name => decode_url($name),                                           nick => decode_url($nick),                                           ts => $ts))            {                # Signature didn't match, or timestamp was out of date.                # This implies tampering, not a user mistake.                return $app->error("The validation failed.");            }                        if ($weblog->require_comment_emails && !is_valid_email($email)) {                return $app->error("This weblog requires commenters to pass an email address. If you'd like to do so you may log in again, and give the authentication service permission to pass your email address.");            }            # Signature was valid, so create a session, etc.            $session = $app->_make_commenter_session($sig_str, $email,                                                     $name, $nick)                || return $app->error($app->errstr()                                      || "Couldn't save the session");            $cmntr = $app->_make_commenter(email => $email,                                           nickname => $nick,                                           name => $name);        } else {            # If there's no signature, then we trust the cookie.            my %cookies = $app->cookies();            if ($cookies{$COMMENTER_COOKIE_NAME}                && ($session = $cookies{$COMMENTER_COOKIE_NAME}->value()))             {                require MT::Session; require MT::Author;                my $sess = MT::Session->load({id => $session});                $cmntr = MT::Author->load({name => $sess->name,                                           type => MT::Author::COMMENTER});                if ($weblog->require_comment_emails                    && !is_valid_email($cmntr->email))                {                    return $app->error("This weblog requires commenters to pass an email address");                }            } else {            }        }        if ($q->param('sig') && !$cmntr) {            return $app->handle_error($app->errstr());        }        return $cmntr;    }}# This actually handles a UI-level sign-in or sign-out request.sub handle_sign_in {    my $app = shift;    my $q = $app->{query};    my $entry = MT::Entry->load($q->param('entry_id'));    my $weblog = MT::Blog->load($q->param('blog_id') || $entry->blog_id);    return $app->handle_error($app->translate("Sign in requires a secure signature; logout requires the logout=1 parameter"))         unless ($q->param('sig') || $q->param('logout'));        $app->_handle_sign_in($weblog)        || return $app->handle_error($app->errstr() ||                   $app->translate("The sign-in attempt was not successful; please try again."), 403);    my $target;    if ($q->param('static')) {        if ($q->param('static') eq 1) {            require MT::Entry;            my $entry = MT::Entry->load($q->param('entry_id'));            $target = $entry->archive_url;        } else {            $target = $q->param('static');        }    } else {        $target = ($app->{cfg}->CGIPath . $app->{cfg}->CommentScript                   . "?entry_id=" . $entry->id                   . ($q->param('arch') ? '&static=0&arch=1' : ''));    }     require MT::Util;    if ($q->param('logout')) {        return $app->redirect($app->{cfg}->SignOffURL . "&_return=" .                              MT::Util::encode_url($target),                              UseMeta => 1);    } else {        return $app->redirect($target, UseMeta => 1);    }}sub commenter_name_js {    local $SIG{__WARN__} = sub {};    my $app = shift;    my $commenter_name = $app->cookie_val('commenter_name');    $app->set_header('Cache-Control' => 'no-cache');    $app->set_header('Expires' => '-1');    return "var commenter_name = '$commenter_name';\n";}sub view {    my $app = shift;    my $q = $app->{query};    my %param = $app->param_hash();    my %overrides = ref($_[0]) ? %{$_[0]} : ();    @param{keys %overrides} = values %overrides;    my $cmntr;    my $session_key;    my $weblog = MT::Blog->load($q->param('blog_id'));    if ($q->param('logout')) {        return $app->handle_sign_in($weblog);    }    if ($q->param('sig')) {        $cmntr = $app->_handle_sign_in($weblog)            || return $app->handle_error($app->translate(                 "The sign-in validation was not successful. Please make sure your weblog is properly configured and try again."));        $cmntr = undef if $cmntr == 1;   # 1 is returned on logout.    } else {        ($session_key, $cmntr) = $app->_get_commenter_session();    }    require MT::Template;    require MT::Template::Context;    require MT::Entry;    my $entry_id = $q->param('entry_id')        or return $app->error("No entry_id");    my $entry = MT::Entry->load($entry_id)        or return $app->error($app->translate(            "No such entry ID '[_1]'", $entry_id));    return $app->error($app->translate(            "No such entry ID '[_1]'", $entry_id))        if $entry->status != RELEASE;    require MT::Blog;    my $blog = MT::Blog->load($entry->blog_id);    if ($cmntr) {        if (!$blog->manual_approve_commenters &&            ($cmntr->status($entry->blog_id) == PENDING))        {            $cmntr->approve($entry->blog_id);        }    }    my $ctx = MT::Template::Context->new;    $ctx->stash('entry', $entry);    $ctx->stash('commenter', $cmntr) if ($cmntr);    $ctx->{current_timestamp} = $entry->created_on;    my %cond = (        EntryIfExtended => $entry->text_more ? 1 : 0,        EntryIfAllowComments => $entry->allow_comments,        EntryIfCommentsOpen => $entry->allow_comments eq '1',        EntryIfAllowPings => $entry->allow_pings,        IfAllowCommentHTML => $blog->allow_comment_html,        IfRegistrationRequired => !$blog->allow_unreg_comments(),        IfCommentsAllowed => $blog->allow_reg_comments                               || $blog->allow_unreg_comments,        IfDynamicCommentsStaticPage => 0,        IfDynamicComments => MT::ConfigMgr->instance()->DynamicComments,                # We suppress IfDynamicComments because we are already                 # AT the dynamic comment page; a bit of a hack        IfCommenterPending => $cmntr && ($cmntr->status($blog->id) == PENDING),        IfNeedEmail => $blog->require_comment_emails,    );    my $tmpl = ($q->param('arch')) ?        (MT::Template->load({ type => 'individual',                                        blog_id => $entry->blog_id })            or return $app->error($app->translate(                 "You must define an Individual template in order to " .                 "display dynamic comments.")))    :        (MT::Template->load({ type => 'comments',                                        blog_id => $entry->blog_id })            or return $app->error($app->translate(                 "You must define a Comment Listing template in order to " .                                                  "display dynamic comments.")));    my $html = $tmpl->build($ctx, \%cond);    $html = MT::Util::encode_html($tmpl->errstr) unless defined $html;    $html;}sub handle_error {    my $app = shift;    my($err, $status_line) = @_;    my $html = do_preview($app, $app->{query}, $err)        || return "An error occurred: " . $err;    $app->{status_line} = $status_line;    $html;}sub do_preview {    my($app, $q, $err) = @_;    require MT::Template;    require MT::Template::Context;    require MT::Entry;    require MT::Util;    require MT::Comment;    my $entry_id = $q->param('entry_id')         || return $app->error($app->translate('No entry was specified; perhaps there is a template problem?'));    my $entry = MT::Entry->load($entry_id)        || return $app->error($app->translate("Somehow, the entry you tried to comment on does not exist"));    my $ctx = MT::Template::Context->new;    my ($comment, $commenter) = $app->_make_comment($entry);    return "An error occurred: " . $app->errstr() unless $comment;    ## Set timestamp as we would usually do in ObjectDriver.    my @ts = MT::Util::offset_time_list(time, $entry->blog_id);    my $ts = sprintf "%04d%02d%02d%02d%02d%02d",        $ts[5]+1900, $ts[4]+1, @ts[3,2,1,0];    $comment->created_on($ts);    $ctx->stash('comment_preview', $comment);    unless ($err) {        ## Serialize comment state, then hex-encode it.        require MT::Serialize;        my $ser = MT::Serialize->new($app->{cfg}->Serializer);        my $state = $comment->column_values;        $state->{static} = $q->param('static');        $ctx->stash('comment_state', unpack 'H*', $ser->serialize(\$state));    }    $ctx->stash('comment_is_static', $q->param('static'));    $ctx->stash('entry', $entry);    $ctx->{current_timestamp} = $ts;    $ctx->stash('commenter', $commenter);    my($tmpl);    $err ||= '';    if ($err eq 'pending') {        $tmpl = MT::Template->load({ type => 'comment_pending',                                     blog_id => $entry->blog_id })        or return $app->error($app->translate(            "You must define a Comment Pending template."));    } elsif ($err) {        $ctx->stash('error_message', $err);        $tmpl = MT::Template->load({ type => 'comment_error',                                     blog_id => $entry->blog_id })        or return $app->error($app->translate(            "You must define a Comment Error template."));    } else {        $tmpl = MT::Template->load({ type => 'comment_preview',                                     blog_id => $entry->blog_id })        or return $app->error($app->translate(            "You must define a Comment Preview template."));    }    require MT::Blog;    my $blog = MT::Blog->load($entry->blog_id);    my %cond = (IfAllowCommentHTML => $blog->allow_comment_html,                IfRegistrationRequired => !$blog->allow_unreg_comments,                IfCommentsAllowed => $blog->allow_reg_comments                                       || $blog->allow_unreg_comments,                IfNeedEmail => $blog->require_comment_emails);    my $html = $tmpl->build($ctx, \%cond);    $html = $tmpl->errstr unless defined $html;    $html;}1;__END__=head1 NAMEMT::App::Comments=head1 SYNOPSISThe application-level callbacks of the C<MT::App::Comments> applicationare documented here.=head1 CALLBACKS=over 4=item CommentThrottleFilterCalled as soon as a new comment has been received. The callback mustreturn a boolean value. If the return value is false, the incomingcomment data will be discarded and the app will output an error pageabout throttling. A CommentThrottleFilter callback has the followingsignature:    sub comment_throttle_filter($eh, $app, $entry)    {        ...    }I<$app> is the C<MT::App::Comments> object, whose interface is documentedin L<MT::App::Comments>, and I<$entry> is the entry on which thecomment is to be placed.Note that no comment object is passed, because it has not yet beenbuilt. As such, this callback can be used to tell the application toexit early from a comment attempt, before much processing takes place.When more than one CommentThrottleFilter is installed, the data isdiscarded unless all callbacks return true.=item CommentFilterCalled once the comment object has been constructed, but before savingit. If any CommentFilter callback returns false, the comment will notbe saved. The callback has the following signature:    sub comment_filter($eh, $app, $comment)    {        ...    }=back

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -