Code

Updated check for activated faiManagement
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_workstationStartup.inc
1 <?php
2 class workstartup extends plugin
3 {
4   /* Ldap server list */
5   var $gotoLdapServers    = array();
6   var $gotoLdapServerList = array();
7   var $gotoLdap_inherit   = FALSE;
9   /* Generic terminal attributes */
10         # This is disabled as long there is no possiblilty to set these parameters
11   var $gotoBootKernel       = "default-inherited";
12   var $gotoKernelParameters = "";
13   var $gotoLdapServer       = "default-inherited";
14   var $gotoModules          = array();
15   var $gotoAutoFs           = array();
16   var $gotoFilesystem       = array();
17   var $gotoTerminalPath     = "";
18   var $gotoBootKernels      = array();
20   /* attribute list for save action */
21   var $attributes           = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", 
22                                     "FAIclass", "FAIstatus", "gotoShare","FAIdebianMirror", "FAIrelease");
23   var $objectclasses        = array("GOhard", "FAIobject");
25   /* Share */
26   var $gotoShares         = array();// Currently Share Option
27   var $gotoShare          = "";     // currently selected Share Option
28   var $gotoShareSelections= array();// Available Shares for this account in Listbox format
29   var $gotoAvailableShares= array();// Available Shares for this account
31   /* Helper */
32   var $orig_dn            = "";
33   var $ignore_account     = TRUE;
34  
35   /* FAI class selection */ 
36   var $FAIclass           = array();  // The currently selected classes 
37   var $FAIrelease           = "";
38   var $FAIdebianMirror      = "auto";
39   var $si_active            = FALSE;
40   var $si_fai_action_failed = FALSE;
42   var $cache              = array(); // Used as cache in fai mehtods
44   var $FAIstatus          = "";
45   var $FAIclasses         = array();
47   var $view_logged        = FALSE;
48   
49   /* FAI class selection */
50   var $InheritedFAIclass       = array();
51   var $InheritedFAIrelease     = "";
52   var $InheritedFAIdebianMirror= "auto";
54   var $CopyPasteVars    = array("gotoModules","gotoShares");
55   var $fai_activated    = FALSE;
56   var $o_group_dn       = "";
57   var $member_of_ogroup = FALSE;
59   function workstartup (&$config, $dn= NULL, $parent= NULL)
60   {
61     /* Check if FAI is active */
62     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
63     if(!empty($tmp) && class_available("faiManagement")){
64       $this->fai_activated = TRUE;
65     }else{
66       $this->attributes = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", "gotoShare");
67       $this->objectclasses  = array("GOhard");
68     }
70     plugin::plugin ($config, $dn, $parent);
72     /* Check for si daemon */
73     $this->si_active = $this->config->get_cfg_value("core","gosaSupportURI") != "";
75     /* Check object group membership */
76     if(!isset($this->parent->by_object['ogroup'])){
77       $ldap = $this->config->get_ldap_link();
78       $ldap->cd ($this->config->current['BASE']);
79       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn","dn"));
80       if($ldap->count()){
81         $this->member_of_ogroup = TRUE;
82         $attrs = $ldap->fetch();
83         $this->o_group_dn = $attrs['dn'];
84       }
85     }
87     /* Creating a list of valid Mirrors 
88      * none will not be saved to ldap.
89      */
90     $ldap   = $this->config->get_ldap_link();
91     $ldap->cd($this->config->current['BASE']);
92     $ui = get_userinfo();
93     foreach($this->config->data['SERVERS']['LDAP'] as $dn => $data){
94       if($ui->get_category_permissions($data['dn'],"server",TRUE)){
95         for($i = 0; $i < $data['goLdapBase']['count']; $i ++){
96           $name = $data["cn"][0].":".$data["goLdapBase"][$i];
97           $this->gotoLdapServerList[]= $name; 
98         }
99       }
100     }
102     /* Get list of assigned ldap servers 
103      */ 
104     if(isset($this->attrs['gotoLdapServer'])){
105       unset($this->attrs['gotoLdapServer']['count']);
106       sort($this->attrs['gotoLdapServer']);
107       foreach($this->attrs['gotoLdapServer'] as $value){
108         $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
109       }
110     } 
111     natcasesort($this->gotoLdapServerList);
113     if(!count($this->gotoLdapServers) && $this->member_of_ogroup){ 
114       $this->gotoLdap_inherit = TRUE;
115     }
117     /* FAI Initialization
118        Skip this if FAI is not activated 
119      */
120     if($this->fai_activated) {
122       /* Parse used FAIclasses (stored as string).
123        * The single classes are seperated by ' '.
124        * There is also the release type given, after first
125        *  occurrence of ':'.
126        */
127       $this->FAIclass =array();
128       if(isset($this->attrs['FAIclass'][0])){
129         $tmp = explode(" ",$this->attrs['FAIclass'][0]);
130         $tmp2 =array();  
132         foreach($tmp as $class){
133           if( ":" == $class[0] ) {
134             $this->FAIrelease = trim(substr($class, 1));
135           }else{
136             $tmp2[$class] = $class;
137           }
138         }
139         $this->FAIclass = $tmp2;
140       }
141     }
143     /* Get arrays */
144     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
145       if (isset($this->attrs["$val"]["count"])){
146         for ($i= 0; $i<$this->attrs["count"]; $i++){
147           if (isset($this->attrs["$val"][$i])){
148             array_push($this->$val, $this->attrs["$val"][$i]);
149           }
150         }
151       }
152       sort ($this->$val);
153       $this->$val= array_unique($this->$val);
154     }
156     /* Prepare Shares */
157     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
158       unset($this->attrs['gotoShare']['count']);
159       foreach($this->attrs['gotoShare'] as $share){
160         $tmp = $tmp2 = array();
161         $tmp = explode("|",$share);
162         $tmp2['server']      =$tmp[0];
163         $tmp2['name']        =$tmp[1];
164         $tmp2['mountPoint']  =$tmp[2];
165         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
166       }
167     }
169     $this->gotoShareSelections= $config->getShareList(true);
170     $this->gotoAvailableShares= $config->getShareList(false);
171     $tmp2 = array();
172   
174     $this->orig_dn= $this->dn;
176     /* Handle inheritance value "default" */
177     if ($this->member_of_ogroup){
178       $this->gotoBootKernels= array("default-inherited" => '['._("inherited").']'); 
179     }
181     /* If we are member in an object group,
182      *  we have to handle inherited values.
183      * So you can see what is inherited.
184      */
185     if ($this->member_of_ogroup){
187       if(count($this->FAIclass)==0 && $this->FAIrelease == ""){
188         $this->FAIdebianMirror = "inherited";
189       }
191       if($this->fai_activated){
192         $map= array("gotoBootKernel","FAIclass","FAIdebianMirror");
193       }else{
194         $map= array("gotoBootKernel");
195       }
197       $ldap = $this->config->get_ldap_link();
198       $ldap->cat($this->o_group_dn);
199       $attrs= $ldap->fetch();
201       foreach ($map as $name){
202         if (!isset($attrs[$name][0])){
203           continue;
204         }
206         switch ($name){
207           case 'gotoBootKernel':
208             $this->gotoBootKernels['default-inherited']=  _("inherited").' ['.$attrs[$name][0].']' ;
209             break;
211           case 'FAIclass':
212             $str = explode(":",$attrs[$name][0]);
213             $this->InheritedFAIclass    = explode(" ",trim($str[0]));
214             $this->InheritedFAIrelease  = trim($str[1]);
215             break;
217           case 'FAIdebianMirror':
218             $this->InheritedFAIdebianMirror = $attrs[$name][0];
219             break;
220         }
221       }
222     }
224     $this->update_fai_cache();
226     if($this->fai_activated && !$this->si_fai_action_failed && $this->si_active){
228       /* Check if the current mirror is available 
229        */
230       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
231         if(count($this->FAIclass)){
232           msg_dialog::display(_("Error"), sprintf(_("FAI mirror '%s' is not available - setting to mirror 'auto'!"), $this->FAIdebianMirror), ERROR_DIALOG);
233         }
234         $this->FAIdebianMirror = "auto";
235         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
236         $this->cache['CLASSES'] = array();
237         $this->update_fai_cache();
238       }
239   
240       /* Check if the current mirror is available 
241        */
242       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
243         $new_release = key($this->cache['SERVERS'][$this->FAIdebianMirror]); 
244         if(count($this->FAIclass)){
245           msg_dialog::display(_("Error"), sprintf(_("FAI release '%s' is not available on mirror '%s' - setting to release '%s'!"), $this->FAIrelease, $this->FAIdebianMirror,$new_release), ERROR_DIALOG);
246         }
247         $this->FAIrelease = $new_release;
248         $this->cache['CLASSES'] = array();
249         $this->update_fai_cache();
250       }
251     }
254     // Prepare lists
255     $this->ldapList = new sortableListing(array(),array(), TRUE);
256     $this->ldapList->setDeleteable(true);
257     $this->ldapList->setEditable(false);
258     $this->ldapList->setWidth("100%");
259     $this->ldapList->setHeight("100px");
260     $this->ldapList->setHeader(array(_("LDAP server")));
261     $this->ldapList->setDefaultSortColumn(0);
263     $this->faiList = new sortableListing(array(),array(), TRUE);
264     $this->faiList->setDeleteable(true);
265     $this->faiList->setEditable(false);
266     $this->faiList->setWidth("100%");
267     $this->faiList->setHeight("160px");
268     $this->faiList->setColspecs(array('50%','50%'));
269     $this->faiList->setHeader(array(_("Name"),_("Classes")));
270     $this->faiList->setDefaultSortColumn(0);
272   }
274   
275   function check()
276   {
277     $messages = array();
278     
279     /* Call common method to give check the hook */
280     $messages= plugin::check();
282     /* If there are packages selected, but no mirror show error */   
283     if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
284       $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
285     }
287     return($messages);
288   }
290   function execute()
291   {
292         /* Call parent execute */
293         plugin::execute();
295     if($this->is_account && !$this->view_logged){
296       $this->view_logged = TRUE;
297       new log("view","workstation/".get_class($this),$this->dn);
298     }
300     /* Do we represent a valid terminal? */
301     if (!$this->is_account && $this->parent === NULL){
302       $display= image("images/small-error.png")."<b>".msgPool::noValidExtension(_("workstation"))."</b>";
303       return ($display);
304     }
306     /* Add module */
307     if (isset ($_POST['add_module'])){
308       if ($_POST['module'] != "" && $this->acl_is_writeable("gotoModules")){
309         $this->add_list ($this->gotoModules, $_POST['module']);
310       }
311     }
313     /* Delete module */
314     if (isset ($_POST['delete_module'])){
315       if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoModules")){
316         $this->del_list ($this->gotoModules, $_POST['modules_list']);
317       }
318     }
320     /* FAI class management */
321     if($this->fai_activated){
322       if(((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))) && ($this->acl_is_writeable("FAIclass"))){
323         $found = 0 ; 
325         /* If this new class/profile will attach a second partition table
326          * to our list of classes, abort and show a message.
327          */
328         foreach($this->FAIclass as $name){
329           if(isset($this->FAIclassInfo[$name])){
330             foreach($this->FAIclassInfo[$name] as $atr){
331               if(isset($atr['obj'])){
332                 if($atr['obj'] == "FAIpartitionTable"){
333                   $found ++ ; 
334                 }
335               }
336             }
337           }
338         }
340         if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpartitionTable']))&&($found>0)){
341           msg_dialog::display(_("Error"), _("There is already a profile containing a partition table in your configuration!") , ERROR_DIALOG);
342         }else{
343           $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
344         }
345       }
348         $this->faiList->save_object();
349         $action = $this->faiList->getAction();
350         if($action['action'] == 'delete'){
351             $id = $this->faiList->getKey($action['targets'][0]);
352             unset($this->FAIclass[$id]);
353         }
354         if($action['action'] == 'reorder'){
355             $this->FAIclass= $this->faiList->getMaintainedData();
356         }
358       /* Move one used class class one position up or down */
359       if($this->acl_is_writeable("FAIclass")){
360         foreach($_POST as $name => $val){
363           if(preg_match("/fai_remove/i",$name)){
364             $value = postDecode(preg_replace("/fai_remove_/i","",$name));
365             unset($this->FAIclass[$value]);
366           }
367         }
368       }
370       /* Delete selected class from our list */
371       if($this->acl_is_writeable("FAIclass")){
372         if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
373           if(isset($this->FAIclass[$_POST['FAIclassSel']])){
374             unset($this->FAIclass[$_POST['FAIclassSel']]);
375           }
376         }
377       }
378     }// END fai handling
380     /* Show main page */
381     $smarty= get_smarty();
383     /* Assign ACLs to smarty */
384     $tmp = $this->plInfo();
385     foreach($tmp['plProvidedAcls'] as $name => $translation){
386       $smarty->assign($name."ACL",$this->getacl($name));
387     } 
389     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
391     /* In this section server shares will be defined
392      * A user can select one of the given shares and a mount point
393      *  and attach this combination to his setup.
394      */
395     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
396     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
398     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
399      * This entry will be, a combination of mountPoint and sharedefinitions
400      */
401     if((isset($_POST['gotoShareAdd'])) && isset($_POST['gotoShareSelection']) && ($this->acl_is_writeable("gotoShare"))) {
402       /* We assign a share to this user, if we don't know where to mount the share */
403       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
404         msg_dialog::display(_("Error"), msgPool::required(_("Mount point")), ERROR_DIALOG);
405       }else{
406     
407         if(isset($this->gotoAvailableShares[$_POST['gotoShareSelection']])){
408           $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
409           $s_mount = $_POST['gotoShareMountPoint'];
410           /* Preparing the new assignment */
411           $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
412           $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
413         }
414       }
415     }
417     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
418      * If there is no defined share selected, we will abort the deletion without any message
419      */
420     if(($this->acl_is_writeable("gotoShare"))&& (isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
421       unset($this->gotoShares[$_POST['gotoShare']]);
422     }
424     $smarty->assign("gotoShares",$this->printOutAssignedShares());
425     $smarty->assign("gotoSharesCount",count($this->printOutAssignedShares()));
426     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
427     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
429     $this->ldapList->setAcl($this->getacl('gotoLdapServer'));
431     /* Add new ldap server to the list */
432     if($this->acl_is_writeable("gotoLdapServer") && 
433         !$this->gotoLdap_inherit && 
434         isset($_POST['add_ldap_server']) && 
435         isset($_POST['ldap_server_to_add'])){
436       if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
437         $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
438         if(!in_array($to_add,$this->gotoLdapServers)){
439           $this->gotoLdapServers[] = $to_add;
440         }
441       }
442     }
443     
444     /* Move ldap servers up and down */
445     $this->ldapList->save_object();
446     $action = $this->ldapList->getAction();
447     if($action['action'] == 'reorder'){
448         $this->gotoLdapServers = array_values($this->ldapList->getMaintainedData());
449     }
450     if($action['action'] == 'delete'){
451         $id = $this->ldapList->getKey($action['targets'][0]);
452         $value = $this->gotoLdapServers[$id];
453         $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
454     }
456     /* Add Entries */
457     $data = $lData = array();
458     foreach($this->gotoLdapServers as $key => $server){
459         $data[$key]=$server;
461         /* Announce missing entries */
462         if(!in_array($server,$this->gotoLdapServerList)){
463             $server = $server."&nbsp;<font style='color:red'>(missing)</font>";
464         }
466         /* Convert old style entry */
467         if (!preg_match('%:ldaps?://%', $server)){
468             $server= "ldap://".preg_replace('/^([^:]+):/', '\1/', $server);
469         } else {
470             $server= preg_replace("/^[^:]+:/", "", $server);
471             $lData[$key] = array('data'=>array($server));
472         }    
473     }
474     $this->ldapList->setListData($data,$lData);
475     $this->ldapList->update();
478     if($this->gotoLdap_inherit){
479       $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
480     }else{
481       $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
482     }
483     
484     $list = array();
485     foreach($this->gotoLdapServerList as $key => $entry){
486       if(!in_array($entry,$this->gotoLdapServers)){
488         /* Convert old style entry */
489         if (!preg_match('%:ldap[s]*://%', $entry)){
490           $entry= "ldap://".preg_replace('/^([^:]+):/', '\1/', $entry);
492         /* Beautify new style entries */
493         } else {
494           $entry= preg_replace("/^[^:]+:/", "", $entry);
495         }
497         $list[$key] = $entry;
498       }
499     }
500     $smarty->assign("gotoLdapServers",    $this->ldapList->render());
501     $smarty->assign("gotoLdapServerList", $list);
502     $smarty->assign("gotoLdap_inherit",   $this->gotoLdap_inherit);
503     $smarty->assign("JS",  session::get('js'));
505     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
506       $smarty->assign("$val", $this->$val);
507     }
509     /* Values */
510     foreach(array("gotoBootKernel","gotoShare","FAIclasses","FAIclass","FAIdebianMirror","FAIrelease") as $val){
511       $smarty->assign($val, $this->$val);
512     }
514     $smarty->assign("fai_activated",$this->fai_activated);
516     /* Create FAI output */
517     $this->update_fai_cache();
518     $smarty->assign("si_fai_action_failed",$this->si_fai_action_failed);
519     $smarty->assign("si_active",$this->si_active);
520   
521     if(!$this->si_fai_action_failed && $this->si_active && $this->fai_activated){
523       $smarty->assign("FAIservers"  , $this->cache['SERVERS']);
524       $smarty->assign("FAIdebianMirror",$this->FAIdebianMirror);
525       $smarty->assign("FAIrelease"  , $this->FAIrelease);
526       $smarty->assign("FAIclasses"  , $this->selectable_classes());
528       /* Get classes for release from cache.
529        * Or build cache
530        */
531       if($this->FAIdebianMirror == "inherited"){
532         $release = $this->InheritedFAIrelease;
533       }else{
534         $release = $this->FAIrelease;
535       }
537       $smarty->assign("gotoBootKernels",$this->cache['KERNELS'][$release]);
538       $smarty->assign("InheritedFAIrelease",$this->InheritedFAIrelease);
540       /* Get classes */
541       if($this->FAIdebianMirror == "inherited"){
542         $tmp = $this->InheritedFAIclass;
543       }else{
544         $tmp = $this->FAIclass;
545       }
547       /* Get invalid classes */
548       $invalid = $this->get_invalid_classes($tmp);
549       $data = $lData = array();
551       /* Draw every single entry */
552       $i = 1;
553       if($this->acl_is_readable("FAIclass")){
554         foreach($tmp as $class){
556           /* Mark invalid classes. (Not in selected release)
557            */
558           $marker = "";
559           if(in_array_ics($class,$invalid)){
560             $marker = "&nbsp;<font color='red'>("._("Not available in current setup").")</font>";
561           }
563           $i ++ ; 
565           /* Get Description tag 
566            *  There may be several FAI objects with the same class name, 
567            *   use the description from FAIprofile, if possible.
568            */  
569           $desc = ""; 
571           $objects = array(
572               "Pl"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png"),
573               "Pt"     => array("IMG"=> "plugins/fai/images/fai_packages.png"),
574               "S"          => array("IMG"=> "plugins/fai/images/fai_script.png"),
575               "V"        => array("IMG"=> "plugins/fai/images/fai_variable.png"),
576               "H"            => array("IMG"=> "plugins/fai/images/fai_hook.png"),
577               "P"         => array("IMG"=> "plugins/fai/images/fai_profile.png"),
578               "T"        => array("IMG"=> "plugins/fai/images/fai_template.png"));
580           if(isset($this->cache['CLASSES'][$this->FAIrelease][$class])){
581             $desc ="";
582             foreach($this->cache['CLASSES'][$this->FAIrelease][$class] as $types ){
583               if(isset($types['Abbr'])){
584                 $desc.= image($objects[$types['Abbr']]['IMG']);
585                 if($types['Type'] == "FAIprofile"){
586                   break;
587                 }
588               }
589             }
590           }
591           if(!empty($desc)){
592             $desc = "&nbsp;".trim($desc)."";
593           }        
595           $data[$class] = $class;
596           $lData[$class] = array('data' => array($class,$desc));
597         }  
598       }// END FAI output generation 
599     }// END FAI output generation 
601     $this->faiList->setAcl('rwcdm');
602     $this->faiList->setReorderable($this->FAIdebianMirror != "inherited");
603     $this->faiList->setListData($data,$lData);
604     $this->faiList->update();
606     $smarty->assign("FAIScriptlist", $this->faiList->render());
608     /* Show main page */
609     $smarty->assign("gotoKernelParameters",$this->gotoKernelParameters);
610     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
611   }
614   function remove_from_parent()
615   {
616     $this->handle_post_events("remove");
617     new log("remove","workstation/".get_class($this),$this->dn);
618   }
621   /* Save data to object */
622   function save_object()
623   {
624     $old_mirror  = $this->FAIdebianMirror;
625     plugin::save_object();
627     /* Update release */
628     if($old_mirror != $this->FAIdebianMirror){
629       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
630         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
631       }
632     }
634     if(isset($_POST['WorkstationStarttabPosted'])){
635       if(isset($_POST['gotoLdap_inherit'])){
636         $this->gotoLdap_inherit = TRUE;
637       }else{
638         $this->gotoLdap_inherit = FALSE;
639       }
640     }
641   }
644   /* Save to LDAP */
645   function save()
646   {
648     /* Depending on the baseobject (Ogroup / WS) we
649      *  use another set of objectClasses
650      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
651      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
652      */
653     if(isset($this->parent->by_object['ogroup'])){
654       $this->objectclasses = array("gotoWorkstationTemplate");
655     }elseif(isset($this->parent->by_object['workgeneric'])){
656       $this->objectclasses = array("GOhard");
657     }elseif(isset($this->parent->by_object['servgeneric'])){
658       $this->objectclasses = array("GOhard","gotoWorkstationTemplate");
659     }else{
660       msg_dialog::display(_("Fatal error"),
661           "Object Type Configuration is unknown. Please contact the GOsa developers.",
662           FATAL_ERROR_DIALOG);
663       exit();
664     }
666     /* Append FAI class */
667     if($this->fai_activated){
668       $this->objectclasses[]  = "FAIobject";
669     }
671     /* Find proper terminal path for tftp configuration
672        FIXME: This is suboptimal when the default has changed to
673        another location! */
674     if (($this->gotoTerminalPath == "default")){
675       $ldap= $this->config->get_ldap_link();
677       /* Strip relevant part from dn, keep trailing ',' */
678       $tmp= preg_replace("/^cn=[^,]+,".preg_quote(get_ou('terminalRDN'), '/')."/i", "", $this->dn);
679       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
681       /* Walk from top to base and try to load default values for
682          'gotoTerminalPath'. Abort when an entry is found. */
683       while (TRUE){
684         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
686         $ldap->cat("cn=default,".get_ou('terminalRDN').$tmp.
687             $this->config->current['BASE'], array('gotoTerminalPath'));
688         $attrs= $ldap->fetch();
689         if (isset($attrs['gotoTerminalPath'])){
690           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
691           break;
692         }
694         /* Nothing left? */
695         if ($tmp == ""){
696           break;
697         }
698       }
699     }
701     plugin::save();
703     unset( $this->attrs['FAIrelease'] );
704     $str = "";
706     /* Skip FAI attribute handling if not necessary */
707     if($this->fai_activated && !$this->si_fai_action_failed){
708       if($this->FAIdebianMirror == "inherited"){
709         $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array();
710       }else{
711         foreach($this->FAIclass as $class){
712           $str .= $class." ";
713         }
714         $str = trim($str);
715         if(empty($this->attrs['FAIclass'])){
716           $this->attrs['FAIclass'] = array();
717         }else{
718           $this->attrs['FAIclass']= $str." :".$this->FAIrelease;
719         }
720       }
721     }
723     /* Add missing arrays */
724     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
725       if (isset ($this->$val) && count ($this->$val) != 0){
726     
727         $this->attrs["$val"]= array_unique($this->$val);
728       }
729       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
730     }
732     /* Prepare list of ldap servers */
733     $this->attrs['gotoLdapServer'] = array();
734     if(!$this->gotoLdap_inherit){
735       $i = 0;
736       foreach($this->gotoLdapServers as $server){
737         $i ++;
738         $this->attrs['gotoLdapServer'][] = $i.":".$server;
739       }
740     }
742     if ($this->attrs['gotoBootKernel'] == "default-inherited"){
743       $this->attrs['gotoBootKernel']= array();
744     }
746     /* if mirror == none stop saving this attribute */
747     if($this->FAIdebianMirror == "none"){
748       $this->FAIdebianMirror = "";
749     }
750    
751     /* Get FAIstate from object, the generic tab could have changed it during execute */
752     $ldap= $this->config->get_ldap_link();
753     $ldap->cd($this->dn);
756     /* Skip FAI attribute handling if not necessary */
757     if($this->fai_activated && !$this->si_fai_action_failed && $this->si_active){
758       $ldap->cat($this->dn,array("FAIstate"));
759       $checkFAIstate = $ldap->fetch();
761       /* Remove FAI objects if no FAI class is selected */ 
762       if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
763         $this->attrs['FAIclass']        = array();
764         $this->attrs['FAIdebianMirror'] = array();
765       }
766     }else{
768       /* Don't touch FAI objects if something went wrong with the si daemon.
769        */
770       if(isset($this->attrs['FAIclass'])) unset($this->attrs['FAIclass']);
771       if(isset($this->attrs['FAIdebianMirror'])) unset($this->attrs['FAIdebianMirror']);
772     }
774     /* prepare share settings */
775     $tmp = array();
776     foreach($this->gotoShares as $name => $settings){
777       $tmp2= explode("|",$name);
778       $name = $tmp2[0];
779       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
780     }
781     $this->attrs['gotoShare']=$tmp;
782     $this->cleanup();
783     $ldap->modify ($this->attrs); 
784     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
786     if (!$ldap->success()){
787       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
788     }
789     $this->handle_post_events("modify");
791     /* Check if LDAP server has changed */
792     if ($this->si_active && (isset($this->attrs['gotoLdapServer']) && class_available("DaemonEvent") || $this->gotoLdap_inherit)){
793       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
794       $o_queue = new gosaSupportDaemon();
795       if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
796         $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
797         $macs = array();
798     
799         /* Get list of macAddresses 
800          */
801         if(isset($this->parent->by_object['ogroup'])){
802         
803           /* If we are an object group, add all member macs 
804            */
805           $p = $this->parent->by_object['ogroup'];
806           foreach($p->memberList as $dn => $obj){
807             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
808               $macs[] = $p->objcache[$dn]['macAddress'];
809             }
810           }
811         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
813           /* We are a workstation. Add current mac.
814            */
815           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
816           if(!empty($mac)){
817             $macs[] = $mac;
818           }          
819         }elseif(isset($this->parent->by_object['servgeneric']->netConfigDNS->macAddress)){
821           /* We are a server. Add current mac.
822            */
823           $mac = $this->parent->by_object['servgeneric']->netConfigDNS->macAddress;
824           if(!empty($mac)){
825             $macs[] = $mac;
826           }          
827         }
829         /* Trigger event for all member objects 
830          */
831         foreach($macs as $mac){
832           $tmp = new $evt['CLASS_NAME']($this->config);
833           $tmp->set_type(TRIGGERED_EVENT);
834           $tmp->add_targets(array($mac));
835           if(!$o_queue->append($tmp)){
836             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
837           }
838         }
839       }
840     }
841   }
844   /* Add value to array, check if unique */
845   function add_list (&$array, $value)
846   {
847     if ($value != ""){
848       $array[]= $value;
849       sort($array);
850       array_unique ($array);
851     }
852   }
855   /* Delete value to array, check if unique */
856   function del_list (&$array, $list)
857   {
858     $tmp= array();
859     foreach ($array as $mod){
860       if (!in_array($mod, $list)){
861         $tmp[]= $mod;
862       }
863     }
864     $array= $tmp;
865   }
867   /* Generate ListBox frindly output for the defined shares
868    * Possibly Add or remove an attribute here,
869    */
870   function printOutAssignedShares()
871   {
872     $a_return = array();
873     if(is_array($this->gotoShares)){
874       foreach($this->gotoShares as $share){
875         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
876       }
877     }
878     return($a_return);
879   }
883   function PrepareForCopyPaste($source)
884   {
885     plugin::PrepareForCopyPaste($source);    
886     $source_o = new workstartup ($this->config, $source['dn']);
887     foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
888           "gotoKernelParameters","gotoShares","gotoLdapServers","gotoLdapServerList",
889           "gotoLdap_inherit") as $attr){
890       $this->$attr = $source_o->$attr;
891     }
892   }
894   
895   function array_switch_item($ar,$from,$to)
896   {
897     if(!is_array($ar)){
898       return(false);
899     }
900     if(!isset($ar[$from])){
901       return(false);
902     }
903     if(!isset($ar[$to])){
904       return(false);
905     }
907     $tmp = $ar[$from];
908     $ar[$from] = $ar[$to];    
909     $ar[$to] = $tmp;    
910     return($ar);
911   }
914   /* Return plugin informations for acl handling */ 
915   static function plInfo()
916   {
917     return (array( 
918           "plShortName"   => _("Startup"),
919           "plDescription" => _("System startup"),
920           "plSelfModify"  => FALSE,
921           "plDepends"     => array(),
922           "plPriority"    => 9,
923           "plSection"     => array("administration"),           
924           "plCategory"    => array("workstation","server","ogroups"),
926           "plProvidedAcls"=> array(
927             "gotoLdapServer"        => _("Ldap server"),
928             "gotoBootKernel"        => _("Boot kernel"),
929             "gotoKernelParameters"  => _("Kernel parameter"),
931             "gotoModules"           => _("Kernel modules"),
932             "gotoShare"             => _("Shares"),
934             "FAIclass"              => _("FAI classes"),
935             "FAIdebianMirror"       => _("Debian mirror"),
936             "FAIrelease"            => _("Debian release"),
938             "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
939           ));
940   }
943   /* Updates release dns 
944    *  and reads all classes for the current release, 
945    *  if not already done ($this->cache).
946    */
947   function update_fai_cache($first_call = FALSE)
948   {
949     $force = FALSE;
950     if(!$this->si_active) return; 
951     $start = microtime(TRUE);  
953     if($this->si_fai_action_failed && !isset($_POST['fai_si_retry'])) return;
955     $this->si_fai_action_failed = FALSE;
957     /* Get the list of available servers and their releases. 
958      */
959     if($force || !isset($this->cache['SERVERS'])){
961       $o_queue = new gosaSupportDaemon();
962       $tmp = $o_queue->FAI_get_server();
963       if($o_queue->is_error()){
964         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
965         $this->si_fai_action_failed = TRUE;
966         $this->cache = array();
967         return;
968       }else{
970         foreach($tmp as $entry){
971           $rel = $entry['FAI_RELEASE'];
972           $this->cache['SERVERS']['auto'][$rel] = $rel;
973           $this->cache['SERVERS'][$entry['SERVER']][$rel] = $rel;
974           uksort($this->cache['SERVERS']['auto'], 'strnatcasecmp');
975           uksort($this->cache['SERVERS'][$entry['SERVER']], 'strnatcasecmp');
976         }
977       }
978     }
980     /* Ensure that our selection is valid, else we get several PHP warnings 
981         if there is no FAI configuration at all.
982      */
983     if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
984       $this->cache['SERVERS'][$this->FAIdebianMirror][''] ='';
985     }
987     /* Build up arrays, without checks */
988     if(!$first_call){
990       /* Check if the selected mirror is available */
991       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
992         $this->FAIdebianMirror = "auto";
993         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
994         trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
995       }
997       /* Check if the selected release is available */
998       if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
1000         if($this->FAIrelease != ""){
1001           trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1002         }
1003         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1004       }
1005     }
1007     /* Get classes for release from cache. 
1008      * Or build cache
1009      */
1010     if($this->FAIdebianMirror == "inherited"){
1011       $release = $this->InheritedFAIrelease;
1012     }else{
1013       $release = $this->FAIrelease;
1014     }
1016     if($force || !isset($this->cache['CLASSES'][$release]) && $release != ""){
1018       /* Get the list of available servers and their releases.
1019        */
1020       $o_queue = new gosaSupportDaemon();
1021       $tmp = $o_queue->FAI_get_classes($release);
1023       $this->cache['CLASSES'][$release] = array();
1024       if($o_queue->is_error()){
1025         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1026         $this->si_fai_action_failed = TRUE;
1027         $this->cache=array();
1028         return;
1029       }else{
1030         foreach($tmp as $entry){
1031           $class = $entry['CLASS'];
1032           $this->cache['CLASSES'][$release][$class][] = $this->analyse_fai_object($entry); 
1033         }
1034       }
1036       /* Add object caught from external hook
1037        */
1038       $lines= $this->GetHookElements();
1039       foreach ($lines as $hline){
1040         $entries= explode(";", $hline);
1041         $server = $entries['0'];
1042         $url    = $entries['1'];
1043         if (!empty($url)){
1045           /* Split releases */
1046           if (isset($entries[2])){
1047             $releases= explode(",", $entries[2]);
1049             foreach ($releases as $release_data){
1050               $release_c  = preg_replace('/:.*$/', '', $release_data);
1051               $sections_c = explode(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1052               $classes_c  = explode('|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1054               if($release_c == $release){
1055                 $this->cache['SERVERS'][$url][$release_c]=$release_c;
1056                 $this->cache['SERVERS']['auto'][$release_c]=$release_c; 
1057                 foreach ($classes_c as $class){
1058                   if ($class != ""){
1059                     $this->cache['CLASSES'][$release_c][$class]= array();
1060                   }
1061                 }
1062               }
1063             }
1064           }
1065         }
1066       }
1067       uksort($this->cache['SERVERS'], 'strnatcasecmp');
1069       /* Only add inherit option, if we are part in an object group
1070        */
1071       if($this->member_of_ogroup){
1072         $this->cache['SERVERS'] = array_merge(array('inherited' => array()),$this->cache['SERVERS']);
1073       }
1074     }
1076     /* Get list of available kernel for this release 
1077      */
1078     if(!isset($this->cache['KERNELS'])) $this->cache['KERNELS'] = array();
1080     if($force || !isset($this->cache['KERNELS'][$release])){
1081       $o_queue = new gosaSupportDaemon();
1082       $tmp = $o_queue->FAI_get_kernels($release);
1083       $this->cache['KERNELS'][$release] = array();
1084       foreach($this->gotoBootKernels as $name => $default){
1085         $this->cache['KERNELS'][$release][$name] = $default;
1086       }
1087       foreach($tmp as $kernel){
1088         if(empty($kernel)) continue;
1089         $this->cache['KERNELS'][$release][$kernel]=$kernel;
1090       }
1091       ksort($this->cache['KERNELS'][$release]);
1092     }
1093   }
1096   /* This function return an array containing all 
1097    *  invalid classes for the selected server/release
1098    */
1099   function get_invalid_classes($classes)
1100   {
1101     $this->update_fai_cache();
1102     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1103       $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1104     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1105       $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1106     }else{
1107       $release_classes = array();
1108     }
1111     /* Detect all classes that are not valid 
1112      *  for the selected release 
1113      */
1114     $NA = array();
1115     foreach($classes as $class){
1116       if(!isset($release_classes[$class])){
1117         $NA[] = $class;
1118       }
1119     }
1120     return($NA);
1121   }  
1123   
1124   /* Get all selectable classes for the ui select box
1125    */
1126   function selectable_classes()
1127   {
1128     $this->update_fai_cache();
1130     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1131       $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1132     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1133       $classes = $this->cache['CLASSES'][$this->FAIrelease];
1134     }else{
1135       $classes = array();
1136     }
1138     $Abbr ="";
1139     $ret= array();
1140     foreach($classes as $class_name => $class_types){
1141       if(!in_array($class_name,$this->FAIclass)){
1142         $Abbr = "";
1143         foreach($class_types as $type){
1144           if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1145             $Abbr .= $type['Abbr']." ";
1146           }
1147         }
1148         $ret[$class_name] = trim($Abbr);
1149       }
1150     }
1151     uksort($ret, 'strnatcasecmp');
1152     return($ret);
1153   }
1156   /* Analyse FAI object and return an array with usefull informations like 
1157    *  FAIobject type.
1158    */
1159   function analyse_fai_object($attr)
1160   {
1161     $tmp = array();
1162     switch($attr['TYPE']){
1164       case 'FAIpackageList':
1165         $tmp["Type"]= 'FAIpackageList';
1166         $tmp["Abbr"]= 'Pl';
1167         break;
1168       case 'FAItemplate': 
1169         $tmp["Type"]= 'FAItemplate'; 
1170         $tmp["Abbr"]= 'T'; 
1171         break;
1172       case 'FAIvariable':
1173         $tmp["Type"]= 'FAIvariable'; 
1174         $tmp["Abbr"]= 'V'; 
1175         break;
1176       case 'FAIscript':
1177         $tmp["Type"]= 'FAIscript'; 
1178         $tmp["Abbr"]= 'S'; 
1179         break;
1180       case 'FAIhook':
1181         $tmp["Type"]= 'FAIhook'; 
1182         $tmp["Abbr"]= 'H'; 
1183         break;
1184       case 'FAIpartitionTable':
1185         $tmp["Type"]= 'FAIpartitionTable'; 
1186         $tmp["Abbr"]= 'Pt'; 
1187         break;
1188       case 'FAIprofile':
1189         $tmp["Type"]= 'FAIprofile'; 
1190         $tmp["Abbr"]= 'P'; 
1191         break;
1192       default: trigger_error("Unknown FAI object type!");;
1193     }
1194     return($tmp);
1195   }
1198   /* Return repository hook output, if possible.
1199    */
1200   function GetHookElements()
1201   {
1202     $ret = array();
1203     $cmd= $this->config->search("servrepository", "repositoryBranchHook",array('tabs'));
1204     if(!empty($cmd)){
1205       $res = shell_exec($cmd);
1206       $res2 = trim($res);
1207       if((!$res)){
1208         msg_dialog::display(_("Configuration error"), msgPool::cmdexecfailed("repositoryBranchHook", $cmd), ERROR_DIALOG);
1209       }elseif(empty($res2)){
1210         msg_dialog::display(_("Configuration error"), _("'repositoryBranchHook' returned no result!"), ERROR_DIALOG);
1211       }else{
1212         $tmp = preg_split("/\n/",$res);
1213         foreach($tmp as $line){
1214           if(empty($line)) continue;
1215           $ret[]= $line;
1216         }
1217       }
1218     }
1219     return($ret);
1220   }
1223   /* This function creates the release name out of a dn 
1224    *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1225    */
1226   function dn_to_release_name($dn)
1227   {
1228     $relevant = preg_replace("/,".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$dn);
1229     $parts    = array_reverse(explode(",",$relevant));
1230     $str ="";
1231     foreach($parts as $part){
1232       $str .= preg_replace("/^ou=/","",$part)."/";
1233     }
1234     return(preg_replace("/\/$/","",$str)); 
1235   }
1238 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1239 ?>