2017-11-19 18:27:57 +01:00
|
|
|
#!/usr/bin/php
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
$root_dir_path=realpath(dirname(__FILE__).'/../');
|
|
|
|
|
|
|
|
include $root_dir_path.'/includes/core.php';
|
|
|
|
require_once 'Console/Table.php';
|
|
|
|
|
|
|
|
use GetOptionKit\OptionCollection;
|
|
|
|
use GetOptionKit\ContinuousOptionParser;
|
|
|
|
use GetOptionKit\OptionPrinter\ConsoleOptionPrinter;
|
|
|
|
|
|
|
|
$specs = new OptionCollection;
|
|
|
|
$specs->add('done', 'Show done queue.' );
|
2017-11-19 20:01:49 +01:00
|
|
|
$specs->add('incoming', 'Show incoming queue.' );
|
|
|
|
$specs->add('incoming-done', 'Show incomingi done queue.' );
|
2017-11-19 18:27:57 +01:00
|
|
|
$specs->add('h|help', 'Show this help message.' );
|
|
|
|
$specs->add('v|verbose', 'Enable verbose mode.' );
|
|
|
|
$specs->add('d|debug', 'Enable debug mode.' );
|
|
|
|
|
|
|
|
// Subcommands
|
|
|
|
$subcommands = array(
|
|
|
|
'send' => array (
|
|
|
|
'desc' => "Send a SMS",
|
|
|
|
'args' => array(
|
|
|
|
'number' => 'check_phone_number',
|
|
|
|
'text' => 'check_sms_text'
|
|
|
|
),
|
|
|
|
),
|
|
|
|
'cron' => array (
|
|
|
|
'desc' => "Execute cron mode",
|
|
|
|
'args' => array(),
|
|
|
|
),
|
|
|
|
'check-gateway' => array (
|
|
|
|
'desc' => "Check SMS gateway is alive",
|
|
|
|
'args' => array(),
|
|
|
|
),
|
|
|
|
'info' => array (
|
|
|
|
'desc' => "Get information on SMS message",
|
|
|
|
'args' => array('uuid' => 'check_uuid'),
|
|
|
|
),
|
|
|
|
'process' => array (
|
|
|
|
'desc' => "Process SMS pending message",
|
|
|
|
'args' => array('uuid' => 'check_uuid'),
|
|
|
|
),
|
|
|
|
'delete' => array (
|
|
|
|
'desc' =>"Delete SMS message",
|
|
|
|
'args' => array('uuid' => 'check_uuid'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
// Subcommands specs
|
|
|
|
$subcommandSpecs = array();
|
|
|
|
foreach ($subcommands as $subcmd => $infos) {
|
|
|
|
$subcommandSpecs[$subcmd] = new OptionCollection;
|
|
|
|
$subcommandSpecs[$subcmd] -> add('h|help', 'Show this help message.' );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Additionnals subcommands options
|
|
|
|
$subcommandSpecs['info'] -> add('show-frags', "Show fragments infos");
|
|
|
|
|
|
|
|
// Format help usage method
|
|
|
|
function usage($subcmd=false, $error=false) {
|
|
|
|
global $argv, $specs, $subcommandSpecs, $subcommands;
|
|
|
|
|
|
|
|
if ($error)
|
|
|
|
echo "$error\n\n";
|
|
|
|
|
|
|
|
$printer = new ConsoleOptionPrinter();
|
|
|
|
if ($subcmd) {
|
|
|
|
echo "Usage : ".$argv[0]."[-v|-d] $subcmd [options]";
|
|
|
|
if (isset($subcommands[$subcmd]) && is_array($subcommands[$subcmd]['args'])) {
|
|
|
|
foreach($subcommands[$subcmd]['args'] as $arg => $check_func) {
|
|
|
|
echo " [$arg]";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
echo "\n".$printer->render($subcommandSpecs[$subcmd]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
echo "Usage : ".$argv[0]."[options] [subcommand] [subcommand options] [subcommand args]\n".$printer->render($specs);
|
|
|
|
echo "\nSubcommands :\n";
|
|
|
|
foreach($subcommands as $subcmd => $infos) {
|
|
|
|
echo " - $subcmd : ".$infos['desc']."\n";
|
|
|
|
}
|
|
|
|
echo "\n Use ".$argv[0]." [subcommand] --help to get more information about subcommand\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($error)
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Parse application and subcommand options
|
|
|
|
$subcommandOptions = array();
|
|
|
|
$subcommandArgs = array();
|
|
|
|
$args = array();
|
|
|
|
try {
|
|
|
|
// parse application options first
|
|
|
|
$parser = new ContinuousOptionParser( $specs );
|
|
|
|
$app_options = $parser->parse( $argv );
|
|
|
|
|
|
|
|
// parse subcommands options
|
|
|
|
$subcommand = false;
|
|
|
|
while (! $parser->isEnd()) {
|
|
|
|
$arg = $parser->getCurrentArgument();
|
|
|
|
if (array_key_exists($arg, $subcommandSpecs)) {
|
|
|
|
$subcommand = $arg;
|
|
|
|
$parser->advance();
|
|
|
|
$parser->setSpecs( $subcommandSpecs[$subcommand] );
|
|
|
|
$subcommandArgs[$subcommand] = array();
|
|
|
|
$subcommandOptions[ $subcommand ] = $parser->continueParse();
|
|
|
|
} else {
|
|
|
|
if ($subcommand) {
|
|
|
|
$subcommandArgs[$subcommand][] = $parser->advance();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$args[] = $parser->advance();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch( Exception $e ) {
|
|
|
|
echo $e->getMessage()."\n\n";
|
|
|
|
usage();
|
|
|
|
if ($subcommand)
|
|
|
|
usage($subcommand);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set main help message and log level
|
|
|
|
if (array_key_exists('help', $app_options->keys)) {
|
|
|
|
usage();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
elseif (array_key_exists('debug', $app_options->keys)) {
|
|
|
|
$log_level='DEBUG';
|
|
|
|
echo "App options :\n";
|
|
|
|
var_dump($app_options);
|
|
|
|
echo "Subcommands options :\n";
|
|
|
|
var_dump($subcommandOptions);
|
|
|
|
echo "Subcommands args :\n";
|
|
|
|
var_dump($subcommandArgs);
|
|
|
|
echo "Arguments :\n";
|
|
|
|
var_dump($args);
|
|
|
|
}
|
|
|
|
elseif (array_key_exists('verbose', $app_options->keys)) {
|
|
|
|
$log_level='INFO';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle subcommands
|
|
|
|
if (!empty($subcommandOptions)) {
|
|
|
|
foreach($subcommandOptions as $subcmd => $subopts) {
|
|
|
|
if (array_key_exists('help', $subopts->keys)) {
|
|
|
|
usage($subcmd);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (count($subcommands[$subcmd]['args']) != count($subcommandArgs[$subcmd])) {
|
|
|
|
usage($subcmd, "Invalid number of argument.");
|
|
|
|
}
|
|
|
|
$idx=0;
|
|
|
|
foreach($subcommands[$subcmd]['args'] as $arg => $check_func) {
|
|
|
|
if (!call_user_func($check_func, $subcommandArgs[$subcmd][$idx])) {
|
|
|
|
usage($subcmd, "Invalid $arg value");
|
|
|
|
}
|
|
|
|
$idx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch($subcmd) {
|
|
|
|
case 'send':
|
|
|
|
$result = send_sms($subcommandArgs[$subcmd][0], $subcommandArgs[$subcmd][1]);
|
|
|
|
if ($result[0] == 'error') {
|
|
|
|
echo "Error sending sms : ".$result[1]."\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
echo "Your SMS has been registred as UUID ".$result[1].". Status : ".$result[0]."\n";
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'cron':
|
|
|
|
logging('DEBUG', 'Execute cron mode');
|
|
|
|
logging('DEBUG', 'Handle outgoing pending messages');
|
|
|
|
if (handle_outgoing_msgs() !== True)
|
|
|
|
logging('ERROR', 'Error handling outgoing pending messages');
|
2017-11-19 20:01:49 +01:00
|
|
|
logging('DEBUG', 'Handle incoming pending messages');
|
|
|
|
if (handle_incoming_msgs() !== True)
|
|
|
|
logging('ERROR', 'Error handling incoming pending messages');
|
2017-11-19 18:27:57 +01:00
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'check-gateway':
|
|
|
|
if ($smsgw->check_gateway()) {
|
|
|
|
echo "SMS gateway is alive.\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
echo "SMS gateway not responding.\n";
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
case 'info':
|
|
|
|
$uuid=$subcommandArgs[$subcmd][0];
|
|
|
|
$msg=get_outgoing_msg($uuid);
|
|
|
|
if (!$msg) {
|
2017-11-19 20:01:49 +01:00
|
|
|
$msg=get_incoming_msg($uuid);
|
|
|
|
if (!$msg) {
|
|
|
|
echo "Message $uuid not found.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
2017-11-19 18:27:57 +01:00
|
|
|
}
|
|
|
|
echo "Message $uuid :\n";
|
|
|
|
echo "----------------------------------------------\n";
|
|
|
|
echo "Date : ".$msg['datetime']."\n";
|
|
|
|
echo "Recipient : ".$msg['number']."\n";
|
|
|
|
echo "Status : ".$msg['status']."\n";
|
|
|
|
if ($msg['uniqueid'])
|
|
|
|
echo "Unique SMS gateway message ID : ".$msg['uniqueid']."\n";
|
|
|
|
if ($msg['nbfrag'])
|
|
|
|
echo "Number of fragment(s) : ".$msg['nbfrag']."\n";
|
2017-11-19 20:01:49 +01:00
|
|
|
if ($msg['delivered_datetime'] && $msg['delivered_datetime']!='0000-00-00 00:00:00')
|
|
|
|
echo "Delivered at : ".$msg['delivered_datetime']."\n";
|
2017-11-19 18:27:57 +01:00
|
|
|
echo "Last update : ".$msg['lastupdate']."\n";
|
|
|
|
echo "\nText :\n------\n";
|
|
|
|
echo preg_replace('/(^|\n)/',"\n ",wordwrap($msg['text'], 70, "\n", true));
|
|
|
|
echo "\n\n";
|
|
|
|
if ($msg['uniqueid'] && array_key_exists('show-frags', $subopts->keys)) {
|
|
|
|
$frags = get_outgoing_msg_frags($msg['uniqueid']);
|
|
|
|
if (is_array($frags)) {
|
|
|
|
echo "Fragments :\n";
|
|
|
|
echo "-----------\n";
|
|
|
|
foreach($frags as $frag) {
|
|
|
|
echo " Fragment ".$frag['fragid']." : ".$frag['status']."\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
echo " Error getting message's fragments infos\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exit(0);
|
|
|
|
break;
|
|
|
|
case 'process':
|
|
|
|
$uuid=$subcommandArgs[$subcmd][0];
|
|
|
|
$msg=get_outgoing_msg($uuid);
|
|
|
|
if (!$msg) {
|
2017-11-19 20:01:49 +01:00
|
|
|
$msg=get_incoming_msg($uuid);
|
|
|
|
if (!$msg) {
|
|
|
|
echo "Message $uuid not found.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (handle_incoming_msg($msg)===True) {
|
|
|
|
echo "Incoming SMS forwarded\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
echo "An error occured forwarding incoming SMS. Please see log file for more informations.\n";
|
2017-11-19 18:27:57 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-11-19 20:01:49 +01:00
|
|
|
else {
|
|
|
|
if (handle_outgoing_msg($msg)===True) {
|
|
|
|
echo "Message push on SMS gateway\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
echo "An error occured sending message on SMS gateway. Please see log file for more informations.\n";
|
|
|
|
exit(1);
|
2017-11-19 18:27:57 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'delete':
|
|
|
|
$uuid=$subcommandArgs[$subcmd][0];
|
|
|
|
$msg=get_outgoing_msg($uuid);
|
|
|
|
if (!$msg) {
|
|
|
|
echo "Message $uuid not found.\n";
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (delete_outgoing_msg($msg)===True) {
|
|
|
|
echo "Message deleted.\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
echo "An error occured deleting message. Please see log file for more informations.\n";
|
|
|
|
exit(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Main command
|
|
|
|
|
|
|
|
// Format messages queue output
|
2017-11-19 20:01:49 +01:00
|
|
|
function show_queue($q, $done=false) {
|
2017-11-19 18:27:57 +01:00
|
|
|
$tbl = new Console_Table();
|
|
|
|
$tbl->setHeaders(
|
2017-11-19 20:01:49 +01:00
|
|
|
array(
|
|
|
|
'Id',
|
|
|
|
'Date',
|
|
|
|
'Number',
|
|
|
|
($done?'Delivered date':'Status'),
|
|
|
|
'Text'
|
|
|
|
)
|
2017-11-19 18:27:57 +01:00
|
|
|
);
|
|
|
|
foreach($q as $msg) {
|
|
|
|
$tbl->addRow(
|
|
|
|
array(
|
|
|
|
$msg['uuid'],
|
|
|
|
$msg['datetime'],
|
|
|
|
$msg['number'],
|
2017-11-19 20:01:49 +01:00
|
|
|
($done?$msg['delivered_datetime']:$msg['status']),
|
2017-11-19 18:27:57 +01:00
|
|
|
(strlen($msg['text'])>40?substr($msg['text'],0,37)."...":$msg['text'])
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
echo $tbl->getTable();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('done', $app_options->keys)) {
|
|
|
|
$q = get_outgoing_msgs('delivered');
|
|
|
|
if (!is_array($q))
|
|
|
|
die("Invalid get_outgoing_msg() result !\n");
|
|
|
|
if (empty($q)) {
|
|
|
|
echo "SMS done queue is empty.\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
2017-11-19 20:01:49 +01:00
|
|
|
show_queue($q, true);
|
|
|
|
echo count($q)." message(s) in done queue\n";
|
|
|
|
}
|
|
|
|
elseif (array_key_exists('incoming', $app_options->keys)) {
|
|
|
|
$q = get_incoming_msgs('pending');
|
|
|
|
if (!is_array($q))
|
|
|
|
die("Invalid get_incoming_msgs() result !\n");
|
|
|
|
if (empty($q)) {
|
|
|
|
echo "SMS incoming queue is empty.\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
2017-11-19 18:27:57 +01:00
|
|
|
show_queue($q);
|
2017-11-19 20:01:49 +01:00
|
|
|
echo count($q)." message(s) in queue\n";
|
|
|
|
}
|
|
|
|
elseif (array_key_exists('incoming-done', $app_options->keys)) {
|
|
|
|
$q = get_incoming_msgs('delivered');
|
|
|
|
if (!is_array($q))
|
|
|
|
die("Invalid get_incoming_msgs() result !\n");
|
|
|
|
if (empty($q)) {
|
|
|
|
echo "SMS incoming done queue is empty.\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
show_queue($q, true);
|
2017-11-19 18:27:57 +01:00
|
|
|
echo count($q)." message(s) in done queue\n";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$q = get_smsq();
|
|
|
|
if (!is_array($q))
|
|
|
|
die("Invalid get_smsq() result !\n");
|
|
|
|
if (empty($q)) {
|
|
|
|
echo "SMS queue is empty.\n";
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
show_queue($q);
|
|
|
|
echo count($q)." message(s) in queue\n";
|
|
|
|
}
|