functions.mailq.inc.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 (isset($returned_output['type']) && $returned_output['type'] == 'danger') {
  14. $_SESSION['return'][] = array(
  15. 'type' => 'danger',
  16. 'log' => array('mailq', $_action, $_data),
  17. 'msg' => 'Error: ' . $returned_output['msg']
  18. );
  19. }
  20. if (isset($returned_output['type']) && $returned_output['type'] == 'success') {
  21. $_SESSION['return'][] = array(
  22. 'type' => 'success',
  23. 'log' => array('mailq', $_action, $_data),
  24. 'msg' => 'queue_command_success'
  25. );
  26. }
  27. }
  28. else {
  29. $_SESSION['return'][] = array(
  30. 'type' => 'danger',
  31. 'log' => array('mailq', $_action, $_data),
  32. 'msg' => 'unknown'
  33. );
  34. }
  35. }
  36. global $lang;
  37. switch ($_action) {
  38. case 'get':
  39. $mailq_lines = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => 'list'));
  40. $lines = 0;
  41. // Hard limit to 10000 items
  42. foreach (preg_split("/((\r?\n)|(\r\n?))/", $mailq_lines) as $mailq_item) if ($lines++ < 10000) {
  43. if (empty($mailq_item) || $mailq_item == '1') {
  44. continue;
  45. }
  46. $mq_line = json_decode($mailq_item, true);
  47. if ($mq_line !== NULL) {
  48. $rcpts = array();
  49. foreach ($mq_line['recipients'] as $rcpt) {
  50. if (isset($rcpt['delay_reason'])) {
  51. $rcpts[] = $rcpt['address'] . ' (' . $rcpt['delay_reason'] . ')';
  52. }
  53. else {
  54. $rcpts[] = $rcpt['address'];
  55. }
  56. }
  57. if (!empty($rcpts)) {
  58. $mq_line['recipients'] = $rcpts;
  59. }
  60. $line[] = $mq_line;
  61. }
  62. }
  63. if (!isset($line) || empty($line)) {
  64. return '{}';
  65. }
  66. else {
  67. return json_encode($line);
  68. }
  69. break;
  70. case 'delete':
  71. if (!is_array($_data['qid'])) {
  72. $qids = array();
  73. $qids[] = $_data['qid'];
  74. }
  75. else {
  76. $qids = $_data['qid'];
  77. }
  78. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => 'delete', 'items' => $qids));
  79. process_mailq_output(json_decode($docker_return, true), $_action, $_data);
  80. break;
  81. case 'edit':
  82. if (in_array($_data['action'], array('hold', 'unhold', 'deliver'))) {
  83. if (!is_array($_data['qid'])) {
  84. $qids = array();
  85. $qids[] = $_data['qid'];
  86. }
  87. else {
  88. $qids = $_data['qid'];
  89. }
  90. if (!empty($qids)) {
  91. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => $_data['action'], 'items' => $qids));
  92. process_mailq_output(json_decode($docker_return, true), $_action, $_data);
  93. }
  94. }
  95. if (in_array($_data['action'], array('flush', 'super_delete'))) {
  96. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => $_data['action']));
  97. process_mailq_output(json_decode($docker_return, true), $_action, $_data);
  98. }
  99. break;
  100. case 'get':
  101. // todo: move get from json_api here
  102. break;
  103. }
  104. }