Template_Overlay.pm 9.85 KB
Newer Older
1
# BEGIN BPS TAGGED BLOCK {{{
2
# 
3
4
# COPYRIGHT:
#  
Jesse Vincent's avatar
Jesse Vincent committed
5
# This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC 
6
#                                          <jesse@bestpractical.com>
7
# 
8
9
10
11
# (Except where explicitly superseded by other copyright notices)
# 
# 
# LICENSE:
12
13
14
15
# 
# 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
16
# from www.gnu.org.
17
18
19
20
21
22
# 
# 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.
# 
23
24
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
Jesse Vincent's avatar
Jesse Vincent committed
25
26
27
# 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.
28
29
30
31
32
33
34
35
36
# 
# 
# 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.)
37
# 
38
39
40
41
42
43
44
45
# 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.
46
# 
47
# END BPS TAGGED BLOCK }}}
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Portions Copyright 2000 Tobias Brox <tobix@cpan.org> 

=head1 NAME

  RT::Template - RT's template object

=head1 SYNOPSIS

  use RT::Template;

=head1 DESCRIPTION


=head1 METHODS


=cut

66
67
68

package RT::Template;

69
use strict;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
70
71
no warnings qw(redefine);

72
use Text::Template;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
73
74
use MIME::Entity;
use MIME::Parser;
75
76
use File::Temp qw /tempdir/;

Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
sub _Accessible {
    my $self = shift;
    my %Cols = (
        id            => 'read',
        Name          => 'read/write',
        Description   => 'read/write',
        Type          => 'read/write',    #Type is one of Action or Message
        Content       => 'read/write',
        Queue         => 'read/write',
        Creator       => 'read/auto',
        Created       => 'read/auto',
        LastUpdatedBy => 'read/auto',
        LastUpdated   => 'read/auto'
    );
    return $self->SUPER::_Accessible( @_, %Cols );
}

sub _Set {
    my $self = shift;
96
97
98
    
    unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
        return ( 0, $self->loc('Permission Denied') );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
99
    }
100
    return $self->SUPER::_Set( @_ );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
101
102
103
104
}

=head2 _Value

Ruslan Zakirov's avatar
Ruslan Zakirov committed
105
106
Takes the name of a table column. Returns its value as a string,
if the user passes an ACL check, otherwise returns undef.
107

Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
108
109
110
111
=cut

sub _Value {
    my $self  = shift;
112
113
114

    unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
        return undef;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
115
    }
116
    return $self->__Value( @_ );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
117
118
119
120
121

}

=head2 Load <identifer>

Ruslan Zakirov's avatar
Ruslan Zakirov committed
122
Load a template, either by number or by name.
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
123

124
125
126
127
128
129
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.

Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
130
131
132
133
134
=cut

sub Load {
    my $self       = shift;
    my $identifier = shift;
135
    return undef unless $identifier;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
136

137
138
    if ( $identifier =~ /\D/ ) {
        return $self->LoadByCol( 'Name', $identifier );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
139
    }
140
    return $self->LoadById( $identifier );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
141
142
143
144
}

=head2 LoadGlobalTemplate NAME

Jesse Vincent's avatar
Jesse Vincent committed
145
Load the global template with the name NAME
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
146
147
148
149
150
151
152
153
154
155

=cut

sub LoadGlobalTemplate {
    my $self = shift;
    my $id   = shift;

    return ( $self->LoadQueueTemplate( Queue => 0, Name => $id ) );
}

Ruslan Zakirov's avatar
Ruslan Zakirov committed
156
=head2 LoadQueueTemplate (Queue => QUEUEID, Name => NAME)
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
157
158
159
160
161
162
163
164
165

Loads the Queue template named NAME for Queue QUEUE.

=cut

sub LoadQueueTemplate {
    my $self = shift;
    my %args = (
        Queue => undef,
166
167
        Name  => undef,
        @_
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
168
169
    );

170
    return ( $self->LoadByCols( Name => $args{'Name'}, Queue => $args{'Queue'} ) );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

}

=head2 Create

Takes a paramhash of Content, Queue, Name and Description.
Name should be a unique string identifying this Template.
Description and Content should be the template's title and content.
Queue should be 0 for a global template and the queue # for a queue-specific 
template.

Returns the Template's id # if the create was successful. Returns undef for
unknown database failure.

=cut

sub Create {
    my $self = shift;
    my %args = (
        Content     => undef,
        Queue       => 0,
        Description => '[no description]',
193
194
        Type        => 'Action', #By default, template are 'Action' templates
        Name        => undef,
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
195
196
197
        @_
    );

198
    unless ( $args{'Queue'} ) {
199
        unless ( $self->CurrentUser->HasRight(Right =>'ModifyTemplate', Object => $RT::System) ) {
200
            return ( undef, $self->loc('Permission denied') );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
201
        }
202
        $args{'Queue'} = 0;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
203
204
205
    }
    else {
        my $QueueObj = new RT::Queue( $self->CurrentUser );
206
        $QueueObj->Load( $args{'Queue'} ) || return ( undef, $self->loc('Invalid queue') );
207
    
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
208
        unless ( $QueueObj->CurrentUserHasRight('ModifyTemplate') ) {
209
            return ( undef, $self->loc('Permission denied') );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
210
        }
211
        $args{'Queue'} = $QueueObj->Id;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
212
213
214
    }

    my $result = $self->SUPER::Create(
215
216
        Content     => $args{'Content'},
        Queue       => $args{'Queue'},
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
217
        Description => $args{'Description'},
218
        Name        => $args{'Name'},
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    );

    return ($result);

}

