Location: PHPKode > projects > LDAP Navigator > ldapnavigator-0.9.6/tree.php
<?php

$ret = include_once ('./config.php');
$ret or die( "Missing configuration file - please refer to install instructions" );

$navbar = false;
include_once( './page.header.php' );


// TEMP
$compact = 1;


// copy some config stuff in local vars for ease of use
$maxTreeItems = $ldapmanager['maxTreeItems'];

// page localvars initializations
/* tree items array */
$items = array();
$newitems = array();

/* temporary directory to keep all the temporary files , including tree
	structure info and mime type files */

/* action flag: 0 - initial; 1 - to expand; 2 - to collapse */
$flag = 0;

$oc2icons = array_keys($config->objectclassIcons);

if (isset ($actionID)) {

	debug( "actionID=$actionID" );
	if( $actionID == 'expand' )
		 $flag = 1;
	elseif( $actionID == "collapse" )
		 $flag = 2;

	$filename= $config->tmpdir . "/" . $fileID;
	$fp = fopen( $filename, "r" );
	$items = unserialize( fread( $fp, filesize($filename)));
	fclose($fp);
}


// depth in the tree
$level = 1;

// defined in default.php
LDAP_Open();

/* initialize OR expand the one branch of the tree */
if (($flag == 0) or ($flag == 1)) {

 /* if it initializes LDAP tree */
if ($flag == 0) {

	debug( "flags == 0" );
	/* initialize $row. rows begin at 1 */
	$row = 1;
	// CacheItem arguments: level, expanded, dn, icon
	$items[0] = CacheItem( $level, 0, $ldapmanager['basedn'], "" );

	/* initialize tree */
	$searchdn = $ldapmanager['basedn'];
	$sr = ldap_list( $ds, $searchdn, $ldapmanager['filter'] );
}

 // node expand request
elseif ($flag == 1) {

	debug( "flags == 1" );

	$searchdn = $items[$row]['dn'];
	$level = $items[$row]['level'];
	$items[$row]['ecs'] = 0; // expanded 

}


/* expand tree
	in case the items count limit is defined
*/

if( $maxTreeItems > 0 ) {

	$sr = @ldap_list($ds, $searchdn, $ldapmanager['filter'], array("objectclass"), 0, $maxTreeItems );
	// TODO : would be better to test warning returned by LDAP server?
	if( $maxTreeItems == ldap_count_entries ($ds, $sr) ) {
		// a flag to show a warning a page bottom
		$partialresult = $searchdn;
	}

} else {
	$sr = ldap_list ($ds, $searchdn, $ldapmanager['filter'], array("objectclass") );
}

if( $sr ) {
	$entries = ldap_get_entries($ds, $sr);
	$count = $entries['count'];
} else {
	 $count = 0;
}

for( $i=0; $i< $count; $i++ ) 
{
		$entry = $entries[$i];
		debug( "get entry $i ($entry[dn])" );
		/* smart icon :lookup for a icon representing this type of object */
		$icon = smartIcon( $entry['objectclass'] );
		// add the item to our array cache 
		$newitems[] = CacheItem( $level+1, 1, $entry['dn'], $icon );
 }

 // sort and insert the child items after the parent node
 if( count($newitems) > 0 ) {
		usort( $newitems, "EntryCompare" );
		array_splice( $items, $row+1, 0, $newitems );
 }
 $level++;
 
}  // end of if (($flag == 0) or ($flag == 1))

// collapse request
elseif ($flag == 2) {
	$items[$row]['ecs'] = 1;
	$level = $items[$row]['level'];
	$numremoveitems = 0;
	for( $i = $row + 1; $i <= count($items); $i++ ) {
		if( $items[$i]['level'] <= $level )
			break;
		$numremoveitems++;
	}
	// remove children items from cache
	array_splice( $items, $row+1, $numremoveitems );
} /* end of else if ($flag == 2) */

ldap_unbind ($ds);


debug( "after ldap block" );

// TODO enforce security here
// we'll need the cache filename in the next loop
$tmpfname = tempnam( $config->tmpdir, $config->tmpfile_prefix );
// get file ID from path generated by tempnam
$newFileID = array_pop( split( "[/\]", $tmpfname ));

?>
<style type="text/css">
A {
	text-decoration : none;
}
</style>
<?php

/* display LDAP root at first - host name */

echo "<table border=0 cellspacing=0 cellpadding=0>
<tr>
	<td><img src=img/openedfolder.gif border=0></td>
	<td nowrap><b>$ldapmanager[host]</b> / $ldapmanager[basedn]</td>
</tr></table>\n";

echo "<table cellspacing=0 cellpadding=0 width=100%>\n";

