Code

Replaced session read processes
[gosa.git] / gosa-core / 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("/".normalizePreg(get_ou('faiou'))."/",$dn)){
109     $base = get_ou('faiou').$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("/,".normalizePreg(get_ou('faiou')).".*$/","",$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)) || arr_diff($ar1,$ar2)){
188     return(true);
189   }else{
190     return(false);
191   }
195 function arr_diff($ar1,$ar2)
197   foreach($ar1 as $ak1 => $av1){
198     if(!isset($ar2[$ak1]) || (!($av1 === $ar2[$ak1]))){
199       return(true);
200     }elseif(is_array($av1)){
201       return(arr_diff($av1,$ar2[$ak1]));
202     }
203   }
204   return(FALSE);
210 /* check which objects must be saved, and save them */
211 function save_release_changes_now()
213   /* Variable init*/
214   $to_save = array();
216   /* check which objects must be saved */
217   $FAI_objects_to_save = session::get('FAI_objects_to_save');
218   foreach($FAI_objects_to_save as $Current_DN => $object){
219     if($object['diff']){
220       $sub_name = $Current_DN;
221       while(isset($FAI_objects_to_save[$sub_name])){
222         $to_save[strlen($sub_name)][$sub_name] = $FAI_objects_to_save[$sub_name]; 
223         unset($FAI_objects_to_save[$sub_name]);
224         $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
225       }
226     }
227   }
228   session::set('FAI_objects_to_save',$FAI_objects_to_save);
230   /* Sort list of objects that must be saved, and ensure that 
231       container   objects are safed, before their childs are saved */
232   ksort($to_save);
233   $tmp = array();
234   foreach($to_save as $SubObjects){
235     foreach($SubObjects as $object){
236       $tmp[] = $object;
237     }
238   }
239   $to_save = $tmp;
241   /* Save objects and manage the correct release behavior*/
242   foreach($to_save as $save){
244     $Current_DN = $save['Current_DN'];
245     $removed    = $save['removed'];
246     $objectAttrs= $save['objectAttrs'];
248     /* Get ldap object */ 
249     global $config;
250     $ldap = $config->get_ldap_link();
251     $ldap->cd($config->current['BASE']);
253     /* Get some basic informations */
254     $base_release       = get_release_dn($Current_DN);
255     $sub_releases       = get_sub_releases_of_this_release($base_release,true);
256     $parent_obj         = get_parent_release_object($Current_DN);
257     $following_releases = get_sub_releases_of_this_release($base_release,true);
258     
259     /* Check if given dn exists or if is a new entry */
260     $ldap->cat($Current_DN);
261     if(!$ldap->count()){
262       $is_new = true;
263     }else{
264       $is_new = false;
265     }
266    
267     /* if parameter removed is true, we have to add FAIstate to the current attrs 
268           FAIstate should end with ...|removed after this operation */  
269     if($removed ){
270       $ldap->cat($Current_DN);
272       /* Get current object, because we must add the FAIstate ...|removed */
273       if((!$ldap->count()) && !empty($parent_obj)){
274         $ldap->cat($parent_obj);
275       }
277       /* Check if we have found a suiteable object */ 
278       if(!$ldap->count()){
279         echo "Error can't remove this object ".$Current_DN;
280         return;
281       }else{
283         /* Set FAIstate to current objectAttrs */
284         $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
285         if(isset($objectAttrs['FAIstate'][0])){
286           if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
287             $objectAttrs['FAIstate'][0] .= "|removed";
288           }
289         }else{
290           $objectAttrs['FAIstate'][0] = "|removed";
291         }
292       }
293     }
294    
295     /* Check if this a leaf release or not */ 
296     if(count($following_releases) == 0 ){
298       /* This is a leaf object. It isn't inherited by any other object */    
299       if(DEBUG_FAI_FUNC) { 
300         echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
301         print_a($objectAttrs);
302       }
303       save_FAI_object($Current_DN,$objectAttrs);
304     }else{
306       /* This object is inherited by some sub releases */  
308       /* Get all releases, that inherit this object */ 
309       $r = get_following_releases_that_inherit_this_object($Current_DN);
311       /* Get parent object */
312       $ldap->cat($parent_obj);
313       $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
314         
315       /* New objects require special handling */
316       if($is_new){
318         /* check if there is already an entry named like this,
319             in one of our parent releases */
320         if(!empty($parent_obj)){
321           if(DEBUG_FAI_FUNC) { 
322             echo "There is already an entry named like this.</b><br>";
324             echo "<b>Saving main object</b>".$Current_DN;
325             print_a($objectAttrs);
326           }    
327           save_FAI_object($Current_DN,$objectAttrs);
329           foreach($r as $key){
330             if(DEBUG_FAI_FUNC) { 
331               echo "<b>Saving parent to following release</b> ".$key;
332               print_a($parent_attrs);
333             }
334             save_FAI_object($key,$parent_attrs);
335           }
336         }else{
338           if(DEBUG_FAI_FUNC) { 
339             echo "<b>Saving main object</b>".$Current_DN;
340             print_a($objectAttrs);
341           }
342           save_FAI_object($Current_DN,$objectAttrs);
344           if(isset($objectAttrs['FAIstate'])){
345             $objectAttrs['FAIstate'] .= "|removed"; 
346           }else{
347             $objectAttrs['FAIstate'] = "|removed";
348           }
350           foreach($r as $key ){
351             if(DEBUG_FAI_FUNC) { 
352               echo "<b>Create an empty placeholder in follwing release</b> ".$key; 
353               print_a($objectAttrs);
354             }
355             save_FAI_object($key,$objectAttrs);
356           }
357         }
358       }else{
360         /* check if we must patch the follwing release */
361         if(!empty($r)){
362           foreach($r as $key ){
363             if(DEBUG_FAI_FUNC) { 
364               echo "<b>Copy current objects original attributes to next release</b> ".$key;
365               print_a($parent_attrs);
366             }
367             save_FAI_object($key,$parent_attrs);
368           }
369         }
371         if(DEBUG_FAI_FUNC) { 
372           echo "<b>Saving current object</b>".$parent_obj;
373           print_a($objectAttrs);
374         }
375         save_FAI_object($parent_obj,$objectAttrs);
377         if(($parent_obj != $Current_DN)){
378           print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN));
379         }
380       }
381     }
382   } 
383   session::set('FAI_objects_to_save',array());
387 /* this function will remove all unused (deleted) objects,
388     that have no parent object */
389 function clean_up_releases($Current_DN)
391   global $config;
392   $ldap = $config->get_ldap_link();
393   $ldap->cd($config->current['BASE']);
395   /* Collect some basic informations and initialize some variables */ 
396   $base_release       = get_release_dn($Current_DN);
397   $previous_releases  = array_reverse(get_previous_releases_of_this_release($base_release,true));
398   $Kill = array();
399   $Skip = array();
401   /* We must also include the given release dn */
402   $previous_releases[] = $base_release;
404   /* Walk through all releases */
405   foreach($previous_releases as $release){
407     /* Get fai departments */
408     $deps_to_search = get_FAI_departments($release); 
410     /* For every single department  (ou=hoos,ou ..) */
411     foreach($deps_to_search as $fai_base){
413       /* Ldap search for fai classes specified in this release */
414       $ldap->cd($fai_base);
415       $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
417       /* check the returned objects, and add/replace them in our return variable */
418       while($attr = $ldap->fetch()){
419         
420         $buffer = array();
421 #        $name = str_ireplace($release,"",$attr['dn']);
422         $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
424         if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
426           /* Check if this object is required somehow */    
427           if(!isset($Skip[$name])){
428             $Kill[$attr['dn']] = $attr['dn'];
429           }
430         }else{
431       
432           /* This object is required (not removed), so do not 
433               delete any following sub releases of this object */
434           $Skip[$name] = $attr['dn'];
435         }
436       }
437     }
438   }
439   return($Kill);
443 /* Remove numeric index and 'count' from ldap->fetch result */
444 function prepare_ldap_fetch_to_be_saved($attrs)
446   foreach($attrs as $key => $value){
447     if(is_numeric($key) || ($key == "count") || ($key == "dn")){
448       unset($attrs[$key]);
449     }
450     if(is_array($value) && isset($value['count'])){
451       unset($attrs[$key]['count']);
452     }
453   }
454   return($attrs);
458 /* Save given attrs to specified dn*/
459 function save_FAI_object($dn,$attrs)
461   global $config;
462   $ldap = $config->get_ldap_link();
463   $ldap->cd($config->current['BASE']);
464   $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
465   $ldap->cd($dn);
466  
467   $ldap->cat($dn,array("dn"));
468   if($ldap->count()){
470     /* Remove FAIstate*/
471     if(!isset($attrs['FAIstate'])){
472       $attrs['FAIstate'] = array();
473     }
475     $ldap->modify($attrs);
476   }else{
477   
478     /* Unset description if empty  */
479     if(empty($attrs['description'])){
480       unset($attrs['description']);
481     }    
483     $ldap->add($attrs);
484   }
485   show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
489 /* Return FAIstate freeze branch or "" for specified release department */
490 function get_release_tag($dn)
492   global $config;
493   $ldap = $config->get_ldap_link();
494   $ldap->cd($dn);
495   $ldap->cat($dn,array("FAIstate"));
497   if($ldap->count()){
498   
499     $attr = $ldap->fetch();
500     if(isset($attr['FAIstate'][0])){
501       if(preg_match("/freeze/",$attr['FAIstate'][0])){
502         return("freeze");
503       }elseif(preg_match("/branch/",$attr['FAIstate'][0])){
504         return("branch");
505       }
506     }
507   }
508   return("");
512 function get_following_releases_that_inherit_this_object($dn)
514   global $config;
515   $ldap = $config->get_ldap_link();
516   $ldap->cd($config->current['BASE']);
518   $ret = array();
520   /* Get base release */
521   $base_release = get_release_dn($dn);
523   /* Get previous release dns */
524   $sub_releases = get_sub_releases_of_this_release($base_release);
526   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
527 #  $dn_suffix = str_ireplace($base_release,"",$dn);
528   $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
530   /* Check if given object also exists whitin one of these releases */
531   foreach($sub_releases as $p_release => $name){
533     $check_dn = $dn_suffix.$p_release;
534   
535     $ldap->cat($check_dn,array("dn","objectClass"));
536     
537     if($ldap->count()){
538       //return($ret);
539     }else{
540       $ret[$check_dn]=$check_dn;
541     }
542   }
543   return($ret);
547 /* Get previous version of the object dn */
548 function get_parent_release_object($dn,$include_myself=true)
550   global $config;
551   $ldap = $config->get_ldap_link();
552   $ldap->cd($config->current['BASE']);
553   $previous_releases= array();
555   /* Get base release */
556   $base_release = get_release_dn($dn);
557   if($include_myself){
558     $previous_releases[] = $base_release;  
559   }
561   /* Get previous release dns */
562   $tmp = get_previous_releases_of_this_release($base_release,true);
563   foreach($tmp as $release){
564     $previous_releases[] = $release;
565   }
567   /* Get dn suffix. Example  "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
568 #  $dn_suffix = str_ireplace($base_release,"",$dn);
569   $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
570     
571   /* Check if given object also exists whitin one of these releases */
572   foreach($previous_releases as $p_release){
573     $check_dn = $dn_suffix.$p_release;
574     $ldap->cat($check_dn,array("dn","objectClass"));
575     
576     if($ldap->count()){
577       return($check_dn);
578     }
579   }
580   return("");
584 /* return release names of all parent releases */
585 function get_previous_releases_of_this_release($dn,$flat)
587   global $config;
588   $ldap = $config->get_ldap_link();
589   $ldap->cd($config->current['BASE']);
590   $ret = array();
592   /* Explode dns into pieces, to be able to build parent dns */
593   $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$dn));
595   if(!is_array($dns_to_check)){
596     return;  
597   }
599   /* Unset first entry which represents the given dn */
600   unset($dns_to_check['count']); 
601   unset($dns_to_check[key($dns_to_check)]);
603   /* Create dns addresses and check if this dn is a release dn */
604   $id = 0;
605   while(count($dns_to_check)){
607     /* build parent dn */
608     $new_dn = "";
609     foreach($dns_to_check as $part){
610       $new_dn .= $part.",";
611     }
612     $new_dn .= $config->current['BASE'];
614     /* check if this dn is a release */
615     if(is_release_department($new_dn)){
616       if($flat){
617         $ret[$id] = $new_dn; 
618       }else{
619         $ret = array($new_dn=>$ret); 
620       }
621       $id ++;
622     }else{
623       return($ret);
624     }
625     reset($dns_to_check);
626     unset($dns_to_check[key($dns_to_check)]);
627   }
628   return($ret);
629
632 /* This function returns all sub release names, recursivly  */
633 function get_sub_releases_of_this_release($dn,$flat = false)
635   global $config;
636   $res  = array();
637   $ldap = $config->get_ldap_link();
638   $ldap->cd($config->current['BASE']);
639   $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
640   while($attr = $ldap->fetch()){
642     /* Append department name */
643     if($flat){
644       $res[$attr['dn']] = $attr['ou'][0];
645     }else{
646       $res[$attr['dn']] = array();
647     }
649     /* Get sub release departments of this department */
650     if(in_array("FAIbranch",$attr['objectClass'])) {
651       if($flat){
652         $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
653         foreach($tmp as $dn => $value){
654           $res[$dn]=$value;
655         }
656       }else{
657         $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
658       }
659     }
660   }
661   return($res);
665 /* Check if the given department is a release department */
666 function is_release_department($dn)
668   global $config;
669   $ldap = $config->get_ldap_link();
670   $ldap->cd($config->current['BASE']);
671   $ldap->cat($dn,array("objectClass","ou"));
673   /* Check objectClasses and name to check if this is a release department */
674   if($ldap->count()){
675     $attrs = $ldap->fetch();
676                         
677     $ou = "";
678     if(isset($attrs['ou'][0])){
679       $ou = $attrs['ou'][0];    
680     }
681         
682     if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
683       return($attrs['dn']);
684     }
685   }
686   return(false);
690 /* This function returns the dn of the object release */
691 function get_release_dn($Current_DN)
693   global $config;
694   $ldap = $config->get_ldap_link();
695   $ldap->cd($config->current['BASE']);
697   /* Split dn into pices */ 
698   $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$Current_DN));
700   if(!is_array($dns_to_check)){
701     return;  
702   }
704   /* Use dn pieces, to create sub dns like 
705       ou=test,ou=1,ou=0...
706               ou=1,ou=0...
707                    ou=0... 
708     To check which dn is our release container.
709   */
710   unset($dns_to_check['count']); 
711   while(count($dns_to_check)){
713     /* Create dn */
714     $new_dn = "";
715     foreach($dns_to_check as $part){
716       $new_dn .= $part.",";
717     }
718     $new_dn .= $config->current['BASE'];
720     /* Check if this dn is a release dn */
721     if(is_release_department($new_dn)){
722       return($new_dn);
723     }
725     /* Remove first element of dn pieces */
726     reset($dns_to_check);
727     unset($dns_to_check[key($dns_to_check)]);
728   }
729   return("");
732 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
733 ?>