I was doing some reading recently and came across a few people doing some rolodex’s for asterisk to re-write the callerid name. Some of them require mysql, some postgresql, some use the asterisk internal database. I wasn’t sure I liked what was available, so with the help of a friend we worked up this little script to modify/add/delete entries into the callerid database on asterisk. It’s a very simple interface, but it works. And now I get some nicer messages on my XMPP client when friends call with blocked caller-name. Eventually 411 reverse will be added, but that’s for a later time.
Prerequisites:
- Asterisk PBX (Tested on Asterisk 1.4)
- PHP (Tested on PHP 4/5)
- Apache Web Server 2.0
- Unixish Operating System, In this case Gentoo Linux 2006.1
STEP.1 Intro:
First, ensure that you have a working Asterisk box. I won’t go into this here, as that’s for another howto.
Note that we don’t enable any directory protection or anything on this, as it’s running on our internal LAN. If you will have this open to the internet, you will most likely want to password protect it with htaccess or similar methods.
STEP.2 Create Directory:
We assume you are using the default asterisk and apache installation on gentoo. You will have to modify your paths accordingly if you have a different setup.
# mkdir -p /var/www/localhost/htdocs/rolodex/
STEP.3 Create Files:
Create the following three files with this information in them. Or, Download all these files right here rolodex.zip
STEP.3A – INDEX.PHP:
# cd /var/www/localhost/htdocs/rolodex/
# nano index.php
<?php
session_start();
?>
<html>
<head>
<title>Flewid Productions :: CallerID Modification Tool</title>
</head>
<body>
<h1>Add a New Number:</h1>
<form name=”EntryForm” action=”cid-store.php” method=POST>
<table cellpadding=2>
<tr>
<td>Phone number:</td>
<td><input name=”PhoneNumber” size=30></td>
</tr>
<tr>
<td>Name:</td>
<td><input name=”PhoneName” size=30></td>
</tr>
<tr>
<td></td>
<td><div align=right>
<input type=submit value=”Save”>
</div></td>
</tr>
</table>
</form>
<h1>Current Number Listing:</h1>
<!–
<?php
set_time_limit(5);
system(“/usr/sbin/asterisk -rx \”database show cidname\” > /tmp/asterisk_cid-list.txt”);
$fd=fopen(“/tmp/asterisk_cid-list.txt”,”r”);
while ($line=fgets($fd,1000))
{
$alltext.=$line;
}
fclose ($fd);
print str_replace(“”, “<br>”, $alltext);
?>
–>
<?php
set_time_limit(0);
ignore_user_abort(true);
$tempfile = ‘/tmp/asterisk_cid-list.txt’;
$mess = file($tempfile);
$result = array();
foreach ($mess as $line) {
$temp = explode(‘:’, $line);
$phone_number = str_replace(‘/cidname/’, ”, trim($temp[0]));
$name = $temp[1];
$result[] = array(‘phone’ => $phone_number, ‘name’ => $name);
}
for ($i=0; $i<count($result); $i++) {
echo $result[$i][‘name’] . ‘ – ‘ . $result[$i][‘phone’] . ‘ ‘ . ‘[<a href=”cid-delete.php?who=’ . $i . ‘” title=”delete”>x</a>]<br />’;
}
$_SESSION[‘list’] = $result;
?>
</body>
</html>
STEP.3B – CID-STORE.PHP:
# cd /var/www/localhost/htdocs/rolodex/
# nano cid-store.php
<HTML>
<HEAD>
<TITLE>Storing Asterisk CID data</TITLE>
</HEAD>
<BODY>
<h1>Asterisk phone book</h1>
<?php
set_time_limit(5);
$PhoneNumber = $_POST[‘PhoneNumber’];
$PhoneName = $_POST[‘PhoneName’];
if ($PhoneNumber <> “” && $PhoneName <> “”) {
// Note: this PHP script runs as user “apache”!
system(“whoami > /tmp/info”);
system(“/usr/sbin/asterisk -rx ” . escapeshellarg(“database put cidname $PhoneNumber \”$PhoneName\””) . ” &> /tmp/error”);
print “Successfully stored <b>$PhoneNumber</b> as <b>$PhoneName</b>.”;
} else {
print “Please enter both phone number and name!”;
}
?>
<br /><br />
<a href=”index.php”><< Back</a>
</BODY>
</HTML>
STEP.3C – CID-DELETE.PHP:
# cd /var/www/localhost/htdocs/rolodex/
# nano cid-delete.php
<?php
session_start();
?>
<HTML>
<HEAD>
<TITLE>Storing Asterisk CID data</TITLE>
</HEAD>
<BODY>
<h1>Asterisk phone book</h1>
<?php
set_time_limit(5);
$whotodelete = $_SESSION[‘list’][$_GET[‘who’]];
$PhoneNumber = $whotodelete[‘phone’];
$PhoneName = $whotodelete[‘name’];
if ($PhoneNumber <> “”) {
// Note: this PHP script runs as user “asterisk”!
system(“/usr/sbin/asterisk -rx ” . escapeshellarg(“database del cidname $PhoneNumber”) . ” &> /tmp/error”);
print “Successfully deleted <b>$PhoneNumber</b> belonging to <b>$PhoneName</b>.”;
} else {
print “Something is wrong. It didn’t delete it!”;
}
?>
<br /><br />
<a href=”index.php”><< Back</a>
</BODY>
</HTML>
STEP.4 – Apache Runing as Asterisk:
As any security concious Asterisk Administrator is doing, we’re Running Asterisk as it’s own user. Our user is called.. well, “asterisk”. We have also created an “asterisk” group for it to belong to as well. So, we have to change apache to run as this as well. Note the paths may be different if you’re not running Gentoo.
# nano /etc/apache/httpd.conf
User asterisk
Group asterisk
<save the file>
# /etc/init.d/apache2 restart
STEP.5 – Testing it out:
Open up http://localhost/rolodex/
in your browser, and you should be confronted with a basic interface for adding numbers associated with names to the Asterisk Internal Database.
Congratulations!
If it’s not working, double check all your permissions and locations of files. Especially that /tmp
is writable by asterisk for the callerid output file.