AccountController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 ($userExisted != true) {
  79. if ($GLOBALS['debug'] == true) {error_log("Writing PHP-FPM Config for : " . $username); }
  80. $phpContent = file_get_contents("/etc/php/8.2/fpm/user.in");
  81. $phpContent = str_replace("USERNAME", $username, $phpContent);
  82. file_put_contents("/etc/php/8.2/fpm/pool.d/" . $username . ".conf", $phpContent);
  83. if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);}
  84. exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode);
  85. if ($phpReturnCode !== 0) {
  86. error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput));
  87. http_response_code(500);
  88. echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]);
  89. return;
  90. }
  91. }
  92. echo json_encode(['success' => 'User crreation successfully','details' => '']);
  93. }
  94. public static function terminate($data): void {
  95. $username = $data['username'] ?? '';
  96. $domain = $data['domain'] ?? '';
  97. if (empty($username)) {
  98. error_log("terminate: ERROR: No username provided");
  99. http_response_code(400);
  100. error_log(print_r($data,true));
  101. echo json_encode(['error' => 'Missing username']);
  102. return;
  103. }
  104. if( strpos(file_get_contents("/etc/passwd"),$username) == false) {
  105. error_log("terminate: ERROR: User $username does not exist");
  106. http_response_code(400);
  107. error_log(print_r($data,true));
  108. echo json_encode(['error' => 'Unknown user']);
  109. return;
  110. }
  111. // remove PHP-FPM User
  112. if ($GLOBALS['debug'] == true) {error_log("Removing PHP-FPM Config for : " . $username); }
  113. $configName = "/etc/php/8.2/fpm/pool.d/" . $username . ".conf";
  114. exec("sudo /usr/bin/rm -f $configName 2>&1", $userOutput, $userReturnCode);
  115. if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);}
  116. exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode);
  117. if ($phpReturnCode !== 0) {
  118. error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput));
  119. http_response_code(500);
  120. echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]);
  121. return;
  122. }
  123. if(!empty($domain)) {
  124. $configFile = "/etc/apache2/sites-enabled/$domain.conf";
  125. if ($GLOBALS['debug'] == true) { error_log("Remove config of user : " . $username); }
  126. exec("sudo /usr/bin/rm -f $configFile 2>&1", $userOutput, $userReturnCode);
  127. exec("sudo /usr/bin/certbot delete --cert-name $domain --non-interactive 2>&1", $output, $returnCode);
  128. if ($returnCode !== 0) {
  129. error_log("deploy: ERROR: certbot failed to delete certificate on $domain, details => " . implode("\n", $output));
  130. http_response_code(500);
  131. echo json_encode(['error' => 'Certbot failed', 'details' => implode("\n", $output)]);
  132. return;
  133. }
  134. exec('sudo /usr/bin/systemctl reload apache2 2>&1', $apacheOutput, $apacheReturnCode);
  135. if ($GLOBALS['debug'] == true) { error_log("Restarting Apache"); }
  136. if ($apacheReturnCode !== 0) {
  137. error_log("deploy: ERROR: Apache Reload error, details => " . implode("\n", $apacheOutput));
  138. http_response_code(500);
  139. echo json_encode(['error' => 'Failed to reload Apache', 'details' => implode("\n", $apacheOutput)]);
  140. return;
  141. }
  142. }
  143. // Remove user and files
  144. if ($GLOBALS['debug'] == true) {error_log("Remove User: " . $username); }
  145. exec("sudo /usr/sbin/userdel -r -f $username 2>&1", $userOutput, $userReturnCode);
  146. if ($userReturnCode !== 0) {
  147. error_log("deploy: ERROR: Userdel for $username failed, details => " . implode("\n", $userOutput));
  148. http_response_code(500);
  149. echo json_encode(['error' => 'Failed to remove user', 'details' => implode("\n", $userOutput)]);
  150. return;
  151. }
  152. echo json_encode(['success' => 'Removing user ' . $username . ' successfully']);
  153. }
  154. }