File: //usr/local/mailchannels/clients/CPanel/UAPI.php
<?php
namespace MailChannels;
abstract class UAPI {
const DOMAIN_INFO_MODULE = "DomainInfo";
const DNS_MODULE = "DNS";
const VARIABLES_MODULE = "Variables";
const LIST_DOMAINS_FUNCTION = 'list_domains';
const HAS_LOCAL_AUTHORITY_FUNCTION = 'has_local_authority';
const GET_USER_INFORMATION_FUNCTION = 'get_user_information';
/**
* @param $user
* @return array
* @throws UAPIException
*/
public function listDomains($user) {
$domains = array();
if (!$this->doesUserExist($user)) {
App::getLogger()->info("User '$user' does not exist. No domains to list.");
return $domains;
}
try {
$response = $this->api($user, self::DOMAIN_INFO_MODULE, self::LIST_DOMAINS_FUNCTION);
if (!$response || empty($response->result) || empty($response->result->data)) {
App::getLogger()->error("Empty response received from UAPI for user '$user'.");
return $domains;
}
} catch (UAPIException $e) {
App::getLogger()->error("UAPI error for listDomains: " . $e->getMessage());
throw $e;
} catch (\Exception $e) {
App::getLogger()->debug("Error in API call for listDomains: " . $e->getMessage() . "\nTrace: " . $e->getTraceAsString());
throw new UAPIException($user, self::DOMAIN_INFO_MODULE, self::LIST_DOMAINS_FUNCTION, $e->getMessage(), $e->getCode());
}
$data = $response->result->data;
$domainBuilder = (new WHM\DomainBuilder())->setUser($user);
$mainDomain = $data->main_domain;
if (!$mainDomain) {
App::getLogger()->warning("main_domain for user '$user' is empty");
} else {
App::getLogger()->debug("UAPI listDomains main domain: {$mainDomain}");
$domains[] = $domainBuilder->setType(WHM\Domain::TYPE_MAIN)->setDomain($mainDomain)->build();
}
$domainBuilder->setParentDomain($mainDomain)->setType(WHM\Domain::TYPE_ADDON);
foreach ($data->addon_domains as $addon_domain) {
if (!$addon_domain) {
App::getLogger()->warning("found an empty addon domain for user '$user'");
continue;
}
App::getLogger()->debug("UAPI listDomains addon domain: {$addon_domain}");
$domains[] = $domainBuilder->setDomain($addon_domain)->build();
}
$domainBuilder->setType(WHM\Domain::TYPE_PARKED);
foreach ($data->parked_domains as $parked_domain) {
if (!$parked_domain) {
App::getLogger()->warning("found an empty parked domain for user '$user'");
continue;
}
App::getLogger()->debug("UAPI listDomains parked domain: {$parked_domain}");
$domains[] = $domainBuilder->setDomain($parked_domain)->build();
}
if (isset($data->sub_domains)) {
$domainBuilder->setType(WHM\Domain::TYPE_SUB);
foreach ($data->sub_domains as $sub_domain) {
if (!$sub_domain) {
App::getLogger()->warning("found an empty subdomain for user '$user'");
continue;
}
App::getLogger()->debug("UAPI listDomains sub domain: {$sub_domain}");
$domains[] = $domainBuilder->setDomain($sub_domain)->build();
}
} else {
App::getLogger()->info("no subdomains for '$user'");
}
return $domains;
}
/**
* @param string $userName
* @return bool
* @throws UAPIException
*/
public function isDemoAccount($userName) {
try {
$response = $this->api($userName, self::VARIABLES_MODULE, self::GET_USER_INFORMATION_FUNCTION);
} catch (\Exception $e) {
throw new UAPIException($userName, self::VARIABLES_MODULE, self::GET_USER_INFORMATION_FUNCTION, $e->getMessage(), $e->getCode());
}
$result = $response->result->data->demo_mode;
App::getLogger()->debug("UAPI Variables get_user_information for user $userName returns {$result}");
return $result == 1;
}
/**
* @param string $user
* @param \MailChannels\Plugin\Domain $domain
* @return bool
* @throws UAPIException
*/
public function hasLocalAuthority($user, Plugin\Domain $domain) {
$params = [
"domain" => $domain->getDomain()
];
try {
$response = $this->api($user, self::DNS_MODULE, self::HAS_LOCAL_AUTHORITY_FUNCTION, $params);
} catch (\Exception $e) {
throw new UAPIException($user, self::DNS_MODULE, self::HAS_LOCAL_AUTHORITY_FUNCTION, $e->getMessage(), $e->getCode());
}
$result = $response->result->data[0]->local_authority;
App::getLogger()->debug("UAPI dns haslocalauthority {$domain->getDomain()} returns {$result}");
return $result == 1;
}
/**
* Checks if a user exists.
*
* @param string $userName The username to check.
* @return bool True if the user exists, false otherwise.
* @throws UAPIException If there is an error in the API call.
*/
public function doesUserExist($userName) {
try {
$response = $this->api($userName, self::VARIABLES_MODULE, self::GET_USER_INFORMATION_FUNCTION);
$exists = isset($response->result) && isset($response->result->data);
return $exists;
} catch (\Exception $e) {
App::getLogger()->error("Error in API call for user existence check: " . $e->getMessage());
throw new UAPIException($userName, self::VARIABLES_MODULE, self::GET_USER_INFORMATION_FUNCTION, $e->getMessage(), $e->getCode());
}
}
protected abstract function api($user, $module, $function, $params=array());
}