$logger->info("Filter account if their package contains the nimbusec package extension"); foreach ($acctResult['data']['acct'] as $acct) { $logger->debug("Reading package: {$acct['plan']}, name: {$acct['user']} and email: {$acct['email']}"); foreach ($pkgInfos as $pkg) { if (in_array($acct['plan'], $pkg, true)) { // Find all users except nimbusec array_push($nimbuAccts, array("email" => $acct['email'], "user" => $acct['user'])); } } } $logger->info("Nimbusec specific accounts retrieved"); if (empty($nimbuAccts)) { $logger->warning("No users existing with a package which has nimbusec included"); } // Removing users $provision = new Provision($key, $secret); foreach ($nimbuAccts as $acct) { $logger->debug("Looping through user {$acct['email']} to read & delete the database entries"); $res = $provision->removeUser(array("user" => $acct['user'], "contactemail" => $acct['email']), $logger); if ($res[0]) { $logger->debug("User {$acct['email']}: {$res[1]}"); } else { $logger->warning("Failed to remove {$acct['email']}. Error message : {$res[1]}"); } } $logger->progress("Removed all user and domains from database"); array_push($responseArray['content'], "Removed all user and domains from database"); // ################################## 2.) Remove all parts of nimbusec ################################## $logger->progress("Removing all parts of nimbusec from the system..."); $logger->info("Removing WHM Hooks + cPanel plugin from system"); $disabled = explode(',', ini_get('disable_functions'));
function checkPackage($input = array()) { $logger = new Logger("/usr/local/nimbusec/logs", "changePackage.log", true); $data = $input['data']; $logger->info("Triggered change_package hook"); try { $logger->info("Check bundles"); // Get access data for WHM API $hash = file_get_contents("/root/.accesshash"); $host = gethostname(); $serverAddr = gethostbyname($host); $whmApi = new WHMAPIClient($hash, $serverAddr); $oldPkgName = $data['cur_pkg']; $newPkgName = $data['new_pkg']; $userName = $data['user']; $logger->debug("Get input data [cur/old_pkg] '{$oldPkgName}' [new_pkg] '{$newPkgName}' and [user] '{$userName}'"); $oldPkgRes = $whmApi->sendRequest('getpkginfo', array("pkg" => $oldPkgName)); $newPkgRes = $whmApi->sendRequest('getpkginfo', array("pkg" => $newPkgName)); $logger->info("Read infomation for both packages"); // Isset is much faster than array_key_exists $old_hasNimbusec = isset($oldPkgRes['data']['pkg']['nimbusec_bundles']); $new_hasNimbusec = isset($newPkgRes['data']['pkg']['nimbusec_bundles']); $logger->debug("Old package [has_nimbusec] 'json_encode({$old_hasNimbusec})', new package [has_nimbusec] 'json_encode({$new_hasNimbusec})'"); list($key, $secret) = $whmApi->getNVData(array("NIMBUSEC_APIKEY", "NIMBUSEC_APISECRET")); if (!$old_hasNimbusec && $new_hasNimbusec) { $logger->info("Include nimbusec and provision [user] '{$userName}'"); $accRes = $whmApi->sendRequest('accountsummary', array("user" => $userName)); $logger->info("Retrieve account information of user"); $data = array("user" => $userName, "domain" => $accRes['data']['acct'][0]['domain'], "contactemail" => $accRes['data']['acct'][0]['email'], "nimbusec_bundles" => $newPkgRes['data']['pkg']['nimbusec_bundles']); if (isset($data['contactemail'])) { $logger->debug("Read relevant data for [user] '{$userName}': [domain] => '{$accRes['data']['acct'][0]['domain']}'\n\t\t\t\t, [contactemail] => '{$accRes['data']['acct'][0]['email']}' and [nimbusec_bundles] => '{$newPkgRes['data']['pkg']['nimbusec_bundles']}'"); $logger->info("Provisioning begins.."); $provision = new Provision($key, $secret); $res = $provision->provisionUser($data, $logger); if ($res[0]) { $logger->info($res[1]); } else { $logger->error($res[1]); } $logger->info("Provisioning ends.."); $logger->close(); return array(1, $res[1]); } else { $str = "No email specified. Therefore user can't be provisioned with nimbusec"; $logger->info($str); $logger->close(); return array("1", $str); } } else { if ($old_hasNimbusec && $new_hasNimbusec) { $logger->info("Checking package bundles..."); $oldBundle = $oldPkgRes['data']['pkg']['nimbusec_bundles']; $newBundle = $newPkgRes['data']['pkg']['nimbusec_bundles']; // Check / update bundles if ($oldBundle == $newBundle) { $logger->info("The old package's bundle and the new package's bundle is the same"); $logger->debug("[old_bundle] '{$oldBundle}' == [new_bundle] '{$newBundle}'"); $logger->close(); return array("1", "The old package's bundle and the new package's bundle is the same"); } else { $logger->info("Updating bundle begins.."); $accRes = $whmApi->sendRequest('accountsummary', array("user" => $userName)); $logger->info("Retrieve account information of user"); $email = $accRes['data']['acct'][0]['email']; $logger->debug("Updating {$oldPkgName} with nimbusec bundle '{$oldBundle}' to {$newPkgName} with nimbusec bundle '{$newBundle}' for user {$email}"); $provision = new Provision($key, $secret); $res = $provision->updateBundle($email, $newBundle, $logger); if ($res[0]) { $logger->info($res[1]); } else { $logger->error($res[1]); } $logger->info("Updating bundle ends.."); $logger->close(); return array(1, $res[1]); } } else { if ($old_hasNimbusec && !$new_hasNimbusec) { $logger->info("Removing account begins.."); $accRes = $whmApi->sendRequest('accountsummary', array("user" => $userName)); $logger->info("Retrieve account information of user"); $email = $accRes['data']['acct'][0]['email']; $logger->debug("Removing user {$userName} with email {$email} from database and system"); $provision = new Provision($key, $secret); $res = $provision->removeUser(array("user" => $userName, "contactemail" => $email), $logger); if ($res[0]) { $logger->info($res[1]); } else { $logger->error($res[1]); } $logger->info("Removing user ends.."); $logger->close(); return array(1, $res[1]); } else { if (!$old_hasNimbusec && !$new_hasNimbusec) { $str = "Neither the old package nor the new package have nimbusec included."; $logger->info($str); $logger->close(); return array("1", $str); } } } } $str = "Something unexpected happened in change_package hook. Better check immediately"; $logger->error($str); $logger->close(); return array("1", $str); } catch (CUrlException $exp) { $logger->error("[CUrl SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in change_package hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (NimbusecException $exp) { $logger->error("[Nimbusec SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in change_package hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (WHMException $exp) { $logger->error("[WHM SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in change_package hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (Exception $exp) { $logger->error("[UNSPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in change_package hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } }
function provisioningUser($input = array()) { $logger = new Logger("/usr/local/nimbusec/logs", "provisioning.log", true); $data = $input['data']; $logger->info("Triggered provisioning hook"); $logger->info("Check bundle"); try { // isset is much faster than array_key_exists // http://stackoverflow.com/questions/2473989/list-of-big-o-for-php-functions // At the same time you can save checking whether with empty(), isset() does the same anyway // https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ if (isset($data['contactemail'])) { if (isset($data['nimbusec_bundles'])) { $logger->info("User has nimbusec"); // Get access data for WHM API $hash = file_get_contents("/root/.accesshash"); $host = gethostname(); $serverAddr = gethostbyname($host); $whmApi = new WHMAPIClient($hash, $serverAddr); list($key, $secret) = $whmApi->getNVData(array("NIMBUSEC_APIKEY", "NIMBUSEC_APISECRET")); $logger->debug("Read relevant data for [user] '{$data['user']}': [domain] => '{$data['domain']}'\n\t\t\t\t, [contactemail] => '{$data['contactemail']}' and [nimbusec_bundles] => '{$data['nimbusec_bundles']}'"); $logger->info("Provisioning begins.."); $provision = new Provision($key, $secret); $res = $provision->provisionUser($data, $logger); if ($res[0]) { $logger->info($res[1]); } else { $logger->error($res[1]); } $logger->info("Provisioning ends.."); $logger->close(); return array(1, $res[1]); } else { $str = "User doesn't have nimbusec"; $logger->info($str); $logger->close(); return array("1", $str); } } else { $str = "No email specified. Therefore user can't be provisioned with nimbusec"; $logger->info($str); $logger->close(); return array("1", $str); } $str = "Something unexpected happened in provision hook. Better check immediately"; $logger->error($str); $logger->close(); return array("1", $str); } catch (CUrlException $exp) { $logger->error("[CUrl SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in provision hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (NimbusecException $exp) { $logger->error("[Nimbusec SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in provision hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (WHMException $exp) { $logger->error("[WHM SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in provision hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (Exception $exp) { $logger->error("[UNSPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in provision hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } }
function removeUser($input = array()) { $logger = new Logger("/usr/local/nimbusec/logs", "remove.log", true); $data = $input['data']; $userName = ""; if (array_key_exists('user', $data)) { $userName = $data['user']; } else { $userName = $data['username']; } $logger->info("Triggered removeuser hook"); $logger->debug("Removing user {$userName}"); $logger->info("Check bundle"); try { // Get access data for WHM API $hash = file_get_contents("/root/.accesshash"); $host = gethostname(); $serverAddr = gethostbyname($host); $whmApi = new WHMAPIClient($hash, $serverAddr); $accRes = $whmApi->sendRequest('accountsummary', array("user" => $userName)); $logger->info("Retrieve account information of user"); $pkgRes = $whmApi->sendRequest('getpkginfo', array("pkg" => $accRes['data']['acct'][0]['plan'])); $logger->info("Read infomation for user's package {$accRes['data']['acct'][0]['plan']}"); $hasNimbusec = isset($pkgRes['data']['pkg']['nimbusec_bundles']); $logger->debug("User has nimbusec [has_nimbusec] 'json_encode({$hasNimbusec})'"); list($key, $secret) = $whmApi->getNVData(array("NIMBUSEC_APIKEY", "NIMBUSEC_APISECRET")); if ($hasNimbusec) { $logger->info("User has nimbusec"); $logger->info("Removing begins.."); $provision = new Provision($key, $secret); $res = $provision->removeUser(array("user" => $userName, "contactemail" => $accRes['data']['acct'][0]['email']), $logger); if ($res[0]) { $logger->info($res[1]); } else { $logger->error($res[1]); } $logger->info("Removing ends.."); $logger->close(); return array(1, $res[1]); } else { $str = "User doesn't have nimbusec"; $logger->info($str); $logger->close(); return array("1", $str); } $str = "Something unexpected happened in remove hook. Better check immediately"; $logger->error($str); $logger->close(); return array("1", $str); } catch (CUrlException $exp) { $logger->error("[CUrl SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in remove hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (NimbusecException $exp) { $logger->error("[Nimbusec SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in remove hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (WHMException $exp) { $logger->error("[WHM SPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in remove hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } catch (Exception $exp) { $logger->error("[UNSPECIFIC ERROR] in {$exp->getFile()}: {$exp->getMessage()} at line {$exp->getLine()}"); $logger->info("Processing data in remove hook aborted..."); $logger->close(); return array("1", $exp->getMessage()); } }