merging RT 4.0.6
[freeside.git] / rt / lib / RT / Attachment.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 =head1 SYNOPSIS
50
51     use RT::Attachment;
52
53 =head1 DESCRIPTION
54
55 This module should never be instantiated directly by client code. it's an internal 
56 module which should only be instantiated through exported APIs in Ticket, Queue and other 
57 similar objects.
58
59 =head1 METHODS
60
61
62
63 =cut
64
65
66 package RT::Attachment;
67 use base 'RT::Record';
68
69 sub Table {'Attachments'}
70
71
72
73
74 use strict;
75 use warnings;
76
77
78 use RT::Transaction;
79 use MIME::Base64;
80 use MIME::QuotedPrint;
81 use MIME::Body;
82 use RT::Util 'mime_recommended_filename';
83
84 sub _OverlayAccessible {
85   {
86     TransactionId   => { 'read'=>1, 'public'=>1, 'write' => 0 },
87     MessageId       => { 'read'=>1, 'write' => 0 },
88     Parent          => { 'read'=>1, 'write' => 0 },
89     ContentType     => { 'read'=>1, 'write' => 0 },
90     Subject         => { 'read'=>1, 'write' => 0 },
91     Content         => { 'read'=>1, 'write' => 0 },
92     ContentEncoding => { 'read'=>1, 'write' => 0 },
93     Headers         => { 'read'=>1, 'write' => 0 },
94     Filename        => { 'read'=>1, 'write' => 0 },
95     Creator         => { 'read'=>1, 'auto'=>1, },
96     Created         => { 'read'=>1, 'auto'=>1, },
97   };
98 }
99
100 =head2 Create
101
102 Create a new attachment. Takes a paramhash:
103     
104     'Attachment' Should be a single MIME body with optional subparts
105     'Parent' is an optional id of the parent attachment
106     'TransactionId' is the mandatory id of the transaction this attachment is associated with.;
107
108 =cut
109
110 sub Create {
111     my $self = shift;
112     my %args = ( id            => 0,
113                  TransactionId => 0,
114                  Parent        => 0,
115                  Attachment    => undef,
116                  @_ );
117
118     # For ease of reference
119     my $Attachment = $args{'Attachment'};
120
121     # if we didn't specify a ticket, we need to bail
122     unless ( $args{'TransactionId'} ) {
123         $RT::Logger->crit( "RT::Attachment->Create couldn't, as you didn't specify a transaction" );
124         return (0);
125     }
126
127     # If we possibly can, collapse it to a singlepart
128     $Attachment->make_singlepart;
129
130     # Get the subject
131     my $Subject = $Attachment->head->get( 'subject', 0 );
132     $Subject = '' unless defined $Subject;
133     chomp $Subject;
134     utf8::decode( $Subject ) unless utf8::is_utf8( $Subject );
135
136     #Get the Message-ID
137     my $MessageId = $Attachment->head->get( 'Message-ID', 0 );
138     defined($MessageId) or $MessageId = '';
139     chomp ($MessageId);
140     $MessageId =~ s/^<(.*?)>$/$1/o;
141
142     #Get the filename
143
144     my $Filename = mime_recommended_filename($Attachment);
145
146     # remove path part. 
147     $Filename =~ s!.*/!! if $Filename;
148
149     # MIME::Head doesn't support perl strings well and can return
150     # octets which later will be double encoded in low-level code
151     my $head = $Attachment->head->as_string;
152     utf8::decode( $head ) unless utf8::is_utf8( $head );
153
154     # If a message has no bodyhandle, that means that it has subparts (or appears to)
155     # and we should act accordingly.  
156     unless ( defined $Attachment->bodyhandle ) {
157         my ($id) = $self->SUPER::Create(
158             TransactionId => $args{'TransactionId'},
159             Parent        => $args{'Parent'},
160             ContentType   => $Attachment->mime_type,
161             Headers       => $head,
162             MessageId     => $MessageId,
163             Subject       => $Subject,
164         );
165
166         unless ($id) {
167             $RT::Logger->crit("Attachment insert failed - ". $RT::Handle->dbh->errstr);
168         }
169
170         foreach my $part ( $Attachment->parts ) {
171             my $SubAttachment = RT::Attachment->new( $self->CurrentUser );
172             my ($id) = $SubAttachment->Create(
173                 TransactionId => $args{'TransactionId'},
174                 Parent        => $id,
175                 Attachment    => $part,
176             );
177             unless ($id) {
178                 $RT::Logger->crit("Attachment insert failed: ". $RT::Handle->dbh->errstr);
179             }
180         }
181         return ($id);
182     }
183
184     #If it's not multipart
185     else {
186
187         my ($ContentEncoding, $Body, $ContentType, $Filename) = $self->_EncodeLOB(
188             $Attachment->bodyhandle->as_string,
189             $Attachment->mime_type,
190             $Filename
191         );
192
193         my $id = $self->SUPER::Create(
194             TransactionId   => $args{'TransactionId'},
195             ContentType     => $ContentType,
196             ContentEncoding => $ContentEncoding,
197             Parent          => $args{'Parent'},
198             Headers         => $head,
199             Subject         => $Subject,
200             Content         => $Body,
201             Filename        => $Filename,
202             MessageId       => $MessageId,
203         );
204
205         unless ($id) {
206             $RT::Logger->crit("Attachment insert failed: ". $RT::Handle->dbh->errstr);
207         }
208         return $id;
209     }
210 }
211
212 =head2 Import
213
214 Create an attachment exactly as specified in the named parameters.
215
216 =cut
217
218 sub Import {
219     my $self = shift;
220     my %args = ( ContentEncoding => 'none', @_ );
221
222     ( $args{'ContentEncoding'}, $args{'Content'} ) =
223         $self->_EncodeLOB( $args{'Content'}, $args{'MimeType'} );
224
225     return ( $self->SUPER::Create(%args) );
226 }
227
228 =head2 TransactionObj
229
230 Returns the transaction object asscoiated with this attachment.
231
232 =cut
233
234 sub TransactionObj {
235     my $self = shift;
236
237     unless ( $self->{_TransactionObj} ) {
238         $self->{_TransactionObj} = RT::Transaction->new( $self->CurrentUser );
239         $self->{_TransactionObj}->Load( $self->TransactionId );
240     }
241
242     unless ($self->{_TransactionObj}->Id) {
243         $RT::Logger->crit(  "Attachment ". $self->id
244                            ." can't find transaction ". $self->TransactionId
245                            ." which it is ostensibly part of. That's bad");
246     }
247     return $self->{_TransactionObj};
248 }
249
250 =head2 ParentObj
251
252 Returns a parent's L<RT::Attachment> object if this attachment
253 has a parent, otherwise returns undef.
254
255 =cut
256
257 sub ParentObj {
258     my $self = shift;
259     return undef unless $self->Parent;
260
261     my $parent = RT::Attachment->new( $self->CurrentUser );
262     $parent->LoadById( $self->Parent );
263     return $parent;
264 }
265
266 =head2 Children
267
268 Returns an L<RT::Attachments> object which is preloaded with
269 all attachments objects with this attachment\'s Id as their
270 C<Parent>.
271
272 =cut
273
274 sub Children {
275     my $self = shift;
276     
277     my $kids = RT::Attachments->new( $self->CurrentUser );
278     $kids->ChildrenOf( $self->Id );
279     return($kids);
280 }
281
282 =head2 Content
283
284 Returns the attachment's content. if it's base64 encoded, decode it 
285 before returning it.
286
287 =cut
288
289 sub Content {
290     my $self = shift;
291     return $self->_DecodeLOB(
292         $self->ContentType,
293         $self->ContentEncoding,
294         $self->_Value('Content', decode_utf8 => 0),
295     );
296 }
297
298 =head2 OriginalContent
299
300 Returns the attachment's content as octets before RT's mangling.
301 Generally this just means restoring text content back to its
302 original encoding.
303
304 If the attachment has a C<message/*> Content-Type, its children attachments
305 are reconstructed and returned as a string.
306
307 =cut
308
309 sub OriginalContent {
310     my $self = shift;
311
312     # message/* content types represent raw messages.  Since we break them
313     # apart when they come in, we'll reconstruct their child attachments when
314     # you ask for the OriginalContent of the message/ part.
315     if ($self->IsMessageContentType) {
316         # There shouldn't be more than one "subpart" to a message/* attachment
317         my $child = $self->Children->First;
318         return $self->Content unless $child and $child->id;
319         return $child->ContentAsMIME(Children => 1)->as_string;
320     }
321
322     return $self->Content unless RT::I18N::IsTextualContentType($self->ContentType);
323     my $enc = $self->OriginalEncoding;
324
325     my $content;
326     if ( !$self->ContentEncoding || $self->ContentEncoding eq 'none' ) {
327         $content = $self->_Value('Content', decode_utf8 => 0);
328     } elsif ( $self->ContentEncoding eq 'base64' ) {
329         $content = MIME::Base64::decode_base64($self->_Value('Content', decode_utf8 => 0));
330     } elsif ( $self->ContentEncoding eq 'quoted-printable' ) {
331         $content = MIME::QuotedPrint::decode($self->_Value('Content', decode_utf8 => 0));
332     } else {
333         return( $self->loc("Unknown ContentEncoding [_1]", $self->ContentEncoding));
334     }
335
336     # Turn *off* the SvUTF8 bits here so decode_utf8 and from_to below can work.
337     local $@;
338     Encode::_utf8_off($content);
339
340     if (!$enc || $enc eq '' ||  $enc eq 'utf8' || $enc eq 'utf-8') {
341         # If we somehow fail to do the decode, at least push out the raw bits
342         eval { return( Encode::decode_utf8($content)) } || return ($content);
343     }
344
345     eval { Encode::from_to($content, 'utf8' => $enc) } if $enc;
346     if ($@) {
347         $RT::Logger->error("Could not convert attachment from assumed utf8 to '$enc' :".$@);
348     }
349     return $content;
350 }
351
352 =head2 OriginalEncoding
353
354 Returns the attachment's original encoding.
355
356 =cut
357
358 sub OriginalEncoding {
359     my $self = shift;
360     return $self->GetHeader('X-RT-Original-Encoding');
361 }
362
363 =head2 ContentLength
364
365 Returns length of L</Content> in bytes.
366
367 =cut
368
369 sub ContentLength {
370     my $self = shift;
371
372     return undef unless $self->TransactionObj->CurrentUserCanSee;
373
374     my $len = $self->GetHeader('Content-Length');
375     unless ( defined $len ) {
376         use bytes;
377         no warnings 'uninitialized';
378         $len = length($self->Content) || 0;
379         $self->SetHeader('Content-Length' => $len);
380     }
381     return $len;
382 }
383
384 =head2 Quote
385
386 =cut
387
388 sub Quote {
389     my $self=shift;
390     my %args=(Reply=>undef, # Prefilled reply (i.e. from the KB/FAQ system)
391               @_);
392
393     my ($quoted_content, $body, $headers);
394     my $max=0;
395
396     # TODO: Handle Multipart/Mixed (eventually fix the link in the
397     # ShowHistory web template?)
398     if (RT::I18N::IsTextualContentType($self->ContentType)) {
399         $body=$self->Content;
400
401         # Do we need any preformatting (wrapping, that is) of the message?
402
403         # Remove quoted signature.
404         $body =~ s/\n-- \n(.*)$//s;
405
406         # What's the longest line like?
407         foreach (split (/\n/,$body)) {
408             $max=length if ( length > $max);
409         }
410
411         if ($max>76) {
412             require Text::Wrapper;
413             my $wrapper = Text::Wrapper->new
414                 (
415                  columns => 70, 
416                  body_start => ($max > 70*3 ? '   ' : ''),
417                  par_start => ''
418                  );
419             $body=$wrapper->wrap($body);
420         }
421
422         $body =~ s/^/> /gm;
423
424         $body = '[' . $self->TransactionObj->CreatorObj->Name() . ' - ' . $self->TransactionObj->CreatedAsString()
425                     . "]:\n\n"
426                 . $body . "\n\n";
427
428     } else {
429         $body = "[Non-text message not quoted]\n\n";
430     }
431     
432     $max=60 if $max<60;
433     $max=70 if $max>78;
434     $max+=2;
435
436     return (\$body, $max);
437 }
438
439 =head2 ContentAsMIME [Children => 1]
440
441 Returns MIME entity built from this attachment.
442
443 If the optional parameter C<Children> is set to a true value, the children are
444 recursively added to the entity.
445
446 =cut
447
448 sub ContentAsMIME {
449     my $self = shift;
450     my %opts = (
451         Children => 0,
452         @_
453     );
454
455     my $entity = MIME::Entity->new();
456     foreach my $header ($self->SplitHeaders) {
457         my ($h_key, $h_val) = split /:/, $header, 2;
458         $entity->head->add( $h_key, RT::Interface::Email::EncodeToMIME( String => $h_val ) );
459     }
460     
461     # since we want to return original content, let's use original encoding
462     $entity->head->mime_attr(
463         "Content-Type.charset" => $self->OriginalEncoding )
464       if $self->OriginalEncoding;
465
466     $entity->bodyhandle(
467         MIME::Body::Scalar->new( $self->OriginalContent )
468     );
469
470     if ($opts{'Children'} and not $self->IsMessageContentType) {
471         my $children = $self->Children;
472         while (my $child = $children->Next) {
473             $entity->make_multipart unless $entity->is_multipart;
474             $entity->add_part( $child->ContentAsMIME(%opts) );
475         }
476     }
477
478     return $entity;
479 }
480
481 =head2 IsMessageContentType
482
483 Returns a boolean indicating if the Content-Type of this attachment is a
484 C<message/> subtype.
485
486 =cut
487
488 sub IsMessageContentType {
489     my $self = shift;
490     return $self->ContentType =~ m{^\s*message/}i ? 1 : 0;
491 }
492
493 =head2 Addresses
494
495 Returns a hashref of all addresses related to this attachment.
496 The keys of the hash are C<From>, C<To>, C<Cc>, C<Bcc>, C<RT-Send-Cc>
497 and C<RT-Send-Bcc>. The values are references to lists of
498 L<Email::Address> objects.
499
500 =cut
501
502 sub Addresses {
503     my $self = shift;
504
505     my %data = ();
506     my $current_user_address = lc $self->CurrentUser->EmailAddress;
507     foreach my $hdr (qw(From To Cc Bcc RT-Send-Cc RT-Send-Bcc)) {
508         my @Addresses;
509         my $line = $self->GetHeader($hdr);
510         
511         foreach my $AddrObj ( Email::Address->parse( $line )) {
512             my $address = $AddrObj->address;
513             $address = lc RT::User->CanonicalizeEmailAddress($address);
514             next if $current_user_address eq $address;
515             next if RT::EmailParser->IsRTAddress($address);
516             push @Addresses, $AddrObj ;
517         }
518         $data{$hdr} = \@Addresses;
519     }
520     return \%data;
521 }
522
523 =head2 NiceHeaders
524
525 Returns a multi-line string of the To, From, Cc, Date and Subject headers.
526
527 =cut
528
529 sub NiceHeaders {
530     my $self = shift;
531     my $hdrs = "";
532     my @hdrs = $self->_SplitHeaders;
533     while (my $str = shift @hdrs) {
534             next unless $str =~ /^(To|From|RT-Send-Cc|Cc|Bcc|Date|Subject):/i;
535             $hdrs .= $str . "\n";
536             $hdrs .= shift( @hdrs ) . "\n" while ($hdrs[0] =~ /^[ \t]+/);
537     }
538     return $hdrs;
539 }
540
541 =head2 Headers
542
543 Returns this object's headers as a string.  This method specifically
544 removes the RT-Send-Bcc: header, so as to never reveal to whom RT sent a Bcc.
545 We need to record the RT-Send-Cc and RT-Send-Bcc values so that we can actually send
546 out mail. The mailing rules are separated from the ticket update code by
547 an abstraction barrier that makes it impossible to pass this data directly.
548
549 =cut
550
551 sub Headers {
552     return join("\n", $_[0]->SplitHeaders);
553 }
554
555 =head2 EncodedHeaders
556
557 Takes encoding as argument and returns the attachment's headers as octets in encoded
558 using the encoding.
559
560 This is not protection using quoted printable or base64 encoding.
561
562 =cut
563
564 sub EncodedHeaders {
565     my $self = shift;
566     my $encoding = shift || 'utf8';
567     return Encode::encode( $encoding, $self->Headers );
568 }
569
570 =head2 GetHeader $TAG
571
572 Returns the value of the header Tag as a string. This bypasses the weeding out
573 done in Headers() above.
574
575 =cut
576
577 sub GetHeader {
578     my $self = shift;
579     my $tag = shift;
580     foreach my $line ($self->_SplitHeaders) {
581         next unless $line =~ /^\Q$tag\E:\s+(.*)$/si;
582
583         #if we find the header, return its value
584         return ($1);
585     }
586     
587     # we found no header. return an empty string
588     return undef;
589 }
590
591 =head2 DelHeader $TAG
592
593 Delete a field from the attachment's headers.
594
595 =cut
596
597 sub DelHeader {
598     my $self = shift;
599     my $tag = shift;
600
601     my $newheader = '';
602     foreach my $line ($self->_SplitHeaders) {
603         next if $line =~ /^\Q$tag\E:\s+(.*)$/is;
604         $newheader .= "$line\n";
605     }
606     return $self->__Set( Field => 'Headers', Value => $newheader);
607 }
608
609 =head2 AddHeader $TAG, $VALUE, ...
610
611 Add one or many fields to the attachment's headers.
612
613 =cut
614
615 sub AddHeader {
616     my $self = shift;
617
618     my $newheader = $self->__Value( 'Headers' );
619     while ( my ($tag, $value) = splice @_, 0, 2 ) {
620         $value = '' unless defined $value;
621         $value =~ s/\s+$//s;
622         $value =~ s/\r+\n/\n /g;
623         $newheader .= "$tag: $value\n";
624     }
625     return $self->__Set( Field => 'Headers', Value => $newheader);
626 }
627
628 =head2 SetHeader ( 'Tag', 'Value' )
629
630 Replace or add a Header to the attachment's headers.
631
632 =cut
633
634 sub SetHeader {
635     my $self = shift;
636     my $tag = shift;
637
638     my $newheader = '';
639     foreach my $line ($self->_SplitHeaders) {
640         if (defined $tag and $line =~ /^\Q$tag\E:\s+(.*)$/i) {
641             $newheader .= "$tag: $_[0]\n";
642             undef $tag;
643         }
644         else {
645             $newheader .= "$line\n";
646         }
647     }
648
649     $newheader .= "$tag: $_[0]\n" if defined $tag;
650     $self->__Set( Field => 'Headers', Value => $newheader);
651 }
652
653 =head2 SplitHeaders
654
655 Returns an array of this attachment object's headers, with one header 
656 per array entry. Multiple lines are folded.
657
658 B<Never> returns C<RT-Send-Bcc> field.
659
660 =cut
661
662 sub SplitHeaders {
663     my $self = shift;
664     return (grep !/^RT-Send-Bcc/i, $self->_SplitHeaders(@_) );
665 }
666
667 =head2 _SplitHeaders
668
669 Returns an array of this attachment object's headers, with one header 
670 per array entry. multiple lines are folded.
671
672
673 =cut
674
675 sub _SplitHeaders {
676     my $self = shift;
677     my $headers = (shift || $self->_Value('Headers'));
678     my @headers;
679     for (split(/\n(?=\w|\z)/,$headers)) {
680         push @headers, $_;
681
682     }
683     return(@headers);
684 }
685
686
687 sub Encrypt {
688     my $self = shift;
689
690     my $txn = $self->TransactionObj;
691     return (0, $self->loc('Permission Denied')) unless $txn->CurrentUserCanSee;
692     return (0, $self->loc('Permission Denied'))
693         unless $txn->TicketObj->CurrentUserHasRight('ModifyTicket');
694     return (0, $self->loc('GnuPG integration is disabled'))
695         unless RT->Config->Get('GnuPG')->{'Enable'};
696     return (0, $self->loc('Attachments encryption is disabled'))
697         unless RT->Config->Get('GnuPG')->{'AllowEncryptDataInDB'};
698
699     require RT::Crypt::GnuPG;
700
701     my $type = $self->ContentType;
702     if ( $type =~ /^x-application-rt\/gpg-encrypted/i ) {
703         return (1, $self->loc('Already encrypted'));
704     } elsif ( $type =~ /^multipart\//i ) {
705         return (1, $self->loc('No need to encrypt'));
706     } else {
707         $type = qq{x-application-rt\/gpg-encrypted; original-type="$type"};
708     }
709
710     my $queue = $txn->TicketObj->QueueObj;
711     my $encrypt_for;
712     foreach my $address ( grep $_,
713         $queue->CorrespondAddress,
714         $queue->CommentAddress,
715         RT->Config->Get('CorrespondAddress'),
716         RT->Config->Get('CommentAddress'),
717     ) {
718         my %res = RT::Crypt::GnuPG::GetKeysInfo( $address, 'private' );
719         next if $res{'exit_code'} || !$res{'info'};
720         %res = RT::Crypt::GnuPG::GetKeysForEncryption( $address );
721         next if $res{'exit_code'} || !$res{'info'};
722         $encrypt_for = $address;
723     }
724     unless ( $encrypt_for ) {
725         return (0, $self->loc('No key suitable for encryption'));
726     }
727
728     $self->__Set( Field => 'ContentType', Value => $type );
729     $self->SetHeader( 'Content-Type' => $type );
730
731     my $content = $self->Content;
732     my %res = RT::Crypt::GnuPG::SignEncryptContent(
733         Content => \$content,
734         Sign => 0,
735         Encrypt => 1,
736         Recipients => [ $encrypt_for ],
737     );
738     if ( $res{'exit_code'} ) {
739         return (0, $self->loc('GnuPG error. Contact with administrator'));
740     }
741
742     my ($status, $msg) = $self->__Set( Field => 'Content', Value => $content );
743     unless ( $status ) {
744         return ($status, $self->loc("Couldn't replace content with encrypted data: [_1]", $msg));
745     }
746     return (1, $self->loc('Successfuly encrypted data'));
747 }
748
749 sub Decrypt {
750     my $self = shift;
751
752     my $txn = $self->TransactionObj;
753     return (0, $self->loc('Permission Denied')) unless $txn->CurrentUserCanSee;
754     return (0, $self->loc('Permission Denied'))
755         unless $txn->TicketObj->CurrentUserHasRight('ModifyTicket');
756     return (0, $self->loc('GnuPG integration is disabled'))
757         unless RT->Config->Get('GnuPG')->{'Enable'};
758
759     require RT::Crypt::GnuPG;
760
761     my $type = $self->ContentType;
762     if ( $type =~ /^x-application-rt\/gpg-encrypted/i ) {
763         ($type) = ($type =~ /original-type="(.*)"/i);
764         $type ||= 'application/octet-stream';
765     } else {
766         return (1, $self->loc('Is not encrypted'));
767     }
768     $self->__Set( Field => 'ContentType', Value => $type );
769     $self->SetHeader( 'Content-Type' => $type );
770
771     my $content = $self->Content;
772     my %res = RT::Crypt::GnuPG::DecryptContent( Content => \$content, );
773     if ( $res{'exit_code'} ) {
774         return (0, $self->loc('GnuPG error. Contact with administrator'));
775     }
776
777     my ($status, $msg) = $self->__Set( Field => 'Content', Value => $content );
778     unless ( $status ) {
779         return ($status, $self->loc("Couldn't replace content with decrypted data: [_1]", $msg));
780     }
781     return (1, $self->loc('Successfuly decrypted data'));
782 }
783
784 =head2 _Value
785
786 Takes the name of a table column.
787 Returns its value as a string, if the user passes an ACL check
788
789 =cut
790
791 sub _Value {
792     my $self  = shift;
793     my $field = shift;
794
795     #if the field is public, return it.
796     if ( $self->_Accessible( $field, 'public' ) ) {
797         return ( $self->__Value( $field, @_ ) );
798     }
799
800     return undef unless $self->TransactionObj->CurrentUserCanSee;
801     return $self->__Value( $field, @_ );
802 }
803
804 # Transactions don't change. by adding this cache congif directiove,
805 # we don't lose pathalogically on long tickets.
806 sub _CacheConfig {
807     {
808         'cache_p'       => 1,
809         'fast_update_p' => 1,
810         'cache_for_sec' => 180,
811     }
812 }
813
814
815
816
817 =head2 id
818
819 Returns the current value of id.
820 (In the database, id is stored as int(11).)
821
822
823 =cut
824
825
826 =head2 TransactionId
827
828 Returns the current value of TransactionId.
829 (In the database, TransactionId is stored as int(11).)
830
831
832
833 =head2 SetTransactionId VALUE
834
835
836 Set TransactionId to VALUE.
837 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
838 (In the database, TransactionId will be stored as a int(11).)
839
840
841 =cut
842
843
844 =head2 Parent
845
846 Returns the current value of Parent.
847 (In the database, Parent is stored as int(11).)
848
849
850
851 =head2 SetParent VALUE
852
853
854 Set Parent to VALUE.
855 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
856 (In the database, Parent will be stored as a int(11).)
857
858
859 =cut
860
861
862 =head2 MessageId
863
864 Returns the current value of MessageId.
865 (In the database, MessageId is stored as varchar(160).)
866
867
868
869 =head2 SetMessageId VALUE
870
871
872 Set MessageId to VALUE.
873 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
874 (In the database, MessageId will be stored as a varchar(160).)
875
876
877 =cut
878
879
880 =head2 Subject
881
882 Returns the current value of Subject.
883 (In the database, Subject is stored as varchar(255).)
884
885
886
887 =head2 SetSubject VALUE
888
889
890 Set Subject to VALUE.
891 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
892 (In the database, Subject will be stored as a varchar(255).)
893
894
895 =cut
896
897
898 =head2 Filename
899
900 Returns the current value of Filename.
901 (In the database, Filename is stored as varchar(255).)
902
903
904
905 =head2 SetFilename VALUE
906
907
908 Set Filename to VALUE.
909 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
910 (In the database, Filename will be stored as a varchar(255).)
911
912
913 =cut
914
915
916 =head2 ContentType
917
918 Returns the current value of ContentType.
919 (In the database, ContentType is stored as varchar(80).)
920
921
922
923 =head2 SetContentType VALUE
924
925
926 Set ContentType to VALUE.
927 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
928 (In the database, ContentType will be stored as a varchar(80).)
929
930
931 =cut
932
933
934 =head2 ContentEncoding
935
936 Returns the current value of ContentEncoding.
937 (In the database, ContentEncoding is stored as varchar(80).)
938
939
940
941 =head2 SetContentEncoding VALUE
942
943
944 Set ContentEncoding to VALUE.
945 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
946 (In the database, ContentEncoding will be stored as a varchar(80).)
947
948
949 =cut
950
951
952 =head2 Content
953
954 Returns the current value of Content.
955 (In the database, Content is stored as longblob.)
956
957
958
959 =head2 SetContent VALUE
960
961
962 Set Content to VALUE.
963 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
964 (In the database, Content will be stored as a longblob.)
965
966
967 =cut
968
969
970 =head2 Headers
971
972 Returns the current value of Headers.
973 (In the database, Headers is stored as longtext.)
974
975
976
977 =head2 SetHeaders VALUE
978
979
980 Set Headers to VALUE.
981 Returns (1, 'Status message') on success and (0, 'Error Message') on failure.
982 (In the database, Headers will be stored as a longtext.)
983
984
985 =cut
986
987
988 =head2 Creator
989
990 Returns the current value of Creator.
991 (In the database, Creator is stored as int(11).)
992
993
994 =cut
995
996
997 =head2 Created
998
999 Returns the current value of Created.
1000 (In the database, Created is stored as datetime.)
1001
1002
1003 =cut
1004
1005
1006
1007 sub _CoreAccessible {
1008     {
1009
1010         id =>
1011                 {read => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
1012         TransactionId =>
1013                 {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => ''},
1014         Parent =>
1015                 {read => 1, write => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
1016         MessageId =>
1017                 {read => 1, write => 1, sql_type => 12, length => 160,  is_blob => 0,  is_numeric => 0,  type => 'varchar(160)', default => ''},
1018         Subject =>
1019                 {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
1020         Filename =>
1021                 {read => 1, write => 1, sql_type => 12, length => 255,  is_blob => 0,  is_numeric => 0,  type => 'varchar(255)', default => ''},
1022         ContentType =>
1023                 {read => 1, write => 1, sql_type => 12, length => 80,  is_blob => 0,  is_numeric => 0,  type => 'varchar(80)', default => ''},
1024         ContentEncoding =>
1025                 {read => 1, write => 1, sql_type => 12, length => 80,  is_blob => 0,  is_numeric => 0,  type => 'varchar(80)', default => ''},
1026         Content =>
1027                 {read => 1, write => 1, sql_type => -4, length => 0,  is_blob => 1,  is_numeric => 0,  type => 'longblob', default => ''},
1028         Headers =>
1029                 {read => 1, write => 1, sql_type => -4, length => 0,  is_blob => 1,  is_numeric => 0,  type => 'longtext', default => ''},
1030         Creator =>
1031                 {read => 1, auto => 1, sql_type => 4, length => 11,  is_blob => 0,  is_numeric => 1,  type => 'int(11)', default => '0'},
1032         Created =>
1033                 {read => 1, auto => 1, sql_type => 11, length => 0,  is_blob => 0,  is_numeric => 0,  type => 'datetime', default => ''},
1034
1035  }
1036 };
1037
1038 RT::Base->_ImportOverlays();
1039
1040 1;