HTMLDecorator.pm 6.44 KB
Newer Older
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
# -*- indent-tabs-mode: nil; -*-
# vim:ft=perl:et:sw=4
# $Id$

# Sympa - SYsteme de Multi-Postage Automatique
#
# Copyright (c) 1997, 1998, 1999 Institut Pasteur & Christophe Wolfhugel
# Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
# 2006, 2007, 2008, 2009, 2010, 2011 Comite Reseau des Universites
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016 GIP RENATER
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

package Sympa::HTMLDecorator;

use strict;
use warnings;
use Encode qw();

use Sympa::Regexps;
use Sympa::Tools::Text;

use base qw(HTML::Parser Class::Singleton);

# Class::Singleton constructor.
sub _new_instance {
    my $class = shift;

    $class->SUPER::new(
        api_version        => 3,
        default_h          => [\&_output, 'self,text'],
        end_h              => [\&_end, 'self,tagname,text'],
        start_h            => [\&_start, 'self,tagname,attr,text'],
        start_document_h   => [\&_start_document, 'self'],
        text_h             => [\&_text, 'self,text'],
        empty_element_tags => 1,
        unbroken_text      => 1,
    );
}

sub _end {
    my $self    = shift;
    my $tagname = shift;
    my $text    = shift;

    my $func = $self->{_shdEmailFunc};
    unless ($func
        and $self->{_shdStart}
        and lc $tagname eq $self->{_shdStart}) {
        $self->{_shdOutput} .= $text;
    } else {
        $self->{_shdOutput} .= $func->($text, '', '');
    }
    delete $self->{_shdStart};
}

sub _start {
    my $self    = shift;
    my $tagname = shift;
    my $attr    = shift;
    my $text    = shift;

    my $func = $self->{_shdEmailFunc};
    unless ($func
        and lc $tagname eq 'a'
        and $attr
        and $attr->{href}
        and $attr->{href} =~ /\Amailto:/i) {
        $self->{_shdOutput} .= $text;
        return;
    }

    if ($text =~ /\A(.+\bhref\s*=\s*([\"\']?)mailto:)([^\"]*)(\2.+)\z/is) {
        my ($before, $dtext, $after) =
            ($1, Sympa::Tools::Text::decode_html($3), $4);
        $self->{_shdOutput} .= $func->($before, $dtext, $after);
        $self->{_shdStart} = lc $tagname;
    } else {
        $self->{_shdOutput} .= $text;
    }
}

sub _start_document {
    my $self = shift;

    $self->{_shdOutput} = '';
    delete $self->{_shdStart};
}

sub _text {
    my $self = shift;
    my $text = shift;

    if (my $func = $self->{_shdEmailFunc}) {
        my $dtext    = Sympa::Tools::Text::decode_html($text);
        my $email_re = Sympa::Regexps::addrspec();

        my $decorated = '';
        pos $dtext = 0;
        while ($dtext =~ /\G((?:\s|.)*?)\b($email_re)\b/cg) {
            my ($t, $email) = ($1, $2);
            $decorated .= Sympa::Tools::Text::encode_html($t);
            $decorated .= $func->('', $email, '');
        }
        if (pos $dtext) {
            $self->{_shdOutput} .= $decorated;
            $self->{_shdOutput} .=
                Sympa::Tools::Text::encode_html(substr $dtext, pos $dtext);
            return;
        }
    }
    $self->{_shdOutput} .= $text;
    return;
}

sub _decorate_email_at {
    my $before = shift;
    my $dtext  = shift;
    my $after  = shift;

    $dtext =~ s/\@/ AT /g;
    return $before . Sympa::Tools::Text::encode_html($dtext) . $after;
}

sub _decorate_email_js {
    my $before = shift;
    my $dtext  = shift;
    my $after  = shift;

    my ($local, $domain) = split /\@/, $dtext, 2;
    ($local, $domain) = map {
        my $str = (defined $_) ? $_ : '';
        $str = Sympa::Tools::Text::encode_html($str);
        $str;
    } ($local, $domain);
    ($before, $local, $domain, $after) = map {
        my $str = (defined $_) ? $_ : '';
        $str =~ s/([\\\"])/\\$1/g;
        $str =~ s/\r\n|\r|\n/\\n/g;
        $str =~ s/\t/\\t/g;
        $str;
    } ($before, $local, $domain, $after);

    if (length $domain) {
        return
              sprintf '<script type="text/javascript">' . "\n" . '<!--' . "\n"
            . 'document.write("%s%s" + "@" + "%s%s")' . "\n"
            . '// -->' . "\n"
            . '</script>', $before, $local, $domain, $after;
    } else {
        return
              sprintf '<script type="text/javascript">' . "\n" . '<!--' . "\n"
            . 'document.write("%s%s%s")' . "\n"
            . '// -->' . "\n"
            . '</script>', $before, $local, $after;
    }
}

sub _output {
    my $self = shift;
    my $text = shift;

    $self->{_shdOutput} .= $text;
}

sub decorate {
    my $self    = shift;
    my $html    = shift;
    my %options = @_;

    return $html unless defined $html and length $html;

    if ($options{email}) {
        $self->{_shdEmailFunc} =
              $options{email} eq 'at'         ? \&_decorate_email_at
            : $options{email} eq 'javascript' ? \&_decorate_email_js
            :                                   undef;
    }
    # No decoration needed.
    return $html unless $self->{_shdEmailFunc};

    if ($html =~ /[<>]/) {
        $self->parse($html);
        $self->eof;
        return $self->{_shdOutput};
    } else {
        $self->_text($html);
        return $self->{_shdOutput};
    }
}

1;
__END__

=encoding utf-8

=head1 NAME

Sympa::HTMLDecorator - Decorating HTML texts

=head1 SYNOPSYS

  use Sympa::HTMLDecorator;
  $decorator = Sympa::HTMLDecorator->instance;
  $ouput = $decorator->decorate($html, email => 'javascript');

=head1 DESCRIPTION

L<Sympa::HTMLDecorator> transforms HTML texts.

=head2 Methods

=over

=item instance ( )

I<Constructor>.
Returns singleton instance of this class.

=item decorate ( $html, email =E<gt> $mode )

I<Instance method>.
Modifys HTML text.

Parameters:

=over

=item $html

A text including HTML document or fragment.
It must be encoded by UTF-8.

=item email =E<gt> $mode

Transformation mode.
C<'at'> replaces C<@> in email addresses.
C<'javascript'> obfuscates emails using JavaScript code.

=back

Returns:

Modified text.

=back

=head1 SEE ALSO

L<Sympa::HTMLSanitizer>.

=head1 HISTORY

L<Sympa::HTMLDecorator> appeared on Sympa 6.2.14.

=cut