<?php
include_once ('config.php');
$maxErrors = 30;
// search filter stuff
$filter = $savedQ;
echo "savedQ=$savedQ";
// convert form infos to ldap operations
$attrtoadd = array();
$attrtomod = array();
$attrtodel = array();
for($i=0; $i<10; $i++ )
{
$num = $i+1;
$attribute = strtolower(trim($attr[$i]));
$value = utf8_encode( $val[$i] );
if( $act[$i] != "" ) {
if( $attribute == "" ) {
$errorMsg .= "Please specify attribute name for batch operation $num\n";
continue;
}
switch( $act[$i] ) {
case "del" :
if( ! isset($attrtodel[$attribute]) )
$attrtodel[$attribute] = array();
$attrtodel[$attribute][] = $value;
break;
case "mod":
if( ! isset($attrtomod[$attribute]) )
$attrtomod[$attribute] = array();
$attrtomod[$attribute][] = $value;
break;
case "add":
if( ! isset($attrtoadd[$attribute]) )
$attrtoadd[$attribute] = array();
$attrtoadd[$attribute][] = $value;
break;
case "rep":
if( ! isset($attrtorep[$attribute]) )
$attrtorep[$attribute] = array();
$attrtorep[$attribute][] = $value;
break;
default:
$errorMsg .= "unknown batch operation #$num $act[$i]\n";
}
}
}
include_once ("./page.header.php");
echo '<div align=center>';
html::form( $PHP_SELF, "get", 'name="frm"' );
echo '<table cellspacing=0 style="padding: 4px; border: 1px solid gray; width:96%">';
echo '<caption>', $config->appName , "> Batch execution</caption>\n";
$bAdd = (count($attrtoadd) > 0 );
$bDel = (count($attrtodel) > 0 );
$bMod = (count($attrtomod) > 0 );
$searchattrs = array( "dn", "cn" );
echo "ldap_search( $ds, $searchdn, $filter, $searchattrs );";
$sr = @ldap_search( $ds, $searchdn, $filter, $searchattrs );
if( $sr ) {
// a) collect entries DN in the array $aDn
$es = ldap_first_entry( $ds, $sr );
$aDn = array();
while( $es ) {
$aDn[] = ldap_get_dn( $ds, $es );
$es = ldap_next_entry( $ds, $es );
}
$DEBUG = 1;
debug( $aDn );
ldap_free_result( $sr);
$entriesCount = count( $aDn );
// b) apply modifications to each entry
foreach( $aDn as $dn ) {
// attributes to replace
if( $bMod ) {
$rs = @ldap_mod_replace( $ds, $dn, $attrtomod );
if( ! $rs )
$aError[] = "Error while replacing values : " . ldap_error($ds);
}
// attributes to add
if( $bAdd ) {
$rs = @ldap_mod_add( $ds, $dn, $attrtoadd );
if( ! $rs )
$aError[] = "Error while adding values : " . ldap_error($ds);
}
// attributes to delete
if( $bDel ) {
debug( $attrtodel );
$rs = ldap_mod_del( $ds, $dn, $attrtodel );
echo "ldap_mod_del( $ds, $dn, $attrtodel ) : $rs<br>";
if( ! $rs )
$aError[] = "Error while deleting attributes " . ldap_error($ds);
}
if( count($aError) > $config->maxBatchErrors ) {
$aError[] = "Maximum of errors reached, batch aborted.";
break;
}
} // foreach $aDn
if( count($aError)) {
$msg = "Some errors were encountered : " . implode( "<br>", $aError );
RowN( "<div class=error>$msg</div>" );
} else {
RowN( "Batch was successfully executed on $entriesCount entries" );
}
} // if $sr
echo "</table></form></div>\n";
include_once ("./page.footer.php");
?>