Initial release
[manu/RT-Extension-AttachmentFilter.git] / lib / RT / Extension / AttachmentFilter.pm
1 package RT::Extension::AttachmentFilter;
2
3 use strict;
4 use warnings;
5 require RT::Interface::Web;
6 package HTML::Mason::Commands;
7
8 our $VERSION = '0.01';
9
10 =head1 NAME
11
12 RT-Extension-AttachmentFilter - Set forbidden attachments file names or extensions
13
14 =head1 DESCRIPTION
15
16
17 Ses RT extension allows to forbid some files names or extensions from web
18 attachment upload.
19
20 It's use may be to match an existing mail policy.
21
22 The filter can be defined per queue so queues that do not have outgoing emails
23 configured can have less restrictions.
24
25 See the configuration example under L</INSTALLATION>.
26
27 =head1 INSTALLATION
28
29 =over
30
31 =item C<perl Makefile.PL>
32
33 =item C<make>
34
35 =item C<make install>
36
37 May need root permissions
38
39 =item Edit your F</opt/rt4/etc/RT_SiteConfig.pm>
40
41 If you are using RT 4.2 or greater, add this line:
42
43     Plugin('RT::Extension::AttachmentFilter');
44
45 For RT 4.0, add this line:
46
47     Set(@Plugins, qw(RT::Extension::AttachmentFilter));
48
49 or add C<RT::Extension::AttachmentFilter> to your existing C<@Plugins> line.
50
51 Then configure the limits using the $AttachmentFilter config option. This
52 option takes the generic form of:
53
54     Set( $AttachmentFilter, 
55         '*'    => 'regexp',
56         queue1 => 'regexp',
57     );
58
59 which allows to set limit per queue. '*' means for all queues that do
60 not have a specific filter.
61
62 'regexp' is a perl regular expression against the filename.
63
64 =item Clear your mason cache
65
66     rm -rf /opt/rt4/var/mason_data/obj
67
68 =item Restart your webserver
69
70 =back
71
72 =head1 IMPLEMENTATION DETAILS
73
74 =head2 Methods
75
76 =head3 CheckAttachmentName
77
78 This is the main routine, it takes an attachment file name and an optional
79 queue (name, id or object) and validate it against RT $AttachmentFilter
80 configuration.
81
82 Returns undef if allowed, (1, error message) else.
83
84 =cut
85
86 sub CheckAttachmentFilter {
87     my %args = (
88         FileName => '',
89         Queue       => '',
90         @_
91     );
92
93     my $filters = RT->Config->Get('AttachmentFilter');
94
95     return undef unless ( $filters );
96     return undef unless ( $args{'FileName'} );
97
98     if ( ref($filters) ne 'HASH' ) {
99         RT->Logger->crit("Configuration error, AttachmentFilter must be a HASH");
100     }
101
102     my $filter;
103     my $SystemQueue = RT::Queue->new( RT->SystemUser );
104     if ( $args{'Queue'} ) {
105         if ( ref($args{'Queue'}) eq 'RT::Queue' ) {
106             $SystemQueue->Load( $args{'Queue'}->id );
107         } else {
108             $SystemQueue->Load( $args{'Queue'} );
109         }
110         
111         if ( $SystemQueue && $SystemQueue->id ) {
112             $filter = $filters->{$SystemQueue->Name} || $filters->{'*'};
113         } else {
114             RT->Logger->error( "Wrong queue passed to RT::Extension::AttachmentFilter->CheckAttachmentFilter" );
115         }
116     
117     }
118
119     $filter ||= $filters->{'*'}; 
120
121     unless ( $filter ) {
122         return undef;
123     }
124
125     if ( $args{'FileName'} =~ m/$filter/ ) {
126         return 1, loc("File name [_1] forbidden", $args{'FileName'});
127     }
128
129     return undef;
130 }
131
132
133 =head1 TODO
134
135 =over 4
136
137 =item Dynamic enforcement using javascript
138
139 =back
140
141 =head1 AUTHOR
142
143 Emmanuel Lacour, E<lt>elacour@home-dn.netE<gt>
144
145 =head1 BUGS
146
147 All bugs should be reported via email to
148
149     L<bug-RT-Extension-AttachmentFilter@rt.cpan.org|mailto:bug-RT-Extension-AttachmentFilter@rt.cpan.org>
150
151 or via the web at
152
153     L<rt.cpan.org|http://rt.cpan.org/Public/Dist/Display.html?Name=RT-Extension-AttachmentFilter>.
154
155 =head1 LICENSE AND COPYRIGHT
156
157 This software is Copyright (c) 2016 by Emmanuel Lacour <elacour@home-dn.net>
158
159 This is free software, licensed under:
160
161   The GNU General Public License, Version 2, June 1991
162
163 =cut
164
165 1;