Code

Replaced str_ireplace it is not PHP4 compatible.
[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']);
48         $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
50         if(isset($attr['FAIstate'][0])){
51           if(preg_match("/removed$/",$attr['FAIstate'][0])){
52             if(isset($res[$name])){
53               unset($res[$name]);
54             }
55             continue;
56           }
57         }
60         /* In detailed mode are some additonal informations visible */
61         if($detailed){
63           /* Create list of parents */
64           if(isset($res[$name])){
65             $buffer = $res[$name];
66             $buffer['parents'][] = $res[$name]['dn'];
67           }else{
68             $buffer['parents'] = array();
69           }
71           /* Append objectClass to resulsts */
72           $buffer['objectClass']  = $attr['objectClass'];
73           unset($buffer['objectClass'][0]);
74         }
76         /* Add this object to our list */
77         $buffer['dn']           = $attr['dn'];
78         $res[$name] = $buffer;
79       }
80     }
81   }
82   return($res);
83 }
86 /* Return all relevant FAI departments */
87 function get_FAI_departments($suffix = "")
88 {
89   $arr = array("hooks","scripts","disk","packages","profiles","templates","variables");
90   $tmp = array();
91   if(preg_match("/^,/",$suffix)){
92     $suffix = preg_replace("/^,/","",$suffix);
93   }
94   foreach($arr as $name){
95     if(empty($suffix)){
96       $tmp[$name] = "ou=".$name;
97     }else{
98       $tmp[$name] = "ou=".$name.",".$suffix;
99     }
100   }
101   return($tmp);
105 /* Return all releases within the given base */
106 function get_all_releases_from_base($dn,$appendedName=false)
108   global $config;
109     
110   if(!preg_match("/ou=fai,ou=configs,ou=systems,/",$dn)){
111     $base = "ou=fai,ou=configs,ou=systems,".$dn;
112   }else{
113     $base = $dn;
114   }
115   $res = array();  
116   
117   $ldap = $config->get_ldap_link();
118   $ldap->cd($base);
119   $ldap->search("(objectClass=FAIbranch)",array("ou","dn"));
120   while($attrs = $ldap->fetch()){
121     if($appendedName){
122       $res[$attrs['dn']] = convert_department_dn(preg_replace("/,ou=fai,ou=configs,ou=system.*$/","",$attrs['dn']));
123     }else{
124       $res[$attrs['dn']] = $attrs['ou'][0];
125     }
126   }
127   return($res);
131 /* Add this object to list of objects, that must be checked for release saving */
132 function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
134   /* Get ldap object */  
135   global $config;
136   $addObj['Current_DN'] = $Current_DN;
137   $addObj['objectAttrs']= $objectAttrs;
138   $addObj['removed']    = $removed;
139   $addObj['diff']       = TRUE;
141   if(!$removed){
142     $ldap = $config->get_ldap_link();
143     $ldap->cd($config->current['BASE']);
145     /* Get some basic informations */
146     $parent_obj   = get_parent_release_object($Current_DN);
147     if(!empty($parent_obj)){
148       $ldap->cat($parent_obj,array("*"));
149       $attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
151       if(!array_diff_FAI( $attrs,$objectAttrs)){
152         $addObj['diff'] = FALSE;
153       }
154     } 
155   }
156    
157   $_SESSION['FAI_objects_to_save'][$Current_DN] =  $addObj;
161 /* Detect differences in attribute arrays  */
162 function array_diff_FAI($ar1,$ar2)
165   if((!isset($ar1['description'])) || (isset($ar1['description']) && (count($ar1['description']) == 0))){
166     $ar1['description'] = "";
167   }
168   if((!isset($ar2['description'])) || (isset($ar2['description']) && (count($ar2['description']) == 0))){
169     $ar2['description'] = "";
170   }
172   if(count($ar1) != count($ar2)) {
173     return (true);
174   }
176   foreach($ar1 as $key1 => $val1){
178     if((is_array($val1)) && (count($val1)==1)){
179       $ar1[$key1] = $val1[0];
180     }
182     if((is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
183       $val1 = $val1[0];
184       $ar2[$key1] = $ar2[$key1][0];
185     }
186   }
187   ksort($ar1);
188   ksort($ar2);
189   if(count( array_diff($ar1,$ar2))){
190     return(true);
191   }else{
192     return(false);
193   }
197 /* check which objects must be saved, and save them */
198 function save_release_changes_now()
200   /* Variable init*/
201   $to_save = array();
203   /* check which objects must be saved */
204   foreach($_SESSION['FAI_objects_to_save'] as $Current_DN => $object){
205     if($object['diff']){
206       $sub_name = $Current_DN;
207       while(isset($_SESSION['FAI_objects_to_save'][$sub_name])){
208         $to_save[strlen($sub_name)][$sub_name] = $_SESSION['FAI_objects_to_save'][$sub_name]; 
209         unset($_SESSION['FAI_objects_to_save'][$sub_name]);
210         $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
211       }
212     }
213   }
215   /* Sort list of objects that must be saved, and ensure that 
216       container   objects are safed, before their childs are saved */
217   ksort($to_save);
218   $tmp = array();
219   foreach($to_save as $SubObjects){
220     foreach($SubObjects as $object){
221       $tmp[] = $object;
222     }
223   }
224   $to_save = $tmp;
226   /* Save objects and manage the correct release behavior*/
227   foreach($to_save as $save){
229     $Current_DN = $save['Current_DN'];
230     $removed    = $save['removed'];
231     $objectAttrs= $save['objectAttrs'];
233     /* Get ldap object */ 
234     global $config;
235     $ldap = $config->get_ldap_link();
236     $ldap->cd($config->current['BASE']);
238     /* Get some basic informations */
239     $base_release       = get_release_dn($Current_DN);
240     $sub_releases       = get_sub_releases_of_this_release($base_release,true);
241     $parent_obj         = get_parent_release_object($Current_DN);
242     $following_releases = get_sub_releases_of_this_release($base_release,true);
243     
244     /* Check if given dn exists or if is a new entry */
245     $ldap->cat($Current_DN);
246     if(!$ldap->count()){
247       $is_new = true;
248     }else{
249       $is_new = false;
250     }
251    
252     /* if parameter removed is true, we have to add FAIstate to the current attrs 
253           FAIstate should end with ...|removed after this operation */  
254     if($removed ){
255       $ldap->cat($Current_DN);
257       /* Get current object, because we must add the FAIstate ...|removed */
258       if((!$ldap->count()) && !empty($parent_obj)){
259         $ldap->cat($parent_obj);
260       }
262       /* Check if we have found a suiteable object */ 
263       if(!$ldap->count()){
264         echo "Error can't remove this object ".$Current_DN;
265         return;
266       }else{
268         /* Set FAIstate to current objectAttrs */
269         $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
270         if(isset($objectAttrs['FAIstate'][0])){
271           if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
272             $objectAttrs['FAIstate'][0] .= "|removed";
273           }
274         }else{
275           $objectAttrs['FAIstate'][0] = "|removed";
276         }
277       }
278     }
279    
280     /* Check if this a leaf release or not */ 
281     if(count($following_releases) == 0 ){
283       /* This is a leaf object. It isn't unherited by any other object */    
284       if(DEBUG_FAI_FUNC) { 
285         echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
286         print_a($objectAttrs);
287       }
288       save_FAI_object($Current_DN,$objectAttrs);
289     }else{
291       /* This object is inherited by some sub releases */  
293       /* Get all releases, that inherit this object */ 
294       $r = get_following_releases_that_inherit_this_object($Current_DN);
296       /* Get parent object */
297       $ldap->cat($parent_obj);
298       $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
299         
300       /* New objects require special handling */
301       if($is_new){
303         /* check if there is already an entry named like this,
304             in one of our parent releases */
305         if(!empty($parent_obj)){
306           if(DEBUG_FAI_FUNC) { 
307             echo "There is already an entry named like this.</b><br>";
309             echo "<b>Saving main object</b>".$Current_DN;
310             print_a($objectAttrs);
311           }    
312           save_FAI_object($Current_DN,$objectAttrs);
314           foreach($r as $key){
315             if(DEBUG_FAI_FUNC) { 
316               echo "<b>Saving parent to following release</b> ".$key;
317               print_a($parent_attrs);
318             }
319             save_FAI_object($key,$parent_attrs);
320           }
321         }else{
323           if(DEBUG_FAI_FUNC) { 
324             echo "<b>Saving main object</b>".$Current_DN;
325             print_a($objectAttrs);
326           }
327           save_FAI_object($Current_DN,$objectAttrs);
329           if(isset($objectAttrs['FAIstate'])){
330             $objectAttrs['FAIstate'] .= "|removed"; 
331           }else{
332             $objectAttrs['FAIstate'] = "|removed";
333           }
335           foreach($r as $key ){
336             if(DEBUG_FAI_FUNC) { 
337               echo "<b>Create an empty placeholder in follwing release</b> ".$key; 
338               print_a($objectAttrs);
339             }
340             save_FAI_object($key,$objectAttrs);
341           }
342         }
343       }else{
345         /* check if we must patch the follwing release */
346         if(!empty($r)){
347           foreach($r as $key ){
348             if(DEBUG_FAI_FUNC) { 
349               echo "<b>Copy current objects original attributes to next release</b> ".$key;
350               print_a($parent_attrs);
351             }
352             save_FAI_object($key,$parent_attrs);
353           }
354         }
356         if(DEBUG_FAI_FUNC) { 
357           echo "<b>Saving current object</b>".$parent_obj;
358           print_a($objectAttrs);
359         }
360         save_FAI_object($parent_obj,$objectAttrs);
362         if(($parent_obj != $Current_DN)){
363           print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN));
364         }
365       }
366     }
367   } 
368   $_SESSION['FAI_objects_to_save'] = array();
372 /* this function will remove all unused (deleted) objects,
373     that have no parent object */
374 function clean_up_releases($Current_DN)
376   global $config;
377   $ldap = $config->get_ldap_link();
378   $ldap->cd($config->current['BASE']);
380   /* Collect some basic informations and initialize some variables */ 
381   $base_release       = get_release_dn($Current_DN);
382   $previous_releases  = array_reverse(get_previous_releases_of_this_release($base_release,true));
383   $Kill = array();
384   $Skip = array();
386   /* We must also include the given release dn */
387   $previous_releases[] = $base_release;
389   /* Walk through all releases */
390   foreach($previous_releases as $release){
392     /* Get fai departments */
393     $deps_to_search = get_FAI_departments($release); 
395     /* For every single department  (ou=hoos,ou ..) */
396     foreach($deps_to_search as $fai_base){
398       /* Ldap search for fai classes specified in this release */
399       $ldap->cd($fai_base);
400       $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
402       /* check the returned objects, and add/replace them in our return variable */
403       while($attr = $ldap->fetch()){
404         
405         $buffer = array();
406 #        $name = str_ireplace($release,"",$attr['dn']);
407         $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
409         if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
411           /* Check if this object is required somehow */    
412           if(!isset($Skip[$name])){
413             $Kill[$attr['dn']] = $attr['dn'];
414           }
415         }else{
416       
417           /* This object is required (not removed), so do not 
418               delete any following sub releases of this object */
419           $Skip[$name] = $attr['dn'];
420         }
421       }
422     }
423   }
424   return($Kill);
428 /* Remove numeric index and 'count' from ldap->fetch result */
429 function prepare_ldap_fetch_to_be_saved($attrs)
431   foreach($attrs as $key => $value){
432     if(is_numeric($key) || ($key == "count") || ($key == "dn")){
433       unset($attrs[$key]);
434     }
435     if(is_array($value) && isset($value['count'])){
436       unset($attrs[$key]['count']);
437     }
438   }
439   return($attrs);
443 /* Save given attrs to specified dn*/
444 function save_FAI_object($dn,$attrs)
446   global $config;
447   $ldap = $config->get_ldap_link();
448   $ldap->cd($config->current['BASE']);
449   $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
450   $ldap->cd($dn);
451  
452   $ldap->cat($dn,array("dn"));
453   if($ldap->count()){
455     /* Remove FAIstate*/
456     if(!isset($attrs['FAIstate'])){
457       $attrs['FAIstate'] = array();
458     }
460     $ldap->modify($attrs);
461   }else{
462   
463     /* Unset description if empty  */
464     if(empty($attrs['description'])){
465       unset($attrs['description']);
466     }    
468     $ldap->add($attrs);
469   }
470   show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
474 /* Return FAIstate freeze branch or "" for specified release department */
475 function get_release_tag($dn)
477   global $config;
478   $ldap = $config->get_ldap_link();
479   $ldap->cd($dn);
480   $ldap->cat($dn,array("FAIstate"));
482   if($ldap->count()){
483   
484     $attr = $ldap->fetch();
485     if(isset($attr['FAIstate'][0])){
486       if(preg_match("/freeze/",$attr['FAIstate'][0])){
487         return("freeze");
488       }elseif(preg_match("/branch/",$attr['FAIstate'][0])){
489         return("branch");
490       }
491     }
492   }
493   return("");
497 function get_following_releases_that_inherit_this_object($dn)
499   global $config;
500   $ldap = $config->get_ldap_link();
501   $ldap->cd($config->current['BASE']);
503   $ret = array();
505   /* Get base release */
506   $base_release = get_release_dn($dn);
508   /* Get previous release dns */
509   $sub_releases = get_sub_releases_of_this_release($base_release);
511   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
512 #  $dn_suffix = str_ireplace($base_release,"",$dn);
513   $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
515   /* Check if given object also exists whitin one of these releases */
516   foreach($sub_releases as $p_release => $name){
518     $check_dn = $dn_suffix.$p_release;
519   
520     $ldap->cat($check_dn,array("dn","objectClass"));
521     
522     if($ldap->count()){
523       //return($ret);
524     }else{
525       $ret[$check_dn]=$check_dn;
526     }
527   }
528   return($ret);
532 /* Get previous version of the object dn */
533 function get_parent_release_object($dn,$include_myself=true)
535   global $config;
536   $ldap = $config->get_ldap_link();
537   $ldap->cd($config->current['BASE']);
538   $previous_releases= array();
540   /* Get base release */
541   $base_release = get_release_dn($dn);
542   if($include_myself){
543     $previous_releases[] = $base_release;  
544   }
546   /* Get previous release dns */
547   $tmp = get_previous_releases_of_this_release($base_release,true);
548   foreach($tmp as $release){
549     $previous_releases[] = $release;
550   }
552   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
553 #  $dn_suffix = str_ireplace($base_release,"",$dn);
554   $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
555     
556   /* Check if given object also exists whitin one of these releases */
557   foreach($previous_releases as $p_release){
558     $check_dn = $dn_suffix.$p_release;
559     $ldap->cat($check_dn,array("dn","objectClass"));
560     
561     if($ldap->count()){
562       return($check_dn);
563     }
564   }
565   return("");
569 /* return release names of all parent releases */
570 function get_previous_releases_of_this_release($dn,$flat)
572   global $config;
573   $ldap = $config->get_ldap_link();
574   $ldap->cd($config->current['BASE']);
575   $ret = array();
577   /* Explode dns into pieces, to be able to build parent dns */
578 #  $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$dn),0);
579   $dns_to_check = ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$dn),0);
580   echo "<font color='red'>Replace ldap_explode_dn it is evil, it changes characters within result... </font><br>";
582   if(!is_array($dns_to_check)){
583     return;  
584   }
586   /* Unset first entry which represents the given dn */
587   unset($dns_to_check['count']); 
588   unset($dns_to_check[key($dns_to_check)]);
590   /* Create dns addresses and check if this dn is a release dn */
591   $id = 0;
592   while(count($dns_to_check)){
594     /* build parent dn */
595     $new_dn = "";
596     foreach($dns_to_check as $part){
597       $new_dn .= $part.",";
598     }
599     $new_dn .= $config->current['BASE'];
601     /* check if this dn is a release */
602     if(is_release_department($new_dn)){
603       if($flat){
604         $ret[$id] = $new_dn; 
605       }else{
606         $ret = array($new_dn=>$ret); 
607       }
608       $id ++;
609     }else{
610       return($ret);
611     }
612     reset($dns_to_check);
613     unset($dns_to_check[key($dns_to_check)]);
614   }
615   return($ret);
616
619 /* This function returns all sub release names, recursivly  */
620 function get_sub_releases_of_this_release($dn,$flat = false)
622   global $config;
623   $res  = array();
624   $ldap = $config->get_ldap_link();
625   $ldap->cd($config->current['BASE']);
626   $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
627   while($attr = $ldap->fetch()){
629     /* Append department name */
630     if($flat){
631       $res[$attr['dn']] = $attr['ou'][0];
632     }else{
633       $res[$attr['dn']] = array();
634     }
636     /* Get sub release departments of this department */
637     if(in_array("FAIbranch",$attr['objectClass'])) {
638       if($flat){
639         $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
640         foreach($tmp as $dn => $value){
641           $res[$dn]=$value;
642         }
643       }else{
644         $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
645       }
646     }
647   }
648   return($res);
652 /* Check if the given department is a release department */
653 function is_release_department($dn)
655   global $config;
656   $ldap = $config->get_ldap_link();
657   $ldap->cd($config->current['BASE']);
658   $ldap->cat($dn,array("objectClass","ou"));
660   /* Check objectClasses and name to check if this is a release department */
661   if($ldap->count()){
662     $attrs = $ldap->fetch();
663                         
664     $ou = "";
665     if(isset($attrs['ou'][0])){
666       $ou = $attrs['ou'][0];    
667     }
668         
669     if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
670       return($attrs['dn']);
671     }
672   }
673   return(false);
677 /* This function returns the dn of the object release */
678 function get_release_dn($Current_DN)
680   global $config;
681   $ldap = $config->get_ldap_link();
682   $ldap->cd($config->current['BASE']);
684   /* Split dn into pices */ 
685 #  $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$Current_DN),0);
686   $dns_to_check = ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$Current_DN),0);
687   echo "<font color='red'>Replace ldap_explode_dn it is evil, it changes characters within result... </font><br>";
689   if(!is_array($dns_to_check)){
690     return;  
691   }
693   /* Use dn pieces, to create sub dns like 
694       ou=test,ou=1,ou=0...
695               ou=1,ou=0...
696                    ou=0... 
697     To check which dn is our release container.
698   */
699   unset($dns_to_check['count']); 
700   while(count($dns_to_check)){
702     /* Create dn */
703     $new_dn = "";
704     foreach($dns_to_check as $part){
705       $new_dn .= $part.",";
706     }
707     $new_dn .= $config->current['BASE'];
709     /* Check if this dn is a release dn */
710     if(is_release_department($new_dn)){
711       return($new_dn);
712     }
714     /* Remove first element of dn pieces */
715     reset($dns_to_check);
716     unset($dns_to_check[key($dns_to_check)]);
717   }
718   return("");
721 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
722 ?>