-
Notifications
You must be signed in to change notification settings - Fork 8
/
Data.pm
executable file
·3040 lines (2286 loc) · 69.8 KB
/
Data.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::Data;
=head1 NAME
Slash::MODULE - 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 Date::Format qw(time2str);
use Date::Language;
use Date::Parse qw(str2time);
use Digest::MD5 'md5_hex';
use HTML::Entities;
use HTML::FormatText;
use HTML::TreeBuilder;
use Safe;
use Slash::Constants qw(:strip);
use Slash::Utility::Environment;
use URI;
use XML::Parser;
use base 'Exporter';
use vars qw($VERSION @EXPORT);
($VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
@EXPORT = qw(
addDomainTags
createStoryTopicData
slashizeLinks
approveCharref
parseDomainTags
parseSlashizedLinks
balanceTags
changePassword
chopEntity
commify
countTotalVisibleKids
countWords
decode_entities
ellipsify
encryptPassword
findWords
fixHref
fixint
fixparam
fixurl
fudgeurl
formatDate
getArmoredEmail
grepn
html2text
nickFix
nick2matchname
root2abs
set_rootdir
sitename2filename
strip_anchor
strip_attribute
strip_code
strip_extrans
strip_html
strip_literal
strip_mode
strip_nohtml
strip_notags
strip_plaintext
strip_paramattr
strip_urlattr
timeCalc
url2abs
xmldecode
xmlencode
xmlencode_plain
vislenify
);
# really, these should not be used externally, but we leave them
# here for reference as to what is in the package
# @EXPORT_OK = qw(
# approveTag
# breakHtml
# processCustomTags
# stripByMode
# );
#========================================================================
sub nickFix {
my($nick) = @_;
my $constants = getCurrentStatic();
$nick =~ s/\s+/ /g;
$nick =~ s/[^$constants->{nick_chars}]+//g;
$nick = substr($nick, 0, $constants->{nick_maxlen});
return $nick;
}
#========================================================================
sub nick2matchname {
my($nick) = @_;
$nick = lc $nick;
$nick =~ s/[^a-zA-Z0-9]//g;
return $nick;
}
#========================================================================
=head2 root2abs()
Convert C<rootdir> to its absolute equivalent. By default, C<rootdir> is
protocol-inspecific (such as "//www.example.com") and for redirects needs
to be converted to its absolute form. There is an C<absolutedir> var, but
it is protocol-specific, and we want to inherit the protocol. So if we're
connected over HTTPS, we use HTTPS, else we use HTTP.
=over 4
=item Return value
rootdir variable, converted to absolute with proper protocol.
=back
=cut
sub root2abs {
my $user = getCurrentUser();
if ($user->{state}{ssl}) {
return getCurrentStatic('absolutedir_secure');
} else {
return getCurrentStatic('absolutedir');
}
}
#========================================================================
=head2 set_rootdir()
Make sure all your rootdirs use the same scheme (even if that scheme is no
scheme), and absolutedir's scheme can still be section-specific, and we don't
need an extra var for rootdir/absolutedir.
In the future, even this behavior should perhaps be overridable (so
sites could have http for the main site, and https for sections, for
example).
=over 4
=item Return value
rootdir variable, converted to proper scheme.
=back
=cut
sub set_rootdir {
my($sectionurl, $rootdir) = @_;
my $rooturi = new URI $rootdir, "http";
my $sectionuri = new URI $sectionurl, "http";
$sectionuri->scheme($rooturi->scheme || undef);
return $sectionuri->as_string;
}
#========================================================================
=head2 url2abs(URL [, BASE])
Take URL and make it absolute. It takes a URL,
and adds base to the beginning if necessary, and
adds the protocol to the beginning if necessary, and
then uses URI->new_abs() to get the correct string.
=over 4
=item Parameters
=over 4
=item URL
URL to make absolute.
=item BASE
URL base. If not provided, uses rootdir.
=back
=item Return value
Fixed URL.
=back
=cut
sub url2abs {
my($url, $base) = @_;
my $newurl;
# set base only if not already set, and rootdir exists
$base ||= root2abs();
if ($base) {
$newurl = URI->new_abs($url, $base)->canonical->as_string;
} elsif ($url !~ m|^https?://|i) { # no base or rootdir, best we can do
$newurl =~ s|^/*|/|;
}
$newurl =~ s|/$|| if $url !~ m|/$|;
return $newurl;
}
#========================================================================
=head2 formatDate(DATA [, COLUMN, AS, FORMAT])
Converts dates from the database; takes an arrayref of rows.
This example would take the 1th element of each arrayref in C<$data>, format it,
and put the result in the 2th element.
formatDate($data, 1, 2);
This example would take the "foo" key of each hashref in C<$data>, format it,
and put the result in the "bar" key.
formatDate($data, 'foo', 'bar');
The C<timeCalc> function does the formatting.
=over 4
=item Parameters
=over 4
=item DATA
Data is either an arrayref of arrayrefs, or an arrayref of hashrefs.
Which it is will be determined by whether COLUMN is numeric or not. If
it is numeric, then DATA will be assumed to be an arrayref of arrayrefs.
=item COLUMN
The column to take the data from, to be translated. If numeric, then
DATA will be taken to be an arrayref of arrayrefs. Otherwise, the value
will be the hashref key. Default value is "date".
=item AS
The column where to put the newly formatted data. If COLUMN is numeric
and AS is not defined, then AS will be the same value as COLUMN. Otherwise,
the default value of AS is "time".
=item FORMAT
Optional Date::Format format string.
=back
=item Return value
True if successful, false if not.
=item Side effects
Changes values in DATA.
=item Dependencies
The C<timeCalc> function.
=back
=cut
sub formatDate {
my($data, $col, $as, $format) = @_;
errorLog('Not arrayref'), return unless ref($data) eq 'ARRAY';
if (defined($col) && $col =~ /^\d+$/) { # LoL
$as = defined($as) ? $as : $col;
for (@$data) {
errorLog('Not arrayref'), return unless ref eq 'ARRAY';
$_->[$as] = timeCalc($_->[$col], $format);
}
} else { # LoH
$col ||= 'date';
$as ||= 'time';
for (@$data) {
errorLog('Not hashref'), return unless ref eq 'HASH';
$_->{$as} = timeCalc($_->{$col}, $format);
}
}
}
#========================================================================
=head2 timeCalc(DATE [, FORMAT, OFFSET])
Format time strings using user's format preference.
=over 4
=item Parameters
=over 4
=item DATE
Raw date/time to format.
Supply a false value here to get the current date/time.
=item FORMAT
Optional format to override user's format.
=item OFFSET
Optional positive or negative integer for offset seconds from GMT,
to override user's offset.
=back
=item Return value
Formatted date string.
=item Dependencies
The 'atonish' and 'aton' template blocks.
=back
=cut
sub timeCalc {
# raw mysql date of story
my($date, $format, $off_set) = @_;
my $user = getCurrentUser();
my(@dateformats, $err);
$off_set = $user->{off_set} unless defined $off_set;
if ($date) {
# massage data for YYYYMMDDHHmmSS or YYYYMMDDHHmm
$date =~ s/^(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})?$/"$1-$2-$3 $4:$5:" . ($6 || '00')/e;
# find out the user's time based on personal offset in seconds
$date = str2time($date) + $off_set;
} else {
# use current time (plus offset) if no time provided
$date = time() + $off_set;
}
# set user's language; we only use this if it is defined,
# so it's not a performance hit
my $lang = getCurrentStatic('datelang');
# convert the raw date to pretty formatted date
if ($lang && $lang ne 'English') {
my $datelang = Date::Language->new($lang);
$date = $datelang->time2str($format || $user->{'format'}, $date);
} else {
$date = time2str($format || $user->{'format'}, $date);
}
# return the new pretty date
return $date;
}
#========================================================================
=head2 changePassword()
Return new random 8-character password composed of 0..9, A..Z, a..z
(but not including possibly hard-to-read characters [0O1Iil]).
=over 4
=item Return value
Random password.
=back
=cut
{
my @chars = grep !/[0O1Iil]/, 0..9, 'A'..'Z', 'a'..'z';
sub changePassword {
return join '', map { $chars[rand @chars] } 0 .. 7;
}
}
#========================================================================
=head2 encryptPassword(PASSWD)
Encrypts given password. Currently uses MD5, but could change in the future,
so do not depend on implementation.
=over 4
=item Parameters
=over 4
=item PASSWD
Password to be encrypted.
=back
=item Return value
Encrypted password.
=back
=cut
sub encryptPassword {
my($passwd) = @_;
return md5_hex($passwd);
}
#========================================================================
=head2 stripByMode(STRING [, MODE, NO_WHITESPACE_FIX])
Private function. Fixes up a string based on what the mode is. This
function is no longer exported, use the C<strip_*> functions instead.
=over 4
[ Should this be somewhat templatized, so they can customize
the little HTML bits? Same goes with related functions. -- pudge ]
=item Parameters
=over 4
=item STRING
The string to be manipulated.
=item MODE
May be one of:
=item nohtml
The default. Just strips out HTML.
=item literal
Prints the text verbatim into HTML, which
means just converting < and > and & to their
HTML entities. Also turns on NO_WHITESPACE_FIX.
=item extrans
Similarly to 'literal', converts everything
to its HTML entity, but then formatting is
preserved by converting spaces to HTML
space entities, and multiple newlines into BR
tags.
=item code
Just like 'extrans' but wraps in CODE tags.
=item attribute
Attempts to format string to fit in as an HTML
attribute, which means the same thing as 'literal',
but " marks are also converted to their HTML entity.
=item plaintext
Similar to 'extrans', but does not translate < and >
and & first (so C<stripBadHtml> is called first).
=item anchor
Removes ALL whitespace from inside the filter. It's
is indented for use (but not limited to) the removal
of white space from in side HREF anchor tags to
prevent nasty browser artifacts from showing up in
the display. (Note: the value of NO_WHITESPACE_FIX
is ignored)
=item html (or anything else)
Just runs through C<stripBadHtml>.
=item NO_WHITESPACE_FIX
A boolean that, if true, disables fixing of whitespace
problems. A common exploit in these things is to
run a lot of characters together so the page will
stretch very wide. If NO_WHITESPACE_FIX is false,
then space is inserted to prevent this (see C<breakHtml>).
=back
=item Return value
The manipulated string.
=back
=cut
{ # closure for stripByMode
my %latin1_to_ascii = (
133 => '...',
135 => 'f',
138 => 'S',
140 => 'OE',
142 => 'Z',
145 => '\'',
146 => '\'',
147 => '"',
148 => '"',
150 => '-',
151 => '--',
153 => '(TM)',
154 => 's',
156 => 'oe',
158 => 'z',
159 => 'Y',
166 => '|',
169 => '(C)',
174 => '(R)',
177 => '+/-',
188 => '1/4',
189 => '1/2',
190 => '3/4',
192 => 'A',
193 => 'A',
194 => 'A',
195 => 'A',
196 => 'A',
197 => 'A',
198 => 'AE',
199 => 'C',
200 => 'E',
201 => 'E',
202 => 'E',
203 => 'E',
204 => 'I',
205 => 'I',
206 => 'I',
207 => 'I',
208 => 'D',
209 => 'N',
210 => 'O',
211 => 'O',
212 => 'O',
213 => 'O',
214 => 'O',
215 => 'x',
216 => 'O',
217 => 'U',
218 => 'U',
219 => 'U',
220 => 'U',
221 => 'Y',
223 => 'B',
224 => 'a',
225 => 'a',
226 => 'a',
227 => 'a',
228 => 'a',
229 => 'a',
230 => 'ae',
231 => 'c',
232 => 'e',
233 => 'e',
234 => 'e',
235 => 'e',
236 => 'i',
237 => 'i',
238 => 'i',
239 => 'i',
240 => 'd',
241 => 'n',
242 => 'o',
243 => 'o',
244 => 'o',
245 => 'o',
246 => 'o',
247 => '/',
248 => 'o',
249 => 'u',
250 => 'u',
251 => 'u',
252 => 'u',
253 => 'y',
255 => 'y',
);
my %action_data = ( );
my %actions = (
newline_to_local => sub {
${$_[0]} =~ s/(?:\015?\012|\015)/\n/g; },
trailing_whitespace => sub {
${$_[0]} =~ s/[\t ]+\n/\n/g; },
encode_html_amp => sub {
${$_[0]} =~ s/&/&/g; },
encode_html_amp_ifnotent => sub {
${$_[0]} =~ s/&(?!#?[a-zA-Z0-9]+;)/&/g; },
encode_html_ltgt => sub {
${$_[0]} =~ s/</</g;
${$_[0]} =~ s/>/>/g; },
encode_html_ltgt_stray => sub {
1 while ${$_[0]} =~ s{
( (?: ^ | > ) [^<]* )
>
}{$1>}gx;
1 while ${$_[0]} =~ s{
<
( [^>]* (?: < | $ ) )
>
}{<$1}gx; },
encode_html_quote => sub {
${$_[0]} =~ s/"/"/g; },
breakHtml_ifwhitefix => sub {
${$_[0]} = breakHtml(${$_[0]})
unless $action_data{no_white_fix}; },
processCustomTags => sub {
${$_[0]} = processCustomTags(${$_[0]}); },
approveTags => sub {
${$_[0]} =~ s/<(.*?)>/approveTag($1)/sge; },
approveCharrefs => sub {
${$_[0]} =~ s{
&(\#?[a-zA-Z0-9]+);?
}{approveCharref($1)}gex; },
space_between_tags => sub {
${$_[0]} =~ s/></> </g; },
whitespace_tagify => sub {
${$_[0]} =~ s/\n/<BR>/gi; # pp breaks
${$_[0]} =~ s/(?:<BR>\s*){2,}<BR>/<BR><BR>/gi;
# Preserve leading indents / spaces
# can mess up internal tabs, oh well
${$_[0]} =~ s/\t/ /g; },
whitespace_and_tt => sub {
${$_[0]} =~ s{((?: )+)(?: (\S))?} {
(" " x (length($1)/2)) .
(defined($2) ? " $2" : "")
}eg;
${$_[0]} = "<TT>${$_[0]}</TT>"; },
newline_indent => sub {
${$_[0]} =~ s{<BR>\n?( +)} {
"<BR>\n" . (" " x length($1))
}ieg; },
remove_tags => sub {
${$_[0]} =~ s/<.*?>//gs; },
remove_ltgt => sub {
${$_[0]} =~ s/<//g;
${$_[0]} =~ s/>//g; },
remove_trailing_lts => sub {
${$_[0]} =~ s/<(?!.*?>)//gs; },
remove_newlines => sub {
${$_[0]} =~ s/\n+//g; },
debugprint => sub {
print STDERR "stripByMode debug ($_[1]) '${$_[0]}'\n"; },
encode_high_bits => sub {
# !! assume Latin-1 !!
if (getCurrentStatic('draconian_charset')) {
my $convert = getCurrentStatic('draconian_charset_convert');
# anything not CRLF tab space or ! to ~ in Latin-1
# is converted to entities, where approveCharrefs or
# encode_html_amp takes care of them later
${$_[0]} =~ s/([^\n\r\t !-~])/($convert && $latin1_to_ascii{ord($1)}) || sprintf("&#%u;", ord($1))/ge;
} },
);
my %mode_actions = (
ANCHOR, [qw(
newline_to_local
remove_newlines )],
NOTAGS, [qw(
newline_to_local
encode_high_bits
remove_tags
remove_ltgt
encode_html_amp_ifnotent
approveCharrefs )],
ATTRIBUTE, [qw(
newline_to_local
encode_high_bits
encode_html_amp
encode_html_ltgt
encode_html_quote )],
LITERAL, [qw(
newline_to_local
encode_html_amp
encode_html_ltgt
breakHtml_ifwhitefix
processCustomTags
remove_trailing_lts
approveTags
space_between_tags
encode_html_ltgt_stray )],
NOHTML, [qw(
newline_to_local
trailing_whitespace
encode_high_bits
remove_tags
remove_ltgt
encode_html_amp )],
PLAINTEXT, [qw(
newline_to_local
trailing_whitespace
encode_high_bits
processCustomTags
remove_trailing_lts
approveTags
space_between_tags
encode_html_ltgt_stray
encode_html_amp_ifnotent
approveCharrefs
breakHtml_ifwhitefix
whitespace_tagify
newline_indent )],
HTML, [qw(
newline_to_local
trailing_whitespace
encode_high_bits
processCustomTags
remove_trailing_lts
approveTags
space_between_tags
encode_html_ltgt_stray
encode_html_amp_ifnotent
approveCharrefs
breakHtml_ifwhitefix )],
CODE, [qw(
newline_to_local
trailing_whitespace
encode_high_bits
encode_html_amp
encode_html_ltgt
whitespace_tagify
whitespace_and_tt
breakHtml_ifwhitefix )],
EXTRANS, [qw(
newline_to_local
trailing_whitespace
encode_high_bits
encode_html_amp
encode_html_ltgt
breakHtml_ifwhitefix
whitespace_tagify
newline_indent )],
);
sub stripByMode {
my($str, $fmode, $no_white_fix) = @_;
$fmode ||= NOHTML;
$no_white_fix = 1 if !defined($no_white_fix) && $fmode == LITERAL;
$action_data{no_white_fix} = $no_white_fix || 0;
my @actions = @{$mode_actions{$fmode}};
for my $action (@actions) {
$actions{$action}->(\$str, $fmode);
}
return $str;
}
}
#========================================================================
=head2 strip_anchor(STRING [, NO_WHITESPACE_FIX])
=head2 strip_attribute(STRING [, NO_WHITESPACE_FIX])
=head2 strip_code(STRING [, NO_WHITESPACE_FIX])
=head2 strip_extrans(STRING [, NO_WHITESPACE_FIX])
=head2 strip_html(STRING [, NO_WHITESPACE_FIX])
=head2 strip_literal(STRING [, NO_WHITESPACE_FIX])
=head2 strip_nohtml(STRING [, NO_WHITESPACE_FIX])
=head2 strip_notags(STRING [, NO_WHITESPACE_FIX])
=head2 strip_plaintext(STRING [, NO_WHITESPACE_FIX])
=head2 strip_mode(STRING [, MODE, NO_WHITESPACE_FIX])
Wrapper for C<stripByMode>. C<strip_mode> simply calls C<stripByMode>
and has the same arguments, but C<strip_mode> will only allow modes
with values greater than 0, that is, the user-supplied modes. C<strip_mode>
is only meant to be used for processing user-supplied modes, to prevent
the user from accessing other mode types. For using specific modes instead
of user-supplied modes, use the function with that mode's name.
See C<stripByMode> for details.
=cut
sub strip_mode {
my($string, $mode, @args) = @_;
return "" if !$mode || $mode < 1 || $mode > 4; # user-supplied modes are 1-4
return stripByMode($string, $mode, @args);
}
sub strip_anchor { stripByMode($_[0], ANCHOR, @_[1 .. $#_]) }
sub strip_attribute { stripByMode($_[0], ATTRIBUTE, @_[1 .. $#_]) }
sub strip_code { stripByMode($_[0], CODE, @_[1 .. $#_]) }
sub strip_extrans { stripByMode($_[0], EXTRANS, @_[1 .. $#_]) }
sub strip_html { stripByMode($_[0], HTML, @_[1 .. $#_]) }
sub strip_literal { stripByMode($_[0], LITERAL, @_[1 .. $#_]) }
sub strip_nohtml { stripByMode($_[0], NOHTML, @_[1 .. $#_]) }
sub strip_notags { stripByMode($_[0], NOTAGS, @_[1 .. $#_]) }
sub strip_plaintext { stripByMode($_[0], PLAINTEXT, @_[1 .. $#_]) }
#========================================================================
=head2 strip_paramattr(STRING [, NO_WHITESPACE_FIX])
=head2 strip_urlattr(STRING [, NO_WHITESPACE_FIX])
Wrappers for strip_attribute(fixparam($param), $no_whitespace_fix) and
strip_attribute(fudgeurl($url), $no_whitespace_fix).
=cut
sub strip_paramattr { strip_attribute(fixparam($_[0]), $_[1]) }
sub strip_urlattr { strip_attribute(fudgeurl($_[0]), $_[1]) }
#========================================================================
=head2 stripBadHtml(STRING)
Private function. Strips out "bad" HTML by removing unbalanced HTML
tags and sending balanced tags through C<approveTag>. The "unbalanced"
checker is primitive; no "E<lt>" or "E<gt>" tags will are allowed inside
tag attributes (such as E<lt>A NAME="E<gt>"E<gt>), that breaks the tag.
Whitespace is inserted between adjacent tags, so "E<lt>BRE<gt>E<lt>BRE<gt>"
becomes "E<lt>BRE<gt> E<lt>BRE<gt>". And character references are routed
through C<approveCharref>.
=over 4
=item Parameters
=over 4
=item STRING
String to be processed.
=back
=item Return value
Processed string.
=item Dependencies
C<approveTag> function, C<approveCharref> function.
=back
=cut
sub stripBadHtml {
my($str) = @_;
#print STDERR "stripBadHtml 1 '$str'\n";
$str =~ s/<(?!.*?>)//gs;
$str =~ s/<(.*?)>/approveTag($1)/sge;
$str =~ s/></> </g;
# Encode stray >
1 while $str =~ s{
(
(?: ^ | > ) # either beginning of string,
# or another close bracket
[^<]* # not matching open bracket
)
> # close bracket
}{$1>}gx;
# Encode stray <
1 while $str =~ s{
< # open bracket
(
[^>]* # not match close bracket
(?: < | $ ) # either open bracket, or
# end of string
)
}{<$1}gx;
#print STDERR "stripBadHtml 2 '$str'\n";
my $ent = qr/#?[a-zA-Z0-9]+/;
$str =~ s/&(?!$ent;)/&/g;
$str =~ s/&($ent);?/approveCharref($1)/ge;
#print STDERR "stripBadHtml 3 '$str'\n";
return $str;
}
#========================================================================
=head2 processCustomTags(STRING)
Private function. It does processing of special custom tags
(so far, just ECODE).
=over 4
=item Parameters
=over 4
=item STRING
String to be processed.
=back
=item Return value
Processed string.
=item Dependencies
It is meant to be used before C<stripBadHtml> is called, only
from regular posting modes, HTML and PLAINTEXT.
=back
=cut
sub processCustomTags {
my($str) = @_;
my $constants = getCurrentStatic();
## Deal with special ECODE tag (Embedded Code). This tag allows
## embedding the Code postmode in plain or HTML modes. It may be
## of the form:
## <ECODE>literal text</ECODE>
## or, for the case where "</ECODE>" is needed in the text:
## <ECODE END="SOMETAG">literal text</SOMETAG>
##
## SOMETAG must match /^\w+$/.
##
##
## Note that we also strip out leading and trailing newlines
## surrounding the tags, because in plain text mode this can
## be hard to manage, so we manage it for the user.
##
## Also note that this won't work if the site disallows TT
## or BLOCKQUOTE tags.
##
## -- pudge