Code

Remove the tabs::tabs call from tabs_printers.inc because
[gosa.git] / trunk / 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 = split(" ",$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 = split("\|",$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 = split(":",$attrs[$name][0]);
215             $this->InheritedFAIclass    = split("\ ",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     }
254   }
256   
257   function check()
258   {
259     $messages = array();
260     
261     /* Call common method to give check the hook */
262     $messages= plugin::check();
264     /* If there are packages selected, but no mirror show error */   
265     if(($this->FAIdebianMirror == "none")&&(count($this->FAIclass)>0)){
266       $messages[]=_("Please select a 'FAI server' or remove the 'FAI classes'.");
267     }
269     return($messages);
270   }
272   function execute()
273   {
274         /* Call parent execute */
275         plugin::execute();
277     if($this->is_account && !$this->view_logged){
278       $this->view_logged = TRUE;
279       new log("view","workstation/".get_class($this),$this->dn);
280     }
282     /* Do we represent a valid terminal? */
283     if (!$this->is_account && $this->parent === NULL){
284       $display= "<img alt=\"\" src=\"images/small-error.png\" align=middle>&nbsp;<b>".
285         msgPool::noValidExtension(_("workstation"))."</b>";
286       return ($display);
287     }
289     /* Add module */
290     if (isset ($_POST['add_module'])){
291       if ($_POST['module'] != "" && $this->acl_is_writeable("gotoModules")){
292         $this->add_list ($this->gotoModules, $_POST['module']);
293       }
294     }
296     /* Delete module */
297     if (isset ($_POST['delete_module'])){
298       if (count($_POST['modules_list']) && $this->acl_is_writeable("gotoModules")){
299         $this->del_list ($this->gotoModules, $_POST['modules_list']);
300       }
301     }
303     /* FAI class management */
304     if($this->fai_activated){
305       if(((isset($_POST['AddClass']))&&(isset($_POST['FAIclassesSel']))) && ($this->acl_is_writeable("FAIclass"))){
306         $found = 0 ; 
308         /* If this new class/profile will attach a second partition table
309          * to our list of classes, abort and show a message.
310          */
311         foreach($this->FAIclass as $name){
312           if(isset($this->FAIclassInfo[$name])){
313             foreach($this->FAIclassInfo[$name] as $atr){
314               if(isset($atr['obj'])){
315                 if($atr['obj'] == "FAIpartitionTable"){
316                   $found ++ ; 
317                 }
318               }
319             }
320           }
321         }
323         if((isset($this->FAIclassInfo[$_POST['FAIclassesSel']]['FAIpartitionTable']))&&($found>0)){
324           msg_dialog::display(_("Error"), _("There is already a profile containing a partition table in your configuration!") , ERROR_DIALOG);
325         }else{
326           $this->FAIclass[$_POST['FAIclassesSel']]=$_POST['FAIclassesSel'];
327         }
328       }
330       $sort = false;
332       /* Move one used class class one position up or down */
333       if($this->acl_is_writeable("FAIclass")){
334         foreach($_POST as $name => $val){
336           $sort_type = false;
337           if((preg_match("/sort_up/",$name))&&(!$sort)){
338             $sort_type = "sort_up_";
339           }
340           if((preg_match("/sort_down/",$name))&&(!$sort)){
341             $sort_type = "sort_down_";
342           }
344           if(($sort_type)&&(!$sort)){
345             $value = base64_decode(preg_replace("/_.*$/i","",preg_replace("/".$sort_type."/i","",$name)));
346             $sort = true;
348             $last = -1;
349             $change_down  = -1;
351             /* Create array with numeric index */ 
352             $tmp = array();
353             foreach($this->FAIclass as $class){
354               $tmp [] = $class;
355             }
357             /* Walk trough array */
358             foreach($tmp as $key => $faiName){
359               if($faiName == $value){
360                 if($sort_type == "sort_up_"){
361                   if($last != -1){
362                     $change_down= $last;
363                   }
364                 }else{
365                   if(isset($tmp[$key+1])){
366                     $change_down = $key;
367                   }
368                 }
369               }
370               $last = $key;
371             }
373             $tmp2 = array();
374             $skip = false;    
376             foreach($tmp as $ky => $vl){
378               if($ky == $change_down){
379                 $skip = $vl;
380               }else{
381                 $tmp2[$vl] = $vl;
382               }
383               if(($skip != false)&&($ky != $change_down)){
384                 $tmp2[$skip]  = $skip;
385                 $skip =false;
386               }
387             }   
388             $this->FAIclass = $tmp2; 
389           }
391           if(preg_match("/fai_remove/i",$name)){
392             $value = base64_decode(preg_replace("/_.*$/i","",preg_replace("/fai_remove_/i","",$name)));
393             unset($this->FAIclass[$value]);
394           }
395         }
396       }
398       /* Delete selected class from our list */
399       if($this->acl_is_writeable("FAIclass")){
400         if((isset($_POST['DelClass']))&&(isset($_POST['FAIclassSel']))){
401           if(isset($this->FAIclass[$_POST['FAIclassSel']])){
402             unset($this->FAIclass[$_POST['FAIclassSel']]);
403           }
404         }
405       }
406     }// END fai handling
408     /* Show main page */
409     $smarty= get_smarty();
411     /* Assign ACLs to smarty */
412     $tmp = $this->plInfo();
413     foreach($tmp['plProvidedAcls'] as $name => $translation){
414       $smarty->assign($name."ACL",$this->getacl($name));
415     } 
417     $smarty->assign("member_of_ogroup",$this->member_of_ogroup);
419     /* In this section server shares will be defined
420      * A user can select one of the given shares and a mount point
421      *  and attach this combination to his setup.
422      */
423     $smarty->assign("gotoShareSelections",    $this->gotoShareSelections);
424     $smarty->assign("gotoShareSelectionKeys", array_flip($this->gotoShareSelections));
426     /* if $_POST['gotoShareAdd'] is set, we will try to add a new entry
427      * This entry will be, a combination of mountPoint and sharedefinitions
428      */
429     if((isset($_POST['gotoShareAdd'])) && isset($_POST['gotoShareSelection']) && ($this->acl_is_writeable("gotoShare"))) {
430       /* We assign a share to this user, if we don't know where to mount the share */
431       if((!isset($_POST['gotoShareMountPoint']))||(empty($_POST['gotoShareMountPoint']))||(preg_match("/[\|]/i",$_POST['gotoShareMountPoint']))){
432         msg_dialog::display(_("Error"), msgPool::required(_("Mount point")), ERROR_DIALOG);
433       }else{
434     
435         if(isset($this->gotoAvailableShares[$_POST['gotoShareSelection']])){
436           $a_share = $this->gotoAvailableShares[$_POST['gotoShareSelection']];
437           $s_mount = $_POST['gotoShareMountPoint'];
438           /* Preparing the new assignment */
439           $this->gotoShares[$a_share['name']."|".$a_share['server']]=$a_share;
440           $this->gotoShares[$a_share['name']."|".$a_share['server']]['mountPoint']=$s_mount;
441         }
442       }
443     }
445     /* if the Post  gotoShareDel is set, someone asked GOsa to delete the selected entry (if there is one selected)
446      * If there is no defined share selected, we will abort the deletion without any message
447      */
448     if(($this->acl_is_writeable("gotoShare"))&& (isset($_POST['gotoShareDel']))&&(isset($_POST['gotoShare']))){
449       unset($this->gotoShares[$_POST['gotoShare']]);
450     }
452     $smarty->assign("gotoShares",$this->printOutAssignedShares());
453     $smarty->assign("gotoSharesCount",count($this->printOutAssignedShares()));
454     $smarty->assign("gotoShareKeys",array_flip($this->printOutAssignedShares()));
455     $smarty->assign("gotoBootKernels",$this->gotoBootKernels);
457     /* Create divSelectBox for ldap server selection
458      */
459     $SelectBoxLdapServer = new divSelectBox("LdapServer");
460     $SelectBoxLdapServer->SetHeight(130);
462     /* Add new ldap server to the list */
463     if($this->acl_is_writeable("gotoLdapServer") && 
464         !$this->gotoLdap_inherit && 
465         isset($_POST['add_ldap_server']) && 
466         isset($_POST['ldap_server_to_add'])){
467       if(isset($this->gotoLdapServerList[$_POST['ldap_server_to_add']])){
468         $to_add = $this->gotoLdapServerList[$_POST['ldap_server_to_add']];
469         if(!in_array($to_add,$this->gotoLdapServers)){
470           $this->gotoLdapServers[] = $to_add;
471         }
472       }
473     }
474     
475     /* Move ldap servers up and down */
476     if(!$this->gotoLdap_inherit && $this->acl_is_writeable("gotoLdapServer")){
477       foreach($_POST as $name => $value){
478         if(preg_match("/sort_ldap_up_/",$name)){
479           $id = preg_replace("/^sort_ldap_up_([0-9]*)_(x|y)$/","\\1",$name);
480           $from =  $id;  
481           $to   =  $id -1;
482           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
483           if($tmp){
484             $this->gotoLdapServers = $tmp;
485           }
486           break;
487         }
488         if(preg_match("/sort_ldap_down_/",$name)){
489           $id = preg_replace("/^sort_ldap_down_([0-9]*)_(x|y)$/","\\1",$name);
490           $from =  $id;  
491           $to   =  $id +1;
492           $tmp = $this->array_switch_item($this->gotoLdapServers,$from,$to);
493           if($tmp){
494             $this->gotoLdapServers = $tmp;
495           }
496           break;
497         }
498         if(preg_match("/gotoLdapRemove_/",$name)){
499           $id = preg_replace("/^gotoLdapRemove_([0-9]*)_(x|y)$/","\\1",$name);
500           $value = $this->gotoLdapServers[$id];
501           $this->gotoLdapServers = array_remove_entries(array($value),$this->gotoLdapServers);
502           break;
503         }
504       } 
505     }
506   
507     /* Add Entries */
508     if($this->acl_is_readable("gotoLdapServer")){
510       foreach($this->gotoLdapServers as $key => $server){
512         /* Announce missing entries */
513         if(!in_array($server,$this->gotoLdapServerList)){
514           $server = $server."&nbsp;<font style='color:red'>(missing)</font>";
515         }
517         /* Convert old style entry */
518         if (!preg_match('%:ldaps?://%', $server)){
519           $server= "ldap://".preg_replace('/^([^:]+):/', '\1/', $server);
521         /* Beautify new style entries */
522       } else {
523         $server= preg_replace("/^[^:]+:/", "", $server);
524       }
526       $SelectBoxLdapServer->AddEntry(
527           array(array("string" => $server),
528             array("string" => 
529               "<input class='center' type='image' src='images/lists/sort-up.png' name='sort_ldap_up_".$key."'>&nbsp;".
530               "<input class='center' type='image' src='images/lists/sort-down.png' name='sort_ldap_down_".$key."'>&nbsp;".
531               "<input class='center' type='image' src='images/lists/trash.png' name='gotoLdapRemove_".$key."'>",
532               "attach" => "style='text-align:right;width:40px;border-right:0px;'")));
533       }    
534     }    
536     if($this->gotoLdap_inherit){
537       $smarty->assign("gotoLdapServerACL_inherit", preg_replace("/w/","",$this->getacl("gotoLdapServer")));;
538     }else{
539       $smarty->assign("gotoLdapServerACL_inherit", $this->getacl("gotoLdapServer"));
540     }
541     
542     $list = array();
543     foreach($this->gotoLdapServerList as $key => $entry){
544       if(!in_array($entry,$this->gotoLdapServers)){
546         /* Convert old style entry */
547         if (!preg_match('%:ldap[s]*://%', $entry)){
548           $entry= "ldap://".preg_replace('/^([^:]+):/', '\1/', $entry);
550         /* Beautify new style entries */
551         } else {
552           $entry= preg_replace("/^[^:]+:/", "", $entry);
553         }
555         $list[$key] = $entry;
556       }
557     }
558     $smarty->assign("gotoLdapServers",    $SelectBoxLdapServer->DrawList());
559     $smarty->assign("gotoLdapServerList", $list);
560     $smarty->assign("gotoLdap_inherit",   $this->gotoLdap_inherit);
561     $smarty->assign("JS",  session::get('js'));
563     foreach (array("gotoModules", "gotoAutoFs", "gotoFilesystem") as $val){
564       $smarty->assign("$val", $this->$val);
565     }
567     /* Values */
568     foreach(array("gotoBootKernel","gotoShare","FAIclasses","FAIclass","FAIdebianMirror","FAIrelease") as $val){
569       $smarty->assign($val, $this->$val);
570     }
572     $smarty->assign("fai_activated",$this->fai_activated);
574     /* Create FAI output */
575     $this->update_fai_cache();
576     $smarty->assign("si_fai_action_failed",$this->si_fai_action_failed);
577     $smarty->assign("si_active",$this->si_active);
579     $div = new divSelectBox("WSFAIscriptClasses");
580     $div -> SetHeight("110");
581   
582     if(!$this->si_fai_action_failed && $this->si_active && $this->fai_activated){
584       $smarty->assign("FAIservers"  , $this->cache['SERVERS']);
585       $smarty->assign("FAIdebianMirror",$this->FAIdebianMirror);
586       $smarty->assign("FAIrelease"  , $this->FAIrelease);
587       $smarty->assign("FAIclasses"  , $this->selectable_classes());
589       /* Get classes for release from cache.
590        * Or build cache
591        */
592       if($this->FAIdebianMirror == "inherited"){
593         $release = $this->InheritedFAIrelease;
594       }else{
595         $release = $this->FAIrelease;
596       }
598       $smarty->assign("gotoBootKernels",$this->cache['KERNELS'][$release]);
599       $smarty->assign("InheritedFAIrelease",$this->InheritedFAIrelease);
601       $str_empty  = " &nbsp;<img src='images/empty.png' alt=\"\" width='7'>"; 
602       if($this->acl_is_writeable("FAIclass")){
603         $str_up     = " &nbsp;<input type='image' src='images/lists/sort-up.png'    name='sort_up_%s'    value='%s'>";
604         $str_down   = " &nbsp;<input type='image' src='images/lists/sort-down.png'  name='sort_down_%s'  value='%s'>";
605         $str_remove = " &nbsp;<input type='image' src='images/lists/trash.png'  name='fai_remove_%s' value='%s'>";
606       }else{
607         $str_up=$str_down=$str_remove=$str_empty;
608       }
610       /* Get classes */
611       if($this->FAIdebianMirror == "inherited"){
612         $tmp = $this->InheritedFAIclass;
613       }else{
614         $tmp = $this->FAIclass;
615       }
617       /* Get invalid classes */
618       $invalid = $this->get_invalid_classes($tmp);
620       /* Draw every single entry */
621       $i = 1;
622       if($this->acl_is_readable("FAIclass")){
623         foreach($tmp as $class){
625           /* Mark invalid classes. (Not in selected release)
626            */
627           $marker = "";
628           if(in_array_ics($class,$invalid)){
629             $marker = "&nbsp;<font color='red'>("._("Not available in current setup").")</font>";
630           }
632           /* Create up/down priority icons  
633            * Skip this, if we have inherited the FAI classes.
634            */
635           if($this->FAIdebianMirror == "inherited"){
636             $str = "";
637           }else{
638             if($i==1){
639               $str = $str_empty.$str_down.$str_remove;
640             }elseif($i == count($this->FAIclass)){
641               $str = $str_up.$str_empty.$str_remove;
642             }else{
643               $str = $str_up.$str_down.$str_remove;
644             }
645           }
646           $i ++ ; 
648           /* Get Description tag 
649            *  There may be several FAI objects with the same class name, 
650            *   use the description from FAIprofile, if possible.
651            */  
652           $desc = ""; 
653           if(isset($this->cache['CLASSES'][$this->FAIrelease][$class])){
654             foreach($this->cache['CLASSES'][$this->FAIrelease][$class] as $types ){
655               if(isset($types['Desc'])){
656                 $desc= $types['Desc'];
657                 if($types['Type'] == "FAIprofile"){
658                   break;
659                 }
660               }
661             }
662           }
663           if(!empty($desc)){
664             $desc = "&nbsp;[".trim($desc)."]";
665           }        
667           $div->AddEntry(array(
668                 array("string"=>$class.$desc.$marker),
669                 array("string"=>preg_replace("/\%s/",base64_encode($class),$str),"attach"=>"style='width:50px;border-right:none;'")
670                 ));
671         }  
672       }// END FAI output generation 
673     }// END FAI output generation 
674     $smarty->assign("FAIScriptlist",$div->DrawList()); 
676     /* Radio button group */
677     if (preg_match("/G/", $this->bootmode)) {
678       $smarty->assign("graphicalbootup", "checked");
679     } else {
680       $smarty->assign("graphicalbootup", "");
681     }
682     if (preg_match("/T/", $this->bootmode)) {
683       $smarty->assign("textbootup", "checked");
684     } else {
685       $smarty->assign("textbootup", "");
686     }
687     if (preg_match("/D/", $this->bootmode)) {
688       $smarty->assign("debugbootup", "checked");
689     } else {
690       $smarty->assign("debugbootup", "");
691     }
693     /* Show main page */
694     $smarty->assign("gotoKernelParameters",$this->gotoKernelParameters);
695     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
696   }
699   function remove_from_parent()
700   {
701     $this->handle_post_events("remove");
702     new log("remove","workstation/".get_class($this),$this->dn);
703   }
706   /* Save data to object */
707   function save_object()
708   {
709     $old_mirror  = $this->FAIdebianMirror;
710     plugin::save_object();
712     /* Update release */
713     if($old_mirror != $this->FAIdebianMirror){
714       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
715         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
716       }
717     }
719     if(isset($_POST['WorkstationStarttabPosted'])){
720       if(isset($_POST['gotoLdap_inherit'])){
721         $this->gotoLdap_inherit = TRUE;
722       }else{
723         $this->gotoLdap_inherit = FALSE;
724       }
726       /* Save group radio buttons */
727       if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
728         $this->bootmode= $_POST["bootmode"];
729       }
730     }
731   }
734   /* Save to LDAP */
735   function save()
736   {
738     /* Depending on the baseobject (Ogroup / WS) we
739      *  use another set of objectClasses
740      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
741      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
742      */
743     if(isset($this->parent->by_object['ogroup'])){
744       $this->objectclasses = array("gotoWorkstationTemplate");
745     }elseif(isset($this->parent->by_object['workgeneric'])){
746       $this->objectclasses = array("GOhard");
747     }elseif(isset($this->parent->by_object['servgeneric'])){
748       $this->objectclasses = array("GOhard","gotoWorkstationTemplate");
749     }else{
750       msg_dialog::display(_("Fatal error"),
751           "Object Type Configuration is unknown. Please contact the GOsa developers.",
752           FATAL_ERROR_DIALOG);
753       exit();
754     }
756     /* Append FAI class */
757     if($this->fai_activated){
758       $this->objectclasses[]  = "FAIobject";
759     }
761     /* Find proper terminal path for tftp configuration
762        FIXME: This is suboptimal when the default has changed to
763        another location! */
764     if (($this->gotoTerminalPath == "default")){
765       $ldap= $this->config->get_ldap_link();
767       /* Strip relevant part from dn, keep trailing ',' */
768       $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalRDN')."/i", "", $this->dn);
769       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
771       /* Walk from top to base and try to load default values for
772          'gotoTerminalPath'. Abort when an entry is found. */
773       while (TRUE){
774         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
776         $ldap->cat("cn=default,".get_ou('terminalRDN').$tmp.
777             $this->config->current['BASE'], array('gotoTerminalPath'));
778         $attrs= $ldap->fetch();
779         if (isset($attrs['gotoTerminalPath'])){
780           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
781           break;
782         }
784         /* Nothing left? */
785         if ($tmp == ""){
786           break;
787         }
788       }
789     }
791     plugin::save();
793     unset( $this->attrs['FAIrelease'] );
794     $str = "";
796     /* Skip FAI attribute handling if not necessary */
797     if($this->fai_activated && !$this->si_fai_action_failed){
798       if($this->FAIdebianMirror == "inherited"){
799         $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array();
800       }else{
801         foreach($this->FAIclass as $class){
802           $str .= $class." ";
803         }
804         $str = trim($str);
805         if(empty($this->attrs['FAIclass'])){
806           $this->attrs['FAIclass'] = array();
807         }else{
808           $this->attrs['FAIclass']= $str." :".$this->FAIrelease;
809         }
810       }
811     }
813     /* Add missing arrays */
814     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
815       if (isset ($this->$val) && count ($this->$val) != 0){
816     
817         $this->attrs["$val"]= array_unique($this->$val);
818       }
819       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
820     }
822     /* Prepare list of ldap servers */
823     $this->attrs['gotoLdapServer'] = array();
824     if(!$this->gotoLdap_inherit){
825       $i = 0;
826       foreach($this->gotoLdapServers as $server){
827         $i ++;
828         $this->attrs['gotoLdapServer'][] = $i.":".$server;
829       }
830     }
832     if ($this->attrs['gotoBootKernel'] == "default-inherited"){
833       $this->attrs['gotoBootKernel']= array();
834     }
836     /* if mirror == none stop saving this attribute */
837     if($this->FAIdebianMirror == "none"){
838       $this->FAIdebianMirror = "";
839     }
840    
841     /* Get FAIstate from object, the generic tab could have changed it during execute */
842     $ldap= $this->config->get_ldap_link();
843     $ldap->cd($this->dn);
846     /* Skip FAI attribute handling if not necessary */
847     if($this->fai_activated && !$this->si_fai_action_failed && $this->si_active){
848       $ldap->cat($this->dn,array("FAIstate"));
849       $checkFAIstate = $ldap->fetch();
851       /* Remove FAI objects if no FAI class is selected */ 
852       if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
853         $this->attrs['FAIclass']        = array();
854         $this->attrs['FAIdebianMirror'] = array();
855       }
856     }else{
858       /* Don't touch FAI objects if something went wrong with the si daemon.
859        */
860       if(isset($this->attrs['FAIclass'])) unset($this->attrs['FAIclass']);
861       if(isset($this->attrs['FAIdebianMirror'])) unset($this->attrs['FAIdebianMirror']);
862     }
864     /* prepare share settings */
865     $tmp = array();
866     foreach($this->gotoShares as $name => $settings){
867       $tmp2= split("\|",$name);
868       $name = $tmp2[0];
869       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
870     }
871     $this->attrs['gotoShare']=$tmp;
872     $this->cleanup();
873     $ldap->modify ($this->attrs); 
874     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
876     if (!$ldap->success()){
877       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
878     }
879     $this->handle_post_events("modify");
881     /* Check if LDAP server has changed */
882     if ((isset($this->attrs['gotoLdapServer']) && class_available("DaemonEvent")) || $this->gotoLdap_inherit){
883       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
884       $o_queue = new gosaSupportDaemon();
885       if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
886         $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
887         $macs = array();
888     
889         /* Get list of macAddresses 
890          */
891         if(isset($this->parent->by_object['ogroup'])){
892         
893           /* If we are an object group, add all member macs 
894            */
895           $p = $this->parent->by_object['ogroup'];
896           foreach($p->memberList as $dn => $obj){
897             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
898               $macs[] = $p->objcache[$dn]['macAddress'];
899             }
900           }
901         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
903           /* We are a workstation. Add current mac.
904            */
905           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
906           if(!empty($mac)){
907             $macs[] = $mac;
908           }          
909         }elseif(isset($this->parent->by_object['servgeneric']->netConfigDNS->macAddress)){
911           /* We are a server. Add current mac.
912            */
913           $mac = $this->parent->by_object['servgeneric']->netConfigDNS->macAddress;
914           if(!empty($mac)){
915             $macs[] = $mac;
916           }          
917         }
919         /* Trigger event for all member objects 
920          */
921         foreach($macs as $mac){
922           $tmp = new $evt['CLASS_NAME']($this->config);
923           $tmp->set_type(TRIGGERED_EVENT);
924           $tmp->add_targets(array($mac));
925           if(!$o_queue->append($tmp)){
926             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
927           }
928         }
929       }
930     }
931   }
934   /* Add value to array, check if unique */
935   function add_list (&$array, $value)
936   {
937     if ($value != ""){
938       $array[]= $value;
939       sort($array);
940       array_unique ($array);
941     }
942   }
945   /* Delete value to array, check if unique */
946   function del_list (&$array, $list)
947   {
948     $tmp= array();
949     foreach ($array as $mod){
950       if (!in_array($mod, $list)){
951         $tmp[]= $mod;
952       }
953     }
954     $array= $tmp;
955   }
957   /* Generate ListBox frindly output for the defined shares
958    * Possibly Add or remove an attribute here,
959    */
960   function printOutAssignedShares()
961   {
962     $a_return = array();
963     if(is_array($this->gotoShares)){
964       foreach($this->gotoShares as $share){
965         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
966       }
967     }
968     return($a_return);
969   }
973   function PrepareForCopyPaste($source)
974   {
975     plugin::PrepareForCopyPaste($source);    
976     $source_o = new workstartup ($this->config, $source['dn']);
977     foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
978           "gotoKernelParameters","gotoShares") as $attr){
979       $this->$attr = $source_o->$attr;
980     }
981   }
983   
984   function array_switch_item($ar,$from,$to)
985   {
986     if(!is_array($ar)){
987       return(false);
988     }
989     if(!isset($ar[$from])){
990       return(false);
991     }
992     if(!isset($ar[$to])){
993       return(false);
994     }
996     $tmp = $ar[$from];
997     $ar[$from] = $ar[$to];    
998     $ar[$to] = $tmp;    
999     return($ar);
1000   }
1003   /* Return plugin informations for acl handling */ 
1004   static function plInfo()
1005   {
1006     return (array( 
1007           "plShortName"   => _("Startup"),
1008           "plDescription" => _("System startup"),
1009           "plSelfModify"  => FALSE,
1010           "plDepends"     => array(),
1011           "plPriority"    => 9,
1012           "plSection"     => array("administration"),           
1013           "plCategory"    => array("workstation","server","ogroups"),
1015           "plProvidedAcls"=> array(
1016             "gotoLdapServer"        => _("Ldap server"),
1017             "gotoBootKernel"        => _("Boot kernel"),
1018             "gotoKernelParameters"  => _("Kernel parameter"),
1020             "gotoModules"           => _("Kernel modules"),
1021             "gotoShare"             => _("Shares"),
1023             "FAIclass"              => _("FAI classes"),
1024             "FAIdebianMirror"       => _("Debian mirror"),
1025             "FAIrelease"            => _("Debian release"),
1027             "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
1028           ));
1029   }
1032   /* Updates release dns 
1033    *  and reads all classes for the current release, 
1034    *  if not already done ($this->cache).
1035    */
1036   function update_fai_cache($first_call = FALSE)
1037   {
1038     $force = FALSE;
1039     if(!$this->si_active) return; 
1040     $start = microtime(TRUE);  
1042     if($this->si_fai_action_failed && !isset($_POST['fai_si_retry'])) return;
1044     $this->si_fai_action_failed = FALSE;
1046     /* Get the list of available servers and their releases. 
1047      */
1048     if($force || !isset($this->cache['SERVERS'])){
1050       $o_queue = new gosaSupportDaemon();
1051       $tmp = $o_queue->FAI_get_server();
1052       if($o_queue->is_error()){
1053         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1054         $this->si_fai_action_failed = TRUE;
1055         $this->cache = array();
1056         return;
1057       }else{
1059         foreach($tmp as $entry){
1060           $rel = $entry['FAI_RELEASE'];
1061           $this->cache['SERVERS']['auto'][$rel] = $rel;
1062           $this->cache['SERVERS'][$entry['SERVER']][$rel] = $rel;
1063           uksort($this->cache['SERVERS']['auto'], 'strnatcasecmp');
1064           uksort($this->cache['SERVERS'][$entry['SERVER']], 'strnatcasecmp');
1065         }
1066       }
1067     }
1069     /* Ensure that our selection is valid, else we get several PHP warnings 
1070         if there is no FAI configuration at all.
1071      */
1072     if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1073       $this->cache['SERVERS'][$this->FAIdebianMirror][''] ='';
1074     }
1076     /* Build up arrays, without checks */
1077     if(!$first_call){
1079       /* Check if the selected mirror is available */
1080       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1081         $this->FAIdebianMirror = "auto";
1082         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1083         trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
1084       }
1086       /* Check if the selected release is available */
1087       if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
1089         if($this->FAIrelease != ""){
1090           trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1091         }
1092         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1093       }
1094     }
1096     /* Get classes for release from cache. 
1097      * Or build cache
1098      */
1099     if($this->FAIdebianMirror == "inherited"){
1100       $release = $this->InheritedFAIrelease;
1101     }else{
1102       $release = $this->FAIrelease;
1103     }
1105     if($force || !isset($this->cache['CLASSES'][$release]) && $release != ""){
1107       /* Get the list of available servers and their releases.
1108        */
1109       $o_queue = new gosaSupportDaemon();
1110       $tmp = $o_queue->FAI_get_classes($release);
1111       $this->cache['CLASSES'][$release] = array();
1112       if($o_queue->is_error()){
1113         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1114         $this->si_fai_action_failed = TRUE;
1115         $this->cache=array();
1116         return;
1117       }else{
1118         foreach($tmp as $entry){
1119           $class = $entry['CLASS'];
1120           $this->cache['CLASSES'][$release][$class] = $this->analyse_fai_object($entry); 
1121         }
1122       }
1124       /* Add object caught from external hook
1125        */
1126       $lines= $this->GetHookElements();
1127       foreach ($lines as $hline){
1128         $entries= split(";", $hline);
1129         $server = $entries['0'];
1130         $url    = $entries['1'];
1131         if (!empty($url)){
1133           /* Split releases */
1134           if (isset($entries[2])){
1135             $releases= split(",", $entries[2]);
1137             foreach ($releases as $release_data){
1138               $release_c  = preg_replace('/:.*$/', '', $release_data);
1139               $sections_c = split(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1140               $classes_c  = split('\|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1142               if($release_c == $release){
1143                 $this->cache['SERVERS'][$url][$release_c]=$release_c;
1144                 $this->cache['SERVERS']['auto'][$release_c]=$release_c; 
1145                 foreach ($classes_c as $class){
1146                   if ($class != ""){
1147                     $this->cache['CLASSES'][$release_c][$class]= array();
1148                   }
1149                 }
1150               }
1151             }
1152           }
1153         }
1154       }
1155       uksort($this->cache['SERVERS'], 'strnatcasecmp');
1157       /* Only add inherit option, if we are part in an object group
1158        */
1159       if($this->member_of_ogroup){
1160         $this->cache['SERVERS'] = array_merge(array('inherited' => array()),$this->cache['SERVERS']);
1161       }
1162     }
1164     /* Get list of available kernel for this release 
1165      */
1166     if(!isset($this->cache['KERNELS'])) $this->cache['KERNELS'] = array();
1168     if($force || !isset($this->cache['KERNELS'][$release])){
1169       $o_queue = new gosaSupportDaemon();
1170       $tmp = $o_queue->FAI_get_kernels($release);
1171       $this->cache['KERNELS'][$release] = array();
1172       foreach($this->gotoBootKernels as $name => $default){
1173         $this->cache['KERNELS'][$release][$name] = $default;
1174       }
1175       foreach($tmp as $kernel){
1176         if(empty($kernel)) continue;
1177         $this->cache['KERNELS'][$release][$kernel]=$kernel;
1178       }
1179       ksort($this->cache['KERNELS'][$release]);
1180     }
1181   }
1184   /* This function return an array containing all 
1185    *  invalid classes for the selected server/release
1186    */
1187   function get_invalid_classes($classes)
1188   {
1189     $this->update_fai_cache();
1190     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1191       $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1192     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1193       $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1194     }else{
1195       $release_classes = array();
1196     }
1199     /* Detect all classes that are not valid 
1200      *  for the selected release 
1201      */
1202     $NA = array();
1203     foreach($classes as $class){
1204       if(!isset($release_classes[$class])){
1205         $NA[] = $class;
1206       }
1207     }
1208     return($NA);
1209   }  
1211   
1212   /* Get all selectable classes for the ui select box
1213    */
1214   function selectable_classes()
1215   {
1216     $this->update_fai_cache();
1218     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1219       $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1220     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1221       $classes = $this->cache['CLASSES'][$this->FAIrelease];
1222     }else{
1223       $classes = array();
1224     }
1226     $Abbr ="";
1227     $ret= array();
1228     foreach($classes as $class_name => $class_types){
1229       if(!in_array($class_name,$this->FAIclass)){
1230         foreach($class_types as $type){
1231           if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1232             $Abbr .= $type['Abbr']." ";
1233           }
1234         }
1235         $ret[$class_name] = trim($Abbr);
1236       }
1237     }
1238     uksort($ret, 'strnatcasecmp');
1239     return($ret);
1240   }
1243   /* Analyse FAI object and return an array with usefull informations like 
1244    *  FAIobject type.
1245    */
1246   function analyse_fai_object($attr)
1247   {
1248     $tmp = array();
1249     switch($attr['TYPE']){
1251       case 'FAIpackageList':
1252         $tmp["Type"]= 'FAIpackageList';
1253         $tmp["Abbr"]= 'Pl';
1254         break;
1255       case 'FAItemplate': 
1256         $tmp["Type"]= 'FAItemplate'; 
1257         $tmp["Abbr"]= 'T'; 
1258         break;
1259       case 'FAIvariable':
1260         $tmp["Type"]= 'FAIvariable'; 
1261         $tmp["Abbr"]= 'V'; 
1262         break;
1263       case 'FAIscript':
1264         $tmp["Type"]= 'FAIscript'; 
1265         $tmp["Abbr"]= 'S'; 
1266         break;
1267       case 'FAIhook':
1268         $tmp["Type"]= 'FAIhook'; 
1269         $tmp["Abbr"]= 'H'; 
1270         break;
1271       case 'FAIpartitionTable':
1272         $tmp["Type"]= 'FAIpartitionTable'; 
1273         $tmp["Abbr"]= 'Pt'; 
1274         break;
1275       case 'FAIprofile':
1276         $tmp["Type"]= 'FAIprofile'; 
1277         $tmp["Abbr"]= 'P'; 
1278         break;
1279       default: trigger_error("Unknown FAI object type!");;
1280     }
1281     return($tmp);
1282   }
1285   /* Return repository hook output, if possible.
1286    */
1287   function GetHookElements()
1288   {
1289     $ret = array();
1290     $cmd= $this->config->search("servrepository", "repositoryBranchHook",array('tabs'));
1291     if(!empty($cmd)){
1292       $res = shell_exec($cmd);
1293       $res2 = trim($res);
1294       if((!$res)){
1295         msg_dialog::display(_("Configuration error"), msgPool::cmdexecfailed("repositoryBranchHook", $cmd), ERROR_DIALOG);
1296       }elseif(empty($res2)){
1297         msg_dialog::display(_("Configuration error"), _("'repositoryBranchHook' returned no result!"), ERROR_DIALOG);
1298       }else{
1299         $tmp = split("\n",$res);
1300         foreach($tmp as $line){
1301           if(empty($line)) continue;
1302           $ret[]= $line;
1303         }
1304       }
1305     }
1306     return($ret);
1307   }
1310   /* This function creates the release name out of a dn 
1311    *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1312    */
1313   function dn_to_release_name($dn)
1314   {
1315     $relevant = preg_replace("/,".preg_quote(get_ou("faiBaseRDN"), '/').".*$/","",$dn);
1316     $parts    = array_reverse(split("\,",$relevant));
1317     $str ="";
1318     foreach($parts as $part){
1319       $str .= preg_replace("/^ou=/","",$part)."/";
1320     }
1321     return(preg_replace("/\/$/","",$str)); 
1322   }
1325 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1326 ?>