Code

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