File: Encode.pm

package info (click to toggle)
otrs2 2.0.4p01-17
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 7,892 kB
  • ctags: 4,437
  • sloc: perl: 81,607; xml: 8,135; sql: 8,013; sh: 1,113; makefile: 22; php: 16
file content (339 lines) | stat: -rw-r--r-- 8,137 bytes parent folder | download | duplicates (2)
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
# --
# Kernel/System/Encode.pm - character encodings
# Copyright (C) 2001-2004 Martin Edenhofer <martin+code@otrs.org>
# --
# $Id: Encode.pm,v 1.12 2004/12/23 21:08:57 martin Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (GPL). If you
# did not receive this file, see http://www.gnu.org/licenses/gpl.txt.
# --

package Kernel::System::Encode;

use strict;

use vars qw(@ISA $VERSION);

$VERSION = '$Revision: 1.12 $';
$VERSION =~ s/^\$.*:\W(.*)\W.+?$/$1/;

=head1 NAME

Kernel::System::Encode - character encodings

=head1 SYNOPSIS

This module will use Perl's Encode module (Perl 5.8.0 or higher required).
If the Perl version is lower then 5.8.0, no encoding will be possible. The
return string is still the same charset.

=head1 PUBLIC INTERFACE

=over 4

=cut

=item new()

create a language object

  use Kernel::Config;
  use Kernel::System::Encode;

  my $ConfigObject = Kernel::Config->new();

  my $EncodeObject = Kernel::System::Encode->new(
      ConfigObject => $ConfigObject,
  );

=cut

sub new {
    my $Type = shift;
    my %Param = @_;

    # allocate new hash for object
    my $Self = {};
    bless ($Self, $Type);

    # get common objects
    foreach (keys %Param) {
        $Self->{$_} = $Param{$_};
    }
    # check needed objects
    foreach (qw(ConfigObject)) {
        die "Got no $_!" if (!$Self->{$_});
    }
    # 0=off; 1=on;
    $Self->{Debug} = 0;

    # check if Perl 5.8.0 encode is available
    if (eval "require Encode") {
        $Self->{CharsetEncodeSupported} = 1;
        $Self->SetIO(\*STDOUT, \*STDERR);
    }
    else {
        if ($Self->{Debug}) {
            print STDERR "Charset encode not supported withyour perl version!\n";
        }
    }
    return $Self;
}

=item EncodeSupported()

Returns true or false if charset encoding is possible (depends on Perl version =< 5.8.0).

  if ($EncodeObject->EncodeSupported()) {
      print "Charset encoding is possible!\n";
  }
  else {
      print "Sorry, charset encoding is not possible!\n";
  }

=cut

sub EncodeSupported {
    my $Self = shift;
    return $Self->{CharsetEncodeSupported};
}

=item EncodeInternalUsed()

Returns the internal used charset if possible. E. g. if "EncodeSupported()"
is true and Kernel/Config.pm "DefaultCharset" is "utf-8", then utf-8 is
the internal charset. It returns false if no internal charset (utf-8) is
used.

  my $Charset = $EncodeObject->EncodeInternalUsed();

=cut

sub EncodeInternalUsed {
    my $Self = shift;
    if ($Self->{CharsetEncodeSupported} && $Self->{ConfigObject}->Get('DefaultCharset') =~ /^utf(-8|8)$/i) {
        return 'utf-8';
    }
    else {
        return;
    }
}

=item EncodeFrontendUsed()

Returns the used frontend charset if possible. E. g. if "EncodeSupported()"
is true and Kernel/Config.pm "DefaultCharset" is "utf-8", then utf-8 is
the frontend charset. It returns false if no frontend charset (utf-8) is
used (then the translation charset (from translation file) will be used).

  my $Charset = $EncodeObject->EncodeFrontendUsed();

=cut

sub EncodeFrontendUsed {
    my $Self = shift;
    if ($Self->{CharsetEncodeSupported} && $Self->{ConfigObject}->Get('DefaultCharset') =~ /^utf(-8|8)$/i) {
        return 'utf-8';
    }
    else {
        return;
    }
}

=item Convert()

Convert one charset to an other charset.

  my $utf8 = $EncodeObject->Convert(
      Text => $iso_8859_1_string,
      From => 'iso-8859-1',
      To => 'utf-8',
  );

  my $iso_8859_1 = $EncodeObject->Convert(
      Text => $utf-8_string,
      From => 'utf-8',
      To => 'iso-8859-1',
  );

There is also a Force => 1 option if you need to force the
already converted string.

=cut

