Extract messages processing from phone daemons and webhook daemons to place in internal controllers

This commit is contained in:
osaajani 2020-04-02 18:40:39 +02:00
parent 63b68cbeb7
commit 2482a997e7
8 changed files with 293 additions and 203 deletions

Binary file not shown.

View File

@ -69,14 +69,14 @@ namespace controllers\internals;
}
/**
* Analyse a message to check if it's a command and extract it.
* Analyse a message to check if it's a command so execute it
*
* @param int $id_user : User id to search a command for
* @param string $message : Text of the message to analyse
* @param string $message : Message to analyse
*
* @return mixed : false on error, array with new text and command to execute ['updated_text' => string, 'command' => string]
* @return mixed bool|string : false if not a valid command, anonymized message if valid command
*/
public function check_for_command(int $id_user, string $message)
public function analyze_and_process (int $id_user, string $message)
{
$extracted_command = [];
@ -131,10 +131,9 @@ namespace controllers\internals;
$args = $decode_message['args'] ?? '';
$generated_command .= ' ' . escapeshellcmd($args);
return [
'updated_text' => $updated_text,
'command' => $generated_command,
];
exec($generated_command);
return $updated_text;
}
/**

View File

@ -40,9 +40,9 @@ namespace controllers\internals;
* @param string $status : Status of the received message
* @param bool $command : Is the sms a command
*
* @return bool : false on error, new received id else
* @return mixed : false on error, new received id else
*/
public function create(int $id_user, int $id_phone, $at, string $text, string $origin, string $status = 'unread', bool $command = false): bool
public function create(int $id_user, int $id_phone, $at, string $text, string $origin, string $status = 'unread', bool $command = false)
{
$received = [
'id_user' => $id_user,
@ -54,7 +54,7 @@ namespace controllers\internals;
'command' => $command,
];
return (bool) $this->get_model()->insert($received);
return $this->get_model()->insert($received);
}
/**
@ -213,4 +213,59 @@ namespace controllers\internals;
return $this->model;
}
/**
* Receive a SMS message
* @param int $id_user : Id of user to create sended message for
* @param int $id_phone : Id of the phone the message was sent to
* @param $text : Text of the message
* @param string $origin : Number of the sender
* @param string $status : Status of a the sms. By default \models\Received::STATUS_UNREAD
* @return array : [
* bool 'error' => false if success, true else
* ?string 'error_message' => null if success, error message else
* ]
*/
public function receive (int $id_user, int $id_phone, string $text, string $origin, string $status = \models\Received::STATUS_UNREAD) : array
{
$return = [
'error' => false,
'error_message' => null,
];
$at = (new \DateTime())->format('Y-m-d H:i:s');
$is_command = false;
//Process the message to check plus potentially execute command and anonymize text
$internal_command = new Command($this->bdd);
$response = $internal_command->analyze_and_process($id_user, $text);
if ($response !== false) //Received sms is a command an we must use anonymized text
{
$is_command = true;
$text = $response;
}
$received_id = $this->create($id_user, $id_phone, $at, $text, $origin, $status, $is_command);
if (!$received_id)
{
$return['error'] = true;
$return['error_message'] = 'Impossible to insert the sms in database.';
return $return;
}
$received = [
'id' => $received_id,
'at' => $at,
'text' => $text,
'destination' => $id_phone,
'origin' => $origin,
];
$internal_webhook = new Webhook($this->bdd);
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_RECEIVE, $sended);
$internal_user = new User($this->bdd);
$internal_user->transfer_received($id_user, $received);
return $return;
}
}

View File

