View | Details | Raw Unified | Return to bug 23290
Collapse All | Expand All

(-)a/Koha/XSLT/Base.pm (-39 / +25 lines)
Lines 1-6 Link Here
1
package Koha::XSLT::Base;
1
package Koha::XSLT::Base;
2
2
3
# Copyright 2014 Rijksmuseum
3
# Copyright 2014, 2019 Rijksmuseum, Prosentient Systems
4
#
4
#
5
# This file is part of Koha.
5
# This file is part of Koha.
6
#
6
#
Lines 19-30 package Koha::XSLT::Base; Link Here
19
19
20
=head1 NAME
20
=head1 NAME
21
21
22
Koha::XSLT_Handler - Facilitate use of XSLT transformations
22
Koha::XSLT::Base - Facilitate use of XSLT transformations
23
23
24
=head1 SYNOPSIS
24
=head1 SYNOPSIS
25
25
26
    use Koha::XSLT_Handler;
26
    use Koha::XSLT::Base;
27
    my $xslt_engine = Koha::XSLT_Handler->new;
27
    my $xslt_engine = Koha::XSLT::Base->new;
28
    my $output = $xslt_engine->transform($xml, $xsltfilename);
28
    my $output = $xslt_engine->transform($xml, $xsltfilename);
29
    $output = $xslt_engine->transform({ xml => $xml, file => $file });
29
    $output = $xslt_engine->transform({ xml => $xml, file => $file });
30
    $output = $xslt_engine->transform({ xml => $xml, code => $code });
30
    $output = $xslt_engine->transform({ xml => $xml, code => $code });
Lines 43-49 Koha::XSLT_Handler - Facilitate use of XSLT transformations Link Here
43
43
44
=head2 new
44
=head2 new
45
45
46
    Create handler object (via Class::Accessor)
46
    Create handler object
47
47
48
=head2 transform
48
=head2 transform
49
49
Lines 118-123 Koha::XSLT_Handler - Facilitate use of XSLT transformations Link Here
118
use Modern::Perl;
118
use Modern::Perl;
119
use XML::LibXML;
119
use XML::LibXML;
120
use XML::LibXSLT;
120
use XML::LibXSLT;
121
use Koha::XSLT::Security;
121
122
122
use base qw(Class::Accessor);
123
use base qw(Class::Accessor);
123
124
Lines 132-137 use constant XSLTH_ERR_5 => 'XSLTH_ERR_PARSING_DATA'; Link Here
132
use constant XSLTH_ERR_6    => 'XSLTH_ERR_TRANSFORMING';
133
use constant XSLTH_ERR_6    => 'XSLTH_ERR_TRANSFORMING';
133
use constant XSLTH_ERR_7    => 'XSLTH_NO_STRING_PASSED';
134
use constant XSLTH_ERR_7    => 'XSLTH_NO_STRING_PASSED';
134
135
136
=head2 new
137
138
    my $xslt_engine = Koha::XSLT::Base->new;
139
140
=cut
141
142
sub new {
143
    my ($class, $params) = @_;
144
    my $self = $class->SUPER::new($params);
145
    $self->{_security} = Koha::XSLT::Security->new;
146
    $self->{_security}->register_callbacks;
147
    return $self;
148
}
149
135
=head2 transform
150
=head2 transform
136
151
137
    my $output= $xslt_engine->transform( $xml, $xsltfilename, [$format] );
152
    my $output= $xslt_engine->transform( $xml, $xsltfilename, [$format] );
Lines 195-200 sub transform { Link Here
195
210
196
    #parse input and transform
211
    #parse input and transform
197
    my $parser = XML::LibXML->new();
212
    my $parser = XML::LibXML->new();
213
    $self->{_security}->set_parser_options($parser);
198
    my $source = eval { $parser->parse_string($xml) };
214
    my $source = eval { $parser->parse_string($xml) };
199
    if ($@) {
215
    if ($@) {
200
        $self->_set_error( XSLTH_ERR_5, $@ );
216
        $self->_set_error( XSLTH_ERR_5, $@ );
Lines 310-315 sub _load { Link Here
310
326
311
    #load sheet
327
    #load sheet
312
    my $parser = XML::LibXML->new;
328
    my $parser = XML::LibXML->new;
329
    $self->{_security}->set_parser_options($parser);
313
    my $style_doc = eval {
330
    my $style_doc = eval {
314
        $parser->load_xml( $self->_load_xml_args($filename, $code) )
331
        $parser->load_xml( $self->_load_xml_args($filename, $code) )
315
    };
332
    };
Lines 318-357 sub _load { Link Here
318
        return;
335
        return;
319
    }
336
    }
320
337
321
    my $security = XML::LibXSLT::Security->new();
322
    $security->register_callback( read_file  => sub {
323
        warn "read_file called in XML::LibXSLT";
324
        #i.e. when using the exsl:document() element or document() function (to read a XML file)
325
        my ($tctxt,$value) = @_;
326
        return 0;
327
    });
328
    $security->register_callback( write_file => sub {
329
        warn "write_file called in XML::LibXSLT";
330
        #i.e. when using the exsl:document element (or document() function?) (to write an output file of many possible types)
331
        #e.g.
332
        #<exsl:document href="file:///tmp/breached.txt">
333
        #   <xsl:text>breached!</xsl:text>
334
        #</exsl:document>
335
        my ($tctxt,$value) = @_;
336
        return 0;
337
    });
338
    $security->register_callback( read_net   => sub {
339
        warn "read_net called in XML::LibXSLT";
340
        #i.e. when using the document() function (to read XML from the network)
341
        #e.g. <xsl:copy-of select="document('http://localhost')" />
342
        my ($tctxt,$value) = @_;
343
        return 0;
344
    });
345
    $security->register_callback( write_net  => sub {
346
        warn "write_net called in XML::LibXSLT";
347
        #NOTE: it's unknown how one would invoke this, but covering our bases anyway
348
        my ($tctxt,$value) = @_;
349
        return 0;
350
    });
351
352
    #parse sheet
338
    #parse sheet
353
    my $xslt = XML::LibXSLT->new;
339
    my $xslt = XML::LibXSLT->new;
354
    $xslt->security_callbacks( $security );
340
    $self->{_security}->set_callbacks($xslt);
355
341
356
    $rv = $code? $digest.$codelen: $filename;
342
    $rv = $code? $digest.$codelen: $filename;
357
    $self->{xslt_hash}->{$rv} = eval { $xslt->parse_stylesheet($style_doc) };
343
    $self->{xslt_hash}->{$rv} = eval { $xslt->parse_stylesheet($style_doc) };
Lines 375-386 sub _set_error { Link Here
375
    my ( $self, $errcode, $warn ) = @_;
361
    my ( $self, $errcode, $warn ) = @_;
376
362
377
    $self->{err} = $errcode; #set or clear error
363
    $self->{err} = $errcode; #set or clear error
378
    warn 'XSLT_Handler: '. $warn if $warn && $self->{print_warns};
364
    warn 'XSLT::Base: '. $warn if $warn && $self->{print_warns};
379
}
365
}
380
366
381
=head1 AUTHOR
367
=head1 AUTHOR
382
368
383
    Marcel de Rooy, Rijksmuseum Netherlands
369
    Marcel de Rooy, Rijksmuseum Netherlands
370
    David Cook, Prosentient Systems
384
371
385
=cut
372
=cut
386
373
387
- 

Return to bug 23290