Grandstream Phone Book Creator for Elastix

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:

<?xml version="1.0" encoding="UTF-8"?>
<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:

<?php
/* 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="UTF8"?>\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:

<?php
/* 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="UTF8"?>\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”.
grandstream_web_login

After logging in click on the Settings tab and then choose Advanced Settings
grandstream_web_settings_advanced

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.
grandstream_phonebook_settings

Once the changes have been made you’ll need to reboot.
grandstream_reboot


Posted

in

by

Comments

11 responses to “Grandstream Phone Book Creator for Elastix”

  1. Michael Taylor Avatar

    These links are dead. Can you repost the scripts please?

    1. Chris Avatar
      Chris

      Oops! Sorry about that. Thanks for drawing the problem to my attention. The download link has been corrected.

  2. […] even virtual extensions like general mailboxes will appear in the phonebook. Here is the link: http://ethertubes.com/grandstream-phone-book-creator-for-elastix. The scripts as well as instructions can be found there. The author also references a script he has […]

  3. Paul Estrella Avatar

    Hi Chris,

    I want to know if you want to include this post on the Elastix Official Blog.

    Regards

    Paul

    1. Chris Avatar
      Chris

      Paul,

      There’s not much to it, but if it’s helpful to someone, that was the purpose behind it. Go for it.

      Thanks,
      Chris

  4. Tunde Odubiyi Avatar

    Thank you so much for this write-up. I used it on my Elastix server version 2.5, with Grandstream GXP1405 and it worked at first attempt.
    However, I tried to use it on Yealink IP Phones, modifying the codes in the phonebook2.php file but could not get it to work.

    Can you please help modify these codes to work with Yealink SIP-T2x, 3x, 4x and VP530 Phones.

    Thanks.

  5. wwenyunkui Avatar
    wwenyunkui

    Tunde Odubiyi
    Plz go the homepage of Yealink, and you will get some help.

  6. Greg Avatar
    Greg

    Great job . And thanks a lot .
    How to run the script automatically , I wish when I create a new extension in elastic ,that the script add the new user automatically .

    Have a good day .

    1. Chris Avatar
      Chris

      Hi Greg,

      Thanks for your interest. Your desired result is exactly what this script is supposed to do. It is intended to be placed in the Elastix web/document root directory and outputs in response to http/https requests coming directly from the phones. On the Grandstream model phone I wrote it for I’m able to set a periodic update interval that keeps our phones more or less up to date with the latest names and numbers in Elastix. It would be great if the phone would request the latest version whenever the user pressed the phone book feature key, but I assume they chose not to do it that way in order to keep the UI latency down. Maybe there is a way to do that, but I don’t know how. For our use the periodic updates seem to work great; generally we aren’t replacing staff members quicker than the update intervals. 😉

  7. Sakis Avatar
    Sakis

    Very nice.. but is it possible to view the “external” phone directory entries insteed the “internal” ? i m using Issabel and for some reason phoonbook2.php dosn’t work.

    Thank you

    1. Chris Avatar
      Chris

      Hi Sakis,

      Well, I don’t have an external phone directory, and I’m not sure how that is stored in Issabel unfortunately. If your directory is small you could just hand code the XML I suppose (in the format I described). Good Luck!

Leave a Reply to Tunde Odubiyi Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.