Add support for numbers with data on scheduled + add support for sending sms to a csv file

This commit is contained in:
osaajani 2022-03-15 02:24:28 +01:00
parent 7fc7a8f245
commit 81fb987740
22 changed files with 635 additions and 114 deletions

View file

@ -81,7 +81,7 @@ namespace controllers\internals;
exit($user ? 0 : 1);
}
/**
* Check if a user exists based on id.
*

View file

@ -31,7 +31,7 @@ namespace controllers\internals;
{
return $this->get_model()->datatable_list_for_user($id_user, $limit, $offset, $search, $search_columns, $order_column, $order_desc, $count);
}
/**
* Return a contact for a user by a number.
*

View file

@ -108,7 +108,7 @@ class Quota extends StandardController
}
/**
* Check if a message can be encoded as gsm0338 or if it must be UTF8
* Check if a message can be encoded as gsm0338 or if it must be UTF8.
*
* @param string $text : Message to send
*

View file

@ -48,8 +48,8 @@ use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
return true;
}
catch (\Throwable $t) //Catch both, exceptions and php error
{
catch (\Throwable $t)
{ //Catch both, exceptions and php error
return false;
}
}
@ -71,8 +71,8 @@ use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
return (bool) $result;
}
catch (\Throwable $t) //Catch both, exceptions and php error
{
catch (\Throwable $t)
{ //Catch both, exceptions and php error
return null;
}
}

View file

@ -24,7 +24,7 @@ namespace controllers\internals;
* @param ?int $id_phone : Id of the phone to send message with, null by default
* @param bool $flash : Is the sms a flash sms, by default false
* @param bool $mms : Is the sms a mms, by default false
* @param array $numbers : Numbers to send message to
* @param array $numbers : Array of numbers to send message to, a number is an array ['number' => '+33XXX', 'data' => '{"key":"value", ...}']
* @param array $contacts_ids : Contact ids to send message to
* @param array $groups_ids : Group ids to send message to
* @param array $conditional_group_ids : Conditional Groups ids to send message to
@ -84,7 +84,7 @@ namespace controllers\internals;
foreach ($numbers as $number)
{
$this->get_model()->insert_scheduled_number($id_scheduled, $number);
$this->get_model()->insert_scheduled_number($id_scheduled, $number['number'], $number['data']);
}
$internal_contact = new Contact($this->bdd);
@ -146,7 +146,7 @@ namespace controllers\internals;
* @param ?int $id_phone : Id of the phone to send message with, null by default
* @param bool $flash : Is the sms a flash sms, by default false
* @param bool $mms : Is the sms a mms, by default false
* @param array $numbers : Numbers to send message to
* @param array $numbers : Array of numbers to send message to, a number is an array ['number' => '+33XXX', 'data' => '{"key":"value", ...}']
* @param array $contacts_ids : Contact ids to send message to
* @param array $groups_ids : Group ids to send message to
* @param array $conditional_group_ids : Conditional Groups ids to send message to
@ -201,7 +201,7 @@ namespace controllers\internals;
foreach ($numbers as $number)
{
$this->get_model()->insert_scheduled_number($id_scheduled, $number);
$this->get_model()->insert_scheduled_number($id_scheduled, $number['number'], $number['data']);
}
$internal_contact = new Contact($this->bdd);
@ -496,6 +496,72 @@ namespace controllers\internals;
return $smss_to_send_per_scheduled;
}
/**
* Parse a CSV file of numbers, potentially associated with datas.
*
* @param resource $file_handler : File handler pointing to the file
*
* @throws Exception : raise exception if file is not valid
*
* @return mixed : array of numbers ['number' => '+XXXX...', 'data' => ['key' => 'value', ...]]
*/
public function parse_csv_numbers_file($file_handler)
{
$numbers = [];
$head = null;
$line_nb = 0;
while ($line = fgetcsv($file_handler))
{
++$line_nb;
if (null === $head)
{
$head = $line;
continue;
}
//Padding line with '' entries to make sure its same length as head
//this allow to mix users with data with users without data
$line = array_pad($line, \count($head), '');
$line = array_combine($head, $line);
if (false === $line)
{
continue;
}
$phone_number = \controllers\internals\Tool::parse_phone($line[array_keys($line)[0]] ?? '');
if (!$phone_number)
{
throw new \Exception('Erreur à la ligne ' . $line_nb . ' colonne 1, numéro de téléphone invalide.');
}
$data = [];
$i = 0;
foreach ($line as $key => $value)
{
++$i;
if ($i < 2)
{ // Ignore first column
continue;
}
if ('' === $value)
{
continue;
}
$key = mb_ereg_replace('[\W]', '', $key);
$data[$key] = $value;
}
$numbers[] = ['number' => $phone_number, 'data' => $data];
}
return $numbers;
}
/**
* Return numbers for a scheduled message.
*

View file

@ -39,14 +39,14 @@ namespace controllers\internals;
* @param int $id_phone : Id of the number the message was send with
* @param $at : Reception date
* @param $text : Text of the message
* @param string $destination : Number of the receiver
* @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
* @param bool $mms : Is the sms a MMS. By default false.
* @param array $medias : Array of medias to link to the MMS
* @param string $destination : Number of the receiver
* @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
* @param bool $mms : Is the sms a MMS. By default false.
* @param array $medias : Array of medias to link to the MMS
* @param ?int $originating_scheduled : Id of the scheduled message that was responsible for sending this message. By default null.
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
*
* @return mixed : false on error, new sended id else
*/
@ -220,11 +220,12 @@ namespace controllers\internals;
* @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 bool $mms : Is the sms a MMS. By default false.
* @param array $medias : Array of medias to link to the MMS
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
* @param string $destination : Number of the receiver
* @param bool $flash : Is the sms a flash. By default false.
* @param bool $mms : Is the sms a MMS. By default false.
* @param array $medias : Array of medias to link to the MMS
* @param string $status : Status of a the sms. By default \models\Sended::STATUS_UNKNOWN
* @param null|mixed $originating_scheduled
*
* @return array : [
* bool 'error' => false if success, true else
@ -291,7 +292,7 @@ namespace controllers\internals;
'medias' => $medias,
'originating_scheduled' => $originating_scheduled,
];
$internal_webhook = new Webhook($this->bdd);
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_SEND_SMS, $sended);

