-
Notifications
You must be signed in to change notification settings - Fork 8
/
Display.pm
executable file
·1533 lines (1098 loc) · 34.1 KB
/
Display.pm
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
# This code is a part of Slash, and is released under the GPL.
# Copyright 1997-2003 by Open Source Development Network. See README
# and COPYING for more information, or see http://slashcode.com/.
# $Id$
package Slash::Utility::Display;
=head1 NAME
Slash::Utility::Display - SHORT DESCRIPTION for Slash
=head1 SYNOPSIS
use Slash::Utility;
# do not use this module directly
=head1 DESCRIPTION
LONG DESCRIPTION.
=head1 EXPORTED FUNCTIONS
=cut
use strict;
use Slash::Display;
use Slash::Utility::Data;
use Slash::Utility::Environment;
use HTML::TokeParser ();
use base 'Exporter';
use vars qw($VERSION @EXPORT);
($VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
@EXPORT = qw(
cleanSlashTags
createMenu
createSelect
currentAdminUsers
fancybox
getImportantWords
horizmenu
linkComment
linkCommentPages
linkStory
lockTest
matchingStrings
pollbooth
portalbox
processSlashTags
selectMode
selectSection
selectSortcode
selectThreshold
selectTopic
titlebar
);
#========================================================================
=head2 getData(VALUE [, PARAMETERS, PAGE])
Returns snippets of data associated with a given page.
=over 4
=item Parameters
=over 4
=item VALUE
The name of the data-snippet to process and retrieve.
=item PARAMETERS
Data stored in a hashref which is to be passed to the retrieved snippet.
=item PAGE
The name of the page to which VALUE is associated.
=back
=item Return value
Returns data snippet with all necessary data interpolated.
=item Dependencies
Gets little snippets of data, determined by the value parameter, from
a data template. A data template is a colletion of data snippets
in one template, which are grouped together for efficiency. Each
script can have it's own data template (specified by the PAGE
parameter). If PAGE is unspecified, snippets will be retrieved from
the last page visited by the user as determined by Slash::Apache::User.
=back
=cut
sub getData {
my($value, $hashref, $page) = @_;
$hashref ||= {};
$hashref->{value} = $value;
my %opts = ( Return => 1, Nocomm => 1 );
$opts{Page} = $page || 'NONE' if defined $page;
return slashDisplay('data', $hashref, \%opts);
}
#========================================================================
=head2 createSelect(LABEL, DATA [, DEFAULT, RETURN, NSORT, ORDERED])
Creates a drop-down list in HTML. List is sorted by default
alphabetically according to list values.
=over 4
=item Parameters
=over 4
=item LABEL
The name for the HTML entity.
=item DATA
A hashref containing key-value pairs for the list.
Keys are list values, and values are list labels.
If an arrayref is passed, it is converted to a
hashref, where the keys and values are the same.
=item DEFAULT
Default value for the list.
=item RETURN
See "Return value" below.
=item NSORT
Sort numerically, not alphabetically.
=item ORDERED
If an arrayref is passed, an already-sorted array reference of keys.
If non-ref, then an arrayref of hash keys is created sorting the
hash values, alphabetically and case-insensitively.
If ORDERED is passed in either form, then the NSORT parameter is ignored.
=back
=item Return value
If RETURN is true, the text of the list is returned.
Otherwise, list is just printed, and returns
true/false if operation is successful.
If there are no elements in DATA, just returns/prints nothing.
=item Dependencies
The 'select' template block.
=back
=cut
sub createSelect {
my($label, $hashref, $default, $return, $nsort, $ordered) = @_;
if (ref $hashref eq 'ARRAY') {
$hashref = { map { ($_, $_) } @$hashref };
} else {
# If $hashref is a hash whose elements are also hashrefs, and
# they all have the field "name", then copy it into another
# hashref that pulls those "name" fields up one level. Talk
# about wacky convenience features!
my @keys = keys %$hashref;
my $all_name = 1;
for my $key (@keys) {
if (!ref($hashref->{$key})
|| !ref($hashref->{$key}) eq 'HASH'
|| !defined($hashref->{$key}{name})) {
$all_name = 0;
last;
}
}
if ($all_name) {
$hashref = {
map { ($_, $hashref->{$_}{name}) }
keys %$hashref
};
}
}
return unless (ref $hashref eq 'HASH' && keys %$hashref);
if ($ordered && !ref $ordered) {
$ordered = [
map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [$_, lc $hashref->{$_}] }
keys %$hashref
];
}
my $display = {
label => $label,
items => $hashref,
default => $default,
numeric => $nsort,
ordered => $ordered,
};
if ($return) {
return slashDisplay('select', $display, 1);
} else {
slashDisplay('select', $display);
}
}
#========================================================================
=head2 selectTopic(LABEL [, DEFAULT, SECTION, RETURN])
Creates a drop-down list of topics in HTML. Calls C<createSelect>.
=over 4
=item Parameters
=over 4
=item LABEL
The name for the HTML entity.
=item DEFAULT
Default topic for the list.
=item SECTION
Default section to take topics from.
=item RETURN
See "Return value" below.
=back
=item Return value
If RETURN is true, the text of the list is returned.
Otherwise, list is just printed, and returns
true/false if operation is successful.
=back
=cut
sub selectTopic {
my($label, $default, $section, $return) = @_;
my $slashdb = getCurrentDB();
$section ||= getCurrentStatic('defaultsection');
$default ||= getCurrentStatic('defaulttopic');
my $topics = $slashdb->getDescriptions('topics_section', $section);
createSelect($label, $topics, $default, $return, 0, 1);
}
#========================================================================
=head2 selectSection(LABEL [, DEFAULT, SECT, RETURN, ALL])
Creates a drop-down list of sections in HTML. Calls C<createSelect>.
=over 4
=item Parameters
=over 4
=item LABEL
The name for the HTML entity.
=item DEFAULT
Default topic for the list.
=item SECT
Hashref for current section. If SECT->{isolate} is true,
list is not created, but hidden value is returned instead.
=item RETURN
See "Return value" below.
=item ALL
Boolean for including "All Topics" item.
=back
=item Return value
If RETURN is true, the text of the list is returned.
Otherwise, list is just printed, and returns
true/false if operation is successful.
=item Dependencies
The 'sectionisolate' template block.
=back
=cut
sub selectSection {
my($label, $default, $SECT, $return, $all) = @_;
my $slashdb = getCurrentDB();
my $seclev = getCurrentUser('seclev');
my $sections = $slashdb->getDescriptions('sections');
createSelect($label, $sections, $default, $return);
}
#========================================================================
=head2 selectSortcode()
Creates a drop-down list of sortcodes in HTML. Default is the user's
preference. Calls C<createSelect>.
=over 4
=item Return value
The created list.
=back
=cut
sub selectSortcode {
my $slashdb = getCurrentDB();
createSelect('commentsort', $slashdb->getDescriptions('sortcodes'),
getCurrentUser('commentsort'), 1);
}
#========================================================================
=head2 selectMode()
Creates a drop-down list of modes in HTML. Default is the user's
preference. Calls C<createSelect>.
=over 4
=item Return value
The created list.
=back
=cut
sub selectMode {
my $slashdb = getCurrentDB();
createSelect('mode', $slashdb->getDescriptions('commentmodes'),
getCurrentUser('mode'), 1);
}
#========================================================================
=head2 selectThreshold(COUNTS)
Creates a drop-down list of thresholds in HTML. Default is the user's
preference. Calls C<createSelect>.
=over 4
=item Parameters
=over 4
=item COUNTS
An arrayref of thresholds -E<gt> counts for that threshold.
=back
=item Return value
The created list.
=item Dependencies
The 'selectThreshLabel' template block.
=back
=cut
sub selectThreshold {
my($counts) = @_;
my $constants = getCurrentStatic();
my $user = getCurrentUser();
my %data;
foreach my $c ($constants->{comment_minscore} .. $constants->{comment_maxscore}) {
$data{$c} = slashDisplay('selectThreshLabel', {
points => $c,
count => $counts->[$c - $constants->{comment_minscore}],
}, { Return => 1, Nocomm => 1 });
}
createSelect('threshold', \%data, getCurrentUser('threshold'), 1, 1);
}
#========================================================================
=head2 linkStory(STORY)
The generic "Link a Story" function, used wherever stories need linking.
=over 4
=item Parameters
=over 4
=item STORY
A hashref containing data about a story to be linked to.
=back
=item Return value
The complete E<lt>A HREF ...E<gt>E<lt>/AE<gt> text for linking to the story.
=item Dependencies
The 'linkStory' template block.
=back
=cut
sub linkStory {
my($story_link) = @_;
my $user = getCurrentUser();
my $constants = getCurrentStatic();
my $reader = getObject('Slash::DB', { db_type => 'reader' });
my $mode = $story_link->{mode} || $user->{mode};
my $threshold = undef;
$threshold = $story_link->{threshold} if exists $story_link->{threshold};
my $dynamic = 0;
# Setting $dynamic properly is important. When generating the
# AC index.shtml, it's a big win if we link to other
# prerendered .shtml URLs whenever possible/appropriate.
# But, we must link to the .pl when necessary.
if ($ENV{SCRIPT_NAME} or !$user->{is_anon}) {
# Whenever we're invoked from Apache, use dynamic links.
# This test will be true 99% of the time we come through
# here, so it's first.
$dynamic = 1;
} elsif ($mode) {
# If we're an AC script, but this is a link to e.g.
# mode=nocomment, then we need to have it be dynamic.
$dynamic = 1 if $mode ne $reader->getUser(
$constants->{anonymous_coward_uid},
'mode',
);
}
if (!$dynamic and defined($threshold)) {
# If we still think we can get away with a nondynamic link,
# we need to check one more thing. Even an AC linking to
# an article needs to make the link dynamic if it's the
# "n comments" link, where threshold = -1. For maximum
# compatibility we check against the AC's threshold.
$dynamic = 1 if $threshold != getCurrentAnonymousCoward('threshold');
}
# We need to make sure we always get the right link -Brian
$story_link->{section} ||= $reader->getStory($story_link->{sid}, 'section');
$story_link->{'link'} = $reader->getStory($story_link->{sid}, 'title')
if $story_link->{'link'} eq "";
my $section = $reader->getSection($story_link->{section});
my $url = $section->{rootdir} || $constants->{real_rootdir} || $constants->{rootdir};
my $tids = $reader->getStoryTopicsJustTids($story_link->{sid});
my $tid_string = join('&tid=', @$tids);
return _hard_linkStory($story_link, $mode, $threshold, $dynamic, $url, $tid_string)
if $constants->{comments_hardcoded} && !$user->{light};
return slashDisplay('linkStory', {
mode => $mode,
threshold => $threshold,
tid => $story_link->{tid},
tid_string => $tid_string,
sid => $story_link->{sid},
section => $story_link->{section},
url => $url,
text => $story_link->{'link'},
dynamic => $dynamic,
title => $story_link->{title},
}, { Return => 1, Nocomm => 1 });
}
#========================================================================
=head2 pollbooth(QID [, NO_TABLE, CENTER, RETURNTO])
Creates a voting pollbooth.
=over 4
=item Parameters
=over 4
=item QID
The unique question ID for the poll.
=item NO_TABLE
Boolean for whether to leave the poll out of a table.
If false, then will be formatted inside a C<fancybox>.
=item CENTER
Whether or not to center the tabled pollbooth (only
works with NO_TABLE).
=item RETURNTO
If this parameter is specified, the voting widget will take the vote and return
the user to the specified URI. Note that you WILL NOT be able to redirect
outside of the site usign this parameter for security reasons (hence the need for
URIs as opposed to URLs).
=back
=item Return value
Returns the pollbooth data.
=item Dependencies
The 'pollbooth' template block.
=back
=cut
sub pollbooth {
my($qid, $no_table, $center, $returnto) = @_;
my $slashdb = getCurrentDB();
my $constants = getCurrentStatic();
my $sect = $slashdb->getSection();
# This special qid means to use the current (sitewide) poll.
$qid = $sect->{qid} if $qid eq '_currentqid';
# If no qid (or no sitewide poll), short-circuit out.
return '' if $qid eq '';
my $poll = $slashdb->getPoll($qid);
return '' unless %$poll;
my $n_comments = $slashdb->countCommentsBySid(
$poll->{pollq}{discussion});
my $poll_open = $slashdb->isPollOpen($qid);
my $has_voted = $slashdb->hasVotedIn($qid);
my $can_vote = !$has_voted && $poll_open;
return slashDisplay('pollbooth', {
question => $poll->{pollq}{question},
answers => $poll->{answers},
qid => $qid,
poll_open => $poll_open,
has_voted => $has_voted,
can_vote => $can_vote,
voters => $poll->{pollq}{voters},
comments => $n_comments,
sect => $sect->{section},
returnto => $returnto,
}, 1);
}
#========================================================================
=head2 currentAdminUsers()
Displays table of current admin users, with what they are adminning.
=over 4
=item Return value
The HTML to display.
=item Dependencies
The 'currentAdminUsers' template block.
=back
=cut
sub currentAdminUsers {
my $html_to_display;
my $slashdb = getCurrentDB();
my $constants = getCurrentStatic();
my $user = getCurrentUser();
# my $now = UnixDate(ParseDate($slashdb->getTime()), "%s");
my $now = timeCalc($slashdb->getTime(), "%s", 0);
my $aids = $slashdb->currentAdmin();
for my $data (@$aids) {
my($usernick, $usertime, $lasttitle, $uid) = @$data;
if ($usernick eq $user->{nickname}) {
$usertime = "-";
} else {
$usertime = $now - timeCalc($usertime, "%s", 0); # UnixDate(ParseDate($usertime), "%s");
if ($usertime <= 99) {
$usertime .= "s";
} elsif ($usertime <= 3600) {
$usertime = int($usertime/60+0.5) . "m";
} else {
$usertime = int($usertime/3600) . "h"
. int(($usertime%3600)/60+0.5) . "m";
}
}
@$data = ($usernick, $usertime, $lasttitle, $uid);
}
return slashDisplay('currentAdminUsers', {
ids => $aids,
can_edit_admins => $user->{seclev} > 10000,
}, 1);
}
#========================================================================
=head2 horizmenu()
Silly little function to create a horizontal menu from the
'mainmenu' block.
=over 4
=item Return value
The horizontal menu.
=item Dependencies
The 'mainmenu' template block.
=back
=cut
sub horizmenu {
my $horizmenu = slashDisplay('mainmenu', {}, { Return => 1, Nocomm => 1 });
$horizmenu =~ s/^\s*//mg;
$horizmenu =~ s/^-\s*//mg;
$horizmenu =~ s/\s*$//mg;
$horizmenu =~ s/<NOBR>//gi;
$horizmenu =~ s/<\/NOBR>//gi;
$horizmenu =~ s/<HR(?:>|\s[^>]*>)//g;
$horizmenu = join ' | ', split /<BR>/, $horizmenu;
$horizmenu =~ s/[\|\s]+$//;
$horizmenu =~ s/^[\|\s]+//;
return "[ $horizmenu ]";
}
#========================================================================
=head2 titlebar(WIDTH, TITLE, OPTIONS)
Prints a titlebar widget. Exactly equivalent to:
slashDisplay('titlebar', {
width => $width,
title => $title
});
or, if template is passed in as an option, e.g. template => user_titlebar:
slashDisplay('user_titlebar', {
width => $width,
title => $title
});
If you're calling this from a template, you better have a really good
reason, since [% PROCESS %] will work just as well.
=over 4
=item Parameters
=over 4
=item WIDTH
Width of the titlebar.
=item TITLE
Title of the titlebar.
=back
=item Return value
None.
=item Dependencies
The 'titlebar' template block.
=back
=cut
sub titlebar {
my($width, $title, $options) = @_;
my $templatename = $options->{template} ? $options->{template} : "titlebar";
my $data = { width => $width, title => $title };
$data->{tab_selected} = $options->{tab_selected} if $options->{tab_selected};
slashDisplay($templatename, $data);
}
#========================================================================
=head2 fancybox(WIDTH, TITLE, CONTENTS [, CENTER, RETURN])
Creates a fancybox widget.
=over 4
=item Parameters
=over 4
=item WIDTH
Width of the fancybox.
=item TITLE
Title of the fancybox.
=item CONTENTS
Contents of the fancybox. (I see a pattern here.)
=item CENTER
Boolean for whether or not the fancybox
should be centered.
=item RETURN
Boolean for whether to return or print the
fancybox.
=back
=item Return value
The fancybox if RETURN is true, or true/false
on success/failure.
=item Dependencies
The 'fancybox' template block.
=back
=cut
sub fancybox {
my($width, $title, $contents, $center, $return) = @_;
return unless $title && $contents;
my $tmpwidth = $width;
# allow width in percent or raw pixels
my $pct = 1 if $tmpwidth =~ s/%$//;
# used in some blocks
my $mainwidth = $tmpwidth-4;
my $insidewidth = $mainwidth-8;
if ($pct) {
for ($mainwidth, $insidewidth) {
$_ .= '%';
}
}
slashDisplay('fancybox', {
width => $width,
contents => $contents,
title => $title,
center => $center,
mainwidth => $mainwidth,
insidewidth => $insidewidth,
}, $return);
}
#========================================================================
=head2 portalbox(WIDTH, TITLE, CONTENTS, BID [, URL])
Creates a portalbox widget. Calls C<fancybox> to process
the box itself.
=over 4
=item Parameters
=over 4
=item WIDTH
Width of the portalbox.
=item TITLE
Title of the portalbox.
=item CONTENTS
Contents of the portalbox.
=item BID
The block ID for the portal in question.
=item URL
URL to link the title of the portalbox to.
=back
=item Return value
The portalbox.
=item Dependencies
The 'fancybox', 'portalboxtitle', and
'portalmap' template blocks.
=back
=cut
sub portalbox {
my($width, $title, $contents, $bid, $url) = @_;
return unless $title && $contents;
my $constants = getCurrentStatic();
my $user = getCurrentUser();
$title = slashDisplay('portalboxtitle', {
title => $title,
url => $url,
}, { Return => 1, Nocomm => 1 });
if ($user->{exboxes}) {
$title = slashDisplay('portalmap', {
title => $title,
bid => $bid,
}, { Return => 1, Nocomm => 1 });
}
fancybox($width, $title, $contents, 0, 1);
}
#========================================================================
=head2 linkCommentPages(SID, PID, CID, TOTAL)
Print links to pages for additional comments.
=over 4
=item Parameters
=over 4
=item SID
Story ID.
=item PID
Parent ID.
=item CID
Comment ID.
=item TOTAL
Total number of comments.
=back
=item Return value
Links.
=item Dependencies
The 'linkCommentPages' template block.
=back
=cut
sub linkCommentPages {
my($sid, $pid, $cid, $total) = @_;
return slashDisplay('linkCommentPages', {
sid => $sid,
pid => $pid,
cid => $cid,
total => $total,
}, 1);
}
#========================================================================
=head2 linkComment(COMMENT [, PRINTCOMMENT, DATE])
Print a link to a comment.
=over 4
=item Parameters
=over 4
=item COMMENT
A hashref containing data about the comment.
=item PRINTCOMMENT
Boolean for whether to create link directly
to comment, instead of to the story for that comment.
=item DATE
Boolean for whather to print date with link.
=back
=item Return value
Link for comment.
=item Dependencies
The 'linkComment' template block.
=back
=cut
sub linkComment {
my($comment, $printcomment, $date) = @_;
my $constants = getCurrentStatic();
return _hard_linkComment(@_) if $constants->{comments_hardcoded};
my $user = getCurrentUser();
my $adminflag = $user->{seclev} >= 10000 ? 1 : 0;
# don't inherit these ...
for (qw(sid cid pid date subject comment uid points lastmod
reason nickname fakeemail homepage sig)) {
$comment->{$_} = '' unless exists $comment->{$_};