2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-06 20:34:26 +01:00
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-11-06 20:34:26 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-11-06 20:34:26 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This source file is subject to the GPL-3.0 license that is bundled
|
2019-11-06 20:34:26 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
namespace controllers\internals;
|
2019-10-29 18:36:25 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
class Received extends StandardController
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
protected $model;
|
2019-11-12 17:37:20 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return the list of unread messages for a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
2019-12-02 01:55:06 +01:00
|
|
|
* @param ?int $nb_entry : Number of entry to return
|
|
|
|
* @param ?int $page : Pagination, used to calcul offset, $nb_entry * $page
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-12-02 01:55:06 +01:00
|
|
|
* @return array : Entrys list
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function list_unread_for_user(int $id_user, ?int $nb_entry = null, ?int $page = null)
|
2019-12-02 01:55:06 +01:00
|
|
|
{
|
|
|
|
return $this->get_model()->list_unread_for_user($id_user, $nb_entry, $nb_entry * $page);
|
|
|
|
}
|
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Create a received.
|
|
|
|
*
|
2020-03-05 23:07:07 +01:00
|
|
|
* @param $id_user : Id of user to create received for
|
2020-03-31 01:53:07 +02:00
|
|
|
* @param int $id_phone : Id of the number the message was send with
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param $at : Reception date
|
|
|
|
* @param $text : Text of the message
|
2020-06-23 21:06:13 +02:00
|
|
|
* @param string $origin : Number of the sender
|
|
|
|
* @param string $status : Status of the received message
|
|
|
|
* @param bool $command : Is the sms a command
|
2021-03-19 02:45:12 +01:00
|
|
|
* @param bool $mms : Is the sms a mms
|
|
|
|
* @param array $media_ids : Ids of the medias to link to received
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-04-02 18:40:39 +02:00
|
|
|
* @return mixed : false on error, new received id else
|
2019-11-14 02:02:50 +01:00
|
|
|
*/
|
2021-03-19 02:45:12 +01:00
|
|
|
public function create(int $id_user, int $id_phone, $at, string $text, string $origin, string $status = 'unread', bool $command = false, bool $mms = false, array $media_ids = [])
|
2019-11-14 02:02:50 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$received = [
|
2020-03-05 23:07:07 +01:00
|
|
|
'id_user' => $id_user,
|
2020-03-31 01:53:07 +02:00
|
|
|
'id_phone' => $id_phone,
|
2019-11-14 02:02:50 +01:00
|
|
|
'at' => $at,
|
2020-01-17 18:19:25 +01:00
|
|
|
'text' => $text,
|
2019-11-14 02:02:50 +01:00
|
|
|
'origin' => $origin,
|
2019-12-02 01:55:06 +01:00
|
|
|
'status' => $status,
|
2019-11-14 02:02:50 +01:00
|
|
|
'command' => $command,
|
2021-03-19 02:45:12 +01:00
|
|
|
'mms' => $mms,
|
2019-11-14 02:02:50 +01:00
|
|
|
];
|
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
//use a transaction to ensure received and media links are created at the same time
|
|
|
|
$this->bdd->beginTransaction();
|
|
|
|
|
|
|
|
$id_received = $this->get_model()->insert($received);
|
|
|
|
if (!$id_received)
|
|
|
|
{
|
|
|
|
$this->bdd->rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Link medias
|
|
|
|
$internal_media = new Media($this->bdd);
|
|
|
|
foreach ($media_ids as $media_id)
|
|
|
|
{
|
|
|
|
$id_media_received = $internal_media->link_to($media_id, 'received', $id_received);
|
|
|
|
if (!$id_media_received)
|
|
|
|
{
|
|
|
|
$this->bdd->rollBack();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//All ok, commit
|
|
|
|
$success = $this->bdd->commit();
|
|
|
|
if (!$success)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $id_received;
|
2019-11-14 02:02:50 +01:00
|
|
|
}
|
2019-11-12 17:37:20 +01:00
|
|
|
|
2019-12-02 01:55:06 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a received message for a user to mark the message as read.
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
2019-12-02 01:55:06 +01:00
|
|
|
* @param int $id_received : received id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-12-02 01:55:06 +01:00
|
|
|
* @return bool : false on error, true on success
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function mark_as_read_for_user(int $id_user, int $id_received): bool
|
2019-12-02 01:55:06 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$received = [
|
2019-12-02 01:55:06 +01:00
|
|
|
'status' => 'read',
|
|
|
|
];
|
|
|
|
|
|
|
|
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-02 01:55:06 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a received message for a user to mark the message as unread.
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
2019-12-02 01:55:06 +01:00
|
|
|
* @param int $id_received : received id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-12-02 01:55:06 +01:00
|
|
|
* @return bool : false on error, true on success
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function mark_as_unread_for_user(int $id_user, int $id_received): bool
|
2019-12-02 01:55:06 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$received = [
|
2019-12-02 01:55:06 +01:00
|
|
|
'status' => 'unread',
|
|
|
|
];
|
|
|
|
|
|
|
|
return (bool) $this->get_model()->update_for_user($id_user, $id_received, $received);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2019-12-02 01:55:06 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return number of unread messages for a user.
|
|
|
|
*
|
2019-12-02 01:55:06 +01:00
|
|
|
* @param int $id_user : User id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2019-12-02 01:55:06 +01:00
|
|
|
*/
|
|
|
|
public function count_unread_for_user(int $id_user)
|
|
|
|
{
|
|
|
|
return $this->get_model()->count_unread_for_user($id_user);
|
|
|
|
}
|
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return x last receiveds message for a user, order by date.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $nb_entry : Number of receiveds messages to return
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2019-11-14 02:02:50 +01:00
|
|
|
*/
|
|
|
|
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
|
|
|
|
{
|
|
|
|
return $this->get_model()->get_lasts_by_date_for_user($id_user, $nb_entry);
|
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return receiveds for an origin and a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param string $origin : Number who sent the message
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-11-15 06:30:23 +01:00
|
|
|
public function gets_by_origin_and_user(int $id_user, string $origin)
|
2019-11-14 02:02:50 +01:00
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
return $this->get_model()->gets_by_origin_and_user($id_user, $origin);
|
2019-11-14 02:02:50 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2021-04-13 02:01:03 +02:00
|
|
|
/**
|
|
|
|
* Return receiveds for an origin and a user since a date.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param string $since : Date we want messages since format Y-m-d H:i:s
|
|
|
|
* @param string $origin : Number who sent the message
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function gets_since_date_by_origin_and_user(int $id_user, string $since, string $origin)
|
|
|
|
{
|
|
|
|
return $this->get_model()->gets_since_date_by_origin_and_user($id_user, $since, $origin);
|
|
|
|
}
|
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Get number of sended SMS for every date since a date for a specific user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param \DateTime $date : Date since which we want the messages
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function count_by_day_since_for_user(int $id_user, $date)
|
|
|
|
{
|
2019-11-14 22:33:00 +01:00
|
|
|
$counts_by_day = $this->get_model()->count_by_day_since_for_user($id_user, $date);
|
|
|
|
$return = [];
|
|
|
|
|
|
|
|
foreach ($counts_by_day as $count_by_day)
|
|
|
|
{
|
|
|
|
$return[$count_by_day['at_ymd']] = $count_by_day['nb'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
2019-11-14 02:02:50 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return all discussions (ie : numbers we have a message received from or sended to) for a user.
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $id_user : User id
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_discussions_for_user(int $id_user)
|
|
|
|
{
|
|
|
|
return $this->get_model()->get_discussions_for_user($id_user);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Get SMS received since a date for a user.
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $id_user : User id
|
2019-11-14 22:33:00 +01:00
|
|
|
* @param $date : La date depuis laquelle on veux les SMS (au format 2014-10-25 20:10:05)
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return array : Tableau avec tous les SMS depuis la date
|
|
|
|
*/
|
|
|
|
public function get_since_by_date_for_user(int $id_user, $date)
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
return $this->get_model()->get_since_by_date_for_user($id_user, $date);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Find messages received since a date for a certain origin and user.
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param $date : Date we want messages sinces
|
|
|
|
* @param string $origin : Origin number
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_since_by_date_for_origin_and_user(int $id_user, $date, string $origin)
|
|
|
|
{
|
|
|
|
return $this->get_model()->get_since_by_date_for_origin_and_user($id_user, $date, $origin);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-11 17:27:07 +01:00
|
|
|
/**
|
2020-03-31 01:53:07 +02:00
|
|
|
* Find last received message for an origin and user.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param string $origin : Origin number
|
|
|
|
*
|
2020-01-11 17:27:07 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function get_last_for_origin_and_user(int $id_user, string $origin)
|
2020-01-11 17:27:07 +01:00
|
|
|
{
|
|
|
|
return $this->get_model()->get_last_for_origin_and_user($id_user, $origin);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
/**
|
2020-06-23 21:06:13 +02:00
|
|
|
* Receive a SMS message.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-06-23 21:06:13 +02:00
|
|
|
* @param int $id_user : Id of user to create sended message for
|
2020-04-02 18:40:39 +02:00
|
|
|
* @param int $id_phone : Id of the phone the message was sent to
|
|
|
|
* @param $text : Text of the message
|
2020-06-23 21:06:13 +02:00
|
|
|
* @param string $origin : Number of the sender
|
|
|
|
* @param ?string $at : Message reception date, if null use current date
|
|
|
|
* @param string $status : Status of a the sms. By default \models\Received::STATUS_UNREAD
|
2021-03-19 02:45:12 +01:00
|
|
|
* @param bool $mms : Is the sms a mms
|
2021-03-26 23:32:29 +01:00
|
|
|
* @param array $medias : Empty array if no medias, or medias to create and link to the received message. Format : [[
|
|
|
|
* string 'filepath' => local path to a readable copy of the media,
|
|
|
|
* ?string 'extension' => extension to use for the file or null
|
|
|
|
* ], ...]
|
2020-06-23 21:06:13 +02:00
|
|
|
*
|
2020-04-02 18:40:39 +02:00
|
|
|
* @return array : [
|
2020-06-23 21:06:13 +02:00
|
|
|
* bool 'error' => false if success, true else
|
|
|
|
* ?string 'error_message' => null if success, error message else
|
|
|
|
* ]
|
2020-04-02 18:40:39 +02:00
|
|
|
*/
|
2021-03-26 23:32:29 +01:00
|
|
|
public function receive(int $id_user, int $id_phone, string $text, string $origin, ?string $at = null, string $status = \models\Received::STATUS_UNREAD, bool $mms = false, array $medias = []): array
|
2020-04-02 18:40:39 +02:00
|
|
|
{
|
|
|
|
$return = [
|
|
|
|
'error' => false,
|
|
|
|
'error_message' => null,
|
|
|
|
];
|
|
|
|
|
2020-04-03 02:15:55 +02:00
|
|
|
$at = $at ?? (new \DateTime())->format('Y-m-d H:i:s');
|
2020-04-02 18:40:39 +02:00
|
|
|
$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);
|
2020-06-23 21:06:13 +02:00
|
|
|
if (false !== $response)
|
|
|
|
{ //Received sms is a command an we must use anonymized text
|
2020-04-02 18:40:39 +02:00
|
|
|
$is_command = true;
|
|
|
|
$text = $response;
|
|
|
|
}
|
2021-03-26 23:32:29 +01:00
|
|
|
|
|
|
|
//We create medias to link to the sms
|
|
|
|
$internal_media = new Media($this->bdd);
|
|
|
|
$media_ids = [];
|
|
|
|
if ($mms)
|
|
|
|
{
|
|
|
|
foreach ($medias as $media)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$new_media_id = $internal_media->create($id_user, $media['filepath'], $media['extension']);
|
|
|
|
$media_ids[] = $new_media_id;
|
|
|
|
}
|
|
|
|
catch (\Throwable $t)
|
|
|
|
{
|
|
|
|
$return['error_message'] = $t->getMessage();
|
|
|
|
continue; //Better loose the media than the message
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-02 18:40:39 +02:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
$received_id = $this->create($id_user, $id_phone, $at, $text, $origin, $status, $is_command, $mms, $media_ids);
|
2020-04-02 18:40:39 +02:00
|
|
|
if (!$received_id)
|
|
|
|
{
|
|
|
|
$return['error'] = true;
|
|
|
|
$return['error_message'] = 'Impossible to insert the sms in database.';
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-04-02 18:40:39 +02:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$received = [
|
|
|
|
'id' => $received_id,
|
|
|
|
'at' => $at,
|
|
|
|
'text' => $text,
|
|
|
|
'destination' => $id_phone,
|
|
|
|
'origin' => $origin,
|
2021-03-19 02:45:12 +01:00
|
|
|
'command' => $is_command,
|
|
|
|
'mms' => $mms,
|
2021-03-26 23:32:29 +01:00
|
|
|
'medias' => $internal_media->gets_in_for_user($id_user, $media_ids),
|
2020-04-02 18:40:39 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
$internal_webhook = new Webhook($this->bdd);
|
2021-03-23 04:31:13 +01:00
|
|
|
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_RECEIVE_SMS, $received);
|
2020-04-02 18:40:39 +02:00
|
|
|
|
|
|
|
$internal_user = new User($this->bdd);
|
|
|
|
$internal_user->transfer_received($id_user, $received);
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-04-02 18:40:39 +02:00
|
|
|
return $return;
|
|
|
|
}
|
2020-06-23 21:06:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the model for the Controller.
|
|
|
|
*/
|
|
|
|
protected function get_model(): \descartes\Model
|
|
|
|
{
|
|
|
|
$this->model = $this->model ?? new \models\Received($this->bdd);
|
|
|
|
|
|
|
|
return $this->model;
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|