09c1505145e98b1a51ae949f12b9c02d9a8d01ab
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,$appendedName=false)
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 if($appendedName){
130 $res[$attrs['dn']] = convert_department_dn(preg_replace("/,ou=fai,ou=configs,ou=system.*$/","",$attrs['dn']));
131 }else{
132 $res[$attrs['dn']] = $attrs['ou'][0];
133 }
134 }
135 return($res);
136 }
139 /* Add this object to list of objects, that must be checked for release saving */
140 function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
141 {
142 /* Get ldap object */
143 global $config;
144 $addObj['Current_DN'] = $Current_DN;
145 $addObj['objectAttrs']= $objectAttrs;
146 $addObj['removed'] = $removed;
147 $addObj['diff'] = TRUE;
149 if(!$removed){
150 $ldap = $config->get_ldap_link();
151 $ldap->cd($config->current['BASE']);
153 /* Get some basic informations */
154 $parent_obj = get_parent_release_object($Current_DN);
155 if(!empty($parent_obj)){
156 $ldap->cat($parent_obj,array("*"));
157 $attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
159 if(!array_diff_FAI( $attrs,$objectAttrs)){
160 $addObj['diff'] = FALSE;
161 }
162 }
163 }
165 $_SESSION['FAI_objects_to_save'][$Current_DN] = $addObj;
166 }
169 /* Detect differences in attribute arrays */
170 function array_diff_FAI($ar1,$ar2)
171 {
172 foreach($ar1 as $key1 => $val1){
174 if((is_array($val1)) && (count($val1)==1)){
175 $ar1[$key1] = $val1[0];
176 }
178 if(!isset($ar2[$key1])){
179 return(true);
180 }
182 if((is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
183 $val1 = $val1[0];
184 $ar2[$key1] = $ar2[$key1][0];
185 }
186 }
187 if(count( array_diff($ar1,$ar2))){
188 return(true);
189 }else{
190 return(false);
191 }
192 }
195 /* check which objects must be saved, and save them */
196 function save_release_changes_now()
197 {
198 /* Variable init*/
199 $to_save = array();
201 /* check which objects must be saved */
202 foreach($_SESSION['FAI_objects_to_save'] as $Current_DN => $object){
203 if($object['diff']){
204 $sub_name = $Current_DN;
205 while(isset($_SESSION['FAI_objects_to_save'][$sub_name])){
206 $to_save[strlen($sub_name)][$sub_name] = $_SESSION['FAI_objects_to_save'][$sub_name];
207 unset($_SESSION['FAI_objects_to_save'][$sub_name]);
208 $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
209 }
210 }
211 }
213 /* Sort list of objects that must be saved, and ensure that
214 container objects are safed, before their childs are saved */
215 ksort($to_save);
216 $tmp = array();
217 foreach($to_save as $SubObjects){
218 foreach($SubObjects as $object){
219 $tmp[] = $object;
220 }
221 }
222 $to_save = $tmp;
224 /* Save objects and manage the correct release behavior*/
225 foreach($to_save as $save){
227 $Current_DN = $save['Current_DN'];
228 $removed = $save['removed'];
229 $objectAttrs= $save['objectAttrs'];
231 /* Get ldap object */
232 global $config;
233 $ldap = $config->get_ldap_link();
234 $ldap->cd($config->current['BASE']);
236 /* Get some basic informations */
237 $base_release = get_release_dn($Current_DN);
238 $sub_releases = get_sub_releases_of_this_release($base_release,true);
239 $parent_obj = get_parent_release_object($Current_DN);
240 $following_releases = get_sub_releases_of_this_release($base_release,true);
242 /* Check if given dn exists or if is a new entry */
243 $ldap->cat($Current_DN);
244 if(!$ldap->count()){
245 $is_new = true;
246 }else{
247 $is_new = false;
248 }
250 /* if parameter removed is true, we have to add FAIstate to the current attrs
251 FAIstate should end with ...|removed after this operation */
252 if($removed ){
253 $ldap->cat($Current_DN);
255 /* Get current object, because we must add the FAIstate ...|removed */
256 if((!$ldap->count()) && !empty($parent_obj)){
257 $ldap->cat($parent_obj);
258 }
260 /* Check if we have found a suiteable object */
261 if(!$ldap->count()){
262 echo "Error can't remove this object ".$Current_DN;
263 return;
264 }else{
266 /* Set FAIstate to current objectAttrs */
267 $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
268 if(isset($objectAttrs['FAIstate'][0])){
269 if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
270 $objectAttrs['FAIstate'][0] .= "|removed";
271 }
272 }else{
273 $objectAttrs['FAIstate'][0] = "|removed";
274 }
275 }
276 }
278 /* Check if this a leaf release or not */
279 if(count($following_releases) == 0 ){
281 /* This is a leaf object. It isn't unherited by any other object */
282 if(DEBUG_FAI_FUNC) {
283 echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
284 print_a($objectAttrs);
285 }
286 save_FAI_object($Current_DN,$objectAttrs);
287 }else{
289 /* This object is inherited by some sub releases */
291 /* Get all releases, that inherit this object */
292 $r = get_following_releases_that_inherit_this_object($Current_DN);
294 /* Get parent object */
295 $ldap->cat($parent_obj);
296 $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
298 /* New objects require special handling */
299 if($is_new){
301 /* check if there is already an entry named like this,
302 in one of our parent releases */
303 if(!empty($parent_obj)){
304 if(DEBUG_FAI_FUNC) {
305 echo "There is already an entry named like this.</b><br>";
307 echo "<b>Saving main object</b>".$Current_DN;
308 print_a($objectAttrs);
309 }
310 save_FAI_object($Current_DN,$objectAttrs);
312 foreach($r as $key){
313 if(DEBUG_FAI_FUNC) {
314 echo "<b>Saving parent to following release</b> ".$key;
315 print_a($parent_attrs);
316 }
317 save_FAI_object($key,$parent_attrs);
318 }
319 }else{
321 if(DEBUG_FAI_FUNC) {
322 echo "<b>Saving main object</b>".$Current_DN;
323 print_a($objectAttrs);
324 }
325 save_FAI_object($Current_DN,$objectAttrs);
327 if(isset($objectAttrs['FAIstate'])){
328 $objectAttrs['FAIstate'] .= "|removed";
329 }else{
330 $objectAttrs['FAIstate'] = "|removed";
331 }
333 foreach($r as $key ){
334 if(DEBUG_FAI_FUNC) {
335 echo "<b>Create an empty placeholder in follwing release</b> ".$key;
336 print_a($objectAttrs);
337 }
338 save_FAI_object($key,$objectAttrs);
339 }
340 }
341 }else{
343 /* check if we must patch the follwing release */
344 if(!empty($r)){
345 foreach($r as $key ){
346 if(DEBUG_FAI_FUNC) {
347 echo "<b>Copy current objects original attributes to next release</b> ".$key;
348 print_a($parent_attrs);
349 }
350 save_FAI_object($key,$parent_attrs);
351 }
352 }
354 if(DEBUG_FAI_FUNC) {
355 echo "<b>Saving current object</b>".$parent_obj;
356 print_a($objectAttrs);
357 }
358 save_FAI_object($parent_obj,$objectAttrs);
360 if(($parent_obj != $Current_DN)){
361 print_red(sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN));
362 }
363 }
364 }
365 }
366 $_SESSION['FAI_objects_to_save'] = array();
367 }
370 /* this function will remove all unused (deleted) objects,
371 that have no parent object */
372 function clean_up_releases($Current_DN)
373 {
374 global $config;
375 $ldap = $config->get_ldap_link();
376 $ldap->cd($config->current['BASE']);
378 /* Collect some basic informations and initialize some variables */
379 $base_release = get_release_dn($Current_DN);
380 $previous_releases = array_reverse(get_previous_releases_of_this_release($base_release,true));
381 $Kill = array();
382 $Skip = array();
384 /* We must also include the given release dn */
385 $previous_releases[] = $base_release;
387 /* Walk through all releases */
388 foreach($previous_releases as $release){
390 /* Get fai departments */
391 $deps_to_search = get_FAI_departments($release);
393 /* For every single department (ou=hoos,ou ..) */
394 foreach($deps_to_search as $fai_base){
396 /* Ldap search for fai classes specified in this release */
397 $ldap->cd($fai_base);
398 $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
400 /* check the returned objects, and add/replace them in our return variable */
401 while($attr = $ldap->fetch()){
403 $buffer = array();
404 $name = str_ireplace($release,"",$attr['dn']);
406 if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
408 /* Check if this object is required somehow */
409 if(!isset($Skip[$name])){
410 $Kill[$attr['dn']] = $attr['dn'];
411 }
412 }else{
414 /* This object is required (not removed), so do not
415 delete any following sub releases of this object */
416 $Skip[$name] = $attr['dn'];
417 }
418 }
419 }
420 }
421 return($Kill);
422 }
425 function prepare_ldap_fetch_to_be_saved($attrs)
426 {
427 foreach($attrs as $key => $value){
428 if(is_numeric($key) || ($key == "count") || ($key == "dn")){
429 unset($attrs[$key]);
430 }
431 if(is_array($value) && isset($value['count'])){
432 unset($attrs[$key]['count']);
433 }
434 }
435 return($attrs);
436 }
439 function save_FAI_object($dn,$attrs)
440 {
441 global $config;
442 $ldap = $config->get_ldap_link();
443 $ldap->cd($config->current['BASE']);
444 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
445 $ldap->cd($dn);
447 $ldap->cat($dn,array("dn"));
448 if($ldap->count()){
450 if(!isset($attrs['FAIstate'])){
451 $attrs['FAIstate'] = array();
452 }
454 $ldap->modify($attrs);
455 }else{
456 $ldap->add($attrs);
457 }
458 show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
459 }
462 function get_following_releases_that_inherit_this_object($dn)
463 {
464 global $config;
465 $ldap = $config->get_ldap_link();
466 $ldap->cd($config->current['BASE']);
468 $ret = array();
470 /* Get base release */
471 $base_release = get_release_dn($dn);
473 /* Get previous release dns */
474 $sub_releases = get_sub_releases_of_this_release($base_release);
476 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
477 $dn_suffix = str_ireplace($base_release,"",$dn);
479 /* Check if given object also exists whitin one of these releases */
480 foreach($sub_releases as $p_release => $name){
482 $check_dn = $dn_suffix.$p_release;
484 $ldap->cat($check_dn,array("dn","objectClass"));
486 if($ldap->count()){
487 //return($ret);
488 }else{
489 $ret[$check_dn]=$check_dn;
490 }
491 }
492 return($ret);
493 }
496 /* Get previous version of the object dn */
497 function get_parent_release_object($dn,$include_myself=true)
498 {
499 global $config;
500 $ldap = $config->get_ldap_link();
501 $ldap->cd($config->current['BASE']);
502 $previous_releases= array();
504 /* Get base release */
505 $base_release = get_release_dn($dn);
506 if($include_myself){
507 $previous_releases[] = $base_release;
508 }
510 /* Get previous release dns */
511 $tmp = get_previous_releases_of_this_release($base_release,true);
512 foreach($tmp as $release){
513 $previous_releases[] = $release;
514 }
516 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
517 $dn_suffix = str_ireplace($base_release,"",$dn);
519 /* Check if given object also exists whitin one of these releases */
520 foreach($previous_releases as $p_release){
521 $check_dn = $dn_suffix.$p_release;
522 $ldap->cat($check_dn,array("dn","objectClass"));
524 if($ldap->count()){
525 return($check_dn);
526 }
527 }
528 return("");
529 }
532 /* return release names of all parent releases */
533 function get_previous_releases_of_this_release($dn,$flat)
534 {
535 global $config;
536 $ldap = $config->get_ldap_link();
537 $ldap->cd($config->current['BASE']);
538 $ret = array();
540 /* Explode dns into pieces, to be able to build parent dns */
541 $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$dn),0);
542 if(!is_array($dns_to_check)){
543 return;
544 }
546 /* Unset first entry which represents the given dn */
547 unset($dns_to_check['count']);
548 unset($dns_to_check[key($dns_to_check)]);
550 /* Create dns addresses and check if this dn is a release dn */
551 $id = 0;
552 while(count($dns_to_check)){
554 /* build parent dn */
555 $new_dn = "";
556 foreach($dns_to_check as $part){
557 $new_dn .= $part.",";
558 }
559 $new_dn .= $config->current['BASE'];
561 /* check if this dn is a release */
562 if(is_release_department($new_dn)){
563 if($flat){
564 $ret[$id] = $new_dn;
565 }else{
566 $ret = array($new_dn=>$ret);
567 }
568 $id ++;
569 }else{
570 return($ret);
571 }
572 reset($dns_to_check);
573 unset($dns_to_check[key($dns_to_check)]);
574 }
575 return($ret);
576 }
579 /* This function returns all sub release names, recursivly */
580 function get_sub_releases_of_this_release($dn,$flat = false)
581 {
582 global $config;
583 $res = array();
584 $ldap = $config->get_ldap_link();
585 $ldap->cd($config->current['BASE']);
586 $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
587 while($attr = $ldap->fetch()){
589 /* Append department name */
590 if($flat){
591 $res[$attr['dn']] = $attr['ou'][0];
592 }else{
593 $res[$attr['dn']] = array();
594 }
596 /* Get sub release departments of this department */
597 if(in_array("FAIbranch",$attr['objectClass'])) {
598 if($flat){
599 $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
600 foreach($tmp as $dn => $value){
601 $res[$dn]=$value;
602 }
603 }else{
604 $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
605 }
606 }
607 }
608 return($res);
609 }
612 /* Check if the given department is a release department */
613 function is_release_department($dn)
614 {
615 global $config;
616 $ldap = $config->get_ldap_link();
617 $ldap->cd($config->current['BASE']);
618 $ldap->cat($dn,array("objectClass","ou"));
620 /* Check objectClasses and name to check if this is a release department */
621 if($ldap->count()){
622 $attrs = $ldap->fetch();
624 $ou = "";
625 if(isset($attrs['ou'][0])){
626 $ou = $attrs['ou'][0];
627 }
629 if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
630 return($attrs['dn']);
631 }
632 }
633 return(false);
634 }
637 /* This function returns the dn of the object release */
638 function get_release_dn($Current_DN)
639 {
640 global $config;
641 $ldap = $config->get_ldap_link();
642 $ldap->cd($config->current['BASE']);
644 /* Split dn into pices */
645 $dns_to_check = ldap_explode_dn(str_ireplace(",".$config->current['BASE'],"",$Current_DN),0);
646 if(!is_array($dns_to_check)){
647 return;
648 }
650 /* Use dn pieces, to create sub dns like
651 ou=test,ou=1,ou=0...
652 ou=1,ou=0...
653 ou=0...
654 To check which dn is our release container.
655 */
656 unset($dns_to_check['count']);
657 while(count($dns_to_check)){
659 /* Create dn */
660 $new_dn = "";
661 foreach($dns_to_check as $part){
662 $new_dn .= $part.",";
663 }
664 $new_dn .= $config->current['BASE'];
666 /* Check if this dn is a release dn */
667 if(is_release_department($new_dn)){
668 return($new_dn);
669 }
671 /* Remove first element of dn pieces */
672 reset($dns_to_check);
673 unset($dns_to_check[key($dns_to_check)]);
674 }
675 return("");
676 }
678 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
679 ?>