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);
42 /* check the returned objects, and add/replace them in our return variable */
43 foreach($res_tmp as $attr){
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);
100 }
103 /* Return all releases within the given base */
104 function get_all_releases_from_base($dn,$appendedName=false)
105 {
106 global $config;
108 if(!preg_match("/".normalizePreg(get_ou('faiou'))."/",$dn)){
109 $base = get_ou('faiou').$dn;
110 }else{
111 $base = $dn;
112 }
113 $res = array();
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);
126 }
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)
131 {
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 $FAI_objects_to_save = session::get('FAI_objects_to_save') ;
155 $FAI_objects_to_save[$Current_DN] = $addObj;
156 session::set('FAI_objects_to_save',$FAI_objects_to_save);
157 }
160 /* Detect differences in attribute arrays */
161 function array_diff_FAI($ar1,$ar2)
162 {
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)) || arr_diff($ar1,$ar2)){
189 return(true);
190 }else{
191 return(false);
192 }
193 }
196 function arr_diff($ar1,$ar2)
197 {
198 foreach($ar1 as $ak1 => $av1){
199 if(!isset($ar2[$ak1]) || (!($av1 === $ar2[$ak1]))){
200 return(true);
201 }elseif(is_array($av1)){
202 return(arr_diff($av1,$ar2[$ak1]));
203 }
204 }
205 return(FALSE);
206 }
211 /* check which objects must be saved, and save them */
212 function save_release_changes_now()
213 {
214 /* Variable init*/
215 $to_save = array();
217 /* check which objects must be saved */
218 $FAI_objects_to_save = session::get('FAI_objects_to_save');
219 foreach($FAI_objects_to_save as $Current_DN => $object){
220 if($object['diff']){
221 $sub_name = $Current_DN;
222 while(isset($FAI_objects_to_save[$sub_name])){
223 $to_save[strlen($sub_name)][$sub_name] = $FAI_objects_to_save[$sub_name];
224 unset($FAI_objects_to_save[$sub_name]);
225 $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
226 }
227 }
228 }
229 session::set('FAI_objects_to_save',$FAI_objects_to_save);
231 /* Sort list of objects that must be saved, and ensure that
232 container objects are safed, before their childs are saved */
233 ksort($to_save);
234 $tmp = array();
235 foreach($to_save as $SubObjects){
236 foreach($SubObjects as $object){
237 $tmp[] = $object;
238 }
239 }
240 $to_save = $tmp;
242 /* Save objects and manage the correct release behavior*/
243 foreach($to_save as $save){
245 $Current_DN = $save['Current_DN'];
246 $removed = $save['removed'];
247 $objectAttrs= $save['objectAttrs'];
249 /* Get ldap object */
250 global $config;
251 $ldap = $config->get_ldap_link();
252 $ldap->cd($config->current['BASE']);
254 /* Get some basic informations */
255 $base_release = get_release_dn($Current_DN);
256 $sub_releases = get_sub_releases_of_this_release($base_release,true);
257 $parent_obj = get_parent_release_object($Current_DN);
258 $following_releases = get_sub_releases_of_this_release($base_release,true);
260 /* Check if given dn exists or if is a new entry */
261 $ldap->cat($Current_DN);
262 if(!$ldap->count()){
263 $is_new = true;
264 }else{
265 $is_new = false;
266 }
268 /* if parameter removed is true, we have to add FAIstate to the current attrs
269 FAIstate should end with ...|removed after this operation */
270 if($removed ){
271 $ldap->cat($Current_DN);
273 /* Get current object, because we must add the FAIstate ...|removed */
274 if((!$ldap->count()) && !empty($parent_obj)){
275 $ldap->cat($parent_obj);
276 }
278 /* Check if we have found a suiteable object */
279 if(!$ldap->count()){
280 echo "Error can't remove this object ".$Current_DN;
281 return;
282 }else{
284 /* Set FAIstate to current objectAttrs */
285 $objectAttrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
286 if(isset($objectAttrs['FAIstate'][0])){
287 if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
288 $objectAttrs['FAIstate'][0] .= "|removed";
289 }
290 }else{
291 $objectAttrs['FAIstate'][0] = "|removed";
292 }
293 }
294 }
296 /* Check if this a leaf release or not */
297 if(count($following_releases) == 0 ){
299 /* This is a leaf object. It isn't inherited by any other object */
300 if(DEBUG_FAI_FUNC) {
301 echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
302 print_a($objectAttrs);
303 }
304 save_FAI_object($Current_DN,$objectAttrs);
305 }else{
307 /* This object is inherited by some sub releases */
309 /* Get all releases, that inherit this object */
310 $r = get_following_releases_that_inherit_this_object($Current_DN);
312 /* Get parent object */
313 $ldap->cat($parent_obj);
314 $parent_attrs = prepare_ldap_fetch_to_be_saved($ldap->fetch());
316 /* New objects require special handling */
317 if($is_new){
319 /* check if there is already an entry named like this,
320 in one of our parent releases */
321 if(!empty($parent_obj)){
322 if(DEBUG_FAI_FUNC) {
323 echo "There is already an entry named like this.</b><br>";
325 echo "<b>Saving main object</b>".$Current_DN;
326 print_a($objectAttrs);
327 }
328 save_FAI_object($Current_DN,$objectAttrs);
330 foreach($r as $key){
331 if(DEBUG_FAI_FUNC) {
332 echo "<b>Saving parent to following release</b> ".$key;
333 print_a($parent_attrs);
334 }
335 save_FAI_object($key,$parent_attrs);
336 }
337 }else{
339 if(DEBUG_FAI_FUNC) {
340 echo "<b>Saving main object</b>".$Current_DN;
341 print_a($objectAttrs);
342 }
343 save_FAI_object($Current_DN,$objectAttrs);
345 if(isset($objectAttrs['FAIstate'])){
346 $objectAttrs['FAIstate'] .= "|removed";
347 }else{
348 $objectAttrs['FAIstate'] = "|removed";
349 }
351 foreach($r as $key ){
352 if(DEBUG_FAI_FUNC) {
353 echo "<b>Create an empty placeholder in follwing release</b> ".$key;
354 print_a($objectAttrs);
355 }
356 save_FAI_object($key,$objectAttrs);
357 }
358 }
359 }else{
361 /* check if we must patch the follwing release */
362 if(!empty($r)){
363 foreach($r as $key ){
364 if(DEBUG_FAI_FUNC) {
365 echo "<b>Copy current objects original attributes to next release</b> ".$key;
366 print_a($parent_attrs);
367 }
368 save_FAI_object($key,$parent_attrs);
369 }
370 }
372 if(DEBUG_FAI_FUNC) {
373 echo "<b>Saving current object</b>".$parent_obj;
374 print_a($objectAttrs);
375 }
376 save_FAI_object($parent_obj,$objectAttrs);
378 if(($parent_obj != $Current_DN)){
379 msg_dialog::display(_("Error"), sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN), ERROR_DIALOG);
380 }
381 }
382 }
383 }
384 session::set('FAI_objects_to_save',array());
385 }
388 /* this function will remove all unused (deleted) objects,
389 that have no parent object */
390 function clean_up_releases($Current_DN)
391 {
392 global $config;
393 $ldap = $config->get_ldap_link();
394 $ldap->cd($config->current['BASE']);
396 /* Collect some basic informations and initialize some variables */
397 $base_release = get_release_dn($Current_DN);
398 $previous_releases = array_reverse(get_previous_releases_of_this_release($base_release,true));
399 $Kill = array();
400 $Skip = array();
402 /* We must also include the given release dn */
403 $previous_releases[] = $base_release;
405 /* Walk through all releases */
406 foreach($previous_releases as $release){
408 /* Get fai departments */
409 $deps_to_search = get_FAI_departments($release);
411 /* For every single department (ou=hoos,ou ..) */
412 foreach($deps_to_search as $fai_base){
414 /* Ldap search for fai classes specified in this release */
415 $ldap->cd($fai_base);
416 $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
418 /* check the returned objects, and add/replace them in our return variable */
419 while($attr = $ldap->fetch()){
421 $buffer = array();
422 # $name = str_ireplace($release,"",$attr['dn']);
423 $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
425 if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
427 /* Check if this object is required somehow */
428 if(!isset($Skip[$name])){
429 $Kill[$attr['dn']] = $attr['dn'];
430 }
431 }else{
433 /* This object is required (not removed), so do not
434 delete any following sub releases of this object */
435 $Skip[$name] = $attr['dn'];
436 }
437 }
438 }
439 }
440 return($Kill);
441 }
444 /* Remove numeric index and 'count' from ldap->fetch result */
445 function prepare_ldap_fetch_to_be_saved($attrs)
446 {
447 foreach($attrs as $key => $value){
448 if(is_numeric($key) || ($key == "count") || ($key == "dn")){
449 unset($attrs[$key]);
450 }
451 if(is_array($value) && isset($value['count'])){
452 unset($attrs[$key]['count']);
453 }
454 }
455 return($attrs);
456 }
459 /* Save given attrs to specified dn*/
460 function save_FAI_object($dn,$attrs)
461 {
462 global $config;
463 $ldap = $config->get_ldap_link();
464 $ldap->cd($config->current['BASE']);
465 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
466 $ldap->cd($dn);
468 $ldap->cat($dn,array("dn"));
469 if($ldap->count()){
471 /* Remove FAIstate*/
472 if(!isset($attrs['FAIstate'])){
473 $attrs['FAIstate'] = array();
474 }
476 $ldap->modify($attrs);
477 }else{
479 /* Unset description if empty */
480 if(empty($attrs['description'])){
481 unset($attrs['description']);
482 }
484 $ldap->add($attrs);
485 }
486 show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
487 }
490 /* Return FAIstate freeze branch or "" for specified release department */
491 function get_release_tag($dn)
492 {
493 global $config;
494 $ldap = $config->get_ldap_link();
495 $ldap->cd($dn);
496 $ldap->cat($dn,array("FAIstate"));
498 if($ldap->count()){
500 $attr = $ldap->fetch();
501 if(isset($attr['FAIstate'][0])){
502 if(preg_match("/freeze/",$attr['FAIstate'][0])){
503 return("freeze");
504 }elseif(preg_match("/branch/",$attr['FAIstate'][0])){
505 return("branch");
506 }
507 }
508 }
509 return("");
510 }
513 function get_following_releases_that_inherit_this_object($dn)
514 {
515 global $config;
516 $ldap = $config->get_ldap_link();
517 $ldap->cd($config->current['BASE']);
519 $ret = array();
521 /* Get base release */
522 $base_release = get_release_dn($dn);
524 /* Get previous release dns */
525 $sub_releases = get_sub_releases_of_this_release($base_release);
527 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
528 # $dn_suffix = str_ireplace($base_release,"",$dn);
529 $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
531 /* Check if given object also exists whitin one of these releases */
532 foreach($sub_releases as $p_release => $name){
534 $check_dn = $dn_suffix.$p_release;
536 $ldap->cat($check_dn,array("dn","objectClass"));
538 if($ldap->count()){
539 //return($ret);
540 }else{
541 $ret[$check_dn]=$check_dn;
542 }
543 }
544 return($ret);
545 }
548 /* Get previous version of the object dn */
549 function get_parent_release_object($dn,$include_myself=true)
550 {
551 global $config;
552 $ldap = $config->get_ldap_link();
553 $ldap->cd($config->current['BASE']);
554 $previous_releases= array();
556 /* Get base release */
557 $base_release = get_release_dn($dn);
558 if($include_myself){
559 $previous_releases[] = $base_release;
560 }
562 /* Get previous release dns */
563 $tmp = get_previous_releases_of_this_release($base_release,true);
564 foreach($tmp as $release){
565 $previous_releases[] = $release;
566 }
568 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
569 # $dn_suffix = str_ireplace($base_release,"",$dn);
570 $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
572 /* Check if given object also exists whitin one of these releases */
573 foreach($previous_releases as $p_release){
574 $check_dn = $dn_suffix.$p_release;
575 $ldap->cat($check_dn,array("dn","objectClass"));
577 if($ldap->count()){
578 return($check_dn);
579 }
580 }
581 return("");
582 }
585 /* return release names of all parent releases */
586 function get_previous_releases_of_this_release($dn,$flat)
587 {
588 global $config;
589 $ldap = $config->get_ldap_link();
590 $ldap->cd($config->current['BASE']);
591 $ret = array();
593 /* Explode dns into pieces, to be able to build parent dns */
594 $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$dn));
596 if(!is_array($dns_to_check)){
597 return;
598 }
600 /* Unset first entry which represents the given dn */
601 unset($dns_to_check['count']);
602 unset($dns_to_check[key($dns_to_check)]);
604 /* Create dns addresses and check if this dn is a release dn */
605 $id = 0;
606 while(count($dns_to_check)){
608 /* build parent dn */
609 $new_dn = "";
610 foreach($dns_to_check as $part){
611 $new_dn .= $part.",";
612 }
613 $new_dn .= $config->current['BASE'];
615 /* check if this dn is a release */
616 if(is_release_department($new_dn)){
617 if($flat){
618 $ret[$id] = $new_dn;
619 }else{
620 $ret = array($new_dn=>$ret);
621 }
622 $id ++;
623 }else{
624 return($ret);
625 }
626 reset($dns_to_check);
627 unset($dns_to_check[key($dns_to_check)]);
628 }
629 return($ret);
630 }
633 /* This function returns all sub release names, recursivly */
634 function get_sub_releases_of_this_release($dn,$flat = false)
635 {
636 global $config;
637 $res = array();
638 $ldap = $config->get_ldap_link();
639 $ldap->cd($config->current['BASE']);
640 $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
641 while($attr = $ldap->fetch()){
643 /* Append department name */
644 if($flat){
645 $res[$attr['dn']] = $attr['ou'][0];
646 }else{
647 $res[$attr['dn']] = array();
648 }
650 /* Get sub release departments of this department */
651 if(in_array("FAIbranch",$attr['objectClass'])) {
652 if($flat){
653 $tmp = get_sub_releases_of_this_release($attr['dn'],$flat);
654 foreach($tmp as $dn => $value){
655 $res[$dn]=$value;
656 }
657 }else{
658 $res[$attr['dn']] = get_sub_releases_of_this_release($attr['dn']);
659 }
660 }
661 }
662 return($res);
663 }
666 /* Check if the given department is a release department */
667 function is_release_department($dn)
668 {
669 global $config;
670 $ldap = $config->get_ldap_link();
671 $ldap->cd($config->current['BASE']);
672 $ldap->cat($dn,array("objectClass","ou"));
674 /* Check objectClasses and name to check if this is a release department */
675 if($ldap->count()){
676 $attrs = $ldap->fetch();
678 $ou = "";
679 if(isset($attrs['ou'][0])){
680 $ou = $attrs['ou'][0];
681 }
683 if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
684 return($attrs['dn']);
685 }
686 }
687 return(false);
688 }
691 /* This function returns the dn of the object release */
692 function get_release_dn($Current_DN)
693 {
694 global $config;
695 $ldap = $config->get_ldap_link();
696 $ldap->cd($config->current['BASE']);
698 /* Split dn into pices */
699 $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$Current_DN));
701 if(!is_array($dns_to_check)){
702 return;
703 }
705 /* Use dn pieces, to create sub dns like
706 ou=test,ou=1,ou=0...
707 ou=1,ou=0...
708 ou=0...
709 To check which dn is our release container.
710 */
711 unset($dns_to_check['count']);
712 while(count($dns_to_check)){
714 /* Create dn */
715 $new_dn = "";
716 foreach($dns_to_check as $part){
717 $new_dn .= $part.",";
718 }
719 $new_dn .= $config->current['BASE'];
721 /* Check if this dn is a release dn */
722 if(is_release_department($new_dn)){
723 return($new_dn);
724 }
726 /* Remove first element of dn pieces */
727 reset($dns_to_check);
728 unset($dns_to_check[key($dns_to_check)]);
729 }
730 return("");
731 }
733 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
734 ?>