From dd869bc74adf89f3d6ffbe6633c8eeccc899bb0c Mon Sep 17 00:00:00 2001 From: hickert Date: Fri, 7 Jul 2006 05:01:42 +0000 Subject: [PATCH] Added a new function that returns all objects, that could be removed from ldap. git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@4038 594d385d-05f5-0310-b6e9-bd551577e9d8 --- include/functions_FAI.inc | 136 ++++++++++++++++++++++++++------------ 1 file changed, 95 insertions(+), 41 deletions(-) diff --git a/include/functions_FAI.inc b/include/functions_FAI.inc index 807e60558..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","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(); @@ -89,7 +91,7 @@ function get_FAI_type($attr) return($name); } } - echo "Not found"; + if(DEBUG_FAI_FUNC) { echo "Not found"; } return(""); } @@ -113,6 +115,7 @@ function get_FAI_departments($suffix = "") } +/* Return all releases within the given base */ function get_all_releases_from_base($dn) { global $config; @@ -129,11 +132,7 @@ function get_all_releases_from_base($dn) } -/* This function does everything to be able to save the given dn. - - !!!!!!! No functionality just output - - */ +/* This function does everything to be able to save the given dn. */ function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false) { /* Get ldap object */ @@ -160,16 +159,18 @@ function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false) 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); } - if(!$ldap->count()){ + /* 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])){ @@ -186,8 +187,10 @@ function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false) if(count($following_releases) == 0 ){ /* This is a leaf object. It isn't unherited by any other object */ - echo "Saving directly, is a leaf object
".$Current_DN; - print_a($objectAttrs); + if(DEBUG_FAI_FUNC) { + echo "Saving directly, is a leaf object
".$Current_DN; + print_a($objectAttrs); + } save_FAI_object($Current_DN,$objectAttrs); }else{ @@ -206,23 +209,27 @@ function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false) /* check if there is already an entry named like this, in one of our parent releases */ if(!empty($parent_obj)){ - echo "There is already an entry named like this.
"; + if(DEBUG_FAI_FUNC) { + echo "There is already an entry named like this.
"; - echo "Saving main object".$Current_DN; - print_a($objectAttrs); - + echo "Saving main object".$Current_DN; + print_a($objectAttrs); + } save_FAI_object($Current_DN,$objectAttrs); foreach($r as $key){ - echo "Saving parent to following release ".$key; - print_a($parent_attrs); + if(DEBUG_FAI_FUNC) { + echo "Saving parent to following release ".$key; + print_a($parent_attrs); + } save_FAI_object($key,$parent_attrs); } }else{ - echo "Saving main object".$Current_DN; - print_a($objectAttrs); - + if(DEBUG_FAI_FUNC) { + echo "Saving main object".$Current_DN; + print_a($objectAttrs); + } save_FAI_object($Current_DN,$objectAttrs); if(isset($objectAttrs['FAIstate'])){ @@ -232,8 +239,10 @@ function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false) } foreach($r as $key ){ - echo "Create an empty placeholder in follwing release ".$key; - print_a($objectAttrs); + if(DEBUG_FAI_FUNC) { + echo "Create an empty placeholder in follwing release ".$key; + print_a($objectAttrs); + } save_FAI_object($key,$objectAttrs); } } @@ -242,35 +251,80 @@ function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false) /* check if we must patch the follwing release */ if(!empty($r)){ foreach($r as $key ){ - echo "Copy current objects original attributes to next release ".$key; - print_a($parent_attrs); + if(DEBUG_FAI_FUNC) { + echo "Copy current objects original attributes to next release ".$key; + print_a($parent_attrs); + } save_FAI_object($key,$parent_attrs); } } - echo "Saving current object".$parent_obj; - print_a($objectAttrs); - + if(DEBUG_FAI_FUNC) { + echo "Saving current object".$parent_obj; + print_a($objectAttrs); + } save_FAI_object($parent_obj,$objectAttrs); - if($parent_obj != $Current_DN){ - echo "Error they must be equal : ".$parent_obj."
and
".$Current_DN."
"; + if(($parent_obj != $Current_DN)){ + print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN)); } } } - /* Remove the source object from ldap. It is no longer required */ - if($removed){ - if(get_parent_release_object($Current_DN,false) == ""){ - $ldap->clearResult(); - $ldap->cat($Current_DN); +} + + +/* 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{ - $tmp = $ldap->fetch(); - return($tmp['dn']); + /* This object is required (not removed), so do not + delete any following sub releases of this object */ + $Skip[$name] = $attr['dn']; + } + } } } - echo "
.--------------------------------------------------------------------.
"; - - return(""); + return($Kill); } -- 2.30.2