Code

removed debug output
[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']);
17   /* Collect some basic informations and initialize some variables */ 
18   $base_release       = get_release_dn($Current_DN);
19   $previous_releases  = array_reverse(get_previous_releases_of_this_release($base_release,true));
20   $res = array();
21   $tmp = array();
23   /* We must also include the given release dn */
24   $previous_releases[] = $base_release;
26   /* Walk through all releases */
27   foreach($previous_releases as $release){
29     /* Get fai departments */
30     $deps_to_search = get_FAI_departments($release); 
32     /* For every single department  (ou=hoos,ou ..) */
33     foreach($deps_to_search as $fai_base){
35       /* Ldap search for fai classes specified in this release */
36       $ldap->cd($fai_base);
37       $ldap->search($filter,array("dn","objectClass","FAIstate"));
39       /* check the returned objects, and add/replace them in our return variable */
40       while($attr = $ldap->fetch()){
41         
42         $buffer = array();
43         $name = str_ireplace($release,"",$attr['dn']);
45         if(isset($attr['FAIstate'][0])){
46           if(preg_match("/removed$/",$attr['FAIstate'][0])){
47             if(isset($res[$name])){
48               unset($res[$name]);
49             }
50             continue;
51           }
52         }
55         /* In detailed mode are some additonal informations visible */
56         if($detailed){
58           /* Create list of parents */
59           if(isset($res[$name])){
60             $buffer = $res[$name];
61             $buffer['parents'][] = $res[$name]['dn'];
62           }else{
63             $buffer['parents'] = array();
64           }
66           /* Append objectClass to resulsts */
67           $buffer['objectClass']  = $attr['objectClass'];
68           unset($buffer['objectClass'][0]);
69         }
71         /* Add this object to our list */
72         $buffer['dn']           = $attr['dn'];
73         $res[$name] = $buffer;
74       }
75     }
76   }
77   return($res);
78 }
82 /* Return the object defining ObjectClass e.g. FAIscriptEntry */
83 function get_FAI_type($attr)
84 {
85   $arr = array( "FAIprofile","FAIpartitionTable", "FAIpartitionDisk","FAIpartitionEntry","FAIhook","FAIhookEntry",
86       "FAIscriptEntry","FAIscript","FAIvariable","FAIvariableEntry","FAIpackageList","FAItemplate",
87       "FAItemplateEntry","FAIdebconfInfo","FAIrepository","FAIrepositoryServer","FAIbranch","FAIreleaseTag");  
88   foreach($arr as $name){    
89     if(in_array($name,$attr['objectClass'])){
90       preg_match("");
91       return($name);
92     }
93   }
94   if(DEBUG_FAI_FUNC) { echo "Not found"; } 
95   return("");
96
99 /* Return all relevant FAI departments */
100 function get_FAI_departments($suffix = "")
102   $arr = array("hooks","scripts","disk","packages","profiles","templates","variables");
103   $tmp = array();
104   if(preg_match("/^,/",$suffix)){
105     $suffix = preg_replace("/^,/","",$suffix);
106   }
107   foreach($arr as $name){
108     if(empty($suffix)){
109       $tmp[$name] = "ou=".$name;
110     }else{
111       $tmp[$name] = "ou=".$name.",".$suffix;
112     }
113   }
114   return($tmp);
118 /* Return all releases within the given base */
119 function get_all_releases_from_base($dn)
121   global $config;
122   $base = "ou=fai,ou=configs,ou=systems,".$dn;
123   $res = array();  
124   
125   $ldap = $config->get_ldap_link();
126   $ldap->cd($base);
127   $ldap->search("(objectClass=FAIbranch)",array("ou","dn"));
128   while($attrs = $ldap->fetch()){
129     $res[$attrs['dn']] = $attrs['ou'][0];
130   } 
131   return($res);
135 /* Add this object to list of objects, that must be checked for release saving */
136 function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
138   /* Get ldap object */  
139   global $config;
140   $addObj['Current_DN'] = $Current_DN;
141   $addObj['objectAttrs']= $objectAttrs;
142   $addObj['removed']    = $removed;
143   $addObj['diff']       = TRUE;
145   if(!$removed){
146     $ldap = $config->get_ldap_link();
147     $ldap->cd($config->current['BASE']);
149     /* Get some basic informations */
150     $parent_obj   = get_parent_release_object($Current_DN);
151     if(!empty($parent_obj)){
152       $ldap->cat($parent_obj,array("*"));
153       $attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
155       if(!array_diff_FAI( $attrs,$objectAttrs)){
156         $addObj['diff'] = FALSE;
157       }
158     } 
159   }
160    
161   $_SESSION['FAI_objects_to_save'][$Current_DN] =  $addObj;
165 /* Detect differences in attribute arrays  */
166 function array_diff_FAI($ar1,$ar2)
168   foreach($ar1 as $key1 => $val1){
170     if((is_array($val1)) && (count($val1)==1)){
171       $ar1[$key1] = $val1[0];
172     }
174     if((is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
175       $val1 = $val1[0];
176       $ar2[$key1] = $ar2[$key1][0];
177     }
178   }
179   if(count( array_diff($ar1,$ar2))){
180     return(true);
181   }else{
182     return(false);
183   }
187 /* check which objects must be saved, and save them */
188 function save_release_changes_now()
190   /* Variable init*/
191   $to_save = array();
193   /* check which objects must be saved */
194   foreach($_SESSION['FAI_objects_to_save'] as $Current_DN => $object){
195     if($object['diff']){
196       $sub_name = $Current_DN;
197       while(isset($_SESSION['FAI_objects_to_save'][$sub_name])){
198         $to_save[strlen($sub_name)][$sub_name] = $_SESSION['FAI_objects_to_save'][$sub_name]; 
199         unset($_SESSION['FAI_objects_to_save'][$sub_name]);
200         $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
201       }
202     }
203   }
205   /* Sort list of objects that must be saved, and ensure that 
206       container   objects are safed, before their childs are saved */
207   ksort($to_save);
208   $tmp = array();
209   foreach($to_save as $SubObjects){
210     foreach($SubObjects as $object){
211       $tmp[] = $object;
212     }
213   }
214   $to_save = $tmp;
216   /* Save objects and manage the correct release behavior*/
217   foreach($to_save as $save){
219     $Current_DN = $save['Current_DN'];
220     $removed    = $save['removed'];
221     $objectAttrs= $save['objectAttrs'];
223     /* Get ldap object */ 
224     global $config;
225     $ldap = $config->get_ldap_link();
226     $ldap->cd($config->current['BASE']);
228     /* Get some basic informations */
229     $base_release       = get_release_dn($Current_DN);
230     $sub_releases       = get_sub_releases_of_this_release($base_release,true);
231     $parent_obj         = get_parent_release_object($Current_DN);
232     $following_releases = get_sub_releases_of_this_release($base_release,true);
233     
234     /* Check if given dn exists or if is a new entry */
235     $ldap->cat($Current_DN);
236     if(!$ldap->count()){
237       $is_new = true;
238     }else{
239       $is_new = false;
240     }
241     
242     /* if parameter removed is true, we have to add FAIstate to the current attrs 
243           FAIstate should end with ...|removed after this operation */  
244     if($removed ){
245       $ldap->cat($Current_DN);
247       /* Get current object, because we must add the FAIstate ...|removed */
248       if((!$ldap->count()) && !empty($parent_obj)){
249         $ldap->cat($parent_obj);
250       }
252       /* Check if we have found a suiteable object */ 
253       if(!$ldap->count()){
254         echo "Error can't remove this object ".$Current_DN;
255         return;
256       }else{
258         /* Set FAIstate to current objectAttrs */
259         $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
260         if(isset($objectAttrs['FAIstate'][0])){
261           if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
262             $objectAttrs['FAIstate'][0] .= "|removed";
263           }
264         }else{
265           $objectAttrs['FAIstate'][0] = "|removed";
266         }
267       }
268     }
269    
270     /* Check if this a leaf release or not */ 
271     if(count($following_releases) == 0 ){
273       /* This is a leaf object. It isn't unherited by any other object */    
274       if(DEBUG_FAI_FUNC) { 
275         echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
276         print_a($objectAttrs);
277       }
278       save_FAI_object($Current_DN,$objectAttrs);
279     }else{
281       /* This object is inherited by some sub releases */  
283       /* Get all releases, that inherit this object */ 
284       $r = get_following_releases_that_inherit_this_object($Current_DN);
286       /* Get parent object */
287       $ldap->cat($parent_obj);
288       $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
289         
290       /* New objects require special handling */
291       if($is_new){
293         /* check if there is already an entry named like this,
294             in one of our parent releases */
295         if(!empty($parent_obj)){
296           if(DEBUG_FAI_FUNC) { 
297             echo "There is already an entry named like this.</b><br>";
299             echo "<b>Saving main object</b>".$Current_DN;
300             print_a($objectAttrs);
301           }    
302           save_FAI_object($Current_DN,$objectAttrs);
304           foreach($r as $key){
305             if(DEBUG_FAI_FUNC) { 
306               echo "<b>Saving parent to following release</b> ".$key;
307               print_a($parent_attrs);
308             }
309             save_FAI_object($key,$parent_attrs);
310           }
311         }else{
313           if(DEBUG_FAI_FUNC) { 
314             echo "<b>Saving main object</b>".$Current_DN;
315             print_a($objectAttrs);
316           }
317           save_FAI_object($Current_DN,$objectAttrs);
319           if(isset($objectAttrs['FAIstate'])){
320             $objectAttrs['FAIstate'] .= "|removed"; 
321           }else{
322             $objectAttrs['FAIstate'] = "|removed";
323           }
325           foreach($r as $key ){
326             if(DEBUG_FAI_FUNC) { 
327               echo "<b>Create an empty placeholder in follwing release</b> ".$key; 
328               print_a($objectAttrs);
329             }
330             save_FAI_object($key,$objectAttrs);
331           }
332         }
333       }else{
335         /* check if we must patch the follwing release */
336         if(!empty($r)){
337           foreach($r as $key ){
338             if(DEBUG_FAI_FUNC) { 
339               echo "<b>Copy current objects original attributes to next release</b> ".$key;
340               print_a($parent_attrs);
341             }
342             save_FAI_object($key,$parent_attrs);
343           }
344         }
346         if(DEBUG_FAI_FUNC) { 
347           echo "<b>Saving current object</b>".$parent_obj;
348           print_a($objectAttrs);
349         }
350         save_FAI_object($parent_obj,$objectAttrs);
352         if(($parent_obj != $Current_DN)){
353           print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN));
354         }
355       }
356     }
357   } 
358   $_SESSION['FAI_objects_to_save'] = array();
362 /* this function will remove all unused (deleted) objects,
363     that have no parent object */
364 function clean_up_releases($Current_DN)
366   global $config;
367   $ldap = $config->get_ldap_link();
368   $ldap->cd($config->current['BASE']);
370   /* Collect some basic informations and initialize some variables */ 
371   $base_release       = get_release_dn($Current_DN);
372   $previous_releases  = array_reverse(get_previous_releases_of_this_release($base_release,true));
373   $Kill = array();
374   $Skip = array();
376   /* We must also include the given release dn */
377   $previous_releases[] = $base_release;
379   /* Walk through all releases */
380   foreach($previous_releases as $release){
382     /* Get fai departments */
383     $deps_to_search = get_FAI_departments($release); 
385     /* For every single department  (ou=hoos,ou ..) */
386     foreach($deps_to_search as $fai_base){
388       /* Ldap search for fai classes specified in this release */
389       $ldap->cd($fai_base);
390       $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
392       /* check the returned objects, and add/replace them in our return variable */
393       while($attr = $ldap->fetch()){
394         
395         $buffer = array();
396         $name = str_ireplace($release,"",$attr['dn']);
398         if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
400           /* Check if this object is required somehow */    
401           if(!isset($Skip[$name])){
402             $Kill[$attr['dn']] = $attr['dn'];
403           }
404         }else{
405       
406           /* This object is required (not removed), so do not 
407               delete any following sub releases of this object */
408           $Skip[$name] = $attr['dn'];
409         }
410       }
411     }
412   }
413   return($Kill);
417 function prepare_ldap_fetch_to_be_saved($attrs)
419   foreach($attrs as $key => $value){
420     if(is_numeric($key) || ($key == "count") || ($key == "dn")){
421       unset($attrs[$key]);
422     }
423     if(is_array($value) && isset($value['count'])){
424       unset($attrs[$key]['count']);
425     }
426   }
427   return($attrs);
431 function save_FAI_object($dn,$attrs)
433   global $config;
434   $ldap = $config->get_ldap_link();
435   $ldap->cd($config->current['BASE']);
436   $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
437   $ldap->cd($dn);
438   
439   $ldap->cat($dn,array("dn"));
440   if($ldap->count()){
441     $ldap->modify($attrs);
442   }else{
443     $ldap->add($attrs);
444   }
445   show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
449 function get_following_releases_that_inherit_this_object($dn)
451   global $config;
452   $ldap = $config->get_ldap_link();
453   $ldap->cd($config->current['BASE']);
455   $ret = array();
457   /* Get base release */
458   $base_release = get_release_dn($dn);
460   /* Get previous release dns */
461   $sub_releases = get_sub_releases_of_this_release($base_release);
463   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
464   $dn_suffix = str_ireplace($base_release,"",$dn);
466   /* Check if given object also exists whitin one of these releases */
467   foreach($sub_releases as $p_release => $name){
469     $check_dn = $dn_suffix.$p_release;
470   
471     $ldap->cat($check_dn,array("dn","objectClass"));
472     
473     if($ldap->count()){
474       //return($ret);
475     }else{
476       $ret[$check_dn]=$check_dn;
477     }
478   }
479   return($ret);
483 /* Get previous version of the object dn */
484 function get_parent_release_object($dn,$include_myself=true)
486   global $config;
487   $ldap = $config->get_ldap_link();
488   $ldap->cd($config->current['BASE']);
489   $previous_releases= array();
491   /* Get base release */
492   $base_release = get_release_dn($dn);
493   if($include_myself){
494     $previous_releases[] = $base_release;  
495   }
497   /* Get previous release dns */
498   $tmp = get_previous_releases_of_this_release($base_release,true);
499   foreach($tmp as $release){
500     $previous_releases[] = $release;
501   }
503   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
504   $dn_suffix = str_ireplace($base_release,"",$dn);
505     
506   /* Check if given object also exists whitin one of these releases */
507   foreach($previous_releases as $p_release){
508     $check_dn = $dn_suffix.$p_release;
509     $ldap->cat($check_dn,array("dn","objectClass"));
510     
511     if($ldap->count()){
512       return($check_dn);
513     }
514   }
515   return("");
519 /* return release names of all parent releases */
520 function get_previous_releases_of_this_release($dn,$flat)
522   global $config;
523   $ldap = $config->get_ldap_link();
524   $ldap->cd($config->current['BASE']);
525   $ret = array();
527   /* Explode dns into pieces, to be able to build parent dns */
528   $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$dn),0);
529   if(!is_array($dns_to_check)){
530     return;  
531   }
533   /* Unset first entry which represents the given dn */
534   unset($dns_to_check['count']); 
535   unset($dns_to_check[key($dns_to_check)]);
537   /* Create dns addresses and check if this dn is a release dn */
538   $id = 0;
539   while(count($dns_to_check)){
541     /* build parent dn */
542     $new_dn = "";
543     foreach($dns_to_check as $part){
544       $new_dn .= $part.",";
545     }
546     $new_dn .= $config->current['BASE'];
548     /* check if this dn is a release */
549     if(is_release_department($new_dn)){
550       if($flat){
551         $ret[$id] = $new_dn; 
552       }else{
553         $ret = array($new_dn=>$ret); 
554       }
555       $id ++;
556     }else{
557       return($ret);
558     }
559     reset($dns_to_check);
560     unset($dns_to_check[key($dns_to_check)]);
561   }
562   return($ret);
563
566 /* This function returns all sub release names, recursivly  */
567 function get_sub_releases_of_this_release($dn,$flat = false)
569   global $config;
570   $res  = array();
571   $ldap = $config->get_ldap_link();
572   $ldap->cd($config->current['BASE']);
573   $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
574   while($attr = $ldap->fetch()){
576     /* Append department name */
577     if($flat){
578       $res[$attr['dn']] = $attr['ou'][0];
579     }else{
580       $res[$attr['dn']] = array();
581     }
583     /* Get sub release departments of this department */
584     if(in_array("FAIbranch",$attr['objectClass'])) {
585       if($flat){
586         $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
587         foreach($tmp as $dn => $value){
588           $res[$dn]=$value;
589         }
590       }else{
591         $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
592       }
593     }
594   }
595   return($res);
599 /* Check if the given department is a release department */
600 function is_release_department($dn)
602   global $config;
603   $ldap = $config->get_ldap_link();
604   $ldap->cd($config->current['BASE']);
605   $ldap->cat($dn,array("objectClass","ou"));
607   /* Check objectClasses and name to check if this is a release department */
608   if($ldap->count()){
609     $attrs = $ldap->fetch();
610                         
611     $ou = "";
612     if(isset($attrs['ou'][0])){
613       $ou = $attrs['ou'][0];    
614     }
615         
616     if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
617       return($attrs['dn']);
618     }
619   }
620   return(false);
624 /* This function returns the dn of the object release */
625 function get_release_dn($Current_DN)
627   global $config;
628   $ldap = $config->get_ldap_link();
629   $ldap->cd($config->current['BASE']);
631   /* Split dn into pices */ 
632   $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$Current_DN),0);
633   if(!is_array($dns_to_check)){
634     return;  
635   }
637   /* Use dn pieces, to create sub dns like 
638       ou=test,ou=1,ou=0...
639               ou=1,ou=0...
640                    ou=0... 
641     To check which dn is our release container.
642   */
643   unset($dns_to_check['count']); 
644   while(count($dns_to_check)){
646     /* Create dn */
647     $new_dn = "";
648     foreach($dns_to_check as $part){
649       $new_dn .= $part.",";
650     }
651     $new_dn .= $config->current['BASE'];
653     /* Check if this dn is a release dn */
654     if(is_release_department($new_dn)){
655       return($new_dn);
656     }
658     /* Remove first element of dn pieces */
659     reset($dns_to_check);
660     unset($dns_to_check[key($dns_to_check)]);
661   }
662   return("");
665 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
666 ?>