Code

Updated FAI tp list opsi stuff again
[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("newClassNameSelected","newClassNameSelected");
99     $this->registerAction("editByGroup","editByGroup");
100     $this->registerAction("createBranch","createBranch");
101     $this->registerAction("createFreeze","createFreeze");
102     $this->registerAction("removeBranch","removeBranch");
103     $this->registerAction("removeBranchConfirmed","removeBranchConfirmed");
104     $this->registerAction("saveBranch","saveBranch");
105     $this->registerAction("PerformBranch","PerformBranch");
106     parent::__construct($config, $ui, "roles", $headpage);
107   }
110   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
111   {
112     /****************
113       Create a new object
114      ****************/
116     $types = array( 
117         "new_partition"     =>  "FAIpartitionTable",
118         "new_script"        =>  "FAIscript",
119         "new_hook"          =>  "FAIhook",
120         "new_variable"      =>  "FAIvariable",
121         "new_template"      =>  "FAItemplate",
122         "new_package"       =>  "FAIpackageList");
123     $types_i18n = array( 
124         "new_partition"     =>  _("partition table"),
125         "new_script"        =>  _("script"),
126         "new_hook"          =>  _("hook"),
127         "new_variable"      =>  _("variable"),
128         "new_template"      =>  _("template"),
129         "new_package"       =>  _("package list"));
131     if(isset($types[$action])){
132       $type_acl_mapping = array(
133           "FAIpartitionTable"  => "faiPartitionTable",
134           "FAIpackageList"     => "faiPackage",
135           "FAIscript"          => "faiScript",
136           "FAIvariable"        => "faiVariable",
137           "FAIhook"            => "faiHook",
138           "FAIprofile"         => "faiProfile",
139           "FAItemplate"        => "faiTemplate");
141       $acl = $this->ui->get_permissions($this->acl_base,"fai/".$type_acl_mapping[$types[$action]]);
142       if(preg_match("/c/",$acl)){
143         $this->dialogObject = new askClassName($this->config,$this->dn,$this->ui,$types[$action]);
144         $this->dialogObject->parent = &$this;
145       }else{
146         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$action]),      ERROR_DIALOG);
147       }
148     }
149     if($action == "new_profile"){
150       $this->dn = "new" ;
152       $acl = $this->ui->get_permissions($this->acl_base,"fai/faiProfile");
153       if(preg_match("/c/",$acl)){
154         $a_setup= $this->get_type(array("objectClass"=>array("FAIprofile")));
155         $this->dialog = new $a_setup[0]($this->config,$this->config->data['TABS'][$a_setup[2]],$this->dn,"fai");
156         $this->dialog->set_acl_base($this->base);
157         $this->dialog->parent = &$this;
159         $this->is_dialog = false;
160       }else{
161         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
162       }
163     }
164   }
167   function newClassNameSelected()
168   {
169     $this->dialogObject->save_object();
170     if(count($this->dialogObject->check())!=0){
171       foreach($this->dialogObject->check() as $msg){
172         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
173       }
174     }elseif(isset($this->dialogObject->objectClass)){
175       $this->dn = "new" ;
176       $type= $this->get_type(array("objectClass"=>array($this->dialogObject->objectClass)));
177       $name = $this->dialogObject->save();
179       if(class_exists($type[0])){
180         $this->closeDialogs();
181         management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
182         $this->tabObject->set_acl_base($this->acl_base);
183         $this->tabObject->by_object[$type[1]]->cn = $name;
184       }
185     }
186   }
190   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
191   {
192     $headpage = $this->getHeadpage();
193     if(count($target) == 1){
194       $entry = $headpage->getEntry($target[0]);
195       if($entry){
196         if(count($entry['GROUPS']) == 1){
197           $data = array_pop($entry['GROUPS']);
198           $type = $this->get_type($data);
199           management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
200           $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
201           $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
203         }else{
204           $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"edit");
205         }
206       }
207     }
208   }
211   function removeEntryRequested($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
212   {
213     $headpage = $this->getHeadpage();
214     if(count($target) == 1){
215       $entry = $headpage->getEntry($target[0]);
216       if($entry){
217         $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"remove");
218       }
219     }
220   }
222   /*! \brief  Object removal was confirmed, now remove the requested entries.
223    *
224    *  @param  String  'action'  The name of the action which was the used as trigger.
225    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
226    *  @param  Array   'all'     A combination of both 'action' and 'target'.
227    */
228   function removeEntryConfirmed($action="",$target=array(),$all=array(),
229       $altTabClass="",$altTabType="",$altAclCategory="")
230   {
231     $ldap = $this->config->get_ldap_link();
232     $ldap->cd($this->config->current['BASE']);  
233     foreach($this->dns as $key => $dn){
234       $ldap->cat($dn);
235       if($ldap->count()){
236         $attrs = $ldap->fetch();
237         $type= $this->get_type($attrs);
238         management::removeEntryConfirmed($action,array($dn),$all,$type[0],$type[2],$type[1]);
240         // Now save changes
241         FAI::save_release_changes_now();
242         $to_del = FAI::clean_up_releases($dn);
243         foreach($to_del as $dn){
244           $ldap->rmdir_recursive($dn);
245         }
246       }
247     }
248   }
251   function editByGroup()
252   {
253     if($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "edit"){
254       $this->dialogObject->save_object();
255       $entry = $this->dialogObject->get_selected();
256       $this->closeDialogs();
257       $data = array_pop($entry);
258       $type = $this->get_type($data);
259       management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
260       $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
261       $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
263     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "remove"){
264       $this->dialogObject->save_object();
265       $to_delete = $entry = $this->dialogObject->get_selected();
266       $dns = array();
269       // Check FAIstate to ensure that we do not remove frozen objects
270       //  additionally check ACLs
271       $locked = array();
272       $disallowed = array();
273       foreach($to_delete as $obj){
274         if(isset($obj['FAIstate']) && preg_match('/^freeze/', $obj['FAIstate'])){
275           $locked[] = $obj['dn'];
276         }else{
277           $type = $this->get_type($obj);
278           $acl = $this->ui->get_permissions($obj['dn'], 'acl/'.$type[1]);
279           if(!preg_match("/d/",$acl)){
280             $disallowed[] = $obj['dn']; 
281           }else{
282             $dns[] = $obj['dn'];
283           }
284         }
285       }
287       // Display info dialog about locked and not removeable entries
288       if(count($locked)){
289         msg_dialog::display(_("Branch locked"),sprintf(_("The following entries are locked, you can't remove them %s."),
290             msgPool::buildList($locked)));
291       }
293       // Display info dialog about not removeable entries due to permissions
294       if(count($disallowed)){
295         msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
296       }
298       // There are entries left to be removed
299       if(count($dns)){  
300         $this->closeDialogs();
301         $this->dns = $dns;
303         @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$target,"Entry removel requested!");
305         // check locks
306         if ($user= get_multiple_locks($this->dns)){
307           return(gen_locked_message($user,$this->dns));
308         }
310         // Add locks
311         $dns_names = array();
312         foreach($this->dns as $dn){
313           $dns_names[] =LDAP::fix($dn);
314         }
315         add_lock ($this->dns, $this->ui->dn);
317         // Display confirmation dialog.
318         $smarty = get_smarty();
319         $smarty->assign("info", msgPool::deleteInfo($dns_names,_($this->objectName)));
320         $smarty->assign("multiple", true);
321         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
322       }else{
323         $this->closeDialogs();
324       }
325     }
326   }
328   
329   protected function saveChanges()
330   {
331     management::saveChanges();
333     // Now save changes
334     FAI::save_release_changes_now();
335     $to_del = FAI::clean_up_releases($this->last_dn);
336     foreach($to_del as $dn){
337       $ldap->rmdir_recursive($dn);
338     }
339   }
341   protected function applyChanges()
342   {
343     management::applyChanges();
345     // Now save changes
346     FAI::save_release_changes_now();
347     $to_del = FAI::clean_up_releases($this->last_dn);
348     foreach($to_del as $dn){
349       $ldap->rmdir_recursive($dn);
350     }
351   }
353   function detectPostActions()
354   {
355     $action = management::detectPostActions();
356     if(isset($_POST['new_profile'])) $action['action'] = "new_profile";
357     if(isset($_POST['new_template'])) $action['action'] = "new_template";
358     if(isset($_POST['new_script'])) $action['action'] = "new_script";
359     if(isset($_POST['new_hook'])) $action['action'] = "new_hook";
360     if(isset($_POST['new_variable'])) $action['action'] = "new_variable";
361     if(isset($_POST['new_package'])) $action['action'] = "new_package";
362     if(isset($_POST['new_partition'])) $action['action'] = "new_partition";
364     if(isset($_POST['edit_continue'])) $action['action'] = "newClassNameSelected";
365     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
367     if(isset($_POST['faiGroupHandle_cancel'])) $action['action'] = "cancel";
368     if(isset($_POST['CancelBranchName'])) $action['action'] = "cancel";
369     if(isset($_POST['delete_branch_confirm'])) $action['action'] = "removeBranchConfirmed";
370     if(isset($_GET['PerformBranch'])) $action['action'] = "PerformBranch";
371     if(isset($_POST['UseBranchName'])) $action['action'] = "saveBranch";
372     if(isset($_POST['faiGroupHandle_apply']))  $action['action'] = "editByGroup";
373     if(isset($_GET['act']) && $_GET['act'] == "branch_branch")  $action['action'] = "createBranch";
374     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch")  $action['action'] = "createFreeze";
375     if(isset($_GET['act']) && $_GET['act'] == "remove_branch")  $action['action'] = "removeBranch";
377     foreach($_POST as $name => $value){
378       if(preg_match("/^edit_([0-9]*)_([a-z]*)_(x|y)/i", $name)){
379         $id = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\1", $name);
380         $tab = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\2", $name);
381         $headpage = $this->getHeadpage();
382         if(isset($headpage->entries[$id]['GROUPS'][$tab])){
383           $data =$headpage->entries[$id]['GROUPS'][$tab];
384           $type = $this->get_type($data);
385           management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]); 
386         } 
387         break;
388       }
389     }
390     return($action);
391   }
394   function renderList()
395   {
396     $filter = $this->getFilter();
397     $headpage = $this->getHeadpage();
398     $filter->setComboBoxOptions("RELEASE",$this->getReleaseList());
400     if(isset($_POST['RELEASE'])){
401       $this->fai_release = get_post('RELEASE');
402     }
403     $headpage->setBase($this->fai_release);
404     $headpage->update();
405     $smarty = get_smarty();
406     $smarty->assign("fai_release", $this->fai_release);
407     $smarty->assign("fai_base", $this->fai_base);
408     $r = $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'));
409     $c = $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'));
410     $smarty->assign("allow_create", $c);
411     $smarty->assign("allow_remove", $r);
412     $display = $headpage->render();
413     return($this->getHeader().$display);
414   }
416   function getReleaseList($base = "", $prefix ="")
417   {
418     $list = array();
419     if(empty($base)){
420       $base = $this->fai_base;
421       $list[$base] = "/";
422     }
424     $ldap = $this->config->get_ldap_link();
425     $ldap->ls("(objectClass=FAIbranch)",$base,array("ou","FAIstate"));
426     $cfg_rel = $this->config->search("faiManagement","DEFAULTFAIRELEASE",array("menu"));
428     while($release = $ldap->fetch()){
429       $list[$release['dn']] = $prefix.$release['ou'][0];
431       // Preset to prefered releaes if necessary  
432       if(empty($this->fai_release) && $cfg_rel == $release['dn']){
433         $this->fai_release = $release['dn'];
434       }
436       $list = array_merge($list,$this->getReleaseList($release['dn'],$prefix."&nbsp; "));
437     }
438     return($list);
439   }
442   static function filterProperties($row, $classes)
443   {
444     $objects = array(
445         "FAIpartitionTable"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png",
446           "NAME"=>_("Partition table"),"KZL"=> "PT", "VAR"=>"ShowPartitions"),
447         "FAIpackageList"     => array("IMG"=> "plugins/fai/images/fai_packages.png",
448           "NAME"=>_("Package list") ,  "KZL"=> "PL", "VAR"=>"ShowPackages"),
449         "FAIscript"          => array("IMG"=> "plugins/fai/images/fai_script.png",
450           "NAME"=>_("Scripts") ,       "KZL"=> "S",  "VAR"=>"ShowScripts"),
451         "FAIvariable"        => array("IMG"=> "plugins/fai/images/fai_variable.png",
452           "NAME"=>_("Variables") ,     "KZL"=> "V",  "VAR"=>"ShowVariables"),
453         "FAIhook"            => array("IMG"=> "plugins/fai/images/fai_hook.png",
454           "NAME"=>_("Hooks"),          "KZL"=> "H",  "VAR"=>"ShowHooks"),
455         "FAIprofile"         => array("IMG"=> "plugins/fai/images/fai_profile.png",
456           "NAME"=>_("Profile") ,       "KZL"=> "P",  "VAR"=>"ShowProfiles"),
457         "FAItemplate"        => array("IMG"=> "plugins/fai/images/fai_template.png",
458           "NAME"=>_("Templates") ,     "KZL"=> "T",  "VAR"=>"ShowTemplates"),
459         "opsi_netboot"       => array("IMG"=> "plugins/opsi/images/netboot_package.png",
460           "NAME"=>_("OPSI netboot product") ,     "KZL"=> "ON",  "VAR"=>"ShowOpsiNetboot"),
461         "opsi_local"         => array("IMG"=> "plugins/opsi/images/local_package.png",
462           "NAME"=>_("OPSI localboot product")   ,     "KZL"=> "OL",  "VAR"=>"ShowOpsiLocal"));
464     $icon_list = "";
465     foreach($objects as $type => $type_data){
466       if(in_array($type, $classes)){
467         $icon_list .= "<input type='image' src='".$type_data['IMG']."' title='".$type_data['NAME']."'
468           alt='".$type_data['KZL']."' class='center' name='edit_".$row."_".$type."'>\n";
469       }else{
470         $icon_list .= "<img src='images/empty.png' alt=' ' class='center'>\n";
471       }
472     }
474     return $icon_list;
475   }
480   function removeBranch()
481   {
482     /* Check if we have a post remove method configured
483      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
484      */
485     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
486       /* Load permissions for selected 'dn' and check if
487          we're allowed to remove this 'dn' */
488       if(preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
489         $smarty=get_smarty();
490         $smarty->assign("release_hidden",base64_encode($this->fai_release));
491         $smarty->assign("info", msgPool::deleteInfo(LDAP::fix($this->fai_release),_("FAI branch/freeze")));
492         return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
493       } else {
494         msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
495       }
496     }
497   }
500   function removeBranchConfirmed()
501   {
502     /* Check if we have a post remove method configured
503      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
504      */
505     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
507       if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
508         msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
509       }else{
511         $bb =  $this->fai_release;
512         $ldap = $this->config->get_ldap_link();
514         $br = $this->getBranches();
516         if(isset($br[$bb]) && preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
517           $name = $br[$bb];
519           $ldap->cd($bb);
520           $ldap->recursive_remove();
521           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('applicationRDN'), $bb));
522           $ldap->recursive_remove();
523           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('mimetypeRDN'), $bb));
524           $ldap->recursive_remove();
526           /* Search for all groups with configured application menus.
527              - First search all groups, to ensure that we only remove entries form whithin groups.
528              - The search für menu configuration for the specified release and collect all those dns.
529              - Remove entries
530            */
531           $release_ou = preg_replace("/".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$bb);
532           $ldap->cd($this->config->current['BASE']);
533           $ldap->search("(objectClass=posixGroup)",array("dn"));
535           /* Collect all group dns
536            */
537           $groups = array();
538           while($attrs = $ldap->fetch()){
539             $groups[] = $attrs['dn'];
540           }
543           /* Collect all group menu release dns that match the release we have removed
544            */
545           $dns = array();
546           foreach($groups as $dn){
547             $ldap->cd($dn);
548             $ldap->search("(objectClass=FAIbranch)",array("dn"));
549             while($attrs = $ldap->fetch()){
550               if(preg_match("/^".preg_quote($release_ou, '/')."/",$attrs['dn'])){
551                 $dns[] = $attrs['dn'];
552               }
553             }
554           }
556           /* Finally remove collected release dns
557            */
558           foreach($dns as $dn){
559             $ldap->cd($dn);
560             $ldap->recursive_remove();
561           }
563           /* Post remove */
564           $this->fai_release = $this->fai_base;
565           $this->lock_name   = $name;
566           $this->lock_dn     = $bb;
567           $this->postremove();
569           $fai_filter = session::get("fai_filter");
570           $fai_filter['fai_release'] = $this->fai_release;
571           session::set("fai_filter",$fai_filter);
573           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
574         }
575       }
576     }
577   }
580   function createBranch()
581   { 
582     $smarty = get_smarty();
583     $this->dispNewBranch=true;
584     $this->dispNewFreeze=false;
585     $smarty->assign("iframe",false);
586     if(isset($_POST['BranchName'])){
587       $smarty->assign("BranchName", $_POST['BranchName']);
588     }else{
589       $smarty->assign("BranchName","");
590     }
591     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
592   }
594   function createFreeze()
595   { 
596     $smarty = get_smarty();
597     $this->dispNewFreeze=true;
598     $this->dispNewBranch=false;
599     $smarty->assign("iframe",false);
600     if(isset($_POST['BranchName'])){
601       $smarty->assign("BranchName", $_POST['BranchName']);
602     }else{
603       $smarty->assign("BranchName","");
604     }
605     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
606   }
610   function PerformBranch()
611   {
612     if(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
613       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
614     }else{
616       /* In order to see error messages we have to reset the error handler.
617          Due to the exit();
618        */
619       restore_error_handler();
621       $this->dispNewBranch = false;
622       $this->dispNewFreeze = false;
624       $LASTPOST = session::get('LASTPOST');
625       $base = $LASTPOST['base'];
626       $_POST  = session::get('LASTPOST');
627       $name = $_POST['BranchName'];
629       $type = $LASTPOST['type'];
630       $ldap = $this->config->get_ldap_link();
632       $baseToUse = $base;
633       if($this->fai_release !=  $this->fai_base){
634         $baseToUse = $this->fai_release;
635       }
637       /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
639       $CurrentReleases  = $this->getBranches();
640       $NewReleaseName   = $name;
641       if(isset($CurrentReleases[$this->fai_release])) {
642         if($this->fai_release != $this->fai_base){
643           $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
644           $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName);
645         }else{
646           $NewReleaseName   = $name;
647         }
648       }
649       $appsrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),$baseToUse);
650       $appdst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),"ou=".$name.",".$baseToUse) ;
652       $mimesrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),$baseToUse);
653       $mimedst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),"ou=".$name.",".$baseToUse) ;
655       /* Check if source depeartments exist */
656       foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
657         $ldap->cd($this->config->current['BASE']);
658         $ldap->cat($dep);
659         if(!$ldap->count()){
660           $ldap->create_missing_trees($dep);
661         }
662       }
664       /* Print header to have styles included */
665       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
666         <html>
667         <head>
668         <title></title>
669         <style type="text/css">@import url("themes/default/style.css");</style>
670         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
671         </head>
672         <body style="background: none;margin:3px;color:black">
673         ';
675       new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
677       /* Duplicate group application releases
678        */
679       FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
681       /* Duplicate applications
682        */
683       $ldap->cat($appsrc,array("dn")) ;
684       if($ldap->count()){
685         $ldap->cd ($appdst);
686         $ldap->recursive_remove();
687         FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
688       }
690       /* Duplicate mime types
691        */
692       $ldap->cat($mimesrc,array("dn")) ;
693       if($ldap->count()){
694         $ldap->cd ($mimedst);
695         $ldap->recursive_remove();
696         FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
697       }
699       $attr = array();
700       $attr['objectClass'] = array("organizationalUnit","FAIbranch");
701       $attr['ou']       = $name;
702       $attr['FAIstate'] = $type;
703       $ldap->cd($this->config->current['BASE']);
704       $ldap->cd("ou=".$name.",".$baseToUse);
705       $ldap->cat("ou=".$name.",".$baseToUse);
706       if($ldap->count()){
707         $ldap->modify($attr);
708       }else{
709         $ldap->add($attr);
710       }
712       /* Duplicate fai objects
713        */
714       //      $ldap->cd ("ou=".$name.",".$baseToUse);
715       //      $ldap->recursive_remove();
716       //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
718       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
719         <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
720         <input type='hidden' name='php_c_check' value='1'>
721         </form></div>";
723       echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
725       /* Print footer to have valid html */
726       echo "</body></html>";
728       $this->dispNewFreeze = false;
730       /* Postcreate */
732       /* Assign possible attributes */
733       $this->lock_type  = $type;
734       $this->lock_name  = $name;
735       $this->lock_dn    = $baseToUse;
736       $this->postcreate();
738       /* Send daemon event to reload the fai release database
739        */
740       if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
741         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
742         if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
743           $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'];
744           $tmp = new $evt['CLASS_NAME']($this->config);
745           $tmp->set_type(TRIGGERED_EVENT);
746           $tmp->add_targets(array("GOSA"));
747           $o_queue = new gosaSupportDaemon();
748           if(!$o_queue->append($tmp)){
749             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
750           }
751         }
752       }else{
753         trigger_error("Unknown class DaemonEvent / gosaSupportDaemon");
754         msg_dialog::display(_("Fatal error"),
755             "Daemon events called but classes where not accessible, DaemonEvent gosaSupportDaemon",
756             FATAL_ERROR_DIALOG);
757       }
758       exit();
759     }
760   }
765   function saveBranch()
766   {
767     if($this->dispNewBranch){
768       $type = "branch";
769     }else{
770       $type = "freeze";
771     }
773     /* Check branch name */
774     $name = $_POST['BranchName'];
775     $is_ok = true;
776     $smarty = get_smarty();
777     $smarty->assign("BranchName",$name);
778     $base= $this->fai_base;
780     /* Check used characters */
781     if(!preg_match("/^[0-9a-z\.]*$/",$name)){
782       msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
783       $is_ok = false;
784     }
786     // Check if this name is already in use 
787     if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
788       msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
789       $is_ok = false;
790     }
792     // Handle errors
793     if(!$is_ok && $this->dispNewFreeze){
794       return($this->createFreeze());
795     }elseif(!$is_ok && $this->dispNewBranch){
796       return($this->createBranch());
797     }
799     // Now create new release
801     if(session::is_set('LASTPOST')){
802       $LASTPOST = session::get('LASTPOST');
803     }else{
804       $LASTPOST = array();
805     }
806     $LASTPOST['base'] = $base;
807     $LASTPOST['type'] = $type;
808     $LASTPOST['BranchName'] = $name;
809     session::set('LASTPOST',$LASTPOST);
810     $smarty->assign("iframe", true);
811     $smarty->assign("plugID", $_GET['plug']);
812     $display  = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
813     return($display);
814  
815   }
818   function CheckNewBranchName($name,$base)
819   {
820     $f = $this->fai_release;
821     if($name == ""){
822       return(false);
823     }elseif(in_array($name,$this->getBranches($f))) {
824       return(false);
825     }elseif(tests::is_department_name_reserved($name,$base)){
826       return(false);
827     }
828     return(true);
829   }
832   /* Get available branches for current base */
833   function getBranches($base = false,$prefix = "")
834   {
835     $ret = array("/"=>$this->fai_base);
836     $ldap = $this->config->get_ldap_link();
837     if(!$base){
838       $base = $this->fai_base;
839     }
840     $tmp = FAI::get_all_releases_from_base($base,true);
841     foreach($tmp as $dn => $name){
842       $ret[$name]=$dn;
843     }
844     ksort($ret);
845     $ret = array_flip($ret);
847     return ($ret);
848   }
851   function get_type($array)
852   {
853     if(!isset($array['objectClass'])) return(array());
854     if(in_array("FAIpartitionTable",$array['objectClass'])){
855       return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
856     }
857     if(in_array("FAIscript",$array['objectClass'])){
858       return(array("tabsScript","faiScript","FAISCRIPTTABS"));
859     }
860     if(in_array("FAItemplate",$array['objectClass'])){
861       return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
862     }
863     if(in_array("FAIhook",$array['objectClass'])){
864       return(array("tabsHook","faiHook","FAIHOOKTABS"));
865     }
866     if(in_array("FAIvariable",$array['objectClass'])){
867       return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
868     }
869     if(in_array("FAIprofile",$array['objectClass'])){
870       return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
871     }
872     if(in_array("FAIpackageList",$array['objectClass'])){
873       return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
874     }
875     return(array());
876   }
879   /* Check if the given FAI class is used in this release
880    */
881   static function check_class_name($oc,$name,$dn)
882   {
883     $base = FAI::get_release_dn($dn);
885     if($oc == "FAIprofile"){
886       $f = "";
887       $ocs = array("FAIprofile","FAItemplate","FAIhook","FAIpartitionTable","FAIpackageList","FAIscript","FAIvariable");
888       foreach($ocs as $oc){
889         $f .= "(objectClass=".$oc.")";
890       }
891       $res  = FAI::get_all_objects_for_given_base($base,"(|".$f.")",TRUE);
892     }else{
893       $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
894     }
895     $delete = array();
896     $used   = array();
897     foreach($res as $object){
898       $used[$object['cn'][0]]= $object['cn'][0];
899     }
900     return($used);
901   }
904
905 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
906 ?>