Code

Updated FAI partition tables.
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
22 class faiManagement extends plugin
23 {
24         /* Definitions */
25   var $plHeadline     = "Software deployment";
26   var $plDescription    = "Manage software packages and deployment reciepes";
28         /* Headpage attributes */
29   var $lock_type    = "";    // should be branch/freeze
30   var $lock_name    = "";
31   var $lock_dn      = "";  
33         /* attribute list for save action */
34         var $attributes         = array("lock_type","lock_name","lock_dn");     //      Attributes Managed by this plugin 
35         var $objectclasses= array();    //      ObjectClasses which the attributes are related to
36         var $dialog                             = array();      //      This object contains every dialog we have currently opened
38         var $objects                    = array();      //      This array contains all available objects shown in divlist
39         var $is_dialog          = false;
41   var $dispNewBranch= false;
42   var $dispNewFreeze= false;
44   var $DivListFai;
45   var $start_pasting_copied_objects = FALSE;
46   var $CopyPasteHandler = FALSE;
48   /* Allow inserting of new elements if freezed releases 
49   */
50   var $allow_freeze_object_attach = TRUE;
52   var $no_save;
53   var $fai_base     ="";
54   var $fai_release  ="";
56   var $acl_module = array("fai");
58         /* construction/reconstruction 
59          */
60         function faiManagement (&$config, $ui)
61         {
62                 /* Set defaults */
63                 $this->dn                       = "";
64                 $this->config   = $config;
65                 $this->ui                       = $ui;  
66     
67     /* Creat dialog object */
68     $this->DivListFai = new divListFai($this->config,$this);
70     /* Copy & Paste handler */
71     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
72       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
73     }
75     /* Set default release */
76     $this->fai_base = get_ou("faiou").$this->config->current['BASE'];
77     if(!session::is_set("fai_filter")){
78       session::set("fai_filter",array("fai_release" => $this->fai_base));
79     }
81     $fai_filter = session::get("fai_filter");
82     $this->fai_release = $fai_filter['fai_release'];
83         }
85         function execute()
86         {
87     /* Call parent execute */
88     plugin::execute();
90     /* Initialise vars and smarty */
91                 $smarty         = get_smarty();
92                 $smarty->assign("BranchName","");
93     
94                 $display        = "";
95     $s_action   = "";
96                 $s_entry        = "";
97     $no_save = FALSE;   // hide Apply / Save buttons
98     
99     /* If an entry was locked, these vars will be stored in a session to allow direct edit */
100     session::set('LOCK_VARS_TO_USE',array("/^edit_freeze_entry$/","/^edit_entry$/","/^id$/","/^entry_edit_/","/^entry_delete_/","/^item_selected/","/^remove_multiple_fai_objects/","/^menu_action/"));
103     /****************
104       Handle posts 
105      ****************/
107                 /* Check ImageButton posts
108                  * Create new tab ich new_xx is posted
109                  */
110     $posts = array( "/remove_branch/"=>"remove_branch",    "/branch_branch/"=>"branch_branch",
111                     "/freeze_branch/"=>"freeze_branch",    "/create_partition/i"=>"new_partition",
112                     "/create_script/i"=>"new_script",      "/create_hook/i"=>"new_hook",
113                     "/create_variable/i"=>"new_variable",  "/create_template/i"=>"new_template",
114                     "/create_package/i"=>"new_package",    "/create_profile/i"=>"new_profile",
115                     "/edit_continue/"=>"select_class_name_finished",
116                     "/^multiple_copy_fai/" => "copy_multiple", 
117                     "/^multiple_cut_fai/" => "cut_multiple", 
118                     "/^copy/" => "copy",
119                     "/^remove_multiple_fai_objects/" => "del_multiple");
121                 foreach($_POST as $name => $value){
122       foreach($posts as $reg => $act ){
123         if(preg_match($reg,$name)){
124           $s_action = $act;
125           $s_entry = ltrim(preg_replace($reg,"",$name),"_");
126           $s_entry = preg_replace("/_.*$/","",$s_entry);
127         }
128       }
129                         if(preg_match("/^entry_edit_.*/",$name)){
130                                 $s_entry = preg_replace("/^entry_edit_/","",$name);
131                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
132                                 $s_action = "edit";
133                         }elseif(preg_match("/^entry_freeze_edit_.*/",$name)){
134                                 $s_entry = preg_replace("/^entry_freeze_edit_/","",$name);
135                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
136                                 $s_action = "edit";
137         $no_save = TRUE;
138                         }elseif(preg_match("/^entry_delete_.*/",$name)){
139                                 $s_entry = preg_replace("/^entry_delete_/","",$name);
140                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
141         $s_action = "delete";
142       }
143     }
145                 if(isset($_GET['edit_entry'])){
146                         $s_entry = $_GET['edit_entry'];
147                         $s_action = "edit";
148                 }
150                 if(isset($_GET['edit_freeze_entry'])){
151                         $s_entry = $_GET['edit_freeze_entry'];
152                         $s_action = "edit";
153       $no_save = TRUE;
154                 }
156     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch"){
157       $s_action = "freeze_branch";
158     }
159     if(isset($_GET['act']) && $_GET['act'] == "branch_branch"){
160       $s_action = "branch_branch";
161     }
162     if(isset($_GET['act']) && $_GET['act'] == "remove_branch"){
163       $s_action = "remove_branch";
164     }
165     
166     if((isset($_POST['CancelBranchName'])) || (isset($_POST['CloseIFrame']))){
167       $this->dispNewBranch = false;
168       $this->dispNewFreeze = false;
169     }
172     $type_acl_mapping = array(
173         "FAIpartitionTable"  => "faiPartitionTable", 
174         "FAIpackageList"     => "faiPackage",
175         "FAIscript"          => "faiScript",
176         "FAIvariable"        => "faiVariable",
177         "FAIhook"            => "faiHook",
178         "FAIprofile"         => "faiProfile",
179         "FAItemplate"        => "faiTemplate");
182     /* handle C&P from layers menu */
183     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
184       $s_action = "copy_multiple";
185     }
186     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
187       $s_action = "cut_multiple";
188     }
189     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
190       $s_action = "editPaste";
191     }
193     /* Create options */
194     if(isset($_POST['menu_action']) && preg_match("/^Create_/",$_POST['menu_action'])){
195       $s_action = "new_".preg_replace("/^Create_/","",$_POST['menu_action']);;
196       $s_entry  = preg_replace("/^Create_/","",$_POST['menu_action']);
197     }
199     /* handle remove from layers menu */
200     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
201       $s_action = "del_multiple";
202     }
205     if(!empty($s_action)){
206       $this->no_save = $no_save;
207     }
209     /********************
210       Copy & Paste
211      ********************/
213     /* Display the copy & paste dialog, if it is currently open */
214     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
215     if($ret){
216       return($ret);
217     }
220     /****************
221       Delete confirme dialog 
222      ****************/
224                 if ($s_action=="delete"){
226       /* Get 'dn' from posted termlinst */
227       $this->dn= $this->objects[$s_entry]['dn'];
229                         /* Load permissions for selected 'dn' and check if
230                            we're allowed to remove this 'dn' */
231       $acl  = $this->ui->get_permissions($this->dn,"fai/".$type_acl_mapping[$this->objects[$s_entry]['type']]);
232       if(preg_match("/d/",$acl)){
234                                 /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
235                                 if (($user= get_lock($this->dn)) != ""){
236                                         return(gen_locked_message ($user, $this->dn));
237                                 }
239                                 /* Lock the current entry, so nobody will edit it during deletion */
240                                 add_lock ($this->dn, $this->ui->dn);
241                                 $smarty->assign("warning",msgPool::deleteInfo(@LDAP::fix($this->dn),_("FAI object")));
242         $smarty->assign("multiple", false);
243                                 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
244                         } else {
246                                 /* Obviously the user isn't allowed to delete. Show message and clean session. */
247         msg_dialog::display(_("Permission error"), msgPool::permDelete(),ERROR_DIALOG);
248                         }
249                 }
252     /********************
253       Delete MULTIPLE entries requested, display confirm dialog
254      ********************/
256     if ($s_action=="del_multiple"){
257       $this->dns = array();
258       $ids = $this->list_get_selected_items();
260       if(count($ids)){
262         $errors = "";
263         foreach($ids as $id){
264           $dn = $this->objects[$id]['dn'];
265           $cn = $this->objects[$id]['cn'];
266           if(!preg_match('/^freeze/', $this->objects[$id]['FAIstate'])){
267             $this->dns[$id] = $dn;
268           }else{
269             $errors .= $cn.", ";
270           }
271         }
272         if ($user= get_multiple_locks($this->dns)){
273           return(gen_locked_message($user,$this->dns));
274         }
276         if($errors != ""){
277           msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
278             "<br><br>".trim($errors,", ")),INFO_DIALOG);
279         }
281         if(count($this->dns)){
283           $dns_names = array();
284           foreach($this->dns as $dn){
285             add_lock ($dn, $this->ui->dn);
286             $dns_names[] = @LDAP::fix($dn);
287           }
289           /* Lock the current entry, so nobody will edit it during deletion */
290                                   $smarty->assign("warning",msgPool::deleteInfo($dns_names,_("FAI object")));
291           $smarty->assign("multiple", true);
292           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
293         }
294       }
295     }
298     /********************
299       Delete MULTIPLE entries confirmed
300      ********************/
302     /* Confirmation for deletion has been passed. Users should be deleted. */
303     if (isset($_POST['delete_multiple_fai_object_confirm'])){
305       /* Find out more about the object type */
306       $ldap   = $this->config->get_ldap_link();
308       /* Remove user by user and check acls before removeing them */
309       foreach($this->dns as $key => $dn){
311         $ldap->cat($dn, array('objectClass'));
312         $attrs  = $ldap->fetch();
313         $type   = $this->get_type($attrs);
315         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
316         if(preg_match("/d/",$acl)){
318           $this->dialog = new $type[0]($this->config, $this->config->data['TABS'][$type[2]], $dn,"fai");
319           $this->dialog->parent = &$this;
320           $this->dialog->set_acl_base($dn);
321           $this->dialog->by_object[$type[1]]->remove_from_parent ();
322           unset ($this->dialog);
323           $this->dialog= FALSE;
324           $to_del = FAI::clean_up_releases($dn);
325           FAI::save_release_changes_now();
327           foreach($to_del as $dn){
328             $ldap->rmdir_recursive($dn);
329           }
331         } else {
333           /* Normally this shouldn't be reached, send some extra
334              logs to notify the administrator */
335           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
336           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
337         }
338       }
340       /* Remove lock file after successfull deletion */
341       $this->remove_lock();
342       $this->dns = array();
343     }
346     /********************
347       Delete MULTIPLE entries Canceled
348      ********************/
350     /* Remove lock */
351     if(isset($_POST['delete_multiple_fai_object_cancel'])){
352       $this->dns = array();
353       $this->remove_lock();
354     }
357     /****************
358       Delete aborted  
359      ****************/
361                 /* Delete canceled? */
362                 if (isset($_POST['delete_cancel'])){
363       $this->remove_lock();
364                 }
367     /****************
368       Delete confirmed 
369      ****************/
371                 /* Deltetion was confirmed, so delete this entry
372      */
373     if (isset($_POST['delete_terminal_confirm'])){
375       /* Some nice guy may send this as POST, so we've to check
376          for the permissions again. */
378       /* Find out more about the object type */
379       $ldap       = $this->config->get_ldap_link();
380       $ldap->cat($this->dn, array('objectClass'));
381       if($ldap->count()){
382         $attrs  = $ldap->fetch();
383         $type     = $this->get_type($attrs);                    
385         $acl  = $this->ui->get_permissions($this->dn,"fai/".$type[1]);
386         if(preg_match("/d/",$acl)){
388           $this->dialog = new $type[0]($this->config,   $this->config->data['TABS'][$type[2]], $this->dn,"fai");
389           $this->dialog->set_acl_base($this->dn);
390           $this->dialog->parent = &$this;
391           $this->dialog->by_object[$type[1]]->remove_from_parent ();
392           unset ($this->dialog);
393           $this->dialog= FALSE;
394           $to_del = FAI::clean_up_releases($this->dn);
395           FAI::save_release_changes_now();
397           foreach($to_del as $dn){
398             $ldap->rmdir_recursive($dn);
399           }
401         } else {
403           /* Normally this shouldn't be reached, send some extra
404              logs to notify the administrator */
405           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
406           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
407         }
409       }else{
410         msg_dialog::display(_("Error"), sprintf(_("Cannot delete '%s': object does not exist!"), $this->dn) , ERROR_DIALOG);
411       }
413       /* Remove lock file after successfull deletion */
414       $this->remove_lock();
415     }
418     /****************
419       Edit entry 
420      ****************/
422                 if(($s_action == "edit") && (!isset($this->dialog->config))){
423                         $entry    = $this->objects[$s_entry];
424                         $a_setup  = $this->get_type($entry);
425                         $this->dn = $entry['dn'];
427                         /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
428                         if (($user= get_lock($this->dn)) != ""){
429                                 return(gen_locked_message ($user, $this->dn));
430                         }
431                         add_lock ($this->dn, $this->ui->dn);
433                         $this->dialog     = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
434       $this->dialog->parent = &$this;
435       $this->dialog->by_object[$a_setup[1]]->FAIstate = $entry['FAIstate'];
436       $this->dialog->set_acl_base($this->dn);
437                         $this->is_dialog  = true;
438                         session::set('objectinfo',$this->dn);
439                 }
442     /*  Branch handling 
443         09.01.2006
444     */
446     /****************
447       Remove branch
448      ****************/
450     /* Remove branch 
451      */
452     if($s_action == "remove_branch"){
453       $base= $this->fai_release;
455       /* Check if we have a post remove method configured
456        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
457        */
458       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
459         /* Load permissions for selected 'dn' and check if
460            we're allowed to remove this 'dn' */
461         if($this->acl_is_removeable()){
462           $smarty->assign("release_hidden",base64_encode($this->fai_release));
463           $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->fai_release),_("FAI branch/freeze")));
464           return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
465         } else {
466           msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
467         }
468       }
469     }
471     
472     /****************
473       Remove branch confirmed
474      ****************/
476     if(isset($_POST['delete_branch_confirm'])){
478       /* Check if we have a post remove method configured
479        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
480        */
481       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
483         if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
484           msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
485         }else{
487           $bb =  $this->fai_release;
488           if(!isset($ldap)){
489             $ldap = $this->config->get_ldap_link();
490           }
492           $br = $this->getBranches();
494           if(isset($br[$bb]) && $this->acl_is_removeable()){
495             $name = $br[$bb];
497             $ldap->cd($bb);
498             $ldap->recursive_remove();
499             $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('applicationou'), $bb));
500             $ldap->recursive_remove();
501             $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('mimetypeou'), $bb));
502             $ldap->recursive_remove();
504             /* Search for all groups with configured application menus.
505               - First search all groups, to ensure that we only remove entries form whithin groups. 
506               - The search für menu configuration for the specified release and collect all those dns.
507               - Remove entries
508              */
509             $release_ou = preg_replace("/".normalizePreg(get_ou("faiou")).".*$/","",$bb);
510             $ldap->cd($this->config->current['BASE']);
511             $ldap->search("(objectClass=posixGroup)",array("dn"));
512           
513             /* Collect all group dns 
514              */
515             $groups = array();
516             while($attrs = $ldap->fetch()){
517               $groups[] = $attrs['dn'];
518             }
520             /* Collect all group menu release dns that match the release we have removed 
521              */
522             $dns = array();
523             foreach($groups as $dn){
524               $ldap->cd($dn);
525               $ldap->search("(objectClass=FAIbranch)",array("dn"));
526               while($attrs = $ldap->fetch()){
527                 if(preg_match("/^".normalizePreg($release_ou)."/",$attrs['dn'])){
528                   $dns[] = $attrs['dn'];
529                 }
530               }
531             }
532             
533             /* Finally remove collected release dns 
534              */
535             foreach($dns as $dn){
536               $ldap->cd($dn);
537               $ldap->recursive_remove();
538             }
540             /* Post remove */
541             $this->fai_release = $this->fai_base;
542             $this->lock_name   = $name;
543             $this->lock_dn     = $bb;
544             $this->postremove();
546             $fai_filter = session::get("fai_filter");
547             $fai_filter['fai_release'] = $this->fai_release;
548             session::set("fai_filter",$fai_filter);
550             new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
551           }
552         }
553       }
554     }
557     /****************
558       Create a new branch "insert Name"
559      ****************/
561     if((isset($_POST['UseBranchName']))&&(($this->dispNewBranch)||($this->dispNewFreeze))){
562       session::set('LASTPOST',$_POST);
564       if($this->dispNewBranch){
565         $type = "branch";
566       }else{
567         $type = "freeze";
568       }
570       /* Check branch name */
571       $name = $_POST['BranchName'];
572       $is_ok = true;
573       $smarty->assign("BranchName",$name);
574       $base= $this->fai_base;
576       /* Check used characters */
577       if(!preg_match("/^[0-9a-z\.]*$/",$name)){
578         msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
579         $is_ok = false;
580       }
582       /* Check if this name is already in use */
583       if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
584         msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
585         $is_ok = false;
586       }
588       if($is_ok){
590         if(session::is_set('LASTPOST')){
591           $LASTPOST = session::get('LASTPOST');
592         }else{
593           $LASTPOST = array();
594         }
595         $LASTPOST['base'] = $base;
596         $LASTPOST['type'] = $type;
597         session::set('LASTPOST',$LASTPOST);
598         $smarty->assign("iframe", true);
599         $smarty->assign("plugID", $_GET['plug']);
600         $display        = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
601         return($display);
602       }
603     }
606     /****************
607       Create a new branch 
608      ****************/
610     if(isset($_GET['PerformBranch'])){
611     
612       if(!$this->acl_is_createable()){
613         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
614       }else{
616         /* In order to see error messages we have to reset the error handler.
617             Due to the exit(); 
618          */
619         restore_error_handler();
621         /* Create it know */
622         $this->dispNewBranch = false;
623         $this->dispNewFreeze = false;
625         $LASTPOST = session::get('LASTPOST');
626         $base = $LASTPOST['base'];
627         $_POST  = session::get('LASTPOST');      
628         $name = $_POST['BranchName'];
630         $type = $LASTPOST['type'];
631         $ldap = $this->config->get_ldap_link();
633         $baseToUse = $base;
634         if($this->fai_release !=  $this->fai_base){
635           $baseToUse = $this->fai_release;
636         }
638         /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
640         $CurrentReleases  = $this->getBranches();
641         $NewReleaseName   = $name;
642         if(isset($CurrentReleases[$this->fai_release])) {
643           if($this->fai_release != $this->fai_base){
644             $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
645             $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName); 
646           }else{
647             $NewReleaseName   = $name;
648           }
649         }
651         $appsrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),$baseToUse); 
652         $appdst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),"ou=".$name.",".$baseToUse) ; 
654         $mimesrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),$baseToUse); 
655         $mimedst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),"ou=".$name.",".$baseToUse) ; 
657         /* Check if source depeartments exist */
658         foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
659           $ldap->cd($this->config->current['BASE']);
660           $ldap->cat($dep);
661           if(!$ldap->count()){
662             $ldap->create_missing_trees($dep);
663           }
664         }
666         /* Print header to have styles included */
667         echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
668           <html>
669           <head>
670           <title></title>
671           <style type="text/css">@import url("themes/default/style.css");</style>
672           <script language="javascript" src="include/focus.js" type="text/javascript"></script>
673           </head>
674           <body style="background: none;margin:3px;color:black">
675           ';
677         new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
679         /* Duplicate group application releases 
680          */
681         FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
683         /* Duplicate applications 
684          */
685         $ldap->cat($appsrc,array("dn")) ;
686         if($ldap->count()){
687           $ldap->cd ($appdst);
688           $ldap->recursive_remove();
689           FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
690         }
692         /* Duplicate mime types 
693          */
694         $ldap->cat($mimesrc,array("dn")) ;
695         if($ldap->count()){
696           $ldap->cd ($mimedst);
697           $ldap->recursive_remove();
698           FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
699         }
701         $attr = array();
702         $attr['objectClass'] = array("organizationalUnit","FAIbranch");
703         $attr['ou']       = $name;
704         $attr['FAIstate'] = $type;
705         $ldap->cd($this->config->current['BASE']);
706         $ldap->cd("ou=".$name.",".$baseToUse);
707         $ldap->cat("ou=".$name.",".$baseToUse);
708         if($ldap->count()){
709           $ldap->modify($attr);
710         }else{
711           $ldap->add($attr);
712         }
714         /* Duplicate fai objects 
715          */
716         //      $ldap->cd ("ou=".$name.",".$baseToUse);
717         //      $ldap->recursive_remove();
718         //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
720         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
721           <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
722           </form></div>";
724         echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
726         /* Print footer to have valid html */
727         echo "</body></html>";
729         $this->dispNewFreeze = false; 
731         /* Postcreate */ 
733         /* Assign possible attributes */
734         $this->lock_type  = $type; 
735         $this->lock_name  = $name; 
736         $this->lock_dn    = $baseToUse;
737         $this->postcreate();
740         /* Send daemon event to reload the fai release database 
741          */
742         if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
743           $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
744           if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
745             $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'];
746             $tmp = new $evt['CLASS_NAME']($this->config);
747             $tmp->set_type(TRIGGERED_EVENT);
748             $tmp->add_targets(array("GOsa"));
749             $o_queue = new gosaSupportDaemon();
750             if(!$o_queue->append($tmp)){
751               msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
752             }
753           }
754         }else{  
755           trigger_error("Unknown class DaemonEvent / gosaSupportDaemon");
756         }
757         exit();
758       }
759     }
761     /****************
762       Display dialog to enter new Branch name
763      ****************/
765     /* Check if we have a post create method configured
766      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
767      */
768     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
769       if(($s_action == "branch_branch")||($this->dispNewBranch)){
770         if(!$this->acl_is_createable()){
771         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
772         }else{
773           $this->dispNewBranch=true;
774           $smarty->assign("iframe",false);
775           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
776           return($display);
777         }
778       } 
779     }
781  
782     /****************
783       Display dialog to enter new Freeze name
784      ****************/
786     /* Check if we have a post create method configured
787      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
788      */
789     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
790       if(($s_action == "freeze_branch")||($this->dispNewFreeze)){
791         if(!$this->acl_is_createable()){
792           msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
793         }else{
794           $this->dispNewFreeze = true;
795           $smarty->assign("iframe",false);
796           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
797           return($display);
798         }
799       }
800     }
803     /****************
804       Create a new object 
805      ****************/
807     $types = array( "new_partition"     =>  "FAIpartitionTable",
808                     "new_script"        =>  "FAIscript",
809                     "new_hook"          =>  "FAIhook",
810                     "new_variable"      =>  "FAIvariable",
811                     "new_template"      =>  "FAItemplate",
812                     "new_package"       =>  "FAIpackageList");
813     $types_i18n = array( "new_partition"     =>  _("partition table"),
814                     "new_script"        =>  _("script"),
815                     "new_hook"          =>  _("hook"),
816                     "new_variable"      =>  _("variable"),
817                     "new_template"      =>  _("template"),
818                     "new_package"       =>  _("package list"));
820     if(isset($types[$s_action])){
821       $acl = $this->ui->get_permissions($this->fai_base,"fai/".$type_acl_mapping[$types[$s_action]]);
822       if(preg_match("/c/",$acl)){
823         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,$types[$s_action]);
824         $this->dialog->parent = &$this;
825       }else{
826         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$s_action]), ERROR_DIALOG);
827       }
828     }
830     /* New Profile */
831     if($s_action == "new_profile"){
832       $this->dn = "new" ;
834       $acl = $this->ui->get_permissions($this->fai_base,"fai/faiProfile");
835       if(preg_match("/c/",$acl)){
836         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
837         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
838         $this->dialog->set_acl_base($this->base);
839         $this->dialog->parent = &$this;
841         $this->is_dialog = false;
842       }else{
843         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
844       }
845     }
848     /****************
849       Get from ask class name dialog 
850      ****************/
852     if($s_action == "select_class_name_finished"){
853       $this->dialog->save_object();
854       if(count($this->dialog->check())!=0){
855         foreach($this->dialog->check() as $msg){
856           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
857         }               
858       }elseif(isset($this->dialog->objectClass)){
859         $this->dn = "new" ;
860         $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
861         $name = $this->dialog->save();
863         if(class_exists($a_setup[0])){
864           $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
865           $this->dialog->set_acl_base($this->base);
866           $this->dialog->by_object[$a_setup[1]]->cn = $name;
867           $this->dialog->parent = &$this;
868           $this->is_dialog = true;
869         }
870       }         
871     }   
874     /****************
875      Cancel dialogs 
876      ****************/
878                 if(isset($_POST['edit_cancel'])){
879                         $this->dialog=FALSE;
880                         $this->is_dialog = false;
881                         session::un_set('objectinfo');
882       $this->remove_lock();
883                 }
886     /****************
887       Save sub dialogs 
888      ****************/
890                 /* This check if the given tab could be saved 
891                  * If it was possible to save it, remove dialog object. 
892                  * If it wasn't possible, show errors and keep dialog.
893                  */
894                 if((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->dialog->config))){
895                         $this->dialog->save_object();
896       $msgs= $this->dialog->check();
897                         if(count($msgs)!=0){
898                                 foreach($msgs as $msg){
899           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
900                                 }
901                         }else{
902                                 $this->dialog->save();
903         FAI::save_release_changes_now();
904         if (!isset($_POST['edit_apply'])){
905           $this->remove_lock();
906           $this->dialog=FALSE;
907           $this->is_dialog=false;
908           session::un_set('objectinfo');
909         }else{
911           /* Reinitialize tab */
912           if($this->dialog instanceof tabs){
913             $this->dialog->re_init();
914           }
915         }
916                         }
917                 }
920     /****************
921       Display currently open dialog 
922      ****************/
924                 /* If dialog is set, but $this->is_dialog==false, then 
925                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
926                  * If is_dialog == true, we are currently editing tab objects.
927                  *  Here we need both, save and cancel
928                  */ 
930                 if(is_object($this->dialog)){
931                         $display .= $this->dialog->execute();
932                         /* Don't show buttons if tab dialog requests this */
934       if(isset($this->dialog->current)){
936         $obj = $this->dialog->by_object[$this->dialog->current];
938         if((isset($obj->is_dialog) && (!$obj->is_dialog)) || (isset($obj->dialog) && (!$obj->dialog))){
940           $display.= "<p style=\"text-align:right\">\n";
941           if(!$this->no_save){
942             $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
943             $display.= "&nbsp;\n";
944             if ($this->dn != "new"){
945               $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
946               $display.= "&nbsp;\n";
947             }
948           }
949           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
950           $display.= "</p>";
951         }elseif(!isset($this->dialog->current)){
952           $display.= "<p style=\"text-align:right\">\n";
953           $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
954           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
955           $display.= "</p>";
956         }
957       }else{
958         $display.= "<p style=\"text-align:right\">\n";
959         $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
960         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
961         $display.= "</p>";
963       }
964       return($display);
965                 }
966                 
968     /****************
969       Dialog display
970      ****************/
972     /* Check if there is a snapshot dialog open */
973     $base = $this->fai_base;
974     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
975       return($str);
976     }
978     /* Display dialog with system list */
979     $this->reload();
980     $this->DivListFai->parent = &$this;
981     $this->DivListFai->execute();
982     $this->DivListFai->setEntries($this->objects);
983     return($this->DivListFai->Draw());
984         }
987   /* Return departments, that will be included within snapshot detection */
988   function get_used_snapshot_bases()
989   {
990     $tmp = array();
991     $types = array("faipartitionou","faiscriptou","faitemplateou","faihookou","faiprofileou","faivariableou","faipackageou");
992     foreach($types as $type){
993       $tmp[] = get_ou($type).$this->fai_release;
994     }
995     return($tmp);
996   }
999   /* Get available branches for current base */
1000   function getBranches($base = false,$prefix = "")
1001   {
1002     $ret = array("/"=>$this->fai_base);
1003     $ldap = $this->config->get_ldap_link();
1004     if(!$base){
1005       $base = $this->fai_base;
1006     }
1007     $tmp = FAI::get_all_releases_from_base($base,true);
1008     foreach($tmp as $dn => $name){
1009       $ret[$name]=$dn;
1010     }
1011     ksort($ret);
1012     $ret = array_flip($ret);
1014     return ($ret);
1015   }
1016   
1018   function list_get_selected_items()
1019   {
1020     $ids = array();
1021     foreach($_POST as $name => $value){
1022       if(preg_match("/^item_selected_[0-9]*$/",$name)){
1023         $id   = preg_replace("/^item_selected_/","",$name);
1024         $ids[$id] = $id;
1025       }
1026     }
1027     return($ids);
1028   }
1031   /* reload list of objects */
1032   function reload()
1033   {
1034     /* Variable initialisation */
1035     $str            = "";
1036     $Regex          = $this->DivListFai->Regex;
1037     $this->objects  = array();
1039     /* Get base */
1040     $base = $this->fai_base;
1041     if($this->fai_release != $this->fai_base){
1042       $br = $this->getBranches();
1043       if(isset($br[$this->fai_release])){
1044         $base = $this->fai_release;
1045       }else{
1046         $base = $this->fai_base;
1047       }
1048     }
1049     $this->base = $base;
1050     $this->set_acl_base($this->base);
1052     $this->lock_type = FAI::get_release_tag(FAI::get_release_dn($base));
1054     /* Create a new list of FAI object 
1055      * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
1056      */
1057     $ObjectTypes = array(
1058         "FAIpartitionTable"  => array("OU"=> get_ou('faipartitionou') , "CHKBOX"=>"ShowPartitions"  ,"ACL" => "faiPartitionTable"),
1059         "FAIpackageList"     => array("OU"=> get_ou('faipackageou')   , "CHKBOX"=>"ShowPackages"    ,"ACL" => "faiPackage"),
1060         "FAIscript"          => array("OU"=> get_ou('faiscriptou')    , "CHKBOX"=>"ShowScripts"     ,"ACL" => "faiScript"),
1061         "FAIvariable"        => array("OU"=> get_ou('faivariableou')  , "CHKBOX"=>"ShowVariables"   ,"ACL" => "faiVariable"),
1062         "FAIhook"            => array("OU"=> get_ou('faihookou')      , "CHKBOX"=>"ShowHooks"       ,"ACL" => "faiHook"),
1063         "FAIprofile"         => array("OU"=> get_ou('faiprofileou')   , "CHKBOX"=>"ShowProfiles"    ,"ACL" => "faiProfile"),
1064         "FAItemplate"        => array("OU"=> get_ou('faitemplateou')  , "CHKBOX"=>"ShowTemplates"   ,"ACL" => "faiTemplate"));
1066     $filter = "";
1067     foreach($ObjectTypes as $key => $data){
1068       if($this->DivListFai->$data['CHKBOX']){
1069         $filter.= "(objectClass=".$key.")";
1070       }
1071     }
1072     $filter = "(&(|".$filter.")(cn=$Regex))";
1073     
1074     /* Get resolved release dependencies */
1075     $tmp = FAI::get_all_objects_for_given_base($base,$filter);
1077     /* Ge listed ldap objects */
1078     $ldap = $this->config->get_ldap_link();
1079     $ldap->cd($this->config->current['BASE']);
1081     foreach($tmp as $entry){
1083       /* Get some more informations about the object */ 
1084       $ldap->cat($entry['dn'], array("cn","description","objectClass","FAIclass","FAIstate","objectClass"));
1085       $object  = $ldap->fetch();
1087       /* Walk through possible types */
1088       foreach($ObjectTypes as $type => $rest){  
1090         $acl = $this->ui->get_permissions($object['dn'],"fai/".$rest ['ACL']);
1092         if(in_array($type,$object['objectClass']) && preg_match("/r/",$acl)){
1094           /* Prepare object */
1095           unset($object['objectClass']['count']);
1096           if(!isset($object['description'][0])){
1097             $object['description'][0]="";
1098           }
1100           /* Clean up object informations */
1101           $obj                  = array();
1102           $obj['cn']                          = $object['cn'][0];
1103           $obj['dn']                          = $object['dn'];
1104           $obj['acl']                       = $acl;
1105           $obj['class']                           = $rest ['ACL'];
1106           $obj['FAIstate']      = $entry['FAIstate'];
1107           $obj['description']   = $object['description'][0];
1108           $obj['objectClass']   = $object['objectClass'];
1110           $this->objects[strtolower($obj['cn']).$obj['cn'].$type] = $obj;
1111           $this->objects[strtolower($obj['cn']).$obj['cn'].$type]['type']=$type;
1112         }
1113                         }
1114                 }
1116                 ksort($this->objects);
1117                 reset ($this->objects);
1118         
1119                 /* use numeric index, thats a bit more secure */        
1120                 $tmp0 = array();
1121                 foreach($this->objects as $obj){
1122                         $tmp0[]= $obj;
1123                 }
1124                 $this->objects = array();
1125                 $this->objects = $tmp0;
1126         }
1128         function remove_lock()
1129         {
1130                 if (isset($this->dn)){
1131                         del_lock ($this->dn);
1132                 }
1133     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
1134       del_lock ($this->dns);
1135     }
1136         }
1138         function get_type($array){
1139                 if(in_array("FAIpartitionTable",$array['objectClass'])){
1140                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
1141                 }
1142                 if(in_array("FAIscript",$array['objectClass'])){
1143                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
1144                 }
1145                 if(in_array("FAItemplate",$array['objectClass'])){
1146                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
1147                 }
1148                 if(in_array("FAIhook",$array['objectClass'])){
1149                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
1150                 }
1151                 if(in_array("FAIvariable",$array['objectClass'])){
1152                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
1153                 }
1154                 if(in_array("FAIprofile",$array['objectClass'])){
1155                         return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
1156                 }
1157                 
1158                 if(in_array("FAIpackageList",$array['objectClass'])){
1159                         return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
1160                 }
1161         }
1163   function CheckNewBranchName($name,$base)
1164   {
1165     $f = $this->fai_release;
1166     if($name == ""){
1167       return(false);
1168     }elseif(in_array($name,$this->getBranches($f))) {
1169       return(false);
1170     }elseif(tests::is_department_name_reserved($name,$base)){
1171       return(false);
1172     }
1173     return(true);
1174   }
1176   function save_object()
1177   {
1178     $this->DivListFai->save_object();
1180     /* Get posted release */
1181     $r_releases = array_flip($this->getBranches());
1182     if(isset($_POST['fai_release']) && isset($r_releases[get_post('fai_release')])){
1184       /* Ensure that we have a valid release selected */
1185       if(!isset($r_releases[get_post('fai_release')])){
1186         msg_dialog::display(_("Warning"),_("The selected release is not available anymore. All triggered actions are skipped."));
1187         $_POST = array();
1188         $plug =$_GET['plug'];
1189         $_GET  = array("plug" => $plug);
1190         $this->fai_release = $this->fai_base;
1191       }else{
1192         $this->fai_release = $r_releases[get_post('fai_release')];
1193       }
1195       $fai_filter = session::get("fai_filter");
1196       $fai_filter['fai_release'] = $this->fai_release;
1197       session::set("fai_filter",$fai_filter);
1198     }
1200     if(is_object($this->CopyPasteHandler)){
1201       $this->CopyPasteHandler->save_object();
1202     }
1203   }
1206   function copyPasteHandling_from_queue($s_action,$s_entry)
1207   {
1208     /* Check if Copy & Paste is disabled */
1209     if(!is_object($this->CopyPasteHandler)){
1210       return("");
1211     }
1213     $ui = get_userinfo();
1215     /* Add a single entry to queue */
1216     if($s_action == "copy"){
1218       /* Cleanup object queue */
1219       $this->CopyPasteHandler->cleanup_queue();
1220       $entry    = $this->objects[$s_entry];
1221       $a_setup  = $this->get_type($entry);
1222       $dn = $entry['dn'];
1224       if($ui->is_copyable($dn,"fai",$a_setup[1])){
1225         $this->CopyPasteHandler->add_to_queue($dn,$s_action,$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1226       }
1227     }
1229     /* Add entries to queue */
1230     if($s_action == "copy_multiple"){
1232       /* Cleanup object queue */
1233       $this->CopyPasteHandler->cleanup_queue();
1235       /* Add new entries to CP queue */
1236       foreach($this->list_get_selected_items() as $id){
1238         /* Cleanup object queue */
1239         $entry    = $this->objects[$id];
1240         $a_setup  = $this->get_type($entry);
1241         $dn = $entry['dn'];
1243         if($s_action == "copy_multiple" && $ui->is_copyable($dn,"fai",$a_setup[1])){
1244           $this->CopyPasteHandler->add_to_queue($dn,"copy",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1245         }
1246       }
1247     }
1249     /* Start pasting entries */
1250     if($s_action == "editPaste" && !($this->lock_type == "freeze" && !$this->allow_freeze_object_attach)){
1251       $this->start_pasting_copied_objects = TRUE;
1252     }
1254     /* Return C&P dialog */
1255     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1257       /* Get dialog */
1258       $this->CopyPasteHandler->SetVar("parent",$this);
1259       $data = $this->CopyPasteHandler->execute();
1261       FAI::save_release_changes_now();
1263       /* Return dialog data */
1264       if(!empty($data)){
1265         return($data);
1266       }
1267     }
1269     /* Automatically disable status for pasting */
1270     if(!$this->CopyPasteHandler->entries_queued()){
1271       $this->start_pasting_copied_objects = FALSE;
1272     }
1273     return("");
1274   }
1277   /* Check if the given FAI class is used in this release 
1278    */
1279   static function check_class_name($oc,$name,$dn)
1280   {
1281     $base = FAI::get_release_dn($dn);
1282     $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
1283     $delete = array();
1284     $used   = array();
1285     foreach($res as $object){
1286       $used[$object['cn'][0]]= $object['cn'][0];
1287     }
1288     return($used);
1289   }
1292   /* Return plugin informations for acl handling */ 
1293   static function plInfo()
1294   {
1295     return (array( 
1296           "plShortName"   => _("FAI releases"),
1297           "plDescription" => _("FAI release management"),
1298           "plSelfModify"  => FALSE,
1299           "plDepends"     => array(),
1300           "plPriority"    => 0,
1301           "plSection"     => array("administration"),           
1302           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1303                                                  "objectClass" => "FAIclass")),
1304           "plProvidedAcls"=> array()));
1305   }
1307 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1308 ?>