]> git.proxmox.com Git - pmg-api.git/blob - src/PMG/RuleDB/ContentTypeFilter.pm
drop executable flag from perl modules
[pmg-api.git] / src / PMG / RuleDB / ContentTypeFilter.pm
1 package PMG::RuleDB::ContentTypeFilter;
2
3 use strict;
4 use warnings;
5 use DBI;
6
7 use PVE::SafeSyslog;
8 use MIME::Words;
9
10 use PMG::RuleDB::MatchField;
11
12 use base qw(PMG::RuleDB::MatchField);
13
14 my $oldtypemap = {
15 'application/x-msdos-program' => 'application/x-ms-dos-executable',
16 'application/java-vm' => 'application/x-java',
17 'application/x-javascript' => 'application/javascript',
18 };
19
20 sub otype {
21 return 3003;
22 }
23
24 sub otype_text {
25 return 'ContentType Filter';
26 }
27
28 sub new {
29 my ($type, $fvalue, $ogroup) = @_;
30
31 my $class = ref($type) || $type;
32
33 # translate old values
34 if ($fvalue && (my $nt = $oldtypemap->{$fvalue})) {
35 $fvalue = $nt;
36 }
37
38 my $self = $class->SUPER::new('content-type', $fvalue, $ogroup);
39
40 return $self;
41 }
42
43 sub load_attr {
44 my ($type, $ruledb, $id, $ogroup, $value) = @_;
45
46 my $class = ref($type) || $type;
47
48 my $obj = $class->SUPER::load_attr($ruledb, $id, $ogroup, $value);
49
50 # translate old values
51 if ($obj->{field_value} && (my $nt = $oldtypemap->{$obj->{field_value}})) {
52 $obj->{field_value} = $nt;
53 }
54
55 return $obj;
56 }
57
58 sub parse_entity {
59 my ($self, $entity) = @_;
60
61 my $res;
62
63 # match subtypes? We currently do exact matches only.
64
65 if (my $id = $entity->head->mime_attr ('x-proxmox-tmp-aid')) {
66 chomp $id;
67
68 my $header_ct = $entity->head->mime_attr ('content-type');
69
70 my $magic_ct = $entity->{PMX_magic_ct};
71
72 my $glob_ct = $entity->{PMX_glob_ct};
73
74 if ($header_ct && $header_ct =~ m|$self->{field_value}|) {
75 push @$res, $id;
76 } elsif ($magic_ct && $magic_ct =~ m|$self->{field_value}|) {
77 push @$res, $id;
78 } elsif ($glob_ct && $glob_ct =~ m|$self->{field_value}|) {
79 push @$res, $id;
80 }
81 }
82
83 foreach my $part ($entity->parts) {
84 if (my $match = $self->parse_entity ($part)) {
85 push @$res, @$match;
86 }
87 }
88
89 return $res;
90 }
91
92 sub what_match {
93 my ($self, $queue, $entity, $msginfo) = @_;
94
95 return $self->parse_entity ($entity);
96 }
97
98 sub properties {
99 my ($class) = @_;
100
101 return {
102 contenttype => {
103 description => "Content Type",
104 type => 'string',
105 pattern => '[0-9a-zA-Z\/\\\[\]\+\-\.\*\_]+',
106 maxLength => 1024,
107 },
108 };
109 }
110
111 sub get {
112 my ($self) = @_;
113
114 return { contenttype => $self->{field_value} };
115 }
116
117 sub update {
118 my ($self, $param) = @_;
119
120 $self->{field_value} = $param->{contenttype};
121 }
122
123 1;
124
125 __END__
126
127 =head1 PMG::RuleDB::ContentTypeFilter
128
129 Content type filter.