Location: PHPKode > projects > LDAP Navigator > ldapnavigator-0.9.6/modules/entry/modify.php
<?php
/*
	entry.modify.php
	Mise a jour d'une entry LDAP
	La mise a jour s'efectue au niveau attribut, c'est a dire que:
	a) l'entry actuel est charge,
	b) chacun de ces attributs est confronte a la valeur dans le formulaire
	c) la presence de nouveau attributs est teste
	d) l'entry est mise a jour seulement si modification il y a
	   parallelement, on construit un message de statut qui decrit les modifications apportees
	e) le script renvoie 0 a l'appelant (ici indeX.php) pour lui signaler que la modif est OK
	f) index.php réaffiche le formulaire avec un message de statut

	@params
	$HTTP_POST_VARS les variable du formulaire d'edition, dont:
		dn	le DN original de l'entry
		newdn le dn du champ de saisie ( non utilise pour cette action Modify)
		ldapnav_<attribute_name> tableau des attributs de l'entry
		newattrname tableau des nouveau attributs (noms)
		newattrval tableau des nouveau attributs (valeurs)
		newattrvalbin tableau des nouveau attributs binaires ( transmis via un "file upload" )
*/


$pageTitle = "$config->appName > Updating entry $dn";

$sr = @ldap_read ($ds, $dn, "objectclass=*" );

if( ! $sr ) {
	ErrorPage(  "Error",
		"Entry not found : "	. ldap_error($ds) . "<br>Maybe someone has just deleted this entry ?",
		"page.default.php"
	);
}

$es = ldap_first_entry ($ds, $sr);
$entry = ldap_get_attributes ($ds, $es);


// form fields that are to be treated as entry attributes
$fields = $HTTP_POST_VARS;
$addentry = $modentry = $delentry = array();


foreach( $fields as $k => $formvalues ) 
{
	if( strpos( $k, 'ldapnav_' ) === 0 ) {
	
		$attrname = substr( $k, strlen( 'ldapnav_' ) );
		$dbg = "\n\nattribute $attrname:";

	// not an attribute field
	} else {
		continue;
	}

	if( ! is_array($formvalues) ) {
		continue;
	}
	
	// remove empty values in form attributes - good values are stored in $newvalues
	$newvalues = array();
	for( $i=0; $i<count($formvalues); $i++ ) {
		if( trim($formvalues[$i]) != "" )
			$newvalues[] = utf8_encode( $formvalues[$i] );
	}
	
	
	if( isset($entry[$attrname]) && $entry[$attrname] ){
	
		// the 'count' field would interfer in array_diff(); so we unset it
		unset( $entry[$attrname]['count'] );
		
		// test if some values have been unset => this is a case of modification 
		if( count($newvalues) < count($entry[$attrname] ) ) {

			$modentry[$attrname] = $newvalues;

		} else {

			$modifiedvalues = array_diff( $newvalues, $entry[$attrname] );
			if( count($modifiedvalues) > 0 ) {
				$modentry[$attrname] = $modifiedvalues;
			}		
		}
	}
	//	echo "$dbg\n";
}

// process new attributes 

foreach( $HTTP_POST_VARS['newattrname'] as $idx => $attrname ) {
	
	$attrname = strtolower(trim($attrname));
	$value = @$HTTP_POST_VARS['newattrval'][$idx];
//	echo "parsing new attribute $idx: $attrname = $value\n";

	if( trim($attrname) == "" )
		continue;

	if( $value == "" )
		continue;
	
	if( ! is_array($addentry[$attrname]) )
		$addentry[$attrname] = array();
	$addentry[$attrname][] = utf8_encode( $value );
	
}


// page output starts from here

if( count($addentry) == 0 
	&& count($modentry) ==0 
	&& count($delentry) ==0 ) {

	InfoPage( 
		$pageTitle,
		"No changes detected" 
	);
	
}


if( count($modentry) > 0 ) {

//	$DEBUG = 1; debug( $modentry );
	
	// building the status message
	foreach( $modentry as $k => $values ) {
		@$statusMsg .= "modified $k" . implode( "<br>", $values ) . "<br>";
	}
	
	// applying the change at attribute level
	$ret = @ldap_mod_replace( $ds, $dn, $modentry );

/*	echo "<pre>"; print_r( $modentry ); echo "\nDECODED:\n</pre>";
	foreach( $modentry as $value ) {
		echo utf8_decode($value), "\n";
	}*/
	
	if( ! $ret ) 
		ErrorPage( 
			$pageTitle,
			"modifying attributes failed : "  . ldap_error($ds),
			"history.go(-1)"
		);		
}


if( count($delentry) > 0 ) {

	// building the status message
	foreach( $delentry as $k => $values ) {
		$statusMsg .= "delete $k" . implode( "<br>", $values ) . "<br>";
	}
	$ret = @ldap_mod_del( $ds, $dn, $delentry );
	if( ! $ret ) 
		ErrorPage( 
			$pageTitle,
			"deleting attributes failed : "  . ldap_error($ds),
			"history.go(-1)"
		);		
}

if( count($addentry) > 0 ) {

	// building the status message
	foreach( $addentry as $k => $values ) {
		$statusMsg .= "added $k" . implode( "<br>", $values ) . "<br>";
	}	
	$ret = @ldap_mod_add( $ds, $dn, $addentry );
	
	if( ! $ret ) {
		ErrorPage( 
			$pageTitle,
			"adding attributes failed : "  . ldap_error($ds),
			"history.go(-1)"
		);
	}
}


?>
Return current item: LDAP Navigator