Code

Group application / FAI
[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                 = "FAI";
26         var $plDescription              = "Fully Automatic Installation - management";
28         /* CLI vars */
29         var $cli_summary                  = "Handling of FAI entries";
30         var $cli_description    = "This plugin represents a management tool\n
31                 which allows us to manage all needed attributes for fully automatic installations (FAI)";
32         var $cli_parameters             = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
34         /* Headpage attributes */
35   var $lock_type    = "";    // should be branch/freeze
36   var $lock_name    = "";
37   var $lock_dn      = "";  
39         /* attribute list for save action */
40         var $attributes         = array("lock_type","lock_name","lock_dn");     //      Attributes Managed by this plugin 
41         var $objectclasses= array();    //      ObjectClasses which the attributes are related to
42         var $dialog                             = array();      //      This object contains every dialog we have currently opened
44         var $objects                    = array();      //      This array contains all available objects shown in divlist
45         var $is_dialog          = false;
47   var $dispNewBranch= false;
48   var $dispNewFreeze= false;
50   var $DivListFai;
51   var $start_pasting_copied_objects = FALSE;
52   var $CopyPasteHandler = FALSE;
53   var $base = "";
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     
86     /* If an entry was locked, these vars will be stored in a session to allow direct edit */
87     session::set('LOCK_VARS_TO_USE',array("/^edit_entry$/","/^id$/","/^entry_edit_/","/^entry_delete_/","/^item_selected/","/^remove_multiple_fai_objects/"));
90     /****************
91       Handle posts 
92      ****************/
94                 /* Check ImageButton posts
95                  * Create new tab ich new_xx is posted
96                  */
97     $posts = array( "/remove_branch/"=>"remove_branch",    "/branch_branch/"=>"branch_branch",
98                     "/freeze_branch/"=>"freeze_branch",    "/create_partition/i"=>"new_partition",
99                     "/create_script/i"=>"new_script",      "/create_hook/i"=>"new_hook",
100                     "/create_variable/i"=>"new_variable",  "/create_template/i"=>"new_template",
101                     "/create_package/i"=>"new_package",    "/create_profile/i"=>"new_profile",
102                     "/edit_continue/"=>"select_class_name_finished",
103                     "/^multiple_copy_fai/" => "copy_multiple", 
104                     "/^multiple_cut_fai/" => "cut_multiple", 
105                     "/^copy/" => "copy",
106                     "/^cut/" => "cut",
107                     "/^remove_multiple_fai_objects/" => "del_multiple");
109                 foreach($_POST as $name => $value){
110       foreach($posts as $reg => $act ){
111         if(preg_match($reg,$name)){
112           $s_action = $act;
113           $s_entry = ltrim(preg_replace($reg,"",$name),"_");
114           $s_entry = preg_replace("/_.*$/","",$s_entry);
115         }
116       }
117                         if(preg_match("/^entry_edit_.*/",$name)){
118                                 $s_entry = preg_replace("/^entry_edit_/","",$name);
119                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
120                                 $s_action = "edit";
121                         }elseif(preg_match("/^entry_delete_.*/",$name)){
122                                 $s_entry = preg_replace("/^entry_delete_/","",$name);
123                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
124         $s_action = "delete";
125       }
126     }
128                 if(isset($_GET['edit_entry'])){
129                         $s_entry = $_GET['edit_entry'];
130                         $s_action = "edit";
131                 }
133     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch"){
134       $s_action = "freeze_branch";
135     }
136     if(isset($_GET['act']) && $_GET['act'] == "branch_branch"){
137       $s_action = "branch_branch";
138     }
139     if(isset($_GET['act']) && $_GET['act'] == "remove_branch"){
140       $s_action = "remove_branch";
141     }
142     
143     if((isset($_POST['CancelBranchName'])) || (isset($_POST['CloseIFrame']))){
144       $this->dispNewBranch = false;
145       $this->dispNewFreeze = false;
146     }
149     $type_acl_mapping = array(
150         "FAIpartitionTable"  => "faiPartitionTable", 
151         "FAIpackageList"     => "faiPackage",
152         "FAIscript"          => "faiScript",
153         "FAIvariable"        => "faiVariable",
154         "FAIhook"            => "faiHook",
155         "FAIprofile"         => "faiProfile",
156         "FAItemplate"        => "faiTemplate");
159     /* handle C&P from layers menu */
160     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
161       $s_action = "copy_multiple";
162     }
163     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
164       $s_action = "cut_multiple";
165     }
166     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
167       $s_action = "editPaste";
168     }
170     /* Create options */
171     if(isset($_POST['menu_action']) && preg_match("/^Create_/",$_POST['menu_action'])){
172       $s_action = "new_".preg_replace("/^Create_/","",$_POST['menu_action']);;
173       $s_entry  = preg_replace("/^Create_/","",$_POST['menu_action']);
174     }
176     /* handle remove from layers menu */
177     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
178       $s_action = "del_multiple";
179     }
181     /********************
182       Copy & Paste
183      ********************/
185     /* Display the copy & paste dialog, if it is currently open */
186     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
187     if($ret){
188       return($ret);
189     }
192     /****************
193       Delete confirme dialog 
194      ****************/
196                 if ($s_action=="delete"){
198       /* Get 'dn' from posted termlinst */
199       $this->dn= $this->objects[$s_entry]['dn'];
201                         /* Load permissions for selected 'dn' and check if
202                            we're allowed to remove this 'dn' */
203       $acl  = $this->ui->get_permissions($this->dn,"fai/".$type_acl_mapping[$this->objects[$s_entry]['type']]);
204       if(preg_match("/d/",$acl)){
206                                 /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
207                                 if (($user= get_lock($this->dn)) != ""){
208                                         return(gen_locked_message ($user, $this->dn));
209                                 }
211                                 /* Lock the current entry, so nobody will edit it during deletion */
212                                 add_lock ($this->dn, $this->ui->dn);
213                                 $smarty->assign("warning", sprintf(_("You're about to delete all information about the FAI class at '%s'."), @LDAP::fix($this->dn)));
214         $smarty->assign("multiple", false);
215                                 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
216                         } else {
218                                 /* Obviously the user isn't allowed to delete. Show message and
219                                    clean session. */
220                                 print_red (_("You are not allowed to delete this component!"));
221                         }
222                 }
225     /********************
226       Delete MULTIPLE entries requested, display confirm dialog
227      ********************/
229     if ($s_action=="del_multiple"){
230       $this->dns = array();
231       $ids = $this->list_get_selected_items();
233       if(count($ids)){
235         foreach($ids as $id){
236           $dn = $this->objects[$id]['dn'];
237           if (($user= get_lock($dn)) != ""){
238             return(gen_locked_message ($user, $dn));
239           }
240           $this->dns[$id] = $dn;
241         }
243         $dns_names = "<br><pre>";
244         foreach($this->dns as $dn){
245           add_lock ($dn, $this->ui->dn);
246           $dns_names .= $dn."\n";
247         }
248         $dns_names .="</pre>";
250         /* Lock the current entry, so nobody will edit it during deletion */
251         if (count($this->dns) == 1){
252           $smarty->assign("warning",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
253         } else {
254           $smarty->assign("warning",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
255         }
256         $smarty->assign("multiple", true);
257         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
258       }
259     }
262     /********************
263       Delete MULTIPLE entries confirmed
264      ********************/
266     /* Confirmation for deletion has been passed. Users should be deleted. */
267     if (isset($_POST['delete_multiple_fai_object_confirm'])){
269       /* Find out more about the object type */
270       $ldap   = $this->config->get_ldap_link();
272       /* Remove user by user and check acls before removeing them */
273       foreach($this->dns as $key => $dn){
275         $ldap->cat($dn, array('objectClass'));
276         $attrs  = $ldap->fetch();
277         $type   = $this->get_type($attrs);
279         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
280         if(preg_match("/d/",$acl)){
282           $this->dialog = new $type[0]($this->config, $this->config->data['TABS'][$type[2]], $dn,"fai");
283           $this->dialog->set_acl_base($dn);
284           $this->dialog->by_object[$type[1]]->remove_from_parent ();
285           unset ($this->dialog);
286           $this->dialog= FALSE;
287           $to_del = FAI::clean_up_releases($dn);
288           FAI::save_release_changes_now();
290           foreach($to_del as $dn){
291             $ldap->rmdir_recursive($dn);
292           }
294         } else {
296           /* Normally this shouldn't be reached, send some extra
297              logs to notify the administrator */
298           print_red (_("You are not allowed to delete this component!"));
299           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
300         }
302         /* Remove lock file after successfull deletion */
303         del_lock ($dn);
304         unset($this->dns[$key]);
305       }
306     }
309     /********************
310       Delete MULTIPLE entries Canceled
311      ********************/
313     /* Remove lock */
314     if(isset($_POST['delete_multiple_fai_object_cancel'])){
315       foreach($this->dns as $key => $dn){
316         del_lock ($dn);
317         unset($this->dns[$key]);
318       }
319     }
322     /****************
323       Delete aborted  
324      ****************/
326                 /* Delete canceled? */
327                 if (isset($_POST['delete_cancel'])){
328                         del_lock ($this->dn);
329                 }
332     /****************
333       Delete confirmed 
334      ****************/
336                 /* Deltetion was confirmed, so delete this entry
337      */
338     if (isset($_POST['delete_terminal_confirm'])){
340       /* Some nice guy may send this as POST, so we've to check
341          for the permissions again. */
343       /* Find out more about the object type */
344       $ldap       = $this->config->get_ldap_link();
345       $ldap->cat($this->dn, array('objectClass'));
346       if($ldap->count()){
347         $attrs  = $ldap->fetch();
348         $type     = $this->get_type($attrs);                    
350         $acl  = $this->ui->get_permissions($this->dn,"fai/".$type[1]);
351         if(preg_match("/d/",$acl)){
353           $this->dialog = new $type[0]($this->config,   $this->config->data['TABS'][$type[2]], $this->dn,"fai");
354           $this->dialog->set_acl_base($this->dn);
355           $this->dialog->by_object[$type[1]]->remove_from_parent ();
356           unset ($this->dialog);
357           $this->dialog= FALSE;
358           $to_del = FAI::clean_up_releases($this->dn);
359           FAI::save_release_changes_now();
361           foreach($to_del as $dn){
362             $ldap->rmdir_recursive($dn);
363           }
365         } else {
367           /* Normally this shouldn't be reached, send some extra
368              logs to notify the administrator */
369           print_red (_("You are not allowed to delete this component!"));
370           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
371         }
373       }else{
374         print_red(sprintf(_("Object could not be deleted '%s', object does not exist."),$this->dn));
375       }
376       /* Remove lock file after successfull deletion */
377       del_lock ($this->dn);
378     }
381     /****************
382       Edit entry 
383      ****************/
385                 if(($s_action == "edit") && (!isset($this->dialog->config))){
386                         $entry    = $this->objects[$s_entry];
387                         $a_setup  = $this->get_type($entry);
388                         $this->dn = $entry['dn'];
390                         /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
391                         if (($user= get_lock($this->dn)) != ""){
392                                 return(gen_locked_message ($user, $this->dn));
393                         }
394                         add_lock ($this->dn, $this->ui->dn);
396                         $this->dialog     = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
397       $this->dialog->set_acl_base($this->dn);
398                         $this->is_dialog  = true;
400       if($entry['FAIstate'] == "freeze"){
401         #$this->dialog->set_acl(array("*none*"))  ;    
402       }
403                         session::set('objectinfo',$this->dn);
404                 }
407     /*  Branch handling 
408         09.01.2006
409     */
411     /****************
412       Remove branch
413      ****************/
415     /* Remove branch 
416      */
417     if($s_action == "remove_branch"){
418       $base= $this->DivListFai->selectedBranch;
420       /* Check if we have a post remove method configured
421        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
422        */
423       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
424         /* Load permissions for selected 'dn' and check if
425            we're allowed to remove this 'dn' */
426         if($this->acl_is_removeable()){
428           $smarty->assign("info", sprintf(_("You're about to delete a fai branch / freeze  '%s'."), $this->DivListFai->selectedBranch));
429           return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
430         } else {
431           print_red (_("You are not allowed to delete this release!"));
432         }
433       }
434     }
436     
437     /****************
438       Remove branch confirmed
439      ****************/
441     if(isset($_POST['delete_branch_confirm'])){
443       /* Check if we have a post remove method configured
444        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
445        */
446       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
448         $bb =  $this->DivListFai->selectedBranch;
449         if(!isset($ldap)){
450           $ldap = $this->config->get_ldap_link();
451         }
453         $br = $this->getBranches();
455         if(isset($br[$bb]) && $this->acl_is_removeable()){
456           $name = $br[$bb];
457           $ldap->cd($bb);
458           $ldap->recursive_remove();
459           $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('applicationou'), $bb));
460           $ldap->recursive_remove();
461           $this->DivListFai->selectedBranch = "main";
463           /* Post remove */
464           $this->lock_name   = $name;
465           $this->lock_dn     = $bb;
466           $this->postremove();
468           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
469         }
470       }
471     }
474     /****************
475       Create a new branch "insert Name"
476      ****************/
478     if((isset($_POST['UseBranchName']))&&(($this->dispNewBranch)||($this->dispNewFreeze))){
479       session::set('LASTPOST',$_POST);
481       if($this->dispNewBranch){
482         $type = "branch";
483       }else{
484         $type = "freeze";
485       }
487       /* Check branch name */
488       $name = $_POST['BranchName'];
489       $is_ok = true;
490       $smarty->assign("BranchName",$name);
491       $base= get_ou('faiou').$this->DivListFai->selectedBase;
493       /* Check used characters */
494       if(preg_match("/[^0-9a-z \ö\ä\ü\.-_:,]/i",$name)){
495         if($type == "branch"){
496           print_red(_("Specified branch name is invalid."));
497         }else{
498           print_red(_("Specified freeze name is invalid."));
499         }
500         $is_ok = false;
501       }
503       /* Check if this name is already in use */
504       if(!$this->CheckNewBranchName($_POST['BranchName'],$this->DivListFai->selectedBranch)){
505         print_red(_("This name is already in use."));
506         $is_ok = false;
507       }
509       if($is_ok){
511         if(session::is_set('LASTPOST')){
512           $LASTPOST = session::get('LASTPOST');
513         }else{
514           $LASTPOST = array();
515         }
516         $LASTPOST['base'] = $base;
517         $LASTPOST['type'] = $type;
518         session::set('LASTPOST',$LASTPOST);
519         $smarty->assign("iframe", true);
520         $smarty->assign("plugID", $_GET['plug']);
521         $display        = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
522         return($display);
523       }
524     }
527     /****************
528       Create a new branch 
529      ****************/
531     if(isset($_GET['PerformBranch'])){
532     
533       if(!$this->acl_is_createable()){
534         print_red(_("You are not allowed to create a new branch."));
535       }else{
537         /* Create it know */
538         $this->dispNewBranch = false;
539         $this->dispNewFreeze = false;
541         $LASTPOST = session::get('LASTPOST');
542         $base = $LASTPOST['base'];
543         $_POST  = session::get('LASTPOST');      
544         $name = $_POST['BranchName'];
546         $type = $LASTPOST['type'];
547         $ldap = $this->config->get_ldap_link();
549         $baseToUse = $base;
550         if($this->DivListFai->selectedBranch != "main" ){
551           $baseToUse = $this->DivListFai->selectedBranch;
552         }
554         /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
556         $CurrentReleases  = $this->getBranches();
557         $NewReleaseName   = $name;
558         if(isset($CurrentReleases[$this->DivListFai->selectedBranch])) {
559           if($this->DivListFai->selectedBranch != "main"){
560             $NewReleaseName = $CurrentReleases[$this->DivListFai->selectedBranch]."/".$name;
561             $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName); 
562           }else{
563             $NewReleaseName   = $name;
564           }
565         }
567         $appsrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),$baseToUse); 
568         $appdst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),"ou=".$name.",".$baseToUse) ; 
570         $mimesrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),$baseToUse); 
571         $mimedst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),"ou=".$name.",".$baseToUse) ; 
573         /* Check if source depeartments exist */
574         foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
575           $ldap->cd($this->config->current['BASE']);
576           $ldap->cat($dep);
577           if(!$ldap->count()){
578             $ldap->create_missing_trees($dep);
579           }
580         }
582         /* Print header to have styles included */
583         echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
584           <html>
585           <head>
586           <title></title>
587           <style type="text/css">@import url("themes/default/style.css");</style>
588           <script language="javascript" src="include/focus.js" type="text/javascript"></script>
589           </head>
590           <body style="background: none;margin:3px;color:black">
591           ';
593         new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
595         /* Duplicate group application releases 
596          */
597         FAI::copy_FAI_group_releases($CurrentReleases[$this->DivListFai->selectedBranch],$name,$type);
599         /* Duplicate applications 
600          */
601         $ldap->cat($appsrc,array("dn")) ;
602         if($ldap->count()){
603           $ldap->cd ($appdst);
604           $ldap->recursive_remove();
605           FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
606         }
608         /* Duplicate mime types 
609          */
610         $ldap->cat($mimesrc,array("dn")) ;
611         if($ldap->count()){
612           $ldap->cd ($mimedst);
613           $ldap->recursive_remove();
614           FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
615         }
617         $attr = array();
618         $attr['objectClass'] = array("organizationalUnit","FAIbranch");
619         $attr['ou']       = $name;
620         $attr['FAIstate'] = $type;
621         $ldap->cd($this->config->current['BASE']);
622         $ldap->cd("ou=".$name.",".$baseToUse);
623         $ldap->cat("ou=".$name.",".$baseToUse);
624         if($ldap->count()){
625           $ldap->modify($attr);
626         }else{
627           $ldap->add($attr);
628         }
630         /* Duplicate fai objects 
631          */
632         //      $ldap->cd ("ou=".$name.",".$baseToUse);
633         //      $ldap->recursive_remove();
634         //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
636         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
637           <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
638           </form></div>";
640         echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
642         /* Print footer to have valid html */
643         echo "</body></html>";
645         $this->dispNewFreeze = false; 
647         /* Postcreate */ 
649         /* Assign possible attributes */
650         $this->lock_type  = $type; 
651         $this->lock_name  = $name; 
652         $this->lock_dn    = $baseToUse;
653         $this->postcreate();
654         exit();
655       }
656     }
658     /****************
659       Display dialog to enter new Branch name
660      ****************/
662     /* Check if we have a post create method configured
663      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
664      */
665     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
666       if(($s_action == "branch_branch")||($this->dispNewBranch)){
667         if(!$this->acl_is_createable()){
668           print_red(_("You are not allowed to create a new branch."));
669         }else{
670           $this->dispNewBranch=true;
671           $smarty->assign("iframe",false);
672           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
673           return($display);
674         }
675       } 
676     }
678  
679     /****************
680       Display dialog to enter new Freeze name
681      ****************/
683     /* Check if we have a post create method configured
684      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
685      */
686     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
687       if(($s_action == "freeze_branch")||($this->dispNewFreeze)){
688         if(!$this->acl_is_createable()){
689           print_red(_("You are not allowed to create a new branch."));
690         }else{
691           $this->dispNewFreeze = true;
692           $smarty->assign("iframe",false);
693           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
694           return($display);
695         }
696       }
697     }
700     /****************
701       Create a new object 
702      ****************/
704     $types = array( "new_partition"     =>  "FAIpartitionTable",
705                     "new_script"        =>  "FAIscript",
706                     "new_hook"          =>  "FAIhook",
707                     "new_variable"      =>  "FAIvariable",
708                     "new_template"      =>  "FAItemplate",
709                     "new_package"       =>  "FAIpackageList");
711     if(isset($types[$s_action])){
712       $acl = $this->ui->get_permissions($this->DivListFai->selectedBase,"fai/".$type_acl_mapping[$types[$s_action]]);
713       if(preg_match("/c/",$acl)){
714         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,$types[$s_action]);
715       }else{
716         print_red(sprintf(_("You are not allowed to create a new '%s' object."),$types[$s_action]));
717       }
718     }
720     /* New Profile */
721     if($s_action == "new_profile"){
722       $this->dn = "new" ;
724       $acl = $this->ui->get_permissions($this->DivListFai->selectedBase,"fai/faiProfile");
725       if(preg_match("/c/",$acl)){
726         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
727         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
728         $this->dialog->set_acl_base($this->base);
730         $this->is_dialog = false;
731       }else{
732         print_red(sprintf(_("You are not allowed to create a new '%s' object."),"FAIprofile"));
733       }
734     }
737     /****************
738       Get from ask class name dialog 
739      ****************/
741     if($s_action == "select_class_name_finished"){
742       $this->dialog->save_object();
743       if(count($this->dialog->check())!=0){
744         foreach($this->dialog->check() as $msg){
745           print_red($msg);
746         }               
747       }elseif(isset($this->dialog->objectClass)){
748         $this->dn = "new" ;
749         $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
750         $name = $this->dialog->save();
752         if(class_exists($a_setup[0])){
753           $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
754           $this->dialog->set_acl_base($this->base);
755           $this->dialog->by_object[$a_setup[1]]->cn = $name;
756           $this->is_dialog = true;
757         }
758       }         
759     }   
762     /****************
763      Cancel dialogs 
764      ****************/
766                 if(isset($_POST['edit_cancel'])){
767                         unset($this->dialog);
768                         $this->dialog=FALSE;
769                         $this->is_dialog = false;
770                         session::un_set('objectinfo');
771                         del_lock ($this->dn);
772                 }
775     /****************
776       Save sub dialogs 
777      ****************/
779                 /* This check if the given tab could be saved 
780                  * If it was possible to save it, remove dialog object. 
781                  * If it wasn't possible, show errors and keep dialog.
782                  */
783                 if((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->dialog->config))){
784                         $this->dialog->save_object();
785       $msgs= $this->dialog->check();
786                         if(count($msgs)!=0){
787                                 foreach($msgs as $msg){
788                                         print_red($msg);
789                                 }
790                         }else{
791                                 $this->dialog->save();
792         FAI::save_release_changes_now();
793         if (!isset($_POST['edit_apply'])){
794           del_lock ($this->dn);
795           unset($this->dialog);
796           $this->dialog=FALSE;
797           $this->is_dialog=false;
798           session::un_set('objectinfo');
799         }
800                         }
801                 }
804     /****************
805       Display currently open dialog 
806      ****************/
808                 /* If dialog is set, but $this->is_dialog==false, then 
809                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
810                  * If is_dialog == true, we are currently editing tab objects.
811                  *  Here we need both, save and cancel
812                  */ 
814                 if(is_object($this->dialog)){
815                         $display .= $this->dialog->execute();
816                         /* Don't show buttons if tab dialog requests this */
818       if(isset($this->dialog->current)){
820         $obj = $this->dialog->by_object[$this->dialog->current];
822         if((isset($obj->is_dialog) && (!$obj->is_dialog)) || (isset($obj->dialog) && (!$obj->dialog))){
824           $display.= "<p style=\"text-align:right\">\n";
825           $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
826           $display.= "&nbsp;\n";
827           if ($this->dn != "new"){
828             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
829             $display.= "&nbsp;\n";
830           }
831           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
832           $display.= "</p>";
833         }elseif(!isset($this->dialog->current)){
834           $display.= "<p style=\"text-align:right\">\n";
835           $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
836           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
837           $display.= "</p>";
838         }
839       }else{
840         $display.= "<p style=\"text-align:right\">\n";
841         $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
842         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
843         $display.= "</p>";
845       }
846       return($display);
847                 }
848                 
850     /****************
851       Dialog display
852      ****************/
854     /* Check if there is a snapshot dialog open */
855     $base = $this->DivListFai->selectedBase;
856     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
857       return($str);
858     }
860     /* Display dialog with system list */
861     $this->DivListFai->parent = $this;
862     $this->DivListFai->execute();
863     $this->DivListFai->AddDepartments($this->DivListFai->selectedBase,4,1);
864     $this->reload();
865     $this->DivListFai->setEntries($this->objects);
866     return($this->DivListFai->Draw());
867         }
870   /* Return departments, that will be included within snapshot detection */
871   function get_used_snapshot_bases()
872   {
873     $tmp = array();
874     $types = array("faipartitionou","faiscriptou","faitemplateou","faihookou","faiprofileou","faivariableou","faipackageou");
875     foreach($types as $type){
876       if($this->DivListFai->selectedBranch == "main"){
877         $tmp[] = get_ou($type).get_ou('faiou').$this->DivListFai->selectedBase;
878       }else{
879         $tmp[] = get_ou($type).$this->DivListFai->selectedBranch;
880       }
881     }
882     return($tmp);
883   }
886   /* Get available branches for current base */
887   function getBranches($base = false,$prefix = "")
888   {
889     $ret = array("/"=>"main");
890     $ldap = $this->config->get_ldap_link();
891     if(!$base){
892       $base = session::get('CurrentMainBase');
893     }
894     $tmp = FAI::get_all_releases_from_base($base,true);
895     foreach($tmp as $dn => $name){
896       $ret[$name]=$dn;
897     }
898     ksort($ret);
899     $ret = array_flip($ret);
901     return ($ret);
902   }
903   
905   function list_get_selected_items()
906   {
907     $ids = array();
908     foreach($_POST as $name => $value){
909       if(preg_match("/^item_selected_[0-9]*$/",$name)){
910         $id   = preg_replace("/^item_selected_/","",$name);
911         $ids[$id] = $id;
912       }
913     }
914     return($ids);
915   }
918   /* reload list of objects */
919   function reload()
920   {
921     /* Variable initialisation */
922     $str            = "";
923     $Regex          = $this->DivListFai->Regex;
924     $this->objects  = array();
926     /* Get base */
927     $base = get_ou('faiou').$this->DivListFai->selectedBase;
928     if($this->DivListFai->selectedBranch != "main"){
929       $br = $this->getBranches();
930       if(isset($br[$this->DivListFai->selectedBranch])){
931         $base = $this->DivListFai->selectedBranch;
932       }else{
933         $base = get_ou('faiou').$this->DivListFai->selectedBase;
934       }
935     }
936     $this->base = $base;
937     $this->set_acl_base($this->base);
939     $this->lock_type = FAI::get_release_tag(FAI::get_release_dn($base));
941     /* Create a new list of FAI object 
942      * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
943      */
944     $ObjectTypes = array(
945         "FAIpartitionTable"  => array("OU"=> get_ou('faipartitionou') , "CHKBOX"=>"ShowPartitions"  ,"ACL" => "faiPartitionTable"),
946         "FAIpackageList"     => array("OU"=> get_ou('faipackageou')   , "CHKBOX"=>"ShowPackages"    ,"ACL" => "faiPackage"),
947         "FAIscript"          => array("OU"=> get_ou('faiscriptou')    , "CHKBOX"=>"ShowScripts"     ,"ACL" => "faiScript"),
948         "FAIvariable"        => array("OU"=> get_ou('faivariableou')  , "CHKBOX"=>"ShowVariables"   ,"ACL" => "faiVariable"),
949         "FAIhook"            => array("OU"=> get_ou('faihookou')      , "CHKBOX"=>"ShowHooks"       ,"ACL" => "faiHook"),
950         "FAIprofile"         => array("OU"=> get_ou('faiprofileou')   , "CHKBOX"=>"ShowProfiles"    ,"ACL" => "faiProfile"),
951         "FAItemplate"        => array("OU"=> get_ou('faitemplateou')  , "CHKBOX"=>"ShowTemplates"   ,"ACL" => "faiTemplate"));
953     $filter = "";
954     foreach($ObjectTypes as $key => $data){
955       if($this->DivListFai->$data['CHKBOX']){
956         $filter.= "(objectClass=".$key.")";
957       }
958     }
959     $filter = "(&(|".$filter.")(cn=$Regex))";
960     
961     /* Get resolved release dependencies */
962     $tmp = FAI::get_all_objects_for_given_base($base,$filter);
964     /* Ge listed ldap objects */
965     $ldap = $this->config->get_ldap_link();
966     $ldap->cd($this->config->current['BASE']);
967     foreach($tmp as $entry){
969       /* Get some more informations about the object */ 
970       $ldap->cat($entry['dn'], array("cn","description","objectClass","FAIclass","FAIstate","objectClass"));
971       $object  = $ldap->fetch();
973       /* Walk through possible types */
974       foreach($ObjectTypes as $type => $rest){  
976         $acl = $this->ui->get_permissions($object['dn'],"fai/".$rest ['ACL']);
978         if(in_array($type,$object['objectClass']) && preg_match("/r/",$acl)){
980           /* Prepare object */
981           unset($object['objectClass']['count']);
982           if(!isset($object['description'][0])){
983             $object['description'][0]="";
984           }
986           /* Clean up object informations */
987           $obj                  = array();
988           $obj['cn']                          = $object['cn'][0];
989           $obj['dn']                          = $object['dn'];
990           $obj['acl']                       = $acl;
991           $obj['description']   = $object['description'][0];
992           $obj['objectClass']   = $object['objectClass'];
994           /* Append type to this string, to be able to check if the selected 
995            * entry is of type 'freeze' or 'branch'
996            */
997           if(!isset($object['FAIstate'])){
998             $obj['FAIstate'] = $this->lock_type;
999           }else{
1000             $obj['FAIstate'] = $object['FAIstate'][0]; 
1001           }
1003           $this->objects[strtolower($obj['cn']).$obj['cn'].$type] = $obj;
1004           $this->objects[strtolower($obj['cn']).$obj['cn'].$type]['type']=$type;
1005         }
1006                         }
1007                 }
1009                 ksort($this->objects);
1010                 reset ($this->objects);
1011         
1012                 /* use numeric index, thats a bit more secure */        
1013                 $tmp0 = array();
1014                 foreach($this->objects as $obj){
1015                         $tmp0[]= $obj;
1016                 }
1017                 $this->objects = array();
1018                 $this->objects = $tmp0;
1019         }
1021         function remove_lock()
1022         {
1023                 if (isset($this->dn)){
1024                         del_lock ($this->dn);
1025                 }
1026         }
1028         function get_type($array){
1029                 if(in_array("FAIpartitionTable",$array['objectClass'])){
1030                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
1031                 }
1032                 if(in_array("FAIscript",$array['objectClass'])){
1033                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
1034                 }
1035                 if(in_array("FAItemplate",$array['objectClass'])){
1036                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
1037                 }
1038                 if(in_array("FAIhook",$array['objectClass'])){
1039                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
1040                 }
1041                 if(in_array("FAIvariable",$array['objectClass'])){
1042                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
1043                 }
1044                 if(in_array("FAIprofile",$array['objectClass'])){
1045                         return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
1046                 }
1047                 
1048                 if(in_array("FAIpackageList",$array['objectClass'])){
1049                         return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
1050                 }
1051         }
1053   function CheckNewBranchName($name,$base)
1054   {
1055     $f = $this->DivListFai->selectedBranch;
1056     if($name == ""){
1057       return(false);
1058     }elseif(in_array($name,$this->getBranches($f))) {
1059       return(false);
1060     }elseif(tests::is_department_name_reserved($name,$base)){
1061       return(false);
1062     }
1063     return(true);
1064   }
1066   function save_object()
1067   {
1068     $this->DivListFai->save_object();
1069   }
1072   function copyPasteHandling_from_queue($s_action,$s_entry)
1073   {
1074     /* Check if Copy & Paste is disabled */
1075     if(!is_object($this->CopyPasteHandler)){
1076       return("");
1077     }
1079     $base = $this->DivListFai->selectedBranch;
1080     if($base == "main"){
1081       $base = $this->DivListFai->selectedBase;
1082     }
1084     /* Add a single entry to queue */
1085     if($s_action == "cut" || $s_action == "copy"){
1087       /* Cleanup object queue */
1088       $this->CopyPasteHandler->cleanup_queue();
1089       $entry    = $this->objects[$s_entry];
1090       $a_setup  = $this->get_type($entry);
1091       $dn = $entry['dn'];
1092       $this->CopyPasteHandler->add_to_queue($dn,$s_action,$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1093     }
1095     /* Add entries to queue */
1096     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1098       /* Cleanup object queue */
1099       $this->CopyPasteHandler->cleanup_queue();
1101       /* Add new entries to CP queue */
1102       foreach($this->list_get_selected_items() as $id){
1104         /* Cleanup object queue */
1105         $entry    = $this->objects[$id];
1106         $a_setup  = $this->get_type($entry);
1107         $dn = $entry['dn'];
1110         if($s_action == "copy_multiple"){
1111           $this->CopyPasteHandler->add_to_queue($dn,"copy",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1112         }
1113         if($s_action == "cut_multiple"){
1114           $this->CopyPasteHandler->add_to_queue($dn,"cut",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1115         }
1116       }
1117     }
1119     /* Start pasting entries */
1120     if($s_action == "editPaste"){
1121       $this->start_pasting_copied_objects = TRUE;
1122     }
1124     /* Return C&P dialog */
1125     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1127       /* Load entry from queue and set base */
1128       $this->CopyPasteHandler->load_entry_from_queue();
1129       $this->CopyPasteHandler->SetVar("base",$base);
1131       /* Get dialog */
1132       $data = $this->CopyPasteHandler->execute();
1134       FAI::save_release_changes_now();
1136       /* Return dialog data */
1137       if(!empty($data)){
1138         return($data);
1139       }
1140     }
1142     /* Automatically disable status for pasting */
1143     if(!$this->CopyPasteHandler->entries_queued()){
1144       $this->start_pasting_copied_objects = FALSE;
1145     }
1146     return("");
1147   }
1150   /* Return plugin informations for acl handling */ 
1151   static function plInfo()
1152   {
1153     return (array( 
1154           "plShortName"   => _("FAI releases"),
1155           "plDescription" => _("FAI release management"),
1156           "plSelfModify"  => FALSE,
1157           "plDepends"     => array(),
1158           "plPriority"    => 0,
1159           "plSection"     => array("administration"),           
1160           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1161                                                  "objectClass" => "FAIclass")),
1162           "plProvidedAcls"=> array()));
1163   }
1165 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1166 ?>