From 57e0905a363ea783f762ef1b7c0f2424b436b533 Mon Sep 17 00:00:00 2001 From: The-Operator Date: Tue, 8 May 2018 16:50:57 +0200 Subject: [PATCH] Enhance Call Blocking [master] (#2988) * Added support for regular expressions in the numbers to be blocked. Reduced the caching timeout, as the counter is only incremented when a call is blocked and the number is not cached. * Added default sort order. * Added option to switch on/off use of regular expression matching for pgsql and mysql. * Added support for sqlite and updated default number match to "LIKE" as suggested by MafooUK on IRC. * Selection of behavior (default [=], regex or like matching) is now done via the variable call_block_matching = regex|like|EMPTY - type text - in the Call Block section of Default Settings. * Updated comment to match new matching. --- app/call_block/app_languages.php | 8 +++--- app/call_block/call_block.php | 8 ++++-- .../install/scripts/app/call_block/index.lua | 25 +++++++++++++++++-- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/app/call_block/app_languages.php b/app/call_block/app_languages.php index 4cf446fd07..0c306aa30a 100644 --- a/app/call_block/app_languages.php +++ b/app/call_block/app_languages.php @@ -521,11 +521,11 @@ $text['label-action']['ru-ru'] = "Действие"; $text['label-action']['sv-se'] = "Åtgärd"; $text['label-action']['uk-ua'] = "Дія"; -$text['description-number']['en-us'] = "Enter the exact number."; +$text['description-number']['en-us'] = "Enter the number. This may be a regex expression or SQL match depending on the setting of the call_block_matching setting."; $text['description-number']['ar-eg'] = ""; -$text['description-number']['de-at'] = "Geben Sie die genaue Nummer an."; //copied from de-de -$text['description-number']['de-ch'] = "Geben Sie die genaue Nummer an."; //copied from de-de -$text['description-number']['de-de'] = "Geben Sie die genaue Nummer an."; +$text['description-number']['de-at'] = "Geben Sie die Nummer an. Es kann sich hierbei um eine regulären Ausdruck oder einen SQL Vergleich handeln, je nach Wert der call_block_matching Einstellung."; //copied from de-de +$text['description-number']['de-ch'] = "Geben Sie die Nummer an. Es kann sich hierbei um eine regulären Ausdruck oder einen SQL Vergleich handeln, je nach Wert der call_block_matching Einstellung."; //copied from de-de +$text['description-number']['de-de'] = "Geben Sie die Nummer an. Es kann sich hierbei um eine regulären Ausdruck oder einen SQL Vergleich handeln, je nach Wert der call_block_matching Einstellung."; $text['description-number']['es-cl'] = "Ingrese el nú exacto."; $text['description-number']['es-mx'] = "Ingrese el nú exacto."; //copied from es-cl $text['description-number']['fr-ca'] = "Entrer le numéro exact."; //copied from fr-fr diff --git a/app/call_block/call_block.php b/app/call_block/call_block.php index 517b006bd1..5b31876399 100644 --- a/app/call_block/call_block.php +++ b/app/call_block/call_block.php @@ -90,7 +90,11 @@ require_once "resources/require.php"; //get the list $sql = "select * from v_call_block "; $sql .= "where domain_uuid = '".$_SESSION['domain_uuid']."' "; - if (strlen($order_by)> 0) { $sql .= "order by $order_by $order "; } + if (strlen($order_by)> 0) { + $sql .= "order by $order_by $order "; + } else { + $sql .= "order by call_block_number asc "; + } $sql .= " limit $rows_per_page offset $offset "; $prep_statement = $db->prepare(check_sql($sql)); $prep_statement->execute(); @@ -177,4 +181,4 @@ require_once "resources/require.php"; //include the footer require_once "resources/footer.php"; -?> \ No newline at end of file +?> diff --git a/resources/install/scripts/app/call_block/index.lua b/resources/install/scripts/app/call_block/index.lua index b2d6723225..eba554f766 100644 --- a/resources/install/scripts/app/call_block/index.lua +++ b/resources/install/scripts/app/call_block/index.lua @@ -37,11 +37,12 @@ This method causes the script to get its manadatory arguments directly from the 12 Jun, 2013: update the database connection, change table name from v_callblock to v_call_block 14 Jun, 2013: Change Voicemail option to use Transfer, avoids mod_voicemail dependency 27 Sep, 2013: Changed the name of the fields to conform with the table name + 12 Feb, 2018: Added support for regular expressions and SQL "like" matching on the phone number ]] --set defaults expire = {} - expire["call_block"] = "3600"; + expire["call_block"] = "60"; source = ""; -- Command line parameters @@ -62,6 +63,8 @@ This method causes the script to get its manadatory arguments directly from the local sql = nil --define the functions + local Settings = require "resources.functions.lazy_settings" + local Database = require "resources.functions.database" require "resources.functions.trim"; --define the logger function @@ -78,6 +81,12 @@ This method causes the script to get its manadatory arguments directly from the -- ensure that we have a fresh status on exit session:setVariable("call_block", "") +-- get the configuration variables from the DB + local db = dbh or Database.new('system') + local settings = Settings.new(db, domain_name, domain_uuid) + local call_block_matching = settings:get('call block', 'call_block_matching', 'text'); + + --send to the log logger("D", "NOTICE", "params are: " .. string.format("'%s', '%s', '%s', '%s'", params["cid_num"], params["cid_name"], params["userid"], params["domain_name"])); @@ -104,7 +113,19 @@ This method causes the script to get its manadatory arguments directly from the --check if the the call block is blocked sql = "SELECT * FROM v_call_block as c " sql = sql .. "JOIN v_domains as d ON c.domain_uuid=d.domain_uuid " - sql = sql .. "WHERE c.call_block_number = :cid_num AND d.domain_name = :domain_name " + if ((database["type"] == "pgsql") and (call_block_matching == "regex")) then + logger("W", "NOTICE", "call_block using regex match on cid_num") + sql = sql .. "WHERE :cid_num ~ c.call_block_number AND d.domain_name = :domain_name " + elseif (((database["type"] == "mysql") or (database["type"] == "sqlite")) and (call_block_matching == "regex")) then + logger("W", "NOTICE", "call_block using regex match on cid_num") + sql = sql .. "WHERE :cid_num REGEXP c.call_block_number AND d.domain_name = :domain_name " + elseif call_block_matching == "like" then + logger("W", "NOTICE", "call_block using like match on cid_num") + sql = sql .. "WHERE :cid_num LIKE c.call_block_number AND d.domain_name = :domain_name " + else + logger("W", "NOTICE", "call_block using exact match on cid_num") + sql = sql .. "WHERE :cid_num = c.call_block_number AND d.domain_name = :domain_name " + end dbh:query(sql, params, function(rows) found_cid_num = rows["call_block_number"]; found_uuid = rows["call_block_uuid"];