Location: PHPKode > projects > LDAP Navigator > ldapnavigator-0.9.6/modules/entry/delete.php
<?php

LDAP_Open();

$encdn = rawurlencode($dn);

$ITEMSPERLINE = 10;

/*
 
 hack to simulate a Try.. catch block "alaJava"
 TODO : an Exception class that encapsulate error, displays the error page and exits 
*/

while( 1 ) {

	if( ! isset($dn) || trim($dn) == ""  )
		ErrorPage(  "Error", "Missing parameter : dn", "page.default.php" );

	// recursive delete request
	if( $recur == 1 ) {

		// a. we get the children entries
		$sr = ldap_search( $ds, $dn, "objectclass=*", array("dn"), 1 );
		$list = ldap_get_entries( $ds, $sr );
		$numentries = ldap_count_entries($ds, $sr);
		$dnlist = array();
		$pageTitle = "$config->appName > Delete Subtree";
		
		include( "./page.header.php" );
		FormStart( $pageTitle );
		FormLine( '',  "Deleting $numentries entries..." );
		
		echo "<tr><td></td><td>";
		
		
		foreach( $list as $tmp ) {
			if( $tmp['dn'] )
				$dnlist[] = $tmp['dn'];
		}
		// sort by reverse dn length, ie entry depth
		usort( $dnlist, "sortByLenRev" );

		$cnt = 0;
		foreach( $dnlist as $tmpdn ) {
			$ret = ldap_delete( $ds, $tmpdn );
			if( ! $ret ) {
				$errorMsg = "Error $errno - Delete failed - " . ldap_error($ds);
				echo '<img src="/img/warning.gif" border=0> &nbsp; ',  "<span class=error>$errorMsg</span><br>\n";
			} else {
				$cnt++;
			}
			// line break to avoid horizontal scrollbar
			if( $cnt != 0 && $cnt % $ITEMSPERLINE == 0 ) {
				echo "$cnt entries deleted<br>\n";
			}
		}
		echo "</td></tr>";
		echo "</table>";
		
		include( "./page.footer.php" );
		exit();		
		
	} else {
		
		$ret = ldap_delete( $ds, $dn );
		$pageTitle = "$config->appName > Delete entry";
	}
	
	// error
	if( ! $ret ) {
		$errno = ldap_errno($ds);
		// deleted a non leaf entry
		if( $errno == 66 ) {
			
			ConfirmationPage( 
				"Recursive Delete confirmation",
				"This entry <b>$dn</b> is a tree and contains child entries<br>Do you really want to delete this entry and its children?<br>
					Please Confirm or Cancel",
				"$PHP_SELF?A=entry.delete&dn=$encdn&recur=1",
				"$PHP_SELF?A=entry.properties&dn=$encdn" 
			);
			
		} else {
			$errorMsg = "Error $errno - Delete failed - " . ldap_error($ds);
			break;
		}
	}		
		
	$statusMsg = "Successfully deleted entry $dn";
	
	break;
	
} // end of try() block

	
include( "./page.header.php" );
FormStart( $pageTitle );

// output result status message
if( $statusMsg )
	FormLine( '<img src="/img/info.gif" border=0>',  $statusMsg );
else
	FormLine( '<img src="/img/warning.gif" border=0>',  "<span class=error>$errorMsg</span>" );


echo "</table>";
include( "./page.footer.php" );


function sortByLenRev( $a, $b )
{
	if( strlen($a) < strlen($b) )
		return 1;
	else
		return -1;
}

?>
Return current item: LDAP Navigator