<?php
/*
  ldap_connect()    // establish connection to server
     |
  ldap_bind()       // anonymous or authenticated "login"
     |
  do something like search or update the directory
  and display the results
     |
  ldap_close()      // "logout"
*/

class LDAP {

	public static function getInstance($ldap = null) {
		static $_instance;
		if (!is_array($_instance)) {
			$_instance = array();
		}
		$ldapConfName = 'default_ldap';
		if (is_numeric($ldap) && $ldap > 0) {
			$ldapConfName = "ldap_{$ldap}";
		}

		if (!array_key_exists($ldapConfName, $_instance)) {
			$_instance[$ldapConfName] = null;

			Lib::loadClass('Config');
			$conf = Config::getConfFile($ldapConfName);
			if ($conf) {
				$version = V::get('version', 3, $conf);
				$host = V::get('host', '', $conf);
				$port = V::get('port', '', $conf);
				$user = V::get('user', '', $conf);
				$pass = V::get('pass', '', $conf);
				$base_dn = V::get('base_dn', '', $conf);

				if ($base_dn && $host) {
					if ($port && $host) {
						$host .= ":{$port}";
					}

					$clientClass = 'Core_Client_Ldap';
					Lib::loadClass($clientClass);
					if (class_exists($clientClass)) {
						$_instance[$ldapConfName] = new $clientClass($host, $user, $pass, $base_dn, $version);
					}
				} else {
					trigger_error("Config file for ldap {$ldapConfName} not set: base_dn, host!", E_USER_WARNING);
				}
			} else {
				trigger_error("Config file for ldap {$ldapConfName} not exists!", E_USER_WARNING);
			}
		}
		return $_instance[$ldapConfName];
	}

	public static function TEST_DUMP_1($ldap, $res) {
		$info = $ldap->get_entries($res);
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">info: ';print_r($info);echo'</pre>';

		echo'<pre style="overflow:auto;border:1px solid orange;">';
		// print number of entries found
		echo "Number of entries found: " . $ldap->count_entries($res) . "\n";
		// iterate over array and print data for each entry
		for ($i=0; $i < $info['count']; $i++) {
		//foreach ($info as $k_1 => $v_1) {
			echo"\n".'-------------------------';
			echo"\n".'dn: <em>('.$info[$i]['dn'].')</em>';
			for ($j=0; $j < $info[$i]['count']; $j++) {
			//foreach ($v_1 as $k_2 => $v_2) {
				$k2 = $info[$i][$j];
				echo"\n".'<b>'.$k2.'</b>: ';
				for ($k=0; $k < $info[$i][$k2]['count']; $k++) {
				//foreach ($v_2 as $k_3 => $v_3) {
					echo '"'.$info[$i][$k2][$k].'" ';
				}//end foreach
			}//end foreach
		}
		echo'</pre>';
	}

	public static function TEST_DUMP($ldap, $res) {
		$ldap = self::getInstance();

		echo'<pre style="overflow:auto;border:1px solid orange;max-height:400px;">';
		// print number of entries found
		echo "Number of entries found: " . $ldap->count_entries($res) . "\n";
		$entry = $ldap->first_entry($res);
		while ($entry) {
			$dn = $ldap->get_dn($entry);
			echo "<b>$dn</b>\n";
			$attrs = $ldap->get_attributes($entry);
			for ( $i=0; $i < $attrs['count']; $i++) {
				echo "$attrs[$i]: ";
				for ( $j=0; $j < $attrs[$attrs[$i]]['count']; $j++ ) {
					echo $attrs[$attrs[$i]][$j] . " ";
				}
				echo "\n";
			}
			echo "\n";
			$entry = $ldap->next_entry($entry);
		}
		$ldap->free_result($res);
		echo'</pre>';
	}

}
