HTMLDecorator.pm 7.88 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
# -*- 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 Sympa::Regexps;
use Sympa::Tools::Text;

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

# Class::Singleton constructor.
sub _new_instance {
37
    return shift->SUPER::new(
38
        api_version        => 3,
39
40
41
42
        default_h          => [\&_default, 'self,text'],
        end_h              => [\&_end, 'self,event,tagname,text'],
        end_document_h     => [\&_end_document, 'self'],
        start_h            => [\&_start, 'self,event,tagname,attr,text'],
43
        start_document_h   => [\&_start_document, 'self'],
44
        text_h             => [\&_text, 'self,event,text'],
45
46
47
48
49
        empty_element_tags => 1,
        unbroken_text      => 1,
    );
}

50
51
52
53
54
55
56
57
sub _default {
    my $self = shift;
    my $text = shift;

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

58
sub _end {
59
60
61
62
63
64
65
66
67
68
    my $self = shift;
    my %options;
    @options{qw(event tagname text)} = @_;

    if ($self->_queue_tagname eq 'a') {
        $self->_queue_push(%options);
        if (lc $options{tagname} eq 'a') {
            $self->_queue_flush;
        }
        return;
69
    }
70
71
72
73
74
75
76
77
78

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

sub _end_document {
    my $self = shift;

    $self->_queue_flush;
79
80
81
}

sub _start {
82
83
84
85
86
87
88
89
90
    my $self = shift;
    my %options;
    @options{qw(event tagname attr text)} = @_;

    if ($self->_queue_tagname eq 'a') {
        unless (grep { lc $options{tagname} eq $_ } qw(a script)) {
            $self->_queue_push(%options);
            return;
        }
91
92
    }

93
94
95
96
97
98
99
    if (    lc $options{tagname} eq 'a'
        and $options{attr}
        and $options{attr}->{href}
        and $options{attr}->{href} =~ /\Amailto:/i) {
        $self->_queue_flush;
        $self->_queue_push(%options);
        return;
100
    }
101
102
103

    $self->_queue_flush;
    $self->{_shdOutput} .= $options{text};
104
105
106
107
108
109
}

sub _start_document {
    my $self = shift;

    $self->{_shdOutput} = '';
110
    $self->_queue_clear;
111
112
113
114
}

sub _text {
    my $self = shift;
115
116
    my %options;
    @options{qw(event text)} = @_;
117

118
119
120
121
122
123
    my $dtext    = Sympa::Tools::Text::decode_html($options{text});
    my $email_re = Sympa::Regexps::addrspec();

    if ($self->_queue_tagname eq 'a' or $dtext =~ /\b$email_re\b/) {
        $self->_queue_push(%options);
        return;
124
    }
125
126
127

    $self->_queue_flush;
    $self->{_shdOutput} .= $options{text};
128
129
130
    return;
}

131
132
sub _queue_clear {
    my $self = shift;
133

134
    $self->{_shdEmailQueued} = [];
135
136
}

137
138
sub _queue_flush {
    my $self = shift;
139

140
141
142
143
    return unless @{$self->{_shdEmailQueued}};

    if (my $func = $self->{_shdEmailFunc}) {
        $self->{_shdOutput} .= $self->$func();
144
    } else {
145
146
147
        while (my $item = $self->_queue_shift) {
            $self->{_shdOutput} .= $item->{text};
        }
148
    }
149
    $self->_queue_clear;
150
151
}

152
153
154
155
156
157
158
159
sub _queue_push {
    my $self    = shift;
    my %options = @_;

    push @{$self->{_shdEmailQueued}}, {%options};
}

sub _queue_shift {
160
161
    my $self = shift;

162
163
164
165
166
167
168
169
170
171
172
173
174
    return shift @{$self->{_shdEmailQueued}};
}

sub _queue_tagname {
    my $self = shift;

    if (    @{$self->{_shdEmailQueued}}
        and $self->{_shdEmailQueued}->[0]->{event} eq 'start'
        and lc $self->{_shdEmailQueued}->[0]->{tagname} eq 'a') {
        return 'a';
    } else {
        return '';
    }
175
176
177
178
179
180
181
182
183
184
185
}

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

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

    if ($options{email}) {
        $self->{_shdEmailFunc} =
186
187
              $options{email} eq 'at'         ? \&decorate_email_at
            : $options{email} eq 'javascript' ? \&decorate_email_js
188
189
190
191
192
193
194
195
196
            :                                   undef;
    }
    # No decoration needed.
    return $html unless $self->{_shdEmailFunc};

    if ($html =~ /[<>]/) {
        $self->parse($html);
        $self->eof;
    } else {
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
        $self->_queue_clear;
        $self->_text('text', $html);
        $self->_queue_flush;
    }
    return $self->{_shdOutput};
}

sub decorate_email_at {
    my $self = shift;

    my $decorated = '';
    my $email_re  = Sympa::Regexps::addrspec();
    while (my $item = $self->_queue_shift) {
        if ($item->{event} eq 'text') {
            my $dtext = Sympa::Tools::Text::decode_html($item->{text});
            if ($dtext =~ s{\b($email_re)\b}{join ' AT ', split(/\@/, $1)}eg)
            {
                $decorated .= Sympa::Tools::Text::encode_html($dtext);
            } else {
                $decorated .= $item->{text};
            }
        } elsif ($item->{event} eq 'start') {
            my $text = $item->{text};
            if ($text =~ s{\b(href=\S+)}{join '%20AT%20', split(/\@/, $1)}egi)
            {
                $decorated .= $text;
            } else {
                $decorated .= $item->{text};
            }
        } else {
            $decorated .= $item->{text};
        }
    }
    return $decorated;
}

sub decorate_email_js {
    my $self = shift;

    my $text = '';
    while (my $item = $self->_queue_shift) {
        $text .= $item->{text};
    }

    if (index($text, '<') == 0) {
        return _decorate_email_js($text);
    }

    my $decorated = '';
    my $email_re  = Sympa::Regexps::addrspec();
    my $dtext     = Sympa::Tools::Text::decode_html($text);
    pos $dtext = 0;
    while ($dtext =~ /\G((?:\n|.)*?)\b($email_re)\b/cg) {
        $decorated .=
              Sympa::Tools::Text::encode_html($1)
            . _decorate_email_js(Sympa::Tools::Text::encode_html($2));
    }
    if (pos $dtext) {
        return $decorated
            . Sympa::Tools::Text::encode_html(substr $dtext, pos $dtext);
257
    }
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277

    return $text;
}

sub _decorate_email_js {
    my $text = shift;

    my @texts = map {
        my $str = (defined $_) ? $_ : '';
        $str =~ s/([\\\"])/\\$1/g;
        $str =~ s/\r\n|\r|\n/\\n/g;
        $str =~ s/\t/\\t/g;
        $str;
    } split /\b|(?=\@)|(?<=\@)/, $text;
    return
          sprintf '<script type="text/javascript">' . "\n" . '<!--' . "\n"
        . 'document.write(%s)' . "\n"
        . '// -->' . "\n"
        . '</script>',
        join(" +\n", map { '"' . $_ . '"' } @texts);
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
}

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