]> git.proxmox.com Git - pmg-api.git/blobdiff - src/PMG/CLI/pmgqm.pm
refactor domain_regex to Utils
[pmg-api.git] / src / PMG / CLI / pmgqm.pm
index 349816ab6487168dc46b082c42d039a9da866e69..39253dbfe0ae632798b2c537acae3f50a538a875 100755 (executable)
@@ -33,31 +33,6 @@ sub setup_environment {
     PMG::RESTEnvironment->setup_default_cli_env();
 }
 
-sub domain_regex {
-    my ($domains) = @_;
-
-    my @ra;
-    foreach my $d (@$domains) {
-       # skip domains with non-DNS name characters
-       next if $d =~ m/[^A-Za-z0-9\-\.]/;
-       if ($d =~ m/^\.(.*)$/) {
-           my $dom = $1;
-           $dom =~ s/\./\\\./g;
-           push @ra, $dom;
-           push @ra, "\.\*\\.$dom";
-       } else {
-           $d =~ s/\./\\\./g;
-           push @ra, $d;
-       }
-    }
-
-    my $re = join ('|', @ra);
-
-    my $regex = qr/\@($re)$/i;
-
-    return $regex;
-}
-
 sub get_item_data {
     my ($data, $ref) = @_;
 
@@ -94,7 +69,7 @@ sub get_item_data {
     $item->{spaminfo} = $ref->{info};
     $item->{file} = $ref->{file};
 
-    my $basehref = "$data->{protocol}://$data->{fqdn}:$data->{port}/quarantine";
+    my $basehref = "$data->{protocol_fqdn_port}/quarantine";
     my $ticket = uri_escape($data->{ticket});
     $item->{href} = "$basehref?ticket=$ticket&cselect=$item->{id}&date=$item->{date}";
 
@@ -145,7 +120,7 @@ __PACKAGE__->register_method ({
        my $dbh = PMG::DBTools::open_ruledb();
 
        my $domains = PVE::INotify::read_file('domains');
-       my $domainregex = domain_regex([keys %$domains]);
+       my $domainregex = PMG::Utils::domain_regex([keys %$domains]);
 
        my $sth = $dbh->prepare(
            "SELECT pmail, AVG(spamlevel) as spamlevel, count(*)  FROM CMailStore, CMSReceivers " .
@@ -248,6 +223,12 @@ __PACKAGE__->register_method ({
 
        my $protocol = $cfg->get('spamquar', 'protocol') // 'https';
 
+       my $protocol_fqdn_port = "$protocol://$fqdn";
+       if (($protocol eq 'https' && $port != 443) ||
+           ($protocol eq 'http' && $port != 80)) {
+           $protocol_fqdn_port .= ":$port";
+       }
+
        my $global_data = {
            protocol => $protocol,
            port => $port,
@@ -256,6 +237,7 @@ __PACKAGE__->register_method ({
            date => strftime("%F", localtime($end - 1)),
            timespan => $timespan,
            items => [],
+           protocol_fqdn_port => $protocol_fqdn_port,
        };
 
        my $mailfrom = $cfg->get ('spamquar', 'mailfrom') //
@@ -271,7 +253,7 @@ __PACKAGE__->register_method ({
        }
 
        my $domains = PVE::INotify::read_file('domains');
-       my $domainregex = domain_regex([keys %$domains]);
+       my $domainregex = PMG::Utils::domain_regex([keys %$domains]);
 
        my $template = "spamreport-${reportstyle}.tt";
        my $found = 0;
@@ -326,7 +308,7 @@ __PACKAGE__->register_method ({
                $data->{pmail} = $creceiver;
                $data->{ticket} = PMG::Ticket::assemble_quarantine_ticket($data->{pmail});
                my $esc_ticket = uri_escape($data->{ticket});
-               $data->{managehref} = "$protocol://$fqdn:$port/quarantine?ticket=${esc_ticket}";
+               $data->{managehref} = "$protocol_fqdn_port/quarantine?ticket=${esc_ticket}";
            }
 
            push @{$data->{items}}, get_item_data($data, $ref);