Code

Added creation Posts
[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 = "";
51   function __construct($config,$ui)
52   {
53     $this->config = $config;
54     $this->fai_base = get_ou("faiBaseRDN").$this->config->current['BASE'];
55     $this->fai_release = $this->fai_base;
56     $this->acl_base = $this->config->current['BASE'];
57     $this->ui = $ui;
58     $this->storagePoints = array(
59         get_ou('faiPartitionRDN'),
60         get_ou('faiPackageRDN'),
61         get_ou('faiScriptRDN'),
62         get_ou('faiVariableRDN'),
63         get_ou('faiHookRDN'),
64         get_ou('faiProfileRDN'),get_ou('faiTemplateRDN'));
66 #    // Build filter
67 #    if (session::global_is_set(get_class($this)."_filter")){
68 #      $filter= session::global_get(get_class($this)."_filter");
69 #    } else {
70       $filter = new filter(get_template_path("fai-filter.xml", true));
71       $filter->setObjectStorage($this->storagePoints);
72 #    }
73     $this->setFilter($filter);
75     // Build headpage
76     $headpage = new listing(get_template_path("fai-list.xml", true));
77     $headpage->setFilter($filter);
78     $headpage->setBase($this->fai_release);
79     $headpage->registerElementFilter("filterProperties", "faiManagement::filterProperties");
81     // Add copy&paste and snapshot handler.
82     if ($this->config->boolValueIsTrue("main", "copyPaste")){
83       $this->cpHandler = new CopyPasteHandler($this->config);
84     }
85     if($this->config->get_cfg_value("enableSnapshots") == "true"){
86       $this->snapHandler = new SnapshotHandler($this->config);
87     }
89     $this->registerAction("new_profile","newEntry");
90     $this->registerAction("new_template","newEntry");
91     $this->registerAction("new_script","newEntry");
92     $this->registerAction("new_hook","newEntry");
93     $this->registerAction("new_variable","newEntry");
94     $this->registerAction("new_package","newEntry");
95     $this->registerAction("new_partition","newEntry");
97     $this->registerAction("editByGroup","editByGroup");
98     $this->registerAction("createBranch","createBranch");
99     $this->registerAction("createFreeze","createFreeze");
100     $this->registerAction("removeBranch","removeBranch");
101     $this->registerAction("removeBranchConfirmed","removeBranchConfirmed");
102     $this->registerAction("saveBranch","saveBranch");
103     $this->registerAction("PerformBranch","PerformBranch");
104     parent::__construct($config, $ui, "roles", $headpage);
105   }
108   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
109   {
110     /****************
111       Create a new object
112      ****************/
114     $types = array( 
115         "new_partition"     =>  "FAIpartitionTable",
116         "new_script"        =>  "FAIscript",
117         "new_hook"          =>  "FAIhook",
118         "new_variable"      =>  "FAIvariable",
119         "new_template"      =>  "FAItemplate",
120         "new_package"       =>  "FAIpackageList");
121     $types_i18n = array( 
122         "new_partition"     =>  _("partition table"),
123         "new_script"        =>  _("script"),
124         "new_hook"          =>  _("hook"),
125         "new_variable"      =>  _("variable"),
126         "new_template"      =>  _("template"),
127         "new_package"       =>  _("package list"));
129     if(isset($types[$action])){
130       $type_acl_mapping = array(
131           "FAIpartitionTable"  => "faiPartitionTable",
132           "FAIpackageList"     => "faiPackage",
133           "FAIscript"          => "faiScript",
134           "FAIvariable"        => "faiVariable",
135           "FAIhook"            => "faiHook",
136           "FAIprofile"         => "faiProfile",
137           "FAItemplate"        => "faiTemplate");
139       $acl = $this->ui->get_permissions($this->acl_base,"fai/".$type_acl_mapping[$types[$action]]);
140       if(preg_match("/c/",$acl)){
141         $this->dialogObject = new askClassName($this->config,$this->dn,$this->ui,$types[$action]);
142         $this->dialogObject->parent = &$this;
143       }else{
144         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$action]),      ERROR_DIALOG);
145       }
146     }
147     if($action == "new_profile"){
148       $this->dn = "new" ;
150       $acl = $this->ui->get_permissions($this->acl_base,"fai/faiProfile");
151       if(preg_match("/c/",$acl)){
152         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
153         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
154         $this->dialog->set_acl_base($this->base);
155         $this->dialog->parent = &$this;
157         $this->is_dialog = false;
158       }else{
159         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
160       }
161     }
162   }
165   function newClassNameSelected()
166   {
167     $this->dialogObject->save_object();
168     if(count($this->dialogObject->check())!=0){
169       foreach($this->dialogObject->check() as $msg){
170         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
171       }
172     }elseif(isset($this->dialogObject->objectClass)){
173       $this->dn = "new" ;
174       $a_setup= $this->get_type(array("objectClass"=>array($this->dialogObject->objectClass)));
175       $name = $this->dialogObject->save();
177       if(class_exists($a_setup[0])){
178         $this->tabObject = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
179         $this->tabObject->set_acl_base($this->base);
180         $this->tabObject->by_object[$a_setup[1]]->cn = $name;
181         $this->tabObject->parent = &$this;
182       }
183     }
184   }
188   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
189   {
190     $headpage = $this->getHeadpage();
191     if(count($target) == 1){
192       $entry = $headpage->getEntry($target[0]);
193       if($entry){
194         if(count($entry['GROUPS']) == 1){
195           $data = array_pop($entry['GROUPS']);
196           $type = $this->get_type($data);
197           management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
198           $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
199           $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
201         }else{
202           $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"edit");
203         }
204       }
205     }
206   }
209   function removeEntryRequested($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
210   {
211     $headpage = $this->getHeadpage();
212     if(count($target) == 1){
213       $entry = $headpage->getEntry($target[0]);
214       if($entry){
215         $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"remove");
216       }
217     }
218   }
220   /*! \brief  Object removal was confirmed, now remove the requested entries.
221    *
222    *  @param  String  'action'  The name of the action which was the used as trigger.
223    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
224    *  @param  Array   'all'     A combination of both 'action' and 'target'.
225    */
226   function removeEntryConfirmed($action="",$target=array(),$all=array(),
227       $altTabClass="",$altTabType="",$altAclCategory="")
228   {
229     $ldap = $this->config->get_ldap_link();
230     $ldap->cd($this->config->current['BASE']);  
231     foreach($this->dns as $key => $dn){
232       $ldap->cat($dn);
233       if($ldap->count()){
234         $attrs = $ldap->fetch();
235         $type= $this->get_type($attrs);
236         management::removeEntryConfirmed($action,array($dn),$all,$type[0],$type[2],$type[1]);
238         // Now save changes
239         FAI::save_release_changes_now();
240         $to_del = FAI::clean_up_releases($dn);
241         foreach($to_del as $dn){
242           $ldap->rmdir_recursive($dn);
243         }
244       }
245     }
246   }
249   function editByGroup()
250   {
251     if($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "edit"){
252       $this->dialogObject->save_object();
253       $entry = $this->dialogObject->get_selected();
254       $this->closeDialogs();
255       $data = array_pop($entry);
256       $type = $this->get_type($data);
257       management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
258       $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
259       $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
261     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "remove"){
262       $this->dialogObject->save_object();
263       $to_delete = $entry = $this->dialogObject->get_selected();
264       $dns = array();
267       // Check FAIstate to ensure that we do not remove frozen objects
268       //  additionally check ACLs
269       $locked = array();
270       $disallowed = array();
271       foreach($to_delete as $obj){
272         if(isset($obj['FAIstate']) && preg_match('/^freeze/', $obj['FAIstate'])){
273           $locked[] = $obj['dn'];
274         }else{
275           $type = $this->get_type($obj);
276           $acl = $this->ui->get_permissions($obj['dn'], 'acl/'.$type[1]);
277           if(!preg_match("/d/",$acl)){
278             $disallowed[] = $obj['dn']; 
279           }else{
280             $dns[] = $obj['dn'];
281           }
282         }
283       }
285       // Display info dialog about locked and not removeable entries
286       if(count($locked)){
287         msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
288             msgPool::buildList($locked)));
289       }
291       // Display info dialog about not removeable entries due to permissions
292       if(count($disallowed)){
293         msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
294       }
296       // There are entries left to be removed
297       if(count($dns)){  
298         $this->closeDialogs();
299         $this->dns = $dns;
301         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
303         // check locks
304         if ($user= get_multiple_locks($this->dns)){
305           return(gen_locked_message($user,$this->dns));
306         }
308         // Add locks
309         $dns_names = array();
310         foreach($this->dns as $dn){
311           $dns_names[] =LDAP::fix($dn);
312         }
313         add_lock ($this->dns, $this->ui->dn);
315         // Display confirmation dialog.
316         $smarty = get_smarty();
317         $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
318         $smarty->assign("multiple", true);
319         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
320       }else{
321         $this->closeDialogs();
322       }
323     }
324   }
326   
327   protected function saveChanges()
328   {
329     management::saveChanges();
331     // Now save changes
332     FAI::save_release_changes_now();
333     $to_del = FAI::clean_up_releases($this->last_dn);
334     foreach($to_del as $dn){
335       $ldap->rmdir_recursive($dn);
336     }
337   }
339   protected function applyChanges()
340   {
341     management::applyChanges();
343     // Now save changes
344     FAI::save_release_changes_now();
345     $to_del = FAI::clean_up_releases($this->last_dn);
346     foreach($to_del as $dn){
347       $ldap->rmdir_recursive($dn);
348     }
349   }
351   function detectPostActions()
352   {
353     $action = management::detectPostActions();
354     if(isset($_POST['new_profile'])) $action['action'] = "new_profile";
355     if(isset($_POST['new_template'])) $action['action'] = "new_template";
356     if(isset($_POST['new_script'])) $action['action'] = "new_script";
357     if(isset($_POST['new_hook'])) $action['action'] = "new_hook";
358     if(isset($_POST['new_variable'])) $action['action'] = "new_variable";
359     if(isset($_POST['new_package'])) $action['action'] = "new_package";
360     if(isset($_POST['new_partition'])) $action['action'] = "new_partition";
362     if(isset($_POST['faiGroupHandle_cancel'])) $action['action'] = "cancel";
363     if(isset($_POST['CancelBranchName'])) $action['action'] = "cancel";
364     if(isset($_POST['delete_branch_confirm'])) $action['action'] = "removeBranchConfirmed";
365     if(isset($_GET['PerformBranch'])) $action['action'] = "PerformBranch";
366     if(isset($_POST['UseBranchName'])) $action['action'] = "saveBranch";
367     if(isset($_POST['faiGroupHandle_apply']))  $action['action'] = "editByGroup";
368     if(isset($_GET['act']) && $_GET['act'] == "branch_branch")  $action['action'] = "createBranch";
369     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch")  $action['action'] = "createFreeze";
370     if(isset($_GET['act']) && $_GET['act'] == "remove_branch")  $action['action'] = "removeBranch";
372     foreach($_POST as $name => $value){
373       if(preg_match("/^edit_([0-9]*)_([a-z]*)_(x|y)/i", $name)){
374         $id = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\1", $name);
375         $tab = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\2", $name);
376         $headpage = $this->getHeadpage();
377         if(isset($headpage->entries[$id]['GROUPS'][$tab])){
378           $data =$headpage->entries[$id]['GROUPS'][$tab];
379           $type = $this->get_type($data);
380           management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]); 
381         } 
382         break;
383       }
384     }
385     return($action);
386   }
389   function renderList()
390   {
391     $filter = $this->getFilter();
392     $headpage = $this->getHeadpage();
393     $filter->setComboBoxOptions("RELEASE",$this->getReleaseList());
395     if(isset($_POST['RELEASE'])){
396       $this->fai_release = get_post('RELEASE');
397     }
398     $headpage->setBase($this->fai_release);
399     $headpage->update();
400     $smarty = get_smarty();
401     $smarty->assign("fai_release", $this->fai_release);
402     $smarty->assign("fai_base", $this->fai_base);
403     $r = $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'));
404     $c = $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'));
405     $smarty->assign("allow_create", $c);
406     $smarty->assign("allow_remove", $r);
407     $display = $headpage->render();
408     return($this->getHeader().$display);
409   }
411   function getReleaseList($base = "", $prefix ="")
412   {
413     $list = array();
414     if(empty($base)){
415       $base = $this->fai_base;
416       $list[$base] = "/";
417     }
419     $ldap = $this->config->get_ldap_link();
420     $ldap->ls("(objectClass=FAIbranch)",$base,array("ou","FAIstate"));
421     $cfg_rel = $this->config->search("faiManagement","DEFAULTFAIRELEASE",array("menu"));
423     while($release = $ldap->fetch()){
424       $list[$release['dn']] = $prefix.$release['ou'][0];
426       // Preset to prefered releaes if necessary  
427       if(empty($this->fai_release) && $cfg_rel == $release['dn']){
428         $this->fai_release = $release['dn'];
429       }
431       $list = array_merge($list,$this->getReleaseList($release['dn'],$prefix."&nbsp; "));
432     }
433     return($list);
434   }
437   static function filterProperties($row, $classes)
438   {
439     $objects = array(
440         "FAIpartitionTable"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png",
441           "NAME"=>_("Partition table"),"KZL"=> "PT", "VAR"=>"ShowPartitions"),
442         "FAIpackageList"     => array("IMG"=> "plugins/fai/images/fai_packages.png",
443           "NAME"=>_("Package list") ,  "KZL"=> "PL", "VAR"=>"ShowPackages"),
444         "FAIscript"          => array("IMG"=> "plugins/fai/images/fai_script.png",
445           "NAME"=>_("Scripts") ,       "KZL"=> "S",  "VAR"=>"ShowScripts"),
446         "FAIvariable"        => array("IMG"=> "plugins/fai/images/fai_variable.png",
447           "NAME"=>_("Variables") ,     "KZL"=> "V",  "VAR"=>"ShowVariables"),
448         "FAIhook"            => array("IMG"=> "plugins/fai/images/fai_hook.png",
449           "NAME"=>_("Hooks"),          "KZL"=> "H",  "VAR"=>"ShowHooks"),
450         "FAIprofile"         => array("IMG"=> "plugins/fai/images/fai_profile.png",
451           "NAME"=>_("Profile") ,       "KZL"=> "P",  "VAR"=>"ShowProfiles"),
452         "FAItemplate"        => array("IMG"=> "plugins/fai/images/fai_template.png",
453           "NAME"=>_("Templates") ,     "KZL"=> "T",  "VAR"=>"ShowTemplates"),
454         "opsi_netboot"       => array("IMG"=> "plugins/opsi/images/netboot_package.png",
455           "NAME"=>_("OPSI netboot product") ,     "KZL"=> "ON",  "VAR"=>"ShowOpsiNetboot"),
456         "opsi_local"         => array("IMG"=> "plugins/opsi/images/local_package.png",
457           "NAME"=>_("OPSI localboot product")   ,     "KZL"=> "OL",  "VAR"=>"ShowOpsiLocal"));
459     $icon_list = "";
460     foreach($objects as $type => $type_data){
461       if(in_array($type, $classes)){
462         $icon_list .= "<input type='image' src='".$type_data['IMG']."' title='".$type_data['NAME']."'
463           alt='".$type_data['KZL']."' class='center' name='edit_".$row."_".$type."'>\n";
464       }else{
465         $icon_list .= "<img src='images/empty.png' alt=' ' class='center'>\n";
466       }
467     }
469     return $icon_list;
470   }
475   function removeBranch()
476   {
477     /* Check if we have a post remove method configured
478      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
479      */
480     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
481       /* Load permissions for selected 'dn' and check if
482          we're allowed to remove this 'dn' */
483       if(preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
484         $smarty=get_smarty();
485         $smarty->assign("release_hidden",base64_encode($this->fai_release));
486         $smarty->assign("info", msgPool::deleteInfo(LDAP::fix($this->fai_release),_("FAI branch/freeze")));
487         return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
488       } else {
489         msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
490       }
491     }
492   }
495   function removeBranchConfirmed()
496   {
497     /* Check if we have a post remove method configured
498      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
499      */
500     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
502       if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
503         msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
504       }else{
506         $bb =  $this->fai_release;
507         $ldap = $this->config->get_ldap_link();
509         $br = $this->getBranches();
511         if(isset($br[$bb]) && preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
512           $name = $br[$bb];
514           $ldap->cd($bb);
515           $ldap->recursive_remove();
516           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('applicationRDN'), $bb));
517           $ldap->recursive_remove();
518           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('mimetypeRDN'), $bb));
519           $ldap->recursive_remove();
521           /* Search for all groups with configured application menus.
522              - First search all groups, to ensure that we only remove entries form whithin groups.
523              - The search für menu configuration for the specified release and collect all those dns.
524              - Remove entries
525            */
526           $release_ou = preg_replace("/".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$bb);
527           $ldap->cd($this->config->current['BASE']);
528           $ldap->search("(objectClass=posixGroup)",array("dn"));
530           /* Collect all group dns
531            */
532           $groups = array();
533           while($attrs = $ldap->fetch()){
534             $groups[] = $attrs['dn'];
535           }
538           /* Collect all group menu release dns that match the release we have removed
539            */
540           $dns = array();
541           foreach($groups as $dn){
542             $ldap->cd($dn);
543             $ldap->search("(objectClass=FAIbranch)",array("dn"));
544             while($attrs = $ldap->fetch()){
545               if(preg_match("/^".preg_quote($release_ou, '/')."/",$attrs['dn'])){
546                 $dns[] = $attrs['dn'];
547               }
548             }
549           }
551           /* Finally remove collected release dns
552            */
553           foreach($dns as $dn){
554             $ldap->cd($dn);
555             $ldap->recursive_remove();
556           }
558           /* Post remove */
559           $this->fai_release = $this->fai_base;
560           $this->lock_name   = $name;
561           $this->lock_dn     = $bb;
562           $this->postremove();
564           $fai_filter = session::get("fai_filter");
565           $fai_filter['fai_release'] = $this->fai_release;
566           session::set("fai_filter",$fai_filter);
568           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
569         }
570       }
571     }
572   }
575   function createBranch()
576   { 
577     $smarty = get_smarty();
578     $this->dispNewBranch=true;
579     $this->dispNewFreeze=false;
580     $smarty->assign("iframe",false);
581     if(isset($_POST['BranchName'])){
582       $smarty->assign("BranchName", $_POST['BranchName']);
583     }else{
584       $smarty->assign("BranchName","");
585     }
586     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
587   }
589   function createFreeze()
590   { 
591     $smarty = get_smarty();
592     $this->dispNewFreeze=true;
593     $this->dispNewBranch=false;
594     $smarty->assign("iframe",false);
595     if(isset($_POST['BranchName'])){
596       $smarty->assign("BranchName", $_POST['BranchName']);
597     }else{
598       $smarty->assign("BranchName","");
599     }
600     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
601   }
605   function PerformBranch()
606   {
607     if(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
608       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
609     }else{
611       /* In order to see error messages we have to reset the error handler.
612          Due to the exit();
613        */
614       restore_error_handler();
616       $this->dispNewBranch = false;
617       $this->dispNewFreeze = false;
619       $LASTPOST = session::get('LASTPOST');
620       $base = $LASTPOST['base'];
621       $_POST  = session::get('LASTPOST');
622       $name = $_POST['BranchName'];
624       $type = $LASTPOST['type'];
625       $ldap = $this->config->get_ldap_link();
627       $baseToUse = $base;
628       if($this->fai_release !=  $this->fai_base){
629         $baseToUse = $this->fai_release;
630       }
632       /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
634       $CurrentReleases  = $this->getBranches();
635       $NewReleaseName   = $name;
636       if(isset($CurrentReleases[$this->fai_release])) {
637         if($this->fai_release != $this->fai_base){
638           $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
639           $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName);
640         }else{
641           $NewReleaseName   = $name;
642         }
643       }
644       $appsrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),$baseToUse);
645       $appdst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),"ou=".$name.",".$baseToUse) ;
647       $mimesrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),$baseToUse);
648       $mimedst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),"ou=".$name.",".$baseToUse) ;
650       /* Check if source depeartments exist */
651       foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
652         $ldap->cd($this->config->current['BASE']);
653         $ldap->cat($dep);
654         if(!$ldap->count()){
655           $ldap->create_missing_trees($dep);
656         }
657       }
659       /* Print header to have styles included */
660       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
661         <html>
662         <head>
663         <title></title>
664         <style type="text/css">@import url("themes/default/style.css");</style>
665         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
666         </head>
667         <body style="background: none;margin:3px;color:black">
668         ';
670       new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
672       /* Duplicate group application releases
673        */
674       FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
676       /* Duplicate applications
677        */
678       $ldap->cat($appsrc,array("dn")) ;
679       if($ldap->count()){
680         $ldap->cd ($appdst);
681         $ldap->recursive_remove();
682         FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
683       }
685       /* Duplicate mime types
686        */
687       $ldap->cat($mimesrc,array("dn")) ;
688       if($ldap->count()){
689         $ldap->cd ($mimedst);
690         $ldap->recursive_remove();
691         FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
692       }
694       $attr = array();
695       $attr['objectClass'] = array("organizationalUnit","FAIbranch");
696       $attr['ou']       = $name;
697       $attr['FAIstate'] = $type;
698       $ldap->cd($this->config->current['BASE']);
699       $ldap->cd("ou=".$name.",".$baseToUse);
700       $ldap->cat("ou=".$name.",".$baseToUse);
701       if($ldap->count()){
702         $ldap->modify($attr);
703       }else{
704         $ldap->add($attr);
705       }
707       /* Duplicate fai objects
708        */
709       //      $ldap->cd ("ou=".$name.",".$baseToUse);
710       //      $ldap->recursive_remove();
711       //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
713       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
714         <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
715         <input type='hidden' name='php_c_check' value='1'>
716         </form></div>";
718       echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
720       /* Print footer to have valid html */
721       echo "</body></html>";
723       $this->dispNewFreeze = false;
725       /* Postcreate */
727       /* Assign possible attributes */
728       $this->lock_type  = $type;
729       $this->lock_name  = $name;
730       $this->lock_dn    = $baseToUse;
731       $this->postcreate();
733       /* Send daemon event to reload the fai release database
734        */
735       if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
736         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
737         if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
738           $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'];
739           $tmp = new $evt['CLASS_NAME']($this->config);
740           $tmp->set_type(TRIGGERED_EVENT);
741           $tmp->add_targets(array("GOSA"));
742           $o_queue = new gosaSupportDaemon();
743           if(!$o_queue->append($tmp)){
744             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
745           }
746         }
747       }else{
748         trigger_error("Unknown class DaemonEvent / gosaSupportDaemon");
749         msg_dialog::display(_("Fatal error"),
750             "Daemon events called but classes where not accessible, DaemonEvent gosaSupportDaemon",
751             FATAL_ERROR_DIALOG);
752       }
753       exit();
754     }
755   }
760   function saveBranch()
761   {
762     if($this->dispNewBranch){
763       $type = "branch";
764     }else{
765       $type = "freeze";
766     }
768     /* Check branch name */
769     $name = $_POST['BranchName'];
770     $is_ok = true;
771     $smarty = get_smarty();
772     $smarty->assign("BranchName",$name);
773     $base= $this->fai_base;
775     /* Check used characters */
776     if(!preg_match("/^[0-9a-z\.]*$/",$name)){
777       msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
778       $is_ok = false;
779     }
781     // Check if this name is already in use 
782     if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
783       msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
784       $is_ok = false;
785     }
787     // Handle errors
788     if(!$is_ok && $this->dispNewFreeze){
789       return($this->createFreeze());
790     }elseif(!$is_ok && $this->dispNewBranch){
791       return($this->createBranch());
792     }
794     // Now create new release
796     if(session::is_set('LASTPOST')){
797       $LASTPOST = session::get('LASTPOST');
798     }else{
799       $LASTPOST = array();
800     }
801     $LASTPOST['base'] = $base;
802     $LASTPOST['type'] = $type;
803     $LASTPOST['BranchName'] = $name;
804     session::set('LASTPOST',$LASTPOST);
805     $smarty->assign("iframe", true);
806     $smarty->assign("plugID", $_GET['plug']);
807     $display  = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
808     return($display);
809  
810   }
813   function CheckNewBranchName($name,$base)
814   {
815     $f = $this->fai_release;
816     if($name == ""){
817       return(false);
818     }elseif(in_array($name,$this->getBranches($f))) {
819       return(false);
820     }elseif(tests::is_department_name_reserved($name,$base)){
821       return(false);
822     }
823     return(true);
824   }
827   /* Get available branches for current base */
828   function getBranches($base = false,$prefix = "")
829   {
830     $ret = array("/"=>$this->fai_base);
831     $ldap = $this->config->get_ldap_link();
832     if(!$base){
833       $base = $this->fai_base;
834     }
835     $tmp = FAI::get_all_releases_from_base($base,true);
836     foreach($tmp as $dn => $name){
837       $ret[$name]=$dn;
838     }
839     ksort($ret);
840     $ret = array_flip($ret);
842     return ($ret);
843   }
846   function get_type($array)
847   {
848     if(!isset($array['objectClass'])) return(array());
849     if(in_array("FAIpartitionTable",$array['objectClass'])){
850       return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
851     }
852     if(in_array("FAIscript",$array['objectClass'])){
853       return(array("tabsScript","faiScript","FAISCRIPTTABS"));
854     }
855     if(in_array("FAItemplate",$array['objectClass'])){
856       return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
857     }
858     if(in_array("FAIhook",$array['objectClass'])){
859       return(array("tabsHook","faiHook","FAIHOOKTABS"));
860     }
861     if(in_array("FAIvariable",$array['objectClass'])){
862       return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
863     }
864     if(in_array("FAIprofile",$array['objectClass'])){
865       return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
866     }
867     if(in_array("FAIpackageList",$array['objectClass'])){
868       return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
869     }
870     return(array());
871   }
872
873 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
874 ?>