Dynamic Phonebook for GXP-2000
- EDIT 07/27/08: I have updated the script to deal with single word names so that something like "Lobby" displays properly on the GXP2000 screen without weird characters.***
For anyone interested, below is a script to generate the xml phonebook for a GXP-2000 based on the users table of the asterisk database. In other words, this dynamically creates the addressbook based on configured extensions (or users if you're in devicesandusers mode) in Trixbox (or FreePBX) when the phone requests the data. With the refresh set in the GXP config, it will automatically update itself with the latest info.
I modified Shane Steinbeck's code found here to get this working:
http://www.voip-info.org/wiki/view/GXP-2000+XML+Phonebook
My update assumes that you're using the default mysql user password (change it in the script otherwise). You will also need to be running a more recent firmware (not sure when xml phonebook and refresh were implemented, maybe 1.1.1.17 per the script notes. I'm running 1.1.6.16). Finally, you need to put the script in a web accessible directory and then set the GXP-2000 to use http for the phone book. I created a directory called /var/www/html/grandstream, put the script there named gs_phonebook.php and set the owner/group to asterisk and the permissions to 755.
To get GXP-2000 to use the script
Log into the GXP-2000 web interface and change:
Enable Phonebook XML Download: YES, HTTP.
Phonebook XML Server Path to point to the .php on your webserver (i.e. 192.168.1.1/grandstream/gs_phonebook.php).
Remove Manually-edited entries on Download: NO
Reboot.
If you want to do this via tftp, the snippet by Mike near the end can be un-commented to generate the xml file to the tftp directory and the script run as a cron job.
One other thing to note is that the script assumes you have entered names in FreePBX as First Last (ie - John Smith). It splits the name based on the first space it finds after the first word so that names such as Dick Van Patton are also parsed correctly.
<?php
/************************************************
version: 1.0
Date: 07-23-2008
FreePBX/Trixbox update modifications by fredo
Original script by: Shane Steinbeck http://www.steinbeckconsulting.com
Description: Realtime XML phonebook from MySQL database for Grandstream GXP-2000 firmware 1.1.1.17
************************************************/
header("Content-type: text/xml");
$host = "localhost";
$user = "asteriskuser";
$pass = "amp109";
$database = "asterisk";
$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");
$query = "SELECT name, extension FROM users ORDER BY name ASC";
$resultID = mysql_query($query, $linkID) or die("Data not found.");
$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "\n";
for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
$row = mysql_fetch_assoc($resultID);
$namedata = explode(' ', $row['name'], 2);
if($namedata[1] != "") {
$lastname = $namedata[1];
$firstname = $namedata[0];
} else {
$lastname = $namedata[0];
$firstname = "";
}
$xml_output .= "\t\n";
$xml_output .= "\t\t" . $lastname . "\n";
$xml_output .= "\t\t" . $firstname . "\n";
$xml_output .= "\t\t\t\n\t\t\t\t" . $row['extension'] . "\n";
$xml_output .= "\t\t\t\t" . 0 . "\n";
$xml_output .= "\t\t\t\n";
$xml_output .= "\t\n";
}
$xml_output .= "";
/***********************************************
Author: Mike
Date: 10/03/2006
This snipplet will save your xml file to the server for easier updating of your phone.
To facilitate automatic updates, add a cron job to run this file at your desired interval (i.e. hourly).
************************************************/
//$fp = fopen('/tftpboot/gs_phonebook.xml', 'wb');
//fwrite($fp, $xml_output);
//fclose($fp);
print($xml_output);
?>


Member Since:
2006-06-08