Code

W3c ......
[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       $ldap->cd($fai_base);
41       $ldap->search($filter,array("dn","objectClass","FAIstate"));
43       /* check the returned objects, and add/replace them in our return variable */
44       while($attr = $ldap->fetch()){
45         
46         $buffer = array();
47         $name = str_ireplace($release,"",$attr['dn']);
49         if(isset($attr['FAIstate'][0])){
50           if(preg_match("/removed$/",$attr['FAIstate'][0])){
51             if(isset($res[$name])){
52               unset($res[$name]);
53             }
54             continue;
55           }
56         }
59         /* In detailed mode are some additonal informations visible */
60         if($detailed){
62           /* Create list of parents */
63           if(isset($res[$name])){
64             $buffer = $res[$name];
65             $buffer['parents'][] = $res[$name]['dn'];
66           }else{
67             $buffer['parents'] = array();
68           }
70           /* Append objectClass to resulsts */
71           $buffer['objectClass']  = $attr['objectClass'];
72           unset($buffer['objectClass'][0]);
73         }
75         /* Add this object to our list */
76         $buffer['dn']           = $attr['dn'];
77         $res[$name] = $buffer;
78       }
79     }
80   }
81   return($res);
82 }
85 /* Return all relevant FAI departments */
86 function get_FAI_departments($suffix = "")
87 {
88   $arr = array("hooks","scripts","disk","packages","profiles","templates","variables");
89   $tmp = array();
90   if(preg_match("/^,/",$suffix)){
91     $suffix = preg_replace("/^,/","",$suffix);
92   }
93   foreach($arr as $name){
94     if(empty($suffix)){
95       $tmp[$name] = "ou=".$name;
96     }else{
97       $tmp[$name] = "ou=".$name.",".$suffix;
98     }
99   }
100   return($tmp);
104 /* Return all releases within the given base */
105 function get_all_releases_from_base($dn,$appendedName=false)
107   global $config;
108     
109   if(!preg_match("/ou=fai,ou=configs,ou=systems,/",$dn)){
110     $base = "ou=fai,ou=configs,ou=systems,".$dn;
111   }else{
112     $base = $dn;
113   }
114   $res = array();  
115   
116   $ldap = $config->get_ldap_link();
117   $ldap->cd($base);
118   $ldap->search("(objectClass=FAIbranch)",array("ou","dn"));
119   while($attrs = $ldap->fetch()){
120     if($appendedName){
121       $res[$attrs['dn']] = convert_department_dn(preg_replace("/,ou=fai,ou=configs,ou=system.*$/","",$attrs['dn']));
122     }else{
123       $res[$attrs['dn']] = $attrs['ou'][0];
124     }
125   }
126   return($res);
130 /* Add this object to list of objects, that must be checked for release saving */
131 function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
133   /* Get ldap object */  
134   global $config;
135   $addObj['Current_DN'] = $Current_DN;
136   $addObj['objectAttrs']= $objectAttrs;
137   $addObj['removed']    = $removed;
138   $addObj['diff']       = TRUE;
140   if(!$removed){
141     $ldap = $config->get_ldap_link();
142     $ldap->cd($config->current['BASE']);
144     /* Get some basic informations */
145     $parent_obj   = get_parent_release_object($Current_DN);
146     if(!empty($parent_obj)){
147       $ldap->cat($parent_obj,array("*"));
148       $attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
150       if(!array_diff_FAI( $attrs,$objectAttrs)){
151         $addObj['diff'] = FALSE;
152       }
153     } 
154   }
155    
156   $_SESSION['FAI_objects_to_save'][$Current_DN] =  $addObj;
160 /* Detect differences in attribute arrays  */
161 function array_diff_FAI($ar1,$ar2)
164   if((!isset($ar1['description'])) || (isset($ar1['description']) && (count($ar1['description']) == 0))){
165     $ar1['description'] = "";
166   }
167   if((!isset($ar2['description'])) || (isset($ar2['description']) && (count($ar2['description']) == 0))){
168     $ar2['description'] = "";
169   }
171   if(count($ar1) != count($ar2)) {
172     return (true);
173   }
175   foreach($ar1 as $key1 => $val1){
177     if((is_array($val1)) && (count($val1)==1)){
178       $ar1[$key1] = $val1[0];
179     }
181     if((is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
182       $val1 = $val1[0];
183       $ar2[$key1] = $ar2[$key1][0];
184     }
185   }
186   ksort($ar1);
187   ksort($ar2);
188   if(count( array_diff($ar1,$ar2))){
189     return(true);
190   }else{
191     return(false);
192   }
196 /* check which objects must be saved, and save them */
197 function save_release_changes_now()
199   /* Variable init*/
200   $to_save = array();
202   /* check which objects must be saved */
203   foreach($_SESSION['FAI_objects_to_save'] as $Current_DN => $object){
204     if($object['diff']){
205       $sub_name = $Current_DN;
206       while(isset($_SESSION['FAI_objects_to_save'][$sub_name])){
207         $to_save[strlen($sub_name)][$sub_name] = $_SESSION['FAI_objects_to_save'][$sub_name]; 
208         unset($_SESSION['FAI_objects_to_save'][$sub_name]);
209         $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
210       }
211     }
212   }
214   /* Sort list of objects that must be saved, and ensure that 
215       container   objects are safed, before their childs are saved */
216   ksort($to_save);
217   $tmp = array();
218   foreach($to_save as $SubObjects){
219     foreach($SubObjects as $object){
220       $tmp[] = $object;
221     }
222   }
223   $to_save = $tmp;
225   /* Save objects and manage the correct release behavior*/
226   foreach($to_save as $save){
228     $Current_DN = $save['Current_DN'];
229     $removed    = $save['removed'];
230     $objectAttrs= $save['objectAttrs'];
232     /* Get ldap object */ 
233     global $config;
234     $ldap = $config->get_ldap_link();
235     $ldap->cd($config->current['BASE']);
237     /* Get some basic informations */
238     $base_release       = get_release_dn($Current_DN);
239     $sub_releases       = get_sub_releases_of_this_release($base_release,true);
240     $parent_obj         = get_parent_release_object($Current_DN);
241     $following_releases = get_sub_releases_of_this_release($base_release,true);
242     
243     /* Check if given dn exists or if is a new entry */
244     $ldap->cat($Current_DN);
245     if(!$ldap->count()){
246       $is_new = true;
247     }else{
248       $is_new = false;
249     }
250    
251     /* if parameter removed is true, we have to add FAIstate to the current attrs 
252           FAIstate should end with ...|removed after this operation */  
253     if($removed ){
254       $ldap->cat($Current_DN);
256       /* Get current object, because we must add the FAIstate ...|removed */
257       if((!$ldap->count()) && !empty($parent_obj)){
258         $ldap->cat($parent_obj);
259       }
261       /* Check if we have found a suiteable object */ 
262       if(!$ldap->count()){
263         echo "Error can't remove this object ".$Current_DN;
264         return;
265       }else{
267         /* Set FAIstate to current objectAttrs */
268         $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
269         if(isset($objectAttrs['FAIstate'][0])){
270           if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
271             $objectAttrs['FAIstate'][0] .= "|removed";
272           }
273         }else{
274           $objectAttrs['FAIstate'][0] = "|removed";
275         }
276       }
277     }
278    
279     /* Check if this a leaf release or not */ 
280     if(count($following_releases) == 0 ){
282       /* This is a leaf object. It isn't unherited by any other object */    
283       if(DEBUG_FAI_FUNC) { 
284         echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
285         print_a($objectAttrs);
286       }
287       save_FAI_object($Current_DN,$objectAttrs);
288     }else{
290       /* This object is inherited by some sub releases */  
292       /* Get all releases, that inherit this object */ 
293       $r = get_following_releases_that_inherit_this_object($Current_DN);
295       /* Get parent object */
296       $ldap->cat($parent_obj);
297       $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
298         
299       /* New objects require special handling */
300       if($is_new){
302         /* check if there is already an entry named like this,
303             in one of our parent releases */
304         if(!empty($parent_obj)){
305           if(DEBUG_FAI_FUNC) { 
306             echo "There is already an entry named like this.</b><br>";
308             echo "<b>Saving main object</b>".$Current_DN;
309             print_a($objectAttrs);
310           }    
311           save_FAI_object($Current_DN,$objectAttrs);
313           foreach($r as $key){
314             if(DEBUG_FAI_FUNC) { 
315               echo "<b>Saving parent to following release</b> ".$key;
316               print_a($parent_attrs);
317             }
318             save_FAI_object($key,$parent_attrs);
319           }
320         }else{
322           if(DEBUG_FAI_FUNC) { 
323             echo "<b>Saving main object</b>".$Current_DN;
324             print_a($objectAttrs);
325           }
326           save_FAI_object($Current_DN,$objectAttrs);
328           if(isset($objectAttrs['FAIstate'])){
329             $objectAttrs['FAIstate'] .= "|removed"; 
330           }else{
331             $objectAttrs['FAIstate'] = "|removed";
332           }
334           foreach($r as $key ){
335             if(DEBUG_FAI_FUNC) { 
336               echo "<b>Create an empty placeholder in follwing release</b> ".$key; 
337               print_a($objectAttrs);
338             }
339             save_FAI_object($key,$objectAttrs);
340           }
341         }
342       }else{
344         /* check if we must patch the follwing release */
345         if(!empty($r)){
346           foreach($r as $key ){
347             if(DEBUG_FAI_FUNC) { 
348               echo "<b>Copy current objects original attributes to next release</b> ".$key;
349               print_a($parent_attrs);
350             }
351             save_FAI_object($key,$parent_attrs);
352           }
353         }
355         if(DEBUG_FAI_FUNC) { 
356           echo "<b>Saving current object</b>".$parent_obj;
357           print_a($objectAttrs);
358         }
359         save_FAI_object($parent_obj,$objectAttrs);
361         if(($parent_obj != $Current_DN)){
362           print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN));
363         }
364       }
365     }
366   } 
367   $_SESSION['FAI_objects_to_save'] = array();
371 /* this function will remove all unused (deleted) objects,
372     that have no parent object */
373 function clean_up_releases($Current_DN)
375   global $config;
376   $ldap = $config->get_ldap_link();
377   $ldap->cd($config->current['BASE']);
379   /* Collect some basic informations and initialize some variables */ 
380   $base_release       = get_release_dn($Current_DN);
381   $previous_releases  = array_reverse(get_previous_releases_of_this_release($base_release,true));
382   $Kill = array();
383   $Skip = array();
385   /* We must also include the given release dn */
386   $previous_releases[] = $base_release;
388   /* Walk through all releases */
389   foreach($previous_releases as $release){
391     /* Get fai departments */
392     $deps_to_search = get_FAI_departments($release); 
394     /* For every single department  (ou=hoos,ou ..) */
395     foreach($deps_to_search as $fai_base){
397       /* Ldap search for fai classes specified in this release */
398       $ldap->cd($fai_base);
399       $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
401       /* check the returned objects, and add/replace them in our return variable */
402       while($attr = $ldap->fetch()){
403         
404         $buffer = array();
405         $name = str_ireplace($release,"",$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);
512   /* Check if given object also exists whitin one of these releases */
513   foreach($sub_releases as $p_release => $name){
515     $check_dn = $dn_suffix.$p_release;
516   
517     $ldap->cat($check_dn,array("dn","objectClass"));
518     
519     if($ldap->count()){
520       //return($ret);
521     }else{
522       $ret[$check_dn]=$check_dn;
523     }
524   }
525   return($ret);
529 /* Get previous version of the object dn */
530 function get_parent_release_object($dn,$include_myself=true)
532   global $config;
533   $ldap = $config->get_ldap_link();
534   $ldap->cd($config->current['BASE']);
535   $previous_releases= array();
537   /* Get base release */
538   $base_release = get_release_dn($dn);
539   if($include_myself){
540     $previous_releases[] = $base_release;  
541   }
543   /* Get previous release dns */
544   $tmp = get_previous_releases_of_this_release($base_release,true);
545   foreach($tmp as $release){
546     $previous_releases[] = $release;
547   }
549   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
550   $dn_suffix = str_ireplace($base_release,"",$dn);
551     
552   /* Check if given object also exists whitin one of these releases */
553   foreach($previous_releases as $p_release){
554     $check_dn = $dn_suffix.$p_release;
555     $ldap->cat($check_dn,array("dn","objectClass"));
556     
557     if($ldap->count()){
558       return($check_dn);
559     }
560   }
561   return("");
565 /* return release names of all parent releases */
566 function get_previous_releases_of_this_release($dn,$flat)
568   global $config;
569   $ldap = $config->get_ldap_link();
570   $ldap->cd($config->current['BASE']);
571   $ret = array();
573   /* Explode dns into pieces, to be able to build parent dns */
574   $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$dn),0);
575   if(!is_array($dns_to_check)){
576     return;  
577   }
579   /* Unset first entry which represents the given dn */
580   unset($dns_to_check['count']); 
581   unset($dns_to_check[key($dns_to_check)]);
583   /* Create dns addresses and check if this dn is a release dn */
584   $id = 0;
585   while(count($dns_to_check)){
587     /* build parent dn */
588     $new_dn = "";
589     foreach($dns_to_check as $part){
590       $new_dn .= $part.",";
591     }
592     $new_dn .= $config->current['BASE'];
594     /* check if this dn is a release */
595     if(is_release_department($new_dn)){
596       if($flat){
597         $ret[$id] = $new_dn; 
598       }else{
599         $ret = array($new_dn=>$ret); 
600       }
601       $id ++;
602     }else{
603       return($ret);
604     }
605     reset($dns_to_check);
606     unset($dns_to_check[key($dns_to_check)]);
607   }
608   return($ret);
609
612 /* This function returns all sub release names, recursivly  */
613 function get_sub_releases_of_this_release($dn,$flat = false)
615   global $config;
616   $res  = array();
617   $ldap = $config->get_ldap_link();
618   $ldap->cd($config->current['BASE']);
619   $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
620   while($attr = $ldap->fetch()){
622     /* Append department name */
623     if($flat){
624       $res[$attr['dn']] = $attr['ou'][0];
625     }else{
626       $res[$attr['dn']] = array();
627     }
629     /* Get sub release departments of this department */
630     if(in_array("FAIbranch",$attr['objectClass'])) {
631       if($flat){
632         $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
633         foreach($tmp as $dn => $value){
634           $res[$dn]=$value;
635         }
636       }else{
637         $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
638       }
639     }
640   }
641   return($res);
645 /* Check if the given department is a release department */
646 function is_release_department($dn)
648   global $config;
649   $ldap = $config->get_ldap_link();
650   $ldap->cd($config->current['BASE']);
651   $ldap->cat($dn,array("objectClass","ou"));
653   /* Check objectClasses and name to check if this is a release department */
654   if($ldap->count()){
655     $attrs = $ldap->fetch();
656                         
657     $ou = "";
658     if(isset($attrs['ou'][0])){
659       $ou = $attrs['ou'][0];    
660     }
661         
662     if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
663       return($attrs['dn']);
664     }
665   }
666   return(false);
670 /* This function returns the dn of the object release */
671 function get_release_dn($Current_DN)
673   global $config;
674   $ldap = $config->get_ldap_link();
675   $ldap->cd($config->current['BASE']);
677   /* Split dn into pices */ 
678   $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$Current_DN),0);
679   if(!is_array($dns_to_check)){
680     return;  
681   }
683   /* Use dn pieces, to create sub dns like 
684       ou=test,ou=1,ou=0...
685               ou=1,ou=0...
686                    ou=0... 
687     To check which dn is our release container.
688   */
689   unset($dns_to_check['count']); 
690   while(count($dns_to_check)){
692     /* Create dn */
693     $new_dn = "";
694     foreach($dns_to_check as $part){
695       $new_dn .= $part.",";
696     }
697     $new_dn .= $config->current['BASE'];
699     /* Check if this dn is a release dn */
700     if(is_release_department($new_dn)){
701       return($new_dn);
702     }
704     /* Remove first element of dn pieces */
705     reset($dns_to_check);
706     unset($dns_to_check[key($dns_to_check)]);
707   }
708   return("");
711 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
712 ?>