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;
|
|
|
|
|
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);
|
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()
|
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
$discussions = $this->internal_received->get_discussions();
|
2019-10-29 18:36:25 +01:00
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($discussions as $key => $discussion)
|
|
|
|
{
|
|
|
|
if (!$contact = $this->internal_contact->get_by_number($discussion['number']))
|
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$discussions[$key]['contact'] = $contact['name'];
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
$this->render('discussion/list', [
|
2019-10-29 18:36:25 +01:00
|
|
|
'discussions' => $discussions,
|
2019-10-30 00:30:39 +01:00
|
|
|
]);
|
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-10-30 00:17:10 +01:00
|
|
|
$contact = $this->internal_contact->get_by_number($number);
|
2019-10-29 14:57:13 +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,
|
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
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction récupère l'ensemble des messages pour un numéro, recçus, envoyés, en cours.
|
|
|
|
*
|
|
|
|
* @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)
|
|
|
|
*/
|
|
|
|
public function get_messages($number, $transaction_id)
|
|
|
|
{
|
|
|
|
$now = new \DateTime();
|
|
|
|
$now = $now->format('Y-m-d H:i:s');
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-10 17:32:59 +01:00
|
|
|
$sendeds = $this->internal_sended->get_by_destination($number);
|
2019-11-10 17:00:21 +01:00
|
|
|
$receiveds = $this->internal_received->get_by_origin($number);
|
2019-10-30 00:17:10 +01:00
|
|
|
$scheduleds = $this->internal_scheduled->get_before_date_for_number($now, $number);
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
$messages = [];
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($sendeds as $sended)
|
|
|
|
{
|
|
|
|
$messages[] = [
|
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',
|
|
|
|
'status' => ($sended['delivered'] ? 'delivered' : ($sended['failed'] ? 'failed' : '')),
|
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 ($receiveds as $received)
|
|
|
|
{
|
|
|
|
$messages[] = [
|
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',
|
2019-11-10 17:32:59 +01:00
|
|
|
'md5' => md5($received['at'].$received['text']),
|
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)
|
|
|
|
{
|
|
|
|
$messages[] = [
|
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',
|
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
|
|
|
});
|
|
|
|
|
|
|
|
//On récupère uniquement les 25 derniers messages sur l'ensemble
|
2019-10-30 00:30:39 +01:00
|
|
|
$messages = \array_slice($messages, -25);
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
echo json_encode(['transaction_id' => $transaction_id, 'messages' => $messages]);
|
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é.
|
|
|
|
*
|
|
|
|
* @param string $csrf : Le jeton csrf
|
2019-11-10 17:36:42 +01:00
|
|
|
* @param string $_POST['text'] : Le contenu du Sms
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $_POST['numbers'] : Un tableau avec le numero des gens auxquel envoyer le 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'] ?? '';
|
2019-10-29 14:57:13 +01:00
|
|
|
$numbers = $_POST['numbers'] ?? false;
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$numbers)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
2019-11-14 22:33:00 +01:00
|
|
|
if (!$this->internal_scheduled->create($_SESSION['user']['id'], $id_user, $at, $text, false, false, $numbers))
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:30:39 +01:00
|
|
|
* Cette fonction retourne les id des sms qui sont envoyés.
|
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return string : json string Tableau des ids des sms qui sont envoyés
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function checksendeds()
|
|
|
|
{
|
|
|
|
$_SESSION['discussion_wait_progress'] = isset($_SESSION['discussion_wait_progress']) ? $_SESSION['discussion_wait_progress'] : [];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-11-14 22:33:00 +01:00
|
|
|
$scheduleds = $this->internal_scheduled->gets_in_for_user($_SESSION['user']['id'], $_SESSION['discussion_wait_progress']);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
//On va chercher à chaque fois si on a trouvé le sms. Si ce n'est pas le cas c'est qu'il a été envoyé
|
|
|
|
$sendeds = [];
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($_SESSION['discussion_wait_progress'] as $key => $id_scheduled)
|
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
$found = false;
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($scheduleds as $scheduled)
|
|
|
|
{
|
|
|
|
if ($id_scheduled === $scheduled['id'])
|
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
$found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
if (!$found)
|
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
unset($_SESSION['discussion_wait_progress'][$key]);
|
2019-10-30 00:17:10 +01:00
|
|
|
$sendeds[] = $id_scheduled;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo json_encode($sendeds);
|
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 retourne les messages reçus pour un numéro après la date $_SESSION['discussion_last_checkreceiveds'].
|
|
|
|
*
|
2019-10-29 18:36:25 +01:00
|
|
|
* @param string $number : Le numéro de téléphone pour lequel on veux les messages
|
2019-10-30 00:30:39 +01:00
|
|
|
*
|
2019-10-30 00:17:10 +01:00
|
|
|
* @return string : json string Un tableau avec les messages
|
2019-10-29 18:36:25 +01:00
|
|
|
*/
|
|
|
|
public function checkreceiveds($number)
|
|
|
|
{
|
|
|
|
$now = new \DateTime();
|
|
|
|
$now = $now->format('Y-m-d H:i');
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
$_SESSION['discussion_last_checkreceiveds'] = isset($_SESSION['discussion_last_checkreceiveds']) ? $_SESSION['discussion_last_checkreceiveds'] : $now;
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
$receiveds = $this->internal_received->get_since_for_number_by_date($_SESSION['discussion_last_checkreceiveds'], $number);
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
//On va gérer le cas des messages en double en stockant ceux déjà reçus et en eliminant les autres
|
|
|
|
$_SESSION['discussion_already_receiveds'] = isset($_SESSION['discussion_already_receiveds']) ? $_SESSION['discussion_already_receiveds'] : [];
|
|
|
|
|
2019-10-30 00:30:39 +01:00
|
|
|
foreach ($receiveds as $key => $received)
|
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
//Sms jamais recu
|
2019-10-30 00:30:39 +01:00
|
|
|
if (false === array_search($received['id'], $_SESSION['discussion_already_receiveds'], true))
|
|
|
|
{
|
2019-10-29 18:36:25 +01:00
|
|
|
$_SESSION['discussion_already_receiveds'][] = $received['id'];
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Sms déjà reçu => on le supprime des resultats
|
|
|
|
unset($receiveds[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//On met à jour la date de dernière verif
|
|
|
|
$_SESSION['discussion_last_checkreceiveds'] = $now;
|
2019-10-30 00:30:39 +01:00
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
echo json_encode($receiveds);
|
|
|
|
}
|
|
|
|
}
|