X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=include%2Ffunctions_FAI.inc;h=2cb851deaee4246c63bb3567c42dae032481599f;hb=1fac73f3983fd6dc8f75341dbe44637c1d7820fe;hp=24ff61ec81169f7f7462fa8f003b2a5ac310da4b;hpb=d76a14dab2fbc5c5e56322afa8a202b36ded7498;p=gosa.git diff --git a/include/functions_FAI.inc b/include/functions_FAI.inc index 24ff61ec8..2cb851dea 100644 --- a/include/functions_FAI.inc +++ b/include/functions_FAI.inc @@ -1,10 +1,12 @@ cd($fai_base); - $ldap->search($filter,array("dn","objectClass")); + $ldap->search($filter,array("dn","objectClass","FAIstate")); - /* check the returned objects, and add/replace them in our return vareable */ + /* check the returned objects, and add/replace them in our return variable */ while($attr = $ldap->fetch()){ - + $buffer = array(); $name = str_ireplace($release,"",$attr['dn']); + if(isset($attr['FAIstate'][0])){ + if(preg_match("/removed$/",$attr['FAIstate'][0])){ + if(isset($res[$name])){ + unset($res[$name]); + } + continue; + } + } + + /* In detailed mode are some additonal informations visible */ if($detailed){ @@ -79,7 +91,7 @@ function get_FAI_type($attr) return($name); } } - echo "Not found"; + if(DEBUG_FAI_FUNC) { echo "Not found"; } return(""); } @@ -103,6 +115,7 @@ function get_FAI_departments($suffix = "") } +/* Return all releases within the given base */ function get_all_releases_from_base($dn) { global $config; @@ -119,54 +132,231 @@ function get_all_releases_from_base($dn) } -/* This function does everything to be able to save the given dn. - - !!!!!!! No functionality just output - - */ -function prepare_to_save_FAI_object($Current_DN) +/* This function does everything to be able to save the given dn. */ +function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false) { - /* Get some basic informations */ - $base_release = get_release_dn($Current_DN); - $sub_releases = get_sub_releases_of_this_release($base_release,true); - $parent_obj = get_parent_release_object($Current_DN); - - $previous_releases = get_previous_releases_of_this_release($base_release,true); - $following_releases= get_sub_releases_of_this_release($base_release,true); - + /* Get ldap object */ global $config; $ldap = $config->get_ldap_link(); $ldap->cd($config->current['BASE']); + + /* Get some basic informations */ + $base_release = get_release_dn($Current_DN); + $sub_releases = get_sub_releases_of_this_release($base_release,true); + $parent_obj = get_parent_release_object($Current_DN); + $following_releases = get_sub_releases_of_this_release($base_release,true); + + /* Check if given dn exists or if is a new entry */ $ldap->cat($Current_DN); if(!$ldap->count()){ $is_new = true; }else{ $is_new = false; } + + /* if parameter removed is true, we have to add FAIstate to the current attrs + FAIstate should end with ...|removed after this operation */ + if($removed ){ + $ldap->cat($Current_DN); + + /* Get current object, because we must add the FAIstate ...|removed */ + if((!$ldap->count()) && !empty($parent_obj)){ + $ldap->cat($parent_obj); + } + + /* Check if we have found a suiteable object */ + if(!$ldap->count()){ + echo "Error can't remove this object ".$Current_DN; + return; + }else{ + + /* Set FAIstate to current objectAttrs */ + $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch()); + if(isset($objectAttrs['FAIstate'][0])){ + if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){ + $objectAttrs['FAIstate'][0] .= "|removed"; + } + }else{ + $objectAttrs['FAIstate'][0] = "|removed"; + } + } + } - echo "

".$Current_DN."