@ -28,9 +28,9 @@ namespace controllers\internals;
* @param bool $flash : Is the sms a flash
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
*
* @return bool : false on error, new sended id else
* @return mixed : false on error, new sended id else
*/
public function create(int $id_user, int $id_phone, $at, string $text, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = \models\Sended::STATUS_UNKNOWN): bool
public function create(int $id_user, int $id_phone, $at, string $text, string $destination, string $uid, string $adapter, bool $flash = false, ?string $status = \models\Sended::STATUS_UNKNOWN)
{
$sended = [
'id_user' => $id_user,
@ -44,7 +44,7 @@ namespace controllers\internals;
'status' => $status,
];
return (bool) $this->get_model()->insert($sended);
return $this->get_model()->insert($sended);
}
/**
@ -167,4 +167,53 @@ namespace controllers\internals;
return $this->model;
}
/**
* Send a SMS message
* @param \adapters\AdapterInterface $adapter : Adapter object to use to send the message
* @param int $id_user : Id of user to create sended message for
* @param int $id_phone : Id of the phone the message was send with
* @param $text : Text of the message
* @param string $destination : Number of the receiver
* @param bool $flash : Is the sms a flash. By default false.
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
* @return array : [
* bool 'error' => false if success, true else
* ?string 'error_message' => null if success, error message else
* ]
*/
public function send (\adapters\AdapterInterface $adapter, int $id_user, int $id_phone, string $text, string $destination, bool $flash = false, string $status = \models\Sended::STATUS_UNKNOWN) : array
{
$return = [
'error' => false,
'error_message' => null,
];
$at = (new \DateTime())->format('Y-m-d H:i:s');
$response = $adapter->send($destination, $text, $flash);
if ($response['error'])
{
$return['error'] = true;
$return['error_message'] = $response['error_message'];
$status = \models\Sended::STATUS_FAILED;
$this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $status);
return $return;
}
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $status);
$sended = [
'id' => $sended_id,
'at' => $at,
'text' => $text,
'destination' => $destination,
'origin' => $id_phone,
];
$internal_webhook = new Webhook($this->bdd);
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_SEND, $sended);
return $return;
}
}

View File

@ -250,4 +250,50 @@ namespace controllers\internals;
{
return bin2hex(random_bytes(16));
}
/**
* Transfer a received sms to user email
* @param int $id_user : User id
* @param array $received : [
* int 'id' => sms id,
* string 'at' => sms reception date,
* string 'text' => sms content,
* string 'destination' => id of phone the sms was sent to
* string 'origin' => phone number that sent the sms
* ]
*
* @return bool : False if no transfer, true else
*/
public function transfer_received (int $id_user, array $received) :
{
$internal_setting = new Setting($this->bdd);
$settings = $internal_setting->gets_for_user($user_id);
if (!$settings['transfer'] ?? false)
{
return false;
}
$user = $this->get($id_user);
if (!$user)
{
return false;
}
$internal_phone = new Phone($this->bdd);
$phone = $internal_phone->get_for_user($id_user, $destination);
if (!$phone)
{
return false;
}
$mailer = new Mailer();
return $mailer->enqueue($user['email'], EMAIL_TRANSFER_SMS, [
'at' => $received['at'],
'origin' => $received['origin'],
'destination' => $phone['name'],
'text' => $received['text'],
]);
}
}

View File

