Follow up to SPA942 Personal Directory with LDAP

I just wanted to follow up on my previous post about using LDAP and PHP to push the directories on the SPA942.
I’ve included a whole lot of code below, and please note that a lot of this is me just getting the job done. The code is probably messy and poorly written, and some of it is taken from other sites/samples.
Unfortunately, blogger seems to take away all my nice formatting (indents etc…), so you’ll have to bear with me. Also, long comments appear across multiple lines in here, so that could make it even harder. Copy the code into a program like notepad++ and set language to php to make it easier to read.

Firstly, just need to include a few files etc…

error_reporting(E_ERROR | E_WARNING | E_PARSE);

dbConnection.php connects to a Mysql DB that I use basically as an asset register. It contains IPs, allocations, extensions, invoice numbers yada yada yada. Originally, I drove this script entirely from LDAP, however once users started to have multiple extensions (depending on which site they were working from), LDAP (well Active Directory actually) just couldn’t cut it. Note that this still uses LDAP for getting user details (display name) and extensions. It cross references these with Mysql.
header.php is just some menu’s etc
ldapInfo.php is the connection to LDAP. It returns the info as $info
voip_ext.php is a list of extensions returned as $extensions. Note that the extensions have changed with firmware updates, so check your phone to see what yours are.
The rest of the code follows.
I’ve tried my best to include comments, and please remember this is not a complete solution (due to DB dependencies etc…) but will hopefully get you started.
If anyone is interested in the DB schema, leave a comment and I can post it here.

$pDir = "";


//get users extensions
$UserExtensions = getExtensions();

//100 slots to use. fill from bottom, so 99 -> 0
$extID = 99;

