HTMLDecorator.pm 9.11 KB
Newer Older
1
2
3
4
5
6
7
8
9
# -*- 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
10
# Copyright (c) 2011, 2012, 2013, 2014, 2015, 2016, 2017 GIP RENATER
11
12
13
# Copyright 2021 The Sympa Community. See the
# AUTHORS.md file at the top-level directory of this distribution and at
# <https://github.com/sympa-community/sympa.git>.
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#
# 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;

33
use Sympa::Language;
34
35
36
37
38
39
40
use Sympa::Regexps;
use Sympa::Tools::Text;

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

# Class::Singleton constructor.
sub _new_instance {
41
    return shift->SUPER::new(
42
        api_version        => 3,
43
44
45
46
        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'],
47
        start_document_h   => [\&_start_document, 'self'],
48
        text_h             => [\&_text, 'self,event,text'],
49
50
51
52
53
        empty_element_tags => 1,
        unbroken_text      => 1,
    );
}

54
55
56
57
58
59
60
61
sub _default {
    my $self = shift;
    my $text = shift;

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

62
sub _end {
63
64
65
66
67
68
69
70
71
72
    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;
73
    }
74
75
76
77
78
79
80
81
82

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

sub _end_document {
    my $self = shift;

    $self->_queue_flush;
83
84
85
}

sub _start {
86
87
88
89
90
91
92
93
94
    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;
        }
95
96
    }

97
98
99
100
101
102
103
    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;
104
    }
105
106
107

    $self->_queue_flush;
    $self->{_shdOutput} .= $options{text};
108
109
110
111
112
113
}

sub _start_document {
    my $self = shift;

    $self->{_shdOutput} = '';
114
    $self->_queue_clear;
115
116
117
118
}

sub _text {
    my $self = shift;
119
120
    my %options;
    @options{qw(event text)} = @_;
121

122
123
124
125
126
127
    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;
128
    }
129
130
131

    $self->_queue_flush;
    $self->{_shdOutput} .= $options{text};
132
133
134
    return;
}

135
136
sub _queue_clear {
    my $self = shift;
137

138
    $self->{_shdEmailQueued} = [];
139
140
}

141
142
sub _queue_flush {
    my $self = shift;
143

144
145
146
147
    return unless @{$self->{_shdEmailQueued}};

    if (my $func = $self->{_shdEmailFunc}) {
        $self->{_shdOutput} .= $self->$func();
148
    } else {
149
150
151
        while (my $item = $self->_queue_shift) {
            $self->{_shdOutput} .= $item->{text};
        }
152
    }
153
    $self->_queue_clear;
154
155
}

156
157
158
159
160
161
162
163
sub _queue_push {
    my $self    = shift;
    my %options = @_;

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

sub _queue_shift {
164
165
    my $self = shift;

166
167
168
169
170
171
172
173
174
175
176
177
178
    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 '';
    }
179
180
181
182
183
184
185
186
187
}

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

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

188
189
190
191
192
193
    $self->{_shdEmailFunc} = {
        at         => \&decorate_email_at,
        concealed  => \&decorate_email_concealed,
        gecos      => \&decorate_email_concealed,    # compat.<=6.2.61b
        javascript => \&decorate_email_js
    }->{$options{email} // ''};
194
195
196
197
198
199
200
    # No decoration needed.
    return $html unless $self->{_shdEmailFunc};

    if ($html =~ /[<>]/) {
        $self->parse($html);
        $self->eof;
    } else {
sikeda's avatar
sikeda committed
201
        $self->_start_document;
202
        $self->_text('text', $html);
sikeda's avatar
sikeda committed
203
        $self->_end_document;
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
    }
    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;
}

237
sub decorate_email_concealed {
238
239
240
241
242
243
244
    my $self = shift;

    my $decorated = '';
    my $email_re  = Sympa::Regexps::addrspec();
    my $language  = Sympa::Language->instance;
    while (my $item = $self->_queue_shift) {
        if ($item->{event} eq 'text') {
245
246
247
            my $dtext       = Sympa::Tools::Text::decode_html($item->{text});
            my $replacement = $language->gettext('address@concealed');
            if ($dtext =~ s{\b($email_re)\b}{$replacement}g) {
248
249
250
251
                $decorated .= Sympa::Tools::Text::encode_html($dtext);
            } else {
                $decorated .= $item->{text};
            }
252
253
254
        } elsif ($item->{event} eq 'start'
            and $item->{attr}
            and 0 == index(lc($item->{attr}->{href} // ''), 'mailto:')) {
IKEDA Soji's avatar
IKEDA Soji committed
255
            # Empties mailto URL in link target
256
257
258
            my $text = $item->{text};
            $text =~ s{(?<=\bhref=)[^\s>]+}{"mailto:"}gi;
            $decorated .= $text;
259
260
261
262
263
264
265
266
        } else {
            $decorated .= $item->{text};
        }
    }

    return $decorated;
}

267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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);
291
    }
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311

    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);
312
313
314
315
316
317
318
319
320
321
322
}

1;
__END__

=encoding utf-8

=head1 NAME

Sympa::HTMLDecorator - Decorating HTML texts

323
=head1 SYNOPSIS
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

  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>.
345
Modifies HTML text.
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

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