FreePBX XML Directory Setup

Creating a directory in FreePBX can open the door to larger companies so that you can easily find people in the company that you are looking for. Once set up you will have a central directory database that will update itself every hour from the generating entries based on your FreePBX's extension database.

Create Script

First, log in to your FreePBX box via SSH under the root user.

Create a new application in /usr/local/sbin called xml_directory.sh

nano -w /usr/local/sbin/xml_directory.sh

*Credit for this file goes to Thyrus Gorges. This file may be modified from the original.

Copy and past below details in xml_directory.sh

#!/bin/bash

# Author: Thyrus Gorges
# Date: 2015/03/04

#The MIT License (MIT)

#Copyright (c) <2015> <Thyrus Gorges>

#Permission is hereby granted, free of charge, to any person obtaining a copy
#of this software and associated documentation files (the "Software"), to deal
#in the Software without restriction, including without limitation the rights
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#copies of the Software, and to permit persons to whom the Software is
#furnished to do so, subject to the following conditions:

#The above copyright notice and this permission notice shall be included in
#all copies or substantial portions of the Software.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#THE SOFTWARE.

# Build an XML Directory file that can be used for Polycom, Yealink, ClearlyIP, Sangoma as well as other phones.
# by pulling the usernames and extensions of all SIP users

# Set so we can loop on newlines instead of spaces
IFS=$'\n'

# MySQL Creds
SQLUSER=root
SQLPASS=""

# Declare our file
FILE=/tftpboot/company_directory.xml

# Remove old file
rm $FILE

# ReCreate our file
touch $FILE

# Write the header of the file
# Passing the -e option to echo allows us to use tabs
echo -e '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>' >> $FILE
echo -e '<!-- $RCSfile: company_directory.xml,v $ $Revision: 1.3 $ -->' >> $FILE
echo -e '<directory>' >> $FILE
echo -e "\t<item_list>" >> $FILE

# Query Database
# We pass -N to MySQL so it doens't print headers
# CS: Original command below, modified to remove password
# for i in $( mysql -u $SQLUSER -p$SQLPASS -N -e "use asterisk; select extension, name from users;" ); do
for i in $( mysql -u $SQLUSER -N -e "use asterisk; select extension, name from users;" ); do
# Assign Variables to each field
EXTEN=`echo $i | cut -f1`
FN=`echo $i | cut -f2 | cut -d" " -f1`
LN=`echo $i | cut -d" " -f2`
# The item tag is the beginning of a contact
echo -e "\t\t<item>" >> $FILE
echo -e "\t\t\t<ln> $LN </ln>" >> $FILE
echo -e "\t\t\t<fn> $FN </fn>" >> $FILE
echo -e "\t\t\t<ct> $EXTEN </ct>" >> $FILE
echo -e "\t\t</item>" >> $FILE
done

# Write file endings
echo -e "\t</item_list>" >> $FILE
echo -e "</directory>" >> $FILE

Once you copy the above into the file exit by pressing CTRL+X followed by Y

Next, you will need to make the file you created Executable.

chmod +x /usr/local/sbin/xml_directory.sh

Run the script to verify that it works.

runuser -l asterisk -c '/usr/local/sbin/xml_directory.sh'

You can now verify that it created the file in /tftpboot if you would like (File name company_directory.xml)

ls -la /tftpboot |egrep xml

Now we will need to automate the script

nano -w /etc/crontab

Add the following to the last line in the file.

0 * * * * asterisk /usr/local/sbin/xml_directory.sh

Exit the file by CTL+x followed by Y to exit and save.

You now have an XML database on your FreePBX server that will update itself every hour.
You will now need to make sure to tell your phone to look for the database. This is done differently for different phones and is not covered in this document.

Leave a Reply

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