summaryrefslogtreecommitdiff
path: root/rt/lib/RT/Template_Overlay.pm
diff options
context:
space:
mode:
Diffstat (limited to 'rt/lib/RT/Template_Overlay.pm')
-rw-r--r--rt/lib/RT/Template_Overlay.pm289
1 files changed, 170 insertions, 119 deletions
diff --git a/rt/lib/RT/Template_Overlay.pm b/rt/lib/RT/Template_Overlay.pm
index 9706404..fce37bc 100644
--- a/rt/lib/RT/Template_Overlay.pm
+++ b/rt/lib/RT/Template_Overlay.pm
@@ -1,8 +1,8 @@
# BEGIN BPS TAGGED BLOCK {{{
#
# COPYRIGHT:
-#
-# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
+#
+# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
# <jesse@bestpractical.com>
#
# (Except where explicitly superseded by other copyright notices)
@@ -45,6 +45,7 @@
# those contributions and any derivatives thereof.
#
# END BPS TAGGED BLOCK }}}
+
# Portions Copyright 2000 Tobias Brox <tobix@cpan.org>
=head1 NAME
@@ -60,11 +61,6 @@
=head1 METHODS
-=begin testing
-
-ok(require RT::Template);
-
-=end testing
=cut
@@ -72,15 +68,12 @@ ok(require RT::Template);
package RT::Template;
use strict;
+use warnings;
no warnings qw(redefine);
use Text::Template;
use MIME::Entity;
use MIME::Parser;
-use File::Temp qw /tempdir/;
-
-
-# {{{ sub _Accessible
sub _Accessible {
my $self = shift;
@@ -99,10 +92,6 @@ sub _Accessible {
return $self->SUPER::_Accessible( @_, %Cols );
}
-# }}}
-
-# {{{ sub _Set
-
sub _Set {
my $self = shift;
@@ -112,27 +101,10 @@ sub _Set {
return $self->SUPER::_Set( @_ );
}
-# }}}
-
-# {{{ sub _Value
-
=head2 _Value
-Takes the name of a table column.
-Returns its value as a string, if the user passes an ACL check
-
-
-=begin testing
-
-my $t = RT::Template->new($RT::SystemUser);
-$t->Create(Name => "Foo", Queue => 1);
-my $t2 = RT::Template->new($RT::Nobody);
-$t2->Load($t->Id);
-ok($t2->QueueObj->id, "Got the template's queue objet");
-
-=end testing
-
-
+Takes the name of a table column. Returns its value as a string,
+if the user passes an ACL check, otherwise returns undef.
=cut
@@ -146,13 +118,15 @@ sub _Value {
}
-# }}}
-
-# {{{ sub Load
-
=head2 Load <identifer>
-Load a template, either by number or by name
+Load a template, either by number or by name.
+
+Note that loading templates by name using this method B<is
+ambiguous>. Several queues may have template with the same name
+and as well global template with the same name may exist.
+Use L</LoadGlobalTemplate> and/or L<LoadQueueTemplate> to get
+precise result.
=cut
@@ -167,10 +141,6 @@ sub Load {
return $self->LoadById( $identifier );
}
-# }}}
-
-# {{{ sub LoadGlobalTemplate
-
=head2 LoadGlobalTemplate NAME
Load the global template with the name NAME
@@ -179,18 +149,28 @@ Load the global template with the name NAME
sub LoadGlobalTemplate {
my $self = shift;
- my $id = shift;
+ my $name = shift;
- return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) );
+ return ( $self->LoadQueueTemplate( Queue => 0, Name => $name ) );
}
-# }}}
+=head2 LoadQueueTemplate (Queue => QUEUEID, Name => NAME)
-# {{{ sub LoadQueueTemplate
+Loads the Queue template named NAME for Queue QUEUE.
-=head2 LoadQueueTemplate (Queue => QUEUEID, Name => NAME)
+Note that this method doesn't load a global template with the same name
+if template in the queue doesn't exist. THe following code can be used:
-Loads the Queue template named NAME for Queue QUEUE.
+ $template->LoadQueueTemplate( Queue => $queue_id, Name => $template_name );
+ unless ( $template->id ) {
+ $template->LoadGlobalTemplate( $template_name );
+ unless ( $template->id ) {
+ # no template
+ ...
+ }
+ }
+ # ok, template either queue's or global
+ ...
=cut
@@ -206,10 +186,6 @@ sub LoadQueueTemplate {
}
-# }}}
-
-# {{{ sub Create
-
=head2 Create
Takes a paramhash of Content, Queue, Name and Description.
@@ -221,7 +197,6 @@ template.
Returns the Template's id # if the create was successful. Returns undef for
unknown database failure.
-
=cut
sub Create {
@@ -237,7 +212,7 @@ sub Create {
unless ( $args{'Queue'} ) {
unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) {
- return ( undef, $self->loc('Permission denied') );
+ return ( undef, $self->loc('Permission Denied') );
}
$args{'Queue'} = 0;
}
@@ -246,7 +221,7 @@ sub Create {
$QueueObj->Load( $args{'Queue'} ) || return ( undef, $self->loc('Invalid queue') );
unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
- return ( undef, $self->loc('Permission denied') );
+ return ( undef, $self->loc('Permission Denied') );
}
$args{'Queue'} = $QueueObj->Id;
}
@@ -262,10 +237,6 @@ sub Create {
}
-# }}}
-
-# {{{ sub Delete
-
=head2 Delete
Delete this template.
@@ -282,59 +253,105 @@ sub Delete {
return ( $self->SUPER::Delete(@_) );
}
-# }}}
+=head2 IsEmpty
-# {{{ sub MIMEObj
-sub MIMEObj {
+Returns true value if content of the template is empty, otherwise
+returns false.
+
+=cut
+
+sub IsEmpty {
my $self = shift;
- return ( $self->{'MIMEObj'} );
+ my $content = $self->Content;
+ return 0 if defined $content && length $content;
+ return 1;
}
-# }}}
+=head2 MIMEObj
-# {{{ sub Parse
+Returns L<MIME::Entity> object parsed using L</Parse> method. Returns
+undef if last call to L</Parse> failed or never be called.
+
+Note that content of the template is UTF-8, but L<MIME::Parser> is not
+good at handling it and all data of the entity should be treated as
+octets and converted to perl strings using Encode::decode_utf8 or
+something else.
+
+=cut
+
+sub MIMEObj {
+ my $self = shift;
+ return ( $self->{'MIMEObj'} );
+}
=head2 Parse
- This routine performs Text::Template parsing on the template and then
- imports the results into a MIME::Entity so we can really use it
+This routine performs L<Text::Template> parsing on the template and then
+imports the results into a L<MIME::Entity> so we can really use it. Use
+L</MIMEObj> method to get the L<MIME::Entity> object.
- Takes a hash containing Argument, TicketObj, and TransactionObj. TicketObj
- and TransactionObj are not mandatory, but highly recommended.
+Takes a hash containing Argument, TicketObj, and TransactionObj and other
+arguments that will be available in the template's code. TicketObj and
+TransactionObj are not mandatory, but highly recommended.
- It returns a tuple of (val, message)
- If val is 0, the message contains an error message
+It returns a tuple of (val, message). If val is false, the message contains
+an error message.
=cut
sub Parse {
my $self = shift;
+ my ($rv, $msg);
- #We're passing in whatever we were passed. it's destined for _ParseContent
- my ($content, $msg) = $self->_ParseContent(@_);
- return ( 0, $msg ) unless defined $content;
- #Lets build our mime Entity
+ if ($self->Content =~ m{^Content-Type:\s+text/html\b}im) {
+ local $RT::Transaction::PreferredContentType = 'text/html';
+ ($rv, $msg) = $self->_Parse(@_);
+ }
+ else {
+ ($rv, $msg) = $self->_Parse(@_);
+ }
- my $parser = MIME::Parser->new();
+ return ($rv, $msg) unless $rv;
- # On some situations TMPDIR is non-writable. sad but true.
- $parser->output_to_core(1);
- $parser->tmp_to_core(1);
+ my $mime_type = $self->MIMEObj->mime_type;
+ if (defined $mime_type and $mime_type eq 'text/html') {
+ $self->_DowngradeFromHTML(@_);
+ }
- #If someone includes a message, don't extract it
- $parser->extract_nested_messages(1);
+ return ($rv, $msg);
+}
- # Set up the prefix for files with auto-generated names:
- $parser->output_prefix("part");
+sub _Parse {
+ my $self = shift;
- # If content length is <= 50000 bytes, store each msg as in-core scalar;
- # Else, write to a disk file (the default action):
- $parser->output_to_core(50000);
+ # clear prev MIME object
+ $self->{'MIMEObj'} = undef;
+
+ #We're passing in whatever we were passed. it's destined for _ParseContent
+ my ($content, $msg) = $self->_ParseContent(@_);
+ return ( 0, $msg ) unless defined $content && length $content;
+
+ if ( $content =~ /^\S/s && $content !~ /^\S+:/ ) {
+ $RT::Logger->error(
+ "Template #". $self->id ." has leading line that doesn't"
+ ." look like header field, if you don't want to override"
+ ." any headers and don't want to see this error message"
+ ." then leave first line of the template empty"
+ );
+ $content = "\n".$content;
+ }
+
+ my $parser = MIME::Parser->new();
+ $parser->output_to_core(1);
+ $parser->tmp_to_core(1);
+ $parser->use_inner_files(1);
### Should we forgive normally-fatal errors?
$parser->ignore_errors(1);
- $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
+ # MIME::Parser doesn't play well with perl strings
+ utf8::encode($content);
+ $self->{'MIMEObj'} = eval { $parser->parse_data( \$content ) };
if ( my $error = $@ || $parser->last_error ) {
$RT::Logger->error( "$error" );
return ( 0, $error );
@@ -347,10 +364,6 @@ sub Parse {
}
-# }}}
-
-# {{{ sub _ParseContent
-
# Perform Template substitutions on the template
sub _ParseContent {
@@ -362,23 +375,15 @@ sub _ParseContent {
@_
);
- no warnings 'redefine';
- local $T::Ticket = $args{'TicketObj'};
- local $T::Transaction = $args{'TransactionObj'};
- local $T::Argument = $args{'Argument'};
- local $T::Requestor = eval { $T::Ticket->Requestors->UserMembersObj->First->Name } if $T::Ticket;
- local $T::rtname = $RT::rtname;
-
- local *T::loc = sub {
- $T::Ticket ? $T::Ticket->loc(@_)
- : $self->CurrentUser->loc(@_)
- };
+ unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
+ return (undef, $self->loc("Permission Denied"));
+ }
- my $content = $self->Content;
- unless ( defined $content ) {
- return ( undef, $self->loc("Permissions denied") );
+ if ( $self->IsEmpty ) {
+ return ( undef, $self->loc("Template is empty") );
}
+ my $content = $self->SUPER::_Value('Content');
# We need to untaint the content of the template, since we'll be working
# with it
$content =~ s/^(.*)$/$1/;
@@ -387,24 +392,72 @@ sub _ParseContent {
SOURCE => $content
);
+ $args{'Ticket'} = delete $args{'TicketObj'} if $args{'TicketObj'};
+ $args{'Transaction'} = delete $args{'TransactionObj'} if $args{'TransactionObj'};
+ $args{'Requestor'} = eval { $args{'Ticket'}->Requestors->UserMembersObj->First->Name }
+ if $args{'Ticket'};
+ $args{'rtname'} = RT->Config->Get('rtname');
+ if ( $args{'Ticket'} ) {
+ my $t = $args{'Ticket'}; # avoid memory leak
+ $args{'loc'} = sub { $t->loc(@_) };
+ } else {
+ $args{'loc'} = sub { $self->loc(@_) };
+ }
+
+ foreach my $key ( keys %args ) {
+ next unless ref $args{ $key };
+ next if ref $args{ $key } =~ /^(ARRAY|HASH|SCALAR|CODE)$/;
+ my $val = $args{ $key };
+ $args{ $key } = \$val;
+ }
+
+
my $is_broken = 0;
- my $retval = $template->fill_in( PACKAGE => 'T', BROKEN => sub {
- my (%args) = @_;
- $RT::Logger->error("Template parsing error: $args{error}")
- unless $args{error} =~ /^Died at /; # ignore intentional die()
- $is_broken++;
- return undef;
- } );
+ my $retval = $template->fill_in(
+ HASH => \%args,
+ BROKEN => sub {
+ my (%args) = @_;
+ $RT::Logger->error("Template parsing error: $args{error}")
+ unless $args{error} =~ /^Died at /; # ignore intentional die()
+ $is_broken++;
+ return undef;
+ },
+ );
return ( undef, $self->loc('Template parsing error') ) if $is_broken;
- # MIME::Parser has problems dealing with high-bit utf8 data.
- Encode::_utf8_off($retval);
return ($retval);
}
-# }}}
+sub _DowngradeFromHTML {
+ my $self = shift;
+ my $orig_entity = $self->MIMEObj;
+
+ my $new_entity = $orig_entity->dup; # this will fail badly if we go away from InCore parsing
+ $new_entity->head->mime_attr( "Content-Type" => 'text/plain' );
+ $new_entity->head->mime_attr( "Content-Type.charset" => 'utf-8' );
-# {{{ sub CurrentUserHasQueueRight
+ $orig_entity->head->mime_attr( "Content-Type" => 'text/html' );
+ $orig_entity->head->mime_attr( "Content-Type.charset" => 'utf-8' );
+ $orig_entity->make_multipart('alternative', Force => 1);
+
+ require HTML::FormatText;
+ require HTML::TreeBuilder;
+ my $tree = HTML::TreeBuilder->new_from_content(
+ $new_entity->bodyhandle->as_string
+ );
+ $new_entity->bodyhandle(MIME::Body::InCore->new(
+ \(scalar HTML::FormatText->new(
+ leftmargin => 0,
+ rightmargin => 78,
+ )->format( $tree ))
+ ));
+ $tree->delete;
+
+ $orig_entity->add_part($new_entity, 0); # plain comes before html
+ $self->{MIMEObj} = $orig_entity;
+
+ return;
+}
=head2 CurrentUserHasQueueRight
@@ -417,6 +470,4 @@ sub CurrentUserHasQueueRight {
return ( $self->QueueObj->CurrentUserHasRight(@_) );
}
-# }}}
-
1;