<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>LdapGuru  - all about LDAP, IT news, and IT art &#187; java</title>
	<atom:link href="http://www.ldapguru.com/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ldapguru.com</link>
	<description>Do it right the first time around.</description>
	<lastBuildDate>Wed, 23 Jun 2010 07:19:56 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Create an LDAP Address Book</title>
		<link>http://www.ldapguru.com/2009/10/create-an-ldap-address-book/</link>
		<comments>http://www.ldapguru.com/2009/10/create-an-ldap-address-book/#comments</comments>
		<pubDate>Fri, 23 Oct 2009 13:45:18 +0000</pubDate>
		<dc:creator>ls</dc:creator>
				<category><![CDATA[Manual]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ldap]]></category>

		<guid isPermaLink="false">http://ldapguru.php5.dev.justwebit.ru/?p=7</guid>
		<description><![CDATA[This article will attempt to demonstrate how to connect to an LDAP server using PHP. Specifically, the example given will connect to a public LDAP server and perform searches. This example closely mimics the way Netscape® Communicator 4.* uses its address book to connect to LDAP resources.
Introduction to LDAP
Many have probably heard much about LDAP, [...]]]></description>
			<content:encoded><![CDATA[<p>This article will attempt to demonstrate how to connect to an LDAP server using PHP. Specifically, the example given will connect to a public LDAP server and perform searches. This example closely mimics the way Netscape® Communicator 4.* uses its address book to connect to LDAP resources.<span id="more-7"></span></p>
<h4>Introduction to LDAP</h4>
<p>Many have probably heard much about LDAP, but have no idea what it is or how it works. I will not attempt to teach everything there is to know about LDAP, but here is a brief description of the protocol. LDAP is a protocol for distributing directory information to many different resources. Most commonly it is used as a centralized address book, but it can be much more powerful depending on an organization&#8217;s needs. LDAP in its most basic form is a standard way to connect to a database. The database is optimized for read queries. Thus, it retrieves information very quickly, in contrast to additions or updates which are slower. It is important to note that LDAP most often uses a hierarchal database, rather than a relational database to store data. Therefore, the structure is better represented with a tree than a table. As a result, SQL syntax will be rendered unusable. In short, LDAP is a fast way to retrieve centralized, static data containing information about people and/or resources.</p>
<h4>Requirements</h4>
<ul>
<li> PHP v.4 (previous versions may work but are untested) compiled with support for LDAP, I.E. <em>&#8211;with-ldap</em>.</li>
<li> Publically accessible LDAP directory. Two are provided in the example.</li>
</ul>
<h4>Overview of Example</h4>
<ol>
<li> Setup Public LDAP Server Information</li>
<li> Create LDAP Query</li>
<li> Connect to LDAP Server</li>
<li> Process Query if Connection Was Successful</li>
<li> Format Output</li>
<li> Close Connection</li>
<li> Make HTML Form for Search Interface</li>
<li> Echo Results</li>
</ol>
<h4>Setup Public LDAP Server Information</h4>
<p>The first thing we need to do is define all of the LDAP servers we might want to search. <em>&#8220;LDAP_NAME&#8221;</em><br />
= The name of the new LDAP entry. <em>&#8220;LDAP_SERVER&#8221;</em><br />
= The IP address or hostname of the new LDAP entry. <em>&#8220;LDAP_ROOT_DN&#8221;</em><br />
= The root distinguished name of the new LDAP entry.</p>
<pre class="brush: php">

&lt;?php
$LDAP_NAME[0] = &quot;Netscape Net Center&quot;;
$LDAP_SERVER[0] = &quot;memberdir.netscape.com&quot;;
$LDAP_ROOT_DN[0] = &quot;ou=member_directory,o=netcenter.com&quot;;
$LDAP_NAME[1] = &quot;Bigfoot&quot;;
$LDAP_SERVER[1] = &quot;ldap.bigfoot.com&quot;;
$LDAP_ROOT_DN[1] = &quot;&quot;;
//If no server chosen set it to 0
if(!$SERVER_ID) $SERVER_ID=0; ?&gt;;
</pre>
<h4>Create LDAP Query</h4>
<p>As mentioned previously, LDAP queries are not much like SQL queries. Therefore, the syntax may seem a bit limiting, but here is a basic example and one that works in this scenario.</p>
<pre class="brush: php">
//Create Query
$ldap_query = &quot;cn=$common&quot;;
</pre>
<p>In our example <em>&#8220;cn&#8221;</em><br />
is the attribute on which we are performing the search, and <em>$common</em><br />
is the search string variable from the search form. LDAP query syntax allows for wildcard matching using &#8216;*&#8217;. For example, &#8216;*stanley&#8217; will find &#8216;dan stanley&#8217;.</p>
<h4>Connect to LDAP Server</h4>
<p>The given function connects to an LDAP resource and assigns the connection link identifier to a variable, much like connecting to a regular database, like MySQL.</p>
<pre class="brush: php">
&lt;?php
//Connect to LDAP
$connect_id = ldap_connect($LDAP_SERVER[$SERVER_ID]); ?&gt;;
</pre>
<p>In our example, <em>&#8220;$connect_id&#8221;</em><br />
is the link identifier, <em>$LDAP_SERVER</em><br />
is the array of possible ldap servers, and <em>$SERVER_ID</em><br />
is the LDAP server variable from the search form.</p>
<h4>Process Query if Connection Was Successful</h4>
<p>If our connection was successful, we will have a valid LDAP link identifier and we can process the query.</p>
<pre class="brush: php">
&lt;?php
if($connect_id) {
//Authenticate
$bind_id = ldap_bind($connect_id);
//Perform Search
$search_id = ldap_search($connect_id, $LDAP_ROOT_DN[$SERVER_ID], $ldap_query);
//Assign Result Set to an Array
$result_array = ldap_get_entries($connect_id, $search_id);
} else {
//Echo Connection Error
echo &quot;Could not connect to LDAP server: $LDAP_SERVER[$SERVER_ID]&quot;; } ?&gt;;
</pre>
<p>Once we have established a connection to the LDAP services, we must identify ourselves. Most database connections with PHP send the username and password with the connection. However, with LDAP, credentials are unknown until a <em>bind</em><br />
is performed. In our example, <em>&#8220;$bind_id&#8221;</em><br />
is the bind link identifier. We are performing an anonymous bind to the public LDAP servers. Therefore, no argument is sent to <em>ldap_bind()</em><br />
accept the connection link identifier. After we have been authorized, via bind as anonymous, we perform the query using the <em>ldap_search()</em><br />
function. <em>$search_id</em><br />
is created and is our search link identifier. Then, we assign our result set to the variable <em>$result_array</em><br />
using the function <em>ldap_get_entries()</em>. This will allow us to sort the information in a logical manner for display.</p>
<h4>Format Output</h4>
<p>When an LDAP search is performed, the data is returned in whatever sequence it is found. In other words, there is not an easy way to sort the data like with a common SQL <em>ORDER BY</em><br />
statement. As well, many public LDAP directories do not have standard capitalization. Since the sort is based on the ASCII value of the strings, we must format the strings with all lowercase letters to appropriately alphabetize our output. It is important to note, that an LDAP result set is returned as a multi-dimensional array. Thus, at this point in our script <em>$result_array</em><br />
contains something like this:</p>
<pre class="brush: php">
$result_array[0][&quot;cn&quot;] [0] = &quot;Dannie Stanley&quot; [&quot;dn&quot;] [0] = &quot;uid=dannie,dc=spinweb.net&quot; [&quot;givenname&quot;][0] = &quot;Dannie&quot; [&quot;sn&quot;] [0] = &quot;Stanley&quot; [&quot;mail&quot;] [0] = &quot;danSPAM@spinweb.net&quot; $result_array[1][&quot;cn&quot;] [0] = &quot;Michael Reynolds&quot; [&quot;dn&quot;] [0] = &quot;uid=michael,dc=spinweb.net&quot; [&quot;givenname&quot;][0] = &quot;Michael&quot; [&quot;sn&quot;] [0] = &quot;Reynolds&quot; [&quot;mail&quot;] [0] = &quot;michaelSPAM@spinweb.net&quot;
</pre>
<p>The data is stored in this format because each attribute may have more than one value (IE a tree structure). For example, if my name is &#8216;Dannie,&#8217; yet everyone knows me as &#8216;Dan,&#8217; I could add an attribute to LDAP to store both representations of my given name like this:</p>
<pre class="brush: php">
$result_array[0][&quot;cn&quot;] [0] = &quot;Dannie Stanley&quot; [&quot;dn&quot;] [0] = &quot;uid=dannie,dc=spinweb.net&quot; [&quot;givenname&quot;][0] = &quot;Dannie&quot; [&quot;givenname&quot;][0] = &quot;Dan&quot; [&quot;sn&quot;] [0] = &quot;Stanley&quot; [&quot;mail&quot;] [0] = &quot;danSPAM@spinweb.net&quot;
</pre>
<p>For this search, we are only worried about the first value of every attribute so we will be using 0 as the index for each attribute, except for <em>dn</em><br />
(Distinguished Name), which contains only one value. Here is a brief list of attributes and their meaning: <em>&#8220;cn&#8221;</em><br />
= Common Name <em>&#8220;dn&#8221;</em><br />
= Distinguished Name <em>&#8220;givenname&#8221;</em><br />
= First Name <em>&#8220;sn&#8221;</em><br />
= Last Name <em>&#8220;mail&#8221;</em><br />
= Email Address</p>
<pre class="brush: php">
&lt;?php

//Sort results if search was successful
if($result_array)
{
for($i=0; $i&lt;count($result_array); $i++)
{ $format_array[$i][0] = strtolower($result_array[$i][&quot;cn&quot;][0]);
$format_array[$i][1] = $result_array[$i][&quot;dn&quot;];
$format_array[$i][2] = strtolower($result_array[$i][&quot;givenname&quot;][0]);
$format_array[$i][3] = strtolower($result_array[$i][&quot;sn&quot;][0]);
$format_array[$i][4] = strtolower($result_array[$i][&quot;mail&quot;][0]);
}
//Sort array
sort($format_array, &quot;SORT_STRING&quot;);
for($i=0; $i&lt;count($format_array); $i++)
{
$cn = $format_array[$i][0];
$dn = $format_array[$i][1];
$fname = ucwords($format_array[$i][2]);
$lname = ucwords($format_array[$i][3]);
$email = $format_array[$i][4];
if($dn &amp;amp;&amp;amp; $fname &amp;amp;&amp;amp; $lname &amp;amp;&amp;amp; $email)
{ $result_list .= &quot;&lt;
A HREF=&quot;ldap://$LDAP_SERVER[$SERVER_ID]/$dn&quot;&gt;$fname $lname&lt;/a&gt;&quot;;
$result_list .= &quot; &lt;&lt;a HREF=&quot;mailto:$email&quot;&gt;$email&lt;/a&gt;&gt;&lt;br /&gt; &quot;;
}
elseif
($dn &amp;amp;amp;&amp;amp;amp; $cn &amp;amp;amp;&amp;amp;amp; $email)
{ $result_list .= &quot;&lt;a HREF=&quot;ldap://$LDAP_SERVER[$SERVER_ID]/$dn&quot;&gt;$cn&lt;/a&gt;&quot;; $result_list .= &quot; &lt;&lt;a HREF=&quot;mailto:$email&quot;&gt;$email&lt;/a&gt;&gt;&lt;br /&gt; &quot;; } } }
else
{ echo &quot;Result set empty for query: $ldap_query&quot;; } ?&gt;
</pre>
<p>In our example, <em>$format_array</em><br />
is our new array which contains the query results in a format optimized for output. First, we loop through every element of the <em>$result_array</em><br />
and assign it to a two-dimensional array for sorting purposes. At the same time we are using the <em>strtolower()</em><br />
function to make all values lower-case. Second, we sort the array using a handy little search algorithm provided by PHP called <em>sort()</em>. The first argument is the array. The second is what type of sorting to perform, as defined by the PHP documentation. Since we are sorting by string, we use <em>&#8220;SORT_STRING&#8221;</em>. Third, we loop through the newly formatted array and assign it to an output string named <em>$result_list</em><br />
that contains the HTML representation of the data. It is important to note that I have used the ldap URL format for the hyper-links. An example of this looks something like this: HREF=&#8221;ldap://ldap.domain.net/uid=dannie,dc=domain.net&#8221;.</p>
<h4>Close Connection</h4>
<p>Now that we have all of our data contained in <em>$result_list</em>, we can safely disconnect from the LDAP connection.</p>
<pre class="brush: php">
&lt;?php

//Close Connection
ldap_close($connect_id); ?&gt;; </pre>
<h4>Make HTML Form for Search Interface</h4>
<p>Finally, we get to the HTML output of the script. This set of code prints out the form that is used for performing the searches.</p>
<pre class="brush: php">
&lt;?php

//Make Form
echo &quot;&lt;center&gt;&lt;form ACTION=&quot;$PHP_SELF&quot; METHOD=&quot;GET&quot;&gt;&quot;;
echo &quot;Search in:&lt;select NAME=&quot;SERVER_ID&quot;&gt;&quot;;
//Loop Through and Create SELECT OPTIONs
for($i=0; $i&lt;count($LDAP_NAME); $i++) echo &quot;&lt;option VALUE=&quot;$i&quot;&gt;&quot;.$LDAP_NAME[$i].&quot;&lt;/option&gt;&quot;;
echo &quot;&lt;/select&gt;&lt;br /&gt;&quot;; echo &quot;Search for:&lt;input TYPE=&quot;text&quot; NAME=&quot;common&quot;&gt;&quot;;
echo &quot;&lt;input TYPE=&quot;submit&quot; NAME=&quot;lookup&quot; VALUE=&quot;go&quot;&gt;&lt;br /&gt;&quot;;
echo &quot;(You can use * for wildcard searches, ex. * Stanley will find all Stanleys)&lt;br /&gt;&quot;; echo &quot;&lt;/form&gt;&lt;/center&gt;&quot;; ?&gt;
</pre>
<p>The only portions of this code that is interpreted is <em>$PHP_SELF</em><br />
which is a global constant for the name of the script itself and the loop that creates the SELECT box from our <em>$LDAP_NAME</em><br />
variable.</p>
<h4>Echo Results</h4>
<p>Now that all of the work has been done, we print out the result set. If no results were returned, a message is given stating the same.</p>
<pre class="brush: php">
&lt;?php

//Echo Results
if($result_list) {
echo &quot;&lt;center&gt;&lt;table BORDER=&quot;1&quot; CELLSPACING=&quot;0&quot; CELLPADDING=&quot;10&quot; BGCOLOR=&quot;#FFFFEA&quot; WIDTH=&quot;450&quot;&gt;&lt;tr&gt;&lt;td&gt;$result_list&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt;&lt;/center&gt;&quot;; } else echo &quot;No Results&quot;; ?&gt;</pre>
<h4>Source Code</h4>
<p>Here is the complete source code. Simply cut and paste this into a valid HTML document and give it a try.</p>
<pre class="brush: php">
&lt;?php

$LDAP_NAME[0] = &quot;Netscape Net Center&quot;;
$LDAP_SERVER[0] = &quot;memberdir.netscape.com&quot;;
$LDAP_ROOT_DN[0] = &quot;ou=member_directory,o=netcenter.com&quot;; $LDAP_NAME[1] = &quot;Bigfoot&quot;;
$LDAP_SERVER[1] = &quot;ldap.bigfoot.com&quot;;
$LDAP_ROOT_DN[1] = &quot;&quot;;
//If no server chosen set it to 0
if(!$SERVER_ID) $SERVER_ID=0;
//Create Query
$ldap_query = &quot;cn=$common&quot;;
//Connect to LDAP
$connect_id = ldap_connect($LDAP_SERVER[$SERVER_ID]);
if($connect_id) {
//Authenticate
$bind_id = ldap_bind($connect_id);
//Perform Search
$search_id = ldap_search($connect_id, $LDAP_ROOT_DN[$SERVER_ID], $ldap_query);
//Assign Result Set to an Array
$result_array = ldap_get_entries($connect_id, $search_id); } else {
//Echo Connection Error
echo &quot;Could not connect to LDAP server: $LDAP_SERVER[$SERVER_ID]&quot;; }
//Sort results if search was successful
if($result_array) { for($i=0; $i&lt;count($result_array); $i++) {
$format_array[$i][0] = strtolower($result_array[$i][&quot;cn&quot;][0]); $format_array[$i][1] = $result_array[$i][&quot;dn&quot;]; $format_array[$i][2] = strtolower($result_array[$i][&quot;givenname&quot;][0]); $format_array[$i][3] = strtolower($result_array[$i][&quot;sn&quot;][0]); $format_array[$i][4] = strtolower($result_array[$i][&quot;mail&quot;][0]); }
//Sort array
sort($format_array, &quot;SORT_STRING&quot;); for($i=0; $i&lt;count($format_array); $i++) { $cn = $format_array[$i][0]; $dn = $format_array[$i][1]; $fname = ucwords($format_array[$i][2]); $lname = ucwords($format_array[$i][3]); $email = $format_array[$i][4]; if($dn &amp;amp;amp;&amp;amp;amp; $fname &amp;amp;amp;&amp;amp;amp; $lname &amp;amp;amp;&amp;amp;amp; $email) {
$result_list .= &quot;&lt;a HREF=&quot;ldap://$LDAP_SERVER[$SERVER_ID]/$dn&quot;&gt;$fname $lname&lt;
/A&gt;&quot;; $result_list .= &quot; &lt;&lt;a HREF=&quot;mailto:$email&quot;&gt;$email&lt;/a&gt;&gt;&lt;br /&gt; &quot;; } elseif($dn &amp;amp;amp;&amp;amp;amp; $cn &amp;amp;amp;&amp;amp;amp; $email) {
$result_list .= &quot;&lt;a HREF=&quot;ldap://$LDAP_SERVER[$SERVER_ID]/$dn&quot;&gt;$cn&lt;/a&gt;&quot;; $result_list .= &quot; &lt;&lt;a HREF=&quot;mailto:$email&quot;&gt;$email&lt;/a&gt;&gt;&lt;br /&gt; &quot;; } } } else {
echo &quot;Result set empty for query: $ldap_query&quot;; }
//Close Connection
ldap_close($connect_id);
//Make Form
echo &quot;&lt;center&gt;&lt;form ACTION=&quot;$PHP_SELF&quot; METHOD=&quot;GET&quot;&gt;&quot;;
echo &quot;Search in:&lt;select NAME=&quot;SERVER_ID&quot;&gt;&quot;;
//Loop Through and Create SELECT OPTIONs
for($i=0; $i&lt;count($LDAP_NAME); $i++)
echo &quot;&lt;option VALUE=&quot;$i&quot;&gt;&quot;.$LDAP_NAME[$i].&quot;&lt;/option&gt;&quot;; echo &quot;&lt;/select&gt;&lt;br /&gt;&quot;; echo &quot;
Search for:&lt;input TYPE=&quot;text&quot; NAME=&quot;common&quot;&gt;&quot;; echo &quot;&lt;input TYPE=&quot;submit&quot; NAME=&quot;lookup&quot; VALUE=&quot;go&quot;&gt;&lt;br /&gt;&quot;; echo &quot;(You can use * for wildcard searches, ex. * Stanley will find all Stanleys)&lt;br /&gt;&quot;; echo &quot;&lt;/form&gt;&lt;/center&gt;&quot;;
//Echo Results
if($result_list) { echo &quot;&lt;center&gt;&lt;table BORDER=&quot;1&quot; CELLSPACING=&quot;0&quot; CELLPADDING=&quot;10&quot; BGCOLOR=&quot;#FFFFEA&quot; WIDTH=&quot;450&quot;&gt;&lt;tr&gt;&lt;td&gt;$result_list&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt;&lt;/center&gt;&quot;; } else echo &quot;No Results&quot;; } ?&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.ldapguru.com/2009/10/create-an-ldap-address-book/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>LDAP Login Example</title>
		<link>http://www.ldapguru.com/2009/08/ldap-login-example/</link>
		<comments>http://www.ldapguru.com/2009/08/ldap-login-example/#comments</comments>
		<pubDate>Sun, 02 Aug 2009 13:43:43 +0000</pubDate>
		<dc:creator>ls</dc:creator>
				<category><![CDATA[Manual]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ldap]]></category>

		<guid isPermaLink="false">http://ldapguru.php5.dev.justwebit.ru/?p=3</guid>
		<description><![CDATA[This app provides a simple example of login authentication using Netscape&#8217;s LDAP Java API.
Release Notes
For Solaris, make sure to compile on the deployed server to ensure all necessary classes are                          [...]]]></description>
			<content:encoded><![CDATA[<p>This app provides a simple example of login authentication using Netscape&#8217;s LDAP Java API.<span id="more-36"></span></p>
<h2>Release Notes</h2>
<p>For Solaris, make sure to compile on the deployed server to ensure all necessary classes are                           included in the CLASSPATH.</p>
<p>The example contains one AppLogic, Login.java (plus the standard base classes Session and                           BaseAppLogic), and two HTML files: LDAPLogin.html and wrongLogin.html. The login page,                           LDAPLogin.html, lets the user enter a user name and password, which are used to authenticate                           the user on the LDAP server. If successful, the user&#8217;s LDAP attributes are streamed out to the                           client. (In a real app, this would probably call a method to extract a user ID or role to save to the                           session, and then call another App Logic that goes into the application itself.) If the login is not                           successful, then the user is redirected to wrongLogin.html and allowed to try again, up to a                           maximum number of failures, after which all login attempts will be refused, until the user&#8217;s                           session expires.</p>
<p>In addition to the standard execute() and guid() method, the Login AppLogic contains the                           following methods:</p>
<ul>
<li>maxFailuresReached: determines if the user&#8217;s maximum number of failed logins has been                                reached.</li>
<li>getDN: gets the LDAP distinguished name for the specified user</li>
<li>authenticateUser: attempts to authenticate the user with the specified password on the                                specified LDAP server.To get this example working you need to do the following:</li>
<li>Put the LDAP JAR file, LDAPJDK.JAR, on your NAS machine, and add it to your system                                CLASSPATH.</li>
<li>Because Java classes with native methods must be loaded by the default Java VM                                classloader, aka the &#8216;primordial&#8217; classloader, instead of the KIVA classloader, you must                                edit a filter list in the registry. Start up kreg tool (Regedit on NT:                                HKEY_LOCAL_MACHINE).Navigate in the tree to the following key&#8230;SOFTWARE/KIVA/Enterprise/2.0/CCS0/SYSTEM_JAVAEdit the GX_CLASSPATH_CORE value. It&#8217;s default installed value should be&#8230;java.;gx.;com.kivasoft.Add &#8220;netscape.&#8221; so that it now looks like:java.;gx.;com.kivasoft.;netscape.</li>
<li>Then restart your Netscape Application Server.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.ldapguru.com/2009/08/ldap-login-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
