X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=rt%2Flib%2FRT%2FTemplate_Overlay.pm;h=e6f6374ee7ab110df9b70295cf004dbcafd1d4e5;hb=96a0d937cdfe26d691e59139088e33e0bc3e1184;hp=bfff0f03e237076c7c6468ae33ecbfcf0a4e8369;hpb=673b9a458d9138523026963df6fa3b4683e09bae;p=freeside.git diff --git a/rt/lib/RT/Template_Overlay.pm b/rt/lib/RT/Template_Overlay.pm index bfff0f03e..e6f6374ee 100644 --- a/rt/lib/RT/Template_Overlay.pm +++ b/rt/lib/RT/Template_Overlay.pm @@ -1,38 +1,40 @@ # BEGIN BPS TAGGED BLOCK {{{ -# +# # COPYRIGHT: -# -# This software is Copyright (c) 1996-2005 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., 675 Mass Ave, Cambridge, MA 02139, USA. -# -# +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +# 02110-1301 or visit their web page on the internet at +# 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 @@ -41,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 @@ -58,11 +61,6 @@ =head1 METHODS -=begin testing - -ok(require RT::Template); - -=end testing =cut @@ -70,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; @@ -97,130 +92,85 @@ sub _Accessible { return $self->SUPER::_Accessible( @_, %Cols ); } -# }}} - -# {{{ sub _Set - sub _Set { my $self = shift; - - # use super::value or we get acl blocked - if ( ( defined $self->SUPER::_Value('Queue') ) - && ( $self->SUPER::_Value('Queue') == 0 ) ) - { - unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ModifyTemplate') ) { - return ( 0, $self->loc('Permission Denied') ); - } - } - else { - - unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) { - return ( 0, $self->loc('Permission Denied') ); - } + + unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) { + return ( 0, $self->loc('Permission Denied') ); } - return ( $self->SUPER::_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 sub _Value { - my $self = shift; - my $field = shift; - - - #If the current user doesn't have ACLs, don't let em at it. - #use super::value or we get acl blocked - if ( ( !defined $self->__Value('Queue') ) - || ( $self->__Value('Queue') == 0 ) ) - { - unless ( $self->CurrentUser->HasRight( Object => $RT::System, Right => 'ShowTemplate') ) { - return (undef); - } - } - else { - unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) { - return (undef); - } + + unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) { + return undef; } - return ( $self->__Value($field) ); + return $self->__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 sub Load { my $self = shift; my $identifier = shift; + return undef unless $identifier; - if ( !$identifier ) { - return (undef); - } - - if ( $identifier !~ /\D/ ) { - $self->SUPER::LoadById($identifier); - } - else { - $self->LoadByCol( 'Name', $identifier ); - + if ( $identifier =~ /\D/ ) { + return $self->LoadByCol( 'Name', $identifier ); } + return $self->LoadById( $identifier ); } -# }}} - -# {{{ sub LoadGlobalTemplate - =head2 LoadGlobalTemplate NAME -Load the global tempalte with the name NAME +Load the global template with the name NAME =cut 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 @@ -236,10 +186,6 @@ sub LoadQueueTemplate { } -# }}} - -# {{{ sub Create - =head2 Create Takes a paramhash of Content, Queue, Name and Description. @@ -251,7 +197,6 @@ template. Returns the Template's id # if the create was successful. Returns undef for unknown database failure. - =cut sub Create { @@ -260,42 +205,38 @@ sub Create { Content => undef, Queue => 0, Description => '[no description]', - Type => 'Action', #By default, template are 'Action' templates - Name => undef, + Type => 'Action', #By default, template are 'Action' templates + Name => undef, @_ ); - if ( !$args{'Queue'} ) { + unless ( $args{'Queue'} ) { unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) { - return (undef); + return ( undef, $self->loc('Permission Denied') ); } $args{'Queue'} = 0; } else { my $QueueObj = new RT::Queue( $self->CurrentUser ); - $QueueObj->Load( $args{'Queue'} ) || return ( 0, $self->loc('Invalid queue') ); + $QueueObj->Load( $args{'Queue'} ) || return ( undef, $self->loc('Invalid queue') ); unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) { - return (undef); + return ( undef, $self->loc('Permission Denied') ); } $args{'Queue'} = $QueueObj->Id; } my $result = $self->SUPER::Create( - Content => $args{'Content'}, - Queue => $args{'Queue'}, + Content => $args{'Content'}, + Queue => $args{'Queue'}, Description => $args{'Description'}, - Name => $args{'Name'} + Name => $args{'Name'}, ); return ($result); } -# }}} - -# {{{ sub Delete - =head2 Delete Delete this template. @@ -312,75 +253,117 @@ 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 + +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 Parse +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 = $self->_ParseContent(@_); - #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) }; - my $error = ( $@ || $parser->last_error ); - - if ($error) { - $RT::Logger->error("$error"); + # 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 ); } # Unfold all headers - $self->{'MIMEObj'}->head->unfold(); + $self->{'MIMEObj'}->head->unfold; return ( 1, $self->loc("Template parsed") ); } -# }}} - -# {{{ sub _ParseContent - # Perform Template substitutions on the template sub _ParseContent { @@ -392,41 +375,91 @@ sub _ParseContent { @_ ); - no warnings 'redefine'; - $T::Ticket = $args{'TicketObj'}; - $T::Transaction = $args{'TransactionObj'}; - $T::Argument = $args{'Argument'}; - $T::Requestor = eval { $T::Ticket->Requestors->UserMembersObj->First->Name }; - $T::rtname = $RT::rtname; - *T::loc = sub { $T::Ticket->loc(@_) }; + unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) { + return (undef, $self->loc("Permission 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 - my $content = $self->Content(); $content =~ s/^(.*)$/$1/; my $template = Text::Template->new( TYPE => 'STRING', 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; - } ); - return undef if $is_broken; - - # MIME::Parser has problems dealing with high-bit utf8 data. - Encode::_utf8_off($retval); + 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; + 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 @@ -439,6 +472,4 @@ sub CurrentUserHasQueueRight { return ( $self->QueueObj->CurrentUserHasRight(@_) ); } -# }}} - 1;