Do you run the Elastix PBX management environment to Asterisk? Do you use Grandstream IP Phones? If you answered yes to both of these questions, then this might be helpful to you.
Grandstream GXP21xx/GXP14xx/GXP116x IP phones (and possibly others) can be configured to automatically download an XML formatted phone book file. The Elastix interface already has a place for naming the users, wouldn’t it be great to use that data for making an always up-to-date phone book file? That is the idea with this script. It was written for use with Elastix version 2.0, but will probably work with others (in the 2.x branch), and maybe FreePBX as well, I haven’t really tested.
To “install” the script simply put it in the root of your Elastix web directory space.
The format of the XML output is something like this:
<AddressBook>
<Contact>
</Lastname>Test User<LastName/>
<FirstName></FirstName>
<Phone>
<phonenumber>1100</phonenumber>
<accountindex>1</accountindex>
<downloaded>0</downloaded>
</Phone>
</Contact>
</AddressBook>
Two versions of the script are presented, you only need one. The first script uses functions included inside of various files that should be present (AFAIK) in your Elastix web root. It is slower and will undoubtedly use more resources during execution. It may also be more flexible and resilient in that it will always use the latest version of the functions available to it. This is a double-edged sword of course, and might end up breaking it as well. Enter the second script, which uses just the applicable parts of the code from those aforementioned functions without needing to include any files. This script will probably work provided the database table stays the same, but your mileage may vary.
You can download both scripts from here: Download
The shorter version:
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
+———————————————————————-+
| Grandstream GXP21xx/GXP14xx/GXP116x IP Phone XML Phone Book Creator |
| For Elastix version 2.0 (maybe others) http://www.elastix.org |
+———————————————————————-+
| Made with recycled code, applicable rights |
| Palosanto Solutions S. A., Coalescent Systems Inc, FreePBX, et al. |
+———————————————————————-+
| The contents of this file are subject to the General Public License |
| (GPL) Version 2 (the "License"); you may not use this file except in |
| compliance with the License. You may obtain a copy of the License at |
| http://www.opensource.org/licenses/gpl-license.php |
| |
| Software distributed under the License is distributed on an "AS IS" |
| basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| the License for the specific language governing rights and |
| limitations under the License. |
+———————————————————————-+
*/
header("Content-Type: text/xml");
require_once(‘admin/functions.inc.php’);
require_once(‘admin/modules/core/functions.inc.php’);
require_once(‘admin/common/php-asmanager.php’);
require_once(‘DB.php’); //PEAR must be installed (why?)
$db_user = $amp_conf["AMPDBUSER"];
$db_pass = $amp_conf["AMPDBPASS"];
$db_host = $amp_conf["AMPDBHOST"];
$db_name = $amp_conf["AMPDBNAME"];
$datasource = ‘mysql://’.$db_user.‘:’.$db_pass.‘@’.$db_host.‘/’.$db_name;
$db = DB::connect($datasource);
$extensions = core_users_list();
echo "<?xml version="1.0" encoding="UTF–8"?>\n";
echo "<AddressBook>\n";
$index = 0;
if (isset($extensions)) {
foreach ($extensions as $key=>$extension) {
$index= $index + 1;
echo "\n\n\t<Contact>";
echo "\n\t\t<LastName>" . $extension[1] . "</LastName>";
echo "\n\t\t<FirstName></FirstName>";
echo "\n\t\t<Phone>";
echo "\n\t\t\t<phonenumber>" . $extension[0] . "</phonenumber>";
echo "\n\t\t\t<accountindex>" . $index . "</accountindex>";
echo "\n\t\t</Phone>";
echo "\n\t</Contact>\n";
}
}
echo "</AddressBook>\n";
?>
The more monolithic version:
/* vim: set expandtab tabstop=4 softtabstop=4 shiftwidth=4:
+———————————————————————-+
| Grandstream GXP21xx/GXP14xx/GXP116x IP Phone XML Phone Book Creator |
| For Elastix version 2.0 (maybe others) http://www.elastix.org |
+———————————————————————-+
| Made from 99% recycled code, applicable rights |
| Palosanto Solutions S. A., Coalescent Systems Inc, FreePBX, et al. |
+———————————————————————-+
| The contents of this file are subject to the General Public License |
| (GPL) Version 2 (the "License"); you may not use this file except in |
| compliance with the License. You may obtain a copy of the License at |
| http://www.opensource.org/licenses/gpl-license.php |
| |
| Software distributed under the License is distributed on an "AS IS" |
| basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See |
| the License for the specific language governing rights and |
| limitations under the License. |
+———————————————————————-+
*/
header("Content-Type: text/xml");
define("AMP_CONF", "/etc/amportal.conf");
$file = file(AMP_CONF);
if (is_array($file)) {
foreach ($file as $line) {
if (preg_match("/^\s*([a-zA-Z0-9_]+)=([a-zA-Z0-9 .&-@=_!<>"\‘]+)\s*$/",$line,$matches)) {
$amp_conf[ $matches[1] ] = $matches[2];
}
}
}
require_once(‘DB.php‘); //PEAR must be installed
$db_user = $amp_conf["AMPDBUSER"];
$db_pass = $amp_conf["AMPDBPASS"];
$db_host = $amp_conf["AMPDBHOST"];
$db_name = $amp_conf["AMPDBNAME"];
$datasource = ‘mysql://’.$db_user.’:’.$db_pass.’@’.$db_host.’/’.$db_name;
$db = DB::connect($datasource); // attempt connection
$type="getAll";
$results = $db->$type("SELECT extension,name,voicemail FROM users ORDER BY extension", null);
foreach($results as $result){
$extensions[] = array($result[0],$result[1],$result[2]);
}
#$extensions = core_users_list();
echo "<?xml version="1.0" encoding="UTF–8"?>\n";
echo "<AddressBook>\n";
$index = 0;
if (isset($extensions)) {
foreach ($extensions as $key=>$extension) {
$index= $index + 1;
echo "\n\n\t<Contact>";
echo "\n\t\t<LastName>" . $extension[1] . "</LastName>";
echo "\n\t\t<FirstName></FirstName>";
echo "\n\t\t<Phone>";
echo "\n\t\t\t<phonenumber>" . $extension[0] . "</phonenumber>";
echo "\n\t\t\t<accountindex>" . $index . "</accountindex>";
echo "\n\t\t</Phone>";
echo "\n\t</Contact>\n";
}
}
echo "</AddressBook>\n";
?>
Before you can use the new phone book you have to update the phone to make it aware of it. I wrote a script that I talk about in another article for this purpose. If you only need to update one phone, or would rather do it manually the basic steps are outlined below.
These screens differ with firmware version, but the general concept should remain roughly the same, for example on newer firmware versions for my phone the phone book options are under Phonebook and then Phonebook Management
To make use of the phone book with the phone, navigate to the web interface on the device and login. I’m using a Grandstream GXP2120 phone. The default password is “admin”.
After logging in click on the Settings tab and then choose Advanced Settings
Scroll down to the field Phonebook XML Download My Elastix installation redirects HTTP request to HTTPS. The Grandstream phone doesn’t seem to care if the certificate is backed by a common certificate authority. Most likely you’ll want to select HTTPS as shown. For Server Path leave off the protocol specification of the URL (e.g. http://, https://). Make sure to set a valid Download Interval (something of at least 5 or higher). Optionally chose to remove old entries in the phone book, this might help to avoid confusing stale data.
Leave a Reply