#!/usr/bin/perl
# wcs 12/15/2006
# deptinput.txml file calls this CGI with the 'dept' variable
# output is a Cisco IP Phone MENU object which dials the phone upon selection
# * This script is very much like lookup.pl (PSU People Search)
# * Changes made to the internal workings of one script should probably also
# be applied to the other
use strict;
use CGI;
use Net::LDAP;
# SET THESE VARIABLES
my $inputPage = "http://webserver/ph/deptinput.txml"; # calling page
my $mustHaveListing = 1; # only return entries that have associated DNs
# (set to 1 for default behavior)
# do this first
print "Content-type: text/xml\n\n";
# Define variables
# ----------------
# LDAP directory to contact
my $directoryURL = "ldap.psu.edu";
# Portion of the directory we'll be searching
my $searchBase = "dc=psu,dc=edu";
# CGI vars
my $q = new CGI;
my $cgi_dept = $q->param('dept');
# The attributes (and their associated values) that we wish to
# search for in the directory.
my $dept;
if (defined $cgi_dept) {
$dept = $cgi_dept;
} else {
errorNoDeptname();
}
my $searchFilter = "(&(psdepartment=*$dept*)(eduPersonPrimaryAffiliation=DEPT))";
# The attributes we'd like to have returned for each entry
#
# (Doing this is entirely optional; it simply reduces the
# volume of data returned by excluding attributes that we're
# not interested in receiving.)
my $attributesToReturn = [
'psDepartment',
'psCampus',
'telephoneNumber',
'psAdminArea'
];
# Connect to the directory
# ------------------------
# Open a connection to the directory
my $ldap = Net::LDAP->new($directoryURL, timeout => '20') # as struct
or errorUnavailable();
# Make an anonyous bind to the directory
$ldap->bind ;
# Perform a search
# ----------------
my $searchResultsObject = $ldap->search
(
base => $searchBase, # Note the comma here
filter => $searchFilter, # and here
# Return only a limited set of attributes from
# the search, *if* we've defined such a set above
attrs => $attributesToReturn
);
# If there is a result code (indicating an error),
# display an error message
if ($searchResultsObject->code) {
print STDERR $searchResultsObject->error;
if ($searchResultsObject->code == 4) { # LDAP_SIZELIMIT_EXCEEDED
errorTooMany();
} else {
errorNoResults();
}
}
# Disconnect from the directory
# -----------------------------
$ldap->unbind;
# Work with the data returned from the search
# -------------------------------------------
my $countOfEntriesReturned = $searchResultsObject->count;
# IP Phone can only produce menus of 100 items or fewer
if ($countOfEntriesReturned > 100) {
errorTooMany();
}
# Begin output
print "\n";
# Cycle through each of the directory entries returned from the
# search, and extract and print the values of selected attributes
# of each entry
# sort on last name, then first name
# keep count of number of entries processed
my $counter = 0;
foreach my $entry ($searchResultsObject->sorted('psDepartment','psCampus'))
{
# Look at each of the 'entry' objects returned from the search
# Initialize each variable each time through the loop
my $deptName = "";
my $campusArea = "";
my $phoneNumber = "";
my $dialNum = "";
$phoneNumber = $entry->get_value('telephoneNumber');
if ($mustHaveListing && !$phoneNumber) { # is a DN required? is there one?
next; # if not, don't process this one
}
$counter++; # we're still here, so add one to process
# Extract the values from selected attributes
$deptName = lc($entry->get_value('psDepartment'));
$deptName =~ s/^(\w)/uc $1/e;
$deptName =~ s/ (\w)/" " . uc $1/eg;
if ($entry->get_value('psCampus')) {
$campusArea = lc($entry->get_value('psCampus'));
$campusArea =~ s/^(\w)/uc $1/e;
$campusArea =~ s/ (\w)/" " . uc $1/eg;
} else {
$campusArea = lc($entry->get_value('psAdminArea'));
$campusArea =~ s/^(\w)/uc $1/e;
$campusArea =~ s/ (\w)/" " . uc $1/eg;
}
print "";
}
print "Search returned $counter";
if ($counter == 1) {
print " entry\n";
} else {
print " entries\n";
}
if ($counter == 0) {
print <
Back
$inputPage
2
Exit
SoftKey:Exit
3
Back to search again or Exit
EOF
} else {
print <
Dial
SoftKey:Select
1
Back
$inputPage
2
Exit
SoftKey:Exit
3
Select a number to dial
EOF
}
print "\n";
exit;
# -----------------------
# the following error subs terminate processing and exit after displaying
# the error message to the phone
sub errorNoResults()
{
print <
No Information
No information was returned from your search, or an error occurred.
Back
$inputPage
2
Exit
SoftKey:Exit
3
Back to search again or Exit
EOF
exit;
}
sub errorNoDeptname()
{
print <
Dept Name Required
Please enter some or all of the department name into the Department field. This is a required field.
Back
$inputPage
2
Exit
SoftKey:Exit
3
Back to search again or Exit
EOF
exit;
}
sub errorTooMany()
{
print <
Too Many Results
Over 100 results returned. Please narrow your search by including more of the department name.
Back
$inputPage
2
Exit
SoftKey:Exit
3
EOF
exit;
}
sub errorUnavailable()
{
print STDERR "$@";
print <
Service unavailable
The search service is temporarily unavailable. Please try again later.
Exit
SoftKey:Exit
3
EOF
exit;
}