AccountController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace application\controllers;
  3. class AccountController {
  4. public static function create($data): void {
  5. $username = $data['username'];
  6. $domain = $data['domain'];
  7. $adminName = $data['admin_name'] ?? '';
  8. $adminPassword = $data['admin_password'];
  9. if (empty($username) || empty($domain) || empty($adminName) || empty($adminPassword)) {
  10. error_log("deploy: ERROR: No username, domain, admin_name or admin_password provided");
  11. http_response_code(400);
  12. error_log("deploy: ERROR: UserName 1 " . $username);
  13. error_log("deploy: ERROR: Domain 1 " . $domain);
  14. error_log("deploy: ERROR: UserName 2 " . $data['username'] );
  15. error_log("deploy: ERROR: Domain 2 " . $data['domain'] );
  16. error_log("deploy: ERROR: AdminName " . $adminName);
  17. error_log("deploy: ERROR: AdminPasswd" . $adminPassword);
  18. error_log(print_r($data,true));
  19. echo json_encode(['error' => 'Missing required parameters']);
  20. return;
  21. }
  22. error_log(" Creating user " . $username . " DebugMode: " . $GLOBALS['debug']);
  23. $userExisted = false;
  24. if( strpos(file_get_contents("/etc/passwd"),$username) !== false) {
  25. $userExisted = true;
  26. }
  27. if ($userExisted != true) {
  28. // Create user without login access
  29. if ($GLOBALS['debug'] == true) {error_log("Adding User: " . $username); }
  30. exec("sudo /usr/sbin/useradd -m -k -M -s /usr/sbin/nologin $username 2>&1", $userOutput, $userReturnCode);
  31. if ($userReturnCode !== 0) {
  32. error_log("deploy: ERROR: Useradd for $username failed, details => " . implode("\n", $userOutput));
  33. http_response_code(500);
  34. echo json_encode(['error' => 'Failed to create user', 'details' => implode("\n", $userOutput)]);
  35. return;
  36. }
  37. }
  38. if ($GLOBALS['debug'] == true) { error_log("Creating logdir for : " . $username); }
  39. if (is_dir("/home/$username/logs") != true) {
  40. exec("sudo /usr/bin/mkdir -p /home/$username/logs 2>&1", $mkdirOutput, $mkdirReturnCode);
  41. if ($mkdirReturnCode !== 0) {
  42. error_log("deploy: ERROR: Failed to create log directory for $username failed, details => " . implode("\n", $mkdirOutput));
  43. http_response_code(500);
  44. echo json_encode(['error' => 'Failed to create logs dir', 'details' => implode("\n", $mkdirOutput)]);
  45. return;
  46. }
  47. }
  48. if ($GLOBALS['debug'] == true) { error_log("Creating backup dir for : " . $username); }
  49. if (is_dir("/home/$username/backups") != true) {
  50. exec("sudo /usr/bin/mkdir -p /home/$username/backups 2>&1", $mkdirOutput, $mkdirReturnCode);
  51. if ($mkdirReturnCode !== 0) {
  52. error_log("deploy: ERROR: mkdir /home/$username/backups failed, details => " . implode("\n", $mkdirOutput));
  53. http_response_code(500);
  54. echo json_encode(['error' => 'Failed to create backups dir', 'details' => implode("\n", $mkdirOutput)]);
  55. return;
  56. }
  57. }
  58. if ($GLOBALS['debug'] == true) { error_log("Creating tmpdir for : " . $username); }
  59. if (is_dir("/home/$username/tmp") != true) {
  60. exec("sudo /usr/bin/mkdir -p /home/$username/tmp 2>&1", $mkdirOutput, $mkdirReturnCode);
  61. if ($mkdirReturnCode !== 0) {
  62. error_log("deploy: ERROR: Failed to create temp directory for $username failed, details => " . implode("\n", $mkdirOutput));
  63. http_response_code(500);
  64. echo json_encode(['error' => 'Failed to create tmp dir', 'details' => implode("\n", $mkdirOutput)]);
  65. return;
  66. }
  67. }
  68. if ($GLOBALS['debug'] == true) { error_log("Chown homedir: " . $username); }
  69. exec("sudo /usr/bin/chown $username:$username /home/$username -R 2>&1", $chownOutput, $chownReturnCode);
  70. if ($chownReturnCode !== 0) {
  71. error_log("deploy: ERROR: chown on /home/$username failed, details => " . implode("\n", $chownOutput));
  72. http_response_code(500);
  73. echo json_encode(['error' => 'Failed to chown backups dir', 'details' => implode("\n", $chownOutput)]);
  74. return;
  75. }
  76. // Create PHP-FPM User
  77. // /etc/php/8.2/fpm/user.in
  78. if ($GLOBALS['debug'] == true) {error_log("Writing PHP-FPM Config for : " . $username); }
  79. $phpContent = file_get_contents("/etc/php/8.2/fpm/user.in");
  80. $phpContent = str_replace("USERNAME", $username, $phpContent);
  81. file_put_contents("/etc/php/8.2/fpm/pool.d/" . $username . ".conf", $phpContent);
  82. if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);}
  83. exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode);
  84. if ($phpReturnCode !== 0) {
  85. error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput));
  86. http_response_code(500);
  87. echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]);
  88. return;
  89. }
  90. echo json_encode(['success' => 'User creation successfully','details' => '']);
  91. }
  92. public static function terminate($data): void {
  93. $username = $data['username'] ?? '';
  94. $domain = $data['domain'] ?? '';
  95. if (empty($username)) {
  96. error_log("terminate: ERROR: No username provided");
  97. http_response_code(400);
  98. error_log(print_r($data,true));
  99. echo json_encode(['error' => 'Missing username']);
  100. return;
  101. }
  102. if( strpos(file_get_contents("/etc/passwd"),$username) == false) {
  103. error_log("terminate: ERROR: User $username does not exist");
  104. http_response_code(400);
  105. error_log(print_r($data,true));
  106. echo json_encode(['error' => 'Unknown user']);
  107. return;
  108. }
  109. // remove PHP-FPM User
  110. if ($GLOBALS['debug'] == true) {error_log("Removing PHP-FPM Config for : " . $username); }
  111. $configName = "/etc/php/8.2/fpm/pool.d/" . $username . ".conf";
  112. exec("sudo /usr/bin/rm -f $configName 2>&1", $userOutput, $userReturnCode);
  113. if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);}
  114. exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode);
  115. if ($phpReturnCode !== 0) {
  116. error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput));
  117. http_response_code(500);
  118. echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]);
  119. return;
  120. }
  121. // Remove user and files
  122. if ($GLOBALS['debug'] == true) {error_log("Remove User: " . $username); }
  123. exec("sudo /usr/sbin/userdel -r -f $username 2>&1", $userOutput, $userReturnCode);
  124. if ($userReturnCode !== 0) {
  125. error_log("deploy: ERROR: Userdel for $username failed, details => " . implode("\n", $userOutput));
  126. http_response_code(500);
  127. echo json_encode(['error' => 'Failed to remove user', 'details' => implode("\n", $userOutput)]);
  128. return;
  129. }
  130. echo json_encode(['success' => 'Removing user ' . $username . ' successfully']);
  131. }
  132. }