note 12095 modified in function.ldap-modify by felipe

From: Date: Tue, 22 Jul 2008 19:03:24 +0000
Subject: note 12095 modified in function.ldap-modify by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142544@lists.php.net to get a copy of this message
The behaviour of OpenLDAP from 1.x to 2.x changed; in 1.x, when you passed ldap_modify the array, if the value was empty that attribute would be deleted. In 2.x, you get an "Invalid Syntax" error and the modify fails. This requires the ldap_mod_del function; unfortunately, that operation requires the attribute to be deleted have it's *old* value specified -- as you can imagine, if you're taking input from a CGI form, the attribute to be deleted's value is now missing (i.e., the user blanked out that textbox in the form and clicked Submit). So, you're in a bit of a conundrum -- you want to delete "empty" form values, but you need their old value to delete them. There are many ways to handle this, but I chose this approach: <?php // The first is what to add, the second to remove $entry=array(); $delval=array(); // Imagine if you will, $o and $title are // form fields with text boxes for data if($o!="") { $entry["o"]="$o"; } else { $delval[]="o"; } if($title!="") { $entry["title"]="$title"; } else { $delval[]="title"; } // First try the normal modify with $entry // $ldap, $dn, $BASEDN are all set up earlier if (@ldap_modify($ldap, $dn, $entry)) { // then do the dirty work $filter = sprintf("(&(uid=%s)(sn=%s))",$uid,$sn); $sres = ldap_search($ldap, $BASEDN, $filter, $delval); $delent = ldap_first_entry($ldap, $sres); $delarr = ldap_get_attributes($ldap, $delent); $findel=array(); for($i=0; $i<$delarr["count"]; $i++) { $attr = $delarr[$i]; $totl = $delarr[$attr]["count"]; for($z=0; $z<$totl; $z++) { if ($totl = 1) { $findel[$attr]=$delarr[$attr][$z]; } else { $findel[$attr][$z]=$delarr[$attr][$z]; } } } if(@ldap_mod_del($ldap, $dn, $findel)) { print("<H3>Modified Entry!</H3>\n"); print("<BR>\n"); } else { $error=ldap_error($ldap); print("<H3>Attribute Delete Failed!</H3>\n"); print("<BR>\n($error)\n"); } } else { $error=ldap_error($ldap); print("<H3>Modify Failed!</H3>\n"); print("<BR>\n($error)\n"); } ?> --was-- The behaviour of OpenLDAP from 1.x to 2.x changed; in 1.x, when you passed ldap_modify the array, if the value was empty that attribute would be deleted. In 2.x, you get an "Invalid Syntax" error and the modify fails. This requires the ldap_mod_del function; unfortunately, that operation requires the attribute to be deleted have it's *old* value specified -- as you can imagine, if you're taking input from a CGI form, the attribute to be deleted's value is now missing (i.e., the user blanked out that textbox in the form and clicked Submit). So, you're in a bit of a conundrum -- you want to delete "empty" form values, but you need their old value to delete them. There are many ways to handle this, but I chose this approach: // The first is what to add, the second to remove $entry=array(); $delval=array(); // Imagine if you will, $o and $title are // form fields with text boxes for data if($o!="") { $entry["o"]="$o"; } else { $delval[]="o"; } if($title!="") { $entry["title"]="$title"; } else { $delval[]="title"; } // First try the normal modify with $entry // $ldap, $dn, $BASEDN are all set up earlier if (@ldap_modify($ldap, $dn, $entry)) { // then do the dirty work $filter = sprintf("(&(uid=%s)(sn=%s))",$uid,$sn); $sres = ldap_search($ldap, $BASEDN, $filter, $delval); $delent = ldap_first_entry($ldap, $sres); $delarr = ldap_get_attributes($ldap, $delent); $findel=array(); for($i=0; $i<$delarr["count"]; $i++) { $attr = $delarr[$i]; $totl = $delarr[$attr]["count"]; for($z=0; $z<$totl; $z++) { if ($totl = 1) { $findel[$attr]=$delarr[$attr][$z]; } else { $findel[$attr][$z]=$delarr[$attr][$z]; } } } if(@ldap_mod_del($ldap, $dn, $findel)) { print("<H3>Modified Entry!</H3>\n"); print("<BR>\n"); } else { $error=ldap_error($ldap); print("<H3>Attribute Delete Failed!</H3>\n"); print("<BR>\n($error)\n"); } } else { $error=ldap_error($ldap); print("<H3>Modify Failed!</H3>\n"); print("<BR>\n($error)\n"); } http://php.net/manual/en/function.ldap-modify.php

« previous php.notes (#142544) next »