sub Convert {
    my $Self = shift;
    my %Param = @_;
    if (!defined($Param{Text}) || $Param{Text} eq '') {
        return;
    }
    # check needed stuff
    foreach (qw(From To)) {
      if (!defined($Param{$_})) {
        print STDERR "Need $_!\n";
        return;
      }
    }
    # if there is no charset encode supported (min. Perl 5.8.0)
    if (!$Self->{CharsetEncodeSupported}) {
        return $Param{Text};
    }
    # if no encode is needed
    if ($Param{From} =~ /^$Param{To}$/i) {
        if ($Param{To} =~ /^utf(-8|8)/i) {
            Encode::_utf8_on($Param{Text});
        }
        return $Param{Text};
    }
    # encode is needed
    else {
        if ($Param{Force}) {
            Encode::_utf8_off($Param{Text});
        }
        if (! eval { Encode::from_to($Param{Text}, $Param{From}, $Param{To}) } ) {
            print STDERR "Charset encode '$Param{From}' -=> '$Param{To}' ($Param{Text}) not supported!\n";
        }
        else {
          # set utf-8 flag
          if ($Param{To} =~ /^utf(8|-8)$/i) {
                Encode::encode_utf8($Param{Text});
                Encode::_utf8_on($Param{Text});
          }
          if ($Self->{Debug}) {
              print STDERR "Charset encode '$Param{From}' -=> '$Param{To}' ($Param{Text})!\n";
          }
        }
        return $Param{Text};
    }
}

=item SetIO()

Set array of file handles to utf-8 output.

  $EncodeObject->SetIO(\*STDOUT, \*STDERR);

=cut

sub SetIO {
    my $Self = shift;
    my @Array = @_;
    if ($Self->{CharsetEncodeSupported} && $Self->EncodeFrontendUsed() && $Self->EncodeFrontendUsed() =~ /utf(-8|8)/i) {
        foreach (@Array) {
            if (defined($_) && ref($_) eq 'GLOB') {
                binmode($_, ":utf8");
            }
        }
    }
    return;
}

=item Encode()

Convert internal used charset (e. g. utf-8) into given charset (utf-8), if
"EncodeInternalUsed()" returns one. Should be used on all I/O interfaces
if data is already utf-8.

  $EncodeObject->Encode(\$String);

=cut

sub Encode {
    my $Self = shift;
    my $What = shift;
    # internel charset
    if ($Self->{CharsetEncodeSupported} && $Self->EncodeFrontendUsed() && $Self->EncodeFrontendUsed() =~ /utf(-8|8)/i) {
        if (defined ($What) && ref($What) eq 'ARRAY') {
            foreach (@{$What}) {
                if (defined($_)) {
                    Encode::encode_utf8($_);
                    Encode::_utf8_on($_);
                }
            }
        }
        elsif (defined ($What) && ref($What) eq 'SCALAR') {
            if (defined(${$What})) {
                Encode::encode_utf8(${$What});
                Encode::_utf8_on(${$What});
            }
        }
        elsif (defined($What)) {
            Encode::encode_utf8($What);
            Encode::_utf8_on($What);
        }
    }
    return 1;
}

=item Decode()

Convert given charset into the internal used charset (utf-8), if
"EncodeInternalUsed()" returns one. Should be used on all I/O interfaces.

  my $String = $EncodeObject->Decode(
      Text => $String,
      From => $SourceCharset,
  );

=cut

sub Decode {
    my $Self = shift;
    my %Param = @_;
    if (!defined $Param{Text}) {
        return;
    }
    # check needed stuff
    foreach (qw(From)) {
      if (!defined($Param{$_})) {
        print STDERR "Need $_!\n";
        return;
      }
    }
    # internel charset
    if ($Self->EncodeInternalUsed()) {
        return $Self->Convert(%Param, To => $Self->EncodeInternalUsed());
    }
    else {
        return $Param{Text};
    }
}

=item EncodeOutput()

Convert utf8 to a sequence of octets. All possible characters have
a UTF-8 representation so this function cannot fail.

This should be used in for output of utf8 chars.

  $EncodeObject->EncodeOutput(\$String);

=cut

sub EncodeOutput {
    my $Self = shift;
    my $What = shift;
    # internel charset
    if ($Self->{CharsetEncodeSupported} && $Self->EncodeFrontendUsed() && $Self->EncodeFrontendUsed() =~ /utf(-8|8)/i) {
        ${$What} = Encode::encode_utf8(${$What});
    }
}

1;

=head1 TERMS AND CONDITIONS

This software is part of the OTRS project (http://otrs.org/).

This software comes with ABSOLUTELY NO WARRANTY. For details, see
the enclosed file COPYING for license information (GPL). If you
did not receive this file, see http://www.gnu.org/licenses/gpl.txt.

=cut

=head1 VERSION

$Revision: 1.12 $ $Date: 2004/12/23 21:08:57 $

=cut