@ -11,79 +11,125 @@
namespace controllers\internals;
class Webhook extends StandardController
class Webhook extends StandardController
{
protected $bdd;
protected $model;
/**
* Create a new webhook.
*
* @param int $id_user : User id
* @param string $url : Webhook url
* @param string $type : Webhook type
*
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
*/
public function create(int $id_user, string $url, string $type)
{
protected $bdd;
protected $model;
$webhook = [
'id_user' => $id_user,
'url' => $url,
'type' => $type,
];
/**
* Create a new webhook.
*
* @param int $id_user : User id
* @param string $url : Webhook url
* @param string $type : Webhook type
*
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
*/
public function create(int $id_user, string $url, string $type)
$result = $this->get_model()->insert($webhook);
if (!$result)
{
$webhook = [
'id_user' => $id_user,
'url' => $url,
'type' => $type,
return false;
}
return $result;
}
/**
* Update a webhook.
*
* @param int $id_user : User id
* @param int $id : Webhook id
* @param string $url : Webhook url
* @param string $type : Webhook type
*
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
*/
public function update_for_user(int $id_user, int $id, string $url, string $type)
{
$datas = [
'url' => $url,
'type' => $type,
];
return $this->get_model()->update_for_user($id_user, $id, $datas);
}
/**
* Find all webhooks for a user and for a type of webhook.
*
* @param int $id_user : User id
* @param string $type : Webhook type
*
* @return array
*/
public function gets_for_type_and_user(int $id_user, string $type)
{
return $this->get_model()->gets_for_type_and_user($id_user, $type);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Webhook($this->bdd);
return $this->model;
}
/**
* Trigger a webhook and transmit the signal to webhook daemon if needed.
* @param int $user_id : User to trigger the webhook for
* @param string $type : Type of webhook to trigger
* @param array $sms : The sms [
* int 'id' => SMS id,
* string 'at' => SMS date,
* string 'text' => sms body,
* string 'origin' => sms origin (number or phone id)
* string 'destination' => sms destination (number or phone id)
* ]
* @return bool : False if no trigger, true else
*/
private function trigger (int $user_id, string $type, array $sms)
{
$internal_setting = new Setting($this->bdd);
$settings = $internal_setting->gets_for_user($user_id);
if (!$settings['webhook'] ?? false)
{
return false;
}
$webhooks = $this->gets_for_type_and_user($id_user, $type);
foreach ($webhooks as $webhook)
{
$message = [
'url' => $webhook['url'],
'datas' => [
'webhook_type' => $webhook['type'],
'id' => $sms['id'],
'at' => $sms['at'],
'text' => $sms['text'],
'origin' => $sms['origin'],
'destination' => $sms['destination'],
],
];
$result = $this->get_model()->insert($webhook);
if (!$result)
{
return false;
}
return $result;
}
/**
* Update a webhook.
*
* @param int $id_user : User id
* @param int $id : Webhook id
* @param string $url : Webhook url
* @param string $type : Webhook type
*
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
*/
public function update_for_user(int $id_user, int $id, string $url, string $type)
{
$datas = [
'url' => $url,
'type' => $type,
];
return $this->get_model()->update_for_user($id_user, $id, $datas);
}
/**
* Find all webhooks for a user and for a type of webhook.
*
* @param int $id_user : User id
* @param string $type : Webhook type
*
* @return array
*/
public function gets_for_type_and_user(int $id_user, string $type)
{
return $this->get_model()->gets_for_type_and_user($id_user, $type);
}
/**
* Get the model for the Controller.
*
* @return \descartes\Model
*/
protected function get_model(): \descartes\Model
{
$this->model = $this->model ?? new \models\Webhook($this->bdd);
return $this->model;
$error_code = null;
$queue = msg_get_queue(QUEUE_ID_WEBHOOK);
$success = msg_send($queue, QUEUE_TYPE_WEBHOOK, $message, true, true, $error_code);
return (bool) $success;
}
}
}

View File

@ -78,7 +78,6 @@ class Phone extends AbstractDaemon
$this->last_message_at = microtime(true);
$this->msg_queue = msg_get_queue($this->msg_queue_id);
$this->webhook_queue = msg_get_queue(QUEUE_ID_WEBHOOK);
//Instanciate adapter
$adapter_class = $this->phone['adapter'];
@ -106,6 +105,8 @@ class Phone extends AbstractDaemon
*/
private function send_smss()
{
$internal_sended = new \controllers\internals\Sended($this->bdd);
$find_message = true;
while ($find_message)
{
@ -131,37 +132,21 @@ class Phone extends AbstractDaemon
continue;
}
$internal_sended = new \controllers\internals\Sended($this->bdd);
//Update last message time
$this->last_message_at = microtime(true);
$now = new \DateTime();
$at = $now->format('Y-m-d H:i:s');
$message['at'] = $at;
$message['id_phone'] = $this->phone['id'];
//Do message sending
$this->logger->info('Try send message : ' . json_encode($message));
$response = $this->adapter->send($message['destination'], $message['text'], $message['flash']);
$response = $internal_sended->send($this->adapter, $this->phone['id_user'], $this->phone['id'], $message['text'], $message['destination'], $message['flash']);
if ($response['error'])
{
$this->logger->error('Failed send message : ' . json_encode($message) . ' with error : ' . $response['error_message']);
$internal_sended->create($this->phone['id_user'], $this->phone['id'], $at, $message['text'], $message['destination'], $response['uid'] ?? uniqid(), $this->phone['adapter'], $message['flash'], \models\Sended::STATUS_FAILED);
continue;
}
//Run webhook
$internal_setting = new \controllers\internals\Setting($this->bdd);
$user_settings = $internal_setting->gets_for_user($this->phone['id_user']);
$this->process_for_webhook($message, 'send_sms', $user_settings);
$this->logger->info('Successfully send message : ' . json_encode($message));
$internal_sended->create($this->phone['id_user'], $this->phone['id'], $at, $message['text'], $message['destination'], $response['uid'], $this->phone['adapter'], $message['flash']);
}
}
@ -171,7 +156,6 @@ class Phone extends AbstractDaemon
private function read_smss()
{
$internal_received = new \controllers\internals\Received($this->bdd);
$internal_setting = new \controllers\internals\Setting($this->bdd);
if (!$this->adapter->meta_support_read())
{
@ -191,111 +175,20 @@ class Phone extends AbstractDaemon
return true;
}
//Get users settings
$user_settings = $internal_setting->gets_for_user($this->phone['id_user']);
//Process smss
foreach ($response['smss'] as $sms)
{
$this->logger->info('Receive message : ' . json_encode($sms));
$command_result = $this->process_for_command($sms);
$sms['text'] = $command_result['text'];
$is_command = $command_result['is_command'];
$this->process_for_webhook($sms, 'receive_sms', $user_settings);
$this->process_for_transfer($sms, $user_settings);
$internal_received->create($this->phone['id_user'], $this->phone['id'], $sms['at'], $sms['text'], $sms['origin'], 'unread', $is_command);
}
}
/**
* Process a sms to find if its a command and so execute it.
*
* @param array $sms : The sms
*
* @return array : ['text' => new sms text, 'is_command' => bool]
*/
private function process_for_command(array $sms)
{
$internal_command = new \controllers\internals\Command($this->bdd);
$is_command = false;
$command = $internal_command->check_for_command($this->phone['id_user'], $sms['text']);
if ($command)
{
$is_command = true;
$sms['text'] = $command['updated_text'];
exec($command['command']);
}
return ['text' => $sms['text'], 'is_command' => $is_command];
}
/**
* Process a sms to transmit a webhook query to webhook daemon if needed.
*
* @param array $sms : The sms
* @param string $webhook_type : Type of webhook to trigger
* @param array $user_settings : Use settings
*/
private function process_for_webhook(array $sms, string $webhook_type, array $user_settings)
{
if (!$user_settings['webhook'])
{
return false;
}
$internal_webhook = new \controllers\internals\Webhook($this->bdd);
$webhooks = $internal_webhook->gets_for_type_and_user($this->phone['id_user'], $webhook_type);
foreach ($webhooks as $webhook)
{
$message = [
'url' => $webhook['url'],
'datas' => [
'webhook_type' => $webhook['type'],
'at' => $sms['at'],
'text' => $sms['text'],
'origin' => $sms['origin'],
'destination' => $sms['destination'],
],
];
$error_code = null;
$success = msg_send($this->webhook_queue, QUEUE_TYPE_WEBHOOK, $message, true, true, $error_code);
if (!$success)
$response = $internal_received->receive($this->phone['id_user'], $this->phone['id'], $sms['text'], $sms['origin']);
if ($response['error'])
{
$this->logger->critical('Failed send webhook message in queue, error code : ' . $error_code);
$this->logger->error('Failed receive message : ' . json_encode($sms) . ' with error : ' . $response['error_message']);
continue;
}
$this->logger->info('Message received successfully.');
}
}
/**
* Process a sms to transfer it by mail.
*
* @param array $sms : The sms
* @param array $user_settings : Use settings
*/
private function process_for_transfer(array $sms, array $user_settings)
{
if (!$user_settings['transfer'])
{
return false;
}
$internal_user = new \controllers\internals\User($this->bdd);
$user = $internal_user->get($this->phone['id_user']);
if (!$user)
{
return false;
}
$this->logger->info('Transfer sms to ' . $user['email'] . ' : ' . json_encode($sms));
\controllers\internals\Tool::send_email($user['email'], EMAIL_TRANSFER_SMS, ['sms' => $sms]);
}
}

View File

@ -61,6 +61,8 @@ class Webhook extends AbstractDaemon
if (!$success && MSG_ENOMSG !== $error_code)
{
$this->logger->critical('Error for webhook queue reading, error code : ' . $error_code);
$find_message = false;
continue;
}
if (!$message)