Code

Simple style updates
[gosa.git] / include / functions_FAI.inc
1 <?php
3 define("DEBUG_FAI_FUNC",FALSE);
5 /* TEST PHASE .... */
7 /* Returns all object for the given release.
8    This function resolves the releases  
9    from base up to the given dn.
10  */
11 function get_all_objects_for_given_base($Current_DN,$filter,$detailed = false)
12 {
13   global $config;
14   $ldap = $config->get_ldap_link();
15   $ldap->cd($config->current['BASE']);
16   $res = array();
17   $tmp = array();
19   if(!is_release_department($Current_DN)) {
20     return($res);
21   }
23   /* Collect some basic informations and initialize some variables */ 
24   $base_release       = get_release_dn($Current_DN);
25   $previous_releases  = array_reverse(get_previous_releases_of_this_release($base_release,true));
27   /* We must also include the given release dn */
28   $previous_releases[] = $base_release;
30   /* Walk through all releases */
31   foreach($previous_releases as $release){
33     /* Get fai departments */
34     $deps_to_search = get_FAI_departments($release); 
36     /* For every single department  (ou=hoos,ou ..) */
37     foreach($deps_to_search as $fai_base){
39       /* Ldap search for fai classes specified in this release */
40       $res_tmp = get_list($filter,"fai",$fai_base,array("dn","objectClass","FAIstate"),GL_SUBSEARCH | GL_SIZELIMIT);
41   
42       /* check the returned objects, and add/replace them in our return variable */
43       foreach($res_tmp as $attr){
44         
45         $buffer = array();
46         $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
48         if(isset($attr['FAIstate'][0])){
49           if(preg_match("/removed$/",$attr['FAIstate'][0])){
50             if(isset($res[$name])){
51               unset($res[$name]);
52             }
53             continue;
54           }
55         }
58         /* In detailed mode are some additonal informations visible */
59         if($detailed){
61           /* Create list of parents */
62           if(isset($res[$name])){
63             $buffer = $res[$name];
64             $buffer['parents'][] = $res[$name]['dn'];
65           }else{
66             $buffer['parents'] = array();
67           }
69           /* Append objectClass to resulsts */
70           $buffer['objectClass']  = $attr['objectClass'];
71           unset($buffer['objectClass'][0]);
72         }
74         /* Add this object to our list */
75         $buffer['dn']           = $attr['dn'];
76         $res[$name] = $buffer;
77       }
78     }
79   }
80   return($res);
81 }
84 /* Return all relevant FAI departments */
85 function get_FAI_departments($suffix = "")
86 {
87   $arr = array("hooks","scripts","disk","packages","profiles","templates","variables");
88   $tmp = array();
89   if(preg_match("/^,/",$suffix)){
90     $suffix = preg_replace("/^,/","",$suffix);
91   }
92   foreach($arr as $name){
93     if(empty($suffix)){
94       $tmp[$name] = "ou=".$name;
95     }else{
96       $tmp[$name] = "ou=".$name.",".$suffix;
97     }
98   }
99   return($tmp);
103 /* Return all releases within the given base */
104 function get_all_releases_from_base($dn,$appendedName=false)
106   global $config;
107     
108   if(!preg_match("/ou=fai,ou=configs,ou=systems,/",$dn)){
109     $base = "ou=fai,ou=configs,ou=systems,".$dn;
110   }else{
111     $base = $dn;
112   }
113   $res = array();  
114   
115   $ldap = $config->get_ldap_link();
116   $ldap->cd($base);
117   $ldap->search("(objectClass=FAIbranch)",array("ou","dn"));
118   while($attrs = $ldap->fetch()){
119     if($appendedName){
120       $res[$attrs['dn']] = convert_department_dn(preg_replace("/,ou=fai,ou=configs,ou=system.*$/","",$attrs['dn']));
121     }else{
122       $res[$attrs['dn']] = $attrs['ou'][0];
123     }
124   }
125   return($res);
129 /* Add this object to list of objects, that must be checked for release saving */
130 function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
132   /* Get ldap object */  
133   global $config;
134   $addObj['Current_DN'] = $Current_DN;
135   $addObj['objectAttrs']= $objectAttrs;
136   $addObj['removed']    = $removed;
137   $addObj['diff']       = TRUE;
139   if(!$removed){
140     $ldap = $config->get_ldap_link();
141     $ldap->cd($config->current['BASE']);
143     /* Get some basic informations */
144     $parent_obj   = get_parent_release_object($Current_DN);
145     if(!empty($parent_obj)){
146       $ldap->cat($parent_obj,array("*"));
147       $attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
149       if(!array_diff_FAI( $attrs,$objectAttrs)){
150         $addObj['diff'] = FALSE;
151       }
152     } 
153   }
154    
155   $_SESSION['FAI_objects_to_save'][$Current_DN] =  $addObj;
159 /* Detect differences in attribute arrays  */
160 function array_diff_FAI($ar1,$ar2)
163   if((!isset($ar1['description'])) || (isset($ar1['description']) && (count($ar1['description']) == 0))){
164     $ar1['description'] = "";
165   }
166   if((!isset($ar2['description'])) || (isset($ar2['description']) && (count($ar2['description']) == 0))){
167     $ar2['description'] = "";
168   }
170   if(count($ar1) != count($ar2)) {
171     return (true);
172   }
174   foreach($ar1 as $key1 => $val1){
176     if((is_array($val1)) && (count($val1)==1)){
177       $ar1[$key1] = $val1[0];
178     }
180     if((is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
181       $val1 = $val1[0];
182       $ar2[$key1] = $ar2[$key1][0];
183     }
184   }
185   ksort($ar1);
186   ksort($ar2);
187   if(count( array_diff($ar1,$ar2))){
188     return(true);
189   }else{
190     return(false);
191   }
195 /* check which objects must be saved, and save them */
196 function save_release_changes_now()
198   /* Variable init*/
199   $to_save = array();
201   /* check which objects must be saved */
202   foreach($_SESSION['FAI_objects_to_save'] as $Current_DN => $object){
203     if($object['diff']){
204       $sub_name = $Current_DN;
205       while(isset($_SESSION['FAI_objects_to_save'][$sub_name])){
206         $to_save[strlen($sub_name)][$sub_name] = $_SESSION['FAI_objects_to_save'][$sub_name]; 
207         unset($_SESSION['FAI_objects_to_save'][$sub_name]);
208         $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
209       }
210     }
211   }
213   /* Sort list of objects that must be saved, and ensure that 
214       container   objects are safed, before their childs are saved */
215   ksort($to_save);
216   $tmp = array();
217   foreach($to_save as $SubObjects){
218     foreach($SubObjects as $object){
219       $tmp[] = $object;
220     }
221   }
222   $to_save = $tmp;
224   /* Save objects and manage the correct release behavior*/
225   foreach($to_save as $save){
227     $Current_DN = $save['Current_DN'];
228     $removed    = $save['removed'];
229     $objectAttrs= $save['objectAttrs'];
231     /* Get ldap object */ 
232     global $config;
233     $ldap = $config->get_ldap_link();
234     $ldap->cd($config->current['BASE']);
236     /* Get some basic informations */
237     $base_release       = get_release_dn($Current_DN);
238     $sub_releases       = get_sub_releases_of_this_release($base_release,true);
239     $parent_obj         = get_parent_release_object($Current_DN);
240     $following_releases = get_sub_releases_of_this_release($base_release,true);
241     
242     /* Check if given dn exists or if is a new entry */
243     $ldap->cat($Current_DN);
244     if(!$ldap->count()){
245       $is_new = true;
246     }else{
247       $is_new = false;
248     }
249    
250     /* if parameter removed is true, we have to add FAIstate to the current attrs 
251           FAIstate should end with ...|removed after this operation */  
252     if($removed ){
253       $ldap->cat($Current_DN);
255       /* Get current object, because we must add the FAIstate ...|removed */
256       if((!$ldap->count()) && !empty($parent_obj)){
257         $ldap->cat($parent_obj);
258       }
260       /* Check if we have found a suiteable object */ 
261       if(!$ldap->count()){
262         echo "Error can't remove this object ".$Current_DN;
263         return;
264       }else{
266         /* Set FAIstate to current objectAttrs */
267         $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
268         if(isset($objectAttrs['FAIstate'][0])){
269           if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
270             $objectAttrs['FAIstate'][0] .= "|removed";
271           }
272         }else{
273           $objectAttrs['FAIstate'][0] = "|removed";
274         }
275       }
276     }
277    
278     /* Check if this a leaf release or not */ 
279     if(count($following_releases) == 0 ){
281       /* This is a leaf object. It isn't unherited by any other object */    
282       if(DEBUG_FAI_FUNC) { 
283         echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
284         print_a($objectAttrs);
285       }
286       save_FAI_object($Current_DN,$objectAttrs);
287     }else{
289       /* This object is inherited by some sub releases */  
291       /* Get all releases, that inherit this object */ 
292       $r = get_following_releases_that_inherit_this_object($Current_DN);
294       /* Get parent object */
295       $ldap->cat($parent_obj);
296       $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
297         
298       /* New objects require special handling */
299       if($is_new){
301         /* check if there is already an entry named like this,
302             in one of our parent releases */
303         if(!empty($parent_obj)){
304           if(DEBUG_FAI_FUNC) { 
305             echo "There is already an entry named like this.</b><br>";
307             echo "<b>Saving main object</b>".$Current_DN;
308             print_a($objectAttrs);
309           }    
310           save_FAI_object($Current_DN,$objectAttrs);
312           foreach($r as $key){
313             if(DEBUG_FAI_FUNC) { 
314               echo "<b>Saving parent to following release</b> ".$key;
315               print_a($parent_attrs);
316             }
317             save_FAI_object($key,$parent_attrs);
318           }
319         }else{
321           if(DEBUG_FAI_FUNC) { 
322             echo "<b>Saving main object</b>".$Current_DN;
323             print_a($objectAttrs);
324           }
325           save_FAI_object($Current_DN,$objectAttrs);
327           if(isset($objectAttrs['FAIstate'])){
328             $objectAttrs['FAIstate'] .= "|removed"; 
329           }else{
330             $objectAttrs['FAIstate'] = "|removed";
331           }
333           foreach($r as $key ){
334             if(DEBUG_FAI_FUNC) { 
335               echo "<b>Create an empty placeholder in follwing release</b> ".$key; 
336               print_a($objectAttrs);
337             }
338             save_FAI_object($key,$objectAttrs);
339           }
340         }
341       }else{
343         /* check if we must patch the follwing release */
344         if(!empty($r)){
345           foreach($r as $key ){
346             if(DEBUG_FAI_FUNC) { 
347               echo "<b>Copy current objects original attributes to next release</b> ".$key;
348               print_a($parent_attrs);
349             }
350             save_FAI_object($key,$parent_attrs);
351           }
352         }
354         if(DEBUG_FAI_FUNC) { 
355           echo "<b>Saving current object</b>".$parent_obj;
356           print_a($objectAttrs);
357         }
358         save_FAI_object($parent_obj,$objectAttrs);
360         if(($parent_obj != $Current_DN)){
361           print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN));
362         }
363       }
364     }
365   } 
366   $_SESSION['FAI_objects_to_save'] = array();
370 /* this function will remove all unused (deleted) objects,
371     that have no parent object */
372 function clean_up_releases($Current_DN)
374   global $config;
375   $ldap = $config->get_ldap_link();
376   $ldap->cd($config->current['BASE']);
378   /* Collect some basic informations and initialize some variables */ 
379   $base_release       = get_release_dn($Current_DN);
380   $previous_releases  = array_reverse(get_previous_releases_of_this_release($base_release,true));
381   $Kill = array();
382   $Skip = array();
384   /* We must also include the given release dn */
385   $previous_releases[] = $base_release;
387   /* Walk through all releases */
388   foreach($previous_releases as $release){
390     /* Get fai departments */
391     $deps_to_search = get_FAI_departments($release); 
393     /* For every single department  (ou=hoos,ou ..) */
394     foreach($deps_to_search as $fai_base){
396       /* Ldap search for fai classes specified in this release */
397       $ldap->cd($fai_base);
398       $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
400       /* check the returned objects, and add/replace them in our return variable */
401       while($attr = $ldap->fetch()){
402         
403         $buffer = array();
404 #        $name = str_ireplace($release,"",$attr['dn']);
405         $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
407         if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
409           /* Check if this object is required somehow */    
410           if(!isset($Skip[$name])){
411             $Kill[$attr['dn']] = $attr['dn'];
412           }
413         }else{
414       
415           /* This object is required (not removed), so do not 
416               delete any following sub releases of this object */
417           $Skip[$name] = $attr['dn'];
418         }
419       }
420     }
421   }
422   return($Kill);
426 /* Remove numeric index and 'count' from ldap->fetch result */
427 function prepare_ldap_fetch_to_be_saved($attrs)
429   foreach($attrs as $key => $value){
430     if(is_numeric($key) || ($key == "count") || ($key == "dn")){
431       unset($attrs[$key]);
432     }
433     if(is_array($value) && isset($value['count'])){
434       unset($attrs[$key]['count']);
435     }
436   }
437   return($attrs);
441 /* Save given attrs to specified dn*/
442 function save_FAI_object($dn,$attrs)
444   global $config;
445   $ldap = $config->get_ldap_link();
446   $ldap->cd($config->current['BASE']);
447   $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
448   $ldap->cd($dn);
449  
450   $ldap->cat($dn,array("dn"));
451   if($ldap->count()){
453     /* Remove FAIstate*/
454     if(!isset($attrs['FAIstate'])){
455       $attrs['FAIstate'] = array();
456     }
458     $ldap->modify($attrs);
459   }else{
460   
461     /* Unset description if empty  */
462     if(empty($attrs['description'])){
463       unset($attrs['description']);
464     }    
466     $ldap->add($attrs);
467   }
468   show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
472 /* Return FAIstate freeze branch or "" for specified release department */
473 function get_release_tag($dn)
475   global $config;
476   $ldap = $config->get_ldap_link();
477   $ldap->cd($dn);
478   $ldap->cat($dn,array("FAIstate"));
480   if($ldap->count()){
481   
482     $attr = $ldap->fetch();
483     if(isset($attr['FAIstate'][0])){
484       if(preg_match("/freeze/",$attr['FAIstate'][0])){
485         return("freeze");
486       }elseif(preg_match("/branch/",$attr['FAIstate'][0])){
487         return("branch");
488       }
489     }
490   }
491   return("");
495 function get_following_releases_that_inherit_this_object($dn)
497   global $config;
498   $ldap = $config->get_ldap_link();
499   $ldap->cd($config->current['BASE']);
501   $ret = array();
503   /* Get base release */
504   $base_release = get_release_dn($dn);
506   /* Get previous release dns */
507   $sub_releases = get_sub_releases_of_this_release($base_release);
509   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
510 #  $dn_suffix = str_ireplace($base_release,"",$dn);
511   $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
513   /* Check if given object also exists whitin one of these releases */
514   foreach($sub_releases as $p_release => $name){
516     $check_dn = $dn_suffix.$p_release;
517   
518     $ldap->cat($check_dn,array("dn","objectClass"));
519     
520     if($ldap->count()){
521       //return($ret);
522     }else{
523       $ret[$check_dn]=$check_dn;
524     }
525   }
526   return($ret);
530 /* Get previous version of the object dn */
531 function get_parent_release_object($dn,$include_myself=true)
533   global $config;
534   $ldap = $config->get_ldap_link();
535   $ldap->cd($config->current['BASE']);
536   $previous_releases= array();
538   /* Get base release */
539   $base_release = get_release_dn($dn);
540   if($include_myself){
541     $previous_releases[] = $base_release;  
542   }
544   /* Get previous release dns */
545   $tmp = get_previous_releases_of_this_release($base_release,true);
546   foreach($tmp as $release){
547     $previous_releases[] = $release;
548   }
550   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
551 #  $dn_suffix = str_ireplace($base_release,"",$dn);
552   $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
553     
554   /* Check if given object also exists whitin one of these releases */
555   foreach($previous_releases as $p_release){
556     $check_dn = $dn_suffix.$p_release;
557     $ldap->cat($check_dn,array("dn","objectClass"));
558     
559     if($ldap->count()){
560       return($check_dn);
561     }
562   }
563   return("");
567 /* return release names of all parent releases */
568 function get_previous_releases_of_this_release($dn,$flat)
570   global $config;
571   $ldap = $config->get_ldap_link();
572   $ldap->cd($config->current['BASE']);
573   $ret = array();
575   /* Explode dns into pieces, to be able to build parent dns */
576   $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$dn));
578   if(!is_array($dns_to_check)){
579     return;  
580   }
582   /* Unset first entry which represents the given dn */
583   unset($dns_to_check['count']); 
584   unset($dns_to_check[key($dns_to_check)]);
586   /* Create dns addresses and check if this dn is a release dn */
587   $id = 0;
588   while(count($dns_to_check)){
590     /* build parent dn */
591     $new_dn = "";
592     foreach($dns_to_check as $part){
593       $new_dn .= $part.",";
594     }
595     $new_dn .= $config->current['BASE'];
597     /* check if this dn is a release */
598     if(is_release_department($new_dn)){
599       if($flat){
600         $ret[$id] = $new_dn; 
601       }else{
602         $ret = array($new_dn=>$ret); 
603       }
604       $id ++;
605     }else{
606       return($ret);
607     }
608     reset($dns_to_check);
609     unset($dns_to_check[key($dns_to_check)]);
610   }
611   return($ret);
612
615 /* This function returns all sub release names, recursivly  */
616 function get_sub_releases_of_this_release($dn,$flat = false)
618   global $config;
619   $res  = array();
620   $ldap = $config->get_ldap_link();
621   $ldap->cd($config->current['BASE']);
622   $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
623   while($attr = $ldap->fetch()){
625     /* Append department name */
626     if($flat){
627       $res[$attr['dn']] = $attr['ou'][0];
628     }else{
629       $res[$attr['dn']] = array();
630     }
632     /* Get sub release departments of this department */
633     if(in_array("FAIbranch",$attr['objectClass'])) {
634       if($flat){
635         $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
636         foreach($tmp as $dn => $value){
637           $res[$dn]=$value;
638         }
639       }else{
640         $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
641       }
642     }
643   }
644   return($res);
648 /* Check if the given department is a release department */
649 function is_release_department($dn)
651   global $config;
652   $ldap = $config->get_ldap_link();
653   $ldap->cd($config->current['BASE']);
654   $ldap->cat($dn,array("objectClass","ou"));
656   /* Check objectClasses and name to check if this is a release department */
657   if($ldap->count()){
658     $attrs = $ldap->fetch();
659                         
660     $ou = "";
661     if(isset($attrs['ou'][0])){
662       $ou = $attrs['ou'][0];    
663     }
664         
665     if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
666       return($attrs['dn']);
667     }
668   }
669   return(false);
673 /* This function returns the dn of the object release */
674 function get_release_dn($Current_DN)
676   global $config;
677   $ldap = $config->get_ldap_link();
678   $ldap->cd($config->current['BASE']);
680   /* Split dn into pices */ 
681   $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$Current_DN));
683   if(!is_array($dns_to_check)){
684     return;  
685   }
687   /* Use dn pieces, to create sub dns like 
688       ou=test,ou=1,ou=0...
689               ou=1,ou=0...
690                    ou=0... 
691     To check which dn is our release container.
692   */
693   unset($dns_to_check['count']); 
694   while(count($dns_to_check)){
696     /* Create dn */
697     $new_dn = "";
698     foreach($dns_to_check as $part){
699       $new_dn .= $part.",";
700     }
701     $new_dn .= $config->current['BASE'];
703     /* Check if this dn is a release dn */
704     if(is_release_department($new_dn)){
705       return($new_dn);
706     }
708     /* Remove first element of dn pieces */
709     reset($dns_to_check);
710     unset($dns_to_check[key($dns_to_check)]);
711   }
712   return("");
715 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
716 ?>