Cisco Phone 7940 7960 Directory Button Script

I wanted a dynamic list of my extensions to use for the Directory button on my cisco phones in the office on our new FreePBX installation. I pointed the "directory_url: " line in my SIPDefault.cnf to a php page (i.e. “directory_url: http://www.mywebserver.com/directory.php”.

I’ve included my phpscript for anyone who might care to use it as a starting point. Our extensions are 4 digits, we have a few that are 5 digits for a couple of people who want to use softphone and hardphone at the same time (Ring Group is their primary extension, 5 digit extension is their soft phone) so this script excludes those extensions. The SQL could probably been written to show only extensions less that 10000 but whatever :slight_smile:

The best part about it, is that it sorts by last name and preserves the extension number. All pretty simple stuff but to a novice programmer, quite satisfying!

Hope you find this useful.

<? echo "    IP Telephony Directory    Employee Directory"; // Edit this line below with your freepbx mysql server, username and password // for the database with extensions $link = mysql_connect("localhost","freepbx", "password"); // Our SQL Query to get name and extension columns $sGetUsers = "SELECT name, extension from asterisk.users ORDER BY name"; $sGetUsersR = mysql_query($sGetUsers) or die("Could not load users:
$sGetUsers
" . mysql_error()); // Init the variables we need $aUsers = array(); $line = ""; // Step through the results of our SQL query, reverse the name and add // a comma "John Doe" is now "Doe, John" // The If statement is NOT needed if you dont have special extensions // you dont want displayed while ($line = mysql_fetch_array($sGetUsersR, MYSQL_ASSOC)) {         extract($line);         if($extension < 10000)         {                 $newname = explode(" ", $name);                 $aUsers[$extension] = "$newname[1], $newname[0]";         } }         // asort preserves our key/index         asort($aUsers);         // Step trhough the array we built from our SQL and write our XML tagging         foreach($aUsers as $ext => $name)         {                 echo "                                                                        $name                                                                                                         $ext                                                        \n";         } echo " "; // We're done! ?>

Well, changing <? to <?php seems to have done the trick. When running it originally, it just spit out the file as-is. Script seems to be working perfectly now. Thank you for your help (and for the script)!

I apologize in advance for bumping this, however I cannot seem to get the script to work. It seems that there are some tags missing from your code on the first post, however, trying to fix it does not work. Does anyone have a working version of this script?

Tough to say. Try going to your server’s command line and running php -f directory.php from the folder you have the directory.php. You should see any errors there, perhaps some of the formatting of your usernames isn’t being handled correctly.

So I uploaded my directory.php file, and added it as an option to the Services menu. However, when trying to launch the directory, the phone states “XML Parse Error”. Unfortunately, it’s not very descriptive. I have ensured that the mySQL server, username, and password are correct, and the query being sent is valid (I tested the query in phpMyAdmin).

Is there something extra I am supposed to do to the script, my server, or my phone?

<? echo " IP Telephony Directory Employee Directory"; // Edit this line below with your freepbx mysql server, username and password // for the database with extensions $link = mysql_connect("localhost","freepbx", "password"); // Our SQL Query to get name and extension columns $sGetUsers = "SELECT name, extension from asterisk.users ORDER BY name"; $sGetUsersR = mysql_query($sGetUsers) or die("Could not load users:
$sGetUsers
" . mysql_error()); // Init the variables we need $aUsers = array(); $line = ""; // Step through the results of our SQL query, reverse the name and add // a comma "John Doe" is now "Doe, John" // The If statement is NOT needed if you dont have special extensions // you dont want displayed while ($line = mysql_fetch_array($sGetUsersR, MYSQL_ASSOC)) { extract($line); if($extension < 10000) { $newname = explode(" ", $name); $aUsers[$extension] = "$newname[1], $newname[0]"; } } // asort preserves our key/index asort($aUsers); // Step trhough the array we built from our SQL and write our XML tagging foreach($aUsers as $ext => $name) { echo " $name $ext \n"; } echo " "; // We're done! ?>

I think an upgrade to the freepbx must have stripped the tags out that were used. Let me see if I can find the original.

Thank’s for that script! I was looking to create one like this! But this one works perfectly! I just change it to display it this way : “FirstName LastName”! Thank’s!

This script looks exactly what I need but I’m in a little over my head (okay maybe more than a little) and I could use some help if anyone is kind enough. I have copied the script and placed it on my freepbx /var/www/html directory so I can access it from any browser but when I try and access it, the page returns -

Employee DirectoryCould not load users:
SELECT name, extension from asterisk.users ORDER BY name
SELECT command denied to user ‘’@‘localhost’ for table ‘users’

Am I correct freepbx sql connection user/password is as per your script (I don’t remember specifying it and from what I read it is standard)? Any ideas what I am doing wrong? If it is a custom username and password any clues where i can find what this might be on my server?

The script does NOT have the correct username/password combination for your installation. You should be able to find the un/pw in /etc/asterisk/cdr_mysql.conf take the user/password from there, and update the line in the script that says

$link = mysql_connect(“localhost”,“freepbx”, “password”);

In the default listed above the parameters are (“server”,“username”,“password”), so change the username parameter and password parameter to match your cdr_mysql.conf file and you should be good.

<? echo " IP Telephony Directory Employee Directory"; // Edit this line below with your freepbx mysql server, username and password // for the database with extensions $link = mysql_connect("localhost","freepbx", "password"); // Our SQL Query to get name and extension columns $sGetUsers = "SELECT name, extension from asterisk.users ORDER BY name"; $sGetUsersR = mysql_query($sGetUsers) or die("Could not load users:
$sGetUsers
" . mysql_error()); // Init the variables we need $aUsers = array(); $line = ""; // Step through the results of our SQL query, reverse the name and add // a comma "John Doe" is now "Doe, John" // The If statement is NOT needed if you dont have special extensions // you dont want displayed while ($line = mysql_fetch_array($sGetUsersR, MYSQL_ASSOC)) { extract($line); if($extension < 10000) { $newname = explode(" ", $name); $aUsers[$extension] = "$newname[1], $newname[0]"; } } // asort preserves our key/index asort($aUsers); // Step trhough the array we built from our SQL and write our XML tagging foreach($aUsers as $ext => $name) { echo " $name $ext \n"; } echo " "; // We're done! ?>

Ok so I figured out my last problem by using ssh and checking /var/lib/mysql/
mysql asterisk which told me what i needed to know but now I need some idea of how to show this on the handset. I can modify the cnf file to point to the directoryURL but I don’t see any new directory under the phone directory menu.

Can I ask for an example how to do that?

directory_url: http://webserver/directory_script.php

Put this in your SIP cnf files for your CISCO devices.

directory_url: http://asteroid.regscan.local/directory.php

Thanks for the quick replies. I tried this syntax but my config file previously had the following syntax - neither seem to do anything.
http://192.168.1.241/directory.php

There is a button on the handset for directories but when I go into it all I see is the standard Missed calls / Received Calls & Placed calls.

I assume I am expecting to see a new item called Employee Directory?

I was also wondering if someone has an example how they use the services menu thinking I might learn something from that -
http://example.domain.ext/services/menu.xml

There is also an on screen item / button which is this but again I am not sure how to format a file to get it to read properly on the phone, if you can point me to further reading I would appreciate it.

  <line button="2"> 
     <featureID>20</featureID> 
     <featureLabel>Menu</featureLabel> 
     <serviceURI>http://example.domain.ext/services/menu.xml</serviceURI> 
  </line> 

http://example.domain.ext/services/directory.php
http://example.domain.ext/services/menu.xml

  <line button="2"> 
     <featureID>20</featureID> 
     <featureLabel>Menu</featureLabel> 
     <serviceURI>http://example.domain.ext/services/menu.xml</serviceURI> 
  </line> 

The “new” directory is accessible under Directories --> External Directory (it should be below Personal Directory and it’s also the last item on the list).

Ok thanks but I don’t see personal directory or external directory. I assume you see these when you hit the button ‘directories’. When I hit this key I only get the 3 options previously listed, no options for directories. If this is not clear tomorrow I will post some images of what I see. Any further ideas appreciated.

What firmware version are you currently running? And is this a 7940 or a 7960 (or neither?).

I also pointed my directory in the services menu as well. Here’s what my services.xml file currently looks like:

Title of Menu Prompt text. Staff Directory http://192.168.1.254/cisco/services/directory.php Current Weather http://192.168.1.254/cisco/services/weather.php

Hopefully that can get you started on the services menu.