forked from bentglasstube/blosxom-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfeedback-v0i23
1815 lines (1432 loc) · 62.6 KB
/
feedback-v0i23
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Blosxom Plug-in: feedback
# Author: Frank Hecker (http://www.hecker.org/)
#
# Version 0.23
package feedback;
use warnings;
# --- Configurable variables ---
# --- You *must* set the following variables properly for your blog ---
# Where should I keep the feedback hierarchy?
# (By default it goes in the Blosxom state directory. However you may
# prefer it to go in the same directory as the Blosxom data directory.
# If so, delete the following line and uncomment the line following it.)
$fb_dir = "$blosxom::plugin_state_dir/feedback";
# $fb_dir = "$blosxom::datadir/../feedback";
# --- Set the following variables according to your preferences ---
# Are comments and TrackBacks allowed? Set to zero to disable either or both.
my $allow_comments = 1;
my $allow_trackbacks = 1;
# Don't allow comments/TrackBacks if story is older than this (in seconds).
# (Set to zero to keep story open for comments/TrackBacks forever.)
my $comment_period = 90 * 86400; # 90 days
my $trackback_period = 90 * 86400; # 90 days
# Do Akismet checking of comments and/or TrackBacks for spam.
my $akismet_comments = 0;
my $akismet_trackbacks = 0;
# WordPress API key for use with Akismet.
# (Register at <http://wordpress.com/> to get your own API key.)
my $wordpress_api_key = '';
# Do MT-blacklist checking of comments and/or TrackBacks for spam.
# NOTE: The MT-Blacklist file is no longer maintained; we suggest using
# Akismet instead.
my $blacklist_comments = 0;
my $blacklist_trackbacks = 0;
# Where can I find the local copy of the MT-Blacklist file?
my $blacklist_file = "$blosxom::plugin_state_dir/blacklist.txt";
# Send an email message to notify the blog owner of new comments and/or
# TrackBacks and (optionally) request approval of new comments/TrackBacks.
my $notify_comments = 0;
my $notify_trackbacks = 0;
my $moderate_comments = 1;
my $moderate_trackbacks = 1;
# Email address and SMTP server used for notifications and moderation requests.
my $address = '[email protected]';
my $smtp_server = 'smtp.example.com';
# Default values for fields not submitted with the comment or TrackBack ping.
my $default_name = "Someone";
my $default_blog_name = "An unnamed blog";
my $default_title = "an article";
# The formatting used for comments, i.e., how they are translated to (X)HTML.
# Valid choices at present are 'none', 'plaintext' and 'markdown'.
my $comment_format = 'plaintext';
# Should we accept and display commenter's email addresses? (The default is
# to support http/https URLs only; this may be the only option in future.)
my $allow_mailto = 0;
# --- You should not normally need to change the following variables ---
# What flavour should I consider an incoming TrackBack ping?
$trackback_flavour = "trackback";
# What file extension should I use for saved comments and TrackBacks?
my $fb_extension = "wb";
# What fields are used in the comments form?
my @comment_fields = qw! name url comment !;
# What fields are used by TrackBacks?
my @trackback_fields = qw! blog_name url title excerpt !;
# Limit all fields to this length or less (just in case).
my $max_param_length = 10000;
# --- Variables for use in flavour templates (e.g., as $feedback::foo) ---
# Comment and TrackBack fields, for use in the comment, preview, and
# trackback templates.
$name = '';
$name_link = ''; # Combines name and link for email/URL
$date = '';
$comment = '';
$blog_name = '';
$title = '';
$title_link = ''; # Combines title and link to article
$excerpt = '';
$url = ''; # Also used in $name_link, $title_link
# Field values for previewed comments, used in the commentform template.
$name_preview = '';
$comment_preview = '';
$url_preview = '';
# Message displayed in response to a comment submission (e.g., to display
# an error message), for use in the story or foot templates. The response is
# formatted for use in HTML/XHTML content.
$comment_response = '';
# XML message displayed in response to a TrackBack ping (e.g., to display
# an error message or indicate success), per the TrackBack Technical
# Specification <http://www.sixapart.com/pronet/docs/trackback_spec>.
$trackback_response = '';
# All comments and TrackBacks for a particular story, for use in the story
# template for an individual story page. Also includes content from the
# comments_head/comments_foot and trackbacks_head/trackbacks_foot templates.
$comments = '';
$trackbacks = '';
# Counts of comments and TrackBacks for a story, for use in the story
# template (e.g., for index and archive pages).
$comments_count = 0;
$trackbacks_count = 0;
$count = 0; # total of both
# Previewed comment for a particular story, for use in the story
# template for an individual story page.
$preview = '';
# Default comment submission form, for use in the foot template (for an
# individual story page). The plug-in sets this value to null if comments
# are disabled or in cases where the page is not for an individual story
# or the story is older than the allowed comment period.
$commentform = '';
# TrackBack discovery information, for use in the foot template (for
# an individual story page). The code sets this value to null if TrackBacks
# are disabled or in cases where the page is not for an individual story
# or the story is older than the allowed TrackBack period.
$trackbackinfo = '';
# --- External modules required ---
use CGI qw/:standard/;
use FileHandle;
use URI;
use URI::Escape;
# --- Global variables (used in interpolation) ---
use vars qw! $fb_dir $trackback_flavour $name $name_link $date $comment
$blog_name $title $name_preview $comment_preview $url_preview
$comment_response $trackback_response $comments $trackbacks
$comments_count $trackbacks_count $count $preview $commentform
$trackbackinfo !;
# --- Private static variables ---
# Spam blacklist array.
my @blacklist_entries = ();
# File handle for use in reading/writing the feedback file, etc.
my $fh = new FileHandle;
# Path and filename for the main feedback file for a story, and item name
# used in contructing filenames for files containing moderated items.
my $fb_path = '';
my $fb_fn = '';
# Whether comments or TrackBacks are closed for a given story.
my $closed_comments = 0;
my $closed_trackbacks = 0;
# --- Plug-in initialization ---
# Strip potentially confounding final slash from feedback directory path.
$fb_dir =~ s!/$!!;
# Strip potentially confounding initial period from file extension.
$fb_extension =~ s!^\.!!;
# Initialize the default templates; use $blosxom::template so we can leverage
# the Blosxom template subroutine (whether default or replaced by a plug-in).
my %template = ();
while (<DATA>) {
last if /^(__END__)?$/;
# TODO: Fix this to correctly handle empty flavours (i.e., no $txt).
my ($ct, $comp, $txt) = /^(\S+)\s(\S+)(?:\s(.*))?$/;
# my ($ct, $comp, $txt) = /^(\S+)\s(\S+)\s(.*)$/;
$txt = '' unless defined($txt);
$txt =~ s/\\n/\n/mg;
$blosxom::template{$ct}{$comp} = $txt;
}
# Moderation implies notification.
$notify_comments = 1 if $moderate_comments;
$notify_trackbacks = 1 if $moderate_trackbacks;
# --- Plug-in subroutines ---
# Create feedback directory if needed.
sub start {
# The $fb_dir variable must be set to activate feedback.
unless ($fb_dir) {
warn "feedback: " .
"The \$fb_dir configurable variable is not set; "
. "please set it to enable comments or TrackBacks.\n";
return 0;
}
# The value of $fb_dir must be a writeable directory.
if (-e $fb_dir && !(-d $fb_dir && -w $fb_dir)) {
warn "feedback: The feedback directory '$fb_dir' "
. "must be a writeable directory; please rename or remove it "
. "and Blosxom will create it properly for you.\n";
return 0;
}
# The $fb_dir does not yet exist, so Blosxom will create it.
unless (-e $fb_dir) {
return 0 unless (mk_feedback_dir($fb_dir));
}
return 1;
}
# Decide whether to close comments and TrackBacks for a story.
sub date {
my ($pkg, $file, $date_ref, $mtime, $dw, $mo, $mo_num, $da, $ti, $yr) = @_;
# A positive value of $comment_period represents the time in seconds
# during which posting comments or TrackBacks is allowed after a
# story has been published. (Note that updating a story has the effect
# of reopening the feedback period.) A zero or negative value for
# $comment_period means that posting feedback is always allowed.
if ($comment_period <= 0) {
$closed_comments = 0;
} elsif ($allow_comments && (time - $mtime) > $comment_period) {
$closed_comments = 1;
} else {
$closed_comments = 0;
}
# $trackback_period works the same way as $comment_period.
if ($trackback_period <= 0) {
$closed_trackbacks = 0;
} elsif ($allow_trackbacks && (time - $mtime) > $trackback_period) {
$closed_trackbacks = 1;
} else {
$closed_trackbacks = 0;
}
return 1;
}
# Parse posted TrackBacks and comments and take action as appropriate.
# Retrieve comments and TrackBacks and format according to the templates.
# Display a comment form and/or TrackBack URL as appropriate.
sub story {
my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
my $submission_type;
my $status_msg;
my $is_story_page;
# We have five possible tasks in this subroutine:
#
# * handle submitted TrackBack pings or comments (or related requests)
# * display previously-submitted TrackBacks or comments
# * display a comment being previewed
# * display a form for entering a comment (or editing a previewed one)
# * display information about submitting TrackBacks
#
# Exactly what we do depends whether we are rendering dynamically or
# statically and on the type of request (GET, HEAD, or POST) (when
# dynamically rendering), the Blosxom flavour, the parameters associated
# with the request, the age of the story, and the way the feedback
# plug-in itself is configured.
# Make $path empty if at top level, preceded by a single slash otherwise.
!defined($path) and $path = "";
$path =~ s!^/*!!; $path &&= "/$path";
# Set feedback path and filename for this story.
$fb_path = $path;
$fb_fn = $filename . '.' . $fb_extension;
# Determine whether this is an individual story page or not.
$is_story_page =
$blosxom::path_info =~ m!^(.*/)?(.+)\.(.+)$! ? 1 : 0;
# For dynamic rendering of an individual story page *only*, check to
# see if this is a feedback-related request, take action, and formulate
# a response.
#
# We have five possible cases: TrackBack ping, comment preview, comment
# post, moderator approval, and moderator rejection. These are
# distinguished based on the type of request (POST vs. GET/HEAD),
# the flavour (for TrackBack pings only), and the request parameters.
$submission_type = $comment_response = $trackback_response = '';
if ($blosxom::static_or_dynamic eq 'dynamic' && $is_story_page) {
($submission_type, $status_msg) = process_submission();
if ($submission_type eq 'trackback') {
$trackback_response = format_tb_response($status_msg);
return 1; # All done.
} elsif ($submission_type eq 'comment'
|| $submission_type eq 'preview'
|| $submission_type eq 'approve'
|| $submission_type eq 'reject') {
$comment_response = format_cmt_response($status_msg);
}
}
# Display previously-submitted comments and TrackBacks for this story.
# For index and and archive pages we just display counts of the comments
# and TrackBacks.
$comments = $trackbacks = '';
$comments_count = $trackbacks_count = 0;
if ($is_story_page) {
($comments, $comments_count, $trackbacks, $trackbacks_count) =
get_feedback($path);
} else {
($comments_count, $trackbacks_count) = count_feedback();
}
$count = $comments_count + $trackbacks_count;
# If we are previewing a comment then format the comment for display.
$preview = '';
if ($submission_type eq 'preview') {
$preview = get_preview($path);
}
# Display a form for comment submission, if we are on an individual
# story page and comments are (still) allowed. (If we are previewing
# a comment then the form will be pre-filled as appropriate.)
$commentform = '';
if ($is_story_page && $allow_comments) {
if ($closed_comments) {
$commentform =
"<p class=\"commentform\">"
. "Comments are closed for this story.</p>";
} else {
# Get the commentform template and interpolate variables in it.
$commentform =
&$blosxom::template($path,'commentform',$blosxom::flavour)
|| &$blosxom::template($path,'commentform','general');
$commentform = &$blosxom::interpolate($commentform);
}
}
# Display information on submitting TrackPack pings (including code for
# TrackBack autodiscovery), if we are on an individual story page and
# TrackBacks are (still) allowed.
$trackbackinfo = '';
if ($is_story_page && $allow_trackbacks) {
if ($closed_trackbacks) {
$trackbackinfo =
"<p class=\"trackbackinfo\">"
. "Trackbacks are closed for this story.</p>";
} else {
# Get the trackbackinfo template and interpolate variables in it.
$trackbackinfo =
&$blosxom::template($path,'trackbackinfo',$blosxom::flavour)
|| &$blosxom::template($path,'trackbackinfo','general');
$trackbackinfo = &$blosxom::interpolate($trackbackinfo);
}
}
# For interpolate_fancy to work properly when deciding whether to include
# certain content or not, the associated variables must be undefined if
# there is no actual content to be displayed.
$comment_response =~ m!^\s*$! and $comment_response = undef;
$comments =~ m!^\s*$! and $comments = undef;
$trackbacks =~ m!^\s*$! and $trackbacks = undef;
$preview =~ m!^\s*$! and $preview = undef;
$commentform =~ m!^\s*$! and $commentform = undef;
$trackbackinfo =~ m!^\s*$! and $trackbackinfo = undef;
return 1;
}
# --- Helper subroutines ---
# Process a submitted HTTP request and take whatever action is appropriate.
# Returns the type of submission: 'trackback', 'comment', 'preview',
# 'approve', 'reject', or null for a request not related to feedback.
# Also sets $comment_response and $trackback_response;
sub process_submission {
my $submission_type = '';
my $status_msg = '';
if (request_method() eq 'POST') {
# We have two possible cases: a TrackBack ping (identified by
# the flavour extension) or a submitted comment.
if ($blosxom::flavour eq $trackback_flavour) {
$status_msg = handle_feedback('trackback');
$submission_type = 'trackback';
} else {
# Comment posts may or may not use a particular flavour
# extension, so we check for the value of the 'plugin'
# hidden field (from the comment form).
my $plugin_param = sanitize_param(param('plugin'));
if ($plugin_param eq 'writeback') {
# Comment previews are distinguished from comment posts
# by the value of the 'submit' parameter associated with
# the 'Post' and 'Preview' form buttons.
my $submit_param = sanitize_param(param('submit'));
$status_msg = '';
if ($submit_param eq 'Preview') {
$status_msg = handle_feedback('preview');
$submission_type = 'preview';
} elsif ($submit_param eq 'Post') {
$status_msg = handle_feedback('comment');
$submission_type = 'comment';
} else {
$status_msg = "The submit parameter must have the value "
. "'Preview' or 'Post'";
}
}
}
} elsif (request_method() eq 'GET' || request_method() eq 'HEAD') {
my $moderate_param = sanitize_param(param('moderate'));
my $feedback_param = sanitize_param(param('feedback'));
if ($moderate_param) {
# We have two possible cases: moderator approval or rejection,
# distinguished based on the value of the 'moderate' parameter.
if (!$feedback_param) {
$status_msg =
"You must provide a 'feedback' parameter and item.";
} elsif ($moderate_param eq 'approve') {
$status_msg = approve_feedback($feedback_param);
$submission_type = 'approve';
} elsif ($moderate_param eq 'reject') {
$status_msg = reject_feedback($feedback_param);
$submission_type = 'reject';
} else {
$status_msg =
"'moderate' parameter must "
. "have the value 'approve' or 'reject'.";
}
}
}
return $submission_type, $status_msg;
}
# Retrieve comments and TrackBacks for a story and format them according
# to the appropriate templates for the story (based on the story's path).
# For comments we use the comment template for each individual comment,
# along with the optional comments_head and comments_foot templates (before
# and after the comments proper). For TrackBacks we use the corresponding
# trackback template for each TrackBack, together with the optional
# trackbacks_head and trackbacks_foot templates.
sub get_feedback {
my $path = shift;
my ($comments, $comments_count, $trackbacks, $trackbacks_count);
$comments = $trackbacks = '';
$comments_count = $trackbacks_count = 0;
# Retrieve the templates for individual comments and TrackBacks.
my $comment_template =
&$blosxom::template($path, 'comment', $blosxom::flavour)
|| &$blosxom::template($path, 'comment', 'general');
my $trackback_template =
&$blosxom::template($path, 'trackback', $blosxom::flavour)
|| &$blosxom::template($path, 'trackback', 'general');
# Open the feedback file (if it exists) and read any comments or
# TrackBacks. Note that we can distinguish comments from TrackBacks
# because comments have a 'comment' field and TrackBacks don't.
my %param = ();
if ($fh->open("$fb_dir$fb_path/$fb_fn")) {
foreach my $line (<$fh>) {
$line =~ /^(.+?): (.*)$/ and $param{$1} = $2;
if ( $line =~ /^-----$/ ) {
if ($param{'comment'}) {
$comment = format_comment($param{'comment'});
$date = format_date($param{'date'});
($name, $name_link) =
format_name($param{'name'}, $param{'url'});
my $cmt = $comment_template;
$cmt = &$blosxom::interpolate($cmt);
$comments .= $cmt;
$comments_count++;
} else {
$blog_name = format_blog_name($param{'blog_name'});
$excerpt = format_excerpt($param{'excerpt'});
$date = format_date($param{'date'});
($title, $title_link) =
format_title($param{'title'}, $param{'url'});
my $trackback = $trackback_template;
$trackback = &$blosxom::interpolate($trackback);
$trackbacks .= $trackback;
$trackbacks_count++;
}
%param = ();
}
}
}
return ($comments, $comments_count, $trackbacks, $trackbacks_count);
}
# Retrieve comments and TrackBacks for a story and (just) count them.
sub count_feedback {
my $comments_count = 0;
my $trackbacks_count = 0;
# Open the feedback file (if it exists) and count any comments or
# TrackBacks. Note that we can distinguish comments from TrackBacks
# because comments have a 'comment' field and TrackBacks don't.
my %param = ();
if ($fh->open("$fb_dir$fb_path/$fb_fn")) {
foreach my $line (<$fh>) {
$line =~ /^(.+?): (.*)$/ and $param{$1} = $2;
if ( $line =~ /^-----$/ ) {
if ($param{'comment'}) {
$comments_count++;
} else {
$trackbacks_count++;
}
%param = ();
}
}
}
return ($comments_count, $trackbacks_count);
}
# Format a previewed comment according to the appropriate preview template
# for the story (based on the story's path).
sub get_preview {
my $path = shift;
my $preview = '';
# Retrieve the comment template (also used for previewed comments).
my $comment_template =
&$blosxom::template($path, 'comment', $blosxom::flavour)
|| &$blosxom::template($path, 'comment', 'general');
# Format the previewed comment using the submitted values.
$comment = format_comment($comment_preview);
$date = format_date($date_preview);
($name, $name_link) =
format_name($name_preview, $url_preview);
$preview = &$blosxom::interpolate($comment_template);
return $preview;
}
# Create top-level directory to hold feedback files, and make it writeable.
sub mk_feedback_dir {
my $mkdir_r = mkdir("$fb_dir", 0755);
warn $mkdir_r
? "feedback: $fb_dir created.\n"
: "feedback: Could not create $fb_dir.\n";
$mkdir_r or return 0;
my $chmod_r = chmod 0755, $fb_dir;
warn $chmod_r
? "feedback: $fb_dir set to 0755 permissions.\n"
: "feedback: Could not set permissions on $fb_dir.\n";
$chmod_r or return 0;
warn "feedback: feedback is enabled!\n";
return 1;
}
# Create subdirectories of feedback directory as necessary.
sub mk_feedback_subdir {
my $dir = shift;
my $p = '';
return 1 if !defined($dir) or $dir eq '';
foreach (('', split /\//, $dir)) {
$p .= "/$_";
$p =~ s!^/!!;
return 0
unless (-d "$fb_dir/$p" or mkdir "$fb_dir/$p", 0755);
}
return 1;
}
# Process a submitted comment or TrackBack.
sub handle_feedback {
my $feedback_type = shift;
my $status_msg = '';
my $is_comment;
my $is_preview;
my $fb_item;
# Set up to handle either a comment, preview, or TrackBack as requested.
if ($feedback_type eq 'comment') {
$is_comment = 1;
$is_preview = 0;
} elsif ($feedback_type eq 'preview') {
$is_comment = 1;
$is_preview = 1;
} else {
$is_comment = 0;
$is_preview = 0;
}
my $allow = $is_comment ? $allow_comments : $allow_trackbacks;
my $closed = $is_comment ? $closed_comments : $closed_trackbacks;
my $period = $is_comment ? $comment_period : $trackback_period;
my $akismet = $is_comment ? $akismet_comments : $akismet_trackbacks;
my $blacklist = $is_comment ? $blacklist_comments : $blacklist_trackbacks;
my $notify = $is_comment ? $notify_comments : $notify_trackbacks;
my $moderate = $is_comment ? $moderate_comments : $moderate_trackbacks;
my @fields = $is_comment ? @comment_fields : @trackback_fields;
# Reject request if feedback is not (still) allowed.
unless ($allow && !$closed) {
if ($closed) {
$status_msg =
"This story is older than " . ($period/86400) . " days. "
. ($is_comment ? "Comments" : "TrackBacks")
. " have now been closed.";
} else {
$status_msg =
($is_comment ? "Comments" : "TrackBacks")
. " are not enabled for this site.";
}
return $status_msg;
}
# Filter out the "good" fields from the CGI parameters.
my %params = copy_params(\@fields);
# Comments must have (at least) a comment parameter, and TrackBacks a URL.
if ($is_comment) {
unless ($params{'comment'}) {
$status_msg =
"You didn't enter anything in the comment field.";
return $status_msg;
}
} elsif (!$params{'url'}) {
$status_msg = "No URL specified for the TrackBack";
return 0;
}
# Check feedback to see if it's spam.
if (is_spam(\%params, $is_comment, $akismet, $blacklist)) {
# If we are previewing a comment then we allow the poster a
# chance to revise the comment; otherwise we just reject it.
if ($is_preview) {
$status_msg =
"Your comment appears to be spam and will be rejected "
. "unless it is revised. ";
} else {
$status_msg =
"Your feedback was rejected because it appears to be spam; "
. "please contact the site administrator if you believe that "
. "your feedback was rejected in error.";
return $status_msg;
}
}
# If we are previewing a comment then just save the fields for later
# use in the previewed comment and (as prefilled values) in the comment
# form. Otherwise attempt to save the new feedback information, either
# into the permanent feedback file for this story (if no moderation) or
# into a temporary file (for later moderation).
if ($is_preview) {
$status_msg .= save_preview(\%params);
} else {
($fb_item, $status_msg) = save_feedback(\%params, $moderate);
return $status_msg unless $fb_item;
# Send a moderation message or notify blog owner of the new feedback.
if ($moderate || $notify) {
send_notification(\%params, $moderate, $fb_item);
}
}
return $status_msg;
}
# Make a "safe" copy of the CGI parameters based on the expected
# field names associated with either a comment or TrackBack.
sub copy_params {
my $fields_ref = shift;
my %params;
foreach my $key (@$fields_ref) {
my $value = substr(param($key), 0, $max_param_length) || "";
# Eliminate leading and trailing whitespace, use carriage returns
# as line delimiters, and collapse multiple blank lines into one.
$value =~ s/^\s+//;
$value =~ s/\s+$//;
$value =~ s/\r?\n\r?/\r/mg;
$value =~ s/\r\r\r*/\r\r/mg;
$params{$key} = $value;
}
return %params;
}
# Send notification or moderation email to blog owner.
sub send_notification {
my ($params_ref, $moderate, $fb_item) = @_;
unless ($address && $smtp_server) {
warn "feedback: No address or SMTP server for notifications\n";
return 0;
}
my $message = "New feedback for your post \"$blosxom::title\" ("
. $blosxom::path_info . "):\n\n";
if ($$params_ref{'comment'}) {
$message .= "Name : " . $$params_ref{'name'} . "\n";
$message .= "Email/URL: " . $$params_ref{'url'} . "\n";
$message .= "Comment :\n";
my $comment = $$params_ref{'comment'};
$comment =~ s!\r!\n!g;
$message .= $comment . "\n";
} else {
$message .= "Blog name: " . $$params_ref{'blog_name'} . "\n";
$message .= "Article : " . $$params_ref{'title'} . "\n";
$message .= "URL : " . $$params_ref{'url'} . "\n";
$message .= "Excerpt :\n";
my $excerpt = $$params_ref{'excerpt'};
$excerpt =~ s!\r!\n!g;
$message .= $excerpt . "\n";
}
if ($moderate) {
# For TrackBacks use the default flavour for the approve/reject URI.
my $moderate_flavour = $blosxom::flavour;
$moderate_flavour eq $trackback_flavour
and $moderate_flavour = $blosxom::default_flavour;
$message .= "\n\nTo approve this feedback, please click on the URL\n"
. "$blosxom::url$blosxom::path/$blosxom::fn.$moderate_flavour"
. "?moderate=approve;feedback=" . uri_escape($fb_item) . "\n";
$message .= "\nTo reject this feedback, please click on the URL\n"
. "$blosxom::url$blosxom::path/$blosxom::fn.$moderate_flavour"
. "?moderate=reject;feedback=" . uri_escape($fb_item) . "\n";
}
# Load Net::SMTP module only now that it's needed.
require Net::SMTP; Net::SMTP->import;
my $smtp = Net::SMTP->new($smtp_server);
$smtp->mail($address);
$smtp->to($address);
$smtp->data();
$smtp->datasend("To: $address\n");
$smtp->datasend("From: $address\n");
$smtp->datasend("Subject: [$blosxom::blog_title] Feedback: "
. "\"$blosxom::title\"\n");
$smtp->datasend("\n\n");
$smtp->datasend($message);
$smtp->dataend();
$smtp->quit;
return 1;
}
# Format the date used in comments and TrackBacks. If the argument is a
# number then it is considered to be a date/time in seconds since the
# (Perl) epoch; otherwise we assume that the date is already formatted.
# (This may allow the feedback plug-in to use legacy writeback files.)
sub format_date {
my $date_value = shift;
if ($date_value =~ m!^\d+$!) {
my ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) =
localtime($date_value);
$year += 1900;
# Modify the following to match your preference.
return sprintf("%4d-%02d-%02d %02d:%02d",
$year, $mon+1, $mday, $hour, $min);
} else {
return $date_value;
}
}
# Format the name used in comments.
sub format_name {
my ($name, $url) = @_;
# If the user didn't supply a name, try to use something sensible.
unless ($name) {
if ($url =~ m/^mailto:/) {
$name = substr($url, 7);
} else {
$name = $default_name;
}
}
# Link to a URL if one was provided.
my $name_link =
$url ? "<a href=\"$url\" rel=\"nofollow\">$name</a>" : $name ;
return $name, $name_link;
}
# Format the comment response message.
sub format_cmt_response {
my $response = shift;
# Clean up the response.
$response =~ s/^\s+//;
$response =~ s/\s+$//;
# Convert the response into a special type of paragraph.
# NOTE: A value 'OK' for $response indicates a successful comment.
if ($response eq 'OK') {
$response = '<p class="comment-response">Thanks for the comment!</p>';
} else {
$response = '<p class="comment-response">' . $response . '</p>';
}
return $response;
}
# Format the TrackBack response message.
sub format_tb_response {
my $response = shift;
# Clean up the response.
$response =~ s/^\s+//;
$response =~ s/\s+$//;
# Convert the response into an XML message per the TrackBack Technical
# Specification <http://www.sixapart.com/pronet/docs/trackback_spec>.
# NOTE: A value 'OK' for $response indicates a successful TrackBack;
# note that this value is *not* used as part of the TrackBack response.
if ($response eq 'OK') {
$response = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"
. "<response><error>0</error></response>";
} else {
$response = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?>"
. "<response><error>1</error>"
. "<message>$response</message></response>";
}
return $response;
}
# Format the comment itself.
sub format_comment {
my $comment = shift;
# TODO: Support other comment formats such as Textile.
if ($comment_format eq 'none') {
# A no-op, assumes formatting will be added in the template.
} elsif ($comment_format eq 'plaintext') {
# Simply convert the comment into a series of paragraphs.
$comment = '<p>' . $comment . '</p>';
$comment =~ s!\r\r!</p><p>!mg;
} elsif ($comment_format eq 'markdown'
&& $blosxom::plugins{'Markdown'} > 0) {
$comment = &Markdown::Markdown($comment);
}
return $comment;
}
# Format the blog name used in TrackBacks.
sub format_blog_name {
my $blog_name = shift;
$blog_name or $blog_name = $default_blog_name;
return $blog_name;
}
# Format the title used in TrackBacks.
sub format_title {
my ($title, $url) = @_;
my $title_link;
# Link to article, quoting the title if one was supplied.
if ($title) {
$title_link = "\"<a href=\"$url\" rel=\"nofollow\">$title</a>\"";
} else {
$title = $default_title;
$title_link = "<a href=\"$url\" rel=\"nofollow\">$title</a>";
}
return $title, $title_link;
}
# Format the TrackBack excerpt.
sub format_excerpt {
my $excerpt = shift;
# TODO: Truncate excerpts at some reasonable length.
# Simply convert the excerpt into a series of paragraphs.
if ($excerpt) {
$excerpt = '<p>' . $excerpt . '</p>';
$excerpt =~ s!\r\r!</p><p>!mg;
}
return $excerpt;
}
# Read in the MT-Blacklist file.
sub read_blacklist {
# No need to do anything if we've already read in the blacklist file.
return 1 if @blacklist_entries;
# Try to find the blacklist file and open it.
open BLACKLIST, "$blacklist_file"
or die "Can't read '$blacklist_file', $!\n";
my @lines = grep {! /^\s*\#/ } <BLACKLIST>;
close BLACKLIST;
die "No blacklists?\n" unless @lines;
foreach my $line (@lines) {
$line =~ s/^\s*//;
$line =~ s/\s*[^\\]\#.*//;
next unless $line;
push @blacklist_entries, $line;
}
die "No entries in blacklist file?\n" unless @blacklist_entries;
return 1;
}