| Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php
 | 
 | 2 | function mailq($_action, $_data = null) {
 | 
 | 3 |   if ($_SESSION['mailcow_cc_role'] != "admin") {
 | 
 | 4 |     $_SESSION['return'][] = array(
 | 
 | 5 |       'type' => 'danger',
 | 
 | 6 |       'log' => array(__FUNCTION__, $_action, $_data),
 | 
 | 7 |       'msg' => 'access_denied'
 | 
 | 8 |     );
 | 
 | 9 |     return false;
 | 
 | 10 |   }
 | 
 | 11 |   function process_mailq_output($returned_output, $_action, $_data) {
 | 
 | 12 |     if ($returned_output !== NULL) {
 | 
 | 13 |       if ($_action == 'cat') {
 | 
 | 14 |         logger(array('return' => array(
 | 
 | 15 |           array(
 | 
 | 16 |             'type' => 'success',
 | 
 | 17 |             'log' => array(__FUNCTION__, $_action, $_data),
 | 
 | 18 |             'msg' => 'queue_cat_success'
 | 
 | 19 |           )
 | 
 | 20 |         )));
 | 
 | 21 |         return $returned_output;
 | 
 | 22 |       }
 | 
 | 23 |       else {
 | 
 | 24 |         if (isset($returned_output['type']) && $returned_output['type'] == 'danger') {
 | 
 | 25 |           $_SESSION['return'][] = array(
 | 
 | 26 |             'type' => 'danger',
 | 
 | 27 |             'log' => array(__FUNCTION__, $_action, $_data),
 | 
 | 28 |             'msg' => 'Error: ' . $returned_output['msg']
 | 
 | 29 |           );
 | 
 | 30 |         }
 | 
 | 31 |         if (isset($returned_output['type']) && $returned_output['type'] == 'success') {
 | 
 | 32 |           $_SESSION['return'][] = array(
 | 
 | 33 |             'type' => 'success',
 | 
 | 34 |             'log' => array(__FUNCTION__, $_action, $_data),
 | 
 | 35 |             'msg' => 'queue_command_success'
 | 
 | 36 |           );
 | 
 | 37 |         }
 | 
 | 38 |       }
 | 
 | 39 |     }
 | 
 | 40 |     else {
 | 
 | 41 |       $_SESSION['return'][] = array(
 | 
 | 42 |         'type' => 'danger',
 | 
 | 43 |         'log' => array(__FUNCTION__, $_action, $_data),
 | 
 | 44 |         'msg' => 'unknown'
 | 
 | 45 |       );
 | 
 | 46 |     }
 | 
 | 47 |   }
 | 
 | 48 |   if ($_action == 'get') {
 | 
 | 49 |     $mailq_lines = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => 'list'));
 | 
 | 50 |     $lines = 0;
 | 
 | 51 |     // Hard limit to 10000 items
 | 
 | 52 |     foreach (preg_split("/((\r?\n)|(\r\n?))/", $mailq_lines) as $mailq_item) if ($lines++ < 10000) {
 | 
 | 53 |       if (empty($mailq_item) || $mailq_item == '1') {
 | 
 | 54 |         continue;
 | 
 | 55 |       }
 | 
 | 56 |       $mq_line = json_decode($mailq_item, true);
 | 
 | 57 |       if ($mq_line !== NULL) {
 | 
 | 58 |         $rcpts = array();
 | 
 | 59 |         foreach ($mq_line['recipients'] as $rcpt) {
 | 
 | 60 |           if (isset($rcpt['delay_reason'])) {
 | 
 | 61 |             $rcpts[] = $rcpt['address'] . ' (' . $rcpt['delay_reason'] . ')';
 | 
 | 62 |           }
 | 
 | 63 |           else {
 | 
 | 64 |             $rcpts[] = $rcpt['address'];
 | 
 | 65 |           }
 | 
 | 66 |         }
 | 
 | 67 |         if (!empty($rcpts)) {
 | 
 | 68 |           $mq_line['recipients'] = $rcpts;
 | 
 | 69 |         }
 | 
 | 70 |         $line[] = $mq_line;
 | 
 | 71 |       }
 | 
 | 72 |     }
 | 
 | 73 |     if (!isset($line) || empty($line)) {
 | 
 | 74 |       return '[]';
 | 
 | 75 |     }
 | 
 | 76 |     else {
 | 
 | 77 |       return json_encode($line);
 | 
 | 78 |     }
 | 
 | 79 |   }
 | 
 | 80 |   elseif ($_action == 'delete') {
 | 
 | 81 |     if (!is_array($_data['qid'])) {
 | 
 | 82 |       $qids = array();
 | 
 | 83 |       $qids[] = $_data['qid'];
 | 
 | 84 |     }
 | 
 | 85 |     else {
 | 
 | 86 |       $qids = $_data['qid'];
 | 
 | 87 |     }
 | 
 | 88 |     $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => 'delete', 'items' => $qids));
 | 
 | 89 |     process_mailq_output(json_decode($docker_return, true), $_action, $_data);
 | 
 | 90 |   }
 | 
 | 91 |   elseif ($_action == 'cat') {
 | 
 | 92 |     if (!is_array($_data['qid'])) {
 | 
 | 93 |       $qids = array();
 | 
 | 94 |       $qids[] = $_data['qid'];
 | 
 | 95 |     }
 | 
 | 96 |     else {
 | 
 | 97 |       $qids = $_data['qid'];
 | 
 | 98 |     }
 | 
 | 99 |     $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => 'cat', 'items' => $qids));
 | 
 | 100 |     return process_mailq_output($docker_return, $_action, $_data);
 | 
 | 101 |   }
 | 
 | 102 |   elseif ($_action == 'edit') {
 | 
 | 103 |     if (in_array($_data['action'], array('hold', 'unhold', 'deliver'))) {
 | 
 | 104 |       if (!is_array($_data['qid'])) {
 | 
 | 105 |         $qids = array();
 | 
 | 106 |         $qids[] = $_data['qid'];
 | 
 | 107 |       }
 | 
 | 108 |       else {
 | 
 | 109 |         $qids = $_data['qid'];
 | 
 | 110 |       }
 | 
 | 111 |       if (!empty($qids)) {
 | 
 | 112 |         $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => $_data['action'], 'items' => $qids));
 | 
 | 113 |         process_mailq_output(json_decode($docker_return, true), $_action, $_data);
 | 
 | 114 |       }
 | 
 | 115 |     }
 | 
 | 116 |     if (in_array($_data['action'], array('flush', 'super_delete'))) {
 | 
 | 117 |       $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => $_data['action']));
 | 
 | 118 |       process_mailq_output(json_decode($docker_return, true), $_action, $_data);
 | 
 | 119 |     }
 | 
 | 120 |   }
 | 
 | 121 | }
 |