Skip to content

Instantly share code, notes, and snippets.

@thejohnnybot
Created August 11, 2014 00:47
Show Gist options
  • Select an option

  • Save thejohnnybot/66ff93e78306b702b69e to your computer and use it in GitHub Desktop.

Select an option

Save thejohnnybot/66ff93e78306b702b69e to your computer and use it in GitHub Desktop.
LDAP
<?php
// Error Reporting
//ini_set('display_errors', 'On');
//error_reporting(E_ALL) . "<br />";
$os = strtolower(PHP_OS);
if(strpos($os, "win") === false) ini_set('include_path',ini_get('include_path').':include:../include');
else ini_set('include_path',ini_get('include_path').';include;..\include');
require_once('AastraCommon.php');
function numberCheck($number) {
$prefix = "1";
$validNumber;
switch($number) {
case (preg_match('/(835)/', $number) ? true : false):
$validNumber = "0000000";
break;
case (preg_match('/(999)/', $number) ? true : false):
$validNumber = "0000000";
break;
case (preg_match('/(000)/', $number) ? true : false):
$validNumber = "0000000";
break;
case (preg_match('/(666)/', $number) ? true : false):
$validNumber = "0000000";
break;
case (preg_match('/(0008)/', $number) ? true : false):
$validNumber = "0000000";
break;
case (preg_match('/(155)/', $number) ? true : false):
$validNumber = "0000000";
break;
case (preg_match('/(502155)/', $number) ? true : false):
$validNumber = "0000000";
break;
case (preg_match('/(503155)/', $number) ? true : false):
$validNumber = "0000000";
break;
case (preg_match('/(57111)/', $number) ? true : false):
$validNumber = "0000000";
break;
case (preg_match('/(5999111)/', $number) ? true : false):
$validNumber = "0000000";
break;
default:
$validNumber = $number;
}
$dial = $prefix . $validNumber;
return $dial;
}
function query_ldap($basedn, $lookup, $firstname, $lastname) {
global $LANGUAGE;
$system = Aastra_readINIfile(AASTRA_CONFIG_DIRECTORY . 'ldap_directory.conf', '#', '=');
$LDAP_SERVER = $system['LDAP_Server']['hostname'];
$LDAP_PORT = $system['LDAP_Server']['port'];
$LDAP_ROOT_DN = 'ou=cbs,dc=ldap,dc=cbscloudvoice,dc=com';
# $LDAP_ROOT_DN = $system['LDAP_Server']['basedn'];
$LDAP_USERDOMAIN = $system['LDAP_Server']['userdomain'];
$LDAP_USERNAME = $system['LDAP_Server']['username'];
$LDAP_PASSWORD = $system['LDAP_Server']['password'];
$LDAP_SIZELIMIT = 25; // limits the number of entries fetched. 0 means no limit.
$LDAP_TIMELIMIT = 5; // max number of seconds spent on the search. 0 means no limit.
if($lookup != '') {
$searchfilter = '(|(sn=' . $lookup . '*)(givenName=' . $lookup . '*))';
} else {
if($firstname != '' && $lastname != '') {
$searchfilter = '(&(sn=' . $lastname . '*)(givenName=' . $firstname . '*))';
} else {
if($firstname != '') {
$searchfilter = '(givenName=' . $firstname . '*)';
} else {
$searchfilter = '(sn=' . $lastname . '*)';
}
}
}
$LDAP_ATTRIBUTES = array('sn', 'givenname', 'telephonenumber', 'pager', 'departmentnumber');
$connection = ldap_connect($LDAP_SERVER, $LDAP_PORT);
ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
if($connection) {
if($LDAP_USERNAME != '' && $LDAP_PASSWORD != '') {
// Need to put for each BG
$username = $LDAP_USERNAME;
$password = $LDAP_PASSWORD;
$auth = ldap_bind($connection, $username, $password);
} else {
$auth = ldap_bind($connection);
}
if($auth) {
$search = ldap_search($connection, $LDAP_ROOT_DN, $searchfilter, $LDAP_ATTRIBUTES, 0, $LDAP_SIZELIMIT, $LDAP_TIMELIMIT);
if($search) {
$results = ldap_get_entries($connection, $search);
$j = 0;
for($i=0; $i < $results['count']; $i++) {
$entries[$j]['firstname'] = $results[$i]["givenname"][0];
$entries[$j]['name'] = $results[$i]["sn"][0];
if(!preg_match('/^10000000/', numberCheck($results[$i]["telephonenumber"][0]))) {
$entries[$j]['url'] = "http://" . $LDAP_SERVER . "/ldap/directory/columbus_detail.php?firstname=" . $results[$i]["givenname"][0] . "&lastname=" . $results[$i]["sn"][0] . "&telephone=" . numberCheck($results[$i]["telephonenumber"][0]) . "&extension=" . $results[$i]["pager"][0] . "&department=" . $results[$i]["departmentnumber"][0];
//$entries[$j]['office'] = numberCheck($results[$i]["telephonenumber"][0]);
} else {
$entries[$j]['url'] = "http://" . $LDAP_SERVER . "/ldap/directory/columbus_detail.php?firstname=" . $results[$i]["givenname"][0] . "&lastname=" . $results[$i]["sn"][0] . "&telephone=" . numberCheck($results[$i]["telephonenumber"][0]) . "&extension=" . $results[$i]["pager"][0] . "&department=" . $results[$i]["departmentnumber"][0];
//$entries[$j]['office'] = numberCheck($results[$i]["telephonenumber"][0]);
}
$entries[$j]['selection'] = $j;
$entries[$j]['officeDigits'] = $results[$i]["pager"][0];
$j++;
}
ldap_close($connection);
return empty($entries) ? NULL : $entries;
}
} else {
ldap_close($connection);
display_message(Aastra_get_label('Server error', $LANGUAGE), Aastra_get_label('Cannot send query to LDAP Server.', $LANGUAGE));
exit;
}
} else {
display_message(Aastra_get_label('Server error',$LANGUAGE),Aastra_get_label('Cannot connect to LDAP Server. Please contact your administrator.',$LANGUAGE));
exit;
}
}
#############################################################################
# display_message($title,$message)
#
# Send message to the phone
#
# Parameters
# @title Title of the message
# @message Body of the message
#############################################################################
function display_message($title,$message,$backURL=NULL) {
global $LANGUAGE;
# Depending on phone type
if(Aastra_phone_type()!=5) {
# non 6739i
require_once('AastraIPPhoneTextScreen.class.php');
$text = new AastraIPPhoneTextScreen();
$text->setDestroyOnExit();
$text->setTitle($title);
$text->setText($message);
$text->addSoftkey('5', Aastra_get_label('Back',$LANGUAGE), $backURL);
$text->addSoftkey('6', Aastra_get_label('Exit',$LANGUAGE), 'SoftKey:Exit');
$text->setCancelAction($backURL);
$text->output();
} else {
# 6739i/BluStar
require_once('AastraIPPhoneFormattedTextScreen.class.php');
$text = new AastraIPPhoneFormattedTextScreen();
$text->setDestroyOnExit();
$text->addLine('','double','center');
$text->addLine($title,'double','center','red');
$text->setScrollStart('3');
$text->addLine('');
$text->addLine('');
$text->addLine($message,NULL,'center');
$text->setScrollEnd();
$text->addLine('',NULL,'center');
$text->addSoftkey('9', Aastra_get_label('Back',$LANGUAGE),$backURL,1);
$text->addSoftkey('10', Aastra_get_label('Exit',$LANGUAGE),'SoftKey:Exit',2);
$text->addIcon(1,'Icon:ArrowLeft');
$text->addIcon(2,'Icon:CircleRed');
$text->setCancelAction($backURL);
$text->output();
}
}
#############################################################################
# WARNINGErrorHandler($code, $string, $file, $line)
#
# Called by error handler in case of WARNINGs
#
#############################################################################
function WARNINGErrorHandler($code, $string, $file, $line) {
global $LANGUAGE, $XML_SERVER;
if (preg_match("/Sizelimit exceeded/i",$string)) {
display_message(Aastra_get_label('LDAP Error',$LANGUAGE),Aastra_get_label('Result size limit exceeded. Provide more letters.',$LANGUAGE),$XML_SERVER);
} else {
display_message(Aastra_get_label('Application error',$LANGUAGE),Aastra_get_label('Error Message',$LANGUAGE).": ".$string, $XML_SERVER);
exit;
}
}
#############################################################################
# Main code
#############################################################################
# Collect parameters
$action =trim(Aastra_getvar_safe('action'));
$lookup =trim(Aastra_getvar_safe('lookup'));
$lastname =trim(Aastra_getvar_safe('lastname'));
$firstname =trim(Aastra_getvar_safe('firstname'));
$basedn = trim(Aastra_getvar_safe('basedn'));
# Log call to the application
Aastra_trace_call('LDAP directory','action='.$action.', lookup='.$lookup.', lastname='.$lastname.', firstname='.$firstname);
# Test Phone
Aastra_test_phone_version('2.3.0.',0);
# Get Language
$LANGUAGE=Aastra_get_language();
# If no search string was provided, don't perfom search
if(empty($lookup) && empty($lastname) && empty($firstname)) $action='start';
# Process action
switch($action) {
# Search
case 'search':
# Make the LDAP query
$result=query_ldap($basedn, $lookup,$firstname,$lastname);
$hits=count($result);
# How many matches
if ($hits==0) {
# No match
display_message(Aastra_get_label('No match found',$LANGUAGE),Aastra_get_label('Please modify your search.',$LANGUAGE),$XML_SERVER.'?lastname='.$lastname.'&firstname='.$firstname.'&lookup='.$lookup);
exit;
} else {
# Display Results
require_once('AastraIPPhoneScrollableDirectory.class.php');
$object = new AastraIPPhoneScrollableDirectory();
if($hits==1) $object->setTitle(Aastra_get_label('One Match',$LANGUAGE));
else $object->setTitle(sprintf(Aastra_get_label('%s Matches',$LANGUAGE),$hits));
$object->setBackURI($XML_SERVER.'?lastname='.$lastname.'&firstname='.$firstname.'&lookup='.$lookup);
if(Aastra_number_softkeys_supported()==10) {
$object->setBackKeyPosition(9);
}
$object->setEntries($result);
if (empty($lookup) && !empty($firstname)) $object->natsortByFirstname();
else $object->natsortByLastname();
}
break;
# INIT
case 'start':
default:
# Depending on the phone
if(Aastra_is_multipleinputfields_supported()) {
# Multiple fields supported
require_once('AastraIPPhoneInputScreen.class.php');
$object = new AastraIPPhoneInputScreen();
$object->setTitle(Aastra_get_label('Directory Lookup',$LANGUAGE));
$object->setDisplayMode('condensed');
$object->setURL($XML_SERVER.'?action=search');
$object->setDestroyOnExit();
if(!empty($lookup)) {
$object->setDefaultIndex('4');
} else {
if (!empty($lastname)) $object->setDefaultIndex('2');
else if (!empty($firstname))$object->setDefaultIndex('3');
}
# Fields
$object->addField('empty');
$object->addField('string');
$object->setFieldPrompt(Aastra_get_label('Last Name:',$LANGUAGE));
$object->setFieldParameter('lastname');
$object->setFieldDefault($lastname);
$object->addField('string');
$object->setFieldPrompt(Aastra_get_label('First Name:',$LANGUAGE));
$object->setFieldParameter('firstname');
$object->setFieldDefault($firstname);
$object->addField('string');
$object->setFieldPrompt(Aastra_get_label('Or Anywhere:',$LANGUAGE));
$object->setFieldParameter('lookup');
$object->setFieldDefault($lookup);
} else {
# Single field
require_once('AastraIPPhoneInputScreen.class.php');
$object = new AastraIPPhoneInputScreen();
$object->setTitle(Aastra_get_label('Directory Lookup',$LANGUAGE));
$object->setPrompt(Aastra_get_label('Last/Firstname?',$LANGUAGE));
$object->setParameter('lookup');
$object->setType('string');
$object->setURL($XML_SERVER.'?action=search');
$object->setDestroyOnExit();
$object->setDefault($lookup);
}
# Softkeys
if(Aastra_is_softkeys_supported()) {
# Depending on phone type
if(Aastra_phone_type()!=5) {
# non-6739i phones
$object->addSoftkey('1', Aastra_get_label('Lookup',$LANGUAGE), 'SoftKey:Submit');
$object->addSoftkey('3', 'ABC', 'SoftKey:ChangeMode');
$object->addSoftkey('4', Aastra_get_label('Backspace',$LANGUAGE), 'SoftKey:BackSpace');
$object->addSoftkey('5', Aastra_get_label('Reset',$LANGUAGE), $XML_SERVER);
$object->addSoftkey('6', Aastra_get_label('Exit',$LANGUAGE), 'SoftKey:Exit');
} else {
# 6739i/BluStar
$object->addSoftkey('8', Aastra_get_label('Lookup',$LANGUAGE), 'SoftKey:Submit',1);
$object->addSoftkey('5', Aastra_get_label('Reset',$LANGUAGE), $XML_SERVER, 3);
$object->addSoftkey('10', Aastra_get_label('Exit',$LANGUAGE), 'SoftKey:Exit',2);
$object->addIcon(1,'Icon:Search');
$object->addIcon(2,'Icon:CircleRed');
$object->addIcon(3,'Icon:Delete');
}
}
break;
}
# Display XML object
$object->output();
exit;
// LDAP Errors
//echo ldap_error($connection) . "<br />";
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment