functions.mailq.inc.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. $mailq_lines .= '{"queue_name":"hold","queue_id":"6004D1021DE","arrival_time":1540195064,"message_size":269,"sender":"watchdog@invalid","recipients":[{"address":"test@example.com","delay_reason":"connect to 123.0.0.1[123.0.0.1]:25: Connection refused"},{"address":"test@example.com","delay_reason":"connect to 123.0.0.1[123.0.0.1]:25: Connection refused"}]}';
  41. $lines = 0;
  42. // Hard limit to 10000 items
  43. foreach (preg_split("/((\r?\n)|(\r\n?))/", $mailq_lines) as $mailq_item) if ($lines++ < 10000) {
  44. if (empty($mailq_item) || $mailq_item == '1') {
  45. continue;
  46. }
  47. $mq_line = json_decode($mailq_item, true);
  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. $mq_line['recipients'] = $rcpts;
  58. $line[] = $mq_line;
  59. }
  60. if (!isset($line) || empty($line)) {
  61. return '{}';
  62. }
  63. else {
  64. return json_encode($line);
  65. }
  66. break;
  67. case 'delete':
  68. if (!is_array($_data['qid'])) {
  69. $qids = array();
  70. $qids[] = $_data['qid'];
  71. }
  72. else {
  73. $qids = $_data['qid'];
  74. }
  75. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => 'delete', 'items' => $qids));
  76. process_mailq_output(json_decode($docker_return, true), $_action, $_data);
  77. break;
  78. case 'edit':
  79. if (in_array($_data['action'], array('hold', 'unhold', 'deliver'))) {
  80. if (!is_array($_data['qid'])) {
  81. $qids = array();
  82. $qids[] = $_data['qid'];
  83. }
  84. else {
  85. $qids = $_data['qid'];
  86. }
  87. if (!empty($qids)) {
  88. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => $_data['action'], 'items' => $qids));
  89. process_mailq_output(json_decode($docker_return, true), $_action, $_data);
  90. }
  91. }
  92. if (in_array($_data['action'], array('flush', 'super_delete'))) {
  93. $docker_return = docker('post', 'postfix-mailcow', 'exec', array('cmd' => 'mailq', 'task' => $_data['action']));
  94. process_mailq_output(json_decode($docker_return, true), $_action, $_data);
  95. }
  96. break;
  97. case 'get':
  98. // todo: move get from json_api here
  99. break;
  100. }
  101. }