Code

Fixed problem with not copyable disks ... took me about an hour ..
[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 copyPasteHandler($action="",$target=array(),$all=array(),
124       $altTabClass ="", $altTabType = "", $altAclCategory="",$altAclPlugin="")
125   {
127     // Collect real dns, the listed objects are grouped by their cn
128     $headpage = $this->getHeadpage();
129     if($action == "copy"){
130       foreach($target as $t){
131         $entry = $headpage->getEntry($t);
132         
133         // Check for valid FAI objects
134         if(in_array('FAKE_OC_FAI', $entry['objectClass'])){
135           foreach($entry['GROUPS'] as $g){
136             $type = $this->get_type($g);
137             $this->cpHandler->add_to_queue($g['dn'],"copy",$type[0],$type[2],'fai',$this);
138             @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__,$dn,"Entry copied!");
139           }
140         }
141       }
142     }
144     // Initiate pasting
145     if($action == "paste"){
146       $this->cpPastingStarted = TRUE;
147     }
149     // Display any c&p dialogs, eg. object modifications required before pasting.
150     if($this->cpPastingStarted && $this->cpHandler->entries_queued()){
151       $this->cpHandler->SetVar("base",$headpage->getBase());
152       $data = $this->cpHandler->execute();
153       FAI::save_release_changes_now();
154       if(!empty($data)){
155         return($data);
156       }
157     }
159     // Automatically disable pasting process since there is no entry left to paste.
160     if(!$this->cpHandler->entries_queued()){
161       $this->cpPastingStarted = FALSE;
162     }
163     return("");
164   }
167   function newEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
168   {
169     /****************
170       Create a new object
171      ****************/
173     $types = array( 
174         "new_partition"     =>  "FAIpartitionTable",
175         "new_script"        =>  "FAIscript",
176         "new_hook"          =>  "FAIhook",
177         "new_variable"      =>  "FAIvariable",
178         "new_template"      =>  "FAItemplate",
179         "new_package"       =>  "FAIpackageList");
180     $types_i18n = array( 
181         "new_partition"     =>  _("partition table"),
182         "new_script"        =>  _("script"),
183         "new_hook"          =>  _("hook"),
184         "new_variable"      =>  _("variable"),
185         "new_template"      =>  _("template"),
186         "new_package"       =>  _("package list"));
188     if(isset($types[$action])){
189       $type_acl_mapping = array(
190           "FAIpartitionTable"  => "faiPartitionTable",
191           "FAIpackageList"     => "faiPackage",
192           "FAIscript"          => "faiScript",
193           "FAIvariable"        => "faiVariable",
194           "FAIhook"            => "faiHook",
195           "FAIprofile"         => "faiProfile",
196           "FAItemplate"        => "faiTemplate");
198       $acl = $this->ui->get_permissions($this->acl_base,"fai/".$type_acl_mapping[$types[$action]]);
199       if(preg_match("/c/",$acl)){
200         $this->dialogObject = new askClassName($this->config,$this->dn,$this->ui,$types[$action]);
201         $this->dialogObject->parent = &$this;
202       }else{
203         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), $types_i18n[$action]),      ERROR_DIALOG);
204       }
205     }
206     if($action == "new_profile"){
207       $this->dn = "new" ;
209       $acl = $this->ui->get_permissions($this->acl_base,"fai/faiProfile");
210       if(preg_match("/c/",$acl)){
211         $type= $this->get_type(array("objectClass"=>array("FAIprofile")));
212         $str= management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
213         if($str) return($str);
214         $this->tabObject->set_acl_base($this->acl_base);
215         $this->tabObject->by_object[$type[1]]->cn = $name;
216       }else{
217         msg_dialog::display(_("Permission error"), sprintf(_("You have no permission to create a new %s!"), _("profile")), ERROR_DIALOG);
218       }
219     }
220   }
223   function newClassNameSelected()
224   {
225     $this->dialogObject->save_object();
226     if(count($this->dialogObject->check())!=0){
227       foreach($this->dialogObject->check() as $msg){
228         msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
229       }
230     }elseif(isset($this->dialogObject->objectClass)){
231       $this->dn = "new" ;
232       $type= $this->get_type(array("objectClass"=>array($this->dialogObject->objectClass)));
233       $name = $this->dialogObject->save();
235       if(class_exists($type[0])){
236         $this->closeDialogs();
237         $str = management::newEntry('newEntry',array(),array(),$type[0],$type[2],$type[1]);
238         if($str) return($str);
239         $this->tabObject->set_acl_base($this->acl_base);
240         $this->tabObject->by_object[$type[1]]->cn = $name;
241       }
242     }
243   }
247   function editEntry($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
248   {
249     $headpage = $this->getHeadpage();
250     if(count($target) == 1){
251       $entry = $headpage->getEntry($target[0]);
252       if($entry){
254         if(in_array("opsi_local", $entry['TYPES']) || in_array("opsi_netboot", $entry['TYPES'])){ 
255           $name = $entry['cn'];
256           $cfg = $this->opsi->get_product_properties($name);
258           $str = management::editEntry('editEntry',array($name),array(),'tabs_opsiProdConfig','OPSIPRODCONFIG','opsi');
259           if($str) return($str);
260           if(isset($this->tabObject->by_object['opsiProperties'])){
261             $this->tabObject->by_object['opsiProperties']->set_cfg($cfg);
262             $this->tabObject->by_object['opsiProperties']->set_product($name);
263             $this->skipFooter = TRUE;
264           }else{
265             trigger_error("Unknown tab, please check config.");
266           }
268         }else{
269           if(count($entry['GROUPS']) == 1){
270             $data = array_pop($entry['GROUPS']);
271             $type = $this->get_type($data);
272             $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
273             if($str) return($str);
274             $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
275             $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
277           }else{
278             $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"edit");
279           }
280         }
281       }
282     }
283   }
286   function saveOpsiProperties()
287   {
288     if($this->tabObject instanceof tabs_opsiProdConfig && isset($_POST['save_properties'])){
289       $this->tabObject->save_object();
290       $op    = $this->tabObject->by_object['opsiProperties'];
291       $name  = $op->get_product();
292       $cfg   = $op->get_cfg();
293       $this->opsi->set_product_properties($name,$cfg);
294       if($this->opsi->is_error()){
295         msg_dialog::display(_("Error"),msgPool::siError($this->opsi->get_error()),ERROR_DIALOG);
296       }else{
297         $this->remove_lock();
298         $this->closeDialogs();
299       }
300     }
301   }
304   function removeFAIObjects($to_delete)
305   {
306     // Do not allow to remove objects with state freeezed
307     $errors=array();
308     foreach($to_delete as $obj){
309       if(isset($obj['FAIstate']) && preg_match('/^freeze/', $obj['FAIstate'])){
310         $errors[] = $obj['dn'];
311       }else{
312         $this->dns[] = $obj['dn'];
313       }
314     }
315     if(count($errors)){
316       msg_dialog::display(_("Branch locked"),
317           sprintf(_("The following entries are locked, you can't remove them %s."),msgPool::buildList($errors)),INFO_DIALOG);
318     }
320     // Check entry locking
321     if(count($this->dns)){
322       if ($user= get_multiple_locks($this->dns)){
323         return(gen_locked_message($user,$this->dns));
324       }
325       if(count($this->dns)){
326         $smarty = get_smarty();
327         $dns_names = array();
328         foreach($this->dns as $dn){
329           add_lock ($dn, $this->ui->dn);
330           $dns_names[] = LDAP::fix($dn);
331         }
332         $smarty->assign("info",msgPool::deleteInfo($dns_names,_("FAI object")));
333         $smarty->assign("multiple", true);
334         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
335       }
336     }
337     
338   }
340   function removeEntryRequested($action="",$target=array(),$all=array(), $altTabClass ="", $altTabType = "", $altAclCategory="")
341   {
343     $this->closeDialogs();
344     if($action == "remove_multiple"){
346       /* Collect objects to delete and check if objects are freezed
347        */
348       $headpage = $this->getHeadpage();
349       $to_delete = array();
350       foreach($target as $id){
351         $object = $headpage->getEntry($id);
352         if(in_array("FAKE_OC_FAI", $object['objectClass'])){
353           foreach($object['GROUPS']  as $entry){
354             array_push($to_delete, $entry);
355           }
356         }
357       }
358       return($this->removeFAIObjects($to_delete));
359       
360     }else{
362       // Try to remove a single object, only FAI objects can be removed!
363       $headpage = $this->getHeadpage();
364       if(count($target) == 1){
365         $entry = $headpage->getEntry($target[0]);
366         if($entry && !in_array("FAKE_OC_OPSI",$entry['objectClass'])){
367           $this->dialogObject = new faiGroupHandle($entry['GROUPS'],"remove");
368         }
369       }
370     }
371   }
373   /*! \brief  Object removal was confirmed, now remove the requested entries.
374    *
375    *  @param  String  'action'  The name of the action which was the used as trigger.
376    *  @param  Array   'target'  A list of object dns, which should be affected by this method.
377    *  @param  Array   'all'     A combination of both 'action' and 'target'.
378    */
379   function removeEntryConfirmed($action="",$target=array(),$all=array(),
380       $altTabClass="",$altTabType="",$altAclCategory="")
381   {
382     $ldap = $this->config->get_ldap_link();
383     $ldap->cd($this->config->current['BASE']); 
385     foreach($this->dns as $key => $dn){
386       $ldap->cat($dn);
387       if($ldap->count()){
388         $attrs = $ldap->fetch();
389         $type= $this->get_type($attrs);
390         $str = management::removeEntryConfirmed($action,array($dn),$all,$type[0],$type[2],$type[1]);
391         if($str) return($str);
393         // Now save changes
394         FAI::save_release_changes_now();
395         $to_del = FAI::clean_up_releases($dn);
396         foreach($to_del as $dn){
397           $ldap->rmdir_recursive($dn);
398         }
399       }
400     }
401   }
404   function editByGroup()
405   {
406     if($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "edit"){
407       $this->dialogObject->save_object();
408       $entry = $this->dialogObject->get_selected();
409       $this->closeDialogs();
410       $data = array_pop($entry);
411       $type = $this->get_type($data);
412       $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]);
413       if($str) return($str);
414       $this->tabObject->by_object[$type[1]]->FAIstate = $data['FAIstate'];
415       $this->tabObject->read_only = preg_match("/freeze/i", $data['FAIstate']);
416     }elseif($this->dialogObject instanceOf faiGroupHandle && $this->dialogObject->get_mode() == "remove"){
417       $this->dialogObject->save_object();
418       $to_delete = $entry = $this->dialogObject->get_selected();
419       return($this->removeFAIObjects($to_delete));
420     }
421   }
423   
424   protected function saveChanges()
425   {
426     $str = management::saveChanges();
427     if($str) return($str);
429     // Now save changes
430     FAI::save_release_changes_now();
431     $to_del = FAI::clean_up_releases($this->last_dn);
432     foreach($to_del as $dn){
433       $ldap->rmdir_recursive($dn);
434     }
435   }
437   protected function applyChanges()
438   {
439     $str = management::applyChanges();
440     if($str) return($str);
442     // Now save changes
443     FAI::save_release_changes_now();
444     $to_del = FAI::clean_up_releases($this->last_dn);
445     foreach($to_del as $dn){
446       $ldap->rmdir_recursive($dn);
447     }
448   }
450   function detectPostActions()
451   {
452     $action = management::detectPostActions();
453     if(isset($_POST['remove_multiple'])) $action['action'] = "remove";
454     if(isset($_POST['new_profile'])) $action['action'] = "new_profile";
455     if(isset($_POST['new_template'])) $action['action'] = "new_template";
456     if(isset($_POST['new_script'])) $action['action'] = "new_script";
457     if(isset($_POST['new_hook'])) $action['action'] = "new_hook";
458     if(isset($_POST['new_variable'])) $action['action'] = "new_variable";
459     if(isset($_POST['new_package'])) $action['action'] = "new_package";
460     if(isset($_POST['new_partition'])) $action['action'] = "new_partition";
463     if(isset($_POST['save_properties'])) $action['action'] = "saveOpsiProperties";
464     if(isset($_POST['cancel_properties'])) $action['action'] = "cancel";
466     if(isset($_POST['edit_continue'])) $action['action'] = "newClassNameSelected";
467     if(isset($_POST['edit_cancel'])) $action['action'] = "cancel";
469     if(isset($_POST['faiGroupHandle_cancel'])) $action['action'] = "cancel";
470     if(isset($_POST['CancelBranchName'])) $action['action'] = "cancel";
471     if(isset($_POST['delete_branch_confirm'])) $action['action'] = "removeBranchConfirmed";
472     if(isset($_GET['PerformBranch'])) $action['action'] = "PerformBranch";
473     if(isset($_POST['UseBranchName'])) $action['action'] = "saveBranch";
474     if(isset($_POST['faiGroupHandle_apply']))  $action['action'] = "editByGroup";
475     if(isset($_GET['act']) && $_GET['act'] == "branch_branch")  $action['action'] = "createBranch";
476     if(isset($_GET['act']) && $_GET['act'] == "freeze_branch")  $action['action'] = "createFreeze";
477     if(isset($_GET['act']) && $_GET['act'] == "remove_branch")  $action['action'] = "removeBranch";
479     foreach($_POST as $name => $value){
480       if(preg_match("/^edit_([0-9]*)_([a-z]*)_(x|y)/i", $name)){
481         $id = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\1", $name);
482         $tab = preg_replace("/^edit_([0-9]*)_([a-z]*)_(x|y)/i","\\2", $name);
483         $headpage = $this->getHeadpage();
484         if(isset($headpage->entries[$id]['GROUPS'][$tab])){
485           $data =$headpage->entries[$id]['GROUPS'][$tab];
486           $type = $this->get_type($data);
487           $str = management::editEntry('editEntry',array($data['dn']),array(),$type[0],$type[2],$type[1]); 
488           if($str) return($str);
489         } 
490         break;
491       }
492     }
493     return($action);
494   }
497   function renderList()
498   {
499     $filter = $this->getFilter();
500     $headpage = $this->getHeadpage();
501     $filter->setComboBoxOptions("RELEASE",$this->getReleaseList());
503     if(isset($_POST['RELEASE'])){
504       $this->fai_release = get_post('RELEASE');
505     }
506     $headpage->setBase($this->fai_release);
507     $headpage->update();
508     $smarty = get_smarty();
509     $smarty->assign("fai_release", $this->fai_release);
510     $smarty->assign("opsi_available", is_object($this->opsi));
511     $smarty->assign("fai_base", $this->fai_base);
512     $r = $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'));
513     $c = $this->config->search("faiManagement", "POSTCREATE",array('menu','tabs'));
514     $smarty->assign("allow_create", $c);
515     $smarty->assign("allow_remove", $r);
516     $display = $headpage->render();
517     return($this->getHeader().$display);
518   }
520   function getReleaseList($base = "", $prefix ="")
521   {
522     $list = array();
523     if(empty($base)){
524       $base = $this->fai_base;
525       $list[$base] = "/";
526     }
528     $ldap = $this->config->get_ldap_link();
529     $ldap->ls("(objectClass=FAIbranch)",$base,array("ou","FAIstate"));
530     $cfg_rel = $this->config->search("faiManagement","DEFAULTFAIRELEASE",array("menu"));
532     while($release = $ldap->fetch()){
533       $list[$release['dn']] = $prefix.$release['ou'][0];
535       // Preset to prefered releaes if necessary  
536       if(empty($this->fai_release) && $cfg_rel == $release['dn']){
537         $this->fai_release = $release['dn'];
538       }
540       $list = array_merge($list,$this->getReleaseList($release['dn'],$prefix."&nbsp; "));
541     }
542     return($list);
543   }
546   static function filterProperties($row, $classes)
547   {
548     $objects = array(
549         "FAIpartitionTable"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png",
550           "NAME"=>_("Partition table"),"KZL"=> "PT", "VAR"=>"ShowPartitions"),
551         "FAIpackageList"     => array("IMG"=> "plugins/fai/images/fai_packages.png",
552           "NAME"=>_("Package list") ,  "KZL"=> "PL", "VAR"=>"ShowPackages"),
553         "FAIscript"          => array("IMG"=> "plugins/fai/images/fai_script.png",
554           "NAME"=>_("Scripts") ,       "KZL"=> "S",  "VAR"=>"ShowScripts"),
555         "FAIvariable"        => array("IMG"=> "plugins/fai/images/fai_variable.png",
556           "NAME"=>_("Variables") ,     "KZL"=> "V",  "VAR"=>"ShowVariables"),
557         "FAIhook"            => array("IMG"=> "plugins/fai/images/fai_hook.png",
558           "NAME"=>_("Hooks"),          "KZL"=> "H",  "VAR"=>"ShowHooks"),
559         "FAIprofile"         => array("IMG"=> "plugins/fai/images/fai_profile.png",
560           "NAME"=>_("Profile") ,       "KZL"=> "P",  "VAR"=>"ShowProfiles"),
561         "FAItemplate"        => array("IMG"=> "plugins/fai/images/fai_template.png",
562           "NAME"=>_("Templates") ,     "KZL"=> "T",  "VAR"=>"ShowTemplates"),
563         "opsi_netboot"       => array("IMG"=> "plugins/opsi/images/netboot_package.png",
564           "NAME"=>_("OPSI netboot product") ,     "KZL"=> "ON",  "VAR"=>"ShowOpsiNetboot"),
565         "opsi_local"         => array("IMG"=> "plugins/opsi/images/local_package.png",
566           "NAME"=>_("OPSI localboot product")   ,     "KZL"=> "OL",  "VAR"=>"ShowOpsiLocal"));
568     $icon_list = "";
569     foreach($objects as $type => $type_data){
570       if(in_array($type, $classes)){
571         $icon_list .= "<input type='image' src='".$type_data['IMG']."' title='".$type_data['NAME']."'
572           alt='".$type_data['KZL']."' class='center' name='edit_".$row."_".$type."'>\n";
573       }else{
574         $icon_list .= "<img src='images/empty.png' alt=' ' class='center'>\n";
575       }
576     }
578     return $icon_list;
579   }
584   function removeBranch()
585   {
586     /* Check if we have a post remove method configured
587      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
588      */
589     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
590       /* Load permissions for selected 'dn' and check if
591          we're allowed to remove this 'dn' */
592       if(preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
593         $smarty=get_smarty();
594         $smarty->assign("release_hidden",base64_encode($this->fai_release));
595         $smarty->assign("info", msgPool::deleteInfo(LDAP::fix($this->fai_release),_("FAI branch/freeze")));
596         return($smarty->fetch(get_template_path('remove_branch.tpl',TRUE)));
597       } else {
598         msg_dialog::display(_("Permission error"), _("You have no permission to delete this release!"), ERROR_DIALOG);
599       }
600     }
601   }
604   function removeBranchConfirmed()
605   {
606     /* Check if we have a post remove method configured
607      *  else skip this operation. (Skip:Button in the ui should be disabled in this case too)
608      */
609     if("" != $this->config->search("faiManagement", "POSTREMOVE",array('menu','tabs'))){
611       if(!isset($_POST['release_hidden']) || base64_decode($_POST['release_hidden']) != $this->fai_release){
612         msg_dialog::display(_("Warning"),_("Release remove aborted because the release name check failed!"));
613       }else{
615         $bb =  $this->fai_release;
616         $ldap = $this->config->get_ldap_link();
618         $br = $this->getBranches();
620         if(isset($br[$bb]) && preg_match("/d/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
621           $name = $br[$bb];
623           $ldap->cd($bb);
624           $ldap->recursive_remove();
625           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('applicationRDN'), $bb));
626           $ldap->recursive_remove();
627           $ldap->cd(preg_replace('/,'.preg_quote(get_ou('faiBaseRDN'), '/').'/i', ','.get_ou('mimetypeRDN'), $bb));
628           $ldap->recursive_remove();
630           /* Search for all groups with configured application menus.
631              - First search all groups, to ensure that we only remove entries form whithin groups.
632              - The search für menu configuration for the specified release and collect all those dns.
633              - Remove entries
634            */
635           $release_ou = preg_replace("/".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$bb);
636           $ldap->cd($this->config->current['BASE']);
637           $ldap->search("(objectClass=posixGroup)",array("dn"));
639           /* Collect all group dns
640            */
641           $groups = array();
642           while($attrs = $ldap->fetch()){
643             $groups[] = $attrs['dn'];
644           }
647           /* Collect all group menu release dns that match the release we have removed
648            */
649           $dns = array();
650           foreach($groups as $dn){
651             $ldap->cd($dn);
652             $ldap->search("(objectClass=FAIbranch)",array("dn"));
653             while($attrs = $ldap->fetch()){
654               if(preg_match("/^".preg_quote($release_ou, '/')."/",$attrs['dn'])){
655                 $dns[] = $attrs['dn'];
656               }
657             }
658           }
660           /* Finally remove collected release dns
661            */
662           foreach($dns as $dn){
663             $ldap->cd($dn);
664             $ldap->recursive_remove();
665           }
667           /* Post remove */
668           $this->fai_release = $this->fai_base;
669           $this->lock_name   = $name;
670           $this->lock_dn     = $bb;
671           $this->postremove();
673           $fai_filter = session::get("fai_filter");
674           $fai_filter['fai_release'] = $this->fai_release;
675           session::set("fai_filter",$fai_filter);
677           new log("remove","fai/".get_class($this),$br[$bb],array(),"Release removed");
678         }
679       }
680     }
681   }
684   function createBranch()
685   { 
686     $smarty = get_smarty();
687     $this->dispNewBranch=true;
688     $this->dispNewFreeze=false;
689     $smarty->assign("iframe",false);
690     if(isset($_POST['BranchName'])){
691       $smarty->assign("BranchName", $_POST['BranchName']);
692     }else{
693       $smarty->assign("BranchName","");
694     }
695     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
696   }
698   function createFreeze()
699   { 
700     $smarty = get_smarty();
701     $this->dispNewFreeze=true;
702     $this->dispNewBranch=false;
703     $smarty->assign("iframe",false);
704     if(isset($_POST['BranchName'])){
705       $smarty->assign("BranchName", $_POST['BranchName']);
706     }else{
707       $smarty->assign("BranchName","");
708     }
709     return($smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__))));
710   }
714   function PerformBranch()
715   {
716     if(!preg_match("/c/",$this->ui->get_permissions($this->acl_base,"fai/faiManagement"))){
717       msg_dialog::display(_("Permission error"), msgPool::permCreate(_("Branch")), ERROR_DIALOG);
718     }else{
720       /* In order to see error messages we have to reset the error handler.
721          Due to the exit();
722        */
723       restore_error_handler();
725       $this->dispNewBranch = false;
726       $this->dispNewFreeze = false;
728       $LASTPOST = session::get('LASTPOST');
729       $base = $LASTPOST['base'];
730       $_POST  = session::get('LASTPOST');
731       $name = $_POST['BranchName'];
733       $type = $LASTPOST['type'];
734       $ldap = $this->config->get_ldap_link();
736       $baseToUse = $base;
737       if($this->fai_release !=  $this->fai_base){
738         $baseToUse = $this->fai_release;
739       }
741       /* Create new Release name to be able to set faidebianRelease for FAIpackageList */
743       $CurrentReleases  = $this->getBranches();
744       $NewReleaseName   = $name;
745       if(isset($CurrentReleases[$this->fai_release])) {
746         if($this->fai_release != $this->fai_base){
747           $NewReleaseName = $CurrentReleases[$this->fai_release]."/".$name;
748           $NewReleaseName = preg_replace("#\/#","/",$NewReleaseName);
749         }else{
750           $NewReleaseName   = $name;
751         }
752       }
753       $appsrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),$baseToUse);
754       $appdst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('applicationRDN'),"ou=".$name.",".$baseToUse) ;
756       $mimesrc = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),$baseToUse);
757       $mimedst = preg_replace("/".preg_quote(get_ou('faiBaseRDN'), '/')."/i",get_ou('mimetypeRDN'),"ou=".$name.",".$baseToUse) ;
759       /* Check if source depeartments exist */
760       foreach(array($baseToUse,$appsrc,$mimesrc) as $dep){
761         $ldap->cd($this->config->current['BASE']);
762         $ldap->cat($dep);
763         if(!$ldap->count()){
764           $ldap->create_missing_trees($dep);
765         }
766       }
768       /* Print header to have styles included */
769       echo '  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
770         <html>
771         <head>
772         <title></title>
773         <style type="text/css">@import url("themes/default/style.css");</style>
774         <script language="javascript" src="include/focus.js" type="text/javascript"></script>
775         </head>
776         <body style="background: none;margin:3px;color:black">
777         ';
779       new log("create","fai/".get_class($this),$NewReleaseName,array(),"New $type created");
781       /* Duplicate group application releases
782        */
783       FAI::copy_FAI_group_releases($CurrentReleases[$this->fai_release],$name,$type);
785       /* Duplicate applications
786        */
787       $ldap->cat($appsrc,array("dn")) ;
788       if($ldap->count()){
789         $ldap->cd ($appdst);
790         $ldap->recursive_remove();
791         FAI::copy_FAI_resource_recursive($appsrc,$appdst,$NewReleaseName,$type,true);
792       }
794       /* Duplicate mime types
795        */
796       $ldap->cat($mimesrc,array("dn")) ;
797       if($ldap->count()){
798         $ldap->cd ($mimedst);
799         $ldap->recursive_remove();
800         FAI::copy_FAI_resource_recursive($mimesrc,$mimedst,$NewReleaseName,$type,true);
801       }
803       $attr = array();
804       $attr['objectClass'] = array("organizationalUnit","FAIbranch");
805       $attr['ou']       = $name;
806       $attr['FAIstate'] = $type;
807       $ldap->cd($this->config->current['BASE']);
808       $ldap->cd("ou=".$name.",".$baseToUse);
809       $ldap->cat("ou=".$name.",".$baseToUse);
810       if($ldap->count()){
811         $ldap->modify($attr);
812       }else{
813         $ldap->add($attr);
814       }
816       /* Duplicate fai objects
817        */
818       //      $ldap->cd ("ou=".$name.",".$baseToUse);
819       //      $ldap->recursive_remove();
820       //      FAI::copy_FAI_resource_recursive($baseToUse,"ou=".$name.",".$baseToUse,$NewReleaseName,$type,true);
822       echo "<div style='width:100%;text-align:right;'><form name='form' method='post' action='?plug=".$_GET['plug']."' target='_parent'>
823         <br><input type='submit' name='CloseIFrame' value='"._("Continue")."'>
824         <input type='hidden' name='php_c_check' value='1'>
825         </form></div>";
827       echo "<script language=\"javascript\" type=\"text/javascript\">scrollDown2();</script>" ;
829       /* Print footer to have valid html */
830       echo "</body></html>";
832       $this->dispNewFreeze = false;
834       /* Postcreate */
836       /* Assign possible attributes */
837       $this->lock_type  = $type;
838       $this->lock_name  = $name;
839       $this->lock_dn    = $baseToUse;
840       $this->postcreate();
842       /* Send daemon event to reload the fai release database
843        */
844       if(class_available("DaemonEvent") && class_available("gosaSupportDaemon")){
845         $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
846         if(isset($events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'])){
847           $evt = $events['TRIGGERED']['DaemonEvent_recreate_fai_release_db'];
848           $tmp = new $evt['CLASS_NAME']($this->config);
849           $tmp->set_type(TRIGGERED_EVENT);
850           $tmp->add_targets(array("GOSA"));
851           $o_queue = new gosaSupportDaemon();
852           if(!$o_queue->append($tmp)){
853             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
854           }
855         }
856       }else{
857         trigger_error("Unknown class DaemonEvent / gosaSupportDaemon");
858         msg_dialog::display(_("Fatal error"),
859             "Daemon events called but classes where not accessible, DaemonEvent gosaSupportDaemon",
860             FATAL_ERROR_DIALOG);
861       }
862       exit();
863     }
864   }
869   function saveBranch()
870   {
871     if($this->dispNewBranch){
872       $type = "branch";
873     }else{
874       $type = "freeze";
875     }
877     /* Check branch name */
878     $name = $_POST['BranchName'];
879     $is_ok = true;
880     $smarty = get_smarty();
881     $smarty->assign("BranchName",$name);
882     $base= $this->fai_base;
884     /* Check used characters */
885     if(!preg_match("/^[0-9a-z\.]*$/",$name)){
886       msg_dialog::display(_("Error"), msgPool::invalid(_("Name"),$name,"/[0-9a-z\.]/"), ERROR_DIALOG);
887       $is_ok = false;
888     }
890     // Check if this name is already in use 
891     if(!$this->CheckNewBranchName($_POST['BranchName'],$this->fai_release)){
892       msg_dialog::display(_("Error"), msgPool::duplicated(_("Name")), ERROR_DIALOG);
893       $is_ok = false;
894     }
896     // Handle errors
897     if(!$is_ok && $this->dispNewFreeze){
898       return($this->createFreeze());
899     }elseif(!$is_ok && $this->dispNewBranch){
900       return($this->createBranch());
901     }
903     // Now create new release
905     if(session::is_set('LASTPOST')){
906       $LASTPOST = session::get('LASTPOST');
907     }else{
908       $LASTPOST = array();
909     }
910     $LASTPOST['base'] = $base;
911     $LASTPOST['type'] = $type;
912     $LASTPOST['BranchName'] = $name;
913     session::set('LASTPOST',$LASTPOST);
914     $smarty->assign("iframe", true);
915     $smarty->assign("plugID", $_GET['plug']);
916     $display  = $smarty->fetch(get_template_path('faiNewBranch.tpl', TRUE, dirname(__FILE__)));
917     return($display);
918  
919   }
922   function CheckNewBranchName($name,$base)
923   {
924     $f = $this->fai_release;
925     if($name == ""){
926       return(false);
927     }elseif(in_array($name,$this->getBranches($f))) {
928       return(false);
929     }elseif(tests::is_department_name_reserved($name,$base)){
930       return(false);
931     }
932     return(true);
933   }
936   /* Get available branches for current base */
937   function getBranches($base = false,$prefix = "")
938   {
939     $ret = array("/"=>$this->fai_base);
940     $ldap = $this->config->get_ldap_link();
941     if(!$base){
942       $base = $this->fai_base;
943     }
944     $tmp = FAI::get_all_releases_from_base($base,true);
945     foreach($tmp as $dn => $name){
946       $ret[$name]=$dn;
947     }
948     ksort($ret);
949     $ret = array_flip($ret);
951     return ($ret);
952   }
955   function get_type($array)
956   {
957     if(!isset($array['objectClass'])) return(array());
958     if(in_array("FAIpartitionTable",$array['objectClass'])){
959       return(array("tabsPartition","faiPartitionTable","FAIPARTITIONTABS"));
960     }
961     if(in_array("FAIscript",$array['objectClass'])){
962       return(array("tabsScript","faiScript","FAISCRIPTTABS"));
963     }
964     if(in_array("FAItemplate",$array['objectClass'])){
965       return(array("tabsTemplate","faiTemplate","FAITEMPLATETABS"));
966     }
967     if(in_array("FAIhook",$array['objectClass'])){
968       return(array("tabsHook","faiHook","FAIHOOKTABS"));
969     }
970     if(in_array("FAIvariable",$array['objectClass'])){
971       return(array("tabsVariable","faiVariable","FAIVARIABLETABS"));
972     }
973     if(in_array("FAIprofile",$array['objectClass'])){
974       return(array("tabsProfile","faiProfile","FAIPROFILETABS"));
975     }
976     if(in_array("FAIpackageList",$array['objectClass'])){
977       return(array("tabsPackage","faiPackage","FAIPACKAGETABS"));
978     }
979     return(array());
980   }
983   /* Check if the given FAI class is used in this release
984    */
985   static function check_class_name($oc,$name,$dn)
986   {
987     $base = FAI::get_release_dn($dn);
989     if($oc == "FAIprofile"){
990       $f = "";
991       $ocs = array("FAIprofile","FAItemplate","FAIhook","FAIpartitionTable","FAIpackageList","FAIscript","FAIvariable");
992       foreach($ocs as $oc){
993         $f .= "(objectClass=".$oc.")";
994       }
995       $res  = FAI::get_all_objects_for_given_base($base,"(|".$f.")",TRUE);
996     }else{
997       $res  = FAI::get_all_objects_for_given_base($base,"(objectClass=".$oc.")",TRUE);
998     }
999     $delete = array();
1000     $used   = array();
1001     foreach($res as $object){
1002       $used[$object['cn'][0]]= $object['cn'][0];
1003     }
1004     return($used);
1005   }
1008
1009 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1010 ?>