container_ctrl.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. // Block requests by checking the 'Sec-Fetch-Dest' header.
  3. if (isset($_SERVER['HTTP_SEC_FETCH_DEST']) && $_SERVER['HTTP_SEC_FETCH_DEST'] !== 'empty') {
  4. header('HTTP/1.1 403 Forbidden');
  5. exit;
  6. }
  7. require_once $_SERVER['DOCUMENT_ROOT'] . '/inc/prerequisites.inc.php';
  8. if (!isset($_SESSION['mailcow_cc_role']) || $_SESSION['mailcow_cc_role'] != 'admin') {
  9. exit();
  10. }
  11. if (preg_match('/^[a-z\-]{0,}-mailcow/', $_GET['service'])) {
  12. if ($_GET['action'] == "start") {
  13. header('Content-Type: text/html; charset=utf-8');
  14. $retry = 0;
  15. while (docker('info', $_GET['service'])['State']['Running'] != 1 && $retry <= 3) {
  16. $response = docker('post', $_GET['service'], 'start');
  17. $response = json_decode($response, true);
  18. $last_response = ($response['type'] == "success") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response['msg'] . '</span></b>';
  19. if ($response['type'] == "success") {
  20. break;
  21. }
  22. usleep(1500000);
  23. $retry++;
  24. }
  25. echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Already running</span></b>' : $last_response;
  26. }
  27. if ($_GET['action'] == "stop") {
  28. header('Content-Type: text/html; charset=utf-8');
  29. $retry = 0;
  30. while (docker('info', $_GET['service'])['State']['Running'] == 1 && $retry <= 3) {
  31. $response = docker('post', $_GET['service'], 'stop');
  32. $response = json_decode($response, true);
  33. $last_response = ($response['type'] == "success") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response['msg'] . '</span></b>';
  34. if ($response['type'] == "success") {
  35. break;
  36. }
  37. usleep(1500000);
  38. $retry++;
  39. }
  40. echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Not running</span></b>' : $last_response;
  41. }
  42. if ($_GET['action'] == "restart") {
  43. header('Content-Type: text/html; charset=utf-8');
  44. $response = docker('post', $_GET['service'], 'restart');
  45. $response = json_decode($response, true);
  46. $last_response = ($response['type'] == "success") ? '<b><span class="pull-right text-success">OK</span></b>' : '<b><span class="pull-right text-danger">Error: ' . $response['msg'] . '</span></b>';
  47. echo (!isset($last_response)) ? '<b><span class="pull-right text-warning">Cannot restart container</span></b>' : $last_response;
  48. }
  49. if ($_GET['action'] == "logs") {
  50. $lines = (empty($_GET['lines']) || !is_numeric($_GET['lines'])) ? 1000 : $_GET['lines'];
  51. header('Content-Type: text/plain; charset=utf-8');
  52. print_r(preg_split('/\n/', docker('logs', $_GET['service'], $lines)));
  53. }
  54. }
  55. ?>