Code

Removed reset ogroup and set Keep ogroup index to ""
[gosa.git] / include / functions_FAI.inc
index e7e14d6758317ef11481042e81812e2b1cdc47e7..26c0a572659f10287d156965b74bdb68579e2095 100644 (file)
@@ -13,12 +13,16 @@ function get_all_objects_for_given_base($Current_DN,$filter,$detailed = false)
   global $config;
   $ldap = $config->get_ldap_link();
   $ldap->cd($config->current['BASE']);
+  $res = array();
+  $tmp = array();
+
+  if(!is_release_department($Current_DN)) {
+    return($res);
+  }
 
   /* 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));
-  $res = array();
-  $tmp = array();
 
   /* We must also include the given release dn */
   $previous_releases[] = $base_release;
@@ -33,14 +37,13 @@ function get_all_objects_for_given_base($Current_DN,$filter,$detailed = false)
     foreach($deps_to_search as $fai_base){
 
       /* Ldap search for fai classes specified in this release */
-      $ldap->cd($fai_base);
-      $ldap->search($filter,array("dn","objectClass","FAIstate"));
-
+      $res_tmp = get_list($filter,"fai",$fai_base,array("dn","objectClass","FAIstate"),GL_SUBSEARCH | GL_SIZELIMIT);
+  
       /* check the returned objects, and add/replace them in our return variable */
-      while($attr = $ldap->fetch()){
+      foreach($res_tmp as $attr){
         
         $buffer = array();
-        $name = str_ireplace($release,"",$attr['dn']);
+        $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
 
         if(isset($attr['FAIstate'][0])){
           if(preg_match("/removed$/",$attr['FAIstate'][0])){
@@ -78,24 +81,6 @@ function get_all_objects_for_given_base($Current_DN,$filter,$detailed = false)
 }
 
 
-
-/* Return the object defining ObjectClass e.g. FAIscriptEntry */
-function get_FAI_type($attr)
-{
-  $arr = array( "FAIprofile","FAIpartitionTable", "FAIpartitionDisk","FAIpartitionEntry","FAIhook","FAIhookEntry",
-      "FAIscriptEntry","FAIscript","FAIvariable","FAIvariableEntry","FAIpackageList","FAItemplate",
-      "FAItemplateEntry","FAIdebconfInfo","FAIrepository","FAIrepositoryServer","FAIbranch","FAIreleaseTag");  
-  foreach($arr as $name){    
-    if(in_array($name,$attr['objectClass'])){
-      preg_match("");
-      return($name);
-    }
-  }
-  if(DEBUG_FAI_FUNC) { echo "Not found"; } 
-  return("");
-} 
-
-
 /* Return all relevant FAI departments */
 function get_FAI_departments($suffix = "")
 {
@@ -116,18 +101,27 @@ function get_FAI_departments($suffix = "")
 
 
 /* Return all releases within the given base */
-function get_all_releases_from_base($dn)
+function get_all_releases_from_base($dn,$appendedName=false)
 {
   global $config;
-  $base = "ou=fai,ou=configs,ou=systems,".$dn;
+    
+  if(!preg_match("/ou=fai,ou=configs,ou=systems,/",$dn)){
+    $base = "ou=fai,ou=configs,ou=systems,".$dn;
+  }else{
+    $base = $dn;
+  }
   $res = array();  
   
   $ldap = $config->get_ldap_link();
   $ldap->cd($base);
   $ldap->search("(objectClass=FAIbranch)",array("ou","dn"));
   while($attrs = $ldap->fetch()){
-    $res[$attrs['dn']] = $attrs['ou'][0];
-  } 
+    if($appendedName){
+      $res[$attrs['dn']] = convert_department_dn(preg_replace("/,ou=fai,ou=configs,ou=system.*$/","",$attrs['dn']));
+    }else{
+      $res[$attrs['dn']] = $attrs['ou'][0];
+    }
+  }
   return($res);
 }
 
@@ -148,14 +142,16 @@ function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
 
     /* Get some basic informations */
     $parent_obj   = get_parent_release_object($Current_DN);
-    $ldap->cat($parent_obj,array("*"));
-    $attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
+    if(!empty($parent_obj)){
+      $ldap->cat($parent_obj,array("*"));
+      $attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
 
-    if(!array_diff_FAI( $attrs,$objectAttrs)){
-      $addObj['diff'] = FALSE;
-    }
+      if(!array_diff_FAI( $attrs,$objectAttrs)){
+        $addObj['diff'] = FALSE;
+      }
+    } 
   }
-
+   
   $_SESSION['FAI_objects_to_save'][$Current_DN] =  $addObj;
 }
 
@@ -163,6 +159,18 @@ function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
 /* Detect differences in attribute arrays  */
 function array_diff_FAI($ar1,$ar2)
 {
+
+  if((!isset($ar1['description'])) || (isset($ar1['description']) && (count($ar1['description']) == 0))){
+    $ar1['description'] = "";
+  }
+  if((!isset($ar2['description'])) || (isset($ar2['description']) && (count($ar2['description']) == 0))){
+    $ar2['description'] = "";
+  }
+
+  if(count($ar1) != count($ar2)) {
+    return (true);
+  }
+
   foreach($ar1 as $key1 => $val1){
 
     if((is_array($val1)) && (count($val1)==1)){
@@ -174,7 +182,9 @@ function array_diff_FAI($ar1,$ar2)
       $ar2[$key1] = $ar2[$key1][0];
     }
   }
-  if(count( array_diff($ar1,$ar2))){
+  ksort($ar1);
+  ksort($ar2);
+  if(count( array_diff($ar1,$ar2)) || arr_diff($ar1,$ar2)){
     return(true);
   }else{
     return(false);
@@ -182,19 +192,33 @@ function array_diff_FAI($ar1,$ar2)
 }
 
 
+function arr_diff($ar1,$ar2)
+{
+  foreach($ar1 as $ak1 => $av1){
+    if(!isset($ar2[$ak1]) || (!($av1 === $ar2[$ak1]))){
+      return(true);
+    }elseif(is_array($av1)){
+      return(arr_diff($av1,$ar2[$ak1]));
+    }
+  }
+  return(FALSE);
+}
+
+
+
+
 /* check which objects must be saved, and save them */
 function save_release_changes_now()
 {
   /* Variable init*/
   $to_save = array();
-  
-  /* check which attributes must realy be saved */
+
+  /* check which objects must be saved */
   foreach($_SESSION['FAI_objects_to_save'] as $Current_DN => $object){
     if($object['diff']){
       $sub_name = $Current_DN;
       while(isset($_SESSION['FAI_objects_to_save'][$sub_name])){
-       
-        $to_save[strlen($sub_name)] = $_SESSION['FAI_objects_to_save'][$sub_name]; 
+        $to_save[strlen($sub_name)][$sub_name] = $_SESSION['FAI_objects_to_save'][$sub_name]; 
         unset($_SESSION['FAI_objects_to_save'][$sub_name]);
         $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
       }
@@ -204,6 +228,13 @@ function save_release_changes_now()
   /* Sort list of objects that must be saved, and ensure that 
       container   objects are safed, before their childs are saved */
   ksort($to_save);
+  $tmp = array();
+  foreach($to_save as $SubObjects){
+    foreach($SubObjects as $object){
+      $tmp[] = $object;
+    }
+  }
+  $to_save = $tmp;
 
   /* Save objects and manage the correct release behavior*/
   foreach($to_save as $save){
@@ -230,7 +261,7 @@ function save_release_changes_now()
     }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 ){
@@ -259,11 +290,10 @@ function save_release_changes_now()
       }
     }
    
-   
     /* Check if this a leaf release or not */ 
     if(count($following_releases) == 0 ){
 
-      /* This is a leaf object. It isn't unherited by any other object */    
+      /* This is a leaf object. It isn't inherited by any other object */    
       if(DEBUG_FAI_FUNC) { 
         echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
         print_a($objectAttrs);
@@ -386,7 +416,8 @@ function clean_up_releases($Current_DN)
       while($attr = $ldap->fetch()){
         
         $buffer = array();
-        $name = str_ireplace($release,"",$attr['dn']);
+#        $name = str_ireplace($release,"",$attr['dn']);
+        $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
 
         if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
 
@@ -407,6 +438,7 @@ function clean_up_releases($Current_DN)
 }
 
 
+/* Remove numeric index and 'count' from ldap->fetch result */
 function prepare_ldap_fetch_to_be_saved($attrs)
 {
   foreach($attrs as $key => $value){
@@ -421,25 +453,60 @@ function prepare_ldap_fetch_to_be_saved($attrs)
 }
 
 
+/* Save given attrs to specified dn*/
 function save_FAI_object($dn,$attrs)
 {
-  print_a(array($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()){
+
+    /* Remove FAIstate*/
+    if(!isset($attrs['FAIstate'])){
+      $attrs['FAIstate'] = array();
+    }
+
     $ldap->modify($attrs);
   }else{
+  
+    /* Unset description if empty  */
+    if(empty($attrs['description'])){
+      unset($attrs['description']);
+    }    
+
     $ldap->add($attrs);
   }
   show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
 }
 
 
+/* Return FAIstate freeze branch or "" for specified release department */
+function get_release_tag($dn)
+{
+  global $config;
+  $ldap = $config->get_ldap_link();
+  $ldap->cd($dn);
+  $ldap->cat($dn,array("FAIstate"));
+
+  if($ldap->count()){
+  
+    $attr = $ldap->fetch();
+    if(isset($attr['FAIstate'][0])){
+      if(preg_match("/freeze/",$attr['FAIstate'][0])){
+        return("freeze");
+      }elseif(preg_match("/branch/",$attr['FAIstate'][0])){
+        return("branch");
+      }
+    }
+  }
+  return("");
+}
+
+
 function get_following_releases_that_inherit_this_object($dn)
 {
   global $config;
@@ -455,7 +522,8 @@ function get_following_releases_that_inherit_this_object($dn)
   $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);
+#  $dn_suffix = str_ireplace($base_release,"",$dn);
+  $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
 
   /* Check if given object also exists whitin one of these releases */
   foreach($sub_releases as $p_release => $name){
@@ -495,7 +563,8 @@ function get_parent_release_object($dn,$include_myself=true)
   }
 
   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
-  $dn_suffix = str_ireplace($base_release,"",$dn);
+#  $dn_suffix = str_ireplace($base_release,"",$dn);
+  $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
     
   /* Check if given object also exists whitin one of these releases */
   foreach($previous_releases as $p_release){
@@ -519,7 +588,8 @@ function get_previous_releases_of_this_release($dn,$flat)
   $ret = array();
 
   /* Explode dns into pieces, to be able to build parent dns */
-  $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$dn),0);
+  $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$dn));
+
   if(!is_array($dns_to_check)){
     return;  
   }
@@ -623,7 +693,8 @@ function get_release_dn($Current_DN)
   $ldap->cd($config->current['BASE']);
 
   /* Split dn into pices */ 
-  $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$Current_DN),0);
+  $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$Current_DN));
+
   if(!is_array($dns_to_check)){
     return;  
   }