View file

@ -137,7 +137,7 @@ namespace controllers\internals;
/**
* Get the model for the Controller.
*
*
* @return \models\StandardModel
*/
abstract protected function get_model();

View file

@ -374,22 +374,24 @@ namespace controllers\internals;
}
/**
* Forge back an url parsed with PHP parse_url function
*
* Forge back an url parsed with PHP parse_url function.
*
* @param array $parsed_url : Parsed url returned by parse_url function
*
* @return string : The url as a string
*/
public static function unparse_url(array $parsed_url)
{
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "$pass@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : '';
$host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
$port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '';
$user = isset($parsed_url['user']) ? $parsed_url['user'] : '';
$pass = isset($parsed_url['pass']) ? ':' . $parsed_url['pass'] : '';
$pass = ($user || $pass) ? "{$pass}@" : '';
$path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
$query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : '';
$fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : '';
return "$scheme$user$pass$host$port$path$query$fragment";
}
return "{$scheme}{$user}{$pass}{$host}{$port}{$path}{$query}{$fragment}";
}
}

View file

@ -222,6 +222,7 @@ namespace controllers\publics;
$groups = $_POST['groups'] ?? [];
$conditional_groups = $_POST['conditional_groups'] ?? [];
$files = $_FILES['medias'] ?? false;
$csv_file = $_FILES['numbers_csv'] ?? false;
$numbers = \is_array($numbers) ? $numbers : [$numbers];
$contacts = \is_array($contacts) ? $contacts : [$contacts];
@ -315,22 +316,66 @@ namespace controllers\publics;
return $this->json($return);
}
if ($csv_file)
{
$uploaded_file = \controllers\internals\Tool::read_uploaded_file($csv_file);
if (!$uploaded_file['success'])
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['INVALID_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['INVALID_PARAMETER'] . 'csv : ' . $uploaded_file['content'];
$this->auto_http_code(false);
return $this->json($return);
}
try
{
$csv_numbers = $this->internal_scheduled->parse_csv_numbers_file($uploaded_file['content'], true);
if (!$csv_numbers)
{
throw new \Exception('no valid number in csv file.');
}
foreach ($csv_numbers as $csv_number)
{
$csv_number['data'] = json_encode($csv_number['data']);
$numbers[] = $csv_number;
}
}
catch (\Exception $e)
{
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['INVALID_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['INVALID_PARAMETER'] . 'csv : ' . $e->getMessage();
$this->auto_http_code(false);
return $this->json($return);
}
}
foreach ($numbers as $key => $number)
{
if (!is_string($number))
// If number is not an array turn it into an array
$number = is_array($number) ? $number : ['number' => $number, 'data' => '[]'];
$number['data'] = $number['data'] ?? '[]';
$number['number'] = \controllers\internals\Tool::parse_phone($number['number'] ?? '');
if (!$number['number'])
{
unset($numbers[$key]);
continue;
}
$number = \controllers\internals\Tool::parse_phone($number);
if (!$number)
if (null === json_decode($number['data']))
{
unset($numbers[$key]);
$return = self::DEFAULT_RETURN;
$return['error'] = self::ERROR_CODES['INVALID_PARAMETER'];
$return['message'] = self::ERROR_MESSAGES['INVALID_PARAMETER'] . 'number data must be a valid json or leave not set.';
$this->auto_http_code(false);
continue;
return $this->json($return);
}
$numbers[$key] = $number;
@ -368,7 +413,7 @@ namespace controllers\publics;
{
try
{
$new_media_id = $this->internal_media->upload_and_create_for_user($this->user['id'], $file);
$new_media_id = $this->internal_media->create_from_uploaded_file_for_user($this->user['id'], $file);
}
catch (\Exception $e)
{

View file

@ -45,7 +45,6 @@ namespace controllers\publics;
return $this->render('contact/list');
}
/**
* Return contacts as json.
*/

View file

@ -313,7 +313,7 @@ namespace controllers\publics;
$mms = (bool) count($media_ids);
//Destinations must be an array of number
$destinations = [$destination];
$destinations = [['number' => $destination]];
if (!$this->internal_scheduled->create($id_user, $at, $text, $id_phone, false, $mms, $destinations, [], [], [], $media_ids))
{

View file

@ -42,7 +42,7 @@ namespace controllers\publics;
{
$this->render('event/list');
}
/**
* Return events as json.
*/

View file

@ -173,7 +173,7 @@ namespace controllers\publics;
foreach ($receiveds as $key => $received)
{
$receiveds[$key]['text'] = $this->s($received['text'], false, true, false);
if (!$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $received['origin']))
{
continue;

View file

@ -197,7 +197,8 @@ namespace controllers\publics;
$numbers = $this->internal_scheduled->get_numbers($scheduled['id']);
foreach ($numbers as $number)
{
$scheduleds[$key]['numbers'][] = $number['number'];
$number['data'] = json_decode($number['data'] ?? '[]');
$scheduleds[$key]['numbers'][] = $number;
}
$contacts = $this->internal_scheduled->get_contacts($scheduled['id']);
@ -259,10 +260,12 @@ namespace controllers\publics;
$flash = (bool) ($_POST['flash'] ?? false);
$id_phone = empty($_POST['id_phone']) ? null : $_POST['id_phone'];
$numbers = $_POST['numbers'] ?? [];
$numbers = is_array($numbers) ? $numbers : [$numbers];
$contacts = $_POST['contacts'] ?? [];
$groups = $_POST['groups'] ?? [];
$conditional_groups = $_POST['conditional_groups'] ?? [];
$files = $_FILES['medias'] ?? false;
$csv_file = $_FILES['csv'] ?? false;
//Iterate over files to re-create individual $_FILES array
$files_arrays = [];
@ -326,17 +329,64 @@ namespace controllers\publics;
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
if ($csv_file)
{
$uploaded_file = \controllers\internals\Tool::read_uploaded_file($csv_file);
if (!$uploaded_file['success'])
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de traiter ce fichier CSV : ' . $uploaded_file['content']);
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
try
{
$csv_numbers = $this->internal_scheduled->parse_csv_numbers_file($uploaded_file['content']);
if (!$csv_numbers)
{
\FlashMessage\FlashMessage::push('danger', 'Aucun destinataire valide dans le fichier CSV, assurez-vous de fournir un fichier CSV valide.');
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
$numbers = array_merge($csv_numbers, $numbers);
}
catch (\Exception $e)
{
\FlashMessage\FlashMessage::push('danger', 'Impossible de traiter ce fichier CSV : ' . $e->getMessage());
return $this->redirect(\descartes\Router::url('Scheduled', 'add'));
}
}
foreach ($numbers as $key => $number)
{
$number = \controllers\internals\Tool::parse_phone($number);
// If number is not an array turn it into an array
$number = is_array($number) ? $number : ['number' => $number, 'data' => []];
$number['data'] = $number['data'] ?? [];
$number['number'] = \controllers\internals\Tool::parse_phone($number['number'] ?? '');
if (!$number)
if (!$number['number'])
{
unset($numbers[$key]);
continue;
}
$clean_data = [];
foreach ($number['data'] as $data_key => $value)
{
if ('' === $value)
{
continue;
}
$data_key = mb_ereg_replace('[\W]', '', $data_key);
$clean_data[$data_key] = (string) $value;
}
$clean_data = json_encode($clean_data);
$number['data'] = $clean_data;
$numbers[$key] = $number;
}
@ -415,6 +465,7 @@ namespace controllers\publics;
$groups = $scheduled['groups'] ?? [];
$conditional_groups = $scheduled['conditional_groups'] ?? [];
$files = $_FILES['scheduleds_' . $id_scheduled . '_medias'] ?? false;
$csv_file = $_FILES['scheduleds_' . $id_scheduled . '_csv'] ?? false;
$media_ids = $scheduled['media_ids'] ?? [];
//Check scheduled exists and belong to user
@ -482,16 +533,59 @@ namespace controllers\publics;
continue;
}
if ($csv_file)
{
$uploaded_file = \controllers\internals\Tool::read_uploaded_file($csv_file);
if (!$uploaded_file['success'])
{
continue;
}
try
{
$csv_numbers = $this->internal_scheduled->parse_csv_numbers_file($uploaded_file['content']);
if (!$csv_numbers)
{
continue;
}
$numbers = array_merge($csv_numbers, $numbers);
}
catch (\Exception $e)
{
continue;
}
}
$numbers = is_array($numbers) ? $numbers : [$numbers];
foreach ($numbers as $key => $number)
{
$number = \controllers\internals\Tool::parse_phone($number);
if (!$number)
// If number is not an array turn it into an array
$number = is_array($number) ? $number : ['number' => $number, 'data' => []];
$number['data'] = $number['data'] ?? [];
$number['number'] = \controllers\internals\Tool::parse_phone($number['number'] ?? '');
if (!$number['number'])
{
unset($numbers[$key]);
continue;
}
$clean_data = [];
foreach ($number['data'] as $data_key => $value)
{
if ('' === $value)
{
continue;
}
$data_key = mb_ereg_replace('[\W]', '', $data_key);
$clean_data[$data_key] = (string) $value;
}
$clean_data = json_encode($clean_data);
$number['data'] = $clean_data;
$numbers[$key] = $number;
}