Code

Language fine-tuning
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
22 class faiManagement extends plugin
23 {
24         /* Definitions */
25   var $plHeadline     = "Software deployment";
26   var $plDescription    = "Manage software packages and deployment reciepes";
28         /* Headpage attributes */
29   var $lock_type    = "";    // should be branch/freeze
30   var $lock_name    = "";
31   var $lock_dn      = "";  
33         /* attribute list for save action */
34         var $attributes         = array("lock_type","lock_name","lock_dn");     //      Attributes Managed by this plugin 
35         var $objectclasses= array();    //      ObjectClasses which the attributes are related to
36         var $dialog                             = array();      //      This object contains every dialog we have currently opened
38         var $objects                    = array();      //      This array contains all available objects shown in divlist
39         var $is_dialog          = false;
41   var $dispNewBranch= false;
42   var $dispNewFreeze= false;
44   var $DivListFai;
45   var $start_pasting_copied_objects = FALSE;
46   var $CopyPasteHandler = FALSE;
48   /* Allow inserting of new elements if freezed releases 
49   */
50   var $allow_freeze_object_attach = FALSE;
52   var $no_save;
53   var $fai_base     ="";
54   var $fai_release  ="";
56         /* construction/reconstruction 
57          */
58         function faiManagement (&$config, $ui)
59         {
60                 /* Set defaults */
61                 $this->dn                       = "";
62                 $this->config   = $config;
63                 $this->ui                       = $ui;  
64     
65     /* Creat dialog object */
66     $this->DivListFai = new divListFai($this->config,$this);
68     /* Copy & Paste handler */
69     if ($this->config->boolValueIsTrue("main", "enableCopyPaste")){
70       $this->CopyPasteHandler= new CopyPasteHandler($this->config);
71     }
73     /* Set default release */
74     $this->fai_base = get_ou("faiou").$this->config->current['BASE'];
75     if(!session::is_set("fai_filter")){
76       session::set("fai_filter",array("fai_release" => $this->fai_base));
77     }
79     $fai_filter = session::get("fai_filter");
80     $this->fai_release = $fai_filter['fai_release'];
81         }
83         function execute()
84         {
85     /* Call parent execute */
86     plugin::execute();
88     /* Initialise vars and smarty */
89                 $smarty         = get_smarty();
90                 $smarty->assign("BranchName","");
91     
92                 $display        = "";
93     $s_action   = "";
94                 $s_entry        = "";
95     $no_save = FALSE;   // hide Apply / Save buttons
96     
97     /* If an entry was locked, these vars will be stored in a session to allow direct edit */
98     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/"));
101     /****************
102       Handle posts 
103      ****************/
105                 /* Check ImageButton posts
106                  * Create new tab ich new_xx is posted
107                  */
108     $posts = array( "/remove_branch/"=>"remove_branch",    "/branch_branch/"=>"branch_branch",
109                     "/freeze_branch/"=>"freeze_branch",    "/create_partition/i"=>"new_partition",
110                     "/create_script/i"=>"new_script",      "/create_hook/i"=>"new_hook",
111                     "/create_variable/i"=>"new_variable",  "/create_template/i"=>"new_template",
112                     "/create_package/i"=>"new_package",    "/create_profile/i"=>"new_profile",
113                     "/edit_continue/"=>"select_class_name_finished",
114                     "/^multiple_copy_fai/" => "copy_multiple", 
115                     "/^multiple_cut_fai/" => "cut_multiple", 
116                     "/^copy/" => "copy",
117                     "/^remove_multiple_fai_objects/" => "del_multiple");
119                 foreach($_POST as $name => $value){
120       foreach($posts as $reg => $act ){
121         if(preg_match($reg,$name)){
122           $s_action = $act;
123           $s_entry = ltrim(preg_replace($reg,"",$name),"_");
124           $s_entry = preg_replace("/_.*$/","",$s_entry);
125         }
126       }
127                         if(preg_match("/^entry_edit_.*/",$name)){
128                                 $s_entry = preg_replace("/^entry_edit_/","",$name);
129                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
130                                 $s_action = "edit";
131                         }elseif(preg_match("/^entry_freeze_edit_.*/",$name)){
132                                 $s_entry = preg_replace("/^entry_freeze_edit_/","",$name);
133                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
134                                 $s_action = "edit";
135         $no_save = TRUE;
136                         }elseif(preg_match("/^entry_delete_.*/",$name)){
137                                 $s_entry = preg_replace("/^entry_delete_/","",$name);
138                                 $s_entry = preg_replace("/_.*$/","",$s_entry);
139         $s_action = "delete";
140       }
141     }
143                 if(isset($_GET['edit_entry'])){
144                         $s_entry = $_GET['edit_entry'];
145                         $s_action = "edit";
146                 }
148                 if(isset($_GET['edit_freeze_entry'])){
149                         $s_entry = $_GET['edit_freeze_entry'];
150                         $s_action = "edit";
151       $no_save = TRUE;
152                 }
154     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch"){
155       $s_action = "freeze_branch";
156     }
157     if(isset($_GET['act']) && $_GET['act'] == "branch_branch"){
158       $s_action = "branch_branch";
159     }
160     if(isset($_GET['act']) && $_GET['act'] == "remove_branch"){
161       $s_action = "remove_branch";
162     }
163     
164     if((isset($_POST['CancelBranchName'])) || (isset($_POST['CloseIFrame']))){
165       $this->dispNewBranch = false;
166       $this->dispNewFreeze = false;
167     }
170     $type_acl_mapping = array(
171         "FAIpartitionTable"  => "faiPartitionTable", 
172         "FAIpackageList"     => "faiPackage",
173         "FAIscript"          => "faiScript",
174         "FAIvariable"        => "faiVariable",
175         "FAIhook"            => "faiHook",
176         "FAIprofile"         => "faiProfile",
177         "FAItemplate"        => "faiTemplate");
180     /* handle C&P from layers menu */
181     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
182       $s_action = "copy_multiple";
183     }
184     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
185       $s_action = "cut_multiple";
186     }
187     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
188       $s_action = "editPaste";
189     }
191     /* Create options */
192     if(isset($_POST['menu_action']) && preg_match("/^Create_/",$_POST['menu_action'])){
193       $s_action = "new_".preg_replace("/^Create_/","",$_POST['menu_action']);;
194       $s_entry  = preg_replace("/^Create_/","",$_POST['menu_action']);
195     }
197     /* handle remove from layers menu */
198     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
199       $s_action = "del_multiple";
200     }
203     if(!empty($s_action)){
204       $this->no_save = $no_save;
205     }
207     /********************
208       Copy & Paste
209      ********************/
211     /* Display the copy & paste dialog, if it is currently open */
212     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
213     if($ret){
214       return($ret);
215     }
218     /****************
219       Delete confirme dialog 
220      ****************/
222                 if ($s_action=="delete"){
224       /* Get 'dn' from posted termlinst */
225       $this->dn= $this->objects[$s_entry]['dn'];
227                         /* Load permissions for selected 'dn' and check if
228                            we're allowed to remove this 'dn' */
229       $acl  = $this->ui->get_permissions($this->dn,"fai/".$type_acl_mapping[$this->objects[$s_entry]['type']]);
230       if(preg_match("/d/",$acl)){
232                                 /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
233                                 if (($user= get_lock($this->dn)) != ""){
234                                         return(gen_locked_message ($user, $this->dn));
235                                 }
237                                 /* Lock the current entry, so nobody will edit it during deletion */
238                                 add_lock ($this->dn, $this->ui->dn);
239                                 $smarty->assign("warning",msgPool::deleteInfo(@LDAP::fix($this->dn),_("FAI object")));
240         $smarty->assign("multiple", false);
241                                 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
242                         } else {
244                                 /* Obviously the user isn't allowed to delete. Show message and clean session. */
245         msg_dialog::display(_("Permission error"), msgPool::permDelete(),ERROR_DIALOG);
246                         }
247                 }
250     /********************
251       Delete MULTIPLE entries requested, display confirm dialog
252      ********************/
254     if ($s_action=="del_multiple"){
255       $this->dns = array();
256       $ids = $this->list_get_selected_items();
258       if(count($ids)){
260         $errors = "";
261         foreach($ids as $id){
262           $dn = $this->objects[$id]['dn'];
263           $cn = $this->objects[$id]['cn'];
264           if(!preg_match('/^freeze/', $this->objects[$id]['FAIstate'])){
265             $this->dns[$id] = $dn;
266           }else{
267             $errors .= $cn.", ";
268           }
269         }
270         if ($user= get_multiple_locks($this->dns)){
271           return(gen_locked_message($user,$this->dns));
272         }
274         if($errors != ""){
275           msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
276             "<br><br>".trim($errors,", ")),INFO_DIALOG);
277         }
279         if(count($this->dns)){
281           $dns_names = array();
282           foreach($this->dns as $dn){
283             add_lock ($dn, $this->ui->dn);
284             $dns_names[] = @LDAP::fix($dn);
285           }
287           /* Lock the current entry, so nobody will edit it during deletion */
288                                   $smarty->assign("warning",msgPool::deleteInfo($dns_names,_("FAI object")));
289           $smarty->assign("multiple", true);
290           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
291         }
292       }
293     }
296     /********************
297       Delete MULTIPLE entries confirmed
298      ********************/
300     /* Confirmation for deletion has been passed. Users should be deleted. */
301     if (isset($_POST['delete_multiple_fai_object_confirm'])){
303       /* Find out more about the object type */
304       $ldap   = $this->config->get_ldap_link();
306       /* Remove user by user and check acls before removeing them */
307       foreach($this->dns as $key => $dn){
309         $ldap->cat($dn, array('objectClass'));
310         $attrs  = $ldap->fetch();
311         $type   = $this->get_type($attrs);
313         $acl  = $this->ui->get_permissions($dn,"fai/".$type[1]);
314         if(preg_match("/d/",$acl)){
316           $this->dialog = new $type[0]($this->config, $this->config->data['TABS'][$type[2]], $dn,"fai");
317           $this->dialog->parent = &$this;
318           $this->dialog->set_acl_base($dn);
319           $this->dialog->by_object[$type[1]]->remove_from_parent ();
320           unset ($this->dialog);
321           $this->dialog= FALSE;
322           $to_del = FAI::clean_up_releases($dn);
323           FAI::save_release_changes_now();
325           foreach($to_del as $dn){
326             $ldap->rmdir_recursive($dn);
327           }
329         } else {
331           /* Normally this shouldn't be reached, send some extra
332              logs to notify the administrator */
333           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
334           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
335         }
336       }
338       /* Remove lock file after successfull deletion */
339       $this->remove_lock();
340       $this->dns = array();
341     }
344     /********************
345       Delete MULTIPLE entries Canceled
346      ********************/
348     /* Remove lock */
349     if(isset($_POST['delete_multiple_fai_object_cancel'])){
350       $this->dns = array();
351       $this->remove_lock();
352     }
355     /****************
356       Delete aborted  
357      ****************/
359                 /* Delete canceled? */
360                 if (isset($_POST['delete_cancel'])){
361       $this->remove_lock();
362                 }
365     /****************
366       Delete confirmed 
367      ****************/
369                 /* Deltetion was confirmed, so delete this entry
370      */
371     if (isset($_POST['delete_terminal_confirm'])){
373       /* Some nice guy may send this as POST, so we've to check
374          for the permissions again. */
376       /* Find out more about the object type */
377       $ldap       = $this->config->get_ldap_link();
378       $ldap->cat($this->dn, array('objectClass'));
379       if($ldap->count()){
380         $attrs  = $ldap->fetch();
381         $type     = $this->get_type($attrs);                    
383         $acl  = $this->ui->get_permissions($this->dn,"fai/".$type[1]);
384         if(preg_match("/d/",$acl)){
386           $this->dialog = new $type[0]($this->config,   $this->config->data['TABS'][$type[2]], $this->dn,"fai");
387           $this->dialog->set_acl_base($this->dn);
388           $this->dialog->parent = &$this;
389           $this->dialog->by_object[$type[1]]->remove_from_parent ();
390           unset ($this->dialog);
391           $this->dialog= FALSE;
392           $to_del = FAI::clean_up_releases($this->dn);
393           FAI::save_release_changes_now();
395           foreach($to_del as $dn){
396             $ldap->rmdir_recursive($dn);
397           }
399         } else {
401           /* Normally this shouldn't be reached, send some extra
402              logs to notify the administrator */
403           msg_dialog::display(_("Permission error"), msgPool::permDelete(), ERROR_DIALOG);
404           new log("security","fai/".get_class($this),$dn,array(),"Tried to trick deletion.");
405         }
407       }else{
408         msg_dialog::display(_("Error"), sprintf(_("Cannot delete '%s': object does not exist!"), $this->dn) , ERROR_DIALOG);
409       }
411       /* Remove lock file after successfull deletion */
412       $this->remove_lock();
413     }
416     /****************
417       Edit entry 
418      ****************/
420                 if(($s_action == "edit") && (!isset($this->dialog->config))){
421                         $entry    = $this->objects[$s_entry];
422                         $a_setup  = $this->get_type($entry);
423                         $this->dn = $entry['dn'];
425                         /* Check locking, save current plugin in 'back_plugin', so the dialog knows where to return. */
426                         if (($user= get_lock($this->dn)) != ""){
427                                 return(gen_locked_message ($user, $this->dn));
428                         }
429                         add_lock ($this->dn, $this->ui->dn);
431                         $this->dialog     = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
432       $this->dialog->parent = &$this;
433       $this->dialog->set_acl_base($this->dn);
434                         $this->is_dialog  = true;
436       if(preg_match("/^freeze/", $entry['FAIstate']) || $this->no_save){
437 #        $this->dialog->set_acl_base("freezed")  ;    
438       }
439                         session::set('objectinfo',$this->dn);
440                 }
443     /*  Branch handling 
444         09.01.2006
445     */
447     /****************
448       Remove branch
449      ****************/
451     /* Remove branch 
452      */
453     if($s_action == "remove_branch"){
454       $base= $this->fai_release;
456       /* Check if we have a post remove method configured
457        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
458        */
459       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
460         /* Load permissions for selected 'dn' and check if
461            we're allowed to remove this 'dn' */
462         if($this->acl_is_removeable()){
463           $smarty->assign("release_hidden",base64_encode($this->fai_release));
464           $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->fai_release),_("FAI branch/freeze")));
465           return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
466         } else {
467           msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
468         }
469       }
470     }
472     
473     /****************
474       Remove branch confirmed
475      ****************/
477     if(isset($_POST['delete_branch_confirm'])){
479       /* Check if we have a post remove method configured
480        *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
481        */
482       if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
484         if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
485           msg_dialog::display(_("Warning"),_("Release remove aborted, release name check failed."));
486         }else{
488           $bb =  $this->fai_release;
489           if(!isset($ldap)){
490             $ldap = $this->config->get_ldap_link();
491           }
493           $br = $this->getBranches();
495           if(isset($br[$bb]) && $this->acl_is_removeable()){
496             $name = $br[$bb];
498             $ldap->cd($bb);
499             $ldap->recursive_remove();
500             $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('applicationou'), $bb));
501             $ldap->recursive_remove();
502             $ldap->cd(preg_replace('/,'.normalizePreg(get_ou('faiou')).'/', ','.get_ou('mimetypeou'), $bb));
503             $ldap->recursive_remove();
505             /* Search for all groups with configured application menus.
506               - First search all groups, to ensure that we only remove entries form whithin groups. 
507               - The search für menu configuration for the specified release and collect all those dns.
508               - Remove entries
509              */
510             $release_ou = preg_replace("/".normalizePreg(get_ou("faiou")).".*$/","",$bb);
511             $ldap->cd($this->config->current['BASE']);
512             $ldap->search("(objectClass=posixGroup)",array("dn"));
513           
514             /* Collect all group dns 
515              */
516             $groups = array();
517             while($attrs = $ldap->fetch()){
518               $groups[] = $attrs['dn'];
519             }
521             /* Collect all group menu release dns that match the release we have removed 
522              */
523             $dns = array();
524             foreach($groups as $dn){
525               $ldap->cd($dn);
526               $ldap->search("(objectClass=FAIbranch)",array("dn"));
527               while($attrs = $ldap->fetch()){
528                 if(preg_match("/^".normalizePreg($release_ou)."/",$attrs['dn'])){
529                   $dns[] = $attrs['dn'];
530                 }
531               }
532             }
533             
534             /* Finally remove collected release dns 
535              */
536             foreach($dns as $dn){
537               $ldap->cd($dn);
538               $ldap->recursive_remove();
539             }
541             /* Post remove */
542             $this->fai_release = $this->fai_base;
543             $this->lock_name   = $name;
544             $this->lock_dn     = $bb;
545             $this->postremove();
547             new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
548           }
549         }
550       }
551     }
554     /****************
555       Create a new branch "insert Name"
556      ****************/
558     if((isset($_POST['UseBranchName']))&&(($this->dispNewBranch)||($this->dispNewFreeze))){
559       session::set('LASTPOST',$_POST);
561       if($this->dispNewBranch){
562         $type = "branch";
563       }else{
564         $type = "freeze";
565       }
567       /* Check branch name */
568       $name = $_POST['BranchName'];
569       $is_ok = true;
570       $smarty->assign("BranchName",$name);
571       $base= $this->fai_base;
573       /* Check used characters */
574       if(!preg_match("/^[0-9a-z \ö\ä\ü\.\-_:,]*$/i",$name)){
575         msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z \ö\ä\ü\.\-_:,]/i"), ERROR_DIALOG);
576         $is_ok = false;
577       }
579       /* Check if this name is already in use */
580       if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
581         msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
582         $is_ok = false;
583       }
585       if($is_ok){
587         if(session::is_set('LASTPOST')){
588           $LASTPOST = session::get('LASTPOST');
589         }else{
590           $LASTPOST = array();
591         }
592         $LASTPOST['base'] = $base;
593         $LASTPOST['type'] = $type;
594         session::set('LASTPOST',$LASTPOST);
595         $smarty->assign("iframe", true);
596         $smarty->assign("plugID", $_GET['plug']);
597         $display        = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
598         return($display);
599       }
600     }
603     /****************
604       Create a new branch 
605      ****************/
607     if(isset($_GET['PerformBranch'])){
608     
609       if(!$this->acl_is_createable()){
610         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
611       }else{
613         /* Create it know */
614         $this->dispNewBranch = false;
615         $this->dispNewFreeze = false;
617         $LASTPOST = session::get('LASTPOST');
618         $base = $LASTPOST['base'];
619         $_POST  = session::get('LASTPOST');      
620         $name = $_POST['BranchName'];
622         $type = $LASTPOST['type'];
623         $ldap = $this->config->get_ldap_link();
625         $baseToUse = $base;
626         if($this->fai_release !=  $this->fai_base){
627           $baseToUse = $this->fai_release;
628         }
630         /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
632         $CurrentReleases  = $this->getBranches();
633         $NewReleaseName   = $name;
634         if(isset($CurrentReleases[$this->fai_release])) {
635           if($this->fai_release != $this->fai_base){
636             $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
637             $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName); 
638           }else{
639             $NewReleaseName   = $name;
640           }
641         }
643         $appsrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),$baseToUse); 
644         $appdst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('applicationou'),"ou=".$name.",".$baseToUse) ; 
646         $mimesrc = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),$baseToUse); 
647         $mimedst = preg_replace("/".normalizePreg(get_ou('faiou'))."/",get_ou('mimetypeou'),"ou=".$name.",".$baseToUse) ; 
649         /* Check if source depeartments exist */
650         foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
651           $ldap->cd($this->config->current['BASE']);
652           $ldap->cat($dep);
653           if(!$ldap->count()){
654             $ldap->create_missing_trees($dep);
655           }
656         }
658         /* Print header to have styles included */
659         echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
660           <html>
661           <head>
662           <title></title>
663           <style type="text/css">@import url("themes/default/style.css");</style>
664           <script language="javascript" src="include/focus.js" type="text/javascript"></script>
665           </head>
666           <body style="background: none;margin:3px;color:black">
667           ';
669         new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
671         /* Duplicate group application releases 
672          */
673         FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
675         /* Duplicate applications 
676          */
677         $ldap->cat($appsrc,array("dn")) ;
678         if($ldap->count()){
679           $ldap->cd ($appdst);
680           $ldap->recursive_remove();
681           FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
682         }
684         /* Duplicate mime types 
685          */
686         $ldap->cat($mimesrc,array("dn")) ;
687         if($ldap->count()){
688           $ldap->cd ($mimedst);
689           $ldap->recursive_remove();
690           FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
691         }
693         $attr = array();
694         $attr['objectClass'] = array("organizationalUnit","FAIbranch");
695         $attr['ou']       = $name;
696         $attr['FAIstate'] = $type;
697         $ldap->cd($this->config->current['BASE']);
698         $ldap->cd("ou=".$name.",".$baseToUse);
699         $ldap->cat("ou=".$name.",".$baseToUse);
700         if($ldap->count()){
701           $ldap->modify($attr);
702         }else{
703           $ldap->add($attr);
704         }
706         /* Duplicate fai objects 
707          */
708         //      $ldap->cd ("ou=".$name.",".$baseToUse);
709         //      $ldap->recursive_remove();
710         //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
712         echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
713           <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
714           </form></div>";
716         echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
718         /* Print footer to have valid html */
719         echo "</body></html>";
721         $this->dispNewFreeze = false; 
723         /* Postcreate */ 
725         /* Assign possible attributes */
726         $this->lock_type  = $type; 
727         $this->lock_name  = $name; 
728         $this->lock_dn    = $baseToUse;
729         $this->postcreate();
730         exit();
731       }
732     }
734     /****************
735       Display dialog to enter new Branch name
736      ****************/
738     /* Check if we have a post create method configured
739      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
740      */
741     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
742       if(($s_action == "branch_branch")||($this->dispNewBranch)){
743         if(!$this->acl_is_createable()){
744         msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
745         }else{
746           $this->dispNewBranch=true;
747           $smarty->assign("iframe",false);
748           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
749           return($display);
750         }
751       } 
752     }
754  
755     /****************
756       Display dialog to enter new Freeze name
757      ****************/
759     /* Check if we have a post create method configured
760      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
761      */
762     if("" != $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'))){
763       if(($s_action == "freeze_branch")||($this->dispNewFreeze)){
764         if(!$this->acl_is_createable()){
765           msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
766         }else{
767           $this->dispNewFreeze = true;
768           $smarty->assign("iframe",false);
769           $display      .= $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
770           return($display);
771         }
772       }
773     }
776     /****************
777       Create a new object 
778      ****************/
780     $types = array( "new_partition"     =>  "FAIpartitionTable",
781                     "new_script"        =>  "FAIscript",
782                     "new_hook"          =>  "FAIhook",
783                     "new_variable"      =>  "FAIvariable",
784                     "new_template"      =>  "FAItemplate",
785                     "new_package"       =>  "FAIpackageList");
786     $types_i18n = array( "new_partition"     =>  _("partition table"),
787                     "new_script"        =>  _("script"),
788                     "new_hook"          =>  _("hook"),
789                     "new_variable"      =>  _("variable"),
790                     "new_template"      =>  _("template"),
791                     "new_package"       =>  _("package list"));
793     if(isset($types[$s_action])){
794       $acl = $this->ui->get_permissions($this->fai_base,"fai/".$type_acl_mapping[$types[$s_action]]);
795       if(preg_match("/c/",$acl)){
796         $this->dialog = new askClassName($this->config,$this->dn,$this->ui,$types[$s_action]);
797         $this->dialog->parent = &$this;
798       }else{
799         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$s_action]), ERROR_DIALOG);
800       }
801     }
803     /* New Profile */
804     if($s_action == "new_profile"){
805       $this->dn = "new" ;
807       $acl = $this->ui->get_permissions($this->fai_base,"fai/faiProfile");
808       if(preg_match("/c/",$acl)){
809         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
810         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
811         $this->dialog->set_acl_base($this->base);
812         $this->dialog->parent = &$this;
814         $this->is_dialog = false;
815       }else{
816         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
817       }
818     }
821     /****************
822       Get from ask class name dialog 
823      ****************/
825     if($s_action == "select_class_name_finished"){
826       $this->dialog->save_object();
827       if(count($this->dialog->check())!=0){
828         foreach($this->dialog->check() as $msg){
829           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
830         }               
831       }elseif(isset($this->dialog->objectClass)){
832         $this->dn = "new" ;
833         $a_setup= $this->get_type(array("objectClass"=>array($this->dialog->objectClass)));
834         $name = $this->dialog->save();
836         if(class_exists($a_setup[0])){
837           $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
838           $this->dialog->set_acl_base($this->base);
839           $this->dialog->by_object[$a_setup[1]]->cn = $name;
840           $this->dialog->parent = &$this;
841           $this->is_dialog = true;
842         }
843       }         
844     }   
847     /****************
848      Cancel dialogs 
849      ****************/
851                 if(isset($_POST['edit_cancel'])){
852                         $this->dialog=FALSE;
853                         $this->is_dialog = false;
854                         session::un_set('objectinfo');
855       $this->remove_lock();
856                 }
859     /****************
860       Save sub dialogs 
861      ****************/
863                 /* This check if the given tab could be saved 
864                  * If it was possible to save it, remove dialog object. 
865                  * If it wasn't possible, show errors and keep dialog.
866                  */
867                 if((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (isset($this->dialog->config))){
868                         $this->dialog->save_object();
869       $msgs= $this->dialog->check();
870                         if(count($msgs)!=0){
871                                 foreach($msgs as $msg){
872           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
873                                 }
874                         }else{
875                                 $this->dialog->save();
876         FAI::save_release_changes_now();
877         if (!isset($_POST['edit_apply'])){
878           $this->remove_lock();
879           $this->dialog=FALSE;
880           $this->is_dialog=false;
881           session::un_set('objectinfo');
882         }else{
884           /* Reinitialize tab */
885           if($this->dialog instanceof tabs){
886             $this->dialog->re_init();
887           }
888         }
889                         }
890                 }
893     /****************
894       Display currently open dialog 
895      ****************/
897                 /* If dialog is set, but $this->is_dialog==false, then 
898                  *  only the "abort" button is shown, this are dialogs that must not be saved.  
899                  * If is_dialog == true, we are currently editing tab objects.
900                  *  Here we need both, save and cancel
901                  */ 
903                 if(is_object($this->dialog)){
904                         $display .= $this->dialog->execute();
905                         /* Don't show buttons if tab dialog requests this */
907       if(isset($this->dialog->current)){
909         $obj = $this->dialog->by_object[$this->dialog->current];
911         if((isset($obj->is_dialog) && (!$obj->is_dialog)) || (isset($obj->dialog) && (!$obj->dialog))){
913           $display.= "<p style=\"text-align:right\">\n";
914           if(!$this->no_save){
915             $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
916             $display.= "&nbsp;\n";
917             if ($this->dn != "new"){
918               $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
919               $display.= "&nbsp;\n";
920             }
921           }
922           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
923           $display.= "</p>";
924         }elseif(!isset($this->dialog->current)){
925           $display.= "<p style=\"text-align:right\">\n";
926           $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
927           $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
928           $display.= "</p>";
929         }
930       }else{
931         $display.= "<p style=\"text-align:right\">\n";
932         $display.= "<input type=\"submit\" name=\"edit_continue\" value=\""._("Continue")."\">&nbsp;";
933         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
934         $display.= "</p>";
936       }
937       return($display);
938                 }
939                 
941     /****************
942       Dialog display
943      ****************/
945     /* Check if there is a snapshot dialog open */
946     $base = $this->fai_base;
947     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
948       return($str);
949     }
951     /* Display dialog with system list */
952     $this->reload();
953     $this->DivListFai->parent = &$this;
954     $this->DivListFai->execute();
955     $this->DivListFai->setEntries($this->objects);
956     return($this->DivListFai->Draw());
957         }
960   /* Return departments, that will be included within snapshot detection */
961   function get_used_snapshot_bases()
962   {
963     $tmp = array();
964     $types = array("faipartitionou","faiscriptou","faitemplateou","faihookou","faiprofileou","faivariableou","faipackageou");
965     foreach($types as $type){
966       $tmp[] = get_ou($type).$this->fai_release;
967     }
968     return($tmp);
969   }
972   /* Get available branches for current base */
973   function getBranches($base = false,$prefix = "")
974   {
975     $ret = array("/"=>$this->fai_base);
976     $ldap = $this->config->get_ldap_link();
977     if(!$base){
978       $base = $this->fai_base;
979     }
980     $tmp = FAI::get_all_releases_from_base($base,true);
981     foreach($tmp as $dn => $name){
982       $ret[$name]=$dn;
983     }
984     ksort($ret);
985     $ret = array_flip($ret);
987     return ($ret);
988   }
989   
991   function list_get_selected_items()
992   {
993     $ids = array();
994     foreach($_POST as $name => $value){
995       if(preg_match("/^item_selected_[0-9]*$/",$name)){
996         $id   = preg_replace("/^item_selected_/","",$name);
997         $ids[$id] = $id;
998       }
999     }
1000     return($ids);
1001   }
1004   /* reload list of objects */
1005   function reload()
1006   {
1007     /* Variable initialisation */
1008     $str            = "";
1009     $Regex          = $this->DivListFai->Regex;
1010     $this->objects  = array();
1012     /* Get base */
1013     $base = $this->fai_base;
1014     if($this->fai_release != $this->fai_base){
1015       $br = $this->getBranches();
1016       if(isset($br[$this->fai_release])){
1017         $base = $this->fai_release;
1018       }else{
1019         $base = $this->fai_base;
1020       }
1021     }
1022     $this->base = $base;
1023     $this->set_acl_base($this->base);
1025     $this->lock_type = FAI::get_release_tag(FAI::get_release_dn($base));
1027     /* Create a new list of FAI object 
1028      * Generate List of Partitions,Hooks,Scripts,Templates,Profiles ... 
1029      */
1030     $ObjectTypes = array(
1031         "FAIpartitionTable"  => array("OU"=> get_ou('faipartitionou') , "CHKBOX"=>"ShowPartitions"  ,"ACL" => "faiPartitionTable"),
1032         "FAIpackageList"     => array("OU"=> get_ou('faipackageou')   , "CHKBOX"=>"ShowPackages"    ,"ACL" => "faiPackage"),
1033         "FAIscript"          => array("OU"=> get_ou('faiscriptou')    , "CHKBOX"=>"ShowScripts"     ,"ACL" => "faiScript"),
1034         "FAIvariable"        => array("OU"=> get_ou('faivariableou')  , "CHKBOX"=>"ShowVariables"   ,"ACL" => "faiVariable"),
1035         "FAIhook"            => array("OU"=> get_ou('faihookou')      , "CHKBOX"=>"ShowHooks"       ,"ACL" => "faiHook"),
1036         "FAIprofile"         => array("OU"=> get_ou('faiprofileou')   , "CHKBOX"=>"ShowProfiles"    ,"ACL" => "faiProfile"),
1037         "FAItemplate"        => array("OU"=> get_ou('faitemplateou')  , "CHKBOX"=>"ShowTemplates"   ,"ACL" => "faiTemplate"));
1039     $filter = "";
1040     foreach($ObjectTypes as $key => $data){
1041       if($this->DivListFai->$data['CHKBOX']){
1042         $filter.= "(objectClass=".$key.")";
1043       }
1044     }
1045     $filter = "(&(|".$filter.")(cn=$Regex))";
1046     
1047     /* Get resolved release dependencies */
1048     $tmp = FAI::get_all_objects_for_given_base($base,$filter);
1050     /* Ge listed ldap objects */
1051     $ldap = $this->config->get_ldap_link();
1052     $ldap->cd($this->config->current['BASE']);
1054     /* Get release tag 
1055        If current release is freezed, all objects are freezed to.
1056      */
1057     $ldap->cat($base);
1058     $release_attrs = $ldap->fetch();
1059     $force_freezed = FALSE;
1060     if(isset($release_attrs['FAIstate'][0]) && preg_match("/freeze/i",$release_attrs['FAIstate'][0])){
1061       $force_freezed = TRUE;
1062     }
1064     foreach($tmp as $entry){
1066       /* Get some more informations about the object */ 
1067       $ldap->cat($entry['dn'], array("cn","description","objectClass","FAIclass","FAIstate","objectClass"));
1068       $object  = $ldap->fetch();
1070       /* Walk through possible types */
1071       foreach($ObjectTypes as $type => $rest){  
1073         $acl = $this->ui->get_permissions($object['dn'],"fai/".$rest ['ACL']);
1075         if(in_array($type,$object['objectClass']) && preg_match("/r/",$acl)){
1077           /* Prepare object */
1078           unset($object['objectClass']['count']);
1079           if(!isset($object['description'][0])){
1080             $object['description'][0]="";
1081           }
1083           /* Clean up object informations */
1084           $obj                  = array();
1085           $obj['cn']                          = $object['cn'][0];
1086           $obj['dn']                          = $object['dn'];
1087           $obj['acl']                       = $acl;
1088           $obj['description']   = $object['description'][0];
1089           $obj['objectClass']   = $object['objectClass'];
1091           /* Append type to this string, to be able to check if the selected 
1092            * entry is of type 'freeze' or 'branch'
1093            */
1094           if(!isset($object['FAIstate'])){
1095             $obj['FAIstate'] = $this->lock_type;
1096           }else{
1097             $obj['FAIstate'] = $object['FAIstate'][0]; 
1098           }
1100           if($force_freezed){
1101             $obj['FAIstate'] = "freeze";
1102           }
1104           $this->objects[strtolower($obj['cn']).$obj['cn'].$type] = $obj;
1105           $this->objects[strtolower($obj['cn']).$obj['cn'].$type]['type']=$type;
1106         }
1107                         }
1108                 }
1110                 ksort($this->objects);
1111                 reset ($this->objects);
1112         
1113                 /* use numeric index, thats a bit more secure */        
1114                 $tmp0 = array();
1115                 foreach($this->objects as $obj){
1116                         $tmp0[]= $obj;
1117                 }
1118                 $this->objects = array();
1119                 $this->objects = $tmp0;
1120         }
1122         function remove_lock()
1123         {
1124                 if (isset($this->dn)){
1125                         del_lock ($this->dn);
1126                 }
1127     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
1128       del_lock ($this->dns);
1129     }
1130         }
1132         function get_type($array){
1133                 if(in_array("FAIpartitionTable",$array['objectClass'])){
1134                         return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
1135                 }
1136                 if(in_array("FAIscript",$array['objectClass'])){
1137                         return(array("tabsScript","faiScript","FAISCRIPTTABS"));
1138                 }
1139                 if(in_array("FAItemplate",$array['objectClass'])){
1140                         return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
1141                 }
1142                 if(in_array("FAIhook",$array['objectClass'])){
1143                         return(array("tabsHook","faiHook","FAIHOOKTABS"));
1144                 }
1145                 if(in_array("FAIvariable",$array['objectClass'])){
1146                         return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
1147                 }
1148                 if(in_array("FAIprofile",$array['objectClass'])){
1149                         return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
1150                 }
1151                 
1152                 if(in_array("FAIpackageList",$array['objectClass'])){
1153                         return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
1154                 }
1155         }
1157   function CheckNewBranchName($name,$base)
1158   {
1159     $f = $this->fai_release;
1160     if($name == ""){
1161       return(false);
1162     }elseif(in_array($name,$this->getBranches($f))) {
1163       return(false);
1164     }elseif(tests::is_department_name_reserved($name,$base)){
1165       return(false);
1166     }
1167     return(true);
1168   }
1170   function save_object()
1171   {
1172     $this->DivListFai->save_object();
1174     /* Get posted release */
1175     $r_releases = array_flip($this->getBranches());
1176     if(isset($_POST['fai_release']) && isset($r_releases[get_post('fai_release')])){
1178       /* Ensure that we have a valid release selected */
1179       if(!isset($r_releases[get_post('fai_release')])){
1180         msg_dialog::display(_("Warning"),_("The selected release is not available anymore. All triggered actions are skipped."));
1181         $_POST = array();
1182         $plug =$_GET['plug'];
1183         $_GET  = array("plug" => $plug);
1184         $this->fai_release = $this->fai_base;
1185       }else{
1186         $this->fai_release = $r_releases[get_post('fai_release')];
1187       }
1189       $fai_filter = session::get("fai_filter");
1190       $fai_filter['fai_release'] = $this->fai_release;
1191       session::set("fai_filter",$fai_filter);
1192     }
1194     if(is_object($this->CopyPasteHandler)){
1195       $this->CopyPasteHandler->save_object();
1196     }
1197   }
1200   function copyPasteHandling_from_queue($s_action,$s_entry)
1201   {
1202     /* Check if Copy & Paste is disabled */
1203     if(!is_object($this->CopyPasteHandler)){
1204       return("");
1205     }
1208     /* Add a single entry to queue */
1209     if($s_action == "copy"){
1211       /* Cleanup object queue */
1212       $this->CopyPasteHandler->cleanup_queue();
1213       $entry    = $this->objects[$s_entry];
1214       $a_setup  = $this->get_type($entry);
1215       $dn = $entry['dn'];
1216       $this->CopyPasteHandler->add_to_queue($dn,$s_action,$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1217     }
1219     /* Add entries to queue */
1220     if($s_action == "copy_multiple"){
1222       /* Cleanup object queue */
1223       $this->CopyPasteHandler->cleanup_queue();
1225       /* Add new entries to CP queue */
1226       foreach($this->list_get_selected_items() as $id){
1228         /* Cleanup object queue */
1229         $entry    = $this->objects[$id];
1230         $a_setup  = $this->get_type($entry);
1231         $dn = $entry['dn'];
1234         if($s_action == "copy_multiple"){
1235           $this->CopyPasteHandler->add_to_queue($dn,"copy",$a_setup[0],$a_setup[2],"fai");//$a_setup[1]);
1236         }
1237       }
1238     }
1240     /* Start pasting entries */
1241     if($s_action == "editPaste" && !($this->lock_type == "freeze" && !$this->allow_freeze_object_attach)){
1242       $this->start_pasting_copied_objects = TRUE;
1243     }
1245     /* Return C&P dialog */
1246     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
1248       /* Get dialog */
1249       $this->CopyPasteHandler->SetVar("parent",$this);
1250       $data = $this->CopyPasteHandler->execute();
1252       FAI::save_release_changes_now();
1254       /* Return dialog data */
1255       if(!empty($data)){
1256         return($data);
1257       }
1258     }
1260     /* Automatically disable status for pasting */
1261     if(!$this->CopyPasteHandler->entries_queued()){
1262       $this->start_pasting_copied_objects = FALSE;
1263     }
1264     return("");
1265   }
1268   /* Check if the given FAI class is used in this release 
1269    */
1270   static function check_class_name($oc,$name,$dn)
1271   {
1272     $base = FAI::get_release_dn($dn);
1273     $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
1274     $delete = array();
1275     $used   = array();
1276     foreach($res as $object){
1277       $used[$object['cn'][0]]= $object['cn'][0];
1278     }
1279     return($used);
1280   }
1283   /* Return plugin informations for acl handling */ 
1284   static function plInfo()
1285   {
1286     return (array( 
1287           "plShortName"   => _("FAI releases"),
1288           "plDescription" => _("FAI release management"),
1289           "plSelfModify"  => FALSE,
1290           "plDepends"     => array(),
1291           "plPriority"    => 0,
1292           "plSection"     => array("administration"),           
1293           "plCategory"    => array("fai"=> array("description" => _("FAI"),
1294                                                  "objectClass" => "FAIclass")),
1295           "plProvidedAcls"=> array()));
1296   }
1298 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1299 ?>