1 <?php
3 define("DEBUG_FAI_FUNC",FALSE);
6 class FAI
7 {
9 /* TEST PHASE .... */
11 /* Returns all object for the given release.
12 This function resolves the releases
13 from base up to the given dn.
14 */
15 static function get_all_objects_for_given_base($Current_DN,$filter,$detailed = false)
16 {
17 global $config;
18 $ldap = $config->get_ldap_link();
19 $ldap->cd($config->current['BASE']);
20 $res = array();
21 $tmp = array();
23 if(!FAI::is_release_department($Current_DN)) {
24 return($res);
25 }
27 /* Collect some basic informations and initialize some variables */
28 $base_release = FAI::get_release_dn($Current_DN);
29 $previous_releases = array_reverse(FAI:: get_previous_releases_of_this_release($base_release,true));
31 /* We must also include the given release dn */
32 $previous_releases[] = $base_release;
34 /* Walk through all releases */
35 foreach($previous_releases as $release){
37 /* Get fai departments */
38 $deps_to_search = FAI::get_FAI_departments($release);
40 /* For every single department (ou=hoos,ou ..) */
41 foreach($deps_to_search as $fai_base){
43 /* Ldap search for fai classes specified in this release */
44 $attributes = array("dn","objectClass","FAIstate","cn");
45 $res_tmp = get_list($filter,"fai",$fai_base,$attributes,GL_SUBSEARCH | GL_SIZELIMIT);
47 /* check the returned objects, and add/replace them in our return variable */
48 foreach($res_tmp as $attr){
50 $buffer = array();
51 $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
53 if(isset($attr['FAIstate'][0])){
54 if(preg_match("/removed$/",$attr['FAIstate'][0])){
55 if(isset($res[$name])){
56 unset($res[$name]);
57 }
58 continue;
59 }
60 }
62 /* In detailed mode are some additonal informations visible */
63 if($detailed){
65 /* Create list of parents */
66 if(isset($res[$name])){
67 $buffer = $res[$name];
68 $buffer['parents'][] = $res[$name]['dn'];
69 }else{
70 $buffer['parents'] = array();
71 }
73 /* Append objectClass to resulsts */
74 foreach($attributes as $val){
75 if(isset($attr[$val])){
76 $buffer[$val] = $attr[$val];
77 }
78 }
79 unset($buffer['objectClass']['count']);
80 }
82 /* Add this object to our list */
83 $buffer['dn'] = $attr['dn'];
84 $res[$name] = $buffer;
85 }
86 }
87 }
88 return($res);
89 }
92 /* Return all relevant FAI departments */
93 static function get_FAI_departments($suffix = "")
94 {
95 $arr = array("hooks","scripts","disk","packages","profiles","templates","variables");
96 $tmp = array();
97 if(preg_match("/^,/",$suffix)){
98 $suffix = preg_replace("/^,/","",$suffix);
99 }
100 foreach($arr as $name){
101 if(empty($suffix)){
102 $tmp[$name] = "ou=".$name;
103 }else{
104 $tmp[$name] = "ou=".$name.",".$suffix;
105 }
106 }
107 return($tmp);
108 }
111 /* Return all releases within the given base */
112 static function get_all_releases_from_base($dn,$appendedName=false)
113 {
114 global $config;
116 if(!preg_match("/".normalizePreg(get_ou('faiou'))."/",$dn)){
117 $base = get_ou('faiou').$dn;
118 }else{
119 $base = $dn;
120 }
121 $res = array();
123 $ldap = $config->get_ldap_link();
124 $ldap->cd($base);
125 $ldap->search("(objectClass=FAIbranch)",array("ou","dn"));
126 while($attrs = $ldap->fetch()){
127 if($appendedName){
128 $res[$attrs['dn']] = convert_department_dn(preg_replace("/,".normalizePreg(get_ou('faiou')).".*$/","",$attrs['dn']));
129 }else{
130 $res[$attrs['dn']] = $attrs['ou'][0];
131 }
132 }
133 return($res);
134 }
137 /* Add this object to list of objects, that must be checked for release saving */
138 static function prepare_to_save_FAI_object($Current_DN,$objectAttrs,$removed = false)
139 {
140 /* Get ldap object */
141 global $config;
142 $addObj['Current_DN'] = $Current_DN;
143 $addObj['objectAttrs']= $objectAttrs;
144 $addObj['removed'] = $removed;
145 $addObj['diff'] = TRUE;
147 if(!$removed){
148 $ldap = $config->get_ldap_link();
149 $ldap->cd($config->current['BASE']);
151 /* Get some basic informations */
152 $parent_obj = FAI::get_parent_release_object($Current_DN);
153 if(!empty($parent_obj)){
154 $ldap->cat($parent_obj,array("*"));
155 $attrs = FAI:: prepare_ldap_fetch_to_be_saved($ldap->fetch());
157 if(!FAI::array_diff_FAI( $attrs,$objectAttrs)){
158 $addObj['diff'] = FALSE;
159 }
160 }
161 }
162 $FAI_objects_to_save = session::get('FAI_objects_to_save') ;
163 $FAI_objects_to_save[$Current_DN] = $addObj;
164 session::set('FAI_objects_to_save',$FAI_objects_to_save);
165 }
168 /* Detect differences in attribute arrays */
169 static function array_diff_FAI($ar1,$ar2)
170 {
172 if((!isset($ar1['description'])) || (isset($ar1['description']) && (count($ar1['description']) == 0))){
173 $ar1['description'] = "";
174 }
175 if((!isset($ar2['description'])) || (isset($ar2['description']) && (count($ar2['description']) == 0))){
176 $ar2['description'] = "";
177 }
179 if(count($ar1) != count($ar2)) {
180 return (true);
181 }
183 foreach($ar1 as $key1 => $val1){
185 if((is_array($val1)) && (count($val1)==1)){
186 $ar1[$key1] = $val1[0];
187 }
189 if((is_array($ar2[$key1])) && (count($ar2[$key1])==1)){
190 $val1 = $val1[0];
191 $ar2[$key1] = $ar2[$key1][0];
192 }
193 }
194 ksort($ar1);
195 ksort($ar2);
196 if(count( array_diff($ar1,$ar2)) || FAI::arr_diff($ar1,$ar2)){
197 return(true);
198 }else{
199 return(false);
200 }
201 }
204 static function arr_diff($ar1,$ar2)
205 {
206 foreach($ar1 as $ak1 => $av1){
207 if(!isset($ar2[$ak1]) || (!($av1 === $ar2[$ak1]))){
208 return(true);
209 }elseif(is_array($av1)){
210 return(FAI::arr_diff($av1,$ar2[$ak1]));
211 }
212 }
213 return(FALSE);
214 }
219 /* check which objects must be saved, and save them */
220 static function save_release_changes_now()
221 {
222 /* Variable init*/
223 $to_save = array();
225 /* check which objects must be saved */
226 $FAI_objects_to_save = session::get('FAI_objects_to_save');
227 foreach($FAI_objects_to_save as $Current_DN => $object){
228 if($object['diff']){
229 $sub_name = $Current_DN;
230 while(isset($FAI_objects_to_save[$sub_name])){
231 $to_save[strlen($sub_name)][$sub_name] = $FAI_objects_to_save[$sub_name];
232 unset($FAI_objects_to_save[$sub_name]);
233 $sub_name = preg_replace('/^[^,]+,/', '', $sub_name);
234 }
235 }
236 }
237 session::set('FAI_objects_to_save',$FAI_objects_to_save);
239 /* Sort list of objects that must be saved, and ensure that
240 container objects are safed, before their childs are saved */
241 ksort($to_save);
242 $tmp = array();
243 foreach($to_save as $SubObjects){
244 foreach($SubObjects as $object){
245 $tmp[] = $object;
246 }
247 }
248 $to_save = $tmp;
250 /* Save objects and manage the correct release behavior*/
251 foreach($to_save as $save){
253 $Current_DN = $save['Current_DN'];
254 $removed = $save['removed'];
255 $objectAttrs= $save['objectAttrs'];
257 /* Get ldap object */
258 global $config;
259 $ldap = $config->get_ldap_link();
260 $ldap->cd($config->current['BASE']);
262 /* Get some basic informations */
263 $base_release = FAI::get_release_dn($Current_DN);
264 $sub_releases = FAI:: get_sub_releases_of_this_release($base_release,true);
265 $parent_obj = FAI::get_parent_release_object($Current_DN);
266 $following_releases = FAI:: get_sub_releases_of_this_release($base_release,true);
268 /* Check if given dn exists or if is a new entry */
269 $ldap->cat($Current_DN);
270 if(!$ldap->count()){
271 $is_new = true;
272 }else{
273 $is_new = false;
274 }
276 /* if parameter removed is true, we have to add FAIstate to the current attrs
277 FAIstate should end with ...|removed after this operation */
278 if($removed ){
279 $ldap->cat($Current_DN);
281 /* Get current object, because we must add the FAIstate ...|removed */
282 if((!$ldap->count()) && !empty($parent_obj)){
283 $ldap->cat($parent_obj);
284 }
286 /* Check if we have found a suiteable object */
287 if(!$ldap->count()){
288 echo "Error can't remove this object ".$Current_DN;
289 return;
290 }else{
292 /* Set FAIstate to current objectAttrs */
293 $objectAttrs = FAI:: prepare_ldap_fetch_to_be_saved($ldap->fetch());
294 if(isset($objectAttrs['FAIstate'][0])){
295 if(!preg_match("/removed$/",$objectAttrs['FAIstate'][0])){
296 $objectAttrs['FAIstate'][0] .= "|removed";
297 }
298 }else{
299 $objectAttrs['FAIstate'][0] = "|removed";
300 }
301 }
302 }
304 /* Check if this a leaf release or not */
305 if(count($following_releases) == 0 ){
307 /* This is a leaf object. It isn't inherited by any other object */
308 if(DEBUG_FAI_FUNC) {
309 echo "<b>Saving directly, is a leaf object</b><br> ".$Current_DN;
310 print_a($objectAttrs);
311 }
312 FAI::save_FAI_object($Current_DN,$objectAttrs);
313 }else{
315 /* This object is inherited by some sub releases */
317 /* Get all releases, that inherit this object */
318 $r = get_following_releases_that_inherit_this_object($Current_DN);
320 /* Get parent object */
321 $ldap->cat($parent_obj);
322 $parent_attrs = FAI:: prepare_ldap_fetch_to_be_saved($ldap->fetch());
324 /* New objects require special handling */
325 if($is_new){
327 /* check if there is already an entry named like this,
328 in one of our parent releases */
329 if(!empty($parent_obj)){
330 if(DEBUG_FAI_FUNC) {
331 echo "There is already an entry named like this.</b><br>";
333 echo "<b>Saving main object</b>".$Current_DN;
334 print_a($objectAttrs);
335 }
336 FAI::save_FAI_object($Current_DN,$objectAttrs);
338 foreach($r as $key){
339 if(DEBUG_FAI_FUNC) {
340 echo "<b>Saving parent to following release</b> ".$key;
341 print_a($parent_attrs);
342 }
343 FAI::save_FAI_object($key,$parent_attrs);
344 }
345 }else{
347 if(DEBUG_FAI_FUNC) {
348 echo "<b>Saving main object</b>".$Current_DN;
349 print_a($objectAttrs);
350 }
351 FAI::save_FAI_object($Current_DN,$objectAttrs);
353 if(isset($objectAttrs['FAIstate'])){
354 $objectAttrs['FAIstate'] .= "|removed";
355 }else{
356 $objectAttrs['FAIstate'] = "|removed";
357 }
359 foreach($r as $key ){
360 if(DEBUG_FAI_FUNC) {
361 echo "<b>Create an empty placeholder in follwing release</b> ".$key;
362 print_a($objectAttrs);
363 }
364 FAI::save_FAI_object($key,$objectAttrs);
365 }
366 }
367 }else{
369 /* check if we must patch the follwing release */
370 if(!empty($r)){
371 foreach($r as $key ){
372 if(DEBUG_FAI_FUNC) {
373 echo "<b>Copy current objects original attributes to next release</b> ".$key;
374 print_a($parent_attrs);
375 }
376 FAI::save_FAI_object($key,$parent_attrs);
377 }
378 }
380 if(DEBUG_FAI_FUNC) {
381 echo "<b>Saving current object</b>".$parent_obj;
382 print_a($objectAttrs);
383 }
384 FAI::save_FAI_object($parent_obj,$objectAttrs);
386 if(($parent_obj != $Current_DN)){
387 msg_dialog::display(_("Error"), sprintf(_("Error, following objects should be equal '%s' and '%s'"),$parent_obj,$Current_DN), ERROR_DIALOG);
388 }
389 }
390 }
391 }
392 session::set('FAI_objects_to_save',array());
393 }
396 /* this function will remove all unused (deleted) objects,
397 that have no parent object */
398 static function clean_up_releases($Current_DN)
399 {
400 global $config;
401 $ldap = $config->get_ldap_link();
402 $ldap->cd($config->current['BASE']);
404 /* Collect some basic informations and initialize some variables */
405 $base_release = FAI::get_release_dn($Current_DN);
406 $previous_releases = array_reverse(FAI:: get_previous_releases_of_this_release($base_release,true));
407 $Kill = array();
408 $Skip = array();
410 /* We must also include the given release dn */
411 $previous_releases[] = $base_release;
413 /* Walk through all releases */
414 foreach($previous_releases as $release){
416 /* Get fai departments */
417 $deps_to_search = FAI::get_FAI_departments($release);
419 /* For every single department (ou=hoos,ou ..) */
420 foreach($deps_to_search as $fai_base){
422 /* Ldap search for fai classes specified in this release */
423 $ldap->cd($fai_base);
424 $ldap->search("(objectClass=FAIclass)",array("dn","objectClass","FAIstate"));
426 /* check the returned objects, and add/replace them in our return variable */
427 while($attr = $ldap->fetch()){
429 $buffer = array();
430 # $name = str_ireplace($release,"",$attr['dn']);
431 $name = preg_replace("/".normalizePreg($release)."/i","",$attr['dn']);
433 if(isset($attr['FAIstate'][0])&&(preg_match("/removed$/",$attr['FAIstate'][0]))){
435 /* Check if this object is required somehow */
436 if(!isset($Skip[$name])){
437 $Kill[$attr['dn']] = $attr['dn'];
438 }
439 }else{
441 /* This object is required (not removed), so do not
442 delete any following sub releases of this object */
443 $Skip[$name] = $attr['dn'];
444 }
445 }
446 }
447 }
448 return($Kill);
449 }
452 /* Remove numeric index and 'count' from ldap->fetch result */
453 static function prepare_ldap_fetch_to_be_saved($attrs)
454 {
455 foreach($attrs as $key => $value){
456 if(is_numeric($key) || ($key == "count") || ($key == "dn")){
457 unset($attrs[$key]);
458 }
459 if(is_array($value) && isset($value['count'])){
460 unset($attrs[$key]['count']);
461 }
462 }
463 return($attrs);
464 }
467 /* Save given attrs to specified dn*/
468 static function save_FAI_object($dn,$attrs)
469 {
470 global $config;
471 $ldap = $config->get_ldap_link();
472 $ldap->cd($config->current['BASE']);
473 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $dn));
474 $ldap->cd($dn);
476 $ldap->cat($dn,array("dn"));
477 if($ldap->count()){
479 /* Remove FAIstate*/
480 if(!isset($attrs['FAIstate'])){
481 $attrs['FAIstate'] = array();
482 }
484 $ldap->modify($attrs);
485 }else{
487 /* Unset description if empty */
488 if(empty($attrs['description'])){
489 unset($attrs['description']);
490 }
492 $ldap->add($attrs);
493 }
494 show_ldap_error($ldap->get_error(),sprintf(_("Release management failed, can't save '%s'"),$dn));
495 }
498 /* Return FAIstate freeze branch or "" for specified release department */
499 static function get_release_tag($dn)
500 {
501 global $config;
502 $ldap = $config->get_ldap_link();
503 $ldap->cd($dn);
504 $ldap->cat($dn,array("FAIstate"));
506 if($ldap->count()){
508 $attr = $ldap->fetch();
509 if(isset($attr['FAIstate'][0])){
510 if(preg_match("/freeze/",$attr['FAIstate'][0])){
511 return("freeze");
512 }elseif(preg_match("/branch/",$attr['FAIstate'][0])){
513 return("branch");
514 }
515 }
516 }
517 return("");
518 }
521 static function get_following_releases_that_inherit_this_object($dn)
522 {
523 global $config;
524 $ldap = $config->get_ldap_link();
525 $ldap->cd($config->current['BASE']);
527 $ret = array();
529 /* Get base release */
530 $base_release = FAI::get_release_dn($dn);
532 /* Get previous release dns */
533 $sub_releases = FAI:: get_sub_releases_of_this_release($base_release);
535 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
536 # $dn_suffix = str_ireplace($base_release,"",$dn);
537 $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
539 /* Check if given object also exists whitin one of these releases */
540 foreach($sub_releases as $p_release => $name){
542 $check_dn = $dn_suffix.$p_release;
544 $ldap->cat($check_dn,array("dn","objectClass"));
546 if($ldap->count()){
547 //return($ret);
548 }else{
549 $ret[$check_dn]=$check_dn;
550 }
551 }
552 return($ret);
553 }
556 /* Get previous version of the object dn */
557 static function get_parent_release_object($dn,$include_myself=true)
558 {
559 global $config;
560 $ldap = $config->get_ldap_link();
561 $ldap->cd($config->current['BASE']);
562 $previous_releases= array();
564 /* Get base release */
565 $base_release = FAI::get_release_dn($dn);
566 if($include_myself){
567 $previous_releases[] = $base_release;
568 }
570 /* Get previous release dns */
571 $tmp = FAI:: get_previous_releases_of_this_release($base_release,true);
572 foreach($tmp as $release){
573 $previous_releases[] = $release;
574 }
576 /* Get dn suffix. Example "FAIvairableEntry=keksdose,FAIvariable=Keksregal," */
577 # $dn_suffix = str_ireplace($base_release,"",$dn);
578 $dn_suffix = preg_replace("/".normalizePreg($base_release)."/i","",$dn);
580 /* Check if given object also exists whitin one of these releases */
581 foreach($previous_releases as $p_release){
582 $check_dn = $dn_suffix.$p_release;
583 $ldap->cat($check_dn,array("dn","objectClass"));
585 if($ldap->count()){
586 return($check_dn);
587 }
588 }
589 return("");
590 }
593 /* return release names of all parent releases */
594 static function get_previous_releases_of_this_release($dn,$flat)
595 {
596 global $config;
597 $ldap = $config->get_ldap_link();
598 $ldap->cd($config->current['BASE']);
599 $ret = array();
601 /* Explode dns into pieces, to be able to build parent dns */
602 $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$dn));
604 if(!is_array($dns_to_check)){
605 return;
606 }
608 /* Unset first entry which represents the given dn */
609 unset($dns_to_check['count']);
610 unset($dns_to_check[key($dns_to_check)]);
612 /* Create dns addresses and check if this dn is a release dn */
613 $id = 0;
614 while(count($dns_to_check)){
616 /* build parent dn */
617 $new_dn = "";
618 foreach($dns_to_check as $part){
619 $new_dn .= $part.",";
620 }
621 $new_dn .= $config->current['BASE'];
623 /* check if this dn is a release */
624 if(FAI::is_release_department($new_dn)){
625 if($flat){
626 $ret[$id] = $new_dn;
627 }else{
628 $ret = array($new_dn=>$ret);
629 }
630 $id ++;
631 }else{
632 return($ret);
633 }
634 reset($dns_to_check);
635 unset($dns_to_check[key($dns_to_check)]);
636 }
637 return($ret);
638 }
641 /* This function returns all sub release names, recursivly */
642 static function get_sub_releases_of_this_release($dn,$flat = false)
643 {
644 global $config;
645 $res = array();
646 $ldap = $config->get_ldap_link();
647 $ldap->cd($config->current['BASE']);
648 $ldap->ls("(objectClass=FAIbranch)",$dn,array("objectClass","dn","ou"));
649 while($attr = $ldap->fetch()){
651 /* Append department name */
652 if($flat){
653 $res[$attr['dn']] = $attr['ou'][0];
654 }else{
655 $res[$attr['dn']] = array();
656 }
658 /* Get sub release departments of this department */
659 if(in_array("FAIbranch",$attr['objectClass'])) {
660 if($flat){
661 $tmp = FAI:: get_sub_releases_of_this_release($attr['dn'],$flat);
662 foreach($tmp as $dn => $value){
663 $res[$dn]=$value;
664 }
665 }else{
666 $res[$attr['dn']] = FAI:: get_sub_releases_of_this_release($attr['dn']);
667 }
668 }
669 }
670 return($res);
671 }
674 /* Check if the given department is a release department */
675 static function is_release_department($dn)
676 {
677 global $config;
678 $ldap = $config->get_ldap_link();
679 $ldap->cd($config->current['BASE']);
680 $ldap->cat($dn,array("objectClass","ou"));
682 /* Check objectClasses and name to check if this is a release department */
683 if($ldap->count()){
684 $attrs = $ldap->fetch();
686 $ou = "";
687 if(isset($attrs['ou'][0])){
688 $ou = $attrs['ou'][0];
689 }
691 if((in_array("FAIbranch",$attrs['objectClass'])) || ($ou == "fai")){
692 return($attrs['dn']);
693 }
694 }
695 return(false);
696 }
699 static function copy_FAI_group_releases($source_release , $destination_name, $type )
700 {
701 global $config;
702 $start = microtime(TRUE);
704 echo "<h2>".sprintf(_("Creating group application release for %s"),$destination_name)."</h2>";
706 $sub_releases = array();
707 $source_dn = "";
708 $tmp = split("\/",$source_release);
709 foreach($tmp as $part){
710 if(empty($part)){
711 continue;
712 }
713 $source_dn = "ou=".$part.",".$source_dn;
714 $sub_releases[$part] = $source_dn;
715 }
717 /* Get all groups */
718 $ldap =$config->get_ldap_link();
719 $ldap->cd($config->current['BASE']);
720 $ldap->search("(objectClass=posixGroup)",array("dn"));
721 $groups = array();
722 while($attrs = $ldap->fetch()){
723 $groups[$attrs['dn']] = $attrs;
724 }
726 /* Get all FAI releases, to be able to create missing group application releases
727 with the correct type of release (FAIstate=freeze/branch).
728 */
729 $f_releases = array();
730 $ldap->cd ($config->current['BASE']);
731 $ldap->search("(objectClass=FAIbranch)",array("ou","FAIstate"));
732 while($attrs = $ldap->fetch()){
733 foreach($sub_releases as $sub_rel){
734 if(preg_match("/^".normalizePreg($sub_rel.get_ou('faiou'))."/",$attrs['dn'])){
735 $f_releases[$sub_rel.get_ou('faiou')] = $attrs;
736 }
737 }
738 }
740 /* Get all group releases */
741 $g_releases = array();
742 foreach($groups as $dn => $data){
743 $ldap->cd($dn);
744 $ldap->search("(objectClass=FAIbranch)",array("ou","FAIstate"));
745 while($attrs = $ldap->fetch()){
746 $g_releases[$attrs['dn']] = $attrs;
747 }
748 }
750 /* Check if base releases exists.
751 If they do not exist, create them and adapt FAIstate attribute from FAI releases.
752 */
753 foreach($sub_releases as $name => $sub_rel){
755 $FAIstate = "";
756 if(isset($f_releases[$sub_rel.get_ou('faiou')]) && isset($f_releases[$sub_rel.get_ou('faiou')]['FAIstate'])){
757 $FAIstate = $f_releases[$sub_rel.get_ou('faiou')]['FAIstate'][0];
758 }
760 foreach($groups as $dn => $data){
761 if(!isset($g_releases[$sub_rel.$dn])){
762 $ldap->cd($dn);
763 $r_data = array();
764 $r_data['ou'] = $name;
765 $r_data['objectClass'] = array("top","organizationalUnit","FAIbranch");
766 if(!empty($FAIstate)) {
767 $r_data['FAIstate'] = $FAIstate;
768 }
770 $ldap->cd($sub_rel.$dn) ;
771 $ldap->add($r_data);
772 echo " <b>"._("Object").":</b> ";
773 echo sprintf(_("Adding missing group application release container %s."),substr(LDAP::fix($sub_rel.$dn),0,96))."<br>";
774 flush();
775 }
776 }
777 }
779 /* Create new release container in each group.
780 */
781 $n_data = array();
782 $n_data = array();
783 $n_data['ou'] = $destination_name;
784 $n_data['objectClass'] = array("top","organizationalUnit","FAIbranch");
785 if(!empty($type)){
786 $n_data['FAIstate'] = $type;
787 }
789 foreach($groups as $dn => $att){
790 $n_dn = "ou=".$destination_name.",".$source_dn.$dn;
791 if(!isset($g_releases[$n_dn])){
792 $ldap->cd ($n_dn);
793 $ldap->add($n_data);
794 echo " <b>"._("Object").":</b> ";
795 echo sprintf(_("Adding group application release container %s."),substr(LDAP::fix($n_dn),0,96))."<br>";
796 flush();
797 }
798 }
800 /* If the source release is empty, then create a new release by copying
801 all group application menus into a new ou=$destination_name release container.
803 If the source release is not empty.
804 We detect all releases which match the source release dn and copy the contents.
805 */
806 if(empty($source_release)){
807 $source_dns = $groups;
808 }else{
809 $source_dns = array();
810 foreach($g_releases as $dn => $data){
811 if(preg_match("/^".normalizePreg($source_dn)."/",$dn)){
812 $source_dns[$dn] = $data;
813 }
814 }
815 }
817 /* Detect all menu object we have to copy
818 */
819 $to_copy = array();
820 foreach($source_dns as $dn => $attrs){
821 $ldap->cd($dn);
822 $ldap->ls("(objectClass=gotoSubmenuEntry)(objectClass=gotoMenuEntry)",$dn,array("dn"));
823 while($attrs = $ldap->fetch()){
824 $destination = preg_replace("/".normalizePreg($dn)."$/","ou=".$destination_name.",".$dn,$attrs['dn']);
825 $to_copy[$attrs['dn']] = $destination;
826 }
827 }
829 /* At least create the menu objects object */
830 $plug = new plugin($config);
831 foreach($to_copy as $source => $destination){
832 $ldap->cat($destination);
833 if($ldap->count()){
834 echo " <b>"._("Object").":</b> ";
835 echo sprintf(_("Could not create menu entry %s. (Already exists)."),substr(LDAP::fix($destination),0,96))."<br>";
836 flush();
837 }else{
838 $plug->copy($source,$destination);
839 echo " <b>"._("Object").":</b> ";
840 echo sprintf(_("Created group application menu entry for %s."),substr(LDAP::fix($destination),0,96))."<br>";
841 flush();
842 }
843 }
844 }
847 /*! \brief Create a new FAI branch.
848 * @param $sourcedn String The source release dn
849 * @param $destinationdn String The destination dn
850 * @param $destinationName String The name of the new release
851 * @param $type String The release type (freeze/branch)
852 * @param $is_first Boolean Use to identify the first func. call when recursivly called.
853 * @param $depth Integer Current depth of recursion.
854 */
855 function copy_FAI_resource_recursive($sourcedn,$destinationdn,$destinationName,$type="branch",$is_first = true,$depth=0)
856 {
857 global $config;
858 error_reporting(E_ALL | E_STRICT);
859 $ldap = $config->get_ldap_link();
860 $basedn = $config->current['BASE'];
861 $delarray = array();
863 /* The following code will output a status string
864 * for each handled object, in a seperate iframe.
865 */
868 /* Display current action information.
869 */
870 if($is_first){
871 echo "<h2>".sprintf(_("Creating copy of %s"),"<i>".LDAP::fix($sourcedn)."</i>")."</h2>";
872 }else{
873 if(preg_match("/^ou=/",$sourcedn)){
874 echo "<h3>"._("Processing")." <i>".LDAP::fix($destinationdn)."</i></h3>";
875 }else{
876 $tmp = split(",",$sourcedn);
877 echo " <b>"._("Object").":</b> ";
878 $deststr = LDAP::fix($destinationdn);
879 if(strlen($deststr) > 96){
880 $deststr = substr($deststr,0,96)."...";
881 }
882 echo $deststr."<br>";
883 }
884 }
885 /* .. immediately display infos */
886 flush();
888 /* Check if destination entry already exists
889 */
890 $ldap->cat($destinationdn);
891 if($ldap->count()){
892 echo _("Could not create new release, the destination dn is already in use.");
893 return;
894 }else{
896 $ldap->clearResult();
898 /* Get source entry
899 * if it does not exist, abort here.
900 */
901 $ldap->cd($basedn);
902 $ldap->cat($sourcedn);
903 $attr = $ldap->fetch();
904 if((!$attr) || (count($attr)) ==0) {
905 echo _("Error while fetching source dn - aborted!");
906 return;
907 }
909 /* The current object we want to create is an department.
910 * Create the department and add the FAIbranch tag.
911 */
912 if(in_array("organizationalUnit",$attr['objectClass'])){
913 $attr['dn'] = LDAP::convert($destinationdn);
914 $ldap->cd($basedn);
915 $ldap->create_missing_trees($destinationdn);
916 $ldap->cd($destinationdn);
918 /* If is first entry, append FAIbranch to department entry */
919 if($is_first){
920 $ldap->cat($destinationdn);
921 $attr= $ldap->fetch();
922 /* Filter unneeded informations */
923 foreach($attr as $key => $value){
924 if(is_numeric($key)) unset($attr[$key]);
925 if(isset($attr[$key]['count'])){
926 if(is_array($attr[$key])){
927 unset($attr[$key]['count']);
928 }
929 }
930 }
932 unset($attr['count']);
933 unset($attr['dn']);
935 /* Add marking attribute */
936 $attr['objectClass'][] = "FAIbranch";
938 /* Add this entry */
939 $ldap->modify($attr);
940 }
941 }else{
943 /* Replicate all relevant FAI objects here.
944 * FAI objects, Apps and Mimetypes.
945 * Get all attributes as binary value, to ensure that Icon, File template aso
946 * are created correctly.
947 */
948 foreach($attr as $key => $value){
950 if(in_array($key ,array("gotoLogonScript", "gosaApplicationIcon","gotoMimeIcon"))){
951 $sr= ldap_read($ldap->cid, LDAP::fix($sourcedn), "$key=*", array($key));
952 $ei= ldap_first_entry($ldap->cid, $sr);
953 if ($tmp= @ldap_get_values_len($ldap->cid, $ei,$key)){
954 $attr[$key] = $tmp;
955 }
956 }
958 if(is_numeric($key)) unset($attr[$key]);
959 if(isset($attr[$key]['count'])){
960 if(is_array($attr[$key])){
961 unset($attr[$key]['count']);
962 }
963 }
964 }
965 unset($attr['count']);
966 unset($attr['dn']);
968 /* Add entry
969 */
970 $ldap->cd($destinationdn);
971 $ldap->cat($destinationdn);
973 $a = $ldap->fetch();
974 if(!count($a)){
975 $ldap->add($attr);
976 }
978 if($ldap->error != "Success"){
980 /* Some error occurred */
981 print "---------------------------------------------";
982 print $ldap->get_error()."<br>";
983 print $sourcedn."<br>";
984 print $destinationdn."<br>";
985 print_a( $attr);
986 exit();
987 }
988 }
989 }
991 echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
993 /* Prepare for recursive copy.
994 * Get all object within the source dn and
995 * call the recursive copy for each.
996 */
997 $ldap->ls ("(objectClass=*)",$sourcedn);
998 while ($ldap->fetch()){
999 $deldn= $ldap->getDN();
1000 $delarray[$deldn]= strlen($deldn);
1001 }
1002 asort ($delarray);
1003 reset ($delarray);
1004 $depth ++;
1005 foreach($delarray as $dn => $bla){
1006 if($dn != $destinationdn){
1007 $ldap->cd($basedn);
1008 $item = $ldap->fetch($ldap->cat($dn));
1009 if(!in_array("FAIbranch",$item['objectClass'])){
1010 FAI::copy_FAI_resource_recursive($dn,str_replace($sourcedn,$destinationdn,$dn),$destinationName,$type,false,$depth);
1011 }
1012 }
1013 }
1014 if($is_first){
1015 echo "<p class='seperator'> </p>";
1016 }
1017 }
1021 /* This function returns the dn of the object release */
1022 static function get_release_dn($Current_DN)
1023 {
1024 global $config;
1025 $ldap = $config->get_ldap_link();
1026 $ldap->cd($config->current['BASE']);
1028 /* Split dn into pices */
1029 $dns_to_check = gosa_ldap_explode_dn(preg_replace("/".normalizePreg(",".$config->current['BASE'])."/i","",$Current_DN));
1031 if(!is_array($dns_to_check)){
1032 return;
1033 }
1035 /* Use dn pieces, to create sub dns like
1036 ou=test,ou=1,ou=0...
1037 ou=1,ou=0...
1038 ou=0...
1039 To check which dn is our release container.
1040 */
1041 unset($dns_to_check['count']);
1042 while(count($dns_to_check)){
1044 /* Create dn */
1045 $new_dn = "";
1046 foreach($dns_to_check as $part){
1047 $new_dn .= $part.",";
1048 }
1049 $new_dn .= $config->current['BASE'];
1051 /* Check if this dn is a release dn */
1052 if(FAI::is_release_department($new_dn)){
1053 return($new_dn);
1054 }
1056 /* Remove first element of dn pieces */
1057 reset($dns_to_check);
1058 unset($dns_to_check[key($dns_to_check)]);
1059 }
1060 return("");
1061 }
1062 }
1067 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1068 ?>