X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=rt%2Flib%2FRT%2FAttachment_Overlay.pm;h=d1f0ed40c6c304d305619f3e9ff1473eeff5476e;hp=9086c52f6b367194eec316e8dd45e4794fd4c1d7;hb=9c68254528b6f2c7d8c1921b452fa56064783782;hpb=289340780927b5bac2c7604d7317c3063c6dd8cc diff --git a/rt/lib/RT/Attachment_Overlay.pm b/rt/lib/RT/Attachment_Overlay.pm index 9086c52f6..d1f0ed40c 100644 --- a/rt/lib/RT/Attachment_Overlay.pm +++ b/rt/lib/RT/Attachment_Overlay.pm @@ -1,8 +1,14 @@ -# BEGIN LICENSE BLOCK +# BEGIN BPS TAGGED BLOCK {{{ # -# Copyright (c) 1996-2003 Jesse Vincent +# COPYRIGHT: +# +# This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC +# # -# (Except where explictly superceded by other copyright notices) +# (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 @@ -14,13 +20,30 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # General Public License for more details. # -# Unless otherwise specified, all modifications, corrections or -# extensions to this work which alter its source code become the -# property of Best Practical Solutions, LLC when submitted for -# inclusion in the work. +# 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. +# # +# CONTRIBUTION SUBMISSION POLICY: # -# END LICENSE BLOCK +# (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 +# you are the copyright holder for those contributions and you grant +# Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable, +# 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 }}} + =head1 SYNOPSIS use RT::Attachment; @@ -44,24 +67,28 @@ ok (require RT::Attachment); =cut + +package RT::Attachment; + use strict; no warnings qw(redefine); use MIME::Base64; use MIME::QuotedPrint; -# {{{ sub _ClassAccessible -sub _ClassAccessible { + +# {{{ sub _OverlayAccessible +sub _OverlayAccessible { { - TransactionId => { 'read'=>1, 'public'=>1, }, - MessageId => { 'read'=>1, }, - Parent => { 'read'=>1, }, - ContentType => { 'read'=>1, }, - Subject => { 'read'=>1, }, - Content => { 'read'=>1, }, - ContentEncoding => { 'read'=>1, }, - Headers => { 'read'=>1, }, - Filename => { 'read'=>1, }, + TransactionId => { 'read'=>1, 'public'=>1, 'write' => 0 }, + MessageId => { 'read'=>1, 'write' => 0 }, + Parent => { 'read'=>1, 'write' => 0 }, + ContentType => { 'read'=>1, 'write' => 0 }, + Subject => { 'read'=>1, 'write' => 0 }, + Content => { 'read'=>1, 'write' => 0 }, + ContentEncoding => { 'read'=>1, 'write' => 0 }, + Headers => { 'read'=>1, 'write' => 0 }, + Filename => { 'read'=>1, 'write' => 0 }, Creator => { 'read'=>1, 'auto'=>1, }, Created => { 'read'=>1, 'auto'=>1, }, }; @@ -83,6 +110,9 @@ sub TransactionObj { $self->{_TransactionObj}=RT::Transaction->new($self->CurrentUser); $self->{_TransactionObj}->Load($self->TransactionId); } + unless ($self->{_TransactionObj}->Id) { + $RT::Logger->crit("Attachment ".$self->id." can't find transaction ".$self->TransactionId." which it is ostensibly part of. That's bad"); + } return $self->{_TransactionObj}; } @@ -127,6 +157,13 @@ sub Create { defined($Subject) or $Subject = ''; chomp($Subject); + #Get the Message-ID + my $MessageId = $Attachment->head->get( 'Message-ID', 0 ); + defined($MessageId) or $MessageId = ''; + chomp ($MessageId); + $MessageId =~ s/^<(.*)>$/$1/go; + + #Get the filename my $Filename = $Attachment->head->recommended_filename || eval { ${ $Attachment->head->{mail_hdr_hash}{'Content-Disposition'}[0] } @@ -136,12 +173,19 @@ sub Create { # If a message has no bodyhandle, that means that it has subparts (or appears to) # and we should act accordingly. unless ( defined $Attachment->bodyhandle ) { + $id = $self->SUPER::Create( TransactionId => $args{'TransactionId'}, Parent => 0, ContentType => $Attachment->mime_type, Headers => $Attachment->head->as_string, + MessageId => $MessageId, Subject => $Subject); + + unless ($id) { + $RT::Logger->crit("Attachment insert failed - ".$RT::Handle->dbh->errstr); + + } foreach my $part ( $Attachment->parts ) { my $SubAttachment = new RT::Attachment( $self->CurrentUser ); @@ -150,7 +194,6 @@ sub Create { Parent => $id, Attachment => $part, ContentType => $Attachment->mime_type, - Headers => $Attachment->head->as_string(), ); } @@ -160,73 +203,21 @@ sub Create { #If it's not multipart else { - my $ContentEncoding = 'none'; - - my $Body = $Attachment->bodyhandle->as_string; - - #get the max attachment length from RT - my $MaxSize = $RT::MaxAttachmentSize; - - #if the current attachment contains nulls and the - #database doesn't support embedded nulls - - if ( $RT::AlwaysUseBase64 or - ( !$RT::Handle->BinarySafeBLOBs ) && ( $Body =~ /\x00/ ) ) { - - # set a flag telling us to mimencode the attachment - $ContentEncoding = 'base64'; - - #cut the max attchment size by 25% (for mime-encoding overhead. - $RT::Logger->debug("Max size is $MaxSize\n"); - $MaxSize = $MaxSize * 3 / 4; - # Some databases (postgres) can't handle non-utf8 data - } elsif ( !$RT::Handle->BinarySafeBLOBs - && $Attachment->mime_type !~ /text\/plain/gi - && !Encode::is_utf8( $Body, 1 ) ) { - $ContentEncoding = 'quoted-printable'; - } - - #if the attachment is larger than the maximum size - if ( ($MaxSize) and ( $MaxSize < length($Body) ) ) { - - # if we're supposed to truncate large attachments - if ($RT::TruncateLongAttachments) { - - # truncate the attachment to that length. - $Body = substr( $Body, 0, $MaxSize ); - - } - - # elsif we're supposed to drop large attachments on the floor, - elsif ($RT::DropLongAttachments) { - - # drop the attachment on the floor - $RT::Logger->info( "$self: Dropped an attachment of size " . length($Body) . "\n" . "It started: " . substr( $Body, 0, 60 ) . "\n" ); - return (undef); - } - } - - # if we need to mimencode the attachment - if ( $ContentEncoding eq 'base64' ) { - - # base64 encode the attachment - Encode::_utf8_off($Body); - $Body = MIME::Base64::encode_base64($Body); - - } elsif ($ContentEncoding eq 'quoted-printable') { - Encode::_utf8_off($Body); - $Body = MIME::QuotedPrint::encode($Body); - } - - + my ($ContentEncoding, $Body) = $self->_EncodeLOB($Attachment->bodyhandle->as_string, $Attachment->mime_type); my $id = $self->SUPER::Create( TransactionId => $args{'TransactionId'}, ContentType => $Attachment->mime_type, ContentEncoding => $ContentEncoding, Parent => $args{'Parent'}, - Headers => $Attachment->head->as_string, + Headers => $Attachment->head->as_string, Subject => $Subject, Content => $Body, - Filename => $Filename, ); + Filename => $Filename, + MessageId => $MessageId + ); + unless ($id) { + $RT::Logger->crit("Attachment insert failed - ".$RT::Handle->dbh->errstr); + } + return ($id); } } @@ -246,7 +237,11 @@ sub Import { my %args = ( ContentEncoding => 'none', @_ ); - return($self->SUPER::Create(@_)); + + + ($args{'ContentEncoding'}, $args{'Content'}) = $self->_EncodeLOB($args{'Content'}, $args{'MimeType'}); + + return($self->SUPER::Create(%args)); } # {{{ sub Content @@ -260,26 +255,7 @@ before returning it. sub Content { my $self = shift; - my $decode_utf8 = (($self->ContentType eq 'text/plain') ? 1 : 0); - - if ( $self->ContentEncoding eq 'none' || ! $self->ContentEncoding ) { - return $self->_Value( - 'Content', - decode_utf8 => $decode_utf8, - ); - } elsif ( $self->ContentEncoding eq 'base64' ) { - return ( $decode_utf8 - ? Encode::decode_utf8(MIME::Base64::decode_base64($self->_Value('Content'))) - : MIME::Base64::decode_base64($self->_Value('Content')) - ); - } elsif ( $self->ContentEncoding eq 'quoted-printable' ) { - return ( $decode_utf8 - ? Encode::decode_utf8(MIME::QuotedPrint::decode($self->_Value('Content'))) - : MIME::QuotedPrint::decode($self->_Value('Content')) - ); - } else { - return( $self->loc("Unknown ContentEncoding [_1]", $self->ContentEncoding)); - } + $self->_DecodeLOB($self->ContentType, $self->ContentEncoding, $self->_Value('Content', decode_utf8 => 0)); } @@ -291,7 +267,7 @@ sub Content { =head2 OriginalContent Returns the attachment's content as octets before RT's mangling. -Currently, this just means restoring text/plain content back to its +Currently, this just means restoring text content back to its original encoding. =cut @@ -299,7 +275,8 @@ original encoding. sub OriginalContent { my $self = shift; - return $self->Content unless $self->ContentType eq 'text/plain'; + return $self->Content unless ( + $self->ContentType =~ qr{^(text/plain|message/rfc822)$}i) ; my $enc = $self->OriginalEncoding; my $content; @@ -319,7 +296,7 @@ sub OriginalContent { eval {return( Encode::decode_utf8($content))} || return ($content); } - eval { Encode::from_to($content, 'utf8' => $enc);}; + eval { Encode::from_to($content, 'utf8' => $enc) } if $enc; if ($@) { $RT::Logger->error("Could not convert attachment from assumed utf8 to '$enc' :".$@); } @@ -424,19 +401,16 @@ sub Quote { =head2 NiceHeaders -Returns the To, From, Cc, Date and Subject headers. - -It is a known issue that this breaks if any of these headers are not -properly unfolded. +Returns a multi-line string of the To, From, Cc, Date and Subject headers. =cut sub NiceHeaders { my $self = shift; my $hdrs = ""; - my @hdrs = split(/\n/,$self->Headers); + my @hdrs = $self->_SplitHeaders; while (my $str = shift @hdrs) { - next unless $str =~ /^(To|From|RT-Send-Cc|Cc|Date|Subject): /i; + next unless $str =~ /^(To|From|RT-Send-Cc|Cc|Bcc|Date|Subject):/i; $hdrs .= $str . "\n"; $hdrs .= shift( @hdrs ) . "\n" while ($hdrs[0] =~ /^[ \t]+/); } @@ -451,7 +425,7 @@ sub NiceHeaders { Returns this object's headers as a string. This method specifically removes the RT-Send-Bcc: header, so as to never reveal to whom RT sent a Bcc. We need to record the RT-Send-Cc and RT-Send-Bcc values so that we can actually send -out mail. (The mailing rules are seperated from the ticket update code by +out mail. (The mailing rules are separated from the ticket update code by an abstraction barrier that makes it impossible to pass this data directly =cut @@ -459,10 +433,9 @@ an abstraction barrier that makes it impossible to pass this data directly sub Headers { my $self = shift; my $hdrs=""; - for (split(/\n/,$self->SUPER::Headers)) { - $hdrs.="$_\n" unless /^(RT-Send-Bcc): /i - } - return $hdrs; + my @headers = grep { !/^RT-Send-Bcc/i } $self->_SplitHeaders; + return join("\n",@headers); + } @@ -480,8 +453,8 @@ done in Headers() above. sub GetHeader { my $self = shift; my $tag = shift; - foreach my $line (split(/\n/,$self->SUPER::Headers)) { - if ($line =~ /^\Q$tag\E:\s+(.*)$/i) { #if we find the header, return its value + foreach my $line ($self->_SplitHeaders) { + if ($line =~ /^\Q$tag\E:\s+(.*)$/si) { #if we find the header, return its value return ($1); } } @@ -504,7 +477,7 @@ sub SetHeader { my $tag = shift; my $newheader = ''; - foreach my $line (split(/\n/,$self->SUPER::Headers)) { + foreach my $line ($self->_SplitHeaders) { if (defined $tag and $line =~ /^\Q$tag\E:\s+(.*)$/i) { $newheader .= "$tag: $_[0]\n"; undef $tag; @@ -528,35 +501,84 @@ Returns its value as a string, if the user passes an ACL check =cut -sub _Value { +sub _Value { - my $self = shift; + my $self = shift; my $field = shift; - - + #if the field is public, return it. - if ($self->_Accessible($field, 'public')) { - #$RT::Logger->debug("Skipping ACL check for $field\n"); - return($self->__Value($field, @_)); - + if ( $self->_Accessible( $field, 'public' ) ) { + return ( $self->__Value( $field, @_ ) ); } - + #If it's a comment, we need to be extra special careful - elsif ( (($self->TransactionObj->CurrentUserHasRight('ShowTicketComments')) and - ($self->TransactionObj->Type eq 'Comment') ) or - ($self->TransactionObj->CurrentUserHasRight('ShowTicket'))) { - return($self->__Value($field, @_)); + elsif ( $self->TransactionObj->Type =~ /^Comment/ ) { + if ( $self->TransactionObj->CurrentUserHasRight('ShowTicketComments') ) + { + return ( $self->__Value( $field, @_ ) ); + } + } + elsif ( $self->TransactionObj->CurrentUserHasRight('ShowTicket') ) { + return ( $self->__Value( $field, @_ ) ); } + #if they ain't got rights to see, don't let em else { - return(undef); - } - - + return (undef); + } + } # }}} +=head2 _SplitHeaders + +Returns an array of this attachment object's headers, with one header +per array entry. multiple lines are folded. + +=begin testing + +my $test1 = "From: jesse"; +my @headers = RT::Attachment->_SplitHeaders($test1); +is ($#headers, 0, $test1 ); + +my $test2 = qq{From: jesse +To: bobby +Subject: foo +}; + +@headers = RT::Attachment->_SplitHeaders($test2); +is ($#headers, 2, "testing a bunch of singline multiple headers" ); + + +my $test3 = qq{From: jesse +To: bobby, + Suzie, + Sally, + Joey: bizzy, +Subject: foo +}; + +@headers = RT::Attachment->_SplitHeaders($test3); +is ($#headers, 2, "testing a bunch of singline multiple headers" ); + + +=end testing + +=cut + +sub _SplitHeaders { + my $self = shift; + my $headers = (shift || $self->SUPER::Headers()); + my @headers; + for (split(/\n(?=\w|\z)/,$headers)) { + push @headers, $_; + + } + return(@headers); +} + + sub ContentLength { my $self = shift;