File: //usr/local/mailchannels/App.php
<?php
namespace MailChannels;
use AWS\AwsApiImpl;
use Katzgrau\KLogger\Logger;
use Net_DNS2_Resolver;
final class App {
private static $appRoot;
private static $logger;
private static $outboundFailureLogger;
private static $inputReader;
private static $verifySSL = true;
private static $mustRun = array();
private static $lockFolder;
private static $storage;
private static $inboundAPIBuilder;
private static $awsAPIClient;
private static $uAPI;
private static $resolver;
/**
* @return Config
*/
public static function getConfig() {
$config = new Config::$CONFIG_CLASS();
if (!($config instanceof Config) && !is_subclass_of($config, Config::class)) {
throw new \Exception("Config class must be a subclass of 'Config' ('" . Config::$CONFIG_CLASS . "' is not)");
}
return $config;
}
public static function setAppRoot($appRoot) {
self::$appRoot = $appRoot;
}
public static function appRoot() {
return self::$appRoot;
}
public static function setInputReader(&$inputReader) {
self::$inputReader = $inputReader;
}
public static function getInputReader() {
if (!self::$inputReader) {
self::$inputReader = new InputReaderImpl();
}
return self::$inputReader;
}
public static function setResolver(Net_DNS2_Resolver $resolver) {
self::$resolver = $resolver;
}
/**
* @return Net_DNS2_Resolver
*/
public static function getResolver() {
if (self::$resolver !== null) {
return self::$resolver;
}
return new Net_DNS2_Resolver();
}
public static function showPageNotFound() {
header("HTTP/1.1 404 Internal Server Error");
echo "Page Not Found";
}
public static function showInternalError() {
header("HTTP/1.1 500 Internal Server Error");
echo "Internal Error";
}
public static function setLogger($logger) {
self::$logger = $logger;
}
public static function setOutboundFailureLogger($outboundFailureLogger) {
self::$outboundFailureLogger = $outboundFailureLogger;
}
/**
* @return Logger
*/
public static function getLogger() {
return self::$logger;
}
/**
* @return Logger
*/
public static function getOutboundFailureLogger() {
return self::$outboundFailureLogger;
}
/**
* @return string
* @throws \Exception
*/
public static function getOutboundFailureLogFile() {
$config = App::getConfig();
return $config::LOG_FOLDER . "/" . $config::OUTBOUND_FAILURE_LOG;
}
public static function setVerifySSL($verify) {
self::$verifySSL = $verify;
}
public static function verifySSL() {
return self::$verifySSL;
}
public static function mustRun($key, $func) {
self::$mustRun[$key] = $func;
}
public static function removeMustRun($key) {
if (key_exists($key, self::$mustRun)) {
unset(self::$mustRun[$key]);
}
}
public static function runMustRun() {
foreach(self::$mustRun as $key=>$func) {
try {
$func();
} catch (\Exception $e) {
if (self::$logger) {
self::$logger->error("($key) runMustRun threw an exception:" . $e->getMessage());
}
}
}
}
public static function setLockFolder($folder) {
self::$lockFolder = $folder;
}
public static function getLockFolder() {
if (!self::$lockFolder) {
return self::appRoot() . "/lock";
}
return self::$lockFolder;
}
public static function setStorage(Storage $storage) {
self::$storage = $storage;
}
/**
* @return Storage
* @throws StorageNotSetException
*/
public static function getStorage() {
if (!self::$storage) {
throw new StorageNotSetException("You must set the storage before retrieving it");
}
return self::$storage;
}
public static function setInboundAPIClientBuilder(InboundAPIBuilder $builder=null) {
self::$inboundAPIBuilder = $builder;
}
public static function getAWSAPIClient() {
if (self::$awsAPIClient == null) {
self::$awsAPIClient = new AwsApiImpl(Config::AWS_URL);
}
return self::$awsAPIClient;
}
/**
* @return UAPIJsonImpl
*/
public static function getUAPI() {
if (self::$uAPI !== null) {
return self::$uAPI;
}
$config = App::getConfig();
return new UAPIJsonImpl(self::getWHMHost(), $config::WHM_USERNAME, self::getWHMToken(), $config::WHM_API_VERSION);
}
public static function setUAPI(UAPI $uAPI) {
self::$uAPI = $uAPI;
}
/**
* @return InboundApi | null
*/
public static function getInboundAPIClient() {
if (self::$inboundAPIBuilder) {
return self::$inboundAPIBuilder->build();
}
return null;
}
private static function getWHMToken() {
$config = App::getConfig();
$b64Token = file_get_contents($config::ACCESS_HASH_FILE);
return base64_decode($b64Token);
}
private static function getWHMHost() {
$config = App::getConfig();
$whmHost = $config::WHM_HOST;
if (!$whmHost) {
$whmHost = $_SERVER["HTTP_HOST"];
}
return $whmHost;
}
}