X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=rt%2Flib%2FRT%2FTemplate_Overlay.pm;h=e6f6374ee7ab110df9b70295cf004dbcafd1d4e5;hb=6587f6ba7d047ddc1686c080090afe7d53365bd4;hp=5cf23ecc3ef5f43860dda4ddc1ba6cc10049ae63;hpb=ef20b2b6b1feb47ad02b5ff7525f1a0fd11d0fa4;p=freeside.git diff --git a/rt/lib/RT/Template_Overlay.pm b/rt/lib/RT/Template_Overlay.pm index 5cf23ecc3..e6f6374ee 100644 --- a/rt/lib/RT/Template_Overlay.pm +++ b/rt/lib/RT/Template_Overlay.pm @@ -1,40 +1,40 @@ # BEGIN BPS TAGGED BLOCK {{{ -# +# # COPYRIGHT: -# -# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC -# -# +# +# This software is Copyright (c) 1996-2011 Best Practical Solutions, LLC +# +# # (Except where explicitly superseded by other copyright notices) -# -# +# +# # LICENSE: -# +# # This work is made available to you under the terms of Version 2 of # the GNU General Public License. A copy of that license should have # been provided with this software, but in any event can be snarfed # from www.gnu.org. -# +# # This work 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, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA # 02110-1301 or visit their web page on the internet at -# http://www.gnu.org/copyleft/gpl.html. -# -# +# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html. +# +# # CONTRIBUTION SUBMISSION POLICY: -# +# # (The following paragraph is not intended to limit the rights granted # to you to modify and distribute this software under the terms of # the GNU General Public License and is only of importance to you if # you choose to contribute your changes and enhancements to the # community by submitting them to Best Practical Solutions, LLC.) -# +# # By intentionally submitting any modifications, corrections or # derivatives to this work, or any other work intended for use with # Request Tracker, to Best Practical Solutions, LLC, you confirm that @@ -43,8 +43,9 @@ # royalty-free, perpetual, license to use, copy, create derivative # works based on those contributions, and sublicense and distribute # those contributions and any derivatives thereof. -# +# # END BPS TAGGED BLOCK }}} + # Portions Copyright 2000 Tobias Brox =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 -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. Several queues may have template with the same name +and as well global template with the same name may exist. +Use L and/or L 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,58 +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 object parsed using L method. Returns +undef if last call to L failed or never be called. + +Note that content of the template is UTF-8, but L 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 parsing on the template and then +imports the results into a L so we can really use it. Use +L method to get the L object. - Takes a hash containing Argument, TicketObj, and TransactionObj. +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); +} + +sub _Parse { + my $self = shift; - # Set up the prefix for files with auto-generated names: - $parser->output_prefix("part"); + # clear prev MIME object + $self->{'MIMEObj'} = undef; - # 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); + #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 ); @@ -346,10 +364,6 @@ sub Parse { } -# }}} - -# {{{ sub _ParseContent - # Perform Template substitutions on the template sub _ParseContent { @@ -361,19 +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 }; - local $T::rtname = $RT::rtname; - local *T::loc = sub { $T::Ticket->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/; @@ -382,24 +392,74 @@ 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 CurrentUserHasQueueRight +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' ); + + $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; + require Encode; + # need to decode_utf8, see the doc of MIMEObj method + my $tree = HTML::TreeBuilder->new_from_content( + Encode::decode_utf8($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 @@ -412,6 +472,4 @@ sub CurrentUserHasQueueRight { return ( $self->QueueObj->CurrentUserHasRight(@_) ); } -# }}} - 1;