Code

list base image migration for gosa-plugins
[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;
47   var $base = "";
49   /* Allow inserting of new elements if freezed releases 
50   */
51   var $allow_freeze_object_attach = FALSE;
53   var $no_save;
55         /* construction/reconstruction 
56          */
57         function faiManagement (&$config, $ui)
58         {
59                 /* Set defaults */
60                 $this->dn                       = "";
61                 $this->config   = $config;
62                 $this->ui                       = $ui;  
63     
64     /* Creat dialog object */
65     $this->DivListFai = new divListFai($this->config,$this);
67     /* Copy & Paste handler */
68     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
69       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
70     }
71         }
73         function execute()
74         {
75     /* Call parent execute */
76     plugin::execute();
78     /* Initialise vars and smarty */
79                 $smarty         = get_smarty();
80                 $smarty->assign("BranchName","");
81     
82                 $display        = "";
83     $s_action   = "";
84                 $s_entry        = "";
85     $no_save = FALSE;   // hide Apply / Save buttons
86     
87     /* If an entry was locked, these vars will be stored in a session to allow direct edit */
88     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/"));
91     /****************
92       Handle posts 
93      ****************/
95                 /* Check ImageButton posts
96                  * Create new tab ich new_xx is posted
97                  */
98     $posts = array( "/remove_branch/"=>"remove_branch",    "/branch_branch/"=>"branch_branch",
99                     "/freeze_branch/"=>"freeze_branch",    "/create_partition/i"=>"new_partition",
100                     "/create_script/i"=>"new_script",      "/create_hook/i"=>"new_hook",
101                     "/create_variable/i"=>"new_variable",  "/create_template/i"=>"new_template",
102                     "/create_package/i"=>"new_package",    "/create_profile/i"=>"new_profile",
103                     "/edit_continue/"=>"select_class_name_finished",
104                     "/^multiple_copy_fai/" => "copy_multiple", 
105                     "/^multiple_cut_fai/" => "cut_multiple", 
106                     "/^copy/" => "copy",
107                     "/^cut/" => "cut",
108                     "/^remove_multiple_fai_objects/" => "del_multiple");
110                 foreach($_POST as $name => $value){
111       foreach($posts as $reg => $act ){
112         if(preg_match($reg,$name)){
113           $s_action = $act;
114           $s_entry = ltrim(preg_replace($reg,"",$name),"_");
115           $s_entry = preg_replace("/_.*$/","",$s_entry);
116         }
117       }
118                         if(preg_match("/^entry_edit_.*/",$name)){
119                                 $s_entry = preg_replace("/^entry_edit_/","",$name);
120                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
121                                 $s_action = "edit";
122                         }elseif(preg_match("/^entry_freeze_edit_.*/",$name)){
123                                 $s_entry = preg_replace("/^entry_freeze_edit_/","",$name);
124                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
125                                 $s_action = "edit";
126         $no_save = TRUE;
127                         }elseif(preg_match("/^entry_delete_.*/",$name)){
128                                 $s_entry = preg_replace("/^entry_delete_/","",$name);
129                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
130         $s_action = "delete";
131       }
132     }
134                 if(isset($_GET['edit_entry'])){
135                         $s_entry = $_GET['edit_entry'];
136                         $s_action = "edit";
137                 }
139                 if(isset($_GET['edit_freeze_entry'])){
140                         $s_entry = $_GET['edit_freeze_entry'];
141                         $s_action = "edit";
142       $no_save = TRUE;
143                 }
145     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch"){
146       $s_action = "freeze_branch";
147     }
148     if(isset($_GET['act']) && $_GET['act'] == "branch_branch"){
149       $s_action = "branch_branch";
150     }
151     if(isset($_GET['act']) && $_GET['act'] == "remove_branch"){
152       $s_action = "remove_branch";
153     }
154     
155     if((isset($_POST['CancelBranchName'])) || (isset($_POST['CloseIFrame']))){
156       $this->dispNewBranch = false;
157       $this->dispNewFreeze = false;
158     }
161     $type_acl_mapping = array(
162         "FAIpartitionTable"  => "faiPartitionTable", 
163         "FAIpackageList"     => "faiPackage",
164         "FAIscript"          => "faiScript",
165         "FAIvariable"        => "faiVariable",
166         "FAIhook"            => "faiHook",
167         "FAIprofile"         => "faiProfile",
168         "FAItemplate"        => "faiTemplate");
171     /* handle C&P from layers menu */
172     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
173       $s_action = "copy_multiple";
174     }
175     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
176       $s_action = "cut_multiple";
177     }
178     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
179       $s_action = "editPaste";
180     }
182     /* Create options */
183     if(isset($_POST['menu_action']) && preg_match("/^Create_/",$_POST['menu_action'])){
184       $s_action = "new_".preg_replace("/^Create_/","",$_POST['menu_action']);;
185       $s_entry  = preg_replace("/^Create_/","",$_POST['menu_action']);
186     }
188     /* handle remove from layers menu */
189     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
190       $s_action = "del_multiple";
191     }
194     if(!empty($s_action)){
195       $this->no_save = $no_save;
196     }
198     /********************
199       Copy & Paste
200      ********************/
202     /* Display the copy & paste dialog, if it is currently open */
203     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
204     if($ret){
205       return($ret);
206     }
209     /****************
210       Delete confirme dialog 
211      ****************/
213                 if ($s_action=="delete"){
215       /* Get 'dn' from posted termlinst */
216       $this->dn= $this->objects[$s_entry]['dn'];
218                         /* Load permissions for selected 'dn' and check if
219                            we're allowed to remove this 'dn' */
220       $acl  = $this->ui->get_permissions($this->dn,"fai/".$type_acl_mapping[$this->objects[$s_entry]['type']]);
221       if(preg_match("/d/",$acl)){
223                                 /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
224                                 if (($user= get_lock($this->dn)) != ""){
225                                         return(gen_locked_message ($user, $this->dn));
226                                 }
228                                 /* Lock the current entry, so nobody will edit it during deletion */
229                                 add_lock ($this->dn, $this->ui->dn);
230                                 $smarty->assign("warning",msgPool::deleteInfo(@LDAP::fix($this->dn),_("FAI object")));
231         $smarty->assign("multiple", false);
232                                 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
233                         } else {
235                                 /* Obviously the user isn't allowed to delete. Show message and clean session. */
236         msg_dialog::display(_("Permission error"), msgPool::permDelete(),ERROR_DIALOG);
237                         }
238                 }
241     /********************
242       Delete MULTIPLE entries requested, display confirm dialog
243      ********************/
245     if ($s_action=="del_multiple"){
246       $this->dns = array();
247       $ids = $this->list_get_selected_items();
249       if(count($ids)){
251         $errors = "";
252         foreach($ids as $id){
253           $dn = $this->objects[$id]['dn'];
254           $cn = $this->objects[$id]['cn'];
255           if(!preg_match('/^freeze/', $this->objects[$id]['FAIstate'])){
256             $this->dns[$id] = $dn;
257           }else{
258             $errors .= $cn.", ";
259           }
260         }
261         if ($user= get_multiple_locks($this->dns)){
262           return(gen_locked_message($user,$this->dns));
263         }
265         if($errors != ""){
266           msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
267             "<br><br>".trim($errors,", ")),INFO_DIALOG);
268         }
270         if(count($this->dns)){
272           $dns_names = array();
273           foreach($this->dns as $dn){
274             add_lock ($dn, $this->ui->dn);
275             $dns_names[] = @LDAP::fix($dn);
276           }
278           /* Lock the current entry, so nobody will edit it during deletion */
279                                   $smarty->assign("warning",msgPool::deleteInfo($dns_names,_("FAI object")));
280           $smarty->assign("multiple", true);
281           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
282         }
283       }
284     }
287     /********************
288       Delete MULTIPLE entries confirmed
289      ********************/
291     /* Confirmation for deletion has been passed. Users should be deleted. */
292     if (isset($_POST['delete_multiple_fai_object_confirm'])){
294       /* Find out more about the object type */
295       $ldap   = $this->config->get_ldap_link();
297       /* Remove user by user and check acls before removeing them */
298       foreach($this->dns as $key => $dn){
300         $ldap->cat($dn, array('objectClass'));
301         $attrs  = $ldap->fetch();
302         $type   = $this->get_type($attrs);
304         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
305         if(preg_match("/d/",$acl)){
307           $this->dialog = new $type[0]($this->config, $this->config->data['TABS'][$type[2]], $dn,"fai");
308           $this->dialog->set_acl_base($dn);
309           $this->dialog->by_object[$type[1]]->remove_from_parent ();
310           unset ($this->dialog);
311           $this->dialog= FALSE;
312           $to_del = FAI::clean_up_releases($dn);
313           FAI::save_release_changes_now();
315           foreach($to_del as $dn){
316             $ldap->rmdir_recursive($dn);
317           }
319         } else {
321           /* Normally this shouldn't be reached, send some extra
322              logs to notify the administrator */
323           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
324           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
325         }
327         /* Remove lock file after successfull deletion */
328         del_lock ($dn);
329         unset($this->dns[$key]);
330       }
331     }
334     /********************
335       Delete MULTIPLE entries Canceled
336      ********************/
338     /* Remove lock */
339     if(isset($_POST['delete_multiple_fai_object_cancel'])){
340       foreach($this->dns as $key => $dn){
341         del_lock ($dn);
342         unset($this->dns[$key]);
343       }
344     }
347     /****************
348       Delete aborted  
349      ****************/
351                 /* Delete canceled? */
352                 if (isset($_POST['delete_cancel'])){
353                         del_lock ($this->dn);
354                 }
357     /****************
358       Delete confirmed 
359      ****************/
361                 /* Deltetion was confirmed, so delete this entry
362      */
363     if (isset($_POST['delete_terminal_confirm'])){
365       /* Some nice guy may send this as POST, so we've to check
366          for the permissions again. */
368       /* Find out more about the object type */
369       $ldap       = $this->config->get_ldap_link();
370       $ldap->cat($this->dn, array('objectClass'));
371       if($ldap->count()){
372         $attrs  = $ldap->fetch();
373         $type     = $this->get_type($attrs);                    
375         $acl  = $this->ui->get_permissions($this->dn,"fai/".$type[1]);
376         if(preg_match("/d/",$acl)){
378           $this->dialog = new $type[0]($this->config,   $this->config->data['TABS'][$type[2]], $this->dn,"fai");
379           $this->dialog->set_acl_base($this->dn);
380           $this->dialog->by_object[$type[1]]->remove_from_parent ();
381           unset ($this->dialog);
382           $this->dialog= FALSE;
383           $to_del = FAI::clean_up_releases($this->dn);
384           FAI::save_release_changes_now();
386           foreach($to_del as $dn){
387             $ldap->rmdir_recursive($dn);
388           }
390         } else {
392           /* Normally this shouldn't be reached, send some extra
393              logs to notify the administrator */
394           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
395           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
396         }
398       }else{
399         msg_dialog::display(_("Error"), sprintf(_("Cannot delete '%s': object does not exist!"), $this->dn) , ERROR_DIALOG);
400       }
401       /* Remove lock file after successfull deletion */
402       del_lock ($this->dn);
403     }
406     /****************
407       Edit entry 
408      ****************/
410                 if(($s_action == "edit") && (!isset($this->dialog->config))){
411                         $entry    = $this->objects[$s_entry];
412                         $a_setup  = $this->get_type($entry);
413                         $this->dn = $entry['dn'];
415                         /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
416                         if (($user= get_lock($this->dn)) != ""){
417                                 return(gen_locked_message ($user, $this->dn));
418                         }
419                         add_lock ($this->dn, $this->ui->dn);
421                         $this->dialog     = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
423       $this->dialog->set_acl_base($this->dn);
424                         $this->is_dialog  = true;
426       if(preg_match("/^freeze/", $entry['FAIstate']) || $this->no_save){
427 #        $this->dialog->set_acl_base("freezed")  ;    
428       }
429                         session::set('objectinfo',$this->dn);
430                 }
433     /*  Branch handling 
434         09.01.2006
435     */
437     /****************
438       Remove branch
439      ****************/
441     /* Remove branch 
442      */
443     if($s_action == "remove_branch"){
444       $base= $this->DivListFai->selectedBranch;
446       /* Check if we have a post remove method configured
447        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
448        */
449       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
450         /* Load permissions for selected 'dn' and check if
451            we're allowed to remove this 'dn' */
452         if($this->acl_is_removeable()){
454           $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->DivListFai->selectedBranch),_("FAI branch/freeze")));
455           return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
456         } else {
457           msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
458         }
459       }
460     }
462     
463     /****************
464       Remove branch confirmed
465      ****************/
467     if(isset($_POST['delete_branch_confirm'])){
469       /* Check if we have a post remove method configured
470        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
471        */
472       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
474         $bb =  $this->DivListFai->selectedBranch;
475         if(!isset($ldap)){
476           $ldap = $this->config->get_ldap_link();
477         }
479         $br = $this->getBranches();
481         if(isset($br[$bb]) && $this->acl_is_removeable()){
482           $name = $br[$bb];
483           $ldap->cd($bb);
484           $ldap->recursive_remove();
485           $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('applicationou'), $bb));
486           $ldap->recursive_remove();
487           $this->DivListFai->selectedBranch = "main";
489           /* Post remove */
490           $this->lock_name   = $name;
491           $this->lock_dn     = $bb;
492           $this->postremove();
494           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
495         }
496       }
497     }
500     /****************
501       Create a new branch "insert Name"
502      ****************/
504     if((isset($_POST['UseBranchName']))&&(($this->dispNewBranch)||($this->dispNewFreeze))){
505       session::set('LASTPOST',$_POST);
507       if($this->dispNewBranch){
508         $type = "branch";
509       }else{
510         $type = "freeze";
511       }
513       /* Check branch name */
514       $name = $_POST['BranchName'];
515       $is_ok = true;
516       $smarty->assign("BranchName",$name);
517       $base= get_ou('faiou').$this->DivListFai->selectedBase;
519       /* Check used characters */
520       if(preg_match("/[^0-9a-z \ö\ä\ü\.-_:,]/i",$name)){
521         msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z \ö\ä\ü\.-_:,]/i"), ERROR_DIALOG);
522         $is_ok = false;
523       }
525       /* Check if this name is already in use */
526       if(!$this->CheckNewBranchName($_POST['BranchName'],$this->DivListFai->selectedBranch)){
527         msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
528         $is_ok = false;
529       }
531       if($is_ok){
533         if(session::is_set('LASTPOST')){
534           $LASTPOST = session::get('LASTPOST');
535         }else{
536           $LASTPOST = array();
537         }
538         $LASTPOST['base'] = $base;
539         $LASTPOST['type'] = $type;
540         session::set('LASTPOST',$LASTPOST);
541         $smarty->assign("iframe", true);
542         $smarty->assign("plugID", $_GET['plug']);
543         $display        = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
544         return($display);
545       }
546     }
549     /****************
550       Create a new branch 
551      ****************/
553     if(isset($_GET['PerformBranch'])){
554     
555       if(!$this->acl_is_createable()){
556         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
557       }else{
559         /* Create it know */
560         $this->dispNewBranch = false;
561         $this->dispNewFreeze = false;
563         $LASTPOST = session::get('LASTPOST');
564         $base = $LASTPOST['base'];
565         $_POST  = session::get('LASTPOST');      
566         $name = $_POST['BranchName'];
568         $type = $LASTPOST['type'];
569         $ldap = $this->config->get_ldap_link();
571         $baseToUse = $base;
572         if($this->DivListFai->selectedBranch != "main" ){
573           $baseToUse = $this->DivListFai->selectedBranch;
574         }
576         /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
578         $CurrentReleases  = $this->getBranches();
579         $NewReleaseName   = $name;
580         if(isset($CurrentReleases[$this->DivListFai->selectedBranch])) {
581           if($this->DivListFai->selectedBranch != "main"){
582             $NewReleaseName = $CurrentReleases[$this->DivListFai->selectedBranch]."/".$name;
583             $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName); 
584           }else{
585             $NewReleaseName   = $name;
586           }
587         }
589         $appsrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),$baseToUse); 
590         $appdst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),"ou=".$name.",".$baseToUse) ; 
592         $mimesrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),$baseToUse); 
593         $mimedst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),"ou=".$name.",".$baseToUse) ; 
595         /* Check if source depeartments exist */
596         foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
597           $ldap->cd($this->config->current['BASE']);
598           $ldap->cat($dep);
599           if(!$ldap->count()){
600             $ldap->create_missing_trees($dep);
601           }
602         }
604         /* Print header to have styles included */
605         echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
606           <html>
607           <head>
608           <title></title>
609           <style type="text/css">@import url("themes/default/style.css");</style>
610           <script language="javascript" src="include/focus.js" type="text/javascript"></script>
611           </head>
612           <body style="background: none;margin:3px;color:black">
613           ';
615         new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
617         /* Duplicate group application releases 
618          */
619         FAI::copy_FAI_group_releases($CurrentReleases[$this->DivListFai->selectedBranch],$name,$type);
621         /* Duplicate applications 
622          */
623         $ldap->cat($appsrc,array("dn")) ;
624         if($ldap->count()){
625           $ldap->cd ($appdst);
626           $ldap->recursive_remove();
627           FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
628         }
630         /* Duplicate mime types 
631          */
632         $ldap->cat($mimesrc,array("dn")) ;
633         if($ldap->count()){
634           $ldap->cd ($mimedst);
635           $ldap->recursive_remove();
636           FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
637         }
639         $attr = array();
640         $attr['objectClass'] = array("organizationalUnit","FAIbranch");
641         $attr['ou']       = $name;
642         $attr['FAIstate'] = $type;
643         $ldap->cd($this->config->current['BASE']);
644         $ldap->cd("ou=".$name.",".$baseToUse);
645         $ldap->cat("ou=".$name.",".$baseToUse);
646         if($ldap->count()){
647           $ldap->modify($attr);
648         }else{
649           $ldap->add($attr);
650         }
652         /* Duplicate fai objects 
653          */
654         //      $ldap->cd ("ou=".$name.",".$baseToUse);
655         //      $ldap->recursive_remove();
656         //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
658         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
659           <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
660           </form></div>";
662         echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
664         /* Print footer to have valid html */
665         echo "</body></html>";
667         $this->dispNewFreeze = false; 
669         /* Postcreate */ 
671         /* Assign possible attributes */
672         $this->lock_type  = $type; 
673         $this->lock_name  = $name; 
674         $this->lock_dn    = $baseToUse;
675         $this->postcreate();
676         exit();
677       }
678     }
680     /****************
681       Display dialog to enter new Branch name
682      ****************/
684     /* Check if we have a post create method configured
685      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
686      */
687     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
688       if(($s_action == "branch_branch")||($this->dispNewBranch)){
689         if(!$this->acl_is_createable()){
690         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
691         }else{
692           $this->dispNewBranch=true;
693           $smarty->assign("iframe",false);
694           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
695           return($display);
696         }
697       } 
698     }
700  
701     /****************
702       Display dialog to enter new Freeze name
703      ****************/
705     /* Check if we have a post create method configured
706      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
707      */
708     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
709       if(($s_action == "freeze_branch")||($this->dispNewFreeze)){
710         if(!$this->acl_is_createable()){
711         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
712         }else{
713           $this->dispNewFreeze = true;
714           $smarty->assign("iframe",false);
715           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
716           return($display);
717         }
718       }
719     }
722     /****************
723       Create a new object 
724      ****************/
726     $types = array( "new_partition"     =>  "FAIpartitionTable",
727                     "new_script"        =>  "FAIscript",
728                     "new_hook"          =>  "FAIhook",
729                     "new_variable"      =>  "FAIvariable",
730                     "new_template"      =>  "FAItemplate",
731                     "new_package"       =>  "FAIpackageList");
732     $types_i18n = array( "new_partition"     =>  _("partition table"),
733                     "new_script"        =>  _("script"),
734                     "new_hook"          =>  _("hook"),
735                     "new_variable"      =>  _("variable"),
736                     "new_template"      =>  _("template"),
737                     "new_package"       =>  _("package list"));
739     if(isset($types[$s_action])){
740       $acl = $this->ui->get_permissions($this->DivListFai->selectedBase,"fai/".$type_acl_mapping[$types[$s_action]]);
741       if(preg_match("/c/",$acl)){
742         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,$types[$s_action]);
743       }else{
744         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$s_action]), ERROR_DIALOG);
745       }
746     }
748     /* New Profile */
749     if($s_action == "new_profile"){
750       $this->dn = "new" ;
752       $acl = $this->ui->get_permissions($this->DivListFai->selectedBase,"fai/faiProfile");
753       if(preg_match("/c/",$acl)){
754         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
755         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
756         $this->dialog->set_acl_base($this->base);
758         $this->is_dialog = false;
759       }else{
760         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
761       }
762     }
765     /****************
766       Get from ask class name dialog 
767      ****************/
769     if($s_action == "select_class_name_finished"){
770       $this->dialog->save_object();
771       if(count($this->dialog->check())!=0){
772         foreach($this->dialog->check() as $msg){
773           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
774         }               
775       }elseif(isset($this->dialog->objectClass)){
776         $this->dn = "new" ;
777         $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
778         $name = $this->dialog->save();
780         if(class_exists($a_setup[0])){
781           $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
782           $this->dialog->set_acl_base($this->base);
783           $this->dialog->by_object[$a_setup[1]]->cn = $name;
784           $this->is_dialog = true;
785         }
786       }         
787     }   
790     /****************
791      Cancel dialogs 
792      ****************/
794                 if(isset($_POST['edit_cancel'])){
795                         unset($this->dialog);
796                         $this->dialog=FALSE;
797                         $this->is_dialog = false;
798                         session::un_set('objectinfo');
799                         del_lock ($this->dn);
800                 }
803     /****************
804       Save sub dialogs 
805      ****************/
807                 /* This check if the given tab could be saved 
808                  * If it was possible to save it, remove dialog object. 
809                  * If it wasn't possible, show errors and keep dialog.
810                  */
811                 if((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->dialog->config))){
812                         $this->dialog->save_object();
813       $msgs= $this->dialog->check();
814                         if(count($msgs)!=0){
815                                 foreach($msgs as $msg){
816           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
817                                 }
818                         }else{
819                                 $this->dialog->save();
820         FAI::save_release_changes_now();
821         if (!isset($_POST['edit_apply'])){
822           del_lock ($this->dn);
823           unset($this->dialog);
824           $this->dialog=FALSE;
825           $this->is_dialog=false;
826           session::un_set('objectinfo');
827         }
828                         }
829                 }
832     /****************
833       Display currently open dialog 
834      ****************/
836                 /* If dialog is set, but $this->is_dialog==false, then 
837                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
838                  * If is_dialog == true, we are currently editing tab objects.
839                  *  Here we need both, save and cancel
840                  */ 
842                 if(is_object($this->dialog)){
843                         $display .= $this->dialog->execute();
844                         /* Don't show buttons if tab dialog requests this */
846       if(isset($this->dialog->current)){
848         $obj = $this->dialog->by_object[$this->dialog->current];
850         if((isset($obj->is_dialog) && (!$obj->is_dialog)) || (isset($obj->dialog) && (!$obj->dialog))){
852           $display.= "<p style=\"text-align:right\">\n";
853           if(!$this->no_save){
854             $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
855             $display.= "&nbsp;\n";
856             if ($this->dn != "new"){
857               $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
858               $display.= "&nbsp;\n";
859             }
860           }
861           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
862           $display.= "</p>";
863         }elseif(!isset($this->dialog->current)){
864           $display.= "<p style=\"text-align:right\">\n";
865           $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
866           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
867           $display.= "</p>";
868         }
869       }else{
870         $display.= "<p style=\"text-align:right\">\n";
871         $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
872         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
873         $display.= "</p>";
875       }
876       return($display);
877                 }
878                 
880     /****************
881       Dialog display
882      ****************/
884     /* Check if there is a snapshot dialog open */
885     $base = $this->DivListFai->selectedBase;
886     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
887       return($str);
888     }
890     /* Display dialog with system list */
891     $this->reload();
892     $this->DivListFai->parent = $this;
893     $this->DivListFai->execute();
894     $this->DivListFai->AddDepartments($this->DivListFai->selectedBase,4,1);
895     $this->DivListFai->setEntries($this->objects);
896     return($this->DivListFai->Draw());
897         }
900   /* Return departments, that will be included within snapshot detection */
901   function get_used_snapshot_bases()
902   {
903     $tmp = array();
904     $types = array("faipartitionou","faiscriptou","faitemplateou","faihookou","faiprofileou","faivariableou","faipackageou");
905     foreach($types as $type){
906       if($this->DivListFai->selectedBranch == "main"){
907         $tmp[] = get_ou($type).get_ou('faiou').$this->DivListFai->selectedBase;
908       }else{
909         $tmp[] = get_ou($type).$this->DivListFai->selectedBranch;
910       }
911     }
912     return($tmp);
913   }
916   /* Get available branches for current base */
917   function getBranches($base = false,$prefix = "")
918   {
919     $ret = array("/"=>"main");
920     $ldap = $this->config->get_ldap_link();
921     if(!$base){
922       $base = session::get('CurrentMainBase');
923     }
924     $tmp = FAI::get_all_releases_from_base($base,true);
925     foreach($tmp as $dn => $name){
926       $ret[$name]=$dn;
927     }
928     ksort($ret);
929     $ret = array_flip($ret);
931     return ($ret);
932   }
933   
935   function list_get_selected_items()
936   {
937     $ids = array();
938     foreach($_POST as $name => $value){
939       if(preg_match("/^item_selected_[0-9]*$/",$name)){
940         $id   = preg_replace("/^item_selected_/","",$name);
941         $ids[$id] = $id;
942       }
943     }
944     return($ids);
945   }
948   /* reload list of objects */
949   function reload()
950   {
951     /* Variable initialisation */
952     $str            = "";
953     $Regex          = $this->DivListFai->Regex;
954     $this->objects  = array();
956     /* Get base */
957     $base = get_ou('faiou').$this->DivListFai->selectedBase;
958     if($this->DivListFai->selectedBranch != "main"){
959       $br = $this->getBranches();
960       if(isset($br[$this->DivListFai->selectedBranch])){
961         $base = $this->DivListFai->selectedBranch;
962       }else{
963         $base = get_ou('faiou').$this->DivListFai->selectedBase;
964       }
965     }
966     $this->base = $base;
967     $this->set_acl_base($this->base);
969     $this->lock_type = FAI::get_release_tag(FAI::get_release_dn($base));
971     /* Create a new list of FAI object 
972      * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
973      */
974     $ObjectTypes = array(
975         "FAIpartitionTable"  => array("OU"=> get_ou('faipartitionou') , "CHKBOX"=>"ShowPartitions"  ,"ACL" => "faiPartitionTable"),
976         "FAIpackageList"     => array("OU"=> get_ou('faipackageou')   , "CHKBOX"=>"ShowPackages"    ,"ACL" => "faiPackage"),
977         "FAIscript"          => array("OU"=> get_ou('faiscriptou')    , "CHKBOX"=>"ShowScripts"     ,"ACL" => "faiScript"),
978         "FAIvariable"        => array("OU"=> get_ou('faivariableou')  , "CHKBOX"=>"ShowVariables"   ,"ACL" => "faiVariable"),
979         "FAIhook"            => array("OU"=> get_ou('faihookou')      , "CHKBOX"=>"ShowHooks"       ,"ACL" => "faiHook"),
980         "FAIprofile"         => array("OU"=> get_ou('faiprofileou')   , "CHKBOX"=>"ShowProfiles"    ,"ACL" => "faiProfile"),
981         "FAItemplate"        => array("OU"=> get_ou('faitemplateou')  , "CHKBOX"=>"ShowTemplates"   ,"ACL" => "faiTemplate"));
983     $filter = "";
984     foreach($ObjectTypes as $key => $data){
985       if($this->DivListFai->$data['CHKBOX']){
986         $filter.= "(objectClass=".$key.")";
987       }
988     }
989     $filter = "(&(|".$filter.")(cn=$Regex))";
990     
991     /* Get resolved release dependencies */
992     $tmp = FAI::get_all_objects_for_given_base($base,$filter);
994     /* Ge listed ldap objects */
995     $ldap = $this->config->get_ldap_link();
996     $ldap->cd($this->config->current['BASE']);
998     /* Get release tag 
999        If current release is freezed, all objects are freezed to.
1000      */
1001     $ldap->cat($base);
1002     $release_attrs = $ldap->fetch();
1003     $force_freezed = FALSE;
1004     if(isset($release_attrs['FAIstate'][0]) && preg_match("/freeze/i",$release_attrs['FAIstate'][0])){
1005       $force_freezed = TRUE;
1006     }
1008     foreach($tmp as $entry){
1010       /* Get some more informations about the object */ 
1011       $ldap->cat($entry['dn'], array("cn","description","objectClass","FAIclass","FAIstate","objectClass"));
1012       $object  = $ldap->fetch();
1014       /* Walk through possible types */
1015       foreach($ObjectTypes as $type => $rest){  
1017         $acl = $this->ui->get_permissions($object['dn'],"fai/".$rest ['ACL']);
1019         if(in_array($type,$object['objectClass']) && preg_match("/r/",$acl)){
1021           /* Prepare object */
1022           unset($object['objectClass']['count']);
1023           if(!isset($object['description'][0])){
1024             $object['description'][0]="";
1025           }
1027           /* Clean up object informations */
1028           $obj                  = array();
1029           $obj['cn']                          = $object['cn'][0];
1030           $obj['dn']                          = $object['dn'];
1031           $obj['acl']                       = $acl;
1032           $obj['description']   = $object['description'][0];
1033           $obj['objectClass']   = $object['objectClass'];
1035           /* Append type to this string, to be able to check if the selected 
1036            * entry is of type 'freeze' or 'branch'
1037            */
1038           if(!isset($object['FAIstate'])){
1039             $obj['FAIstate'] = $this->lock_type;
1040           }else{
1041             $obj['FAIstate'] = $object['FAIstate'][0]; 
1042           }
1044           if($force_freezed){
1045             $obj['FAIstate'] = "freeze";
1046           }
1048           $this->objects[strtolower($obj['cn']).$obj['cn'].$type] = $obj;
1049           $this->objects[strtolower($obj['cn']).$obj['cn'].$type]['type']=$type;
1050         }
1051                         }
1052                 }
1054                 ksort($this->objects);
1055                 reset ($this->objects);
1056         
1057                 /* use numeric index, thats a bit more secure */        
1058                 $tmp0 = array();
1059                 foreach($this->objects as $obj){
1060                         $tmp0[]= $obj;
1061                 }
1062                 $this->objects = array();
1063                 $this->objects = $tmp0;
1064         }
1066         function remove_lock()
1067         {
1068                 if (isset($this->dn)){
1069                         del_lock ($this->dn);
1070                 }
1071         }
1073         function get_type($array){
1074                 if(in_array("FAIpartitionTable",$array['objectClass'])){
1075                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
1076                 }
1077                 if(in_array("FAIscript",$array['objectClass'])){
1078                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
1079                 }
1080                 if(in_array("FAItemplate",$array['objectClass'])){
1081                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
1082                 }
1083                 if(in_array("FAIhook",$array['objectClass'])){
1084                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
1085                 }
1086                 if(in_array("FAIvariable",$array['objectClass'])){
1087                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
1088                 }
1089                 if(in_array("FAIprofile",$array['objectClass'])){
1090                         return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
1091                 }
1092                 
1093                 if(in_array("FAIpackageList",$array['objectClass'])){
1094                         return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
1095                 }
1096         }
1098   function CheckNewBranchName($name,$base)
1099   {
1100     $f = $this->DivListFai->selectedBranch;
1101     if($name == ""){
1102       return(false);
1103     }elseif(in_array($name,$this->getBranches($f))) {
1104       return(false);
1105     }elseif(tests::is_department_name_reserved($name,$base)){
1106       return(false);
1107     }
1108     return(true);
1109   }
1111   function save_object()
1112   {
1113     $this->DivListFai->save_object();
1115     if(is_object($this->CopyPasteHandler)){
1116       $this->CopyPasteHandler->save_object();
1117     }
1118   }
1121   function copyPasteHandling_from_queue($s_action,$s_entry)
1122   {
1123     /* Check if Copy & Paste is disabled */
1124     if(!is_object($this->CopyPasteHandler)){
1125       return("");
1126     }
1128     $base = $this->DivListFai->selectedBranch;
1129     if($base == "main"){
1130       $base = $this->DivListFai->selectedBase;
1131     }
1133     /* Add a single entry to queue */
1134     if($s_action == "cut" || $s_action == "copy"){
1136       /* Cleanup object queue */
1137       $this->CopyPasteHandler->cleanup_queue();
1138       $entry    = $this->objects[$s_entry];
1139       $a_setup  = $this->get_type($entry);
1140       $dn = $entry['dn'];
1141       $this->CopyPasteHandler->add_to_queue($dn,$s_action,$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1142     }
1144     /* Add entries to queue */
1145     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1147       /* Cleanup object queue */
1148       $this->CopyPasteHandler->cleanup_queue();
1150       /* Add new entries to CP queue */
1151       foreach($this->list_get_selected_items() as $id){
1153         /* Cleanup object queue */
1154         $entry    = $this->objects[$id];
1155         $a_setup  = $this->get_type($entry);
1156         $dn = $entry['dn'];
1159         if($s_action == "copy_multiple"){
1160           $this->CopyPasteHandler->add_to_queue($dn,"copy",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1161         }
1162         if($s_action == "cut_multiple"){
1163           $this->CopyPasteHandler->add_to_queue($dn,"cut",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1164         }
1165       }
1166     }
1168     /* Start pasting entries */
1169     if($s_action == "editPaste" && !($this->lock_type == "freeze" && !$this->allow_freeze_object_attach)){
1170       $this->start_pasting_copied_objects = TRUE;
1171     }
1173     /* Return C&P dialog */
1174     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1176       /* Get dialog */
1177       $data = $this->CopyPasteHandler->execute();
1178       $this->CopyPasteHandler->SetVar("base",$base);
1180       FAI::save_release_changes_now();
1182       /* Return dialog data */
1183       if(!empty($data)){
1184         return($data);
1185       }
1186     }
1188     /* Automatically disable status for pasting */
1189     if(!$this->CopyPasteHandler->entries_queued()){
1190       $this->start_pasting_copied_objects = FALSE;
1191     }
1192     return("");
1193   }
1196   /* Check if the given FAI class is used in this release 
1197    */
1198   static function check_class_name($oc,$name,$dn)
1199   {
1200     $base = FAI::get_release_dn($dn);
1201     $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
1202     $delete = array();
1203     $used   = array();
1204     foreach($res as $object){
1205       $used[$object['cn'][0]]= $object['cn'][0];
1206     }
1207     return($used);
1208   }
1211   /* Return plugin informations for acl handling */ 
1212   static function plInfo()
1213   {
1214     return (array( 
1215           "plShortName"   => _("FAI releases"),
1216           "plDescription" => _("FAI release management"),
1217           "plSelfModify"  => FALSE,
1218           "plDepends"     => array(),
1219           "plPriority"    => 0,
1220           "plSection"     => array("administration"),           
1221           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1222                                                  "objectClass" => "FAIclass")),
1223           "plProvidedAcls"=> array()));
1224   }
1226 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1227 ?>