"; - /* If we are a leaf object */ + /* Check if this a leaf release or not */ if(count($following_releases) == 0 ){ - echo "Saving dircetly, is a leaf object
"; + + /* This is a leaf object. It isn't unherited by any other object */ + if(DEBUG_FAI_FUNC) { + echo "Saving directly, is a leaf object
".$Current_DN; + print_a($objectAttrs); + } + save_FAI_object($Current_DN,$objectAttrs); }else{ - + + /* This object is inherited by some sub releases */ + + /* Get all releases, that inherit this object */ $r = get_following_releases_that_inherit_this_object($Current_DN); + /* Get parent object */ + $ldap->cat($parent_obj); + $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch()); + + /* New objects require special handling */ if($is_new){ - echo "Creating an empty entry for ".$r[key($r)]."
"; - echo "By setting 'FAIstate' to '*|removed'
"; + + /* check if there is already an entry named like this, + in one of our parent releases */ + if(!empty($parent_obj)){ + if(DEBUG_FAI_FUNC) { + echo "There is already an entry named like this.
"; + + echo "Saving main object".$Current_DN; + print_a($objectAttrs); + } + save_FAI_object($Current_DN,$objectAttrs); + + foreach($r as $key){ + if(DEBUG_FAI_FUNC) { + echo "Saving parent to following release ".$key; + print_a($parent_attrs); + } + save_FAI_object($key,$parent_attrs); + } + }else{ + + if(DEBUG_FAI_FUNC) { + echo "Saving main object".$Current_DN; + print_a($objectAttrs); + } + save_FAI_object($Current_DN,$objectAttrs); + + if(isset($objectAttrs['FAIstate'])){ + $objectAttrs['FAIstate'] .= "|removed"; + }else{ + $objectAttrs['FAIstate'] = "|removed"; + } + + foreach($r as $key ){ + if(DEBUG_FAI_FUNC) { + echo "Create an empty placeholder in follwing release ".$key; + print_a($objectAttrs); + } + save_FAI_object($key,$objectAttrs); + } + } }else{ - echo "This object has sub releases:
"; - echo "This object must be updated ".$r[key($r)]." with ".$parent_obj."
"; - } - unset($r[key($r)]); - echo "
the result will be inherited by ."; - foreach($r as $release => $value){ - echo "
  : ".$release.""; + + /* check if we must patch the follwing release */ + if(!empty($r)){ + foreach($r as $key ){ + if(DEBUG_FAI_FUNC) { + echo "Copy current objects original attributes to next release ".$key; + print_a($parent_attrs); + } + save_FAI_object($key,$parent_attrs); + } + } + + if(DEBUG_FAI_FUNC) { + echo "Saving current object".$parent_obj; + print_a($objectAttrs); + } + save_FAI_object($parent_obj,$objectAttrs); + + if(($parent_obj != $Current_DN)){ + print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN)); + } } } - echo "
.--------------------------------------------------------------------.
"; +} + + +/* this function will remove all unused (deleted) objects, + that have no parent object */ +function clean_up_releases($Current_DN) +{ + global $config; + $ldap = $config->get_ldap_link(); + $ldap->cd($config->current['BASE']); + + /* Collect some basic informations and initialize some variables */ + $base_release = get_release_dn($Current_DN); + $previous_releases = array_reverse(get_previous_releases_of_this_release($base_release,true)); + $Kill = array(); + $Skip = array(); + + /* We must also include the given release dn */ + $previous_releases[] = $base_release; + + /* Walk through all releases */ + foreach($previous_releases as $release){ + + /* Get fai departments */ + $deps_to_search = get_FAI_departments($release); + + /* For every single department (ou=hoos,ou ..) */ + foreach($deps_to_search as $fai_base){ + + /* Ldap search for fai classes specified in this release */ + $ldap->cd($fai_base); + $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate")); + + /* check the returned objects, and add/replace them in our return variable */ + while($attr = $ldap->fetch()){ + + $buffer = array(); + $name = str_ireplace($release,"",$attr['dn']); + + if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){ + + /* Check if this object is required somehow */ + if(!isset($Skip[$name])){ + $Kill[$attr['dn']] = $attr['dn']; + } + }else{ + + /* This object is required (not removed), so do not + delete any following sub releases of this object */ + $Skip[$name] = $attr['dn']; + } + } + } + } + return($Kill); +} + + +function prepare_ldap_fetch_to_be_saved($attrs) +{ + foreach($attrs as $key => $value){ + if(is_numeric($key) || ($key == "count") || ($key == "dn")){ + unset($attrs[$key]); + } + if(is_array($value) && isset($value['count'])){ + unset($attrs[$key]['count']); + } + } + return($attrs); +} + + +function save_FAI_object($dn,$attrs) +{ + global $config; + $ldap = $config->get_ldap_link(); + $ldap->cd($config->current['BASE']); + $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn)); + $ldap->cd($dn); + + $ldap->cat($dn,array("dn")); + if($ldap->count()){ + $ldap->modify($attrs); + }else{ + $ldap->add($attrs); + } + show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn)); } @@ -182,11 +372,11 @@ function get_following_releases_that_inherit_this_object($dn) $base_release = get_release_dn($dn); /* Get previous release dns */ - $sub_releases = get_sub_releases_of_this_release($base_release,true); + $sub_releases = get_sub_releases_of_this_release($base_release); /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */ $dn_suffix = str_ireplace($base_release,"",$dn); - + /* Check if given object also exists whitin one of these releases */ foreach($sub_releases as $p_release => $name){ @@ -195,7 +385,7 @@ function get_following_releases_that_inherit_this_object($dn) $ldap->cat($check_dn,array("dn","objectClass")); if($ldap->count()){ - return($ret); + //return($ret); }else{ $ret[$check_dn]=$check_dn; } @@ -205,7 +395,7 @@ function get_following_releases_that_inherit_this_object($dn) /* Get previous version of the object dn */ -function get_parent_release_object($dn) +function get_parent_release_object($dn,$include_myself=true) { global $config; $ldap = $config->get_ldap_link(); @@ -214,7 +404,9 @@ function get_parent_release_object($dn) /* Get base release */ $base_release = get_release_dn($dn); - $previous_releases[] = $base_release; + if($include_myself){ + $previous_releases[] = $base_release; + } /* Get previous release dns */ $tmp = get_previous_releases_of_this_release($base_release,true);