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()){
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 = "")
101 {
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);
115 }
118 /* Return all releases within the given base */
119 function get_all_releases_from_base($dn)
120 {
121 global $config;
122 $base = "ou=fai,ou=configs,ou=systems,".$dn;
123 $res = array();
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);
132 }
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)
137 {
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 }
161 $_SESSION['FAI_objects_to_save'][$Current_DN] = $addObj;
162 }
165 /* Detect differences in attribute arrays */
166 function array_diff_FAI($ar1,$ar2)
167 {
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 }
184 }
187 /* check which objects must be saved, and save them */
188 function save_release_changes_now()
189 {
190 /* Variable init*/
191 $to_save = array();
194 /* check which attributes must realy be saved */
195 foreach($_SESSION['FAI_objects_to_save'] as $Current_DN => $object){
196 if($object['diff']){
197 $sub_name = $Current_DN;
198 while(isset($_SESSION['FAI_objects_to_save'][$sub_name])){
201 $to_save[strlen($sub_name)][$sub_name] = $_SESSION['FAI_objects_to_save'][$sub_name];
202 unset($_SESSION['FAI_objects_to_save'][$sub_name]);
203 $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
204 }
205 }
206 }
208 /* Sort list of objects that must be saved, and ensure that
209 container objects are safed, before their childs are saved */
210 ksort($to_save);
212 $tmp = array();
213 foreach($to_save as $SubObjects){
214 foreach($SubObjects as $object){
215 $tmp[] = $object;
216 }
217 }
219 $to_save = $tmp;
221 /* Save objects and manage the correct release behavior*/
222 foreach($to_save as $save){
224 $Current_DN = $save['Current_DN'];
225 $removed = $save['removed'];
226 $objectAttrs= $save['objectAttrs'];
228 /* Get ldap object */
229 global $config;
230 $ldap = $config->get_ldap_link();
231 $ldap->cd($config->current['BASE']);
233 /* Get some basic informations */
234 $base_release = get_release_dn($Current_DN);
235 $sub_releases = get_sub_releases_of_this_release($base_release,true);
236 $parent_obj = get_parent_release_object($Current_DN);
237 $following_releases = get_sub_releases_of_this_release($base_release,true);
239 /* Check if given dn exists or if is a new entry */
240 $ldap->cat($Current_DN);
241 if(!$ldap->count()){
242 $is_new = true;
243 }else{
244 $is_new = false;
245 }
247 /* if parameter removed is true, we have to add FAIstate to the current attrs
248 FAIstate should end with ...|removed after this operation */
249 if($removed ){
250 $ldap->cat($Current_DN);
252 /* Get current object, because we must add the FAIstate ...|removed */
253 if((!$ldap->count()) && !empty($parent_obj)){
254 $ldap->cat($parent_obj);
255 }
257 /* Check if we have found a suiteable object */
258 if(!$ldap->count()){
259 echo "Error can't remove this object ".$Current_DN;
260 return;
261 }else{
263 /* Set FAIstate to current objectAttrs */
264 $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
265 if(isset($objectAttrs['FAIstate'][0])){
266 if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
267 $objectAttrs['FAIstate'][0] .= "|removed";
268 }
269 }else{
270 $objectAttrs['FAIstate'][0] = "|removed";
271 }
272 }
273 }
276 /* Check if this a leaf release or not */
277 if(count($following_releases) == 0 ){
279 /* This is a leaf object. It isn't unherited by any other object */
280 if(DEBUG_FAI_FUNC) {
281 echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
282 print_a($objectAttrs);
283 }
284 save_FAI_object($Current_DN,$objectAttrs);
285 }else{
287 /* This object is inherited by some sub releases */
289 /* Get all releases, that inherit this object */
290 $r = get_following_releases_that_inherit_this_object($Current_DN);
292 /* Get parent object */
293 $ldap->cat($parent_obj);
294 $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
296 /* New objects require special handling */
297 if($is_new){
299 /* check if there is already an entry named like this,
300 in one of our parent releases */
301 if(!empty($parent_obj)){
302 if(DEBUG_FAI_FUNC) {
303 echo "There is already an entry named like this.</b><br>";
305 echo "<b>Saving main object</b>".$Current_DN;
306 print_a($objectAttrs);
307 }
308 save_FAI_object($Current_DN,$objectAttrs);
310 foreach($r as $key){
311 if(DEBUG_FAI_FUNC) {
312 echo "<b>Saving parent to following release</b> ".$key;
313 print_a($parent_attrs);
314 }
315 save_FAI_object($key,$parent_attrs);
316 }
317 }else{
319 if(DEBUG_FAI_FUNC) {
320 echo "<b>Saving main object</b>".$Current_DN;
321 print_a($objectAttrs);
322 }
323 save_FAI_object($Current_DN,$objectAttrs);
325 if(isset($objectAttrs['FAIstate'])){
326 $objectAttrs['FAIstate'] .= "|removed";
327 }else{
328 $objectAttrs['FAIstate'] = "|removed";
329 }
331 foreach($r as $key ){
332 if(DEBUG_FAI_FUNC) {
333 echo "<b>Create an empty placeholder in follwing release</b> ".$key;
334 print_a($objectAttrs);
335 }
336 save_FAI_object($key,$objectAttrs);
337 }
338 }
339 }else{
341 /* check if we must patch the follwing release */
342 if(!empty($r)){
343 foreach($r as $key ){
344 if(DEBUG_FAI_FUNC) {
345 echo "<b>Copy current objects original attributes to next release</b> ".$key;
346 print_a($parent_attrs);
347 }
348 save_FAI_object($key,$parent_attrs);
349 }
350 }
352 if(DEBUG_FAI_FUNC) {
353 echo "<b>Saving current object</b>".$parent_obj;
354 print_a($objectAttrs);
355 }
356 save_FAI_object($parent_obj,$objectAttrs);
358 if(($parent_obj != $Current_DN)){
359 print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN));
360 }
361 }
362 }
363 }
364 $_SESSION['FAI_objects_to_save'] = array();
365 }
368 /* this function will remove all unused (deleted) objects,
369 that have no parent object */
370 function clean_up_releases($Current_DN)
371 {
372 global $config;
373 $ldap = $config->get_ldap_link();
374 $ldap->cd($config->current['BASE']);
376 /* Collect some basic informations and initialize some variables */
377 $base_release = get_release_dn($Current_DN);
378 $previous_releases = array_reverse(get_previous_releases_of_this_release($base_release,true));
379 $Kill = array();
380 $Skip = array();
382 /* We must also include the given release dn */
383 $previous_releases[] = $base_release;
385 /* Walk through all releases */
386 foreach($previous_releases as $release){
388 /* Get fai departments */
389 $deps_to_search = get_FAI_departments($release);
391 /* For every single department (ou=hoos,ou ..) */
392 foreach($deps_to_search as $fai_base){
394 /* Ldap search for fai classes specified in this release */
395 $ldap->cd($fai_base);
396 $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
398 /* check the returned objects, and add/replace them in our return variable */
399 while($attr = $ldap->fetch()){
401 $buffer = array();
402 $name = str_ireplace($release,"",$attr['dn']);
404 if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
406 /* Check if this object is required somehow */
407 if(!isset($Skip[$name])){
408 $Kill[$attr['dn']] = $attr['dn'];
409 }
410 }else{
412 /* This object is required (not removed), so do not
413 delete any following sub releases of this object */
414 $Skip[$name] = $attr['dn'];
415 }
416 }
417 }
418 }
419 return($Kill);
420 }
423 function prepare_ldap_fetch_to_be_saved($attrs)
424 {
425 foreach($attrs as $key => $value){
426 if(is_numeric($key) || ($key == "count") || ($key == "dn")){
427 unset($attrs[$key]);
428 }
429 if(is_array($value) && isset($value['count'])){
430 unset($attrs[$key]['count']);
431 }
432 }
433 return($attrs);
434 }
437 function save_FAI_object($dn,$attrs)
438 {
439 global $config;
440 $ldap = $config->get_ldap_link();
441 $ldap->cd($config->current['BASE']);
442 echo $dn;
443 print_a($attrs);
444 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
445 $ldap->cd($dn);
447 $ldap->cat($dn,array("dn"));
448 if($ldap->count()){
449 $ldap->modify($attrs);
450 }else{
451 $ldap->add($attrs);
452 }
453 show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
454 }
457 function get_following_releases_that_inherit_this_object($dn)
458 {
459 global $config;
460 $ldap = $config->get_ldap_link();
461 $ldap->cd($config->current['BASE']);
463 $ret = array();
465 /* Get base release */
466 $base_release = get_release_dn($dn);
468 /* Get previous release dns */
469 $sub_releases = get_sub_releases_of_this_release($base_release);
471 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
472 $dn_suffix = str_ireplace($base_release,"",$dn);
474 /* Check if given object also exists whitin one of these releases */
475 foreach($sub_releases as $p_release => $name){
477 $check_dn = $dn_suffix.$p_release;
479 $ldap->cat($check_dn,array("dn","objectClass"));
481 if($ldap->count()){
482 //return($ret);
483 }else{
484 $ret[$check_dn]=$check_dn;
485 }
486 }
487 return($ret);
488 }
491 /* Get previous version of the object dn */
492 function get_parent_release_object($dn,$include_myself=true)
493 {
494 global $config;
495 $ldap = $config->get_ldap_link();
496 $ldap->cd($config->current['BASE']);
497 $previous_releases= array();
499 /* Get base release */
500 $base_release = get_release_dn($dn);
501 if($include_myself){
502 $previous_releases[] = $base_release;
503 }
505 /* Get previous release dns */
506 $tmp = get_previous_releases_of_this_release($base_release,true);
507 foreach($tmp as $release){
508 $previous_releases[] = $release;
509 }
511 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
512 $dn_suffix = str_ireplace($base_release,"",$dn);
514 /* Check if given object also exists whitin one of these releases */
515 foreach($previous_releases as $p_release){
516 $check_dn = $dn_suffix.$p_release;
517 $ldap->cat($check_dn,array("dn","objectClass"));
519 if($ldap->count()){
520 return($check_dn);
521 }
522 }
523 return("");
524 }
527 /* return release names of all parent releases */
528 function get_previous_releases_of_this_release($dn,$flat)
529 {
530 global $config;
531 $ldap = $config->get_ldap_link();
532 $ldap->cd($config->current['BASE']);
533 $ret = array();
535 /* Explode dns into pieces, to be able to build parent dns */
536 $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$dn),0);
537 if(!is_array($dns_to_check)){
538 return;
539 }
541 /* Unset first entry which represents the given dn */
542 unset($dns_to_check['count']);
543 unset($dns_to_check[key($dns_to_check)]);
545 /* Create dns addresses and check if this dn is a release dn */
546 $id = 0;
547 while(count($dns_to_check)){
549 /* build parent dn */
550 $new_dn = "";
551 foreach($dns_to_check as $part){
552 $new_dn .= $part.",";
553 }
554 $new_dn .= $config->current['BASE'];
556 /* check if this dn is a release */
557 if(is_release_department($new_dn)){
558 if($flat){
559 $ret[$id] = $new_dn;
560 }else{
561 $ret = array($new_dn=>$ret);
562 }
563 $id ++;
564 }else{
565 return($ret);
566 }
567 reset($dns_to_check);
568 unset($dns_to_check[key($dns_to_check)]);
569 }
570 return($ret);
571 }
574 /* This function returns all sub release names, recursivly */
575 function get_sub_releases_of_this_release($dn,$flat = false)
576 {
577 global $config;
578 $res = array();
579 $ldap = $config->get_ldap_link();
580 $ldap->cd($config->current['BASE']);
581 $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
582 while($attr = $ldap->fetch()){
584 /* Append department name */
585 if($flat){
586 $res[$attr['dn']] = $attr['ou'][0];
587 }else{
588 $res[$attr['dn']] = array();
589 }
591 /* Get sub release departments of this department */
592 if(in_array("FAIbranch",$attr['objectClass'])) {
593 if($flat){
594 $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
595 foreach($tmp as $dn => $value){
596 $res[$dn]=$value;
597 }
598 }else{
599 $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
600 }
601 }
602 }
603 return($res);
604 }
607 /* Check if the given department is a release department */
608 function is_release_department($dn)
609 {
610 global $config;
611 $ldap = $config->get_ldap_link();
612 $ldap->cd($config->current['BASE']);
613 $ldap->cat($dn,array("objectClass","ou"));
615 /* Check objectClasses and name to check if this is a release department */
616 if($ldap->count()){
617 $attrs = $ldap->fetch();
619 $ou = "";
620 if(isset($attrs['ou'][0])){
621 $ou = $attrs['ou'][0];
622 }
624 if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
625 return($attrs['dn']);
626 }
627 }
628 return(false);
629 }
632 /* This function returns the dn of the object release */
633 function get_release_dn($Current_DN)
634 {
635 global $config;
636 $ldap = $config->get_ldap_link();
637 $ldap->cd($config->current['BASE']);
639 /* Split dn into pices */
640 $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$Current_DN),0);
641 if(!is_array($dns_to_check)){
642 return;
643 }
645 /* Use dn pieces, to create sub dns like
646 ou=test,ou=1,ou=0...
647 ou=1,ou=0...
648 ou=0...
649 To check which dn is our release container.
650 */
651 unset($dns_to_check['count']);
652 while(count($dns_to_check)){
654 /* Create dn */
655 $new_dn = "";
656 foreach($dns_to_check as $part){
657 $new_dn .= $part.",";
658 }
659 $new_dn .= $config->current['BASE'];
661 /* Check if this dn is a release dn */
662 if(is_release_department($new_dn)){
663 return($new_dn);
664 }
666 /* Remove first element of dn pieces */
667 reset($dns_to_check);
668 unset($dns_to_check[key($dns_to_check)]);
669 }
670 return("");
671 }
673 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
674 ?>