XML Phone Directory

I had a colleuge ask about running a small ASP web site on a Linux Distro. My first question was why, and echoed my concern of spending more time troubleshooting than to just rewrite the app. With that I went ahead and rewrote the ap to PHP. Its a like for like app so just moving what feaures were there. The origional app provided an XML file that contained a contained the phone directory and then a couple of scripts that parsed that directory for the phone to display. Obviuosly the first additon to start looking at would be an editable site that would allow the user to edit the XML without messing with the XML itself. We’ll see.

XML Directory entries


<?xml version="1.0"?>
<CiscoIPPhoneDirectory>
	<DirectoryEntry>
		<Name>Doe, John</Name>
		<Telephone>5551234567</Telephone>
	</DirectoryEntry>
	<DirectoryEntry>
		<Name>Smith, Jane</Name>
		<Telephone>5551234567</Telephone>
	</DirectoryEntry>
	<DirectoryEntry>
		<Name>williams, bob</Name>
		<Telephone>1000</Telephone>
	</DirectoryEntry>
</CiscoIPPhoneDirectory>

php code for phoneb>

);
$host_url = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$host_url = preg_replace('/\?.*/','',$host_url);
echo '<?xml version="1.0"?>';

function searchXML($first_name,$last_name, $array) {
    $search_directory = array();
    foreach ( $array as $dir_entry ) {

       if ( preg_match('/, '.strtolower($first_name).'.*$/',strtolower($dir_entry['Name'])) &&
        preg_match('/^'.strtolower($last_name).'.*/',strtolower($dir_entry['Name'])) ) {
           $search_directory[] = array(  "Name"=>$dir_entry['Name']
                                           ,"Telephone"=>$dir_entry['Telephone']);
       }
    }
   return $search_directory;
}

if ( ! isset($_GET['f']) && ! isset($_GET['l'])  ) {

echo<<<XML
<CiscoIPPhoneInput>
<Title>Directory Search</Title>
<Prompt>Enter search parameters...</Prompt>
<URL>http://172.20.10.142/phonedirectory/directory.php</URL>
<InputItem>
<DisplayName>First Name</DisplayName>
<QueryStringParam>f</QueryStringParam>
<InputFlags>U</InputFlags>
<DefaultValue></DefaultValue>
</InputItem>
<InputItem>
<DisplayName>Last Name</DisplayName>
<QueryStringParam>l</QueryStringParam>
<InputFlags>U</InputFlags>
<DefaultValue></DefaultValue>
</InputItem>
<SoftKeyItem>
<Position>1</Position>
<Name>Search</Name>
<URL>SoftKey:Submit</URL>
</SoftKeyItem>
<SoftKeyItem>
<Position>2</Position>
<Name>&lt;&lt;</Name>
<URL>SoftKey:&lt;&lt;</URL>
</SoftKeyItem>
<SoftKeyItem>
<Position>3</Position>
<Name>Cancel</Name>
<URL>SoftKey:Cancel</URL>
</SoftKeyItem>
</CiscoIPPhoneInput>

XML;

} else {

    $directory = json_decode(json_encode((array)simplexml_load_string(file_get_contents('./directory.xml'))),1);

    if ( isset($_GET['f']) ) {
        $f = $_GET['f'];
    } else {
        $f = "";
    }
    if ( isset($_GET['l']) ) {
        $l = $_GET['l'];
    } else {
        $l = "";
    }

    $results = searchXML($f,$l,$directory['DirectoryEntry']);

    echo "\n<CiscoIPPhoneDirectory>\n";

    $result_count = sizeof($results);
    if (  $result_count == 0 ) {
        echo "<Prompt>No Records Found</Prompt>";
    } else {
        if  ( isset($_GET['page']) ){
            $page = $_GET['page'];
        } else {
            $page = 1;
        }

        echo "<Title>Search Results</Title>\n<Prompt>Page ".$page." of ".ceil( $result_count / 32 )."</Prompt>\n";

        $i = ($page * 32) - 31;
        while ( $i <= (32 * $page) && $i <= $result_count ){
            echo "<DirectoryEntry>\n";
            echo "\t<Name>".$results[$i - 1]['Name']."</Name>\n";
            echo "\t<Telephone>".$results[$i - 1]['Telephone']."</Telephone>\n";
            echo "</DirectoryEntry>\n";

            $i++;

        }
    } 
    echo "<SoftKeyItem>\n\t<Name>Dial</Name>\n\t<URL>SoftKey:Dial</URL>\n\t<Position>1</Position>\n</SoftKeyItem>\n";
        echo "<SoftKeyItem>\n\t<Name>Prev</Name>\n\t";
        if ( $page > 1 ){
            echo "<URL>".$host_url."?f=".$f."&amp;l=".$l."&amp;page=".( $page - 1 )."</URL>\n";
        }
        echo "\t<Position>2</Position>\n</SoftKeyItem>";

        echo "<SoftKeyItem>\n\t<Name>Next</Name>\n\t";
        if ( $page < ceil($result_count / 32 ) ){
            echo "<URL>".$host_url."?f=".$f."&amp;l=".$l."&amp;page=".( $page + 1 )."</URL>\n";
        }
        echo "\t<Position>3</Position>\n</SoftKeyItem>";

    echo "<SoftKeyItem>\n\t<Name>Exit</Name>\n\t<URL>SoftKey:Exit</URL>\n\t<Position>4</Position>\n</SoftKeyItem>\n";
    echo "</CiscoIPPhoneDirectory>";
}

?>