Skip to content

Instantly share code, notes, and snippets.

@lgaetz
Last active January 9, 2022 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lgaetz/2fdcb8b8a2e8bca5351162bbff23e7e6 to your computer and use it in GitHub Desktop.
Save lgaetz/2fdcb8b8a2e8bca5351162bbff23e7e6 to your computer and use it in GitHub Desktop.
<?php
/**** **** **** **** **** **** **** **** **** **** **** **** **** ****
*
* License: GNU GPL/2+
*
* /var/www/html/admin/modules/superfecta/sources/source-Trunk_Provided.module
*
* History: 2012-09-18 update by lgaetz
* 2021-12-20 update by tm1000
* 2022-01-04 add SPAM flagging based on keyword
* 2022-01-09 add SPAM flagging based on CNAM regex
**** **** **** **** **** **** **** **** **** **** **** **** **** ****/
class Trunk_Provided extends superfecta_base {
public $description = "Locates the Caller ID Name provided by the trunk, and then decides based on a list of key words if the provided name should be used.";
public $version_requirement = "2.11";
public $source_param = array(
'Discard_Numeric' => array(
'description' => 'Enable this setting to discard trunk provided CNAM that is all digits.',
'type' => 'checkbox',
'default' => "checked",
),
'CNAM_Lookup' => array(
'description' => 'If enabled, Superfecta will use trunk provided CNAM, otherwise it is ignored.',
'type' => 'checkbox',
'default' => "checked",
),
'Spam_Scoring' => array(
'description' => 'If enabled, Superfecta will attempt to match the trunk provided Caller ID Name against the list of spam regular expressions to determine if call is spam',
'type' => 'checkbox',
'default' => "checked",
),
'Ignore_Keywords' => array(
'description' => 'If the trunk provided caller id name includes any of these keywords, the trunk provided value will be ignored and other sources will be used to find the value.<br> Seperate keywords with commas.',
'type' => 'textarea',
'default' => 'unknown, toll free, unlisted, (N/A)',
),
'SPAM_Regex' => array(
'description' => 'If the trunk provided caller ID name matches any of the following regular expressions, the call will be flagged as SPAM. Each regular expression must be on a new line, must be PCRE format and must include delimiters.',
'type' => 'textarea',
'default' => null,
),
);
function get_caller_id($thenumber, $run_param=array()) {
$this->DebugPrint("Looking for Trunk Provided Caller ID ... ");
// Don't do anything when in debug test mode
// disabled for testing
// if ($this->debug) {
// $this->DebugPrint("We're in Debug/Test mode, this module only runs on live calls.");
// return null;
// }
$this->DebugPrint("cnam ".$run_param['CNAM_Lookup']);
$this->DebugPrint("spam ".$run_param['Spam_Scoring']);
$this->DebugPrint("numeric ".$run_param['Discard_Numeric']);
$key_words = array();
$temp_array = explode(',', (isset($run_param['Ignore_Keywords']) ? $run_param['Ignore_Keywords'] : $this->source_param['Ignore_Keywords']['default']));
foreach ($temp_array as $val) {
$key_words[] = trim($val);
}
$SPAM_Regex = array();
$temp_array = explode(PHP_EOL, (isset($run_param['SPAM_Regex']) ? $run_param['SPAM_Regex'] : $this->source_param['SPAM_Regex']['default']));
foreach ($temp_array as $val) {
if (trim($val) != "") {
$SPAM_Regex[] = trim($val);
}
}
$provided_caller_id = str_replace('+','',$this->trunk_info['calleridname']);
$thenumber_orig = $this->trunk_info['callerid'];
$provided_caller_id = "Koch Industries";
if ($provided_caller_id == $thenumber_orig) {
$this->DebugPrint("CID name is the same as CID number..skipping");
return null;
}
if ($provided_caller_id == '') {
$this->DebugPrint("not found");
return null;
} else {
$this->DebugPrint("found value of $provided_caller_id ... ");
// spam check
if ($run_param['Spam_Scoring']=="on") {
$this->DebugPrint("Checking SPAM regexs ...");
foreach($SPAM_Regex as $val) {
if (@preg_match($val, '') === false) {
$this->DebugPrint("Skipping invalid regex: ".$val);
} else {
$match=null;
$match=preg_match($val,$provided_caller_id);
if ($match) {
$this->DebugPrint("MATCH against regex: ".$val);
$this->DebugPrint("Flagging call as SPAM!");
$this->spam = true;
break;
} else {
$this->DebugPrint("No match against regex: ".$val);
}
}
}
}
// CNAM check
if ($run_param['CNAM_Lookup']=="on") {
$test_string = str_ireplace($key_words, '', $provided_caller_id);
// $run_param['Discard_Numeric'] = isset($run_param['Discard_Numeric']) ? $run_param['Discard_Numeric'] : $this->source_param['Discard_Numeric']['default'];
if (($run_param['Discard_Numeric'] == 'on') && (is_numeric($provided_caller_id))) {
$this->DebugPrint("CID is all numeric - discarded");
return null;
}
if ($test_string == $provided_caller_id) {
$this->DebugPrint("CNAM determined good.");
return($provided_caller_id);
} else {
$this->DebugPrint("CNAM contains flagged key words, returning nothing");
return null;
}
}
}
}
}
@lgaetz
Copy link
Author

lgaetz commented Jan 5, 2022

2022-01-04 not useful as it is , the spam list is just a basic keyword match. Needs to accept a list of regexs and flag calls based on cnam patterns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment