<?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>LDAP &#8211; VeriTeknik</title>
	<atom:link href="https://www.veriteknik.net.tr/en/category/knowledge_base/databases/ldap/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.veriteknik.net.tr/en/</link>
	<description>VeriTeknik Bilişim &#124; VeriTeknik Bilişim</description>
	<lastBuildDate>Sun, 25 Nov 2012 00:12:55 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Simple LDAP Class for PHP</title>
		<link>https://www.veriteknik.net.tr/en/simple-ldap-class-for-php/</link>
					<comments>https://www.veriteknik.net.tr/en/simple-ldap-class-for-php/#respond</comments>
		
		<dc:creator><![CDATA[ckaraca]]></dc:creator>
		<pubDate>Sun, 25 Nov 2012 00:12:55 +0000</pubDate>
				<category><![CDATA[LDAP]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Developer]]></category>
		<guid isPermaLink="false">http://www.plugged.in/?p=652</guid>

					<description><![CDATA[<p>In this post, i&#8217;m gonna explain how to connect to a LDAP server via using PHP. First i wanna talk about some definitions; LDAP means Lightweight Directory Access Protocol. As you can understand from it&#8217;s name, it is a database which uses directory-tree based structure. It&#8217;s used by OpenLDAP, Sun Directory Server, Microsoft Active Directory [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.veriteknik.net.tr/en/simple-ldap-class-for-php/">Simple LDAP Class for PHP</a> appeared first on <a rel="nofollow" href="https://www.veriteknik.net.tr/en/">VeriTeknik</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this post, i&#8217;m gonna explain how to connect to a LDAP server via using PHP.</p>
<p>First i wanna talk about some definitions;</p>
<p><strong>LDAP</strong> means Lightweight Directory Access Protocol.</p>
<p>As you can understand from it&#8217;s name, it is a database which uses directory-tree based structure.</p>
<p>It&#8217;s used by OpenLDAP, Sun Directory Server, Microsoft Active Directory and such directory services.</p>
<p><strong>LDIF</strong> means LDAP Data Interchange Format.</p>
<p>You can easily execute LDAP queries using ldif files. For example;</p>
<pre class="brush: powershell; gutter: true; first-line: 1">dn: cn=John Doe,dc=example,dc=com
cn: John Doe
givenName: John
sn: Doe
telephoneNumber: +1 888 555 6789
telephoneNumber: +1 888 555 1232
mail: john@example.com
Manager: cn=Jane Doe,dc=example,dc=com
objectClass: inetOrgPerson
objectClass: organizationalPerson
objectClass: person
objectClass: top</pre>
<p>You can see some other terms(actually attributes) in the LDAP query above. Such as dn: Distinguished Name, cn: Common Name, sn: Surname.</p>
<p>I&#8217;ll give more information about LDAP later in another post.</p>
<p><strong>PHP LDAP Functions</strong></p>
<p>PHP has it&#8217;s own LDAP functions by it&#8217;s own. But i&#8217;ve decided to write a class for easier access to these functions.</p>
<p>First, you have to include our class below.</p>
<pre class="brush: php; gutter: true; first-line: 1">&lt;?php 

class LDAP{

    public  $ldapserver = "99.245.56.89";
    public  $ldapport = "389";
    public  $basedn = "cn=admin,dc=web-sistem,dc=com";
    public  $basepass = "yourldap_password"; 

	function connect($server,$port){

		echo "Connecting to LDAP Server..."; 
		$connection = ldap_connect($server,$port);  // must be a valid LDAP server!	
		ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);

		// PHP Reference says there is no control of connection status in OpenLDAP 2.x.x
		// So we'll use binding function to check connection status.

		return $connection;

	}

	function bind($connection,$basedn,$basepass){

		echo "&lt;br&gt;&lt;br&gt;Binding...&lt;br&gt;";  
		$ldaprdn  = $basedn;    // ldap rdn or dn 
		$ldappass = $basepass;  // associated password
		$bind = ldap_bind($connection, $ldaprdn, $ldappass);

		if ($bind) { 
			echo "LDAP bind successful...";

		} else { 
			echo "LDAP bind failed..."; 
		}
	}

	function search($connection, $searchdn, $filter, $attributes = array()){

		$sr = ldap_search($connection, $searchdn, $filter, $attributes);

		if ($sr) {
			echo 'Search Succeeded. Getting Entries...&lt;br&gt;';

	        echo "Number of entires returned:  " . ldap_count_entries($connection, $sr) . "&lt;br /&gt;";

	        $info = ldap_get_entries($connection, $sr);

	        echo "Data for " . $info["count"] . " items returned:&lt;p&gt;\n";

			print_r($info);
			echo '&lt;hr&gt;&lt;br&gt;';

		} else {
			echo 'Search Failed.&lt;br&gt;';
		}
	}

	function addRecord($connection, $adddn, $record){

	    $addProcess = ldap_add($connection, $adddn, $record);

	    if($addProcess){
	        echo "Entry added&lt;br&gt;";
			echo '&lt;hr&gt;&lt;br&gt;';
	    } else {
	    	echo "Please check your data&lt;br&gt;";
			echo '&lt;hr&gt;&lt;br&gt;';
	    }
	}

	function modifyRecord($connection, $modifydn, $record){
		$modifyProcess = ldap_modify($connection, $modifydn, $record);
	    if($modifyProcess){
	        echo "Entry modified&lt;br&gt;";
			echo '&lt;hr&gt;&lt;br&gt;';
	    } else {
	    	echo "Please check your data&lt;br&gt;";
			echo '&lt;hr&gt;&lt;br&gt;';
	    }
	}

	function deleteRecord($connection, $dn, $recursive = false){
	    echo "Deleting Record...&lt;br&gt;";

	    if($recursive == false){
	    	echo 'Entry: ' . $dn . ' deleted.';
	        return(ldap_delete($connection, $dn));
	    } else {

	        // Search for child entries	        
	        $sr = ldap_list($connection, $dn, "ObjectClass=*", array(""));
	        $info = ldap_get_entries($connection, $sr);

	        for($i=0;$i&lt;$info['count'];$i++){
	            // Recursive delete child entries - using myldap_delete to recursive deletion
	            $result = myldap_delete($connection, $info[$i]['dn'], $recursive);
	            if(!$result){
	                // return status code if deletion fails.
	                return($result);
	            }
	        }
			// Delete top dn
			echo 'Entry: ' . $dn . ' deleted.';
	        return(ldap_delete($connection, $dn));
	    }
	}

	function close($connection){
		echo '&lt;hr&gt;&lt;br&gt;';
	    echo "Closing connection";
	    ldap_close($connection);
	}

}

?&gt;</pre>
<p>And then you may want to initialize and use our class. Usage is so simple.</p>
<pre class="brush: php; gutter: true; first-line: 1">&lt;?php

	require_once('class.ldap.php');

	$ldap = new LDAP();

	// Connect to LDAP Server  - connect(ldap_server, port)
	$connection = $ldap-&gt;connect($ldap-&gt;ldapserver,$ldap-&gt;ldapport);

	// Bind with LDAP instance
	$ldap-&gt;bind($connection,'cn=admin,dc=web-sistem,dc=com','yourldap_password');

	echo '&lt;hr&gt;&lt;br&gt;';

	// Search LDAP directory

	// Search with a wildcard
	$ldap-&gt;search($connection,'o=hosting,dc=web-sistem,dc=com','vd=*');

	// Search with no attributes specified	
	$ldap-&gt;search($connection,'o=hosting,dc=web-sistem,dc=com','vd=plugged.in');

	// Search with attributes (attributes must be an array)

	$ldap-&gt;search($connection,'o=hosting,dc=web-sistem,dc=com','vd=web-sistem.com', array('custID'));

	// Prepare data to insert

	// Please change the record entry as required by your company directory structure

	$insert_data['objectclass'][0] = "top";
	$insert_data['objectclass'][1] = "VirtualDomain";

	$insert_data["accountActive"] = "TRUE";
	$insert_data["delete"] = "FALSE";
	$insert_data["lastChange"] = "103";
	$insert_data["vd"] = "plugged.in";
	$insert_data["adminID"] = "3";
	$insert_data["custID"] = "2";
	$insert_data["editAV"] = "FALSE";
	$insert_data["maxAlias"] = "20";
	$insert_data["maxMail"] = "22";
	$insert_data["maxQuota"] = "300";
	$insert_data["postfixTransport"] = "maildrop:";

	// LDAP Insert DN		
	$addDN = "vd=plugged.in,o=hosting,dc=web-sistem,dc=com";
	$ldap-&gt;addRecord($connection,$addDN,$insert_data);

	// Prepare data to modify

	// Please change the record entry as required by your company directory structure

	$modify_data["adminID"] = "3213";
	$modify_data["custID"] = "2441";

	// LDAP Modify DN		
	$modifyDN = "vd=plugged.in,o=hosting,dc=web-sistem,dc=com";
	$ldap-&gt;modifyRecord($connection,$modifyDN,$modify_data);

	// Delete LDAP record	(third parameter is "Recursive")
	$deleteDN = "vd=plugged.in,o=hosting,dc=web-sistem,dc=com";
	$ldap-&gt;deleteRecord($connection,$deleteDN,true);

	//Close LDAP Connection
	$ldap-&gt;close($connection);
?&gt;</pre>
<p><strong>That&#8217;s all for now.</strong></p>
<p><strong>If you have questions or advices about this class please leave a message in comments section.</strong></p>
<p>The post <a rel="nofollow" href="https://www.veriteknik.net.tr/en/simple-ldap-class-for-php/">Simple LDAP Class for PHP</a> appeared first on <a rel="nofollow" href="https://www.veriteknik.net.tr/en/">VeriTeknik</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.veriteknik.net.tr/en/simple-ldap-class-for-php/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">652</post-id>	</item>
	</channel>
</rss>
