| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace application\controllers;
- class AccountController {
- public static function create($data): void {
- $username = $data['username'];
- $domain = $data['domain'];
- $adminName = $data['admin_name'] ?? '';
- $adminPassword = $data['admin_password'];
-
- if (empty($username) || empty($domain) || empty($adminName) || empty($adminPassword)) {
- error_log("deploy: ERROR: No username, domain, admin_name or admin_password provided");
- http_response_code(400);
- error_log("deploy: ERROR: UserName 1 " . $username);
- error_log("deploy: ERROR: Domain 1 " . $domain);
- error_log("deploy: ERROR: UserName 2 " . $data['username'] );
- error_log("deploy: ERROR: Domain 2 " . $data['domain'] );
- error_log("deploy: ERROR: AdminName " . $adminName);
- error_log("deploy: ERROR: AdminPasswd" . $adminPassword);
- error_log(print_r($data,true));
- echo json_encode(['error' => 'Missing required parameters']);
- return;
- }
- error_log(" Creating user " . $username . " DebugMode: " . $GLOBALS['debug']);
- $userExisted = false;
- if( strpos(file_get_contents("/etc/passwd"),$username) !== false) {
- $userExisted = true;
- }
- if ($userExisted != true) {
- // Create user without login access
- if ($GLOBALS['debug'] == true) {error_log("Adding User: " . $username); }
- exec("sudo /usr/sbin/useradd -m -k -M -s /usr/sbin/nologin $username 2>&1", $userOutput, $userReturnCode);
- if ($userReturnCode !== 0) {
- error_log("deploy: ERROR: Useradd for $username failed, details => " . implode("\n", $userOutput));
- http_response_code(500);
- echo json_encode(['error' => 'Failed to create user', 'details' => implode("\n", $userOutput)]);
- return;
- }
- }
- if ($GLOBALS['debug'] == true) { error_log("Creating logdir for : " . $username); }
- if (is_dir("/home/$username/logs") != true) {
- exec("sudo /usr/bin/mkdir -p /home/$username/logs 2>&1", $mkdirOutput, $mkdirReturnCode);
- if ($mkdirReturnCode !== 0) {
- error_log("deploy: ERROR: Failed to create log directory for $username failed, details => " . implode("\n", $mkdirOutput));
- http_response_code(500);
- echo json_encode(['error' => 'Failed to create logs dir', 'details' => implode("\n", $mkdirOutput)]);
- return;
- }
- }
- if ($GLOBALS['debug'] == true) { error_log("Creating backup dir for : " . $username); }
- if (is_dir("/home/$username/backups") != true) {
- exec("sudo /usr/bin/mkdir -p /home/$username/backups 2>&1", $mkdirOutput, $mkdirReturnCode);
- if ($mkdirReturnCode !== 0) {
- error_log("deploy: ERROR: mkdir /home/$username/backups failed, details => " . implode("\n", $mkdirOutput));
- http_response_code(500);
- echo json_encode(['error' => 'Failed to create backups dir', 'details' => implode("\n", $mkdirOutput)]);
- return;
- }
- }
- if ($GLOBALS['debug'] == true) { error_log("Creating tmpdir for : " . $username); }
- if (is_dir("/home/$username/tmp") != true) {
- exec("sudo /usr/bin/mkdir -p /home/$username/tmp 2>&1", $mkdirOutput, $mkdirReturnCode);
- if ($mkdirReturnCode !== 0) {
- error_log("deploy: ERROR: Failed to create temp directory for $username failed, details => " . implode("\n", $mkdirOutput));
- http_response_code(500);
- echo json_encode(['error' => 'Failed to create tmp dir', 'details' => implode("\n", $mkdirOutput)]);
- return;
- }
- }
- if ($GLOBALS['debug'] == true) { error_log("Chown homedir: " . $username); }
- exec("sudo /usr/bin/chown $username:$username /home/$username -R 2>&1", $chownOutput, $chownReturnCode);
- if ($chownReturnCode !== 0) {
- error_log("deploy: ERROR: chown on /home/$username failed, details => " . implode("\n", $chownOutput));
- http_response_code(500);
- echo json_encode(['error' => 'Failed to chown backups dir', 'details' => implode("\n", $chownOutput)]);
- return;
- }
- // Create PHP-FPM User
- // /etc/php/8.2/fpm/user.in
- if ($GLOBALS['debug'] == true) {error_log("Writing PHP-FPM Config for : " . $username); }
- $phpContent = file_get_contents("/etc/php/8.2/fpm/user.in");
- $phpContent = str_replace("USERNAME", $username, $phpContent);
- file_put_contents("/etc/php/8.2/fpm/pool.d/" . $username . ".conf", $phpContent);
- if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);}
- exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode);
- if ($phpReturnCode !== 0) {
- error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput));
- http_response_code(500);
- echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]);
- return;
- }
- echo json_encode(['success' => 'User creation successfully','details' => '']);
- }
- public static function terminate($data): void {
- $username = $data['username'] ?? '';
- $domain = $data['domain'] ?? '';
- if (empty($username)) {
- error_log("terminate: ERROR: No username provided");
- http_response_code(400);
- error_log(print_r($data,true));
- echo json_encode(['error' => 'Missing username']);
- return;
- }
- if( strpos(file_get_contents("/etc/passwd"),$username) == false) {
- error_log("terminate: ERROR: User $username does not exist");
- http_response_code(400);
- error_log(print_r($data,true));
- echo json_encode(['error' => 'Unknown user']);
- return;
- }
- // remove PHP-FPM User
- if ($GLOBALS['debug'] == true) {error_log("Removing PHP-FPM Config for : " . $username); }
- $configName = "/etc/php/8.2/fpm/pool.d/" . $username . ".conf";
- exec("sudo /usr/bin/rm -f $configName 2>&1", $userOutput, $userReturnCode);
- if ($GLOBALS['debug'] == true) {error_log("Restarting PHP-FPM : " . $username);}
- exec('sudo /usr/bin/systemctl reload php8.2-fpm', $phpOutput, $phpReturnCode);
- if ($phpReturnCode !== 0) {
- error_log("deploy: ERROR: PHP-FPM reload error, details => " . implode("\n", $phpOutput));
- http_response_code(500);
- echo json_encode(['error' => 'Failed to reload PHP-FPM', 'details' => implode("\n", $phpOutput)]);
- return;
- }
- // Remove user and files
- if ($GLOBALS['debug'] == true) {error_log("Remove User: " . $username); }
- exec("sudo /usr/sbin/userdel -r -f $username 2>&1", $userOutput, $userReturnCode);
- if ($userReturnCode !== 0) {
- error_log("deploy: ERROR: Userdel for $username failed, details => " . implode("\n", $userOutput));
- http_response_code(500);
- echo json_encode(['error' => 'Failed to remove user', 'details' => implode("\n", $userOutput)]);
- return;
- }
- echo json_encode(['success' => 'Removing user ' . $username . ' successfully']);
- }
- }
|