Code

Enabled multiple remove
[gosa.git] / gosa-plugins / fai / admin / fai / class_faiManagement.inc
1 <?php
2 /*
3  * This code is part of GOsa (http://www.gosa-project.org)
4  * Copyright (C) 2003-2008 GONICUS GmbH
5  *
6  * ID: $$Id: class_roleManagement.inc 14742 2009-11-04 13:18:33Z hickert $$
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
23 class faiManagement extends management
24 {
25   var $plHeadline     = "Software deployment";
26   var $plDescription  = "Manage software packages and deployment reciepes";
27   var $plIcon  = "plugins/fai/images/plugin.png";
29   // Tab definition 
30   protected $tabClass = "roletabs";
31   protected $tabType = "ROLETABS";
32   protected $aclCategory = "roles";
33   protected $aclPlugin   = "role";
34   protected $objectName   = "role";
36   //  Attributes Managed by this plugin can be used in post events
37   protected $attributes   = array("lock_type","lock_name","lock_dn");  
39   var $dispNewBranch=false;
40   var $dispNewFreeze=false;
42   var $fai_release = ""; // The currently selected release while in release management mode!
43   var $fai_base = ""; 
44   var $acl_base = ""; 
46   var $lock_type = "";
47   var $lock_dn = "";
48   var $lock_name = "";
50   var $opsi = NULL;
53   function __construct($config,$ui)
54   {
55     $this->config = $config;
57     /* Check if the opsi plugin is installed.
58      */
59     if(class_available("opsi")){
60       $this->opsi = new opsi($this->config);;
61     }
63     $this->fai_base = get_ou("faiBaseRDN").$this->config->current['BASE'];
64     $this->fai_release = $this->fai_base;
65     $this->acl_base = $this->config->current['BASE'];
66     $this->ui = $ui;
67     $this->storagePoints = array(
68         get_ou('faiPartitionRDN'),
69         get_ou('faiPackageRDN'),
70         get_ou('faiScriptRDN'),
71         get_ou('faiVariableRDN'),
72         get_ou('faiHookRDN'),
73         get_ou('faiProfileRDN'),get_ou('faiTemplateRDN'));
75 #    // Build filter
76 #    if (session::global_is_set(get_class($this)."_filter")){
77 #      $filter= session::global_get(get_class($this)."_filter");
78 #    } else {
79   $filter = new filter(get_template_path("fai-filter.xml", true));
80   $filter->setObjectStorage($this->storagePoints);
81 #    }
82   $this->setFilter($filter);
84   // Build headpage
85   $headpage = new listing(get_template_path("fai-list.xml", true));
86   $headpage->setFilter($filter);
87   $headpage->setBase($this->fai_release);
88   $headpage->registerElementFilter("filterProperties", "faiManagement::filterProperties");
90   // Add copy&paste and snapshot handler.
91   if ($this->config->boolValueIsTrue("main", "copyPaste")){
92     $this->cpHandler = new CopyPasteHandler($this->config);
93   }
94   if($this->config->get_cfg_value("enableSnapshots") == "true"){
95     $this->snapHandler = new SnapshotHandler($this->config);
96   }
98   $this->registerAction("remove_multiple","removeEntryRequested");
100   $this->registerAction("new_profile","newEntry");
101   $this->registerAction("new_template","newEntry");
102   $this->registerAction("new_script","newEntry");
103   $this->registerAction("new_hook","newEntry");
104   $this->registerAction("new_variable","newEntry");
105   $this->registerAction("new_package","newEntry");
106   $this->registerAction("new_partition","newEntry");
108   $this->registerAction("newClassNameSelected","newClassNameSelected");
110   $this->registerAction("saveOpsiProperties","saveOpsiProperties");
112   $this->registerAction("editByGroup","editByGroup");
113   $this->registerAction("createBranch","createBranch");
114   $this->registerAction("createFreeze","createFreeze");
115   $this->registerAction("removeBranch","removeBranch");
116   $this->registerAction("removeBranchConfirmed","removeBranchConfirmed");
117   $this->registerAction("saveBranch","saveBranch");
118   $this->registerAction("PerformBranch","PerformBranch");
119   parent::__construct($config, $ui, "roles", $headpage);
120   }
123   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
124   {
125     /****************
126       Create a new object
127      ****************/
129     $types = array( 
130         "new_partition"     =>  "FAIpartitionTable",
131         "new_script"        =>  "FAIscript",
132         "new_hook"          =>  "FAIhook",
133         "new_variable"      =>  "FAIvariable",
134         "new_template"      =>  "FAItemplate",
135         "new_package"       =>  "FAIpackageList");
136     $types_i18n = array( 
137         "new_partition"     =>  _("partition table"),
138         "new_script"        =>  _("script"),
139         "new_hook"          =>  _("hook"),
140         "new_variable"      =>  _("variable"),
141         "new_template"      =>  _("template"),
142         "new_package"       =>  _("package list"));
144     if(isset($types[$action])){
145       $type_acl_mapping = array(
146           "FAIpartitionTable"  => "faiPartitionTable",
147           "FAIpackageList"     => "faiPackage",
148           "FAIscript"          => "faiScript",
149           "FAIvariable"        => "faiVariable",
150           "FAIhook"            => "faiHook",
151           "FAIprofile"         => "faiProfile",
152           "FAItemplate"        => "faiTemplate");
154       $acl = $this->ui->get_permissions($this->acl_base,"fai/".$type_acl_mapping[$types[$action]]);
155       if(preg_match("/c/",$acl)){
156         $this->dialogObject = new askClassName($this->config,$this->dn,$this->ui,$types[$action]);
157         $this->dialogObject->parent = &$this;
158       }else{
159         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$action]),      ERROR_DIALOG);
160       }
161     }
162     if($action == "new_profile"){
163       $this->dn = "new" ;
165       $acl = $this->ui->get_permissions($this->acl_base,"fai/faiProfile");
166       if(preg_match("/c/",$acl)){
167         $type= $this->get_type(array("objectClass"=>array("FAIprofile")));
168         $str= management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
169         if($str) return($str);
170         $this->tabObject->set_acl_base($this->acl_base);
171         $this->tabObject->by_object[$type[1]]->cn = $name;
172       }else{
173         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
174       }
175     }
176   }
179   function newClassNameSelected()
180   {
181     $this->dialogObject->save_object();
182     if(count($this->dialogObject->check())!=0){
183       foreach($this->dialogObject->check() as $msg){
184         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
185       }
186     }elseif(isset($this->dialogObject->objectClass)){
187       $this->dn = "new" ;
188       $type= $this->get_type(array("objectClass"=>array($this->dialogObject->objectClass)));
189       $name = $this->dialogObject->save();
191       if(class_exists($type[0])){
192         $this->closeDialogs();
193         $str = management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
194         if($str) return($str);
195         $this->tabObject->set_acl_base($this->acl_base);
196         $this->tabObject->by_object[$type[1]]->cn = $name;
197       }
198     }
199   }
203   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
204   {
205     $headpage = $this->getHeadpage();
206     if(count($target) == 1){
207       $entry = $headpage->getEntry($target[0]);
208       if($entry){
210         if(in_array("opsi_local", $entry['TYPES']) || in_array("opsi_netboot", $entry['TYPES'])){ 
211           $name = $entry['cn'];
212           $cfg = $this->opsi->get_product_properties($name);
214           $str = management::editEntry('editEntry',array($name),array(),'tabs_opsiProdConfig','OPSIPRODCONFIG','opsi');
215           if($str) return($str);
216           if(isset($this->tabObject->by_object['opsiProperties'])){
217             $this->tabObject->by_object['opsiProperties']->set_cfg($cfg);
218             $this->tabObject->by_object['opsiProperties']->set_product($name);
219             $this->skipFooter = TRUE;
220           }else{
221             trigger_error("Unknown tab, please check config.");
222           }
224         }else{
225           if(count($entry['GROUPS']) == 1){
226             $data = array_pop($entry['GROUPS']);
227             $type = $this->get_type($data);
228             $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
229             if($str) return($str);
230             $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
231             $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
233           }else{
234             $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"edit");
235           }
236         }
237       }
238     }
239   }
242   function saveOpsiProperties()
243   {
244     if($this->tabObject instanceof tabs_opsiProdConfig && isset($_POST['save_properties'])){
245       $this->tabObject->save_object();
246       $op    = $this->tabObject->by_object['opsiProperties'];
247       $name  = $op->get_product();
248       $cfg   = $op->get_cfg();
249       $this->opsi->set_product_properties($name,$cfg);
250       if($this->opsi->is_error()){
251         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
252       }else{
253         $this->remove_lock();
254         $this->closeDialogs();
255       }
256     }
257   }
260   function removeEntryRequested($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
261   {
263     $this->closeDialogs();
264     if($action == "remove_multiple"){
266       /* Collect objects to delete and check if objects are freezed
267        */
268       $errors = "";
269       $headpage = $this->getHeadpage();
270       $to_delete = array();
271       foreach($target as $id){
272         $object = $headpage->getEntry($id);
273         if(in_array("FAKE_OC_FAI", $object['objectClass'])){
274           foreach($object['GROUPS']  as $entry){
276             // Only allow to remove FAI classes  
277             array_push($to_delete, $entry);
278           }
279         }
280       }
282       foreach($to_delete as $obj){
283         if(isset($obj['FAIstate']) && preg_match('/^freeze/', $obj['FAIstate'])){
284           $errors .= $obj['cn'].", ";
285         }else{
286           $this->dns[] = $obj['dn'];
287         }
288       }
290       if($errors != ""){
291         msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
292               "<br><br>".trim($errors,", ")),INFO_DIALOG);
293       }
294       /* Check locking
295        */
296       if(count($this->dns)){
297         if ($user= get_multiple_locks($this->dns)){
298           return(gen_locked_message($user,$this->dns));
299         }
300         if(count($this->dns)){
301           $smarty = get_smarty();
302           $dns_names = array();
303           foreach($this->dns as $dn){
304             add_lock ($dn, $this->ui->dn);
305             $dns_names[] = LDAP::fix($dn);
306           }
307           $smarty->assign("info",msgPool::deleteInfo($dns_names,_("FAI object")));
308           $smarty->assign("multiple", true);
309           return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
310         }
311       }
313     }else{
315       // Try to remove a single object, only FAI objects can be removed!
316       $headpage = $this->getHeadpage();
317       if(count($target) == 1){
318         $entry = $headpage->getEntry($target[0]);
319         if($entry && !in_array("FAKE_OC_OPSI",$entry['objectClass'])){
320           $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"remove");
321         }
322       }
323     }
324   }
326   /*! \brief  Object removal was confirmed, now remove the requested entries.
327    *
328    *  @param  String  'action'  The name of the action which was the used as trigger.
329    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
330    *  @param  Array   'all'     A combination of both 'action' and 'target'.
331    */
332   function removeEntryConfirmed($action="",$target=array(),$all=array(),
333       $altTabClass="",$altTabType="",$altAclCategory="")
334   {
335     $ldap = $this->config->get_ldap_link();
336     $ldap->cd($this->config->current['BASE']); 
338     foreach($this->dns as $key => $dn){
339       $ldap->cat($dn);
340       if($ldap->count()){
341         $attrs = $ldap->fetch();
342         $type= $this->get_type($attrs);
343         $str = management::removeEntryConfirmed($action,array($dn),$all,$type[0],$type[2],$type[1]);
344         if($str) return($str);
346         // Now save changes
347         FAI::save_release_changes_now();
348         $to_del = FAI::clean_up_releases($dn);
349         foreach($to_del as $dn){
350           $ldap->rmdir_recursive($dn);
351         }
352       }
353     }
354   }
357   function editByGroup()
358   {
359     if($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "edit"){
360       $this->dialogObject->save_object();
361       $entry = $this->dialogObject->get_selected();
362       $this->closeDialogs();
363       $data = array_pop($entry);
364       $type = $this->get_type($data);
365       $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
366       if($str) return($str);
367       $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
368       $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
370     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "remove"){
371       $this->dialogObject->save_object();
372       $to_delete = $entry = $this->dialogObject->get_selected();
373       $dns = array();
376       // Check FAIstate to ensure that we do not remove frozen objects
377       //  additionally check ACLs
378       $locked = array();
379       $disallowed = array();
380       foreach($to_delete as $obj){
381         if(isset($obj['FAIstate']) && preg_match('/^freeze/', $obj['FAIstate'])){
382           $locked[] = $obj['dn'];
383         }else{
384           $type = $this->get_type($obj);
385           $acl = $this->ui->get_permissions($obj['dn'], 'acl/'.$type[1]);
386           if(!preg_match("/d/",$acl)){
387             $disallowed[] = $obj['dn']; 
388           }else{
389             $dns[] = $obj['dn'];
390           }
391         }
392       }
394       // Display info dialog about locked and not removeable entries
395       if(count($locked)){
396         msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
397             msgPool::buildList($locked)));
398       }
400       // Display info dialog about not removeable entries due to permissions
401       if(count($disallowed)){
402         msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
403       }
405       // There are entries left to be removed
406       if(count($dns)){  
407         $this->closeDialogs();
408         $this->dns = $dns;
410         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
412         // check locks
413         if ($user= get_multiple_locks($this->dns)){
414           return(gen_locked_message($user,$this->dns));
415         }
417         // Add locks
418         $dns_names = array();
419         foreach($this->dns as $dn){
420           $dns_names[] =LDAP::fix($dn);
421         }
422         add_lock ($this->dns, $this->ui->dn);
424         // Display confirmation dialog.
425         $smarty = get_smarty();
426         $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
427         $smarty->assign("multiple", true);
428         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
429       }else{
430         $this->closeDialogs();
431       }
432     }
433   }
435   
436   protected function saveChanges()
437   {
438     $str = management::saveChanges();
439     if($str) return($str);
441     // Now save changes
442     FAI::save_release_changes_now();
443     $to_del = FAI::clean_up_releases($this->last_dn);
444     foreach($to_del as $dn){
445       $ldap->rmdir_recursive($dn);
446     }
447   }
449   protected function applyChanges()
450   {
451     $str = management::applyChanges();
452     if($str) return($str);
454     // Now save changes
455     FAI::save_release_changes_now();
456     $to_del = FAI::clean_up_releases($this->last_dn);
457     foreach($to_del as $dn){
458       $ldap->rmdir_recursive($dn);
459     }
460   }
462   function detectPostActions()
463   {
464     $action = management::detectPostActions();
465     if(isset($_POST['remove_multiple'])) $action['action'] = "remove";
466     if(isset($_POST['new_profile'])) $action['action'] = "new_profile";
467     if(isset($_POST['new_template'])) $action['action'] = "new_template";
468     if(isset($_POST['new_script'])) $action['action'] = "new_script";
469     if(isset($_POST['new_hook'])) $action['action'] = "new_hook";
470     if(isset($_POST['new_variable'])) $action['action'] = "new_variable";
471     if(isset($_POST['new_package'])) $action['action'] = "new_package";
472     if(isset($_POST['new_partition'])) $action['action'] = "new_partition";
475     if(isset($_POST['save_properties'])) $action['action'] = "saveOpsiProperties";
476     if(isset($_POST['cancel_properties'])) $action['action'] = "cancel";
478     if(isset($_POST['edit_continue'])) $action['action'] = "newClassNameSelected";
479     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
481     if(isset($_POST['faiGroupHandle_cancel'])) $action['action'] = "cancel";
482     if(isset($_POST['CancelBranchName'])) $action['action'] = "cancel";
483     if(isset($_POST['delete_branch_confirm'])) $action['action'] = "removeBranchConfirmed";
484     if(isset($_GET['PerformBranch'])) $action['action'] = "PerformBranch";
485     if(isset($_POST['UseBranchName'])) $action['action'] = "saveBranch";
486     if(isset($_POST['faiGroupHandle_apply']))  $action['action'] = "editByGroup";
487     if(isset($_GET['act']) && $_GET['act'] == "branch_branch")  $action['action'] = "createBranch";
488     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch")  $action['action'] = "createFreeze";
489     if(isset($_GET['act']) && $_GET['act'] == "remove_branch")  $action['action'] = "removeBranch";
491     foreach($_POST as $name => $value){
492       if(preg_match("/^edit_([0-9]*)_([a-z]*)_(x|y)/i", $name)){
493         $id = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\1", $name);
494         $tab = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\2", $name);
495         $headpage = $this->getHeadpage();
496         if(isset($headpage->entries[$id]['GROUPS'][$tab])){
497           $data =$headpage->entries[$id]['GROUPS'][$tab];
498           $type = $this->get_type($data);
499           $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]); 
500           if($str) return($str);
501         } 
502         break;
503       }
504     }
505     return($action);
506   }
509   function renderList()
510   {
511     $filter = $this->getFilter();
512     $headpage = $this->getHeadpage();
513     $filter->setComboBoxOptions("RELEASE",$this->getReleaseList());
515     if(isset($_POST['RELEASE'])){
516       $this->fai_release = get_post('RELEASE');
517     }
518     $headpage->setBase($this->fai_release);
519     $headpage->update();
520     $smarty = get_smarty();
521     $smarty->assign("fai_release", $this->fai_release);
522     $smarty->assign("opsi_available", is_object($this->opsi));
523     $smarty->assign("fai_base", $this->fai_base);
524     $r = $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'));
525     $c = $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'));
526     $smarty->assign("allow_create", $c);
527     $smarty->assign("allow_remove", $r);
528     $display = $headpage->render();
529     return($this->getHeader().$display);
530   }
532   function getReleaseList($base = "", $prefix ="")
533   {
534     $list = array();
535     if(empty($base)){
536       $base = $this->fai_base;
537       $list[$base] = "/";
538     }
540     $ldap = $this->config->get_ldap_link();
541     $ldap->ls("(objectClass=FAIbranch)",$base,array("ou","FAIstate"));
542     $cfg_rel = $this->config->search("faiManagement","DEFAULTFAIRELEASE",array("menu"));
544     while($release = $ldap->fetch()){
545       $list[$release['dn']] = $prefix.$release['ou'][0];
547       // Preset to prefered releaes if necessary  
548       if(empty($this->fai_release) && $cfg_rel == $release['dn']){
549         $this->fai_release = $release['dn'];
550       }
552       $list = array_merge($list,$this->getReleaseList($release['dn'],$prefix."&nbsp; "));
553     }
554     return($list);
555   }
558   static function filterProperties($row, $classes)
559   {
560     $objects = array(
561         "FAIpartitionTable"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png",
562           "NAME"=>_("Partition table"),"KZL"=> "PT", "VAR"=>"ShowPartitions"),
563         "FAIpackageList"     => array("IMG"=> "plugins/fai/images/fai_packages.png",
564           "NAME"=>_("Package list") ,  "KZL"=> "PL", "VAR"=>"ShowPackages"),
565         "FAIscript"          => array("IMG"=> "plugins/fai/images/fai_script.png",
566           "NAME"=>_("Scripts") ,       "KZL"=> "S",  "VAR"=>"ShowScripts"),
567         "FAIvariable"        => array("IMG"=> "plugins/fai/images/fai_variable.png",
568           "NAME"=>_("Variables") ,     "KZL"=> "V",  "VAR"=>"ShowVariables"),
569         "FAIhook"            => array("IMG"=> "plugins/fai/images/fai_hook.png",
570           "NAME"=>_("Hooks"),          "KZL"=> "H",  "VAR"=>"ShowHooks"),
571         "FAIprofile"         => array("IMG"=> "plugins/fai/images/fai_profile.png",
572           "NAME"=>_("Profile") ,       "KZL"=> "P",  "VAR"=>"ShowProfiles"),
573         "FAItemplate"        => array("IMG"=> "plugins/fai/images/fai_template.png",
574           "NAME"=>_("Templates") ,     "KZL"=> "T",  "VAR"=>"ShowTemplates"),
575         "opsi_netboot"       => array("IMG"=> "plugins/opsi/images/netboot_package.png",
576           "NAME"=>_("OPSI netboot product") ,     "KZL"=> "ON",  "VAR"=>"ShowOpsiNetboot"),
577         "opsi_local"         => array("IMG"=> "plugins/opsi/images/local_package.png",
578           "NAME"=>_("OPSI localboot product")   ,     "KZL"=> "OL",  "VAR"=>"ShowOpsiLocal"));
580     $icon_list = "";
581     foreach($objects as $type => $type_data){
582       if(in_array($type, $classes)){
583         $icon_list .= "<input type='image' src='".$type_data['IMG']."' title='".$type_data['NAME']."'
584           alt='".$type_data['KZL']."' class='center' name='edit_".$row."_".$type."'>\n";
585       }else{
586         $icon_list .= "<img src='images/empty.png' alt=' ' class='center'>\n";
587       }
588     }
590     return $icon_list;
591   }
596   function removeBranch()
597   {
598     /* Check if we have a post remove method configured
599      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
600      */
601     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
602       /* Load permissions for selected 'dn' and check if
603          we're allowed to remove this 'dn' */
604       if(preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
605         $smarty=get_smarty();
606         $smarty->assign("release_hidden",base64_encode($this->fai_release));
607         $smarty->assign("info", msgPool::deleteInfo(LDAP::fix($this->fai_release),_("FAI branch/freeze")));
608         return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
609       } else {
610         msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
611       }
612     }
613   }
616   function removeBranchConfirmed()
617   {
618     /* Check if we have a post remove method configured
619      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
620      */
621     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
623       if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
624         msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
625       }else{
627         $bb =  $this->fai_release;
628         $ldap = $this->config->get_ldap_link();
630         $br = $this->getBranches();
632         if(isset($br[$bb]) && preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
633           $name = $br[$bb];
635           $ldap->cd($bb);
636           $ldap->recursive_remove();
637           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('applicationRDN'), $bb));
638           $ldap->recursive_remove();
639           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('mimetypeRDN'), $bb));
640           $ldap->recursive_remove();
642           /* Search for all groups with configured application menus.
643              - First search all groups, to ensure that we only remove entries form whithin groups.
644              - The search für menu configuration for the specified release and collect all those dns.
645              - Remove entries
646            */
647           $release_ou = preg_replace("/".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$bb);
648           $ldap->cd($this->config->current['BASE']);
649           $ldap->search("(objectClass=posixGroup)",array("dn"));
651           /* Collect all group dns
652            */
653           $groups = array();
654           while($attrs = $ldap->fetch()){
655             $groups[] = $attrs['dn'];
656           }
659           /* Collect all group menu release dns that match the release we have removed
660            */
661           $dns = array();
662           foreach($groups as $dn){
663             $ldap->cd($dn);
664             $ldap->search("(objectClass=FAIbranch)",array("dn"));
665             while($attrs = $ldap->fetch()){
666               if(preg_match("/^".preg_quote($release_ou, '/')."/",$attrs['dn'])){
667                 $dns[] = $attrs['dn'];
668               }
669             }
670           }
672           /* Finally remove collected release dns
673            */
674           foreach($dns as $dn){
675             $ldap->cd($dn);
676             $ldap->recursive_remove();
677           }
679           /* Post remove */
680           $this->fai_release = $this->fai_base;
681           $this->lock_name   = $name;
682           $this->lock_dn     = $bb;
683           $this->postremove();
685           $fai_filter = session::get("fai_filter");
686           $fai_filter['fai_release'] = $this->fai_release;
687           session::set("fai_filter",$fai_filter);
689           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
690         }
691       }
692     }
693   }
696   function createBranch()
697   { 
698     $smarty = get_smarty();
699     $this->dispNewBranch=true;
700     $this->dispNewFreeze=false;
701     $smarty->assign("iframe",false);
702     if(isset($_POST['BranchName'])){
703       $smarty->assign("BranchName", $_POST['BranchName']);
704     }else{
705       $smarty->assign("BranchName","");
706     }
707     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
708   }
710   function createFreeze()
711   { 
712     $smarty = get_smarty();
713     $this->dispNewFreeze=true;
714     $this->dispNewBranch=false;
715     $smarty->assign("iframe",false);
716     if(isset($_POST['BranchName'])){
717       $smarty->assign("BranchName", $_POST['BranchName']);
718     }else{
719       $smarty->assign("BranchName","");
720     }
721     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
722   }
726   function PerformBranch()
727   {
728     if(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
729       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
730     }else{
732       /* In order to see error messages we have to reset the error handler.
733          Due to the exit();
734        */
735       restore_error_handler();
737       $this->dispNewBranch = false;
738       $this->dispNewFreeze = false;
740       $LASTPOST = session::get('LASTPOST');
741       $base = $LASTPOST['base'];
742       $_POST  = session::get('LASTPOST');
743       $name = $_POST['BranchName'];
745       $type = $LASTPOST['type'];
746       $ldap = $this->config->get_ldap_link();
748       $baseToUse = $base;
749       if($this->fai_release !=  $this->fai_base){
750         $baseToUse = $this->fai_release;
751       }
753       /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
755       $CurrentReleases  = $this->getBranches();
756       $NewReleaseName   = $name;
757       if(isset($CurrentReleases[$this->fai_release])) {
758         if($this->fai_release != $this->fai_base){
759           $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
760           $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName);
761         }else{
762           $NewReleaseName   = $name;
763         }
764       }
765       $appsrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),$baseToUse);
766       $appdst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),"ou=".$name.",".$baseToUse) ;
768       $mimesrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),$baseToUse);
769       $mimedst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),"ou=".$name.",".$baseToUse) ;
771       /* Check if source depeartments exist */
772       foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
773         $ldap->cd($this->config->current['BASE']);
774         $ldap->cat($dep);
775         if(!$ldap->count()){
776           $ldap->create_missing_trees($dep);
777         }
778       }
780       /* Print header to have styles included */
781       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
782         <html>
783         <head>
784         <title></title>
785         <style type="text/css">@import url("themes/default/style.css");</style>
786         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
787         </head>
788         <body style="background: none;margin:3px;color:black">
789         ';
791       new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
793       /* Duplicate group application releases
794        */
795       FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
797       /* Duplicate applications
798        */
799       $ldap->cat($appsrc,array("dn")) ;
800       if($ldap->count()){
801         $ldap->cd ($appdst);
802         $ldap->recursive_remove();
803         FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
804       }
806       /* Duplicate mime types
807        */
808       $ldap->cat($mimesrc,array("dn")) ;
809       if($ldap->count()){
810         $ldap->cd ($mimedst);
811         $ldap->recursive_remove();
812         FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
813       }
815       $attr = array();
816       $attr['objectClass'] = array("organizationalUnit","FAIbranch");
817       $attr['ou']       = $name;
818       $attr['FAIstate'] = $type;
819       $ldap->cd($this->config->current['BASE']);
820       $ldap->cd("ou=".$name.",".$baseToUse);
821       $ldap->cat("ou=".$name.",".$baseToUse);
822       if($ldap->count()){
823         $ldap->modify($attr);
824       }else{
825         $ldap->add($attr);
826       }
828       /* Duplicate fai objects
829        */
830       //      $ldap->cd ("ou=".$name.",".$baseToUse);
831       //      $ldap->recursive_remove();
832       //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
834       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
835         <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
836         <input type='hidden' name='php_c_check' value='1'>
837         </form></div>";
839       echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
841       /* Print footer to have valid html */
842       echo "</body></html>";
844       $this->dispNewFreeze = false;
846       /* Postcreate */
848       /* Assign possible attributes */
849       $this->lock_type  = $type;
850       $this->lock_name  = $name;
851       $this->lock_dn    = $baseToUse;
852       $this->postcreate();
854       /* Send daemon event to reload the fai release database
855        */
856       if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
857         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
858         if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
859           $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'];
860           $tmp = new $evt['CLASS_NAME']($this->config);
861           $tmp->set_type(TRIGGERED_EVENT);
862           $tmp->add_targets(array("GOSA"));
863           $o_queue = new gosaSupportDaemon();
864           if(!$o_queue->append($tmp)){
865             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
866           }
867         }
868       }else{
869         trigger_error("Unknown class DaemonEvent / gosaSupportDaemon");
870         msg_dialog::display(_("Fatal error"),
871             "Daemon events called but classes where not accessible, DaemonEvent gosaSupportDaemon",
872             FATAL_ERROR_DIALOG);
873       }
874       exit();
875     }
876   }
881   function saveBranch()
882   {
883     if($this->dispNewBranch){
884       $type = "branch";
885     }else{
886       $type = "freeze";
887     }
889     /* Check branch name */
890     $name = $_POST['BranchName'];
891     $is_ok = true;
892     $smarty = get_smarty();
893     $smarty->assign("BranchName",$name);
894     $base= $this->fai_base;
896     /* Check used characters */
897     if(!preg_match("/^[0-9a-z\.]*$/",$name)){
898       msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
899       $is_ok = false;
900     }
902     // Check if this name is already in use 
903     if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
904       msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
905       $is_ok = false;
906     }
908     // Handle errors
909     if(!$is_ok && $this->dispNewFreeze){
910       return($this->createFreeze());
911     }elseif(!$is_ok && $this->dispNewBranch){
912       return($this->createBranch());
913     }
915     // Now create new release
917     if(session::is_set('LASTPOST')){
918       $LASTPOST = session::get('LASTPOST');
919     }else{
920       $LASTPOST = array();
921     }
922     $LASTPOST['base'] = $base;
923     $LASTPOST['type'] = $type;
924     $LASTPOST['BranchName'] = $name;
925     session::set('LASTPOST',$LASTPOST);
926     $smarty->assign("iframe", true);
927     $smarty->assign("plugID", $_GET['plug']);
928     $display  = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
929     return($display);
930  
931   }
934   function CheckNewBranchName($name,$base)
935   {
936     $f = $this->fai_release;
937     if($name == ""){
938       return(false);
939     }elseif(in_array($name,$this->getBranches($f))) {
940       return(false);
941     }elseif(tests::is_department_name_reserved($name,$base)){
942       return(false);
943     }
944     return(true);
945   }
948   /* Get available branches for current base */
949   function getBranches($base = false,$prefix = "")
950   {
951     $ret = array("/"=>$this->fai_base);
952     $ldap = $this->config->get_ldap_link();
953     if(!$base){
954       $base = $this->fai_base;
955     }
956     $tmp = FAI::get_all_releases_from_base($base,true);
957     foreach($tmp as $dn => $name){
958       $ret[$name]=$dn;
959     }
960     ksort($ret);
961     $ret = array_flip($ret);
963     return ($ret);
964   }
967   function get_type($array)
968   {
969     if(!isset($array['objectClass'])) return(array());
970     if(in_array("FAIpartitionTable",$array['objectClass'])){
971       return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
972     }
973     if(in_array("FAIscript",$array['objectClass'])){
974       return(array("tabsScript","faiScript","FAISCRIPTTABS"));
975     }
976     if(in_array("FAItemplate",$array['objectClass'])){
977       return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
978     }
979     if(in_array("FAIhook",$array['objectClass'])){
980       return(array("tabsHook","faiHook","FAIHOOKTABS"));
981     }
982     if(in_array("FAIvariable",$array['objectClass'])){
983       return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
984     }
985     if(in_array("FAIprofile",$array['objectClass'])){
986       return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
987     }
988     if(in_array("FAIpackageList",$array['objectClass'])){
989       return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
990     }
991     return(array());
992   }
995   /* Check if the given FAI class is used in this release
996    */
997   static function check_class_name($oc,$name,$dn)
998   {
999     $base = FAI::get_release_dn($dn);
1001     if($oc == "FAIprofile"){
1002       $f = "";
1003       $ocs = array("FAIprofile","FAItemplate","FAIhook","FAIpartitionTable","FAIpackageList","FAIscript","FAIvariable");
1004       foreach($ocs as $oc){
1005         $f .= "(objectClass=".$oc.")";
1006       }
1007       $res  = FAI::get_all_objects_for_given_base($base,"(|".$f.")",TRUE);
1008     }else{
1009       $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
1010     }
1011     $delete = array();
1012     $used   = array();
1013     foreach($res as $object){
1014       $used[$object['cn'][0]]= $object['cn'][0];
1015     }
1016     return($used);
1017   }
1020
1021 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1022 ?>