2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
2019-10-30 00:30:39 +01:00
|
|
|
|
|
|
|
/*
|
2019-11-10 17:48:54 +01:00
|
|
|
* This file is part of RaspiSMS.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-11-10 17:48:54 +01:00
|
|
|
* (c) Pierre-Lin Bonnemaison <plebwebsas@gmail.com>
|
2019-10-30 00:30:39 +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-10-30 00:30:39 +01:00
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
2019-10-29 14:57:13 +01:00
|
|
|
namespace controllers\internals;
|
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
class Sended extends StandardController
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
protected $model;
|
2021-07-14 04:38:52 +02:00
|
|
|
|
|
|
|
/**
|
2021-07-16 22:53:33 +02:00
|
|
|
*
|
2021-07-14 04:38:52 +02:00
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param ?int $limit : Number of entry to return
|
|
|
|
* @param ?int $offset : Number of entry to avoid
|
|
|
|
* @param ?string $search : String to search for
|
2021-07-16 22:53:33 +02:00
|
|
|
* @param ?array $search_columns : List of columns to search on
|
|
|
|
* @param ?string $order_column : Name of the column to order by
|
|
|
|
* @param bool $order_desc : Should result be ordered DESC, if false order ASC
|
|
|
|
* @param bool $count : Should the query only count results
|
2021-07-14 04:38:52 +02:00
|
|
|
*
|
2021-07-16 22:53:33 +02:00
|
|
|
* @return array : Entries list
|
2021-07-14 04:38:52 +02:00
|
|
|
*/
|
|
|
|
public function datatable_list_for_user(int $id_user, ?int $limit = null, ?int $offset = null, ?string $search = null, ?array $search_columns = [], ?string $order_column = null, bool $order_desc = false, $count = false)
|
|
|
|
{
|
|
|
|
return $this->get_model()->datatable_list_for_user($id_user, $limit, $offset, $search, $search_columns, $order_column, $order_desc, $count);
|
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Create a sended.
|
|
|
|
*
|
2020-06-23 21:06:13 +02:00
|
|
|
* @param int $id_user : Id of user to create sended message for
|
2020-03-31 01:19:21 +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
|
|
|
|
* @param string $destination : Number of the receiver
|
2020-01-17 18:19:25 +01:00
|
|
|
* @param string $uid : Uid of the sms on the adapter service used
|
|
|
|
* @param string $adapter : Name of the adapter service used to send the message
|
|
|
|
* @param bool $flash : Is the sms a flash
|
2021-03-19 02:45:12 +01:00
|
|
|
* @param bool $mms : Is the sms a MMS. By default false.
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param array $medias : Array of medias to link to the MMS
|
2020-04-02 01:55:55 +02:00
|
|
|
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-04-02 18:40:39 +02:00
|
|
|
* @return mixed : false on error, new sended id else
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2021-03-19 02:45:12 +01:00
|
|
|
public function create(int $id_user, int $id_phone, $at, string $text, string $destination, string $uid, string $adapter, bool $flash = false, bool $mms = false, array $medias = [], ?string $status = \models\Sended::STATUS_UNKNOWN)
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$sended = [
|
2020-03-05 23:07:07 +01:00
|
|
|
'id_user' => $id_user,
|
2020-03-31 01:19:21 +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
|
|
|
'destination' => $destination,
|
2020-01-06 23:35:55 +01:00
|
|
|
'uid' => $uid,
|
|
|
|
'adapter' => $adapter,
|
2019-11-14 02:02:50 +01:00
|
|
|
'flash' => $flash,
|
2021-03-19 02:45:12 +01:00
|
|
|
'mms' => $mms,
|
2019-11-14 02:02:50 +01:00
|
|
|
'status' => $status,
|
|
|
|
];
|
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
//Ensure atomicity
|
|
|
|
$this->bdd->beginTransaction();
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
$id_sended = $this->get_model()->insert($sended);
|
|
|
|
if (!$id_sended)
|
|
|
|
{
|
|
|
|
$this->bdd->rollback();
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
return false;
|
|
|
|
}
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
//Link medias
|
|
|
|
$internal_media = new Media($this->bdd);
|
|
|
|
foreach ($medias as $media)
|
|
|
|
{
|
|
|
|
$internal_media->link_to($media['id'], 'sended', $id_sended); //No rollback on error, keeping track of mms is more important than integrity
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$this->bdd->commit())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $id_sended;
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a sended status for a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : user id
|
|
|
|
* @param int $id_sended : Sended id
|
|
|
|
* @param string $status : Status of a the sms (unknown, delivered, failed)
|
|
|
|
*
|
2019-11-14 02:02:50 +01:00
|
|
|
* @return bool : false on error, true on success
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function update_status_for_user(int $id_user, int $id_sended, string $status): bool
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$sended = [
|
2019-11-14 02:02:50 +01:00
|
|
|
'status' => $status,
|
|
|
|
];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-14 02:02:50 +01:00
|
|
|
return (bool) $this->get_model()->update_for_user($id_user, $id_sended, $sended);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Update a sended status for a sended.
|
|
|
|
*
|
|
|
|
* @param int $id_sended : Sended id
|
|
|
|
* @param string $status : Status of a the sms (unknown, delivered, failed)
|
|
|
|
*
|
2020-01-08 02:14:38 +01:00
|
|
|
* @return bool : false on error, true on success
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function update_status(int $id_sended, string $status): bool
|
2020-01-08 02:14:38 +01:00
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
$sended = [
|
2020-01-08 02:14:38 +01:00
|
|
|
'status' => $status,
|
|
|
|
];
|
|
|
|
|
|
|
|
return (bool) $this->get_model()->update($id_sended, $sended);
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return x last sendeds 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 sendeds messages to return
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
|
|
|
* @return array
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-11-14 02:02:50 +01:00
|
|
|
public function get_lasts_by_date_for_user(int $id_user, int $nb_entry)
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-11-14 02:02:50 +01:00
|
|
|
return $this->get_model()->get_lasts_by_date_for_user($id_user, $nb_entry);
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
2019-11-07 16:17:18 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return sendeds for a destination and a user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
|
|
|
* @param string $origin : Number who sent the message
|
|
|
|
*
|
2019-11-07 16:17:18 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2019-11-15 06:30:23 +01:00
|
|
|
public function gets_by_destination_and_user(int $id_user, string $origin)
|
2019-11-07 16:17:18 +01:00
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
return $this->get_model()->gets_by_destination_and_user($id_user, $origin);
|
2019-11-07 16:17:18 +01:00
|
|
|
}
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-04-13 02:01:03 +02:00
|
|
|
/**
|
|
|
|
* Return sendeds for a destination 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
|
2021-06-17 00:51:33 +02:00
|
|
|
*
|
2021-04-13 02:01:03 +02:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function gets_since_date_by_destination_and_user(int $id_user, string $since, string $origin)
|
|
|
|
{
|
|
|
|
return $this->get_model()->gets_since_date_by_destination_and_user($id_user, $since, $origin);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-08 02:14:38 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Return sended for an uid and an adapter.
|
|
|
|
*
|
2020-06-23 21:06:13 +02:00
|
|
|
* @param int $id_user : user id
|
2020-01-17 18:19:25 +01:00
|
|
|
* @param string $uid : Uid of the sended
|
2020-01-08 02:14:38 +01:00
|
|
|
* @param string $adapter : Adapter used to send the message
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-08 02:14:38 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-03-05 23:07:07 +01:00
|
|
|
public function get_by_uid_and_adapter_for_user(int $id_user, string $uid, string $adapter)
|
2020-01-08 02:14:38 +01:00
|
|
|
{
|
2020-03-05 23:07:07 +01:00
|
|
|
return $this->get_model()->get_by_uid_and_adapter_for_user($id_user, $uid, $adapter);
|
2020-01-08 02:14:38 +01:00
|
|
|
}
|
2019-11-07 16:17:18 +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
|
2019-11-07 16:17:18 +01:00
|
|
|
*/
|
2019-11-14 02:02:50 +01:00
|
|
|
public function count_by_day_since_for_user(int $id_user, $date)
|
2019-11-07 16:17:18 +01:00
|
|
|
{
|
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-07 16:17:18 +01:00
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
2020-01-11 17:27:07 +01:00
|
|
|
/**
|
2020-01-17 18:19:25 +01:00
|
|
|
* Find last sended message for a destination and user.
|
|
|
|
*
|
|
|
|
* @param int $id_user : User id
|
2020-01-11 17:27:07 +01:00
|
|
|
* @param string $destination : Destination number
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-01-11 17:27:07 +01:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-01-17 18:19:25 +01:00
|
|
|
public function get_last_for_destination_and_user(int $id_user, string $destination)
|
2020-01-11 17:27:07 +01:00
|
|
|
{
|
|
|
|
return $this->get_model()->get_last_for_destination_and_user($id_user, $destination);
|
|
|
|
}
|
2020-01-17 18:19:25 +01:00
|
|
|
|
|
|
|
/**
|
2020-06-23 21:06:13 +02:00
|
|
|
* Send a SMS message.
|
2020-01-17 18:19:25 +01:00
|
|
|
*
|
2020-06-23 21:06:13 +02:00
|
|
|
* @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
|
2020-04-02 18:40:39 +02:00
|
|
|
* @param $text : Text of the message
|
|
|
|
* @param string $destination : Number of the receiver
|
|
|
|
* @param bool $flash : Is the sms a flash. By default false.
|
2021-03-19 02:45:12 +01:00
|
|
|
* @param bool $mms : Is the sms a MMS. By default false.
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param array $medias : Array of medias to link to the MMS
|
2020-04-02 18:40:39 +02:00
|
|
|
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
|
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-19 02:45:12 +01:00
|
|
|
public function send(\adapters\AdapterInterface $adapter, int $id_user, int $id_phone, string $text, string $destination, bool $flash = false, bool $mms = false, array $medias = [], string $status = \models\Sended::STATUS_UNKNOWN): array
|
2020-04-02 18:40:39 +02:00
|
|
|
{
|
|
|
|
$return = [
|
|
|
|
'error' => false,
|
|
|
|
'error_message' => null,
|
|
|
|
];
|
|
|
|
|
2021-06-08 02:00:48 +02:00
|
|
|
//If we reached our max quota, do not send the message
|
|
|
|
$internal_quota = new Quota($this->bdd);
|
|
|
|
$nb_credits = $internal_quota::compute_credits_for_message($text); //Calculate how much credit the message require
|
2021-06-14 19:48:42 +02:00
|
|
|
if (!$internal_quota->has_enough_credit($id_user, $nb_credits))
|
2021-06-08 02:00:48 +02:00
|
|
|
{
|
|
|
|
$return['error'] = false;
|
|
|
|
$return['error_message'] = 'Not enough credit to send message.';
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2020-04-02 18:40:39 +02:00
|
|
|
$at = (new \DateTime())->format('Y-m-d H:i:s');
|
2021-03-19 02:45:12 +01:00
|
|
|
$media_uris = [];
|
|
|
|
foreach ($medias as $media)
|
|
|
|
{
|
|
|
|
$media_uris[] = [
|
2021-04-06 23:20:00 +02:00
|
|
|
'path' => $media['path'],
|
2021-03-26 23:32:29 +01:00
|
|
|
'local_uri' => PWD_DATA_PUBLIC . '/' . $media['path'],
|
2021-03-19 02:45:12 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2021-04-16 20:20:19 +02:00
|
|
|
//If adapter does not support mms and the message is a mms, add medias as link
|
|
|
|
if (!$adapter::meta_support_mms_sending() && $mms)
|
|
|
|
{
|
|
|
|
$media_urls = [];
|
|
|
|
foreach ($media_uris as $media_uri)
|
|
|
|
{
|
|
|
|
$media_urls[] = STATIC_HTTP_URL . '/data/public/' . $media_uri['path'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$text .= "\n" . join(' - ', $media_urls);
|
|
|
|
}
|
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
$response = $adapter->send($destination, $text, $flash, $mms, $media_uris);
|
2020-04-02 18:40:39 +02:00
|
|
|
|
|
|
|
if ($response['error'])
|
|
|
|
{
|
|
|
|
$return['error'] = true;
|
|
|
|
$return['error_message'] = $response['error_message'];
|
|
|
|
$status = \models\Sended::STATUS_FAILED;
|
2021-03-19 02:45:12 +01:00
|
|
|
$this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $status);
|
2020-06-23 21:06:13 +02:00
|
|
|
|
2020-04-02 18:40:39 +02:00
|
|
|
return $return;
|
|
|
|
}
|
|
|
|
|
2021-06-08 02:00:48 +02:00
|
|
|
$internal_quota->consume_credit($id_user, $nb_credits);
|
|
|
|
|
2021-03-19 02:45:12 +01:00
|
|
|
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $status);
|
2020-04-02 18:40:39 +02:00
|
|
|
|
|
|
|
$sended = [
|
|
|
|
'id' => $sended_id,
|
|
|
|
'at' => $at,
|
|
|
|
'text' => $text,
|
|
|
|
'destination' => $destination,
|
|
|
|
'origin' => $id_phone,
|
2021-03-23 04:31:13 +01:00
|
|
|
'mms' => $mms,
|
|
|
|
'medias' => $medias,
|
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_SEND_SMS, $sended);
|
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.
|
|
|
|
*/
|
2021-07-16 22:53:33 +02:00
|
|
|
protected function get_model(): \models\Sended
|
2020-06-23 21:06:13 +02:00
|
|
|
{
|
|
|
|
$this->model = $this->model ?? new \models\Sended($this->bdd);
|
|
|
|
|
|
|
|
return $this->model;
|
|
|
|
}
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|