File: //usr/local/mailchannels/hooks/Hook.php
<?php
namespace MailChannels;
require_once(dirname(__FILE__) . '/HookDescribable.php');
abstract class Hook implements HookDescribable {
protected $inboundSmtpService;
protected $outboundSMTPService;
protected $inboundConfig;
protected $outboundConfig;
/**
* @throws StorageNotSetException
*/
public function __construct() {
$this->inboundSmtpService = $this->getInboundSMTPService();
$this->outboundSMTPService = $this->getOutboundSMTPService();
$this->inboundConfig = App::getStorage()->getInboundConfig();
$this->outboundConfig = App::getStorage()->getOutboundConfig();
}
/**
* @return bool
*/
protected function inboundEnabled() {
if (!$this->inboundSmtpService) {
return false;
}
try {
$this->inboundSmtpService->validateServiceUsage();
return true;
} catch (InboundSMTPServiceNotConfiguredException $e) {
return false;
}
}
/**
* @return bool
* @throws OutboundConfigurationException
*/
protected function outboundEnabled() {
return $this->outboundSMTPService && $this->outboundSMTPService->isMailServerConfigured();
}
/**
* @return InboundSMTPService
*/
protected function getInboundSMTPService() {
return ServiceSelector::getService(InboundSMTPService::class);
}
/**
* @return OutboundSMTPService
*/
protected function getOutboundSMTPService() {
return ServiceSelector::getService(OutboundSMTPService::class);
}
/**
* @param $hookData
* @return array
*/
public abstract function execute($hookData);
/**
* @param $hookData
* @return boolean
*/
public function isSuccessfulCpanelResult($hookData) {
return isset($hookData['data'])
&& isset($hookData['data']['output'])
&& count($hookData['data']['output']) > 0
&& isset($hookData['data']['output'][0]['result'])
&& $hookData['data']['output'][0]['result'] == 1;
}
/**
* @param $user string the suspended account; a valid username on the server.
* @return array the first element in the array is a boolean representing if the function succeeded or not.
* the second element in the array is a string message describing the result of the function
*/
protected function whostMgrAccountsDeProvision($user) {
$isInboundEnabled = $this->inboundEnabled();
if ($isInboundEnabled) {
$autoDomainDeProvisioning = $this->inboundConfig->autoDomainDeProvisioning();
if ($autoDomainDeProvisioning) {
try {
$domains = $this->outboundSMTPService->getAllDomainsForUser($user);
$result = true;
$message = "";
foreach ($domains as $domain) {
$domainName = $domain->getDomain();
try {
$deprovisionResult = $this->inboundSmtpService->deProvisionDomain($domainName);
if ($deprovisionResult) {
$message .= " domain '$domainName' no longer uses MailChannels;";
} else {
$message .= " action not taken; domain '$domainName' not found;";
}
} catch (InboundSMTPServiceInternalErrorException $e) {
$errorMessage = $e->getMessage() . ": " . $e->getTraceAsString();
$message .= " unexpected error deprovisioning domain '$domainName': $errorMessage;";
if( !is_a($e->getPrevious(), get_class(new InboundApiUnprocessableException())) ) {
$result = false;
}
} catch (\Exception $e) {
$errorMessage = $e->getMessage() . ": " . $e->getTraceAsString();
$message .= " unexpected error deprovisioning domain '$domainName': $errorMessage;";
$result = false;
}
}
return array($result, $message);
} catch (\Exception $e) {
$errorMessage = $e->getTraceAsString();
$message = "unexpected error getting domains for user '$user' (deprovisioning): $errorMessage";
return array(false, $message);
}
}
$isAutoProtectionEnabled = var_export($autoDomainDeProvisioning, true);
return array(true, "action not taken; autoDomainDeProvisioning: $isAutoProtectionEnabled");
}
return array(true, "action not taken; inbound not enabled");
}
}