FreePBX and OpenCNAM
OpenCNAM is a great international service which allows you, in real time, to lookup numbers and receive in response who it thinks it is. Or at the very least the type of number it is.
While it can easily be integrated into FreePBX as one of the standard CallerID applications, it doesn't tend to work all too well. The main obstacle being that FreePBX does not reformat the numbers into the E164 format OpenCNAM is expecting. So if you have incoming services which don't use E164, as, they tend not to do, you're up shit creek.
Therefore instead we can create our own PHP script to perform this. With the added benefit that was can use our own CallerID database too, for caching etc. FreePBX does support direct MySQL queries, however in using a script we can explore many different purposes and keep the logic in the same place.
OpenCNAM does unfortunately charge per lookup, however the fee is extremely low. And we do use caching to prevent unnecessary repeats, by saving all values to an SQL database.
To replicate similar functionality, first create an appropriate database and table.
CREATE TABLE `CID` (
`idx` int(11) NOT NULL AUTO_INCREMENT,
`number` varchar(15) NOT NULL,
`name` varchar(45) DEFAULT NULL,
`source` varchar(10) DEFAULT NULL,
`modified` date DEFAULT NULL,
PRIMARY KEY (`idx`),
UNIQUE KEY `number_UNIQUE` (`number`)
) ENGINE=InnoDB AUTO_INCREMENT=0 DEFAULT CHARSET=latin1;
And add the following script to a webserver. Saving as "num.php". Bare in mind I have the line $num = "+44".substr( $num, 1, strlen($num) );
where the +44 indicates the UK dialing code. Yours may vary.
$num = $_GET["num"];
$num = preg_replace('/\D/', '', $num); //remove all non-digits
if ($num[0] == "0" && strlen($num) > 6) {
$num = "+44".substr( $num, 1, strlen($num) );
} else if ($num[0] != "0" && strlen($num) > 6) {
$num = "+". $num;
}
$sqlhost="yourhost";
$sqluser="yousqluser";
$sqlpass="yoursqlpass";
$sqldbas="sqldbname";
$conn = new mysqli($sqlhost, $sqluser, $sqlpass, $sqldbas);
$qCheck = $conn->prepare("SELECT name FROM CID WHERE number=?");
$qStore = $conn->prepare("INSERT INTO CID (number,name,source,modified)
SELECT ?,?,'CNAM','".date('Y-m-d')."' FROM DUAL
WHERE NOT EXISTS (SELECT number FROM CID WHERE number=?);");
$myfile = fopen("./lookup_log.txt", "a") or die("Unable to open file!");
fwrite($myfile, "scriptlaunched ".$num);
fclose($myfile);
$opts = array(
'http'=>array(
'method'=>"GET",
'header'=>"Accept: application/json\r\n"
)
);
$cacheNam = checkLocal($num, $qCheck, $conn);
if (strlen($cacheNam) < 3) {
$context = stream_context_create($opts);
$ocnam = file_get_contents('https://api.opencnam.com/v3/phone/'.$num.'?account_sid=OPENCNUMAPIKEY', false, $context);
$cnam_json = json_decode($ocnam,true);
echo $cnam_json['name'];
updateLocal($num, $cnam_json['name'], $qStore, $conn);
} else {
echo $cacheNam;
}
$conn->close();
function checkLocal($num, $qCheck, $conn) { //See if we know the number already
$name="0";
$qCheck -> bind_param("s", $num);
$qCheck ->execute();
$qCheck ->bind_result($name);
$qCheck ->fetch();
return $name;
}
function updateLocal($num, $name, $qStore, $conn) {
$qStore -> bind_param("sss", $num, $name, $num);
$qStore ->execute();
}
Then we can go into FreePBX and add the new CID source;
Now when a caller rings, in the SIP application, it will display the name of the caller from OpenCNAM or your cache.
You can also use the script to augment your own applications. For example, I have a CDR screen which will use the script to lookup numbers, and allow me to add/edit them as needed.