]> git.proxmox.com Git - pmg-api.git/blame - PMG/RuleDB/ContentTypeFilter.pm
fix bug #1625 - change default rule priorities
[pmg-api.git] / PMG / RuleDB / ContentTypeFilter.pm
CommitLineData
11b7d5dc
DM
1package PMG::RuleDB::ContentTypeFilter;
2
3use strict;
4use warnings;
11b7d5dc
DM
5use DBI;
6
7use PVE::SafeSyslog;
8use MIME::Words;
9
10use PMG::RuleDB::MatchField;
11
b8ea5d5d 12use base qw(PMG::RuleDB::MatchField);
11b7d5dc 13
11b7d5dc
DM
14my $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
11b7d5dc
DM
20sub otype {
21 return 3003;
22}
23
24sub otype_text {
25 return 'ContentType Filter';
26}
27
11b7d5dc
DM
28sub new {
29 my ($type, $fvalue, $ogroup) = @_;
a0ec4098 30
11b7d5dc
DM
31 my $class = ref($type) || $type;
32
33 # translate old values
34 if ($fvalue && (my $nt = $oldtypemap->{$fvalue})) {
35 $fvalue = $nt;
a0ec4098 36 }
11b7d5dc
DM
37
38 my $self = $class->SUPER::new('content-type', $fvalue, $ogroup);
8b748e2a 39
11b7d5dc
DM
40 return $self;
41}
42
43sub load_attr {
44 my ($type, $ruledb, $id, $ogroup, $value) = @_;
a0ec4098 45
11b7d5dc
DM
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
11b7d5dc
DM
55 return $obj;
56}
57
58sub 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 }
a0ec4098 82
11b7d5dc
DM
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
92sub what_match {
93 my ($self, $queue, $entity, $msginfo) = @_;
94
95 return $self->parse_entity ($entity);
96}
97
39458a82
DC
98sub 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
111sub get {
112 my ($self) = @_;
113
114 return { contenttype => $self->{field_value} };
115}
116
117sub update {
118 my ($self, $param) = @_;
119
120 $self->{field_value} = $param->{contenttype};
121}
11b7d5dc
DM
122
1231;
124
125__END__
126
127=head1 PMG::RuleDB::ContentTypeFilter
128
129Content type filter.