A Sample LDAP Application in PHP Page 15
February 22, 2002
As mentioned earlier, empdir_functions.php has a common set of
functions used by other scripts. The functions are of two types -
display related functions that print the HTML and utility
functions such as those that encapsulate the logic of connecting
and binding to the directory:
<?php
// empdir_functions.php
// Common functions go here
// Avoid multiple includes of this file
if (isset($EMPDIR_FUNCS)) {
return;
} else {
$EMPDIR_FUNCS = "true";
}
This function generates a standard HTML page with a heading
passed to it as an argument. This ensures a uniform look for the
pages of the application:
function generateHTMLHeader($message)
{
printf ("<head> <title> Foo Widgets - Employee Directory </title>
</head>");
printf("<body text="\"#000000\"" bgcolor="\"#999999\""
link="\"#0000EE\""
vlink="\"#551A8B\"" alink="\"#FF0000\"">\n");
printf("<h1>Foo Widgets Employee Directory</h1><br><br>");
printf("<table cellpadding="\"4\"" cellspacing="\"0\""
border="\"0\"" width="\"600\"">");
printf("<tr bgcolor="\"#dcdcdc\""><td><font face="\"Arial\""><b>");
printf("%s</b></font><br></td>", $message);
printf("<td align="\"right\"">");
printf("</font></td></tr>");
printf("</table>");
printf("<br>");
printf("<br>");
}
[The red lines above are one line. They have been split for
formatting purposes.]
This function generates the first page seen in the earlier
screenshot. It outputs an HTML form which allows the user to
choose between searching for entries or adding a new entry:
function generateFrontPage()
{
printf("<form method="\"post\"" action="\"empdir_first.php\"">");
printf("<input type="\"submit\"" name="\"choice\""
value="\"SEARCH\"">");
printf(" ");
printf("<input type="\"submit\"" name="\"choice\"" value="\"ADD\"">");
printf("<br>");
printf("<br>");
printf("<ul>");
printf("<li> Search for employees by clicking <i>SEARCH FOR
EMPLOYEE</i> </li>");
printf("<li> Add new employees (Admin only) by clicking <i>ADD A NEW
EMPLOYEE</i> </li>");
printf("<li> Modify employee details by clicking <i>SEARCH FOR
EMPLOYEES</i> first and then choosing the entry to
Modify</li>");
printf("<li> Delete an existing entry (Admin only) by clicking
<i>SEARCH FOR EMPLOYEES</i> first and then choosing the
entry to Delete</li>");
printf("</form>");
}
[The red lines above are one line. They have been split for
formatting purposes.]
This function generates HTML that prompts the user for the
administrator's password while attempting to delete a user entry
from the directory. The hidden form fields are required to re-
construct the DN of the entry that is to be deleted, provided the
authentication succeeds. Such a scheme is more illustrative than
the definitive method to do this since the focus is on LDAP APIs.
In a production environment, this information should be stored in
HTTP sessions:
function promptPassword($mail, $ou, $actionScript)
{
printf("<form method="\"GET\"" action="\"%s\"">", $actionScript);
printf("Admin Password: <input type="\"password\""
name="\"adminpassword\""> ");
printf("<input type="\"hidden\"" name="\"mail\"" value="\"%s\"">",
urlencode($mail));
printf("<input type="\"hidden\"" name="\"ou\"" value="\"%s\"">",
urlencode($ou));
printf("<input type="\"submit\"" name="\"submit\""
value="\"Submit\"">");
printf("</form>");
}
[The red lines above are one line. They have been split for
formatting purposes.]
Standard mechanism to print out an error message in HTML:
function displayErrMsg($message)
{
printf("<blockquote><blockquote><blockquote><h3><font
color="\"#cc0000\"">%s</font></h3></blockquote>
</blockquote></blockquote>\n", $message);
}
A Sample LDAP Application in PHP Page 14
Professional PHP4 Programming
A Sample LDAP Application in PHP Page 16
|