Code

Fixed FAI management.
[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/","/^menu_action/"));
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         $errors = "";
236         foreach($ids as $id){
237           $dn = $this->objects[$id]['dn'];
238           $cn = $this->objects[$id]['cn'];
239           if($this->objects[$id]['FAIstate'] != "freeze"){
240             $this->dns[$id] = $dn;
241           }else{
242             $errors .= $cn.", ";
243           }
244         }
245         if ($user= get_multiple_locks($this->dns)){
246           return(gen_locked_message($user,$this->dns));
247         }
249         if($errors != ""){
250           msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
251             "<br><br>".trim($errors,", ")),INFO_DIALOG);
252         }
254         if(count($this->dns)){
256           $dns_names = "<br><pre>";
257           foreach($this->dns as $dn){
258             add_lock ($dn, $this->ui->dn);
259             $dns_names .= $dn."\n";
260           }
261           $dns_names .="</pre>";
263           /* Lock the current entry, so nobody will edit it during deletion */
264           if (count($this->dns) == 1){
265             $smarty->assign("warning",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
266           } else {
267             $smarty->assign("warning",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
268           }
269           $smarty->assign("multiple", true);
270           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
271         }
272       }
273     }
276     /********************
277       Delete MULTIPLE entries confirmed
278      ********************/
280     /* Confirmation for deletion has been passed. Users should be deleted. */
281     if (isset($_POST['delete_multiple_fai_object_confirm'])){
283       /* Find out more about the object type */
284       $ldap   = $this->config->get_ldap_link();
286       /* Remove user by user and check acls before removeing them */
287       foreach($this->dns as $key => $dn){
289         $ldap->cat($dn, array('objectClass'));
290         $attrs  = $ldap->fetch();
291         $type   = $this->get_type($attrs);
293         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
294         if(preg_match("/d/",$acl)){
296           $this->dialog = new $type[0]($this->config, $this->config->data['TABS'][$type[2]], $dn,"fai");
297           $this->dialog->set_acl_base($dn);
298           $this->dialog->by_object[$type[1]]->remove_from_parent ();
299           unset ($this->dialog);
300           $this->dialog= FALSE;
301           $to_del = FAI::clean_up_releases($dn);
302           FAI::save_release_changes_now();
304           foreach($to_del as $dn){
305             $ldap->rmdir_recursive($dn);
306           }
308         } else {
310           /* Normally this shouldn't be reached, send some extra
311              logs to notify the administrator */
312           print_red (_("You are not allowed to delete this component!"));
313           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
314         }
316         /* Remove lock file after successfull deletion */
317         del_lock ($dn);
318         unset($this->dns[$key]);
319       }
320     }
323     /********************
324       Delete MULTIPLE entries Canceled
325      ********************/
327     /* Remove lock */
328     if(isset($_POST['delete_multiple_fai_object_cancel'])){
329       foreach($this->dns as $key => $dn){
330         del_lock ($dn);
331         unset($this->dns[$key]);
332       }
333     }
336     /****************
337       Delete aborted  
338      ****************/
340                 /* Delete canceled? */
341                 if (isset($_POST['delete_cancel'])){
342                         del_lock ($this->dn);
343                 }
346     /****************
347       Delete confirmed 
348      ****************/
350                 /* Deltetion was confirmed, so delete this entry
351      */
352     if (isset($_POST['delete_terminal_confirm'])){
354       /* Some nice guy may send this as POST, so we've to check
355          for the permissions again. */
357       /* Find out more about the object type */
358       $ldap       = $this->config->get_ldap_link();
359       $ldap->cat($this->dn, array('objectClass'));
360       if($ldap->count()){
361         $attrs  = $ldap->fetch();
362         $type     = $this->get_type($attrs);                    
364         $acl  = $this->ui->get_permissions($this->dn,"fai/".$type[1]);
365         if(preg_match("/d/",$acl)){
367           $this->dialog = new $type[0]($this->config,   $this->config->data['TABS'][$type[2]], $this->dn,"fai");
368           $this->dialog->set_acl_base($this->dn);
369           $this->dialog->by_object[$type[1]]->remove_from_parent ();
370           unset ($this->dialog);
371           $this->dialog= FALSE;
372           $to_del = FAI::clean_up_releases($this->dn);
373           FAI::save_release_changes_now();
375           foreach($to_del as $dn){
376             $ldap->rmdir_recursive($dn);
377           }
379         } else {
381           /* Normally this shouldn't be reached, send some extra
382              logs to notify the administrator */
383           print_red (_("You are not allowed to delete this component!"));
384           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
385         }
387       }else{
388         print_red(sprintf(_("Object could not be deleted '%s', object does not exist."),$this->dn));
389       }
390       /* Remove lock file after successfull deletion */
391       del_lock ($this->dn);
392     }
395     /****************
396       Edit entry 
397      ****************/
399                 if(($s_action == "edit") && (!isset($this->dialog->config))){
400                         $entry    = $this->objects[$s_entry];
401                         $a_setup  = $this->get_type($entry);
402                         $this->dn = $entry['dn'];
404                         /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
405                         if (($user= get_lock($this->dn)) != ""){
406                                 return(gen_locked_message ($user, $this->dn));
407                         }
408                         add_lock ($this->dn, $this->ui->dn);
410                         $this->dialog     = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
411       $this->dialog->set_acl_base($this->dn);
412                         $this->is_dialog  = true;
414       if($entry['FAIstate'] == "freeze"){
415         #$this->dialog->set_acl(array("*none*"))  ;    
416       }
417                         session::set('objectinfo',$this->dn);
418                 }
421     /*  Branch handling 
422         09.01.2006
423     */
425     /****************
426       Remove branch
427      ****************/
429     /* Remove branch 
430      */
431     if($s_action == "remove_branch"){
432       $base= $this->DivListFai->selectedBranch;
434       /* Check if we have a post remove method configured
435        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
436        */
437       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
438         /* Load permissions for selected 'dn' and check if
439            we're allowed to remove this 'dn' */
440         if($this->acl_is_removeable()){
442           $smarty->assign("info", sprintf(_("You're about to delete a fai branch / freeze  '%s'."), $this->DivListFai->selectedBranch));
443           return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
444         } else {
445           print_red (_("You are not allowed to delete this release!"));
446         }
447       }
448     }
450     
451     /****************
452       Remove branch confirmed
453      ****************/
455     if(isset($_POST['delete_branch_confirm'])){
457       /* Check if we have a post remove method configured
458        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
459        */
460       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
462         $bb =  $this->DivListFai->selectedBranch;
463         if(!isset($ldap)){
464           $ldap = $this->config->get_ldap_link();
465         }
467         $br = $this->getBranches();
469         if(isset($br[$bb]) && $this->acl_is_removeable()){
470           $name = $br[$bb];
471           $ldap->cd($bb);
472           $ldap->recursive_remove();
473           $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('applicationou'), $bb));
474           $ldap->recursive_remove();
475           $this->DivListFai->selectedBranch = "main";
477           /* Post remove */
478           $this->lock_name   = $name;
479           $this->lock_dn     = $bb;
480           $this->postremove();
482           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
483         }
484       }
485     }
488     /****************
489       Create a new branch "insert Name"
490      ****************/
492     if((isset($_POST['UseBranchName']))&&(($this->dispNewBranch)||($this->dispNewFreeze))){
493       session::set('LASTPOST',$_POST);
495       if($this->dispNewBranch){
496         $type = "branch";
497       }else{
498         $type = "freeze";
499       }
501       /* Check branch name */
502       $name = $_POST['BranchName'];
503       $is_ok = true;
504       $smarty->assign("BranchName",$name);
505       $base= get_ou('faiou').$this->DivListFai->selectedBase;
507       /* Check used characters */
508       if(preg_match("/[^0-9a-z \ö\ä\ü\.-_:,]/i",$name)){
509         if($type == "branch"){
510           print_red(_("Specified branch name is invalid."));
511         }else{
512           print_red(_("Specified freeze name is invalid."));
513         }
514         $is_ok = false;
515       }
517       /* Check if this name is already in use */
518       if(!$this->CheckNewBranchName($_POST['BranchName'],$this->DivListFai->selectedBranch)){
519         print_red(_("This name is already in use."));
520         $is_ok = false;
521       }
523       if($is_ok){
525         if(session::is_set('LASTPOST')){
526           $LASTPOST = session::get('LASTPOST');
527         }else{
528           $LASTPOST = array();
529         }
530         $LASTPOST['base'] = $base;
531         $LASTPOST['type'] = $type;
532         session::set('LASTPOST',$LASTPOST);
533         $smarty->assign("iframe", true);
534         $smarty->assign("plugID", $_GET['plug']);
535         $display        = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
536         return($display);
537       }
538     }
541     /****************
542       Create a new branch 
543      ****************/
545     if(isset($_GET['PerformBranch'])){
546     
547       if(!$this->acl_is_createable()){
548         print_red(_("You are not allowed to create a new branch."));
549       }else{
551         /* Create it know */
552         $this->dispNewBranch = false;
553         $this->dispNewFreeze = false;
555         $LASTPOST = session::get('LASTPOST');
556         $base = $LASTPOST['base'];
557         $_POST  = session::get('LASTPOST');      
558         $name = $_POST['BranchName'];
560         $type = $LASTPOST['type'];
561         $ldap = $this->config->get_ldap_link();
563         $baseToUse = $base;
564         if($this->DivListFai->selectedBranch != "main" ){
565           $baseToUse = $this->DivListFai->selectedBranch;
566         }
568         /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
570         $CurrentReleases  = $this->getBranches();
571         $NewReleaseName   = $name;
572         if(isset($CurrentReleases[$this->DivListFai->selectedBranch])) {
573           if($this->DivListFai->selectedBranch != "main"){
574             $NewReleaseName = $CurrentReleases[$this->DivListFai->selectedBranch]."/".$name;
575             $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName); 
576           }else{
577             $NewReleaseName   = $name;
578           }
579         }
581         $appsrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),$baseToUse); 
582         $appdst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),"ou=".$name.",".$baseToUse) ; 
584         $mimesrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),$baseToUse); 
585         $mimedst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),"ou=".$name.",".$baseToUse) ; 
587         /* Check if source depeartments exist */
588         foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
589           $ldap->cd($this->config->current['BASE']);
590           $ldap->cat($dep);
591           if(!$ldap->count()){
592             $ldap->create_missing_trees($dep);
593           }
594         }
596         /* Print header to have styles included */
597         echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
598           <html>
599           <head>
600           <title></title>
601           <style type="text/css">@import url("themes/default/style.css");</style>
602           <script language="javascript" src="include/focus.js" type="text/javascript"></script>
603           </head>
604           <body style="background: none;margin:3px;color:black">
605           ';
607         new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
609         /* Duplicate group application releases 
610          */
611         FAI::copy_FAI_group_releases($CurrentReleases[$this->DivListFai->selectedBranch],$name,$type);
613         /* Duplicate applications 
614          */
615         $ldap->cat($appsrc,array("dn")) ;
616         if($ldap->count()){
617           $ldap->cd ($appdst);
618           $ldap->recursive_remove();
619           FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
620         }
622         /* Duplicate mime types 
623          */
624         $ldap->cat($mimesrc,array("dn")) ;
625         if($ldap->count()){
626           $ldap->cd ($mimedst);
627           $ldap->recursive_remove();
628           FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
629         }
631         $attr = array();
632         $attr['objectClass'] = array("organizationalUnit","FAIbranch");
633         $attr['ou']       = $name;
634         $attr['FAIstate'] = $type;
635         $ldap->cd($this->config->current['BASE']);
636         $ldap->cd("ou=".$name.",".$baseToUse);
637         $ldap->cat("ou=".$name.",".$baseToUse);
638         if($ldap->count()){
639           $ldap->modify($attr);
640         }else{
641           $ldap->add($attr);
642         }
644         /* Duplicate fai objects 
645          */
646         //      $ldap->cd ("ou=".$name.",".$baseToUse);
647         //      $ldap->recursive_remove();
648         //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
650         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
651           <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
652           </form></div>";
654         echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
656         /* Print footer to have valid html */
657         echo "</body></html>";
659         $this->dispNewFreeze = false; 
661         /* Postcreate */ 
663         /* Assign possible attributes */
664         $this->lock_type  = $type; 
665         $this->lock_name  = $name; 
666         $this->lock_dn    = $baseToUse;
667         $this->postcreate();
668         exit();
669       }
670     }
672     /****************
673       Display dialog to enter new Branch name
674      ****************/
676     /* Check if we have a post create method configured
677      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
678      */
679     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
680       if(($s_action == "branch_branch")||($this->dispNewBranch)){
681         if(!$this->acl_is_createable()){
682           print_red(_("You are not allowed to create a new branch."));
683         }else{
684           $this->dispNewBranch=true;
685           $smarty->assign("iframe",false);
686           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
687           return($display);
688         }
689       } 
690     }
692  
693     /****************
694       Display dialog to enter new Freeze name
695      ****************/
697     /* Check if we have a post create method configured
698      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
699      */
700     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
701       if(($s_action == "freeze_branch")||($this->dispNewFreeze)){
702         if(!$this->acl_is_createable()){
703           print_red(_("You are not allowed to create a new branch."));
704         }else{
705           $this->dispNewFreeze = true;
706           $smarty->assign("iframe",false);
707           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
708           return($display);
709         }
710       }
711     }
714     /****************
715       Create a new object 
716      ****************/
718     $types = array( "new_partition"     =>  "FAIpartitionTable",
719                     "new_script"        =>  "FAIscript",
720                     "new_hook"          =>  "FAIhook",
721                     "new_variable"      =>  "FAIvariable",
722                     "new_template"      =>  "FAItemplate",
723                     "new_package"       =>  "FAIpackageList");
725     if(isset($types[$s_action])){
726       $acl = $this->ui->get_permissions($this->DivListFai->selectedBase,"fai/".$type_acl_mapping[$types[$s_action]]);
727       if(preg_match("/c/",$acl)){
728         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,$types[$s_action]);
729       }else{
730         print_red(sprintf(_("You are not allowed to create a new '%s' object."),$types[$s_action]));
731       }
732     }
734     /* New Profile */
735     if($s_action == "new_profile"){
736       $this->dn = "new" ;
738       $acl = $this->ui->get_permissions($this->DivListFai->selectedBase,"fai/faiProfile");
739       if(preg_match("/c/",$acl)){
740         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
741         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
742         $this->dialog->set_acl_base($this->base);
744         $this->is_dialog = false;
745       }else{
746         print_red(sprintf(_("You are not allowed to create a new '%s' object."),"FAIprofile"));
747       }
748     }
751     /****************
752       Get from ask class name dialog 
753      ****************/
755     if($s_action == "select_class_name_finished"){
756       $this->dialog->save_object();
757       if(count($this->dialog->check())!=0){
758         foreach($this->dialog->check() as $msg){
759           print_red($msg);
760         }               
761       }elseif(isset($this->dialog->objectClass)){
762         $this->dn = "new" ;
763         $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
764         $name = $this->dialog->save();
766         if(class_exists($a_setup[0])){
767           $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
768           $this->dialog->set_acl_base($this->base);
769           $this->dialog->by_object[$a_setup[1]]->cn = $name;
770           $this->is_dialog = true;
771         }
772       }         
773     }   
776     /****************
777      Cancel dialogs 
778      ****************/
780                 if(isset($_POST['edit_cancel'])){
781                         unset($this->dialog);
782                         $this->dialog=FALSE;
783                         $this->is_dialog = false;
784                         session::un_set('objectinfo');
785                         del_lock ($this->dn);
786                 }
789     /****************
790       Save sub dialogs 
791      ****************/
793                 /* This check if the given tab could be saved 
794                  * If it was possible to save it, remove dialog object. 
795                  * If it wasn't possible, show errors and keep dialog.
796                  */
797                 if((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->dialog->config))){
798                         $this->dialog->save_object();
799       $msgs= $this->dialog->check();
800                         if(count($msgs)!=0){
801                                 foreach($msgs as $msg){
802                                         print_red($msg);
803                                 }
804                         }else{
805                                 $this->dialog->save();
806         FAI::save_release_changes_now();
807         if (!isset($_POST['edit_apply'])){
808           del_lock ($this->dn);
809           unset($this->dialog);
810           $this->dialog=FALSE;
811           $this->is_dialog=false;
812           session::un_set('objectinfo');
813         }
814                         }
815                 }
818     /****************
819       Display currently open dialog 
820      ****************/
822                 /* If dialog is set, but $this->is_dialog==false, then 
823                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
824                  * If is_dialog == true, we are currently editing tab objects.
825                  *  Here we need both, save and cancel
826                  */ 
828                 if(is_object($this->dialog)){
829                         $display .= $this->dialog->execute();
830                         /* Don't show buttons if tab dialog requests this */
832       if(isset($this->dialog->current)){
834         $obj = $this->dialog->by_object[$this->dialog->current];
836         if((isset($obj->is_dialog) && (!$obj->is_dialog)) || (isset($obj->dialog) && (!$obj->dialog))){
838           $display.= "<p style=\"text-align:right\">\n";
839           $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
840           $display.= "&nbsp;\n";
841           if ($this->dn != "new"){
842             $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
843             $display.= "&nbsp;\n";
844           }
845           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
846           $display.= "</p>";
847         }elseif(!isset($this->dialog->current)){
848           $display.= "<p style=\"text-align:right\">\n";
849           $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
850           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
851           $display.= "</p>";
852         }
853       }else{
854         $display.= "<p style=\"text-align:right\">\n";
855         $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
856         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
857         $display.= "</p>";
859       }
860       return($display);
861                 }
862                 
864     /****************
865       Dialog display
866      ****************/
868     /* Check if there is a snapshot dialog open */
869     $base = $this->DivListFai->selectedBase;
870     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
871       return($str);
872     }
874     /* Display dialog with system list */
875     $this->DivListFai->parent = $this;
876     $this->DivListFai->execute();
877     $this->DivListFai->AddDepartments($this->DivListFai->selectedBase,4,1);
878     $this->reload();
879     $this->DivListFai->setEntries($this->objects);
880     return($this->DivListFai->Draw());
881         }
884   /* Return departments, that will be included within snapshot detection */
885   function get_used_snapshot_bases()
886   {
887     $tmp = array();
888     $types = array("faipartitionou","faiscriptou","faitemplateou","faihookou","faiprofileou","faivariableou","faipackageou");
889     foreach($types as $type){
890       if($this->DivListFai->selectedBranch == "main"){
891         $tmp[] = get_ou($type).get_ou('faiou').$this->DivListFai->selectedBase;
892       }else{
893         $tmp[] = get_ou($type).$this->DivListFai->selectedBranch;
894       }
895     }
896     return($tmp);
897   }
900   /* Get available branches for current base */
901   function getBranches($base = false,$prefix = "")
902   {
903     $ret = array("/"=>"main");
904     $ldap = $this->config->get_ldap_link();
905     if(!$base){
906       $base = session::get('CurrentMainBase');
907     }
908     $tmp = FAI::get_all_releases_from_base($base,true);
909     foreach($tmp as $dn => $name){
910       $ret[$name]=$dn;
911     }
912     ksort($ret);
913     $ret = array_flip($ret);
915     return ($ret);
916   }
917   
919   function list_get_selected_items()
920   {
921     $ids = array();
922     foreach($_POST as $name => $value){
923       if(preg_match("/^item_selected_[0-9]*$/",$name)){
924         $id   = preg_replace("/^item_selected_/","",$name);
925         $ids[$id] = $id;
926       }
927     }
928     return($ids);
929   }
932   /* reload list of objects */
933   function reload()
934   {
935     /* Variable initialisation */
936     $str            = "";
937     $Regex          = $this->DivListFai->Regex;
938     $this->objects  = array();
940     /* Get base */
941     $base = get_ou('faiou').$this->DivListFai->selectedBase;
942     if($this->DivListFai->selectedBranch != "main"){
943       $br = $this->getBranches();
944       if(isset($br[$this->DivListFai->selectedBranch])){
945         $base = $this->DivListFai->selectedBranch;
946       }else{
947         $base = get_ou('faiou').$this->DivListFai->selectedBase;
948       }
949     }
950     $this->base = $base;
951     $this->set_acl_base($this->base);
953     $this->lock_type = FAI::get_release_tag(FAI::get_release_dn($base));
955     /* Create a new list of FAI object 
956      * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
957      */
958     $ObjectTypes = array(
959         "FAIpartitionTable"  => array("OU"=> get_ou('faipartitionou') , "CHKBOX"=>"ShowPartitions"  ,"ACL" => "faiPartitionTable"),
960         "FAIpackageList"     => array("OU"=> get_ou('faipackageou')   , "CHKBOX"=>"ShowPackages"    ,"ACL" => "faiPackage"),
961         "FAIscript"          => array("OU"=> get_ou('faiscriptou')    , "CHKBOX"=>"ShowScripts"     ,"ACL" => "faiScript"),
962         "FAIvariable"        => array("OU"=> get_ou('faivariableou')  , "CHKBOX"=>"ShowVariables"   ,"ACL" => "faiVariable"),
963         "FAIhook"            => array("OU"=> get_ou('faihookou')      , "CHKBOX"=>"ShowHooks"       ,"ACL" => "faiHook"),
964         "FAIprofile"         => array("OU"=> get_ou('faiprofileou')   , "CHKBOX"=>"ShowProfiles"    ,"ACL" => "faiProfile"),
965         "FAItemplate"        => array("OU"=> get_ou('faitemplateou')  , "CHKBOX"=>"ShowTemplates"   ,"ACL" => "faiTemplate"));
967     $filter = "";
968     foreach($ObjectTypes as $key => $data){
969       if($this->DivListFai->$data['CHKBOX']){
970         $filter.= "(objectClass=".$key.")";
971       }
972     }
973     $filter = "(&(|".$filter.")(cn=$Regex))";
974     
975     /* Get resolved release dependencies */
976     $tmp = FAI::get_all_objects_for_given_base($base,$filter);
978     /* Ge listed ldap objects */
979     $ldap = $this->config->get_ldap_link();
980     $ldap->cd($this->config->current['BASE']);
981     foreach($tmp as $entry){
983       /* Get some more informations about the object */ 
984       $ldap->cat($entry['dn'], array("cn","description","objectClass","FAIclass","FAIstate","objectClass"));
985       $object  = $ldap->fetch();
987       /* Walk through possible types */
988       foreach($ObjectTypes as $type => $rest){  
990         $acl = $this->ui->get_permissions($object['dn'],"fai/".$rest ['ACL']);
992         if(in_array($type,$object['objectClass']) && preg_match("/r/",$acl)){
994           /* Prepare object */
995           unset($object['objectClass']['count']);
996           if(!isset($object['description'][0])){
997             $object['description'][0]="";
998           }
1000           /* Clean up object informations */
1001           $obj                  = array();
1002           $obj['cn']                          = $object['cn'][0];
1003           $obj['dn']                          = $object['dn'];
1004           $obj['acl']                       = $acl;
1005           $obj['description']   = $object['description'][0];
1006           $obj['objectClass']   = $object['objectClass'];
1008           /* Append type to this string, to be able to check if the selected 
1009            * entry is of type 'freeze' or 'branch'
1010            */
1011           if(!isset($object['FAIstate'])){
1012             $obj['FAIstate'] = $this->lock_type;
1013           }else{
1014             $obj['FAIstate'] = $object['FAIstate'][0]; 
1015           }
1017           $this->objects[strtolower($obj['cn']).$obj['cn'].$type] = $obj;
1018           $this->objects[strtolower($obj['cn']).$obj['cn'].$type]['type']=$type;
1019         }
1020                         }
1021                 }
1023                 ksort($this->objects);
1024                 reset ($this->objects);
1025         
1026                 /* use numeric index, thats a bit more secure */        
1027                 $tmp0 = array();
1028                 foreach($this->objects as $obj){
1029                         $tmp0[]= $obj;
1030                 }
1031                 $this->objects = array();
1032                 $this->objects = $tmp0;
1033         }
1035         function remove_lock()
1036         {
1037                 if (isset($this->dn)){
1038                         del_lock ($this->dn);
1039                 }
1040         }
1042         function get_type($array){
1043                 if(in_array("FAIpartitionTable",$array['objectClass'])){
1044                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
1045                 }
1046                 if(in_array("FAIscript",$array['objectClass'])){
1047                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
1048                 }
1049                 if(in_array("FAItemplate",$array['objectClass'])){
1050                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
1051                 }
1052                 if(in_array("FAIhook",$array['objectClass'])){
1053                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
1054                 }
1055                 if(in_array("FAIvariable",$array['objectClass'])){
1056                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
1057                 }
1058                 if(in_array("FAIprofile",$array['objectClass'])){
1059                         return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
1060                 }
1061                 
1062                 if(in_array("FAIpackageList",$array['objectClass'])){
1063                         return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
1064                 }
1065         }
1067   function CheckNewBranchName($name,$base)
1068   {
1069     $f = $this->DivListFai->selectedBranch;
1070     if($name == ""){
1071       return(false);
1072     }elseif(in_array($name,$this->getBranches($f))) {
1073       return(false);
1074     }elseif(tests::is_department_name_reserved($name,$base)){
1075       return(false);
1076     }
1077     return(true);
1078   }
1080   function save_object()
1081   {
1082     $this->DivListFai->save_object();
1083   }
1086   function copyPasteHandling_from_queue($s_action,$s_entry)
1087   {
1088     /* Check if Copy & Paste is disabled */
1089     if(!is_object($this->CopyPasteHandler)){
1090       return("");
1091     }
1093     $base = $this->DivListFai->selectedBranch;
1094     if($base == "main"){
1095       $base = $this->DivListFai->selectedBase;
1096     }
1098     /* Add a single entry to queue */
1099     if($s_action == "cut" || $s_action == "copy"){
1101       /* Cleanup object queue */
1102       $this->CopyPasteHandler->cleanup_queue();
1103       $entry    = $this->objects[$s_entry];
1104       $a_setup  = $this->get_type($entry);
1105       $dn = $entry['dn'];
1106       $this->CopyPasteHandler->add_to_queue($dn,$s_action,$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1107     }
1109     /* Add entries to queue */
1110     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
1112       /* Cleanup object queue */
1113       $this->CopyPasteHandler->cleanup_queue();
1115       /* Add new entries to CP queue */
1116       foreach($this->list_get_selected_items() as $id){
1118         /* Cleanup object queue */
1119         $entry    = $this->objects[$id];
1120         $a_setup  = $this->get_type($entry);
1121         $dn = $entry['dn'];
1124         if($s_action == "copy_multiple"){
1125           $this->CopyPasteHandler->add_to_queue($dn,"copy",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1126         }
1127         if($s_action == "cut_multiple"){
1128           $this->CopyPasteHandler->add_to_queue($dn,"cut",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1129         }
1130       }
1131     }
1133     /* Start pasting entries */
1134     if($s_action == "editPaste"){
1135       $this->start_pasting_copied_objects = TRUE;
1136     }
1138     /* Return C&P dialog */
1139     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1141       /* Load entry from queue and set base */
1142       $this->CopyPasteHandler->load_entry_from_queue();
1143       $this->CopyPasteHandler->SetVar("base",$base);
1145       /* Get dialog */
1146       $data = $this->CopyPasteHandler->execute();
1148       FAI::save_release_changes_now();
1150       /* Return dialog data */
1151       if(!empty($data)){
1152         return($data);
1153       }
1154     }
1156     /* Automatically disable status for pasting */
1157     if(!$this->CopyPasteHandler->entries_queued()){
1158       $this->start_pasting_copied_objects = FALSE;
1159     }
1160     return("");
1161   }
1164   /* Return plugin informations for acl handling */ 
1165   static function plInfo()
1166   {
1167     return (array( 
1168           "plShortName"   => _("FAI releases"),
1169           "plDescription" => _("FAI release management"),
1170           "plSelfModify"  => FALSE,
1171           "plDepends"     => array(),
1172           "plPriority"    => 0,
1173           "plSection"     => array("administration"),           
1174           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1175                                                  "objectClass" => "FAIclass")),
1176           "plProvidedAcls"=> array()));
1177   }
1179 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1180 ?>