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\publics;
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Page des discussions.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
class Discussion extends \descartes\Controller
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
private $internal_sended;
|
|
|
|
private $internal_scheduled;
|
|
|
|
private $internal_received;
|
|
|
|
private $internal_contact;
|
2020-03-04 04:41:31 +01:00
|
|
|
private $internal_phone;
|
2021-03-21 18:29:23 +01:00
|
|
|
private $internal_media;
|
2019-10-30 00:17:10 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
|
|
|
* Cette fonction est appelée avant toute les autres :
|
2019-10-30 00:30:39 +01:00
|
|
|
* Elle vérifie que l'utilisateur est bien connecté.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @return void;
|
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public function __construct()
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$bdd = \descartes\Model::_connect(DATABASE_HOST, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-07 16:17:18 +01:00
|
|
|
$this->internal_sended = new \controllers\internals\Sended($bdd);
|
2019-10-30 00:17:10 +01:00
|
|
|
$this->internal_scheduled = new \controllers\internals\Scheduled($bdd);
|
|
|
|
$this->internal_received = new \controllers\internals\Received($bdd);
|
|
|
|
$this->internal_contact = new \controllers\internals\Contact($bdd);
|
2020-03-04 04:41:31 +01:00
|
|
|
$this->internal_phone = new \controllers\internals\Phone($bdd);
|
2021-03-21 18:29:23 +01:00
|
|
|
$this->internal_media = new \controllers\internals\Media($bdd);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
\controllers\internals\Tool::verifyconnect();
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne toutes les discussions, sous forme d'un tableau permettant l'administration de ces contacts.
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function list()
|
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$this->render('discussion/list');
|
|
|
|
}
|
2021-01-14 03:32:17 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
/**
|
2021-01-14 03:32:17 +01:00
|
|
|
* Return discussions as json.
|
2020-09-23 03:02:13 +02:00
|
|
|
*/
|
|
|
|
public function list_json()
|
|
|
|
{
|
|
|
|
$entities = $this->internal_received->get_discussions_for_user($_SESSION['user']['id']);
|
2019-10-29 18:36:25 +01:00
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
foreach ($entities as &$entity)
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2020-09-23 03:02:13 +02:00
|
|
|
$entity['number_formatted'] = \controllers\internals\Tool::phone_link($entity['number']);
|
|
|
|
$entity['link'] = \descartes\Router::url('Discussion', 'show', ['number' => $entity['number']]);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
|
|
|
|
2020-09-23 03:02:13 +02:00
|
|
|
header('Content-Type: application/json');
|
|
|
|
echo json_encode(['data' => $entities]);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction permet d'afficher la discussion avec un numero.
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $number : La numéro de téléphone avec lequel on discute
|
|
|
|
*/
|
|
|
|
public function show($number)
|
|
|
|
{
|
2019-11-15 06:30:23 +01:00
|
|
|
$contact = $this->internal_contact->get_by_number_and_user($_SESSION['user']['id'], $number);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2020-01-11 17:27:07 +01:00
|
|
|
$last_sended = $this->internal_sended->get_last_for_destination_and_user($_SESSION['user']['id'], $number);
|
|
|
|
$last_received = $this->internal_received->get_last_for_origin_and_user($_SESSION['user']['id'], $number);
|
|
|
|
|
2020-04-03 17:09:48 +02:00
|
|
|
$response_phone_id = ($last_received['id_phone'] ?? $last_sended['id_phone'] ?? false);
|
|
|
|
if ($response_phone_id)
|
2020-03-04 04:41:31 +01:00
|
|
|
{
|
2020-04-03 17:09:48 +02:00
|
|
|
$response_phone = $this->internal_phone->get_for_user($_SESSION['user']['id'], $response_phone_id);
|
2020-03-04 04:41:31 +01:00
|
|
|
}
|
2020-01-11 17:27:07 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$this->render('discussion/show', [
|
2019-10-29 18:36:25 +01:00
|
|
|
'number' => $number,
|
|
|
|
'contact' => $contact,
|
2020-04-03 17:09:48 +02:00
|
|
|
'response_phone' => $response_phone ?? false,
|
2019-10-30 00:30:39 +01:00
|
|
|
]);
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
/**
|
2022-04-25 18:47:55 +02:00
|
|
|
* Cette fonction récupère l'ensemble des messages pour un numéro, reçus, envoyés, en cours.
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
|
|
|
* @param string $number : Le numéro cible
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $transaction_id : Le numéro unique de la transaction ajax (sert à vérifier si la requete doit être prise en compte)
|
|
|
|
*/
|
2021-04-16 17:11:24 +02:00
|
|
|
public function get_messages($number, $transaction_id)
|
2019-10-29 18:36:25 +01:00
|
|
|
{
|
|
|
|
$now = new \DateTime();
|
|
|
|
$now = $now->format('Y-m-d H:i:s');
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-15 06:30:23 +01:00
|
|
|
$id_user = $_SESSION['user']['id'];
|
|
|
|
|
2021-04-16 17:11:24 +02:00
|
|
|
$sendeds = $this->internal_sended->gets_by_destination_and_user($id_user, $number);
|
|
|
|
$receiveds = $this->internal_received->gets_by_origin_and_user($id_user, $number);
|
|
|
|
$scheduleds = $this->internal_scheduled->gets_before_date_for_number_and_user($id_user, $now, $number);
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
$messages = [];
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($sendeds as $sended)
|
|
|
|
{
|
2021-03-21 18:29:23 +01:00
|
|
|
$medias = [];
|
|
|
|
if ($sended['mms'])
|
|
|
|
{
|
|
|
|
$medias = $this->internal_media->gets_for_sended($sended['id']);
|
|
|
|
foreach ($medias as &$media)
|
|
|
|
{
|
2021-03-26 23:32:29 +01:00
|
|
|
$media = HTTP_PWD_DATA_PUBLIC . '/' . $media['path'];
|
2021-03-21 18:29:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = [
|
2021-06-17 00:51:33 +02:00
|
|
|
'uid' => 'sended-' . $sended['id'],
|
2019-10-29 18:36:25 +01:00
|
|
|
'date' => htmlspecialchars($sended['at']),
|
2019-11-10 17:32:59 +01:00
|
|
|
'text' => htmlspecialchars($sended['text']),
|
2019-10-29 18:36:25 +01:00
|
|
|
'type' => 'sended',
|
2021-03-21 18:29:23 +01:00
|
|
|
'medias' => $medias,
|
2019-11-15 06:30:23 +01:00
|
|
|
'status' => $sended['status'],
|
2019-10-30 00:30:39 +01:00
|
|
|
];
|
2021-03-21 18:29:23 +01:00
|
|
|
|
|
|
|
$messages[] = $message;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($receiveds as $received)
|
|
|
|
{
|
2020-01-17 18:19:25 +01:00
|
|
|
if ('read' !== $received['status'])
|
2019-12-02 01:55:06 +01:00
|
|
|
{
|
|
|
|
$this->internal_received->mark_as_read_for_user($id_user, $received['id']);
|
|
|
|
}
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-21 18:29:23 +01:00
|
|
|
$medias = [];
|
2021-03-27 01:15:09 +01:00
|
|
|
if ($received['mms'])
|
2021-03-21 18:29:23 +01:00
|
|
|
{
|
|
|
|
$medias = $this->internal_media->gets_for_received($received['id']);
|
|
|
|
foreach ($medias as &$media)
|
|
|
|
{
|
2021-03-26 23:32:29 +01:00
|
|
|
$media = HTTP_PWD_DATA_PUBLIC . '/' . $media['path'];
|
2021-03-21 18:29:23 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-02 01:55:06 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$messages[] = [
|
2021-06-17 00:51:33 +02:00
|
|
|
'uid' => 'received-' . $received['id'],
|
2019-10-29 18:36:25 +01:00
|
|
|
'date' => htmlspecialchars($received['at']),
|
2019-11-10 17:32:59 +01:00
|
|
|
'text' => htmlspecialchars($received['text']),
|
2019-10-29 18:36:25 +01:00
|
|
|
'type' => 'received',
|
2021-03-21 18:29:23 +01:00
|
|
|
'medias' => $medias,
|
2019-10-30 00:30:39 +01:00
|
|
|
];
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($scheduleds as $scheduled)
|
|
|
|
{
|
2021-03-21 18:29:23 +01:00
|
|
|
$medias = [];
|
2021-04-13 02:01:03 +02:00
|
|
|
if ($scheduled['mms'])
|
2021-03-21 18:29:23 +01:00
|
|
|
{
|
|
|
|
$medias = $this->internal_media->gets_for_scheduled($scheduled['id']);
|
|
|
|
foreach ($medias as &$media)
|
|
|
|
{
|
2021-03-26 23:32:29 +01:00
|
|
|
$media = HTTP_PWD_DATA_PUBLIC . '/' . $media['path'];
|
2021-03-21 18:29:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$messages[] = [
|
2021-06-17 00:51:33 +02:00
|
|
|
'uid' => 'scheduled-' . $scheduled['id'],
|
2019-10-29 18:36:25 +01:00
|
|
|
'date' => htmlspecialchars($scheduled['at']),
|
2019-11-10 17:32:59 +01:00
|
|
|
'text' => htmlspecialchars($scheduled['text']),
|
2019-10-29 18:36:25 +01:00
|
|
|
'type' => 'inprogress',
|
2021-03-21 18:29:23 +01:00
|
|
|
'medias' => $medias,
|
2019-10-30 00:30:39 +01:00
|
|
|
];
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//On va trier le tableau des messages
|
2019-10-30 00:30:39 +01:00
|
|
|
usort($messages, function ($a, $b)
|
|
|
|
{
|
|
|
|
return strtotime($a['date']) - strtotime($b['date']);
|
2019-10-29 18:36:25 +01:00
|
|
|
});
|
|
|
|
|
2021-04-16 17:11:24 +02:00
|
|
|
//Récupère uniquement les 25 derniers messages sur l'ensemble pour limiter la charge
|
|
|
|
$messages = \array_slice($messages, -25);
|
2021-04-13 02:01:03 +02:00
|
|
|
|
|
|
|
$response = [
|
|
|
|
'transaction_id' => $transaction_id,
|
|
|
|
'messages' => $messages,
|
|
|
|
];
|
|
|
|
|
|
|
|
echo json_encode($response);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction permet d'envoyer facilement un sms à un numéro donné.
|
|
|
|
*
|
2020-01-17 18:19:25 +01:00
|
|
|
* @param string $csrf : Le jeton csrf
|
|
|
|
* @param string $_POST['text'] : Le contenu du Sms
|
2020-01-11 17:27:07 +01:00
|
|
|
* @param string $_POST['destination'] : Number to send sms to
|
2020-03-04 05:10:45 +01:00
|
|
|
* @param string $_POST['id_phone'] : If of phone to send sms with
|
2021-06-17 00:51:33 +02:00
|
|
|
* @param array $_FILES['medias'] : Medias to upload and link to sms
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return string : json string Le statut de l'envoi
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function send($csrf)
|
|
|
|
{
|
2019-10-29 14:57:13 +01:00
|
|
|
$return = ['success' => true, 'message' => ''];
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
//On vérifie que le jeton csrf est bon
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$this->verify_csrf($csrf))
|
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
$return['success'] = false;
|
|
|
|
$return['message'] = 'Jeton CSRF invalide';
|
|
|
|
echo json_encode($return);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$now = new \DateTime();
|
|
|
|
$now = $now->format('Y-m-d H:i:s');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-11-12 17:37:20 +01:00
|
|
|
$id_user = $_SESSION['user']['id'];
|
2019-11-10 17:32:59 +01:00
|
|
|
$at = $now;
|
|
|
|
$text = $_POST['text'] ?? '';
|
2020-01-11 17:27:07 +01:00
|
|
|
$destination = $_POST['destination'] ?? false;
|
2020-03-04 04:41:31 +01:00
|
|
|
$id_phone = $_POST['id_phone'] ?? false;
|
2021-03-22 02:08:59 +01:00
|
|
|
$files = $_FILES['medias'] ?? false;
|
2021-06-17 00:51:33 +02:00
|
|
|
|
2021-03-22 02:08:59 +01:00
|
|
|
//Iterate over files to re-create individual $_FILES array
|
2021-06-17 00:51:33 +02:00
|
|
|
$files_arrays = [];
|
2021-03-22 02:08:59 +01:00
|
|
|
if ($files && is_array($files['name']))
|
|
|
|
{
|
|
|
|
foreach ($files as $property_name => $files_values)
|
|
|
|
{
|
|
|
|
foreach ($files_values as $file_key => $property_value)
|
|
|
|
{
|
|
|
|
if (!isset($files_arrays[$file_key]))
|
|
|
|
{
|
2021-06-17 00:51:33 +02:00
|
|
|
$files_arrays[$file_key] = [];
|
2021-03-22 02:08:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$files_arrays[$file_key][$property_name] = $property_value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Remove empty files input
|
|
|
|
foreach ($files_arrays as $key => $file)
|
|
|
|
{
|
2021-06-17 00:51:33 +02:00
|
|
|
if (UPLOAD_ERR_NO_FILE === $file['error'])
|
2021-03-22 02:08:59 +01:00
|
|
|
{
|
|
|
|
unset($files_arrays[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$text)
|
|
|
|
{
|
|
|
|
$return['success'] = false;
|
|
|
|
$return['message'] = 'Vous devez renseigner le texte de votre sms.';
|
|
|
|
echo json_encode($return);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2020-01-11 17:27:07 +01:00
|
|
|
if (!$destination)
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
$return['success'] = false;
|
2019-10-29 14:57:13 +01:00
|
|
|
$return['message'] = 'Vous devez renseigner un numéro valide';
|
2019-10-29 18:36:25 +01:00
|
|
|
echo json_encode($return);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-03-04 04:41:31 +01:00
|
|
|
if (!$id_phone)
|
2020-01-11 17:27:07 +01:00
|
|
|
{
|
2020-03-04 04:41:31 +01:00
|
|
|
$id_phone = null;
|
2020-01-11 17:27:07 +01:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:08:59 +01:00
|
|
|
//If mms is enable and we have medias uploaded
|
|
|
|
$media_ids = [];
|
|
|
|
if ($_SESSION['user']['settings']['mms'] && $files_arrays)
|
|
|
|
{
|
|
|
|
foreach ($files_arrays as $file)
|
|
|
|
{
|
2021-03-26 23:32:29 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$new_media_id = $this->internal_media->create_from_uploaded_file_for_user($_SESSION['user']['id'], $file);
|
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
2021-03-22 02:08:59 +01:00
|
|
|
{
|
|
|
|
$return['success'] = false;
|
2021-03-26 23:32:29 +01:00
|
|
|
$return['message'] = $e->getMessage();
|
2021-03-22 02:08:59 +01:00
|
|
|
echo json_encode($return);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$media_ids[] = $new_media_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$mms = (bool) count($media_ids);
|
|
|
|
|
2020-01-11 17:27:07 +01:00
|
|
|
//Destinations must be an array of number
|
2022-04-25 18:47:55 +02:00
|
|
|
$destinations = [['number' => $destination, 'data' => '[]']];
|
2020-01-11 17:27:07 +01:00
|
|
|
|
2021-03-22 02:08:59 +01:00
|
|
|
if (!$this->internal_scheduled->create($id_user, $at, $text, $id_phone, false, $mms, $destinations, [], [], [], $media_ids))
|
2019-10-30 00:30:39 +01:00
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
$return['success'] = false;
|
2019-10-30 00:17:10 +01:00
|
|
|
$return['message'] = 'Impossible de créer le Sms';
|
2019-10-29 18:36:25 +01:00
|
|
|
echo json_encode($return);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
return false;
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
echo json_encode($return);
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|