Code

Removed old style list
[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 $bootmode             = "G";
12   var $bootmode             = "";
13   var $gotoBootKernel       = "default-inherited";
14   var $gotoKernelParameters = "";
15   var $gotoLdapServer       = "default-inherited";
16   var $gotoModules          = array();
17   var $gotoAutoFs           = array();
18   var $gotoFilesystem       = array();
19   var $gotoTerminalPath     = "";
20   var $gotoBootKernels      = array();
22   /* attribute list for save action */
23   var $attributes           = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", 
24                                     "FAIclass", "FAIstatus", "gotoShare","FAIdebianMirror", "FAIrelease");
25   var $objectclasses        = array("GOhard", "FAIobject");
27   /* Share */
28   var $gotoShares         = array();// Currently Share Option
29   var $gotoShare          = "";     // currently selected Share Option
30   var $gotoShareSelections= array();// Available Shares for this account in Listbox format
31   var $gotoAvailableShares= array();// Available Shares for this account
33   /* Helper */
34   var $orig_dn            = "";
35   var $ignore_account     = TRUE;
36  
37   /* FAI class selection */ 
38   var $FAIclass           = array();  // The currently selected classes 
39   var $FAIrelease           = "";
40   var $FAIdebianMirror      = "auto";
41   var $si_active            = FALSE;
42   var $si_fai_action_failed = FALSE;
44   var $cache              = array(); // Used as cache in fai mehtods
46   var $FAIstatus          = "";
47   var $FAIclasses         = array();
49   var $view_logged        = FALSE;
50   
51   /* FAI class selection */
52   var $InheritedFAIclass       = array();
53   var $InheritedFAIrelease     = "";
54   var $InheritedFAIdebianMirror= "auto";
56   var $CopyPasteVars    = array("gotoModules","gotoShares");
57   var $fai_activated    = FALSE;
58   var $o_group_dn       = "";
59   var $member_of_ogroup = FALSE;
61   function workstartup (&$config, $dn= NULL, $parent= NULL)
62   {
63     /* Check if FAI is active */
64     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
65     if(!empty($tmp) && class_available("faiManagement")){
66       $this->fai_activated = TRUE;
67     }else{
68       $this->attributes = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", "gotoShare");
69       $this->objectclasses  = array("GOhard");
70     }
72     plugin::plugin ($config, $dn, $parent);
74     /* Check for si daemon */
75     $this->si_active = $this->config->get_cfg_value("gosaSupportURI") != "";
77     /* Check object group membership */
78     if(!isset($this->parent->by_object['ogroup'])){
79       $ldap = $this->config->get_ldap_link();
80       $ldap->cd ($this->config->current['BASE']);
81       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn","dn"));
82       if($ldap->count()){
83         $this->member_of_ogroup = TRUE;
84         $attrs = $ldap->fetch();
85         $this->o_group_dn = $attrs['dn'];
86       }
87     }
89     /* Creating a list of valid Mirrors 
90      * none will not be saved to ldap.
91      */
92     $ldap   = $this->config->get_ldap_link();
93     $ldap->cd($this->config->current['BASE']);
94     $ui = get_userinfo();
95     foreach($this->config->data['SERVERS']['LDAP'] as $dn => $data){
96       if($ui->get_category_permissions($data['dn'],"server",TRUE)){
97         for($i = 0; $i < $data['goLdapBase']['count']; $i ++){
98           $name = $data["cn"][0].":".$data["goLdapBase"][$i];
99           $this->gotoLdapServerList[]= $name; 
100         }
101       }
102     }
104     /* Get list of assigned ldap servers 
105      */ 
106     if(isset($this->attrs['gotoLdapServer'])){
107       unset($this->attrs['gotoLdapServer']['count']);
108       sort($this->attrs['gotoLdapServer']);
109       foreach($this->attrs['gotoLdapServer'] as $value){
110         $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
111       }
112     } 
113     natcasesort($this->gotoLdapServerList);
115     if(!count($this->gotoLdapServers) && $this->member_of_ogroup){ 
116       $this->gotoLdap_inherit = TRUE;
117     }
119     /* FAI Initialization
120        Skip this if FAI is not activated 
121      */
122     if($this->fai_activated) {
124       /* Parse used FAIclasses (stored as string).
125        * The single classes are seperated by ' '.
126        * There is also the release type given, after first
127        *  occurrence of ':'.
128        */
129       $this->FAIclass =array();
130       if(isset($this->attrs['FAIclass'][0])){
131         $tmp = explode(" ",$this->attrs['FAIclass'][0]);
132         $tmp2 =array();  
134         foreach($tmp as $class){
135           if( ":" == $class[0] ) {
136             $this->FAIrelease = trim(substr($class, 1));
137           }else{
138             $tmp2[$class] = $class;
139           }
140         }
141         $this->FAIclass = $tmp2;
142       }
143     }
145     /* Get arrays */
146     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
147       if (isset($this->attrs["$val"]["count"])){
148         for ($i= 0; $i<$this->attrs["count"]; $i++){
149           if (isset($this->attrs["$val"][$i])){
150             array_push($this->$val, $this->attrs["$val"][$i]);
151           }
152         }
153       }
154       sort ($this->$val);
155       $this->$val= array_unique($this->$val);
156     }
158     /* Prepare Shares */
159     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
160       unset($this->attrs['gotoShare']['count']);
161       foreach($this->attrs['gotoShare'] as $share){
162         $tmp = $tmp2 = array();
163         $tmp = explode("|",$share);
164         $tmp2['server']      =$tmp[0];
165         $tmp2['name']        =$tmp[1];
166         $tmp2['mountPoint']  =$tmp[2];
167         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
168       }
169     }
171     $this->gotoShareSelections= $config->getShareList(true);
172     $this->gotoAvailableShares= $config->getShareList(false);
173     $tmp2 = array();
174   
176     $this->orig_dn= $this->dn;
178     /* Handle inheritance value "default" */
179     if ($this->member_of_ogroup){
180       $this->gotoBootKernels= array("default-inherited" => '['._("inherited").']'); 
181     }
183     /* If we are member in an object group,
184      *  we have to handle inherited values.
185      * So you can see what is inherited.
186      */
187     if ($this->member_of_ogroup){
189       if(count($this->FAIclass)==0 && $this->FAIrelease == ""){
190         $this->FAIdebianMirror = "inherited";
191       }
193       if($this->fai_activated){
194         $map= array("gotoBootKernel","FAIclass","FAIdebianMirror");
195       }else{
196         $map= array("gotoBootKernel");
197       }
199       $ldap = $this->config->get_ldap_link();
200       $ldap->cat($this->o_group_dn);
201       $attrs= $ldap->fetch();
203       foreach ($map as $name){
204         if (!isset($attrs[$name][0])){
205           continue;
206         }
208         switch ($name){
209           case 'gotoBootKernel':
210             $this->gotoBootKernels['default-inherited']=  _("inherited").' ['.$attrs[$name][0].']' ;
211             break;
213           case 'FAIclass':
214             $str = explode(":",$attrs[$name][0]);
215             $this->InheritedFAIclass    = explode(" ",trim($str[0]));
216             $this->InheritedFAIrelease  = trim($str[1]);
217             break;
219           case 'FAIdebianMirror':
220             $this->InheritedFAIdebianMirror = $attrs[$name][0];
221             break;
222         }
223       }
224     }
226     $this->update_fai_cache();
228     if($this->fai_activated && !$this->si_fai_action_failed && $this->si_active){
230       /* Check if the current mirror is available 
231        */
232       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
233         if(count($this->FAIclass)){
234           msg_dialog::display(_("Error"), sprintf(_("FAI mirror '%s' is not available - setting to mirror 'auto'!"), $this->FAIdebianMirror), ERROR_DIALOG);
235         }
236         $this->FAIdebianMirror = "auto";
237         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
238         $this->cache['CLASSES'] = array();
239         $this->update_fai_cache();
240       }
241   
242       /* Check if the current mirror is available 
243        */
244       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
245         $new_release = key($this->cache['SERVERS'][$this->FAIdebianMirror]); 
246         if(count($this->FAIclass)){
247           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);
248         }
249         $this->FAIrelease = $new_release;
250         $this->cache['CLASSES'] = array();
251         $this->update_fai_cache();
252       }
253     }
256     // Prepare lists
257     $this->ldapList = new sortableListing(array(),array(), TRUE);
258     $this->ldapList->setDeleteable(true);
259     $this->ldapList->setEditable(false);
260     $this->ldapList->setWidth("100%");
261     $this->ldapList->setHeight("100px");
262     $this->ldapList->setHeader(array(_("LDAP server")));
263     $this->ldapList->setDefaultSortColumn(0);
264   }
266   
267   function check()
268   {
269     $messages = array();
270     
271     /* Call common method to give check the hook */
272     $messages= plugin::check();
274     /* If there are packages selected, but no mirror show error */   
275     if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
276       $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
277     }
279     return($messages);
280   }
282   function execute()
283   {
284         /* Call parent execute */
285         plugin::execute();
287     if($this->is_account && !$this->view_logged){
288       $this->view_logged = TRUE;
289       new log("view","workstation/".get_class($this),$this->dn);
290     }
292     /* Do we represent a valid terminal? */
293     if (!$this->is_account && $this->parent === NULL){
294       $display= image("images/small-error.png")."<b>".msgPool::noValidExtension(_("workstation"))."</b>";
295       return ($display);
296     }
298     /* Add module */
299     if (isset ($_POST['add_module'])){
300       if ($_POST['module'] != "" && $this->acl_is_writeable("gotoModules")){
301         $this->add_list ($this->gotoModules, $_POST['module']);
302       }
303     }
305     /* Delete module */
306     if (isset ($_POST['delete_module'])){
307       if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoModules")){
308         $this->del_list ($this->gotoModules, $_POST['modules_list']);
309       }
310     }
312     /* FAI class management */
313     if($this->fai_activated){
314       if(((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))) && ($this->acl_is_writeable("FAIclass"))){
315         $found = 0 ; 
317         /* If this new class/profile will attach a second partition table
318          * to our list of classes, abort and show a message.
319          */
320         foreach($this->FAIclass as $name){
321           if(isset($this->FAIclassInfo[$name])){
322             foreach($this->FAIclassInfo[$name] as $atr){
323               if(isset($atr['obj'])){
324                 if($atr['obj'] == "FAIpartitionTable"){
325                   $found ++ ; 
326                 }
327               }
328             }
329           }
330         }
332         if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpartitionTable']))&&($found>0)){
333           msg_dialog::display(_("Error"), _("There is already a profile containing a partition table in your configuration!") , ERROR_DIALOG);
334         }else{
335           $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
336         }
337       }
339       $sort = false;
341       /* Move one used class class one position up or down */
342       if($this->acl_is_writeable("FAIclass")){
343         foreach($_POST as $name => $val){
345           $sort_type = false;
346           if((preg_match("/sort_up/",$name))&&(!$sort)){
347             $sort_type = "sort_up_";
348           }
349           if((preg_match("/sort_down/",$name))&&(!$sort)){
350             $sort_type = "sort_down_";
351           }
353           if(($sort_type)&&(!$sort)){
354             $value = postDecode(preg_replace("/_.*$/i","",preg_replace("/".$sort_type."/i","",$name)));
355             $sort = true;
357             $last = -1;
358             $change_down  = -1;
360             /* Create array with numeric index */ 
361             $tmp = array();
362             foreach($this->FAIclass as $class){
363               $tmp [] = $class;
364             }
366             /* Walk trough array */
367             foreach($tmp as $key => $faiName){
368               if($faiName == $value){
369                 if($sort_type == "sort_up_"){
370                   if($last != -1){
371                     $change_down= $last;
372                   }
373                 }else{
374                   if(isset($tmp[$key+1])){
375                     $change_down = $key;
376                   }
377                 }
378               }
379               $last = $key;
380             }
382             $tmp2 = array();
383             $skip = false;    
385             foreach($tmp as $ky => $vl){
387               if($ky == $change_down){
388                 $skip = $vl;
389               }else{
390                 $tmp2[$vl] = $vl;
391               }
392               if(($skip != false)&&($ky != $change_down)){
393                 $tmp2[$skip]  = $skip;
394                 $skip =false;
395               }
396             }   
397             $this->FAIclass = $tmp2; 
398           }
400           if(preg_match("/fai_remove/i",$name)){
401             $value = postDecode(preg_replace("/fai_remove_/i","",$name));
402             unset($this->FAIclass[$value]);
403           }
404         }
405       }
407       /* Delete selected class from our list */
408       if($this->acl_is_writeable("FAIclass")){
409         if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
410           if(isset($this->FAIclass[$_POST['FAIclassSel']])){
411             unset($this->FAIclass[$_POST['FAIclassSel']]);
412           }
413         }
414       }
415     }// END fai handling
417     /* Show main page */
418     $smarty= get_smarty();
420     /* Assign ACLs to smarty */
421     $tmp = $this->plInfo();
422     foreach($tmp['plProvidedAcls'] as $name => $translation){
423       $smarty->assign($name."ACL",$this->getacl($name));
424     } 
426     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
428     /* In this section server shares will be defined
429      * A user can select one of the given shares and a mount point
430      *  and attach this combination to his setup.
431      */
432     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
433     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
435     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
436      * This entry will be, a combination of mountPoint and sharedefinitions
437      */
438     if((isset($_POST['gotoShareAdd'])) && isset($_POST['gotoShareSelection']) && ($this->acl_is_writeable("gotoShare"))) {
439       /* We assign a share to this user, if we don't know where to mount the share */
440       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
441         msg_dialog::display(_("Error"), msgPool::required(_("Mount point")), ERROR_DIALOG);
442       }else{
443     
444         if(isset($this->gotoAvailableShares[$_POST['gotoShareSelection']])){
445           $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
446           $s_mount = $_POST['gotoShareMountPoint'];
447           /* Preparing the new assignment */
448           $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
449           $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
450         }
451       }
452     }
454     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
455      * If there is no defined share selected, we will abort the deletion without any message
456      */
457     if(($this->acl_is_writeable("gotoShare"))&& (isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
458       unset($this->gotoShares[$_POST['gotoShare']]);
459     }
461     $smarty->assign("gotoShares",$this->printOutAssignedShares());
462     $smarty->assign("gotoSharesCount",count($this->printOutAssignedShares()));
463     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
464     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
466     $this->ldapList->setAcl($this->getacl('gotoLdapServer'));
468     /* Add new ldap server to the list */
469     if($this->acl_is_writeable("gotoLdapServer") && 
470         !$this->gotoLdap_inherit && 
471         isset($_POST['add_ldap_server']) && 
472         isset($_POST['ldap_server_to_add'])){
473       if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
474         $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
475         if(!in_array($to_add,$this->gotoLdapServers)){
476           $this->gotoLdapServers[] = $to_add;
477         }
478       }
479     }
480     
481     /* Move ldap servers up and down */
482     $this->ldapList->save_object();
483     $action = $this->ldapList->getAction();
484     if($action['action'] == 'reorder'){
485         $this->gotoLdapServers = array_values($this->ldapList->getMaintainedData());
486     }
487     if($action['action'] == 'delete'){
488         $id = $this->ldapList->getKey($action['targets'][0]);
489         $value = $this->gotoLdapServers[$id];
490         $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
491     }
493     /* Add Entries */
494     $data = $lData = array();
495     foreach($this->gotoLdapServers as $key => $server){
496         $data[$key]=$server;
498         /* Announce missing entries */
499         if(!in_array($server,$this->gotoLdapServerList)){
500             $server = $server."&nbsp;<font style='color:red'>(missing)</font>";
501         }
503         /* Convert old style entry */
504         if (!preg_match('%:ldaps?://%', $server)){
505             $server= "ldap://".preg_replace('/^([^:]+):/', '\1/', $server);
506         } else {
507             $server= preg_replace("/^[^:]+:/", "", $server);
508             $lData[$key] = array('data'=>array($server));
509         }    
510     }
511     $this->ldapList->setListData($data,$lData);
512     $this->ldapList->update();
513     $smarty->assign("usePrototype", "true");
515     if($this->gotoLdap_inherit){
516       $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
517     }else{
518       $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
519     }
520     
521     $list = array();
522     foreach($this->gotoLdapServerList as $key => $entry){
523       if(!in_array($entry,$this->gotoLdapServers)){
525         /* Convert old style entry */
526         if (!preg_match('%:ldap[s]*://%', $entry)){
527           $entry= "ldap://".preg_replace('/^([^:]+):/', '\1/', $entry);
529         /* Beautify new style entries */
530         } else {
531           $entry= preg_replace("/^[^:]+:/", "", $entry);
532         }
534         $list[$key] = $entry;
535       }
536     }
537     $smarty->assign("gotoLdapServers",    $this->ldapList->render());
538     $smarty->assign("gotoLdapServerList", $list);
539     $smarty->assign("gotoLdap_inherit",   $this->gotoLdap_inherit);
540     $smarty->assign("JS",  session::get('js'));
542     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
543       $smarty->assign("$val", $this->$val);
544     }
546     /* Values */
547     foreach(array("gotoBootKernel","gotoShare","FAIclasses","FAIclass","FAIdebianMirror","FAIrelease") as $val){
548       $smarty->assign($val, $this->$val);
549     }
551     $smarty->assign("fai_activated",$this->fai_activated);
553     /* Create FAI output */
554     $this->update_fai_cache();
555     $smarty->assign("si_fai_action_failed",$this->si_fai_action_failed);
556     $smarty->assign("si_active",$this->si_active);
558     $div = new divSelectBox("WSFAIscriptClasses");
559     $div -> SetHeight("100");
560   
561     if(!$this->si_fai_action_failed && $this->si_active && $this->fai_activated){
563       $smarty->assign("FAIservers"  , $this->cache['SERVERS']);
564       $smarty->assign("FAIdebianMirror",$this->FAIdebianMirror);
565       $smarty->assign("FAIrelease"  , $this->FAIrelease);
566       $smarty->assign("FAIclasses"  , $this->selectable_classes());
568       /* Get classes for release from cache.
569        * Or build cache
570        */
571       if($this->FAIdebianMirror == "inherited"){
572         $release = $this->InheritedFAIrelease;
573       }else{
574         $release = $this->FAIrelease;
575       }
577       $smarty->assign("gotoBootKernels",$this->cache['KERNELS'][$release]);
578       $smarty->assign("InheritedFAIrelease",$this->InheritedFAIrelease);
580       $str_empty  = image('images/empty.png');
581       if($this->acl_is_writeable("FAIclass")){
582         $str_up     = image('images/lists/sort-up.png','sort_up_%s');
583         $str_down   = image('images/lists/sort-down.png','sort_down_%s');
584         $str_remove = image('images/lists/trash.png','fai_remove_%s');
585       }else{
586         $str_up=$str_down=$str_remove=$str_empty;
587       }
589       /* Get classes */
590       if($this->FAIdebianMirror == "inherited"){
591         $tmp = $this->InheritedFAIclass;
592       }else{
593         $tmp = $this->FAIclass;
594       }
596       /* Get invalid classes */
597       $invalid = $this->get_invalid_classes($tmp);
599       /* Draw every single entry */
600       $i = 1;
601       if($this->acl_is_readable("FAIclass")){
602         foreach($tmp as $class){
604           /* Mark invalid classes. (Not in selected release)
605            */
606           $marker = "";
607           if(in_array_ics($class,$invalid)){
608             $marker = "&nbsp;<font color='red'>("._("Not available in current setup").")</font>";
609           }
611           /* Create up/down priority icons  
612            * Skip this, if we have inherited the FAI classes.
613            */
614           if($this->FAIdebianMirror == "inherited"){
615             $str = "";
616           }else{
617             if($i==1){
618               $str = $str_empty.$str_down.$str_remove;
619             }elseif($i == count($this->FAIclass)){
620               $str = $str_up.$str_empty.$str_remove;
621             }else{
622               $str = $str_up.$str_down.$str_remove;
623             }
624           }
625           $i ++ ; 
627           /* Get Description tag 
628            *  There may be several FAI objects with the same class name, 
629            *   use the description from FAIprofile, if possible.
630            */  
631           $desc = ""; 
633           $objects = array(
634               "Pl"  => array("IMG"=> "plugins/fai/images/fai_partitionTable.png"),
635               "Pt"     => array("IMG"=> "plugins/fai/images/fai_packages.png"),
636               "S"          => array("IMG"=> "plugins/fai/images/fai_script.png"),
637               "V"        => array("IMG"=> "plugins/fai/images/fai_variable.png"),
638               "H"            => array("IMG"=> "plugins/fai/images/fai_hook.png"),
639               "P"         => array("IMG"=> "plugins/fai/images/fai_profile.png"),
640               "T"        => array("IMG"=> "plugins/fai/images/fai_template.png"));
642           if(isset($this->cache['CLASSES'][$this->FAIrelease][$class])){
643             $desc ="";
644             foreach($this->cache['CLASSES'][$this->FAIrelease][$class] as $types ){
645               if(isset($types['Abbr'])){
646                 $desc.= image($objects[$types['Abbr']]['IMG']);
647                 if($types['Type'] == "FAIprofile"){
648                   break;
649                 }
650               }
651             }
652           }
653           if(!empty($desc)){
654             $desc = "&nbsp;".trim($desc)."";
655           }        
657           $div->AddEntry(array(
658                 array("string"=>$class.$marker),
659                 array("string"=>$desc),
660                 array("string"=>preg_replace("/\%s/",postEncode($class),$str),"attach"=>"style='width:60px;border-right:none;'")
661                 ));
662         }  
663       }// END FAI output generation 
664     }// END FAI output generation 
665     $smarty->assign("FAIScriptlist",$div->DrawList()); 
667     /* Radio button group */
668     if (preg_match("/G/", $this->bootmode)) {
669       $smarty->assign("graphicalbootup", "checked");
670     } else {
671       $smarty->assign("graphicalbootup", "");
672     }
673     if (preg_match("/T/", $this->bootmode)) {
674       $smarty->assign("textbootup", "checked");
675     } else {
676       $smarty->assign("textbootup", "");
677     }
678     if (preg_match("/D/", $this->bootmode)) {
679       $smarty->assign("debugbootup", "checked");
680     } else {
681       $smarty->assign("debugbootup", "");
682     }
684     /* Show main page */
685     $smarty->assign("gotoKernelParameters",$this->gotoKernelParameters);
686     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
687   }
690   function remove_from_parent()
691   {
692     $this->handle_post_events("remove");
693     new log("remove","workstation/".get_class($this),$this->dn);
694   }
697   /* Save data to object */
698   function save_object()
699   {
700     $old_mirror  = $this->FAIdebianMirror;
701     plugin::save_object();
703     /* Update release */
704     if($old_mirror != $this->FAIdebianMirror){
705       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
706         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
707       }
708     }
710     if(isset($_POST['WorkstationStarttabPosted'])){
711       if(isset($_POST['gotoLdap_inherit'])){
712         $this->gotoLdap_inherit = TRUE;
713       }else{
714         $this->gotoLdap_inherit = FALSE;
715       }
717       /* Save group radio buttons */
718       if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
719         $this->bootmode= $_POST["bootmode"];
720       }
721     }
722   }
725   /* Save to LDAP */
726   function save()
727   {
729     /* Depending on the baseobject (Ogroup / WS) we
730      *  use another set of objectClasses
731      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
732      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
733      */
734     if(isset($this->parent->by_object['ogroup'])){
735       $this->objectclasses = array("gotoWorkstationTemplate");
736     }elseif(isset($this->parent->by_object['workgeneric'])){
737       $this->objectclasses = array("GOhard");
738     }elseif(isset($this->parent->by_object['servgeneric'])){
739       $this->objectclasses = array("GOhard","gotoWorkstationTemplate");
740     }else{
741       msg_dialog::display(_("Fatal error"),
742           "Object Type Configuration is unknown. Please contact the GOsa developers.",
743           FATAL_ERROR_DIALOG);
744       exit();
745     }
747     /* Append FAI class */
748     if($this->fai_activated){
749       $this->objectclasses[]  = "FAIobject";
750     }
752     /* Find proper terminal path for tftp configuration
753        FIXME: This is suboptimal when the default has changed to
754        another location! */
755     if (($this->gotoTerminalPath == "default")){
756       $ldap= $this->config->get_ldap_link();
758       /* Strip relevant part from dn, keep trailing ',' */
759       $tmp= preg_replace("/^cn=[^,]+,".preg_quote(get_ou('terminalRDN'), '/')."/i", "", $this->dn);
760       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
762       /* Walk from top to base and try to load default values for
763          'gotoTerminalPath'. Abort when an entry is found. */
764       while (TRUE){
765         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
767         $ldap->cat("cn=default,".get_ou('terminalRDN').$tmp.
768             $this->config->current['BASE'], array('gotoTerminalPath'));
769         $attrs= $ldap->fetch();
770         if (isset($attrs['gotoTerminalPath'])){
771           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
772           break;
773         }
775         /* Nothing left? */
776         if ($tmp == ""){
777           break;
778         }
779       }
780     }
782     plugin::save();
784     unset( $this->attrs['FAIrelease'] );
785     $str = "";
787     /* Skip FAI attribute handling if not necessary */
788     if($this->fai_activated && !$this->si_fai_action_failed){
789       if($this->FAIdebianMirror == "inherited"){
790         $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array();
791       }else{
792         foreach($this->FAIclass as $class){
793           $str .= $class." ";
794         }
795         $str = trim($str);
796         if(empty($this->attrs['FAIclass'])){
797           $this->attrs['FAIclass'] = array();
798         }else{
799           $this->attrs['FAIclass']= $str." :".$this->FAIrelease;
800         }
801       }
802     }
804     /* Add missing arrays */
805     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
806       if (isset ($this->$val) && count ($this->$val) != 0){
807     
808         $this->attrs["$val"]= array_unique($this->$val);
809       }
810       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
811     }
813     /* Prepare list of ldap servers */
814     $this->attrs['gotoLdapServer'] = array();
815     if(!$this->gotoLdap_inherit){
816       $i = 0;
817       foreach($this->gotoLdapServers as $server){
818         $i ++;
819         $this->attrs['gotoLdapServer'][] = $i.":".$server;
820       }
821     }
823     if ($this->attrs['gotoBootKernel'] == "default-inherited"){
824       $this->attrs['gotoBootKernel']= array();
825     }
827     /* if mirror == none stop saving this attribute */
828     if($this->FAIdebianMirror == "none"){
829       $this->FAIdebianMirror = "";
830     }
831    
832     /* Get FAIstate from object, the generic tab could have changed it during execute */
833     $ldap= $this->config->get_ldap_link();
834     $ldap->cd($this->dn);
837     /* Skip FAI attribute handling if not necessary */
838     if($this->fai_activated && !$this->si_fai_action_failed && $this->si_active){
839       $ldap->cat($this->dn,array("FAIstate"));
840       $checkFAIstate = $ldap->fetch();
842       /* Remove FAI objects if no FAI class is selected */ 
843       if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
844         $this->attrs['FAIclass']        = array();
845         $this->attrs['FAIdebianMirror'] = array();
846       }
847     }else{
849       /* Don't touch FAI objects if something went wrong with the si daemon.
850        */
851       if(isset($this->attrs['FAIclass'])) unset($this->attrs['FAIclass']);
852       if(isset($this->attrs['FAIdebianMirror'])) unset($this->attrs['FAIdebianMirror']);
853     }
855     /* prepare share settings */
856     $tmp = array();
857     foreach($this->gotoShares as $name => $settings){
858       $tmp2= explode("|",$name);
859       $name = $tmp2[0];
860       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
861     }
862     $this->attrs['gotoShare']=$tmp;
863     $this->cleanup();
864     $ldap->modify ($this->attrs); 
865     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
867     if (!$ldap->success()){
868       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
869     }
870     $this->handle_post_events("modify");
872     /* Check if LDAP server has changed */
873     if ((isset($this->attrs['gotoLdapServer']) && class_available("DaemonEvent")) || $this->gotoLdap_inherit){
874       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
875       $o_queue = new gosaSupportDaemon();
876       if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
877         $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
878         $macs = array();
879     
880         /* Get list of macAddresses 
881          */
882         if(isset($this->parent->by_object['ogroup'])){
883         
884           /* If we are an object group, add all member macs 
885            */
886           $p = $this->parent->by_object['ogroup'];
887           foreach($p->memberList as $dn => $obj){
888             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
889               $macs[] = $p->objcache[$dn]['macAddress'];
890             }
891           }
892         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
894           /* We are a workstation. Add current mac.
895            */
896           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
897           if(!empty($mac)){
898             $macs[] = $mac;
899           }          
900         }elseif(isset($this->parent->by_object['servgeneric']->netConfigDNS->macAddress)){
902           /* We are a server. Add current mac.
903            */
904           $mac = $this->parent->by_object['servgeneric']->netConfigDNS->macAddress;
905           if(!empty($mac)){
906             $macs[] = $mac;
907           }          
908         }
910         /* Trigger event for all member objects 
911          */
912         foreach($macs as $mac){
913           $tmp = new $evt['CLASS_NAME']($this->config);
914           $tmp->set_type(TRIGGERED_EVENT);
915           $tmp->add_targets(array($mac));
916           if(!$o_queue->append($tmp)){
917             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
918           }
919         }
920       }
921     }
922   }
925   /* Add value to array, check if unique */
926   function add_list (&$array, $value)
927   {
928     if ($value != ""){
929       $array[]= $value;
930       sort($array);
931       array_unique ($array);
932     }
933   }
936   /* Delete value to array, check if unique */
937   function del_list (&$array, $list)
938   {
939     $tmp= array();
940     foreach ($array as $mod){
941       if (!in_array($mod, $list)){
942         $tmp[]= $mod;
943       }
944     }
945     $array= $tmp;
946   }
948   /* Generate ListBox frindly output for the defined shares
949    * Possibly Add or remove an attribute here,
950    */
951   function printOutAssignedShares()
952   {
953     $a_return = array();
954     if(is_array($this->gotoShares)){
955       foreach($this->gotoShares as $share){
956         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
957       }
958     }
959     return($a_return);
960   }
964   function PrepareForCopyPaste($source)
965   {
966     plugin::PrepareForCopyPaste($source);    
967     $source_o = new workstartup ($this->config, $source['dn']);
968     foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
969           "gotoKernelParameters","gotoShares","gotoLdapServers","gotoLdapServerList",
970           "gotoLdap_inherit") as $attr){
971       $this->$attr = $source_o->$attr;
972     }
973   }
975   
976   function array_switch_item($ar,$from,$to)
977   {
978     if(!is_array($ar)){
979       return(false);
980     }
981     if(!isset($ar[$from])){
982       return(false);
983     }
984     if(!isset($ar[$to])){
985       return(false);
986     }
988     $tmp = $ar[$from];
989     $ar[$from] = $ar[$to];    
990     $ar[$to] = $tmp;    
991     return($ar);
992   }
995   /* Return plugin informations for acl handling */ 
996   static function plInfo()
997   {
998     return (array( 
999           "plShortName"   => _("Startup"),
1000           "plDescription" => _("System startup"),
1001           "plSelfModify"  => FALSE,
1002           "plDepends"     => array(),
1003           "plPriority"    => 9,
1004           "plSection"     => array("administration"),           
1005           "plCategory"    => array("workstation","server","ogroups"),
1007           "plProvidedAcls"=> array(
1008             "gotoLdapServer"        => _("Ldap server"),
1009             "gotoBootKernel"        => _("Boot kernel"),
1010             "bootmode"              => _("Boot mode"),
1011             "gotoKernelParameters"  => _("Kernel parameter"),
1013             "gotoModules"           => _("Kernel modules"),
1014             "gotoShare"             => _("Shares"),
1016             "FAIclass"              => _("FAI classes"),
1017             "FAIdebianMirror"       => _("Debian mirror"),
1018             "FAIrelease"            => _("Debian release"),
1020             "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
1021           ));
1022   }
1025   /* Updates release dns 
1026    *  and reads all classes for the current release, 
1027    *  if not already done ($this->cache).
1028    */
1029   function update_fai_cache($first_call = FALSE)
1030   {
1031     $force = FALSE;
1032     if(!$this->si_active) return; 
1033     $start = microtime(TRUE);  
1035     if($this->si_fai_action_failed && !isset($_POST['fai_si_retry'])) return;
1037     $this->si_fai_action_failed = FALSE;
1039     /* Get the list of available servers and their releases. 
1040      */
1041     if($force || !isset($this->cache['SERVERS'])){
1043       $o_queue = new gosaSupportDaemon();
1044       $tmp = $o_queue->FAI_get_server();
1045       if($o_queue->is_error()){
1046         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1047         $this->si_fai_action_failed = TRUE;
1048         $this->cache = array();
1049         return;
1050       }else{
1052         foreach($tmp as $entry){
1053           $rel = $entry['FAI_RELEASE'];
1054           $this->cache['SERVERS']['auto'][$rel] = $rel;
1055           $this->cache['SERVERS'][$entry['SERVER']][$rel] = $rel;
1056           uksort($this->cache['SERVERS']['auto'], 'strnatcasecmp');
1057           uksort($this->cache['SERVERS'][$entry['SERVER']], 'strnatcasecmp');
1058         }
1059       }
1060     }
1062     /* Ensure that our selection is valid, else we get several PHP warnings 
1063         if there is no FAI configuration at all.
1064      */
1065     if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1066       $this->cache['SERVERS'][$this->FAIdebianMirror][''] ='';
1067     }
1069     /* Build up arrays, without checks */
1070     if(!$first_call){
1072       /* Check if the selected mirror is available */
1073       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1074         $this->FAIdebianMirror = "auto";
1075         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1076         trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
1077       }
1079       /* Check if the selected release is available */
1080       if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
1082         if($this->FAIrelease != ""){
1083           trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1084         }
1085         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1086       }
1087     }
1089     /* Get classes for release from cache. 
1090      * Or build cache
1091      */
1092     if($this->FAIdebianMirror == "inherited"){
1093       $release = $this->InheritedFAIrelease;
1094     }else{
1095       $release = $this->FAIrelease;
1096     }
1098     if($force || !isset($this->cache['CLASSES'][$release]) && $release != ""){
1100       /* Get the list of available servers and their releases.
1101        */
1102       $o_queue = new gosaSupportDaemon();
1103       $tmp = $o_queue->FAI_get_classes($release);
1105       $this->cache['CLASSES'][$release] = array();
1106       if($o_queue->is_error()){
1107         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1108         $this->si_fai_action_failed = TRUE;
1109         $this->cache=array();
1110         return;
1111       }else{
1112         foreach($tmp as $entry){
1113           $class = $entry['CLASS'];
1114           $this->cache['CLASSES'][$release][$class][] = $this->analyse_fai_object($entry); 
1115         }
1116       }
1118       /* Add object caught from external hook
1119        */
1120       $lines= $this->GetHookElements();
1121       foreach ($lines as $hline){
1122         $entries= explode(";", $hline);
1123         $server = $entries['0'];
1124         $url    = $entries['1'];
1125         if (!empty($url)){
1127           /* Split releases */
1128           if (isset($entries[2])){
1129             $releases= explode(",", $entries[2]);
1131             foreach ($releases as $release_data){
1132               $release_c  = preg_replace('/:.*$/', '', $release_data);
1133               $sections_c = explode(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1134               $classes_c  = explode('|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1136               if($release_c == $release){
1137                 $this->cache['SERVERS'][$url][$release_c]=$release_c;
1138                 $this->cache['SERVERS']['auto'][$release_c]=$release_c; 
1139                 foreach ($classes_c as $class){
1140                   if ($class != ""){
1141                     $this->cache['CLASSES'][$release_c][$class]= array();
1142                   }
1143                 }
1144               }
1145             }
1146           }
1147         }
1148       }
1149       uksort($this->cache['SERVERS'], 'strnatcasecmp');
1151       /* Only add inherit option, if we are part in an object group
1152        */
1153       if($this->member_of_ogroup){
1154         $this->cache['SERVERS'] = array_merge(array('inherited' => array()),$this->cache['SERVERS']);
1155       }
1156     }
1158     /* Get list of available kernel for this release 
1159      */
1160     if(!isset($this->cache['KERNELS'])) $this->cache['KERNELS'] = array();
1162     if($force || !isset($this->cache['KERNELS'][$release])){
1163       $o_queue = new gosaSupportDaemon();
1164       $tmp = $o_queue->FAI_get_kernels($release);
1165       $this->cache['KERNELS'][$release] = array();
1166       foreach($this->gotoBootKernels as $name => $default){
1167         $this->cache['KERNELS'][$release][$name] = $default;
1168       }
1169       foreach($tmp as $kernel){
1170         if(empty($kernel)) continue;
1171         $this->cache['KERNELS'][$release][$kernel]=$kernel;
1172       }
1173       ksort($this->cache['KERNELS'][$release]);
1174     }
1175   }
1178   /* This function return an array containing all 
1179    *  invalid classes for the selected server/release
1180    */
1181   function get_invalid_classes($classes)
1182   {
1183     $this->update_fai_cache();
1184     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1185       $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1186     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1187       $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1188     }else{
1189       $release_classes = array();
1190     }
1193     /* Detect all classes that are not valid 
1194      *  for the selected release 
1195      */
1196     $NA = array();
1197     foreach($classes as $class){
1198       if(!isset($release_classes[$class])){
1199         $NA[] = $class;
1200       }
1201     }
1202     return($NA);
1203   }  
1205   
1206   /* Get all selectable classes for the ui select box
1207    */
1208   function selectable_classes()
1209   {
1210     $this->update_fai_cache();
1212     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1213       $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1214     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1215       $classes = $this->cache['CLASSES'][$this->FAIrelease];
1216     }else{
1217       $classes = array();
1218     }
1220     $Abbr ="";
1221     $ret= array();
1222     foreach($classes as $class_name => $class_types){
1223       if(!in_array($class_name,$this->FAIclass)){
1224         $Abbr = "";
1225         foreach($class_types as $type){
1226           if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1227             $Abbr .= $type['Abbr']." ";
1228           }
1229         }
1230         $ret[$class_name] = trim($Abbr);
1231       }
1232     }
1233     uksort($ret, 'strnatcasecmp');
1234     return($ret);
1235   }
1238   /* Analyse FAI object and return an array with usefull informations like 
1239    *  FAIobject type.
1240    */
1241   function analyse_fai_object($attr)
1242   {
1243     $tmp = array();
1244     switch($attr['TYPE']){
1246       case 'FAIpackageList':
1247         $tmp["Type"]= 'FAIpackageList';
1248         $tmp["Abbr"]= 'Pl';
1249         break;
1250       case 'FAItemplate': 
1251         $tmp["Type"]= 'FAItemplate'; 
1252         $tmp["Abbr"]= 'T'; 
1253         break;
1254       case 'FAIvariable':
1255         $tmp["Type"]= 'FAIvariable'; 
1256         $tmp["Abbr"]= 'V'; 
1257         break;
1258       case 'FAIscript':
1259         $tmp["Type"]= 'FAIscript'; 
1260         $tmp["Abbr"]= 'S'; 
1261         break;
1262       case 'FAIhook':
1263         $tmp["Type"]= 'FAIhook'; 
1264         $tmp["Abbr"]= 'H'; 
1265         break;
1266       case 'FAIpartitionTable':
1267         $tmp["Type"]= 'FAIpartitionTable'; 
1268         $tmp["Abbr"]= 'Pt'; 
1269         break;
1270       case 'FAIprofile':
1271         $tmp["Type"]= 'FAIprofile'; 
1272         $tmp["Abbr"]= 'P'; 
1273         break;
1274       default: trigger_error("Unknown FAI object type!");;
1275     }
1276     return($tmp);
1277   }
1280   /* Return repository hook output, if possible.
1281    */
1282   function GetHookElements()
1283   {
1284     $ret = array();
1285     $cmd= $this->config->search("servrepository", "repositoryBranchHook",array('tabs'));
1286     if(!empty($cmd)){
1287       $res = shell_exec($cmd);
1288       $res2 = trim($res);
1289       if((!$res)){
1290         msg_dialog::display(_("Configuration error"), msgPool::cmdexecfailed("repositoryBranchHook", $cmd), ERROR_DIALOG);
1291       }elseif(empty($res2)){
1292         msg_dialog::display(_("Configuration error"), _("'repositoryBranchHook' returned no result!"), ERROR_DIALOG);
1293       }else{
1294         $tmp = preg_split("/\n/",$res);
1295         foreach($tmp as $line){
1296           if(empty($line)) continue;
1297           $ret[]= $line;
1298         }
1299       }
1300     }
1301     return($ret);
1302   }
1305   /* This function creates the release name out of a dn 
1306    *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1307    */
1308   function dn_to_release_name($dn)
1309   {
1310     $relevant = preg_replace("/,".preg_quote(get_ou("faiBaseRDN"), '/').".*$/i","",$dn);
1311     $parts    = array_reverse(explode(",",$relevant));
1312     $str ="";
1313     foreach($parts as $part){
1314       $str .= preg_replace("/^ou=/","",$part)."/";
1315     }
1316     return(preg_replace("/\/$/","",$str)); 
1317   }
1320 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1321 ?>