functions.mailq.inc.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. global $lang;
  49. if ($_action == 'get') {
  50. $mailq_lines = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => 'list'));
  51. $lines = 0;
  52. // Hard limit to 10000 items
  53. foreach (preg_split("/((\r?\n)|(\r\n?))/", $mailq_lines) as $mailq_item) if ($lines++ < 10000) {
  54. if (empty($mailq_item) || $mailq_item == '1') {
  55. continue;
  56. }
  57. $mq_line = json_decode($mailq_item, true);
  58. if ($mq_line !== NULL) {
  59. $rcpts = array();
  60. foreach ($mq_line['recipients'] as $rcpt) {
  61. if (isset($rcpt['delay_reason'])) {
  62. $rcpts[] = $rcpt['address'] . ' (' . $rcpt['delay_reason'] . ')';
  63. }
  64. else {
  65. $rcpts[] = $rcpt['address'];
  66. }
  67. }
  68. if (!empty($rcpts)) {
  69. $mq_line['recipients'] = $rcpts;
  70. }
  71. $line[] = $mq_line;
  72. }
  73. }
  74. if (!isset($line) || empty($line)) {
  75. return '{}';
  76. }
  77. else {
  78. return json_encode($line);
  79. }
  80. }
  81. elseif ($_action == 'delete') {
  82. if (!is_array($_data['qid'])) {
  83. $qids = array();
  84. $qids[] = $_data['qid'];
  85. }
  86. else {
  87. $qids = $_data['qid'];
  88. }
  89. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => 'delete', 'items' => $qids));
  90. process_mailq_output(json_decode($docker_return, true), $_action, $_data);
  91. }
  92. elseif ($_action == 'cat') {
  93. if (!is_array($_data['qid'])) {
  94. $qids = array();
  95. $qids[] = $_data['qid'];
  96. }
  97. else {
  98. $qids = $_data['qid'];
  99. }
  100. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => 'cat', 'items' => $qids));
  101. return process_mailq_output($docker_return, $_action, $_data);
  102. }
  103. elseif ($_action == 'edit') {
  104. if (in_array($_data['action'], array('hold', 'unhold', 'deliver'))) {
  105. if (!is_array($_data['qid'])) {
  106. $qids = array();
  107. $qids[] = $_data['qid'];
  108. }
  109. else {
  110. $qids = $_data['qid'];
  111. }
  112. if (!empty($qids)) {
  113. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => $_data['action'], 'items' => $qids));
  114. process_mailq_output(json_decode($docker_return, true), $_action, $_data);
  115. }
  116. }
  117. if (in_array($_data['action'], array('flush', 'super_delete'))) {
  118. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => $_data['action']));
  119. process_mailq_output(json_decode($docker_return, true), $_action, $_data);
  120. }
  121. }
  122. }