Asterisk simple php lookup up callerid name from Horde Turba

I have developed this script based on the simple PHP callerid lookup setup. The main difference is that I wanted to lookup the callerid in the Horde Turba address book - so that users could easily add records to the address book through the Horde interface (for those not familiar with Horde, it is a great, free suite of web-based groupware applications - such as email client, calendar client, address book etc)

I've also developed using Asterisk AGI Asterisk abbreviated dialing using Horde Turba

I've started with the Asterisk simple php lookup mysql database to set callerid name - but the crucial difference is that I have my Horde installation running on PostgreSQL:

Remember to amend the Horde PostgreSQL database and grant sufficient permissions to whatever user the php script will be using to access the database ("asterisk" user in my case).

First extensions.conf:

exten => s,n,AGI(/var/lib/asterisk/agi-bin/lookupname.php)
exten => s,n,Set(CALLERID(name)=${lookupcid})
exten => s,n,Dial(SIP/20&SIP/21&SIP/22,,tc)
exten => s,n,HangUp()

Then the php script (I keep mine, as you probably gathered from above, at /var/lib/asterisk/agi-bin/lookupname.php):

require 'phpagi.php';

$agi = new AGI();

//remove any non numeric characters

/* Connect to Horde database */

$db = 'horde';
$dbuser = 'asterisk';
$dbpass = '';
$dbhost = 'localhost';

$dbconn = pg_connect("host=" . $dbhost . " " . "dbname=" . $db . " " . 
    "user=" . $dbuser . " " . "password=" . $dbpass);
if (!$dbconn) {
    echo "Can't connect to database.\n";

$sql = "SELECT object_firstname, object_lastname FROM turba_objects WHERE object_cellphone " .
    "LIKE '%$no%' OR object_workphone LIKE '%$no%' OR object_homephone LIKE '%$no%' LIMIT 1";

$result = pg_query($dbconn, $sql);
if (!$result) {
    echo "An error occurred running the query.\n";

//if found number and number greater than 4 digits to avoid 3 digit internal extensions
if (pg_num_rows($result)==1&&strlen($no)>4){
    $row = pg_fetch_array($result);
    if ($row[object_firstname]) $name .= $row[object_firstname]." ";
    if ($row[object_lastname]) $name .= $row[object_lastname];

//else set calleridname to callerid number
else $name=$agi->request[agi_callerid];

$agi->set_variable("lookupcid", $name);



Created by: xj25vm, Last modification: Mon 20 of May, 2013 (20:48 UTC)
Please update this page with new information, just login and click on the "Edit" or "Discussion" tab. Get a free login here: Register Thanks! - Find us on Google+