#!/usr/bin/perl # searchdirectory.pl - # Version 2 - October 2010 # # Program to imp[lement a simple search for numbers on a CISCO IP # phone that supports XML # # just call this script as the services (or part thereof) URL # for a CISCO IP Phone # # For a 796x/7940, in the SIPDefault or specific SIP config file add # services_url: "http:////searchdirectory.pl # EG :- services_url: "http://1.2.3.4/cgi-bin/searchdirectory.pl" # # For a 797x/7945, in the SEP config file add # http://196.22.67.196/cgi-bin/searchdirectory.pl # EG :- http://1.2.3.4/cgi-bin/searchdirectory.pl" # # # This program is licensed under the GPL: http://www.gnu.org/licenses/gpl.txt # Nic Tjirkalli # # # requires the perl CGI module # It can be obtained from http://search.cpan.org/ if the perl you are using # alreadty dose not have it included use strict; use CGI; # Hash of names and numbers # The key is the name my %directory = ( 'Receptionist','9999407100', 'Adele White','9999400101', 'Alan Smith','9999406102', 'Justin Last','9999400643', 'Yolanda Blue','9999400591', ); # max number of entries to return # Phones have a limit to the number of directory entries they can display my $maxcount = 18; #################################################################### ## There should be nothing to change after here ## Unless you wanna rewrite the internals #################################################################### my %property; # Construct url to where script is script $ENV{SERVER_NAME} =~ s!/+$!!; $ENV{SCRIPT_NAME} =~ s!^/+!!; my $pathto = "http://$ENV{SERVER_NAME}/$ENV{SCRIPT_NAME}"; # Get environment variables returned by phone web browser getenv(); # Get variables passed in response my $query = new CGI; #Return HTTP header my $header=$query->header(); # Get the mac address of the phone - well sort of MAC my $name=$query->param('name'); # Get other Variables and Values my $letters=undef; my $numbers=undef; my $help=undef; my $search=undef; $letters=$query->param('letters'); $numbers=$query->param('numbers'); $help=$query->param('help'); $search=$query->param('search'); # Verify if there has been any input to the script my $src = 0; if (defined($letters)) { $src = 1; } if (defined($numbers)) { $src = 1; } if (defined($search)) { $src = 1; } if (defined($help)) { $src = 1; } # First time page is being displayed so it needs to ask the user # for either a search or help if ( $src == 0 ) { &intro; exit; } # A search was requested, so print out search page if (defined($search)) { &request_search; exit; } # Help was requested if (defined($help)) { &help; exit; } # A search criteria was specified so it needs to do a lookup if (defined($letters)) { &return_names; exit; } # A search criteria was specified so it needs to do a lookup if (defined($numbers)) { &return_numbers; exit; } exit; # # Get Environment Variables from HTTP header # sub getenv { my $var; my $val; foreach $var (sort(keys(%ENV))) { $val = $ENV{$var}; $val =~ s|\n|\\n|g; $val =~ s|"|\\"|g; $property{$var} = $val; } } # # Return search entries (Names) matching search criteria # sub return_names { my $pth= "$pathto?opts="; my $hash_val = ""; print "Content-Type: text/xml\n\n"; print "\n"; print < IP Telephony Directory External directory TEST my $count = 0; foreach (sort keys %directory) { $hash_val = $_; if ( ($hash_val =~ m/$letters/i) && ($count < $maxcount)) { print < $hash_val $directory{$hash_val} TEST $count++; } } print < TEST } # # Return search entries (Numbers) matching search criteria # sub return_numbers { my $pth= "$pathto?opts="; my $hash_val = ""; print "Content-Type: text/xml\n\n"; print "\n"; print < IP Telephony Directory External directory TEST my $count = 0; my %directory_value = reverse %directory; foreach (sort keys %directory_value) { $hash_val = $_; if ( ($hash_val =~ m/$numbers/i) && ($count < $maxcount)) { print < $directory_value{$hash_val} $hash_val TEST $count++; } } print < TEST } # # Display search criteria entry Screen # sub request_search() { print < Internal Directory Lookup Who You Looking For? $pathto Name letters A Number numbers N TEXT } # # Display First Page # sub intro() { print < Internal Directory Lookup Search Or Help? Search $pathto?search=1 Help $pathto?help=1 TEXT } # # User needs help ..... show it to them # sub help() { print < Directory Lookup Help Enter in either a Name or Number (or part thereof) and hit Submit. The program will return up to the first $maxcount entries which match your search criteria Search $pathto?search=1 4 Exit $pathto 3 TEXT }