File: //usr/local/mailchannels/bin/runHook.php
#!/usr/local/cpanel/3rdparty/bin/php -q
<?php
namespace MailChannels;
require_once(dirname(__FILE__) . '/../boot.php');
Config::$CONFIG_CLASS = 'MailChannels\HookConfig';
require_once(App::appRoot() . '/load.php');
$args = (count($argv) > 1) ? $argv : array();
if(sizeof($args) > 1) {
$class = $argv[1];
$hook = new $class;
if (is_subclass_of($hook, Hook::class)) {
try {
list($resultResult, $resultMessage) = $hook->execute(get_passed_data());
// $resultResult should be a boolean. we want to echo the integer representation
$resultInt = (int) $resultResult;
echo "$resultInt $resultMessage";
} catch (\Exception $e) {
$message = $e->getTraceAsString();
echo "0 $message";
}
} else {
echo "0 $hook must extend class Hook";
}
}
function get_passed_data() {
// Get input from STDIN.
$raw_data = null;
$stdin_fh = fopen('php://stdin', 'r');
if ( is_resource($stdin_fh) ) {
stream_set_blocking($stdin_fh, 0);
while ( ($line = fgets( $stdin_fh, 1024 )) !== false ) {
$raw_data .= trim($line);
}
fclose($stdin_fh);
}
// Process and JSON-decode the raw output.
if ($raw_data) {
$input_data = json_decode($raw_data, true);
} else {
$input_data = array('context'=>array(),'data'=>array(), 'hook'=>array());
}
// Return the output.
return $input_data;
}
?>