/* display tree $row from 1 */
for ($i = 1; $i < count($items); $i++) {
	
	echo '<tr><td nowrap valign="middle"/>';
	
	$item = $items[$i];
	$level = $item['level'];
	$label = $item['rdn'];
	$dn = $item['dn'];
	$icon = ($item['icon'] == ""  ? "openedfolder.gif" : $item['icon'] );

	$ecs = $item['ecs'];
	// used to evaluate which icon to display : plus/bplus minus/bminus 
	$nextlevel = @$items[$i+1]['level'];
	// kept for downsize tests
	if( $compact ) {
		$iconwidth = 16 * ($level -1);
		if( $iconwidth > 0 )
			echo "<img src=img/blank.gif height=16 width=$iconwidth>";
	} else {
		// display multiple blank images
		echo str_repeat( "<img src=img/blank.gif height=16 width=16>", $level -1 );	
	}
	
	echo "<A href=\"tree.php";
	// expand or not?
	if ( $ecs == 0)
		 echo "?actionID=collapse";
	else // $ecs == 1
		 echo "?actionID=expand";
		 
	echo "&fileID=" . $newFileID . "&row=" . $i . "\">";
	

	// open or closed ?	
	if ($ecs == 0) {
		 for ($j = $i + 1; $j <= $row; $j++) {
				if ($level >= $items[$j]['level'] ) {
				   echo "<img src=img/bminus.gif border=0>";
				   break;
				}
		 }

		 if( $j > $row )
				echo "<img src=img/bminus.gif border=0>";

	}
	elseif ($ecs == 1) {

		// next item is a brother current item
		if( $level == $nextlevel ) {
			echo "<img src=img/plus.gif border=0>";
		// next item is a child of current item
		} elseif( $nextlevel > $level ) {
			echo "<img src=img/bplus.gif border=0>";
		// next item is a parent of current item
		} else {
			echo "<img src=img/bplus.gif border=0>";
		}
		
	}

	// show item icon
	echo "<img src=\"img/$icon\" border=0></a>";

	// item label
	echo "<a href=\"submit.php?A=entry.properties&dn=" . rawurlencode ($dn) . "\" target=\"right\">",
			$label . "</a>",
			"</td></tr>\n";

} /* for ($i = 1; $i <= $row; $i++) */

echo "</tr></table>\n\n";


// save the cache file
$fp = fopen( $tmpfname, "w+");
fwrite( $fp, serialize($items));
fclose ($fp);



?>
<!-- this piece of code is to debug auto scrolling stuff
<input type="button" onClick="savePos()" value="savePos" />
-->

<script language="javascript">

	window.onload = restorePos;
	// sorry , but konqueror wont update cookie on the window.unload() event
	window.onunload = savePos;

<?

// display a JS popup if partial results was found
if( $partialresult ) {
	$msg = "Warning: displayed only the first $maxTreeItems children of node $partialresult\\nTo change this number, go to Login page";
	echo "alert( \"$msg\" );";
}

?>

function restorePos()
{
	var Ypos = getCookie("savedYOffset");
	window.defaultStatus = "Yposition=" + Ypos;	
	if( ! Ypos )
		return;
		
	if( document.all )
		window.scroll( 0, Ypos );
	else {
		window.scrollTo( 0, Ypos );
	}
}

function savePos()
{
	if (document.all) {
		var savedXOffset= document.body.scrollLeft;
		var savedYOffset= document.body.scrollTop;
		
	} else {
		var savedXOffset = window.pageXOffset;
		var savedYOffset = window.pageYOffset;
	}
	setCookie( "savedYOffset", savedYOffset, null );
//	alert( "[savePos] document.cookie=" + document.cookie );
}


function setCookie(name, value, expire) 
{
	document.cookie = name + "=" + escape(value)
		+ ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}

function getCookie( cookieName ) {
	
	pattern = cookieName + "=";
	if( document.cookie.length > 0 ) {
		offset = document.cookie.indexOf( pattern );
		if (offset != -1) { // if cookie exists 
			offset += pattern.length;
			// set index of beginning of value
			end = document.cookie.indexOf(";", offset );
			// set index of end of cookie value
			if (end == -1) 
				end = document.cookie.length;
//			alert( "got cookie value:" + document.cookie.substring(offset, end) );
			return unescape(document.cookie.substring(offset, end));
		} 
	}
	return null;
}
</script>
<?
include ("./page.footer.php");


// used to sort entries 
// Usage: usort ($info, EntryCompare)
function EntryCompare ($a, $b)
{
	return strcmp ($a['dn'], $b['dn']);
}


function CacheItem( $level, $expanded, $dn, $icon="" )
{
	global $ldapmanager;
	
	$rdn = ldap_explode_dn( $dn, 0 );

	if( $ldapmanager['convertCharset'] ) {
		$rdn[0] = utf8_decode($rdn[0]);
	}
	
	$item = array(
		  'level'=> $level,
		 'ecs' => $expanded,
		  'dn'=> $dn,
		  'rdn'=> $rdn[0],
		  'icon'=> $icon,
	);
	return $item;
}

function CacheItemIndex( $dn )
{
	global $items;
	for($i=0; $i< count($items); $i++ ) {
		  if( $items[$i]['dn'] == $dn )
				return $i;
	}
	// we should never happens to be here
	die( "Error : CacheItemIndex dn not found" );
	return false;
}

?> 
Return current item: LDAP Navigator