-
Notifications
You must be signed in to change notification settings - Fork 8
/
Data.pm
executable file
·3640 lines (2748 loc) · 84.3 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-2005 by Open Source Technology Group. 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 qw(md5_hex md5_base64);
use Email::Valid;
use HTML::Entities qw(:DEFAULT %char2entity %entity2char);
use HTML::FormatText;
use HTML::TreeBuilder;
use POSIX qw(UINT_MAX);
use Safe;
use Slash::Constants qw(:strip);
use Slash::Utility::Environment;
use URI;
#use URI::Find 0.15;
use XML::Parser;
use Lingua::Stem;
use base 'Exporter';
use vars qw($VERSION @EXPORT);
($VERSION) = ' $Revision$ ' =~ /\$Revision:\s+([^\s]+)/;
@EXPORT = qw(
addDomainTags
createStoryTopicData
slashizeLinks
approveCharref
parseDomainTags
parseSlashizedLinks
balanceTags
changePassword
chopEntity
cleanRedirectUrl
cleanRedirectUrlFromForm
commify
countTotalVisibleKids
countWords
createSid
decode_entities
ellipsify
emailValid
encryptPassword
findWords
fixHref
fixint
fixparam
fixurl
fudgeurl
fullhost_to_domain
formatDate
getArmoredEmail
createLogToken
grepn
html2text
issueAge
nickFix
nick2matchname
noFollow
regexSid
root2abs
roundrand
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
submitDomainAllowed
timeCalc
url2html
url2abs
urlFromSite
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();
my $nc = $constants->{nick_chars} || join("", 'a' .. 'z');
$nick =~ s/\s+/ /g;
$nick =~ s/[^$nc]+//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;
}
#========================================================================
# If you change createSid() for your site, change regexSid() too.
# If your site will have multiple formats of sids, you'll want this
# to continue matching the old formats too.
sub regexSid {
return qr{\b(\d{2}/\d{2}/\d{2}/\d{3,8}|\d{1,8})\b};
}
#========================================================================
=head2 emailValid(EMAIL)
Returns true if email is valid, false otherwise.
=over 4
=item Parameters
=over 4
=item EMAIL
Email address to check.
=back
=item Return value
True if email is valid, false otherwise.
=back
=cut
sub emailValid {
my($email) = @_;
my $constants = getCurrentStatic();
return 0 if $constants->{email_domains_invalid}
&& ref($constants->{email_domains_invalid})
&& $email =~ $constants->{email_domains_invalid};
my $valid = Email::Valid->new;
return 0 unless $valid->rfc822($email);
return 1;
}
#========================================================================
=head2 issueAge(ISSUE)
Returns the "age" in days of an issue, given in issue mode form: yyyymmdd.
=over 4
=item Parameters
=over 4
=item ISSUE
Which issue, in yyyymmdd form (matches /^\d{8}$/)
=back
=item Return value
Age in days of that issue (a decimal number). Takes current user's
timezone into account. Return value of 0 indicates error.
=back
=cut
sub issueAge {
my($issue) = @_;
return 0 unless $issue =~ /^\d{8}$/;
my $user = getCurrentUser();
my $issue_unix_timestamp = timeCalc("${issue}0000", "%s", -$user->{off_set});
my $age = (time - $issue_unix_timestamp) / 86400;
$age = 0.00001 if $age == 0; # don't return 0 on success
return $age;
}
#========================================================================
=head2 submitDomainAllowed(DOMAIN)
Returns true if domain is allowed, false otherwise.
=over 4
=item Parameters
=over 4
=item DOMAIN
host domain to check.
=back
=item Return value
True if domain is valid, false otherwise.
=back
=cut
sub submitDomainAllowed {
my($domain) = @_;
my $constants = getCurrentStatic();
return 0 if $constants->{submit_domains_invalid}
&& ref($constants->{submit_domains_invalid})
&& $domain =~ $constants->{submit_domains_invalid};
return 1;
}
#========================================================================
=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 getCurrentSkin('absolutedir_secure');
} else {
return getCurrentSkin('absolutedir');
}
}
#========================================================================
=head2 roundrand()
Rounds a real value to an integer value, randomly, with the
two options weighted in linear proportion to the fractional
component. E.g. 1.3 is 30% likely to round to 1, 70% to 2.
And -4.9 is 90% likely to round to -5, 10% to -4.
=over 4
=item Return value
Input value converted to integer.
=back
=cut
sub roundrand {
my($real) = @_;
return 0 if !$real;
my $i = int($real);
$i-- if $real < 0;
my $frac = $real - $i;
return( (rand(1) >= $frac) ? $i : $i+1 );
}
#========================================================================
=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 cleanRedirectUrl(URL)
Clean an untrusted URL for safe redirection. We do not redirect URLs received
from outside Slash (such as in $form->{returnto}) to arbitrary sites, only
to ourself.
=over 4
=item Parameters
=over 4
=item URL
URL to clean.
=back
=item Return value
Fixed URL.
=back
=cut
sub cleanRedirectUrl {
my($redirect) = @_;
my $gSkin = getCurrentSkin();
if (urlFromSite($redirect)) {
my $base = root2abs();
return URI->new_abs($redirect || $gSkin->{rootdir}, $base);
} else {
return url2abs($gSkin->{rootdir});
}
}
sub urlFromSite {
my($url) = @_;
my $constants = getCurrentStatic();
my $user = getCurrentUser();
my $gSkin = getCurrentSkin();
# We absolutize the return-to URL to our domain just to
# be sure nobody can use the site as a redirection service.
# We decide whether to use the secure homepage or not
# based on whether the current page is secure.
my $base = root2abs();
my $clean = URI->new_abs($url || $gSkin->{rootdir}, $base);
my @site_domain = split m/\./, $gSkin->{basedomain};
my $site_domain = join '.', @site_domain[-2, -1];
$site_domain =~ s/:.+$//; # strip port, if available
my @host = split m/\./, ($clean->can('host') ? $clean->host : '');
my $host = join '.', @host[-2, -1];
return $site_domain eq $host;
}
#========================================================================
sub cleanRedirectUrlFromForm {
my($redirect_formname) = @_;
my $constants = getCurrentStatic();
my $gSkin = getCurrentSkin();
my $form = getCurrentForm();
my $formname = $redirect_formname ? "returnto_$redirect_formname" : "returnto";
my $formname_confirm = "${formname}_confirm";
my $returnto = $form->{$formname} || "";
return undef if !$returnto;
my $returnto_confirm = $form->{$formname_confirm} || "";
my $returnto_passwd = $constants->{returnto_passwd};
my $confirmed = md5_hex("$returnto$returnto_passwd") eq $returnto_confirm;
if ($confirmed) {
# The URL and the password have been concatted together
# and confirmed with the MD5, so we know it comes from a
# trusted source. Approve it.
return $returnto;
} else {
# There is no proper MD5, so don't redirect.
return undef;
}
}
#========================================================================
=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 createLogToken()
Return new random 22-character logtoken, composed of \w chars.
=over 4
=item Return value
Return a random password that matches /^\w{22}$/.
We're only pulling out 3 chars each time thru this loop, so we only
need (and trust) about 18 bits worth of randomness. We re-seed srand
periodically to try to get more randomness into the mix ("it uses a
semirandom value supplied by the kernel (if it supports the /dev/urandom
device)", says the Camel book). I don't think I'm doing anything
mathematically dumb to introduce any predictability into this, so it
should be fine, wasteful of a few microseconds perhaps, ugly perhaps, but
the 22-char value it returns should have very close to 131 bits of
randomness.
=back
=cut
sub createLogToken {
my $str = "";
my $need_srand = 0;
while (length($str) < 22) {
if ($need_srand) {
srand();
$need_srand = 0;
}
my $r = rand(UINT_MAX) . ":" . rand(UINT_MAX);
my $md5 = md5_base64($r);
$md5 =~ tr/A-Za-z0-9//cd;
$str .= substr($md5, int(rand 8) + 5, 3);
$need_srand = 1 if rand() < 0.3;
}
return substr($str, 0, 22);
}
#========================================================================
=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',
);
# protect the hash by just returning it, for external use only
sub _latin1_to_ascii { %latin1_to_ascii }
sub _charsetConvert {
my($char, $constants) = @_;
$constants ||= getCurrentStatic();
my $str = '';
if ($constants->{draconian_charset_convert}) {
if ($constants->{draconian_charrefs}) {
if ($constants->{good_numeric}{$char}) {
$str = sprintf("&#%u;", $char);
} else { # see if char is in %good_entity
my $ent = $char2entity{chr $char};
if ($ent) {
(my $data = $ent) =~ s/^&(\w+);$/$1/;
$str = $ent if $constants->{good_entity}{$data};
}
}
}
# fall back
$str ||= $latin1_to_ascii{$char};
}
# fall further back
$str ||= sprintf("&#%u;", $char);
return $str;
}
sub _fixupCharrefs {
my $constants = getCurrentStatic();
return if $constants->{bad_numeric};
# At the moment, unless the "draconian" rule is set, only
# entities that change the direction of text are forbidden.
# For more information, see
# <http://www.w3.org/TR/html4/struct/dirlang.html#bidirection>
# and <http://www.htmlhelp.com/reference/html40/special/bdo.html>.
$constants->{bad_numeric} = { map { $_, 1 } @{$constants->{charrefs_bad_numeric}} };
$constants->{bad_entity} = { map { $_, 1 } @{$constants->{charrefs_bad_entity}} };
$constants->{good_numeric} = { map { $_, 1 } @{$constants->{charrefs_good_numeric}},
grep { $_ < 128 || $_ > 159 } keys %latin1_to_ascii };
$constants->{good_entity} = { map { $_, 1 } @{$constants->{charrefs_good_entity}}, qw(apos quot),
grep { s/^&(\w+);$/$1/ } map { $char2entity{chr $_} }
grep { $_ < 128 || $_ > 159 } keys %latin1_to_ascii };
}
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{