Code

Update workstation startup
[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   var $bootmode             = "G";
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 $customParameters   = "";
33   var $orig_dn            = "";
34   var $ignore_account     = TRUE;
35  
36   /* FAI class selection */ 
37   var $FAIclass           = array();  // The currently selected classes 
38   var $FAIrelease         = "";
39   var $FAIdebianMirror    = "auto";
41   var $cache              = array(); // Used as cache in fai mehtods
43   var $FAIstatus          = "";
44   var $FAIclasses         = array();
46   var $view_logged        = FALSE;
47   
48   /* FAI class selection */
49   var $InheritedFAIclass       = array();
50   var $InheritedFAIrelease     = "";
51   var $InheritedFAIdebianMirror= "auto";
53   var $CopyPasteVars    = array("gotoModules","gotoShares");
54   var $fai_activated    = FALSE;
55   var $o_group_dn       = "";
56   var $member_of_ogroup = FALSE;
58   function workstartup (&$config, $dn= NULL, $parent= NULL)
59   {
60     /* Check if FAI is active */
61     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
62     if(!empty($tmp)){
63       $this->fai_activated = TRUE;
64     }else{
65       $this->attributes = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", "gotoShare");
66       $this->objectclasses  = array("GOhard");
67     }
69     plugin::plugin ($config, $dn, $parent);
71     /* Check object group membership */
72     if(!isset($this->parent->by_object['ogroup'])){
73       $ldap = $this->config->get_ldap_link();
74       $ldap->cd ($this->config->current['BASE']);
75       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn","dn"));
76       if($ldap->count()){
77         $this->member_of_ogroup = TRUE;
78         $attrs = $ldap->fetch();
79         $this->o_group_dn = $attrs['dn'];
80       }
81     }
83     /* Creating a list of valid Mirrors 
84      * none will not be saved to ldap.
85      */
86     $ldap   = $this->config->get_ldap_link();
87     $ldap->cd($this->config->current['BASE']);
88     foreach($this->config->data['SERVERS']['LDAP'] as $server) {
89       $this->gotoLdapServerList[]= $server; 
90     }
92     /* Get list of assigned ldap servers 
93      */ 
94     if(isset($this->attrs['gotoLdapServer'])){
95       unset($this->attrs['gotoLdapServer']['count']);
96       sort($this->attrs['gotoLdapServer']);
97       foreach($this->attrs['gotoLdapServer'] as $value){
98         $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
99       }
100     }
101     if(!count($this->gotoLdapServers) && $this->member_of_ogroup){ 
102       $this->gotoLdap_inherit = TRUE;
103     }
105     /* FAI Initialization
106        Skip this if FAI is not activated 
107      */
108     if($this->fai_activated) {
110       $this->update_fai_cache(TRUE);
112       /* Parse used FAIclasses (stored as string).
113        * The single classes are seperated by ' '.
114        * There is also the release type given, after first
115        *  occurrence of ':'.
116        */
117       $this->FAIclass =array();
118       if(isset($this->attrs['FAIclass'][0])){
119         $tmp = split(" ",$this->attrs['FAIclass'][0]);
120         $tmp2 =array();  
122         foreach($tmp as $class){
123           if( ":" == $class[0] ) {
124             $this->FAIrelease = trim(substr($class, 1));
125           }else{
126             $tmp2[$class] = $class;
127           }
128         }
129         $this->FAIclass = $tmp2;
130       }
131     }
133     /* Get arrays */
134     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
135       if (isset($this->attrs["$val"]["count"])){
136         for ($i= 0; $i<$this->attrs["count"]; $i++){
137           if (isset($this->attrs["$val"][$i])){
138             array_push($this->$val, $this->attrs["$val"][$i]);
139           }
140         }
141       }
142       sort ($this->$val);
143       $this->$val= array_unique($this->$val);
144     }
146     /* Parse Kernel Parameters to decide what boot mode is enabled */
147     if (preg_match("/ splash=silent/", $this->gotoKernelParameters)){
148       $this->bootmode= "G";
149     } elseif (preg_match("/ debug/", $this->gotoKernelParameters)){
150       $this->bootmode= "D";
151     } elseif ($this->gotoKernelParameters == "") {
152       $this->bootmode= "G";
153     } else {
154       $this->bootmode= "T";
155     }
156     if (preg_match("/ o /", $this->gotoKernelParameters)){
157       $this->customParameters= preg_replace ("/^.* o /", "", $this->gotoKernelParameters);
158     } else {
159       $this->customParameters= "";
160     }
162     /* Prepare Shares */
163     if((isset($this->attrs['gotoShare']))&&(is_array($this->attrs['gotoShare']))){
164       unset($this->attrs['gotoShare']['count']);
165       foreach($this->attrs['gotoShare'] as $share){
166         $tmp = $tmp2 = array();
167         $tmp = split("\|",$share);
168         $tmp2['server']      =$tmp[0];
169         $tmp2['name']        =$tmp[1];
170         $tmp2['mountPoint']  =$tmp[2];
171         $this->gotoShares[$tmp[1]."|".$tmp[0]]=$tmp2;
172       }
173     }
175     $this->gotoShareSelections= $config->getShareList(true);
176     $this->gotoAvailableShares= $config->getShareList(false);
177     $tmp2 = array();
178   
180     $this->orig_dn= $this->dn;
182     /* Handle inheritance value "default" */
183     if ($this->member_of_ogroup){
184       $this->gotoBootKernels= array("default-inherited" => '['._("inherited").']'); 
185     }
187     /* If we are member in an object group,
188      *  we have to handle inherited values.
189      * So you can see what is inherited.
190      */
191     if ($this->member_of_ogroup){
193       if(count($this->FAIclass)==0 && $this->FAIrelease == ""){
194         $this->FAIdebianMirror = "inherited";
195       }
197       if($this->fai_activated){
198         $map= array("gotoBootKernel","FAIclass","FAIdebianMirror");
199       }else{
200         $map= array("gotoBootKernel");
201       }
203       $ldap = $this->config->get_ldap_link();
204       $ldap->cat($this->o_group_dn);
205       $attrs= $ldap->fetch();
207       foreach ($map as $name){
208         if (!isset($attrs[$name][0])){
209           continue;
210         }
212         switch ($name){
213           case 'gotoBootKernel':
214             $this->gotoBootKernels['default-inherited']=  _("inherited").' ['.$attrs[$name][0].']' ;
215             break;
217           case 'FAIclass':
218             $str = split(":",$attrs[$name][0]);
219             $this->InheritedFAIclass    = split("\ ",trim($str[0]));
220             $this->InheritedFAIrelease  = trim($str[1]);
221             break;
223           case 'FAIdebianMirror':
224             $this->InheritedFAIdebianMirror = $attrs[$name][0];
225             break;
226         }
227       }
228     }
231     if($this->fai_activated){
233       /* Check if the current mirror is available 
234        */
235       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
236         if(count($this->FAIclass)){
237           msg_dialog::display(_("Error"), sprintf(_("FAI mirror '%s' is not available - setting to mirror 'auto'!"), $this->FAIdebianMirror), ERROR_DIALOG);
238         }
239         $this->FAIdebianMirror = "auto";
240         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
241         $this->cache =array();
242         $this->update_fai_cache();
243         
244       }
245   
246       /* Check if the current mirror is available 
247        */
248       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
249         $new_release = key($this->cache['SERVERS'][$this->FAIdebianMirror]); 
250         if(count($this->FAIclass)){
251           msg_dialog::display(_("Error"), sprintf(_("FAI release '%s' is not available on mirror '%s' - setting to release '%s'!"), $this->FAIrelease, $this->FAIdebianmirror), ERROR_DIALOG);
252         }
253         $this->FAIrelease = $new_release;
254         $this->cache =array();
255         $this->update_fai_cache();
256       }
257     }
259     /* Get list of boot kernels */
260     if (isset($this->config->data['TABS'])){
261       $command= $this->config->search(get_class($this), "KERNELS",array('tabs'));
262       if (!check_command($command)){
263         $message[]= sprintf(_("Command '%s', specified as KERNELS hook for plugin '%s' doesn't seem to exist."), $command,
264             get_class($this));
265       } else {
266         $fh= popen($command, "r");
267         while (!feof($fh)) {
268           $buffer= trim(fgets($fh, 256));
269           if(!empty($buffer)){
270             $name=$value = $buffer;
271             if(preg_match("/:/",$buffer)){
272               $name = preg_replace("/:.*$/","",$buffer);
273               $value= preg_replace("/^.*:/","",$buffer);
274               $this->gotoBootKernels[$name]= $name.":".$value;
275             }else{
276               $this->gotoBootKernels[$name]= $value;
277             }
278           }
279         }
280         pclose($fh);
281       }
282     }
284     /* Turn to default, if we've nothing to inherit */
285     if (!isset($this->gotoBootKernels['default-inherited']) && $this->gotoBootKernel == "default-inherited"){
286       $this->gotoBootKernel= "default";
287     }
288   }
290   
291   function check()
292   {
293     $messages = array();
294     
295     /* Call common method to give check the hook */
296     $messages= plugin::check();
298     /* If there are packages selected, but no mirror show error */   
299     if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
300       $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
301     }
303     return($messages);
304   }
306   function execute()
307   {
308         /* Call parent execute */
309         plugin::execute();
311     if($this->is_account && !$this->view_logged){
312       $this->view_logged = TRUE;
313       new log("view","workstation/".get_class($this),$this->dn);
314     }
316     /* Do we need to flip is_account state? */
317     if(isset($_POST['modify_state'])){
318       if($this->is_account && $this->acl_is_removeable()){
319         $this->is_account= FALSE;
320       }elseif(!$this->is_account && $this->acl_is_createable()){
321         $this->is_account= TRUE;
322       }
323     }
325     /* Do we represent a valid terminal? */
326     if (!$this->is_account && $this->parent === NULL){
327       $display= "<img alt=\"\" src=\"images/stop.png\" align=middle>&nbsp;<b>".
328         msgPool::noValidExtension(_("workstation"))."</b>";
329       return ($display);
330     }
332     /* Add module */
333     if (isset ($_POST['add_module'])){
334       if ($_POST['module'] != "" && $this->acl_is_writeable("gotoModule")){
335         $this->add_list ($this->gotoModules, $_POST['module']);
336       }
337     }
339     /* Delete module */
340     if (isset ($_POST['delete_module'])){
341       if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoModule")){
342         $this->del_list ($this->gotoModules, $_POST['modules_list']);
343       }
344     }
346     /* FAI class management */
347     if($this->fai_activated){
348       if(((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))) && ($this->acl_is_writeable("FAIclass"))){
349         $found = 0 ; 
351         /* If this new class/profile will attach a second partition table
352          * to our list of classes, abort and show a message.
353          */
354         foreach($this->FAIclass as $name){
355           if(isset($this->FAIclassInfo[$name])){
356             foreach($this->FAIclassInfo[$name] as $atr){
357               if(isset($atr['obj'])){
358                 if($atr['obj'] == "FAIpartitionTable"){
359                   $found ++ ; 
360                 }
361               }
362             }
363           }
364         }
366         if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpartitionTable']))&&($found>0)){
367           msg_dialog(_("Error"), _("There is already a profile containing a partition table in your configuration!") , ERROR_DIALOG);
368         }else{
369           $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
370         }
371       }
373       $sort = false;
375       /* Move one used class class one position up or down */
376       if($this->acl_is_writeable("FAIclass")){
377         foreach($_POST as $name => $val){
379           $sort_type = false;
380           if((preg_match("/sort_up/",$name))&&(!$sort)){
381             $sort_type = "sort_up_";
382           }
383           if((preg_match("/sort_down/",$name))&&(!$sort)){
384             $sort_type = "sort_down_";
385           }
387           if(($sort_type)&&(!$sort)){
388             $value = base64_decode(preg_replace("/_.*$/i","",preg_replace("/".$sort_type."/i","",$name)));
389             $sort = true;
391             $last = -1;
392             $change_down  = -1;
394             /* Create array with numeric index */ 
395             $tmp = array();
396             foreach($this->FAIclass as $class){
397               $tmp [] = $class;
398             }
400             /* Walk trough array */
401             foreach($tmp as $key => $faiName){
402               if($faiName == $value){
403                 if($sort_type == "sort_up_"){
404                   if($last != -1){
405                     $change_down= $last;
406                   }
407                 }else{
408                   if(isset($tmp[$key+1])){
409                     $change_down = $key;
410                   }
411                 }
412               }
413               $last = $key;
414             }
416             $tmp2 = array();
417             $skip = false;    
419             foreach($tmp as $ky => $vl){
421               if($ky == $change_down){
422                 $skip = $vl;
423               }else{
424                 $tmp2[$vl] = $vl;
425               }
426               if(($skip != false)&&($ky != $change_down)){
427                 $tmp2[$skip]  = $skip;
428                 $skip =false;
429               }
430             }   
431             $this->FAIclass = $tmp2; 
432           }
434           if(preg_match("/fai_remove/i",$name)){
435             $value = base64_decode(preg_replace("/_.*$/i","",preg_replace("/fai_remove_/i","",$name)));
436             unset($this->FAIclass[$value]);
437           }
438         }
439       }
441       /* Delete selected class from our list */
442       if($this->acl_is_writeable("FAIclass")){
443         if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
444           if(isset($this->FAIclass[$_POST['FAIclassSel']])){
445             unset($this->FAIclass[$_POST['FAIclassSel']]);
446           }
447         }
448       }
449     }// END fai handling
451     /* Show main page */
452     $smarty= get_smarty();
454     /* Assign ACLs to smarty */
455     $tmp = $this->plInfo();
456     foreach($tmp['plProvidedAcls'] as $name => $translation){
457       $smarty->assign($name."ACL",$this->getacl($name));
458     } 
460     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
462     /* In this section server shares will be defined
463      * A user can select one of the given shares and a mount point
464      *  and attach this combination to his setup.
465      */
466     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
467     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
469     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
470      * This entry will be, a combination of mountPoint and sharedefinitions
471      */
472     if((isset($_POST['gotoShareAdd'])) && ($this->acl_is_writeable("gotoShare"))) {
473       /* We assign a share to this user, if we don't know where to mount the share */
474       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
475         msg_dialog(_("Error"), msgPool::required(_("Mount point")), ERROR_DIALOG);
476       }else{
477         if(count($this->gotoAvailableShares)){
478           $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
479           $s_mount = $_POST['gotoShareMountPoint'];
480           /* Preparing the new assignment */
481           $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
482           $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
483         }
484       }
485     }
487     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
488      * If there is no defined share selected, we will abort the deletion without any message
489      */
490     if(($this->acl_is_writeable("gotoShare"))&& (isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
491       unset($this->gotoShares[$_POST['gotoShare']]);
492     }
494     $smarty->assign("gotoShares",$this->printOutAssignedShares());
495     $smarty->assign("gotoSharesCount",count($this->printOutAssignedShares()));
496     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
497     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
499     /* Create divSelectBox for ldap server selection
500      */
501     $SelectBoxLdapServer = new divSelectBox("LdapServer");
502     $SelectBoxLdapServer->SetHeight(130);
504     /* Add new ldap server to the list */
505     if(!$this->gotoLdap_inherit && isset($_POST['add_ldap_server']) && isset($_POST['ldap_server_to_add'])){
506       if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
507         $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
508         if(!in_array($to_add,$this->gotoLdapServers)){
509           $this->gotoLdapServers[] = $to_add;
510         }
511       }
512     }
513     
514     /* Move ldap servers up and down */
515     if(!$this->gotoLdap_inherit){
516       foreach($_POST as $name => $value){
517         if(preg_match("/sort_ldap_up_/",$name)){
518           $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
519           $from =  $id;  
520           $to   =  $id -1;
521           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
522           if($tmp){
523             $this->gotoLdapServers = $tmp;
524           }
525           break;
526         }
527         if(preg_match("/sort_ldap_down_/",$name)){
528           $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
529           $from =  $id;  
530           $to   =  $id +1;
531           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
532           if($tmp){
533             $this->gotoLdapServers = $tmp;
534           }
535           break;
536         }
537         if(preg_match("/gotoLdapRemove_/",$name)){
538           $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
539           $value = $this->gotoLdapServers[$id];
540           $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
541           break;
542         }
543       } 
544     }
545   
546     /* Add Entries */
547     foreach($this->gotoLdapServers as $key => $server){
548       /* Announce missing entries */
549       if(!in_array($server,$this->gotoLdapServerList)){
550         $server = $server."&nbsp;<font style='color:red'>(missing)</font>";
551       }
553       /* Convert old style entry */
554       if (!preg_match('%:ldap://%', $server)){
555         $server= "ldap://".preg_replace('/^([^:]+):/', '\1/', $server);
557       /* Beautify new style entries */
558       } else {
559         $server= preg_replace("/^[^:]+:/", "", $server);
560       }
562       $SelectBoxLdapServer->AddEntry(
563           array(array("string" => $server),
564             array("string" => 
565               "<input class='center' type='image' src='images/sort_up.png' name='sort_ldap_up_".$key."'>&nbsp;".
566               "<input class='center' type='image' src='images/sort_down.png' name='sort_ldap_down_".$key."'>&nbsp;".
567               "<input class='center' type='image' src='images/edittrash.png' name='gotoLdapRemove_".$key."'>",
568               "attach" => "style='text-align:right;width:40px;border-right:0px;'")));
569     }    
571     if($this->gotoLdap_inherit){
572       $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
573     }else{
574       $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
575     }
576     
577     $list = array();
578     foreach($this->gotoLdapServerList as $key => $entry){
579       if(!in_array($entry,$this->gotoLdapServers)){
581         /* Convert old style entry */
582         if (!preg_match('%:ldap://%', $entry)){
583           $entry= "ldap://".preg_replace('/^([^:]+):/', '\1/', $entry);
585         /* Beautify new style entries */
586         } else {
587           $entry= preg_replace("/^[^:]+:/", "", $entry);
588         }
590         $list[$key] = $entry;
591       }
592     }
593     $smarty->assign("gotoLdapServers",    $SelectBoxLdapServer->DrawList());
594     $smarty->assign("gotoLdapServerList", $list);
595     $smarty->assign("gotoLdap_inherit",   $this->gotoLdap_inherit);
596     $smarty->assign("JS",  session::get('js'));
598     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
599       $smarty->assign("$val", $this->$val);
600     }
602     /* Values */
603     foreach(array("gotoBootKernel", "customParameters", "gotoShare","FAIclasses","FAIclass","FAIdebianMirror","FAIrelease") as $val){
604       $smarty->assign($val, $this->$val);
605     }
607     $smarty->assign("fai_activated",$this->fai_activated);
609     /* Create FAI output */
610     if($this->fai_activated){
612       $this->update_fai_cache();
614       $smarty->assign("FAIservers"  , $this->cache['SERVERS']);
615       $smarty->assign("FAIdebianMirror",$this->FAIdebianMirror);
616       $smarty->assign("FAIrelease"  , $this->FAIrelease);
617       $smarty->assign("FAIclasses"  , $this->selectable_classes());
619       $smarty->assign("InheritedFAIrelease",$this->InheritedFAIrelease);
621       $div = new divSelectBox("WSFAIscriptClasses");
622       $div -> SetHeight("110");
623       $str_up     = " &nbsp;<input type='image' src='images/sort_up.png'    name='sort_up_%s'    value='%s'>";
624       $str_down   = " &nbsp;<input type='image' src='images/sort_down.png'  name='sort_down_%s'  value='%s'>";
625       $str_remove = " &nbsp;<input type='image' src='images/edittrash.png'  name='fai_remove_%s' value='%s'>";
626       $str_empty  = " &nbsp;<img src='images/empty.png' alt=\"\" width='7'>"; 
628       /* Get classes */
629       if($this->FAIdebianMirror == "inherited"){
630         $tmp = $this->InheritedFAIclass;
631       }else{
632         $tmp = $this->FAIclass;
633       }
635       /* Get invalid classes */
636       $invalid = $this->get_invalid_classes($tmp);
638       /* Draw every single entry */
639       $i = 1;
640       foreach($tmp as $class){
642         /* Mark invalid classes. (Not in selected release)
643          */
644         $marker = "";
645         if(in_array_ics($class,$invalid)){
646           $marker = "&nbsp;<font color='red'>("._("Not available in current setup").")</font>";
647         }
649         /* Create up/down priority icons  
650          * Skip this, if we have inherited the FAI classes.
651          */
652         if($this->FAIdebianMirror == "inherited"){
653           $str = "";
654         }else{
655           if($i==1){
656             $str = $str_empty.$str_down.$str_remove;
657           }elseif($i == count($this->FAIclass)){
658             $str = $str_up.$str_empty.$str_remove;
659           }else{
660             $str = $str_up.$str_down.$str_remove;
661           }
662         }
663         $i ++ ; 
665         /* Get Description tag 
666          *  There may be several FAI objects with the same class name, 
667          *   use the description from FAIprofile, if possible.
668          */  
669         $desc = ""; 
670         if(isset($this->cache['CLASSES'][$this->FAIrelease][$class])){
671           foreach($this->cache['CLASSES'][$this->FAIrelease][$class] as $types ){
672             if(isset($types['Desc'])){
673               $desc= $types['Desc'];
674               if($types['Type'] == "FAIprofile"){
675                 break;
676               }
677             }
678           }
679         }
680         if(!empty($desc)){
681           $desc = "&nbsp;[".trim($desc)."]";
682         }        
684         $div->AddEntry(array(
685               array("string"=>$class.$desc.$marker),
686               array("string"=>preg_replace("/\%s/",base64_encode($class),$str),"attach"=>"style='width:50px;border-right:none;'")
687               ));
688       }  
689       $smarty->assign("FAIScriptlist",$div->DrawList()); 
690     }// END FAI output generation 
692     /* Radio button group */
693     if (preg_match("/G/", $this->bootmode)) {
694       $smarty->assign("graphicalbootup", "checked");
695     } else {
696       $smarty->assign("graphicalbootup", "");
697     }
698     if (preg_match("/T/", $this->bootmode)) {
699       $smarty->assign("textbootup", "checked");
700     } else {
701       $smarty->assign("textbootup", "");
702     }
703     if (preg_match("/D/", $this->bootmode)) {
704       $smarty->assign("debugbootup", "checked");
705     } else {
706       $smarty->assign("debugbootup", "");
707     }
709     /* Show main page */
710     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
711   }
714   function remove_from_parent()
715   {
716     $this->handle_post_events("remove");
717     new log("remove","workstation/".get_class($this),$this->dn);
718   }
721   /* Save data to object */
722   function save_object()
723   {
724     $old_mirror  = $this->FAIdebianMirror;
725     plugin::save_object();
727     /* Update release */
728     if($old_mirror != $this->FAIdebianMirror){
729       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
730         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
731       }
732     }
734     if(isset($_POST['WorkstationStarttabPosted'])){
735       if(isset($_POST['gotoLdap_inherit'])){
736         $this->gotoLdap_inherit = TRUE;
737       }else{
738         $this->gotoLdap_inherit = FALSE;
739       }
741       /* Save group radio buttons */
742       if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
743         $this->bootmode= $_POST["bootmode"];
744       }
746       /* Save kernel parameters */
747       if ($this->acl_is_writeable("gotoKernelParameters") && isset($_POST["customParameters"])){
748         $this->customParameters= $_POST["customParameters"];
749       }
750     }
751   }
754   /* Save to LDAP */
755   function save()
756   {
758     /* Depending on the baseobject (Ogroup / WS) we
759      *  use another set of objectClasses
760      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
761      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
762      */
763     if(isset($this->parent->by_object['ogroup'])){
764       $this->objectclasses = array("gotoWorkstationTemplate");
765     }elseif(isset($this->parent->by_object['workgeneric'])){
766       $this->objectclasses = array("GOhard");
767     }elseif(isset($this->parent->by_object['servgeneric'])){
768       $this->objectclasses = array("GOhard","gotoWorkstationTemplate");
769     }else{
770       print "Object Type Configuration : unknown";
771       exit();
772     }
774     /* Append FAI class */
775     if($this->fai_activated){
776       $this->objectclasses[]  = "FAIobject";
777     }
779     /* Find proper terminal path for tftp configuration
780        FIXME: This is suboptimal when the default has changed to
781        another location! */
782     if (($this->gotoTerminalPath == "default")){
783       $ldap= $this->config->get_ldap_link();
785       /* Strip relevant part from dn, keep trailing ',' */
786       $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalou')."/i", "", $this->dn);
787       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
789       /* Walk from top to base and try to load default values for
790          'gotoTerminalPath'. Abort when an entry is found. */
791       while (TRUE){
792         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
794         $ldap->cat("cn=default,".get_ou('terminalou').$tmp.
795             $this->config->current['BASE'], array('gotoTerminalPath'));
796         $attrs= $ldap->fetch();
797         if (isset($attrs['gotoTerminalPath'])){
798           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
799           break;
800         }
802         /* Nothing left? */
803         if ($tmp == ""){
804           break;
805         }
806       }
807     }
809     /* Add semi automatic values */
810     // FIXME: LDAP Server may not be set here...
811     $this->gotoKernelParameters= "ldap=".base64_encode($this->gotoLdapServer);
813     switch ($this->bootmode){
814       case "D":
815         $this->gotoKernelParameters.= " debug";
816       break;
817       case "G":
818         $this->gotoKernelParameters.= " splash=silent";
819       break;
820     }
821     if ($this->customParameters != ""){
822       $this->gotoKernelParameters.= " o ".$this->customParameters;
823     }
825     plugin::save();
827     unset( $this->attrs['FAIrelease'] );
828     $str = "";
830     /* Skip FAI attribute handling if not necessary */
831     if($this->fai_activated){
832       if($this->FAIdebianMirror == "inherited"){
833         $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array();
834       }else{
835         foreach($this->FAIclass as $class){
836           $str .= $class." ";
837         }
838         $str = trim($str);
839         if(empty($this->attrs['FAIclass'])){
840           $this->attrs['FAIclass'] = array();
841         }else{
842           $this->attrs['FAIclass']= $str.":".$this->FAIrelease;
843         }
844       }
845     }
847     /* Add missing arrays */
848     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
849       if (isset ($this->$val) && count ($this->$val) != 0){
850     
851         $this->attrs["$val"]= array_unique($this->$val);
852       }
853       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
854     }
856     /* Prepare list of ldap servers */
857     $this->attrs['gotoLdapServer'] = array();
858     if(!$this->gotoLdap_inherit){
859       $i = 0;
860       foreach($this->gotoLdapServers as $server){
861         $i ++;
862         $this->attrs['gotoLdapServer'][] = $i.":".$server;
863       }
864     }
866     /* Check if LDAP server has changed */
867     $ldap_changed= false;
868     if (isset($this->saved_attributes['gotoLdapServer'])){
869       if (isset($this->attrs['gotoLdapServer']) && $this->attrs['gotoLdapServer'] != $this->saved_attributes['gotoLdapServer']){
870         $ldap_changed= true;
871       }
872     } else {
873       if (isset($this->attrs['gotoLdapServer'])){
874         $ldap_changed= true;
875       }
876     }
878     if (($this->attrs['gotoBootKernel'] == "default-inherited") || ($this->attrs['gotoBootKernel'] == "%default%")){
879       $this->attrs['gotoBootKernel']= array();
880     }
882     /* if mirror == none stop saving this attribute */
883     if($this->FAIdebianMirror == "none"){
884       $this->FAIdebianMirror = "";
885     }
886    
887     /* Get FAIstate from object, the generic tab could have changed it during execute */
888     $ldap= $this->config->get_ldap_link();
889     $ldap->cd($this->dn);
892     /* Skip FAI attribute handling if not necessary */
893     if($this->fai_activated){
894       $ldap->cat($this->dn,array("FAIstate"));
895       $checkFAIstate = $ldap->fetch();
897       /* Remove FAI objects if no FAI class is selected */ 
898       if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
899         $this->attrs['FAIclass']        = array();
900         $this->attrs['FAIdebianMirror'] = array();
901       }
902     }
905     /* prepare share settings */
906     $tmp = array();
907     foreach($this->gotoShares as $name => $settings){
908       $tmp2= split("\|",$name);
909       $name = $tmp2[0];
910       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
911     }
912     $this->attrs['gotoShare']=$tmp;
914     $this->cleanup();
915     $ldap->modify ($this->attrs); 
916     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
918     if (!$ldap->success()){
919       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
920     }
921     $this->handle_post_events("modify");
923     /* Check if LDAP server has changed */
924     if ($ldap_changed && class_available("DaemonEvent")){
925       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
926       $o_queue = new gosaSupportDaemon();
927       if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
928         $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
929         $macs = array();
930     
931         /* Get list of macAddresses 
932          */
933         if(isset($this->parent->by_object['ogroup'])){
934         
935           /* If we are an object group, add all member macs 
936            */
937           $p = $this->parent->by_object['ogroup'];
938           foreach($p->memberList as $dn => $obj){
939             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
940               $macs[] = $p->objcache[$dn]['macAddress'];
941             }
942           }
943         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
945           /* We are a workstation. Add current mac.
946            */
947           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
948           if(!empty($mac)){
949             $macs[] = $mac;
950           }          
951         }
953         /* Trigger event for all member objects 
954          */
955         foreach($macs as $mac){
956           $tmp = new $evt['CLASS_NAME']($this->config);
957           $tmp->set_type(TRIGGERED_EVENT);
958           $tmp->add_targets(array($mac));
959           if(!$o_queue->append($tmp)){
960             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
961           }
962         }
963       }
964     }
965   }
968   /* Add value to array, check if unique */
969   function add_list (&$array, $value)
970   {
971     if ($value != ""){
972       $array[]= $value;
973       sort($array);
974       array_unique ($array);
975     }
976   }
979   /* Delete value to array, check if unique */
980   function del_list (&$array, $list)
981   {
982     $tmp= array();
983     foreach ($array as $mod){
984       if (!in_array($mod, $list)){
985         $tmp[]= $mod;
986       }
987     }
988     $array= $tmp;
989   }
991   /* Generate ListBox frindly output for the defined shares
992    * Possibly Add or remove an attribute here,
993    */
994   function printOutAssignedShares()
995   {
996     $a_return = array();
997     if(is_array($this->gotoShares)){
998       foreach($this->gotoShares as $share){
999         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
1000       }
1001     }
1002     return($a_return);
1003   }
1007   function PrepareForCopyPaste($source)
1008   {
1009     plugin::PrepareForCopyPaste($source);    
1010     $source_o = new workstartup ($this->config, $source['dn']);
1011     foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
1012           "gotoKernelParameters","gotoShares","customParameters") as $attr){
1013       $this->$attr = $source_o->$attr;
1014     }
1015   }
1017   
1018   function array_switch_item($ar,$from,$to)
1019   {
1020     if(!is_array($ar)){
1021       return(false);
1022     }
1023     if(!isset($ar[$from])){
1024       return(false);
1025     }
1026     if(!isset($ar[$to])){
1027       return(false);
1028     }
1030     $tmp = $ar[$from];
1031     $ar[$from] = $ar[$to];    
1032     $ar[$to] = $tmp;    
1033     return($ar);
1034   }
1037   /* Return plugin informations for acl handling */ 
1038   static function plInfo()
1039   {
1040     return (array( 
1041           "plShortName"   => _("Startup"),
1042           "plDescription" => _("System startup"),
1043           "plSelfModify"  => FALSE,
1044           "plDepends"     => array(),
1045           "plPriority"    => 9,
1046           "plSection"     => array("administration"),           
1047           "plCategory"    => array("workstation","server","ogroups"),
1049           "plProvidedAcls"=> array(
1050             "gotoLdapServer"        => _("Ldap server"),
1051             "gotoBootKernel"        => _("Boot kernel"),
1052             "gotoKernelParameters"  => _("Kernel parameter"),
1054             "gotoModules"           => _("Kernel modules"),
1055             "gotoShare"             => _("Shares"),
1057             "FAIclass"              => _("FAI classes"),
1058             "FAIdebianMirror"       => _("Debian mirror"),
1059             "FAIrelease"            => _("Debian release"),
1061             "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
1062           ));
1063   }
1066   /* Updates release dns 
1067    *  and reads all classes for the current release, 
1068    *  if not already done ($this->cache).
1069    */
1070   function update_fai_cache($first_call = FALSE)
1071   {
1072     $force = FALSE;
1074     $start = microtime(TRUE);  
1076     /* Get the list of available servers and their releases. 
1077      */
1078     if($force || !isset($this->cache['SERVERS'])){
1080       /* Only add inherit option, if we are part in an object group
1081        */
1082       if($this->member_of_ogroup){
1083         $this->cache['SERVERS']['inherited']=array();
1084       }
1086       $o_queue = new gosaSupportDaemon();
1087       $tmp = $o_queue->FAI_get_server(); 
1088       if($o_queue->is_error()){
1089         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1090       }else{
1091         foreach($tmp as $entry){
1092           $rel = $entry['RELEASE'];
1093           $this->cache['SERVERS']['auto'][$rel] = $rel;
1094           $this->cache['SERVERS'][$entry['SERVER']][$rel] = $rel;
1095         }
1096       }
1097     }
1099     /* Build up arrays, without checks */
1100     if(!$first_call){
1102       /* Check if the selected mirror is available */
1103       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1104         $this->FAIdebianMirror = "auto";
1105         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1106         trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
1107       }
1109       /* Check if the selected release is available */
1110       if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
1111         trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1112         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1113       }
1114     }
1116     /* Get classes for release from cache. 
1117      * Or build cache
1118      */
1119     if($this->FAIdebianMirror == "inherited"){
1120       $release = $this->InheritedFAIrelease;
1121     }else{
1122       $release = $this->FAIrelease;
1123     }
1125     if($force || !isset($this->cache['CLASSES'][$release])){
1127       /* Get the list of available servers and their releases.
1128        */
1129       $o_queue = new gosaSupportDaemon();
1130       $tmp = $o_queue->FAI_get_classes($release);
1131       $this->cache['CLASSES'][$release] = array();
1132       if($o_queue->is_error()){
1133         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1134       }else{
1135         foreach($tmp as $entry){
1136           $class = $entry['CLASS'];
1137           $this->cache['CLASSES'][$release][$class] = $this->analyse_fai_object($entry); 
1138         }
1139       }
1141       /* Add object caught from external hook
1142        */
1143       $lines= $this->GetHookElements();
1144       foreach ($lines as $hline){
1145         $entries= split(";", $hline);
1146         $server = $entries['0'];
1147         $url    = $entries['1'];
1148         if (!empty($url)){
1150           /* Split releases */
1151           if (isset($entries[2])){
1152             $releases= split(",", $entries[2]);
1154             foreach ($releases as $release_data){
1155               $release_c  = preg_replace('/:.*$/', '', $release_data);
1156               $sections_c = split(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1157               $classes_c  = split('\|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1159               if($release_c == $release){
1160                 $this->cache['SERVERS'][$url][$release_c]=$release_c;
1161                 $this->cache['SERVERS']['auto'][$release_c]=$release_c; 
1162                 foreach ($classes_c as $class){
1163                   if ($class != ""){
1164                     $this->cache['CLASSES'][$release_c][$class]= array();
1165                   }
1166                 }
1167               }
1168             }
1169           }
1170         }
1171       }
1172     }
1173 #    echo sprintf("Took %.6f <br> ",microtime(1) - $start);
1174   }
1177   /* This function return an array containing all 
1178    *  invalid classes for the selected server/release
1179    */
1180   function get_invalid_classes($classes)
1181   {
1182     $this->update_fai_cache();
1183     if($this->FAIdebianMirror == "inherited"){
1184       $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1185     }else{
1186       $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1187     }
1190     /* Detect all classes that are not valid 
1191      *  for the selected release 
1192      */
1193     $NA = array();
1194     foreach($classes as $class){
1195       if(!isset($release_classes[$class])){
1196         $NA[] = $class;
1197       }
1198     }
1199     return($NA);
1200   }  
1202   
1203   /* Get all selectable classes for the ui select box
1204    */
1205   function selectable_classes()
1206   {
1207     $this->update_fai_cache();
1209     if($this->FAIdebianMirror == "inherited"){
1210       $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1211     }else{
1212       $classes = $this->cache['CLASSES'][$this->FAIrelease];
1213     }
1215     $Abbr ="";
1216     $ret= array();
1217     foreach($classes as $class_name => $class_types){
1218       if(!in_array($class_name,$this->FAIclass)){
1219         foreach($class_types as $type){
1220           if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1221             $Abbr .= $type['Abbr']." ";
1222           }
1223         }
1224         $ret[$class_name] = trim($Abbr);
1225       }
1226     }
1227     uksort($ret, 'strnatcasecmp');
1228     return($ret);
1229   }
1232   /* Analyse FAI object and return an array with usefull informations like 
1233    *  FAIobject type.
1234    */
1235   function analyse_fai_object($attr)
1236   {
1237     $tmp = array();
1238     switch($attr['TYPE']){
1240       case 'FAIpackageList':
1241         $tmp["Type"]= 'FAIpackageList';
1242         $tmp["Abbr"]= 'Pl';
1243         break;
1244       case 'FAItemplate': 
1245         $tmp["Type"]= 'FAItemplate'; 
1246         $tmp["Abbr"]= 'T'; 
1247         break;
1248       case 'FAIvariable':
1249         $tmp["Type"]= 'FAIvariable'; 
1250         $tmp["Abbr"]= 'V'; 
1251         break;
1252       case 'FAIscript':
1253         $tmp["Type"]= 'FAIscript'; 
1254         $tmp["Abbr"]= 'S'; 
1255         break;
1256       case 'FAIhook':
1257         $tmp["Type"]= 'FAIhook'; 
1258         $tmp["Abbr"]= 'H'; 
1259         break;
1260       case 'FAIpartitionTable':
1261         $tmp["Type"]= 'FAIpartitionTable'; 
1262         $tmp["Abbr"]= 'Pt'; 
1263         break;
1264       case 'FAIprofile':
1265         $tmp["Type"]= 'FAIprofile'; 
1266         $tmp["Abbr"]= 'P'; 
1267         break;
1268       default: trigger_error("Unknown FAI object type!");;
1269     }
1270     return($tmp);
1271   }
1274   /* Return repository hook output, if possible.
1275    */
1276   function GetHookElements()
1277   {
1278     $ret = array();
1279     $cmd= $this->config->search("servrepository", "REPOSITORY_HOOK",array('tabs'));
1280     if(!empty($cmd)){
1281       $res = shell_exec($cmd);
1282       $res2 = trim($res);
1283       if((!$res)){
1284         msg_dialog(_("Configuration error"), msgPool::cmdexecfailed("REPOSITORY_HOOK", $cmd), ERROR_DIALOG);
1285       }elseif(empty($res2)){
1286         msg_dialog(_("Configuration error"), _("REPOSITORY_HOOK returned no result!"), ERROR_DIALOG);
1287       }else{
1288         $tmp = split("\n",$res);
1289         foreach($tmp as $line){
1290           if(empty($line)) continue;
1291           $ret[]= $line;
1292         }
1293       }
1294     }
1295     return($ret);
1296   }
1299   /* This function creates the release name out of a dn 
1300    *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1301    */
1302   function dn_to_release_name($dn)
1303   {
1304     $relevant = preg_replace("/,".normalizePreg(get_ou("faiou")).".*$/","",$dn);
1305     $parts    = array_reverse(split("\,",$relevant));
1306     $str ="";
1307     foreach($parts as $part){
1308       $str .= preg_replace("/^ou=/","",$part)."/";
1309     }
1310     return(preg_replace("/\/$/","",$str)); 
1311   }
1314 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1315 ?>