File: //usr/local/mailchannels/bin/install.php
<?php
namespace MailChannels;
ini_set("error_log", "/var/log/mailchannels/php-error.log");
require_once("/usr/local/mailchannels/boot.php");
App::setLogger(new \Katzgrau\KLogger\Logger("/var/log/mailchannels/"));
$logger = App::getLogger();
$accessHashLocation = "/etc/mailchannels/access_hash";
const CHECKING_WHM_HASH_EXISTS_MESSAGE = "Checking for WHM access hash existence...";
const CREATING_WHM_HASH_MESSAGE = "Creating access hash...";
const WHM_HASH_CREATE_SUCCESS = "Access hash successfully created";
const CREATING_WHM_HASH_ERROR_MESSAGE = "An error occurred while creating the access hash, please check the logs";
const WHM_HASH_EXISTS_MESSAGE = "Access hash exists";
const REGISTERING_HOOKS_MESSAGE = "Registering hooks...";
const COULD_NOT_WRITE_TO_WHM_HAS_FILE_MESSAGE = "Couldn't write access hash to file.";
const EXIM_LOCAL_NOT_EDITABLE_MESSAGE = "Current local exim config file may cause updating MailChannels outbound settings to fail, please see https://mailchannels.zendesk.com/hc/en-us/categories/200009810-Outbound-Filtering for more information";
echo CHECKING_WHM_HASH_EXISTS_MESSAGE . "\n";
echo CREATING_WHM_HASH_MESSAGE . "\n";
try {
createAccessHash($accessHashLocation);
echo WHM_HASH_CREATE_SUCCESS . "\n";
} catch(\Exception $e) {
echo CREATING_WHM_HASH_ERROR_MESSAGE . "\n";
$logger->error($e->getMessage());
exit(1);
}
function createAccessHash($accessHashLocation) {
$tokenName = "mailchannels";
$tokenACLs = ["all"];
$token = null;
try {
$whmapi1 = new WHMAPI1CMDImpl();
$token = $whmapi1->apiTokenCreate($tokenName, $tokenACLs);
} catch (WHMApiBadStatusException $e) {
if (preg_match("/API token with the name .* already exists/", $e->getMessage())) {
$whmapi1->apiTokenRevoke($tokenName);
$token = $whmapi1->apiTokenCreate($tokenName, $tokenACLs);
} else {
throw $e;
}
}
$b64Token = base64_encode($token->getToken());
if (!file_put_contents($accessHashLocation, $b64Token)) {
throw new \Exception(COULD_NOT_WRITE_TO_WHM_HAS_FILE_MESSAGE);
}
chmod($accessHashLocation, 0640);
}
$config = App::getConfig();
// check local exim config file to see if MailChannels outbound settings could be updated
if (file_exists($config::EXIM_CONF_LOCAL) && !$config->isEditableForMCConfig($config::EXIM_CONF_LOCAL) ) {
echo EXIM_LOCAL_NOT_EDITABLE_MESSAGE . "\n";
}
//Register hooks
$hookRegister = new HookRegister($config::HOOKS);
echo REGISTERING_HOOKS_MESSAGE . "\n";
$out = $hookRegister->registerHooks();
if (!empty($out)) {
echo "\n";
foreach ($out as $o) {
echo "$o\n";
}
}