HTMLDecorator.pm 8.94 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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#
# 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;

30
use Sympa::Language;
31
32
33
34
35
36
37
use Sympa::Regexps;
use Sympa::Tools::Text;

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

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

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

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

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

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

sub _end_document {
    my $self = shift;

    $self->_queue_flush;
80
81
82
}

sub _start {
83
84
85
86
87
88
89
90
91
    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;
        }
92
93
    }

94
95
96
97
98
99
100
    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;
101
    }
102
103
104

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

sub _start_document {
    my $self = shift;

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

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

119
120
121
122
123
124
    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;
125
    }
126
127
128

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

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

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

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

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

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

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

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

sub _queue_shift {
161
162
    my $self = shift;

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

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

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

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

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

234
sub decorate_email_concealed {
235
236
237
238
239
240
241
    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') {
242
243
244
            my $dtext       = Sympa::Tools::Text::decode_html($item->{text});
            my $replacement = $language->gettext('address@concealed');
            if ($dtext =~ s{\b($email_re)\b}{$replacement}g) {
245
246
247
248
                $decorated .= Sympa::Tools::Text::encode_html($dtext);
            } else {
                $decorated .= $item->{text};
            }
249
250
251
        } elsif ($item->{event} eq 'start'
            and $item->{attr}
            and 0 == index(lc($item->{attr}->{href} // ''), 'mailto:')) {
IKEDA Soji's avatar
IKEDA Soji committed
252
            # Empties mailto URL in link target
253
254
255
            my $text = $item->{text};
            $text =~ s{(?<=\bhref=)[^\s>]+}{"mailto:"}gi;
            $decorated .= $text;
256
257
258
259
260
261
262
263
        } else {
            $decorated .= $item->{text};
        }
    }

    return $decorated;
}

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

    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);
309
310
311
312
313
314
315
316
317
318
319
}

1;
__END__

=encoding utf-8

=head1 NAME

Sympa::HTMLDecorator - Decorating HTML texts

320
=head1 SYNOPSIS
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

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

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