=head2 Delete

Delete this template.

=cut

sub Delete {
    my $self = shift;

234
    unless ( $self->CurrentUserHasQueueRight('ModifyTemplate') ) {
235
        return ( 0, $self->loc('Permission Denied') );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
236
237
238
239
240
241
242
243
244
245
    }

    return ( $self->SUPER::Delete(@_) );
}

sub MIMEObj {
    my $self = shift;
    return ( $self->{'MIMEObj'} );
}

Kevin Riggle's avatar
Kevin Riggle committed
246
=head2 Parse
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
247

Ruslan Zakirov's avatar
Ruslan Zakirov committed
248
249
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.
250

Ruslan Zakirov's avatar
Ruslan Zakirov committed
251
Takes a hash containing Argument, TicketObj, and TransactionObj.
252

Ruslan Zakirov's avatar
Ruslan Zakirov committed
253
254
It returns a tuple of (val, message). If val is false, the message contains
an error message.
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
255
256
257
258
259
260
261

=cut

sub Parse {
    my $self = shift;

    #We're passing in whatever we were passed. it's destined for _ParseContent
262
    my ($content, $msg) = $self->_ParseContent(@_);
263
    return ( 0, $msg ) unless defined $content && length $content;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
264
265
266
267
268

    #Lets build our mime Entity

    my $parser = MIME::Parser->new();

269
270
271
    # On some situations TMPDIR is non-writable. sad but true.
    $parser->output_to_core(1);
    $parser->tmp_to_core(1);
272

273
274
    #If someone includes a message, don't extract it
    $parser->extract_nested_messages(1);
275

276
277
    # Set up the prefix for files with auto-generated names:
    $parser->output_prefix("part");
278

279
280
281
282
    # 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);

Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
283
284
285
    ### Should we forgive normally-fatal errors?
    $parser->ignore_errors(1);
    $self->{'MIMEObj'} = eval { $parser->parse_data($content) };
286
287
    if ( my $error = $@ || $parser->last_error ) {
        $RT::Logger->error( "$error" );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
288
289
290
291
        return ( 0, $error );
    }

    # Unfold all headers
292
    $self->{'MIMEObj'}->head->unfold;
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
293

294
    return ( 1, $self->loc("Template parsed") );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
295
296
297
298
299
300
301
302
303
304
305
306
307
308

}

# Perform Template substitutions on the template

sub _ParseContent {
    my $self = shift;
    my %args = (
        Argument       => undef,
        TicketObj      => undef,
        TransactionObj => undef,
        @_
    );

309
310
311
312
313
314
315
    unless ( $self->CurrentUserHasQueueRight('ShowTemplate') ) {
        return (undef, $self->loc("Permissions denied"));
    }

    my $content = $self->SUPER::_Value('Content');
    unless ( defined $content && length $content ) {
        return ( '', $self->loc("Template is empty") );
316
317
    }

Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
318
319
320
    # We need to untaint the content of the template, since we'll be working
    # with it
    $content =~ s/^(.*)$/$1/;
321
322
    my $template = Text::Template->new(
        TYPE   => 'STRING',
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
323
324
325
        SOURCE => $content
    );

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
    $args{'Ticket'} = delete $args{'TicketObj'} if $args{'TicketObj'};
    $args{'Transaction'} = delete $args{'TransactionObj'} if $args{'TransactionObj'};
    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;
    }

    $args{'Requestor'} = eval { $args{'Ticket'}->Requestors->UserMembersObj->First->Name };
    $args{'rtname'}    = RT->Config->Get('rtname');
    if ( $args{'Ticket'} ) {
        $args{'loc'} = sub { $args{'Ticket'}->loc(@_) };
    } else {
        $args{'loc'} = sub { $self->loc(@_) };
    }

343
    my $is_broken = 0;
344
345
346
347
348
349
350
351
352
353
    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;
        }, 
    );
354
    return ( undef, $self->loc('Template parsing error') ) if $is_broken;
355
356
357

    # MIME::Parser has problems dealing with high-bit utf8 data.
    Encode::_utf8_off($retval);
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
    return ($retval);
}

=head2 CurrentUserHasQueueRight

Helper function to call the template's queue's CurrentUserHasQueueRight with the passed in args.

=cut

sub CurrentUserHasQueueRight {
    my $self = shift;
    return ( $self->QueueObj->CurrentUserHasRight(@_) );
}

1;