]> git.proxmox.com Git - pmg-api.git/blame - PMG/API2/Quarantine.pm
code cleanup - use defined()
[pmg-api.git] / PMG / API2 / Quarantine.pm
CommitLineData
b66faa68
DM
1package PMG::API2::Quarantine;
2
3use strict;
4use warnings;
5use Time::Local;
6use Time::Zone;
7use Data::Dumper;
8
9use PVE::SafeSyslog;
10use PVE::Exception qw(raise_param_exc);
11use PVE::Tools qw(extract_param);
12use PVE::JSONSchema qw(get_standard_option);
13use PVE::RESTHandler;
14use PVE::INotify;
15
16use PMG::AccessControl;
17use PMG::DBTools;
18
19use base qw(PVE::RESTHandler);
20
21
22__PACKAGE__->register_method ({
23 name => 'index',
24 path => '',
25 method => 'GET',
26 permissions => { user => 'all' },
27 description => "Directory index.",
28 parameters => {
29 additionalProperties => 0,
30 properties => {},
31 },
32 returns => {
33 type => 'array',
34 items => {
35 type => "object",
36 properties => {},
37 },
38 links => [ { rel => 'child', href => "{name}" } ],
39 },
40 code => sub {
41 my ($param) = @_;
42
43 my $result = [
44 { name => 'deliver' },
45 { name => 'spam' },
46 { name => 'virus' },
47 ];
48
49 return $result;
50 }});
51
52__PACKAGE__->register_method ({
53 name => 'spam',
54 path => 'spam',
55 method => 'GET',
56 permissions => { check => [ 'admin', 'qmanager', 'audit', 'quser'] },
57 description => "Show spam mails distribution (per day).",
58 parameters => {
59 additionalProperties => 0,
60 properties => {
61 starttime => {
62 description => "Only consider entries newer than 'startime' (unix epoch).",
63 type => 'integer',
64 minimum => 0,
65 optional => 1,
66 },
67 endtime => {
68 description => "Only consider entries older than 'endtime' (unix epoch).",
69 type => 'integer',
70 minimum => 1,
71 optional => 1,
72 },
73 pmail => {
74 description => "List entries for the user with this primary email address.",
75 type => 'string', format => 'email',
76 optional => 1,
77 },
78 },
79 },
80 returns => {
81 type => 'array',
82 items => {
83 type => "object",
84 properties => {
85 day => {
86 description => "Day (as unix epoch).",
87 type => 'integer',
88 },
bc1ebe25 89 count => {
b66faa68
DM
90 description => "Number of quarantine entries.",
91 type => 'integer',
92 },
93 spamavg => {
94 description => "Average spam level.",
95 type => 'number',
bc1ebe25 96 },
b66faa68
DM
97 },
98 },
99 },
100 code => sub {
101 my ($param) = @_;
102
103 my $rpcenv = PMG::RESTEnvironment->get();
104 my $authuser = $rpcenv->get_user();
105 my $role = $rpcenv->get_role();
106
107 my $pmail = $param->{pmail};
108
109 if ($role eq 'quser') {
110 raise_param_exc({ pmail => "paramater not allwed with role '$role'"})
111 if defined($pmail);
112 $pmail = $authuser;
113 }
114
115 my $res = [];
bc1ebe25 116
b66faa68
DM
117 my $dbh = PMG::DBTools::open_ruledb();
118
ec7035c2 119 my $start = $param->{starttime};
b66faa68
DM
120 my $end = $param->{endtime};
121
122 my $timezone = tz_local_offset();
123
124 my $sth = $dbh->prepare(
125 "SELECT " .
126 "((time + $timezone) / 86400) * 86400 - $timezone as day, " .
127 "count (ID) as count, avg (Spamlevel) as spamavg " .
128 "FROM CMailStore, CMSReceivers WHERE " .
ec7035c2
DM
129 (defined($start) ? "time >= $start AND " : '') .
130 (defined($end) ? "time < $end AND " : '') .
b66faa68
DM
131 (defined($pmail) ? "pmail = ? AND " : '') .
132 "QType = 'S' AND CID = CMailStore_CID AND RID = CMailStore_RID " .
133 "AND Status = 'N' " .
134 "GROUP BY day " .
135 "ORDER BY day DESC");
136
137 if (defined($pmail)) {
138 $sth->execute($pmail);
139 } else {
140 $sth->execute();
141 }
142
143 while (my $ref = $sth->fetchrow_hashref()) {
144 push @$res, $ref;
145 }
146
147 return $res;
148 }});
149
1501;