2019-10-29 14:57:13 +01:00
|
|
|
<?php
|
|
|
|
namespace controllers\internals;
|
2019-10-29 18:36:25 +01:00
|
|
|
|
|
|
|
class Console extends \descartes\InternalController
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
/**
|
2019-10-30 00:17:10 +01:00
|
|
|
* Cette fonction envoie tous les Sms programmés qui doivent l'être
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
|
|
|
public function sendScheduled()
|
|
|
|
{
|
|
|
|
//On créé l'objet de base de données
|
|
|
|
global $db;
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
for ($i = 0; $i < 30; $i++) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$now = new \DateTime();
|
|
|
|
$now = $now->format('Y-m-d H:i:s');
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
echo "Début de l'envoi des Sms programmés\n";
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
$scheduleds = $db->getScheduledNotInProgressBefore($now);
|
|
|
|
|
|
|
|
$ids_scheduleds = array();
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On passe en cours de progression tous les Sms
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($scheduleds as $scheduled) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$ids_scheduleds[] = $scheduled['id'];
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
echo count($ids_scheduleds) . " Sms à envoyer ont été trouvés et ajoutés à la liste des Sms en cours d'envoi.\n";
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
$db->updateProgressScheduledIn($ids_scheduleds, true);
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//Pour chaque Sms à envoyer
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($scheduleds as $scheduled) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$id_scheduled = $scheduled['id'];
|
|
|
|
$text_sms = escapeshellarg($scheduled['content']);
|
|
|
|
$flash = $scheduled['flash'];
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On initialise les numéros auxquelles envoyer le Sms
|
2019-10-29 14:57:13 +01:00
|
|
|
$numbers = array();
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On récupère les numéros pour le Sms et on les ajoute
|
2019-10-29 14:57:13 +01:00
|
|
|
$target_numbers = $db->getNumbersForScheduled($id_scheduled);
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($target_numbers as $target_number) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$numbers[] = $target_number['number'];
|
|
|
|
}
|
|
|
|
|
|
|
|
//On récupère les contacts, et on ajoute les numéros
|
|
|
|
$contacts = $db->getContactForScheduled($id_scheduled);
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($contacts as $contact) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$numbers[] = $contact['number'];
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On récupère les groups
|
|
|
|
$groups = $db->getGroupForScheduled($id_scheduled);
|
|
|
|
foreach ($groups as $group) {
|
|
|
|
//On récupère les contacts du group et on les ajoute aux numéros
|
|
|
|
$contacts = $db->getContactForGroup($group['id']);
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($contacts as $contact) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$numbers[] = $contact['number'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$smsStops = $db->getFromTableWhere('smsstop');
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($numbers as $number) {
|
2019-10-30 00:17:10 +01:00
|
|
|
//Si les Sms STOP sont activés, on passe au numéro suivant si le numéro actuelle fait parti des Sms STOP
|
|
|
|
if (RASPISms_SETTINGS_SmsSTOPS) {
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($smsStops as $smsStop) {
|
|
|
|
if (!($number == $smsStop['number'])) {
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
echo "Un Sms destiné au " . $number . " a été bloqué par Sms STOP\n";
|
2019-10-29 14:57:13 +01:00
|
|
|
continue(2); //On passe au numéro suivant !
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
echo " Envoi d'un Sms au " . $number . "\n";
|
|
|
|
//On ajoute le Sms aux Sms envoyés
|
2019-10-29 14:57:13 +01:00
|
|
|
//Pour plus de précision, on remet la date à jour en réinstanciant l'objet \DateTime (et on reformatte la date, bien entendu)
|
|
|
|
$now = new \DateTime();
|
|
|
|
$now = $now->format('Y-m-d H:i:s');
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On peut maintenant ajouter le Sms
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!$db->insertIntoTable('sendeds', ['at' => $now, 'target' => $number, 'content' => $scheduled['content'], 'before_delivered' => ceil(mb_strlen($scheduled['content'])/160)])) {
|
2019-10-29 14:57:13 +01:00
|
|
|
echo 'Impossible d\'inserer le sms pour le numero ' . $number . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
$id_sended = $db->lastId();
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//Commande qui envoie le Sms
|
2019-10-29 14:57:13 +01:00
|
|
|
$commande_send_sms = 'gammu-smsd-inject TEXT ' . escapeshellarg($number) . ' -report -len ' . mb_strlen($text_sms) . ' -text ' . $text_sms;
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
if (RASPISms_SETTINGS_Sms_FLASH && $flash) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$commande_send_sms .= ' -flash';
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//Commande qui s'assure de passer le Sms dans ceux envoyés, et de lui donner le bon statut
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On va liée les deux commandes pour envoyer le Sms puis le passer en echec
|
2019-10-29 14:57:13 +01:00
|
|
|
$commande = '(' . $commande_send_sms . ') >/dev/null 2>/dev/null &';
|
2019-10-30 00:17:10 +01:00
|
|
|
exec($commande); //On execute la commande d'envoie d'un Sms
|
2019-10-29 14:57:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
echo "Tous les Sms sont en cours d'envoi.\n";
|
|
|
|
//Tous les Sms ont été envoyés.
|
2019-10-29 14:57:13 +01:00
|
|
|
$db->deleteScheduledIn($ids_scheduleds);
|
|
|
|
|
|
|
|
//On dors 2 secondes
|
|
|
|
sleep(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-30 00:17:10 +01:00
|
|
|
* Cette fonction reçoit un Sms, et l'enregistre, en essayant dde trouver une commande au passage.
|
2019-10-29 14:57:13 +01:00
|
|
|
*/
|
2019-10-30 00:17:10 +01:00
|
|
|
public function parseReceivedSms()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
|
|
|
//On créer l'objet de base de données
|
|
|
|
global $db;
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
for ($i = 0; $i < 30; $i++) {
|
|
|
|
foreach (scandir(PWD_RECEIVEDS) as $dir) {
|
2019-10-29 14:57:13 +01:00
|
|
|
//Si le fichier est un fichier système, on passe à l'itération suivante
|
2019-10-29 18:36:25 +01:00
|
|
|
if ($dir == '.' || $dir == '..' || $dir == '.tokeep') {
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
2019-10-29 18:36:25 +01:00
|
|
|
}
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
echo "Analyse du Sms " . $dir . "\n";
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On récupère la date du Sms à la seconde près grâce au nom du fichier (Cf. parseSms.sh)
|
2019-10-29 14:57:13 +01:00
|
|
|
//Il faut mettre la date au format Y-m-d H:i:s
|
|
|
|
$date = substr($dir, 0, 4) . '-' . substr($dir, 4, 2) . '-' . substr($dir, 6, 2) . ' ' . substr($dir, 8, 2) . ':' . substr($dir, 10, 2) . ':' . substr($dir, 12, 2);
|
|
|
|
|
|
|
|
//On récupère le fichier, et on récupère la chaine jusqu'au premier ':' pour le numéro de téléphone source, et la fin pour le message
|
|
|
|
$content_file = file_get_contents(PWD_RECEIVEDS . $dir);
|
|
|
|
|
|
|
|
//Si on peux pas ouvrir le fichier, on quitte en logant une erreur
|
2019-10-29 18:36:25 +01:00
|
|
|
if ($content_file == false) {
|
2019-10-30 00:17:10 +01:00
|
|
|
error_log('Unable to read file "' . $dir);
|
2019-10-29 14:57:13 +01:00
|
|
|
die(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
//On supprime le fichier. Si on n'y arrive pas, alors on log
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!unlink(PWD_RECEIVEDS . $dir)) {
|
2019-10-30 00:17:10 +01:00
|
|
|
error_log('Unable to delete file "' . $dir);
|
2019-10-29 14:57:13 +01:00
|
|
|
die(8);
|
|
|
|
}
|
|
|
|
|
|
|
|
$content_file = explode(':', $content_file, 2);
|
|
|
|
|
|
|
|
//Si on a pas passé de numéro ou de message, alors on lève une erreur
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!isset($content_file[0], $content_file[1])) {
|
2019-10-30 00:17:10 +01:00
|
|
|
error_log('Missing params in file "' . $dir);
|
2019-10-29 14:57:13 +01:00
|
|
|
die(5);
|
|
|
|
}
|
|
|
|
|
|
|
|
$number = $content_file[0];
|
2019-10-30 00:17:10 +01:00
|
|
|
$number = \controllers\internals\Tool::parse_phone($number);
|
2019-10-29 14:57:13 +01:00
|
|
|
$text = $content_file[1];
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On gère les Sms STOP
|
2019-10-29 18:36:25 +01:00
|
|
|
if (trim($text) == 'STOP') {
|
2019-10-30 00:17:10 +01:00
|
|
|
echo 'STOP Sms detected ' . $number . "\n";
|
|
|
|
error_log('STOP Sms detected ' . $number);
|
2019-10-29 14:57:13 +01:00
|
|
|
$db->insertIntoTable('smsstop', ['number' => $number]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//On gère les accusés de reception
|
2019-10-29 18:36:25 +01:00
|
|
|
if (trim($text) == 'Delivered' || trim($text) == 'Failed') {
|
2019-10-30 00:17:10 +01:00
|
|
|
echo 'Delivered or Failed Sms for ' . $number . "\n";
|
|
|
|
error_log('Delivered or Failed Sms for ' . $number);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On récupère les Sms pas encore validé, uniquement sur les dernières 12h
|
2019-10-29 14:57:13 +01:00
|
|
|
$now = new \DateTime();
|
|
|
|
$interval = new \DateInterval('PT12H');
|
|
|
|
$sinceDate = $now->sub($interval)->format('Y-m-d H:i:s');
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!$sendeds = $db->getFromTableWhere('sendeds', ['target' => $number, 'delivered' => false, 'failed' => false, '>at' => $sinceDate], 'at', false, 1)) {
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sended = $sendeds[0];
|
|
|
|
|
|
|
|
//On gère les echecs
|
2019-10-29 18:36:25 +01:00
|
|
|
if (trim($text) == 'Failed') {
|
2019-10-29 14:57:13 +01:00
|
|
|
$db->updateTableWhere('sendeds', ['before_delivered' => 0, 'failed' => true], ['id' => $sended['id']]);
|
2019-10-30 00:17:10 +01:00
|
|
|
echo "Sended Sms id " . $sended['id'] . " pass to failed status\n";
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//On gère le cas des messages de plus de 160 caractères, lesquels impliquent plusieurs accusés
|
2019-10-29 18:36:25 +01:00
|
|
|
if ($sended['before_delivered'] > 1) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$db->updateTableWhere('sendeds', ['before_delivered' => $sended['before_delivered'] - 1], ['id' => $sended['id']]);
|
2019-10-30 00:17:10 +01:00
|
|
|
echo "Sended Sms id " . $sended['id'] . " before_delivered decrement\n";
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Si tout est bon, que nous avons assez d'accusés, nous validons !
|
|
|
|
$db->updateTableWhere('sendeds', ['before_delivered' => 0, 'delivered' => true], ['id' => $sended['id']]);
|
2019-10-30 00:17:10 +01:00
|
|
|
echo "Sended Sms id " . $sended['id'] . " to delivered status\n";
|
2019-10-29 14:57:13 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!$number) {
|
2019-10-30 00:17:10 +01:00
|
|
|
error_log('Invalid phone number in file "' . $dir);
|
2019-10-29 14:57:13 +01:00
|
|
|
die(6);
|
|
|
|
}
|
|
|
|
|
|
|
|
//On va vérifier si on a reçu une commande, et des identifiants
|
2019-10-30 00:17:10 +01:00
|
|
|
$flags = \controllers\internals\Tool::parse_for_flag($text);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
//On créer le tableau qui permettra de stocker les commandes trouvées
|
|
|
|
|
|
|
|
$found_commands = array();
|
|
|
|
|
|
|
|
//Si on reçu des identifiants
|
2019-10-29 18:36:25 +01:00
|
|
|
if (array_key_exists('LOGIN', $flags) && array_key_exists('PASSWORD', $flags)) {
|
2019-10-29 14:57:13 +01:00
|
|
|
//Si on a bien un utilisateur avec les identifiants reçus
|
|
|
|
$user = $db->getUserFromEmail($flags['LOGIN']);
|
2019-10-30 00:17:10 +01:00
|
|
|
error_log('We found ' . count($user) . ' users');
|
2019-10-29 18:36:25 +01:00
|
|
|
if ($user && $user['password'] == sha1($flags['PASSWORD'])) {
|
2019-10-30 00:17:10 +01:00
|
|
|
error_log('Password is valid');
|
2019-10-29 14:57:13 +01:00
|
|
|
//On va passer en revue toutes les commandes, pour voir si on en trouve dans ce message
|
|
|
|
$commands = $db->getFromTableWhere('commands');
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
error_log('We found ' . count($commands) . ' commands');
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($commands as $command) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$command_name = mb_strtoupper($command['name']);
|
2019-10-29 18:36:25 +01:00
|
|
|
if (array_key_exists($command_name, $flags)) {
|
2019-10-30 00:17:10 +01:00
|
|
|
error_log('We found command ' . $command_name);
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
//Si la commande ne nécessite pas d'être admin, ou si on est admin
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!$command['admin'] || $user['admin']) {
|
2019-10-30 00:17:10 +01:00
|
|
|
error_log('And the count is ok');
|
2019-10-29 14:57:13 +01:00
|
|
|
$found_commands[$command_name] = PWD_SCRIPTS . $command['script'] . escapeshellcmd($flags[$command_name]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On va supprimer le mot de passe du Sms pour pouvoir l'enregistrer sans danger
|
2019-10-29 18:36:25 +01:00
|
|
|
if (isset($flags['PASSWORD'])) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$text = str_replace($flags['PASSWORD'], '*****', $text);
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On map les données et on créer le Sms reçu
|
2019-10-29 14:57:13 +01:00
|
|
|
$send_by = $number;
|
|
|
|
$content = $text;
|
|
|
|
$is_command = count($found_commands);
|
2019-10-29 18:36:25 +01:00
|
|
|
if (!$db->insertIntoTable('receiveds', ['at' => $date, 'send_by' => $send_by, 'content' => $content, 'is_command' => $is_command])) {
|
2019-10-30 00:17:10 +01:00
|
|
|
echo "Erreur lors de l'enregistrement du Sms\n";
|
|
|
|
error_log('Unable to process the Sms in file "' . $dir);
|
2019-10-29 14:57:13 +01:00
|
|
|
die(7);
|
|
|
|
}
|
|
|
|
|
2019-10-30 00:17:10 +01:00
|
|
|
//On insert le Sms dans le tableau des sms à envoyer par mail
|
2019-10-29 14:57:13 +01:00
|
|
|
$db->insertIntoTable('transfers', ['id_received' => $db->lastId(), 'progress' => false]);
|
|
|
|
|
|
|
|
//Chaque commande sera executée.
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($found_commands as $command_name => $command) {
|
2019-10-29 14:57:13 +01:00
|
|
|
echo 'Execution de la commande : ' . $command_name . ' :: ' . $command . "\n";
|
|
|
|
exec($command);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//On attend 2 secondes
|
|
|
|
sleep(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cette fonction permet d'envoyer par mail les sms à transférer
|
|
|
|
*/
|
2019-10-29 18:36:25 +01:00
|
|
|
public function sendTransfers()
|
2019-10-29 14:57:13 +01:00
|
|
|
{
|
2019-10-30 00:17:10 +01:00
|
|
|
if (!RASPISms_SETTINGS_TRANSFER) {
|
|
|
|
echo "Le transfer de Sms est désactivé ! \n";
|
2019-10-29 14:57:13 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
global $db;
|
|
|
|
$transfers = $db->getFromTableWhere('transfers', ['progress' => false]);
|
|
|
|
|
|
|
|
$ids_transfers = [];
|
|
|
|
$ids_receiveds = [];
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($transfers as $transfer) {
|
2019-10-29 14:57:13 +01:00
|
|
|
$ids_transfers[] = $transfer['id'];
|
|
|
|
$ids_receiveds[] = $transfer['id_received'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->updateProgressTransfersIn($ids_transfers, true);
|
|
|
|
|
|
|
|
$receiveds = $db->getReceivedIn($ids_receiveds);
|
|
|
|
|
|
|
|
$users = $db->getFromTableWhere('users', ['transfer' => true]);
|
|
|
|
|
2019-10-29 18:36:25 +01:00
|
|
|
foreach ($users as $user) {
|
|
|
|
foreach ($receiveds as $received) {
|
2019-10-30 00:17:10 +01:00
|
|
|
echo "Transfer d'un Sms du " . $received['send_by'] . " à l'email " . $user['email'];
|
2019-10-29 14:57:13 +01:00
|
|
|
$to = $user['email'];
|
2019-10-30 00:17:10 +01:00
|
|
|
$subject = '[RaspiSms] - Transfert d\'un Sms du ' . $received['send_by'];
|
|
|
|
$message = "Le numéro " . $received['send_by'] . " vous a envoyé un Sms : \n" . $received['content'];
|
2019-10-29 14:57:13 +01:00
|
|
|
|
|
|
|
$ok = mail($to, $subject, $message);
|
|
|
|
|
|
|
|
echo " ... " . ($ok ? 'OK' : 'KO') . "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$db->deleteTransfersIn($ids_transfers);
|
|
|
|
}
|
|
|
|
}
|