//for each extension, find the appropriate info
foreach ($UserExtensions as $ext) {
if ($ext <> "" && $ext <> 0){
$pDir .= ("&".dirEntry($info,$ext));

//tidy up and sort the pdir string
$pDir = substr($pDir,1);
$pDir = explode("&",$pDir);

//assign an extid to each entry
foreach ($pDir as &$entry) {
$entry = ($extensions[$extID]."=n%3D".$entry);

//more tidying of pdir
$pDir = implode("&",$pDir);
$pDir .= getGroups($info);
$pDir = str_replace(" ","%20",$pDir);

//need to fill in from extID to 0 to clean vacant entries from pdir
//note that this will wipe out any duplicate names (was occuring when users were leaving)
//this will also wipe out any personal entries (i.e. this script is the only way that entries will appear in the directory)
while ($extID > 0) {
$pDir .= "&".$extensions[$extID]."=;";
echo $extID."<br />";
echo $pDir."<br />";

//run the wget (comment this out to test sending to a single phone - use next bit instead)
$phoneIPs = GetPhoneIPs();
foreach ($phoneIPs as $phoneIP) {

//uncomment for testing


function getExtensions(){
//this gets all the phone extensions that i've allocated in mysql. Note that other extensions probably exist in ldap, but they're not assigned to a phone and hence we don't want them here.
$sql = "select allocation.extension from allocation order by allocation.extension asc";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$extensions .=$row["extension"].";";
$extensions = explode(";", $extensions);
return $extensions;

function GetPhoneIPs(){
//this gets the ips of the phones. need this when performing the wget command
$sql = "select distinct phone_ip from asset where phone_ip not like ''";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
$phoneIPs .=$row["phone_ip"].";";
$phoneIPs = explode(";", $phoneIPs);
return $phoneIPs;

function dirEntry($info,$ext){
//this is creating the sting that we eventually want to POST to the phone's pdir
$allocationDetails = getAllocationDetails($ext);
$phoneMAC = $allocationDetails[0];
$phoneIP = $allocationDetails[1];
$siteName = $allocationDetails[2];
$serverIP = $allocationDetails[3];

$userDetails = ldapUserDetails($info,$ext);
$userSamAccountName = $userDetails[0];
$userDisplayName = $userDetails[1];
$userComany = $userDetails[2];

$ringtone = getRingTone($phoneIP);

return "$userDisplayName;p%3D$ext;r%3D$ringtone";
//return "n%3D$userDisplayName;p%3D$ext;r%3D$ringtone";

function ldapUserDetails($info,$ext){
//this matches the extensions with the names from ldap (we don't keep names in mysql)
for ($i=0; $i<$info["count"]; $i++) {
if ($info[$i]["ipphone"][0] == $ext) {
$samaccountname = strtolower($info[$i]["samaccountname"][0]);
$displayName = $info[$i]["displayname"][0];
$company = $info[$i]["company"][0];
if ($company == "") $company = "GLiNTECH"; //catch accounts with no company name specified
$results = array($samaccountname,$displayName,$company);
return $results;

function getAllocationDetails($extension){
// this gets more info about the phone (we use multiple sites and servers)
$sql = "select asset.mac, allocation.extension, server.server_ip, asset.phone_ip, site.site_name
from allocation
join asset as asset on allocation.asset_id = asset.asset_id
join site as site on allocation.site_id = site.site_id
join server as server on site.site_id = server.site_id
where allocation.extension = '"
.$extension."' limit 1";
$result = mysql_query($sql);

if (!$result) {
$thisPhoneMac = $row["0000000000"];
$thisPhoneIP = $row[""];
$thisSiteName = $row["NULL"];
$thisServerIP = $row[""];
} elseif (mysql_num_rows($result) == 0) {
$thisPhoneMac = $row["0000000000"];
$thisPhoneIP = $row[""];
$thisSiteName = $row["NULL"];
$thisServerIP = $row[""];
} else {

// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
//       then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
$thisPhoneMac = $row["mac"];
$thisPhoneIP = $row["phone_ip"];
$thisSiteName = $row["site_name"];
$thisServerIP = $row["server_ip"];


$results = array($thisPhoneMac,$thisPhoneIP,$thisSiteName,$thisServerIP);
return $results;


function getRingTone($phoneIP){
//this checks to see if a user as set a custom ringtone.
if ($xmlstr = file_get_contents("http://$phoneIP/admin/spacfg.xml")){
if (strlen($xmlstr) < 1){
$ringtone = "1";
} else {
$xml = new SimpleXMLElement($xmlstr);
$ringtone = $xml->Default_Ring_1_;
if ($ringtone == "User 1") {
$ringtone = "11";
} elseif ($ringtone == "User 2") {
$ringtone = "12";
} else {
//default ringtone for people in the directory
$ringtone = "12";
return $ringtone;

function updatePhonePDir($phoneIP,$pDir){
//this generates the wget command. -t 1 means it will only try once. if it fails then it won't try again until the next day.
$command = "wget --post-data '".$pDir."' http://".$phoneIP."/ -t 1";
runCommand($command); // or die("update to $phoneIP failed");

function runCommand($command){
//calls the command and writes some debugging to the screen
echo $command."<br />";
echo $returned."<br /><br />";
return $returned;

function getGroups($info){
//this is used for getting groups in ldap and finding their exts etc... read up in a previous post for more on this. Note that groups are prepended with '*' to make them appear at the top of the directory
global $extID;
global $extensions;
for ($i=0; $i<$info["count"]; $i++) {
if (ereg("[[6][0-9]{3}]",substr($info[$i]["info"][0],0,6))){
//calling groups
$extensionstring .= "&".$extensions[$extID]."=n%3D*".strtoupper(str_replace(" ","%20",$info[$i]["name"][0])).";";
$extensionstring .= "p%3D".substr($info[$i]["info"][0],1,4).";r%3D11";
return $extensionstring;

Be Sociable, Share!

2 thoughts on “Follow up to SPA942 Personal Directory with LDAP”

  1. ldapInfo.php and dbConnection.php are stock files, you can search them up on google – as for header.php it’s not entirely needed, as said it’s just menus.

    with some basic php knowledge you can figure out the attributes of the $extension array so creating an include file (or even define the array in the main file) will suffice.

    a bit messy but good code for the situation i say! saves me a lot of work cheers.

Leave a Reply

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