Code

6d2c6bb2c64dcca6ce4868ccd5a178b588cef9a8
[gosa.git] / gosa-plugins / goto / admin / systems / goto / class_workstationStartup.inc
1 <?php
2 class workstartup extends plugin
3 {
4   /* Ldap server list */
5   var $gotoLdapServers    = array();
6   var $gotoLdapServerList = array();
7   var $gotoLdap_inherit   = FALSE;
9   /* Generic terminal attributes */
10         # This is disabled as long there is no possiblilty to set these parameters
11   #var $bootmode             = "G";
12   var $bootmode             = "";
13   var $gotoBootKernel       = "default-inherited";
14   var $gotoKernelParameters = "";
15   var $gotoLdapServer       = "default-inherited";
16   var $gotoModules          = array();
17   var $gotoAutoFs           = array();
18   var $gotoFilesystem       = array();
19   var $gotoTerminalPath     = "";
20   var $gotoBootKernels      = array();
22   /* attribute list for save action */
23   var $attributes           = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", 
24                                     "FAIclass", "FAIstatus", "gotoShare","FAIdebianMirror", "FAIrelease");
25   var $objectclasses        = array("GOhard", "FAIobject");
27   /* Share */
28   var $gotoShares         = array();// Currently Share Option
29   var $gotoShare          = "";     // currently selected Share Option
30   var $gotoShareSelections= array();// Available Shares for this account in Listbox format
31   var $gotoAvailableShares= array();// Available Shares for this account
33   /* Helper */
34   var $orig_dn            = "";
35   var $ignore_account     = TRUE;
36  
37   /* FAI class selection */ 
38   var $FAIclass           = array();  // The currently selected classes 
39   var $FAIrelease           = "";
40   var $FAIdebianMirror      = "auto";
41   var $si_active            = FALSE;
42   var $si_fai_action_failed = FALSE;
44   var $cache              = array(); // Used as cache in fai mehtods
46   var $FAIstatus          = "";
47   var $FAIclasses         = array();
49   var $view_logged        = FALSE;
50   
51   /* FAI class selection */
52   var $InheritedFAIclass       = array();
53   var $InheritedFAIrelease     = "";
54   var $InheritedFAIdebianMirror= "auto";
56   var $CopyPasteVars    = array("gotoModules","gotoShares");
57   var $fai_activated    = FALSE;
58   var $o_group_dn       = "";
59   var $member_of_ogroup = FALSE;
61   function workstartup (&$config, $dn= NULL, $parent= NULL)
62   {
63     /* Check if FAI is active */
64     $tmp= $config->search("faiManagement", "CLASS",array('menu','tabs'));
65     if(!empty($tmp) && class_available("faiManagement")){
66       $this->fai_activated = TRUE;
67     }else{
68       $this->attributes = array("gotoLdapServer", "gotoBootKernel", "gotoKernelParameters", "gotoShare");
69       $this->objectclasses  = array("GOhard");
70     }
72     plugin::plugin ($config, $dn, $parent);
74     /* Check for si daemon */
75     $this->si_active = $this->config->get_cfg_value("gosaSupportURI") != "";
77     /* Check object group membership */
78     if(!isset($this->parent->by_object['ogroup'])){
79       $ldap = $this->config->get_ldap_link();
80       $ldap->cd ($this->config->current['BASE']);
81       $ldap->search("(&(objectClass=gotoWorkstationTemplate)(member=".LDAP::prepare4filter($this->dn)."))",array("cn","dn"));
82       if($ldap->count()){
83         $this->member_of_ogroup = TRUE;
84         $attrs = $ldap->fetch();
85         $this->o_group_dn = $attrs['dn'];
86       }
87     }
89     /* Creating a list of valid Mirrors 
90      * none will not be saved to ldap.
91      */
92     $ldap   = $this->config->get_ldap_link();
93     $ldap->cd($this->config->current['BASE']);
94     $ui = get_userinfo();
95     foreach($this->config->data['SERVERS']['LDAP'] as $dn => $data){
96       if($ui->get_category_permissions($data['dn'],"server",TRUE)){
97         for($i = 0; $i < $data['goLdapBase']['count']; $i ++){
98           $name = $data["cn"][0].":".$data["goLdapBase"][$i];
99           $this->gotoLdapServerList[]= $name; 
100         }
101       }
102     }
104     /* Get list of assigned ldap servers 
105      */ 
106     if(isset($this->attrs['gotoLdapServer'])){
107       unset($this->attrs['gotoLdapServer']['count']);
108       sort($this->attrs['gotoLdapServer']);
109       foreach($this->attrs['gotoLdapServer'] as $value){
110         $this->gotoLdapServers[] = preg_replace("/^[0-9]*:/","",$value);
111       }
112     } 
113     natcasesort($this->gotoLdapServerList);
115     if(!count($this->gotoLdapServers) && $this->member_of_ogroup){ 
116       $this->gotoLdap_inherit = TRUE;
117     }
119     /* FAI Initialization
120        Skip this if FAI is not activated 
121      */
122     if($this->fai_activated) {
124       /* Parse used FAIclasses (stored as string).
125        * The single classes are seperated by ' '.
126        * There is also the release type given, after first
127        *  occurrence of ':'.
128        */
129       $this->FAIclass =array();
130       if(isset($this->attrs['FAIclass'][0])){
131         $tmp = 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             $desc ="";
655             foreach($this->cache['CLASSES'][$this->FAIrelease][$class] as $types ){
656               if(isset($types['Abbr'])){
657                 $desc.= $types['Abbr']." ";
658                 if($types['Type'] == "FAIprofile"){
659                   break;
660                 }
661               }
662             }
663           }
664           if(!empty($desc)){
665             $desc = "&nbsp;[".trim($desc)."]";
666           }        
668           $div->AddEntry(array(
669                 array("string"=>$class.$desc.$marker),
670                 array("string"=>preg_replace("/\%s/",base64_encode($class),$str),"attach"=>"style='width:50px;border-right:none;'")
671                 ));
672         }  
673       }// END FAI output generation 
674     }// END FAI output generation 
675     $smarty->assign("FAIScriptlist",$div->DrawList()); 
677     /* Radio button group */
678     if (preg_match("/G/", $this->bootmode)) {
679       $smarty->assign("graphicalbootup", "checked");
680     } else {
681       $smarty->assign("graphicalbootup", "");
682     }
683     if (preg_match("/T/", $this->bootmode)) {
684       $smarty->assign("textbootup", "checked");
685     } else {
686       $smarty->assign("textbootup", "");
687     }
688     if (preg_match("/D/", $this->bootmode)) {
689       $smarty->assign("debugbootup", "checked");
690     } else {
691       $smarty->assign("debugbootup", "");
692     }
694     /* Show main page */
695     $smarty->assign("gotoKernelParameters",$this->gotoKernelParameters);
696     return($smarty->fetch (get_template_path('workstationStartup.tpl', TRUE,dirname(__FILE__))));
697   }
700   function remove_from_parent()
701   {
702     $this->handle_post_events("remove");
703     new log("remove","workstation/".get_class($this),$this->dn);
704   }
707   /* Save data to object */
708   function save_object()
709   {
710     $old_mirror  = $this->FAIdebianMirror;
711     plugin::save_object();
713     /* Update release */
714     if($old_mirror != $this->FAIdebianMirror){
715       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
716         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
717       }
718     }
720     if(isset($_POST['WorkstationStarttabPosted'])){
721       if(isset($_POST['gotoLdap_inherit'])){
722         $this->gotoLdap_inherit = TRUE;
723       }else{
724         $this->gotoLdap_inherit = FALSE;
725       }
727       /* Save group radio buttons */
728       if ($this->acl_is_writeable("bootmode") && isset($_POST["bootmode"])){
729         $this->bootmode= $_POST["bootmode"];
730       }
731     }
732   }
735   /* Save to LDAP */
736   function save()
737   {
739     /* Depending on the baseobject (Ogroup / WS) we
740      *  use another set of objectClasses
741      * In case of WS itself, we use  "array("GOhard", "FAIobject");"
742      * if we are currently editing from ogroup menu we use (array("gotWorkstationTemplate","GOhard", "FAIobject"))
743      */
744     if(isset($this->parent->by_object['ogroup'])){
745       $this->objectclasses = array("gotoWorkstationTemplate");
746     }elseif(isset($this->parent->by_object['workgeneric'])){
747       $this->objectclasses = array("GOhard");
748     }elseif(isset($this->parent->by_object['servgeneric'])){
749       $this->objectclasses = array("GOhard","gotoWorkstationTemplate");
750     }else{
751       msg_dialog::display(_("Fatal error"),
752           "Object Type Configuration is unknown. Please contact the GOsa developers.",
753           FATAL_ERROR_DIALOG);
754       exit();
755     }
757     /* Append FAI class */
758     if($this->fai_activated){
759       $this->objectclasses[]  = "FAIobject";
760     }
762     /* Find proper terminal path for tftp configuration
763        FIXME: This is suboptimal when the default has changed to
764        another location! */
765     if (($this->gotoTerminalPath == "default")){
766       $ldap= $this->config->get_ldap_link();
768       /* Strip relevant part from dn, keep trailing ',' */
769       $tmp= preg_replace("/^cn=[^,]+,".get_ou('terminalRDN')."/i", "", $this->dn);
770       $tmp= preg_replace("/".$this->config->current['BASE']."$/i", "", $tmp);
772       /* Walk from top to base and try to load default values for
773          'gotoTerminalPath'. Abort when an entry is found. */
774       while (TRUE){
775         $tmp= preg_replace ("/^[^,]+,/", "", $tmp);
777         $ldap->cat("cn=default,".get_ou('terminalRDN').$tmp.
778             $this->config->current['BASE'], array('gotoTerminalPath'));
779         $attrs= $ldap->fetch();
780         if (isset($attrs['gotoTerminalPath'])){
781           $this->gotoTerminalPath= $attrs['gotoTerminalPath'][0];
782           break;
783         }
785         /* Nothing left? */
786         if ($tmp == ""){
787           break;
788         }
789       }
790     }
792     plugin::save();
794     unset( $this->attrs['FAIrelease'] );
795     $str = "";
797     /* Skip FAI attribute handling if not necessary */
798     if($this->fai_activated && !$this->si_fai_action_failed){
799       if($this->FAIdebianMirror == "inherited"){
800         $this->attrs['FAIclass'] = $this->attrs['FAIrelease'] =  $this->attrs['FAIdebianMirror'] = array();
801       }else{
802         foreach($this->FAIclass as $class){
803           $str .= $class." ";
804         }
805         $str = trim($str);
806         if(empty($this->attrs['FAIclass'])){
807           $this->attrs['FAIclass'] = array();
808         }else{
809           $this->attrs['FAIclass']= $str." :".$this->FAIrelease;
810         }
811       }
812     }
814     /* Add missing arrays */
815     foreach (array("gotoFilesystem", "gotoAutoFs", "gotoModules") as $val){
816       if (isset ($this->$val) && count ($this->$val) != 0){
817     
818         $this->attrs["$val"]= array_unique($this->$val);
819       }
820       if(!isset($this->attrs["$val"])) $this->attrs["$val"]=array();
821     }
823     /* Prepare list of ldap servers */
824     $this->attrs['gotoLdapServer'] = array();
825     if(!$this->gotoLdap_inherit){
826       $i = 0;
827       foreach($this->gotoLdapServers as $server){
828         $i ++;
829         $this->attrs['gotoLdapServer'][] = $i.":".$server;
830       }
831     }
833     if ($this->attrs['gotoBootKernel'] == "default-inherited"){
834       $this->attrs['gotoBootKernel']= array();
835     }
837     /* if mirror == none stop saving this attribute */
838     if($this->FAIdebianMirror == "none"){
839       $this->FAIdebianMirror = "";
840     }
841    
842     /* Get FAIstate from object, the generic tab could have changed it during execute */
843     $ldap= $this->config->get_ldap_link();
844     $ldap->cd($this->dn);
847     /* Skip FAI attribute handling if not necessary */
848     if($this->fai_activated && !$this->si_fai_action_failed && $this->si_active){
849       $ldap->cat($this->dn,array("FAIstate"));
850       $checkFAIstate = $ldap->fetch();
852       /* Remove FAI objects if no FAI class is selected */ 
853       if((count($this->FAIclass)==0) && (!isset($checkFAIstate['FAIstate']))){
854         $this->attrs['FAIclass']        = array();
855         $this->attrs['FAIdebianMirror'] = array();
856       }
857     }else{
859       /* Don't touch FAI objects if something went wrong with the si daemon.
860        */
861       if(isset($this->attrs['FAIclass'])) unset($this->attrs['FAIclass']);
862       if(isset($this->attrs['FAIdebianMirror'])) unset($this->attrs['FAIdebianMirror']);
863     }
865     /* prepare share settings */
866     $tmp = array();
867     foreach($this->gotoShares as $name => $settings){
868       $tmp2= split("\|",$name);
869       $name = $tmp2[0];
870       $tmp[] = $settings['server']."|".$name."|".$settings['mountPoint'];
871     }
872     $this->attrs['gotoShare']=$tmp;
873     $this->cleanup();
874     $ldap->modify ($this->attrs); 
875     new log("modify","workstation/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
877     if (!$ldap->success()){
878       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, LDAP_MOD, get_class()));
879     }
880     $this->handle_post_events("modify");
882     /* Check if LDAP server has changed */
883     if ((isset($this->attrs['gotoLdapServer']) && class_available("DaemonEvent")) || $this->gotoLdap_inherit){
884       $events = DaemonEvent::get_event_types(SYSTEM_EVENT | HIDDEN_EVENT);
885       $o_queue = new gosaSupportDaemon();
886       if(isset($events['TRIGGERED']['DaemonEvent_reload_ldap_config'])){
887         $evt = $events['TRIGGERED']['DaemonEvent_reload_ldap_config'];
888         $macs = array();
889     
890         /* Get list of macAddresses 
891          */
892         if(isset($this->parent->by_object['ogroup'])){
893         
894           /* If we are an object group, add all member macs 
895            */
896           $p = $this->parent->by_object['ogroup'];
897           foreach($p->memberList as $dn => $obj){
898             if(isset($p->objcache[$dn]['macAddress']) && !empty($p->objcache[$dn]['macAddress'])){
899               $macs[] = $p->objcache[$dn]['macAddress'];
900             }
901           }
902         }elseif(isset($this->parent->by_object['workgeneric']->netConfigDNS->macAddress)){
904           /* We are a workstation. Add current mac.
905            */
906           $mac = $this->parent->by_object['workgeneric']->netConfigDNS->macAddress;
907           if(!empty($mac)){
908             $macs[] = $mac;
909           }          
910         }elseif(isset($this->parent->by_object['servgeneric']->netConfigDNS->macAddress)){
912           /* We are a server. Add current mac.
913            */
914           $mac = $this->parent->by_object['servgeneric']->netConfigDNS->macAddress;
915           if(!empty($mac)){
916             $macs[] = $mac;
917           }          
918         }
920         /* Trigger event for all member objects 
921          */
922         foreach($macs as $mac){
923           $tmp = new $evt['CLASS_NAME']($this->config);
924           $tmp->set_type(TRIGGERED_EVENT);
925           $tmp->add_targets(array($mac));
926           if(!$o_queue->append($tmp)){
927             msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
928           }
929         }
930       }
931     }
932   }
935   /* Add value to array, check if unique */
936   function add_list (&$array, $value)
937   {
938     if ($value != ""){
939       $array[]= $value;
940       sort($array);
941       array_unique ($array);
942     }
943   }
946   /* Delete value to array, check if unique */
947   function del_list (&$array, $list)
948   {
949     $tmp= array();
950     foreach ($array as $mod){
951       if (!in_array($mod, $list)){
952         $tmp[]= $mod;
953       }
954     }
955     $array= $tmp;
956   }
958   /* Generate ListBox frindly output for the defined shares
959    * Possibly Add or remove an attribute here,
960    */
961   function printOutAssignedShares()
962   {
963     $a_return = array();
964     if(is_array($this->gotoShares)){
965       foreach($this->gotoShares as $share){
966         $a_return[$share['name']."|".$share['server']]= $share['name']." [".$share['server']."]";
967       }
968     }
969     return($a_return);
970   }
974   function PrepareForCopyPaste($source)
975   {
976     plugin::PrepareForCopyPaste($source);    
977     $source_o = new workstartup ($this->config, $source['dn']);
978     foreach(array("FAIclass","gotoModules", "gotoAutoFs", "gotoFilesystem",
979           "gotoKernelParameters","gotoShares") as $attr){
980       $this->$attr = $source_o->$attr;
981     }
982   }
984   
985   function array_switch_item($ar,$from,$to)
986   {
987     if(!is_array($ar)){
988       return(false);
989     }
990     if(!isset($ar[$from])){
991       return(false);
992     }
993     if(!isset($ar[$to])){
994       return(false);
995     }
997     $tmp = $ar[$from];
998     $ar[$from] = $ar[$to];    
999     $ar[$to] = $tmp;    
1000     return($ar);
1001   }
1004   /* Return plugin informations for acl handling */ 
1005   static function plInfo()
1006   {
1007     return (array( 
1008           "plShortName"   => _("Startup"),
1009           "plDescription" => _("System startup"),
1010           "plSelfModify"  => FALSE,
1011           "plDepends"     => array(),
1012           "plPriority"    => 9,
1013           "plSection"     => array("administration"),           
1014           "plCategory"    => array("workstation","server","ogroups"),
1016           "plProvidedAcls"=> array(
1017             "gotoLdapServer"        => _("Ldap server"),
1018             "gotoBootKernel"        => _("Boot kernel"),
1019             "gotoKernelParameters"  => _("Kernel parameter"),
1021             "gotoModules"           => _("Kernel modules"),
1022             "gotoShare"             => _("Shares"),
1024             "FAIclass"              => _("FAI classes"),
1025             "FAIdebianMirror"       => _("Debian mirror"),
1026             "FAIrelease"            => _("Debian release"),
1028             "FAIstatus"             => _("FAI status flag")) // #FIXME is this acl realy necessary ?
1029           ));
1030   }
1033   /* Updates release dns 
1034    *  and reads all classes for the current release, 
1035    *  if not already done ($this->cache).
1036    */
1037   function update_fai_cache($first_call = FALSE)
1038   {
1039     $force = FALSE;
1040     if(!$this->si_active) return; 
1041     $start = microtime(TRUE);  
1043     if($this->si_fai_action_failed && !isset($_POST['fai_si_retry'])) return;
1045     $this->si_fai_action_failed = FALSE;
1047     /* Get the list of available servers and their releases. 
1048      */
1049     if($force || !isset($this->cache['SERVERS'])){
1051       $o_queue = new gosaSupportDaemon();
1052       $tmp = $o_queue->FAI_get_server();
1053       if($o_queue->is_error()){
1054         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1055         $this->si_fai_action_failed = TRUE;
1056         $this->cache = array();
1057         return;
1058       }else{
1060         foreach($tmp as $entry){
1061           $rel = $entry['FAI_RELEASE'];
1062           $this->cache['SERVERS']['auto'][$rel] = $rel;
1063           $this->cache['SERVERS'][$entry['SERVER']][$rel] = $rel;
1064           uksort($this->cache['SERVERS']['auto'], 'strnatcasecmp');
1065           uksort($this->cache['SERVERS'][$entry['SERVER']], 'strnatcasecmp');
1066         }
1067       }
1068     }
1070     /* Ensure that our selection is valid, else we get several PHP warnings 
1071         if there is no FAI configuration at all.
1072      */
1073     if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1074       $this->cache['SERVERS'][$this->FAIdebianMirror][''] ='';
1075     }
1077     /* Build up arrays, without checks */
1078     if(!$first_call){
1080       /* Check if the selected mirror is available */
1081       if(!isset($this->cache['SERVERS'][$this->FAIdebianMirror])){
1082         $this->FAIdebianMirror = "auto";
1083         $this->FAIrelease      = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1084         trigger_error("There was a problem with the selected FAIdebianMirror. This mirror ('".$this->FAIdebianMirror."') is not available");
1085       }
1087       /* Check if the selected release is available */
1088       if($this->FAIdebianMirror != "inherited" && !isset($this->cache['SERVERS'][$this->FAIdebianMirror][$this->FAIrelease])){
1090         if($this->FAIrelease != ""){
1091           trigger_error("There was a problem with the selected FAIrelease. This release ('".$this->FAIrelease."') is not available");
1092         }
1093         $this->FAIrelease = key($this->cache['SERVERS'][$this->FAIdebianMirror]);
1094       }
1095     }
1097     /* Get classes for release from cache. 
1098      * Or build cache
1099      */
1100     if($this->FAIdebianMirror == "inherited"){
1101       $release = $this->InheritedFAIrelease;
1102     }else{
1103       $release = $this->FAIrelease;
1104     }
1106     if($force || !isset($this->cache['CLASSES'][$release]) && $release != ""){
1108       /* Get the list of available servers and their releases.
1109        */
1110       $o_queue = new gosaSupportDaemon();
1111       $tmp = $o_queue->FAI_get_classes($release);
1113       $this->cache['CLASSES'][$release] = array();
1114       if($o_queue->is_error()){
1115         msg_dialog::display(_("Service infrastructure"),msgPool::siError($o_queue->get_error()),ERROR_DIALOG);
1116         $this->si_fai_action_failed = TRUE;
1117         $this->cache=array();
1118         return;
1119       }else{
1120         foreach($tmp as $entry){
1121           $class = $entry['CLASS'];
1122           $this->cache['CLASSES'][$release][$class][] = $this->analyse_fai_object($entry); 
1123         }
1124       }
1126       /* Add object caught from external hook
1127        */
1128       $lines= $this->GetHookElements();
1129       foreach ($lines as $hline){
1130         $entries= split(";", $hline);
1131         $server = $entries['0'];
1132         $url    = $entries['1'];
1133         if (!empty($url)){
1135           /* Split releases */
1136           if (isset($entries[2])){
1137             $releases= split(",", $entries[2]);
1139             foreach ($releases as $release_data){
1140               $release_c  = preg_replace('/:.*$/', '', $release_data);
1141               $sections_c = split(':', preg_replace('/^[^:]+:([^|]+)|.*$/', '\1', $release_data));
1142               $classes_c  = split('\|', preg_replace('/^[^|]+\|(.*)$/', '\1', $release_data));
1144               if($release_c == $release){
1145                 $this->cache['SERVERS'][$url][$release_c]=$release_c;
1146                 $this->cache['SERVERS']['auto'][$release_c]=$release_c; 
1147                 foreach ($classes_c as $class){
1148                   if ($class != ""){
1149                     $this->cache['CLASSES'][$release_c][$class]= array();
1150                   }
1151                 }
1152               }
1153             }
1154           }
1155         }
1156       }
1157       uksort($this->cache['SERVERS'], 'strnatcasecmp');
1159       /* Only add inherit option, if we are part in an object group
1160        */
1161       if($this->member_of_ogroup){
1162         $this->cache['SERVERS'] = array_merge(array('inherited' => array()),$this->cache['SERVERS']);
1163       }
1164     }
1166     /* Get list of available kernel for this release 
1167      */
1168     if(!isset($this->cache['KERNELS'])) $this->cache['KERNELS'] = array();
1170     if($force || !isset($this->cache['KERNELS'][$release])){
1171       $o_queue = new gosaSupportDaemon();
1172       $tmp = $o_queue->FAI_get_kernels($release);
1173       $this->cache['KERNELS'][$release] = array();
1174       foreach($this->gotoBootKernels as $name => $default){
1175         $this->cache['KERNELS'][$release][$name] = $default;
1176       }
1177       foreach($tmp as $kernel){
1178         if(empty($kernel)) continue;
1179         $this->cache['KERNELS'][$release][$kernel]=$kernel;
1180       }
1181       ksort($this->cache['KERNELS'][$release]);
1182     }
1183   }
1186   /* This function return an array containing all 
1187    *  invalid classes for the selected server/release
1188    */
1189   function get_invalid_classes($classes)
1190   {
1191     $this->update_fai_cache();
1192     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1193       $release_classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1194     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1195       $release_classes = $this->cache['CLASSES'][$this->FAIrelease];
1196     }else{
1197       $release_classes = array();
1198     }
1201     /* Detect all classes that are not valid 
1202      *  for the selected release 
1203      */
1204     $NA = array();
1205     foreach($classes as $class){
1206       if(!isset($release_classes[$class])){
1207         $NA[] = $class;
1208       }
1209     }
1210     return($NA);
1211   }  
1213   
1214   /* Get all selectable classes for the ui select box
1215    */
1216   function selectable_classes()
1217   {
1218     $this->update_fai_cache();
1220     if($this->FAIdebianMirror == "inherited" && isset($this->cache['CLASSES'][$this->InheritedFAIrelease])){
1221       $classes = $this->cache['CLASSES'][$this->InheritedFAIrelease];
1222     }elseif(isset($this->cache['CLASSES'][$this->FAIrelease])){
1223       $classes = $this->cache['CLASSES'][$this->FAIrelease];
1224     }else{
1225       $classes = array();
1226     }
1228     $Abbr ="";
1229     $ret= array();
1230     foreach($classes as $class_name => $class_types){
1231       if(!in_array($class_name,$this->FAIclass)){
1232         $Abbr = "";
1233         foreach($class_types as $type){
1234           if(!preg_match("/".$type['Abbr']."/",$Abbr)){
1235             $Abbr .= $type['Abbr']." ";
1236           }
1237         }
1238         $ret[$class_name] = trim($Abbr);
1239       }
1240     }
1241     uksort($ret, 'strnatcasecmp');
1242     return($ret);
1243   }
1246   /* Analyse FAI object and return an array with usefull informations like 
1247    *  FAIobject type.
1248    */
1249   function analyse_fai_object($attr)
1250   {
1251     $tmp = array();
1252     switch($attr['TYPE']){
1254       case 'FAIpackageList':
1255         $tmp["Type"]= 'FAIpackageList';
1256         $tmp["Abbr"]= 'Pl';
1257         break;
1258       case 'FAItemplate': 
1259         $tmp["Type"]= 'FAItemplate'; 
1260         $tmp["Abbr"]= 'T'; 
1261         break;
1262       case 'FAIvariable':
1263         $tmp["Type"]= 'FAIvariable'; 
1264         $tmp["Abbr"]= 'V'; 
1265         break;
1266       case 'FAIscript':
1267         $tmp["Type"]= 'FAIscript'; 
1268         $tmp["Abbr"]= 'S'; 
1269         break;
1270       case 'FAIhook':
1271         $tmp["Type"]= 'FAIhook'; 
1272         $tmp["Abbr"]= 'H'; 
1273         break;
1274       case 'FAIpartitionTable':
1275         $tmp["Type"]= 'FAIpartitionTable'; 
1276         $tmp["Abbr"]= 'Pt'; 
1277         break;
1278       case 'FAIprofile':
1279         $tmp["Type"]= 'FAIprofile'; 
1280         $tmp["Abbr"]= 'P'; 
1281         break;
1282       default: trigger_error("Unknown FAI object type!");;
1283     }
1284     return($tmp);
1285   }
1288   /* Return repository hook output, if possible.
1289    */
1290   function GetHookElements()
1291   {
1292     $ret = array();
1293     $cmd= $this->config->search("servrepository", "repositoryBranchHook",array('tabs'));
1294     if(!empty($cmd)){
1295       $res = shell_exec($cmd);
1296       $res2 = trim($res);
1297       if((!$res)){
1298         msg_dialog::display(_("Configuration error"), msgPool::cmdexecfailed("repositoryBranchHook", $cmd), ERROR_DIALOG);
1299       }elseif(empty($res2)){
1300         msg_dialog::display(_("Configuration error"), _("'repositoryBranchHook' returned no result!"), ERROR_DIALOG);
1301       }else{
1302         $tmp = split("\n",$res);
1303         foreach($tmp as $line){
1304           if(empty($line)) continue;
1305           $ret[]= $line;
1306         }
1307       }
1308     }
1309     return($ret);
1310   }
1313   /* This function creates the release name out of a dn 
1314    *  e.g. "ou=1.0rc2,ou=siga,ou=fai,..." => "siga/1.0rc2"
1315    */
1316   function dn_to_release_name($dn)
1317   {
1318     $relevant = preg_replace("/,".preg_quote(get_ou("faiBaseRDN"), '/').".*$/","",$dn);
1319     $parts    = array_reverse(split("\,",$relevant));
1320     $str ="";
1321     foreach($parts as $part){
1322       $str .= preg_replace("/^ou=/","",$part)."/";
1323     }
1324     return(preg_replace("/\/$/","",$str)); 
1325   }
1328 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1329 ?>