From 6e6c51a9eec127911dfce4907a38f05792643680 Mon Sep 17 00:00:00 2001 From: osaajani <> Date: Sun, 25 Feb 2024 11:10:10 +0100 Subject: [PATCH] Add support for automatic response to SMS stop --- VERSION | 2 +- controllers/internals/Received.php | 36 +++++++++++++++++++------ controllers/internals/Scheduled.php | 4 +-- controllers/internals/SmsStop.php | 3 ++- env.php.dist | 2 ++ models/SmsStop.php | 2 ++ templates/phone/add.php | 10 +++++++ templates/phone/edit.php | 10 +++++++ templates/setting/show.php | 41 +++++++++++++++++++++++++++++ 9 files changed, 98 insertions(+), 12 deletions(-) diff --git a/VERSION b/VERSION index 5b3a517e..5f22788f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v3.8.4 +v3.9.0 diff --git a/controllers/internals/Received.php b/controllers/internals/Received.php index 3e6218ee..8c96abfa 100644 --- a/controllers/internals/Received.php +++ b/controllers/internals/Received.php @@ -11,6 +11,8 @@ namespace controllers\internals; +use Exception; + class Received extends StandardController { protected $model; @@ -88,14 +90,6 @@ public function create(int $id_user, int $id_phone, $at, string $text, string $o return false; } - //Check if the received message is a SMS STOP and we must register it - $internal_smsstop = new SmsStop($this->bdd); - $is_stop = $internal_smsstop->check_for_stop($received['text']); - if ($is_stop) - { - $internal_smsstop->create($id_user, $origin); - } - //Link medias $internal_media = new Media($this->bdd); foreach ($media_ids as $media_id) @@ -116,6 +110,32 @@ public function create(int $id_user, int $id_phone, $at, string $text, string $o return false; } + //Check if the received message is a SMS STOP and we must register it + $internal_smsstop = new SmsStop($this->bdd); + $is_stop = $internal_smsstop->check_for_stop($received['text']); + if ($is_stop) + { + $stop_exists = (bool) $internal_smsstop->get_by_number_for_user($id_user, $origin); + if ($stop_exists) + { + return $id_received; + } + + $internal_smsstop->create($id_user, $origin); + + //If stop response enabled, respond to user + //(this will happen only for first stop, any further stop will not trigger responses) + $internal_setting = new Setting($this->bdd); + $user_settings = $internal_setting->gets_for_user($id_user); + + if ((int) ($user_settings['smsstop_respond'] ?? false)) + { + $response = $user_settings['smsstop_response']; + $internal_scheduled = new Scheduled($this->bdd); + $internal_scheduled->create($id_user, (new \DateTime())->format('Y-m-d H:i:s'), $response, $id_phone, null, false, false, \models\SmsStop::SMS_STOP_TAG, [['number' => $origin, 'data' => '[]']]); + } + } + return $id_received; } diff --git a/controllers/internals/Scheduled.php b/controllers/internals/Scheduled.php index b4ec0d6e..a02e040e 100644 --- a/controllers/internals/Scheduled.php +++ b/controllers/internals/Scheduled.php @@ -602,8 +602,8 @@ public function get_smss_to_send() continue; } - //Remove messages to smsstops numbers - if (($users_smsstops[$id_user] ?? false) && in_array($target['number'], $users_smsstops[$id_user])) + //Remove messages to smsstops numbers if not with tag SMS_STOP + if ($scheduled['tag'] != \models\SmsStop::SMS_STOP_TAG && ($users_smsstops[$id_user] ?? false) && in_array($target['number'], $users_smsstops[$id_user])) { unset($targets[$key]); continue; diff --git a/controllers/internals/SmsStop.php b/controllers/internals/SmsStop.php index 29d310ae..00e88659 100644 --- a/controllers/internals/SmsStop.php +++ b/controllers/internals/SmsStop.php @@ -73,7 +73,8 @@ public function get_by_number_for_user(int $id_user, string $number) */ public function check_for_stop(string $str) { - return 'stop' == trim(mb_strtolower($str)); + $str = trim(mb_strtolower($str)); + return 'stop' == $str || 'stop sms' == $str; } /** diff --git a/env.php.dist b/env.php.dist index dcae8727..1ce52ac5 100644 --- a/env.php.dist +++ b/env.php.dist @@ -89,6 +89,8 @@ 'phone_limit' => 0, 'phone_priority' => 0, 'shorten_url' => 0, + 'smsstop_respond' => 1, + 'smsstop_response' => 'Demande prise en compte, vous ne recevrez plus de messages.', ], ]; diff --git a/models/SmsStop.php b/models/SmsStop.php index 65cc522d..18b7db7c 100644 --- a/models/SmsStop.php +++ b/models/SmsStop.php @@ -13,6 +13,8 @@ class SmsStop extends StandardModel { + const SMS_STOP_TAG = 'SMS_STOP'; + /** * Return a smsstop by his number and user. * diff --git a/templates/phone/add.php b/templates/phone/add.php index b7fb0ee3..c071d6ff 100644 --- a/templates/phone/add.php +++ b/templates/phone/add.php @@ -161,6 +161,16 @@ function change_adapter () '' + ''; } + else if (field.type == 'textarea') + { + html += '
' + field.description + '
' + + '' + field.description + '
' + + '