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;
}
}
}